sitehub 0.4.8 → 0.4.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.reek +1 -0
- data/Gemfile.lock +1 -1
- data/lib/sitehub/builder.rb +8 -11
- data/lib/sitehub/forward_proxy.rb +9 -17
- data/lib/sitehub/middleware/forward_proxies.rb +17 -14
- data/lib/sitehub/version.rb +1 -1
- data/spec/sitehub/builder_spec.rb +30 -8
- data/spec/sitehub/forward_proxy_spec.rb +2 -2
- data/spec/sitehub/middleware/forward_proxies_spec.rb +24 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc1b1f945c39176d1b996c7e1d874132686b2d11
|
4
|
+
data.tar.gz: 65e4926ceab0628a35a257a31c8a6f320501623c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87eb4057e4afb5766c060f8b976700d1764f6230412da1fcb0ef022deeefa816e4af4bf434c73ee82c232ae7fa0ef7f93975d965cc7dc7e727ec0c537353114e
|
7
|
+
data.tar.gz: e548bc84ed6d7f341f0909978bcc7d1e694115c421d24c8bff74fa5cdd254d83caab55c1d295eb389b4befaf02423e97f4b152bd05d7172b39fc4410a6be27fd
|
data/.reek
CHANGED
data/Gemfile.lock
CHANGED
data/lib/sitehub/builder.rb
CHANGED
@@ -22,11 +22,14 @@ class SiteHub
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def initialize(&block)
|
25
|
-
@forward_proxies = ForwardProxies.new
|
26
25
|
@reverse_proxies = {}
|
27
26
|
instance_eval(&block) if block
|
28
27
|
end
|
29
28
|
|
29
|
+
def forward_proxies
|
30
|
+
@forward_proxies ||= ForwardProxies.new(sitehub_cookie_name)
|
31
|
+
end
|
32
|
+
|
30
33
|
def build
|
31
34
|
forward_proxies.init
|
32
35
|
add_default_middleware
|
@@ -45,17 +48,11 @@ class SiteHub
|
|
45
48
|
end
|
46
49
|
|
47
50
|
def proxy(opts = {}, &block)
|
48
|
-
|
49
|
-
mapped_path, url = *opts.to_a.flatten
|
50
|
-
else
|
51
|
-
mapped_path = opts
|
52
|
-
url = nil
|
53
|
-
end
|
51
|
+
mapped_path, url = *(opts.respond_to?(:to_a) ? opts.to_a : [opts]).flatten
|
54
52
|
|
55
|
-
forward_proxies
|
56
|
-
|
57
|
-
|
58
|
-
&block)
|
53
|
+
forward_proxies.add_proxy(url: url,
|
54
|
+
mapped_path: mapped_path,
|
55
|
+
&block)
|
59
56
|
end
|
60
57
|
|
61
58
|
def reverse_proxy(hash)
|
@@ -1,9 +1,11 @@
|
|
1
1
|
# rubocop:disable Metrics/ParameterLists
|
2
2
|
class SiteHub
|
3
3
|
class ForwardProxy
|
4
|
-
include Rules, Resolver
|
4
|
+
include Rules, Resolver, Equality
|
5
5
|
|
6
|
-
attr_reader :downstream_client, :sitehub_cookie_name, :
|
6
|
+
attr_reader :downstream_client, :sitehub_cookie_name, :id, :sitehub_cookie_path, :mapped_path, :mapped_url
|
7
|
+
|
8
|
+
transient :downstream_client
|
7
9
|
|
8
10
|
def initialize(sitehub_cookie_path: nil, sitehub_cookie_name:, id:, rule: nil, mapped_path:, mapped_url:)
|
9
11
|
@downstream_client = DownstreamClient.new
|
@@ -19,25 +21,15 @@ class SiteHub
|
|
19
21
|
request = env[REQUEST]
|
20
22
|
request.map(mapped_path, mapped_url)
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
downstream_client.call(request).tap do |response|
|
25
|
+
response.set_cookie(sitehub_cookie_name,
|
26
|
+
path: resolve_sitehub_cookie_path(request),
|
27
|
+
value: id)
|
28
|
+
end
|
26
29
|
end
|
27
30
|
|
28
31
|
def resolve_sitehub_cookie_path(request)
|
29
32
|
sitehub_cookie_path || request.path
|
30
33
|
end
|
31
|
-
|
32
|
-
def ==(other)
|
33
|
-
expected = [other.mapped_path,
|
34
|
-
other.mapped_url,
|
35
|
-
other.rule,
|
36
|
-
other.id,
|
37
|
-
other.sitehub_cookie_name,
|
38
|
-
other.sitehub_cookie_path]
|
39
|
-
|
40
|
-
expected == [mapped_path, mapped_url, rule, id, sitehub_cookie_name, sitehub_cookie_path]
|
41
|
-
end
|
42
34
|
end
|
43
35
|
end
|
@@ -4,12 +4,20 @@ require 'rack/request'
|
|
4
4
|
require 'rack/response'
|
5
5
|
require 'rack/utils'
|
6
6
|
require 'em-http'
|
7
|
+
require 'forwardable'
|
7
8
|
|
8
9
|
class SiteHub
|
9
10
|
module Middleware
|
10
|
-
class ForwardProxies
|
11
|
+
class ForwardProxies < Hash
|
11
12
|
NIL_PROXY = NilProxy.new
|
12
13
|
|
14
|
+
attr_reader :sitehub_cookie_name
|
15
|
+
|
16
|
+
def initialize(sitehub_cookie_name)
|
17
|
+
@sitehub_cookie_name = sitehub_cookie_name
|
18
|
+
self.default = NIL_PROXY
|
19
|
+
end
|
20
|
+
|
13
21
|
def call(env)
|
14
22
|
source_request = Rack::Request.new(env)
|
15
23
|
|
@@ -19,20 +27,23 @@ class SiteHub
|
|
19
27
|
end
|
20
28
|
|
21
29
|
def init
|
22
|
-
|
30
|
+
values.each(&:build)
|
23
31
|
self
|
24
32
|
end
|
25
33
|
|
26
|
-
def
|
27
|
-
|
34
|
+
def add_proxy(url: nil, mapped_path:, &block)
|
35
|
+
self[mapped_path] = ForwardProxyBuilder.new(sitehub_cookie_name: sitehub_cookie_name,
|
36
|
+
url: url,
|
37
|
+
mapped_path: mapped_path,
|
38
|
+
&block)
|
28
39
|
end
|
29
40
|
|
30
41
|
def mapped_proxy(path:, request:)
|
31
|
-
|
42
|
+
self[mapping(path)].resolve(id: request.cookies[sitehub_cookie_name], env: request.env)
|
32
43
|
end
|
33
44
|
|
34
45
|
def mapping(path)
|
35
|
-
|
46
|
+
keys.find do |key|
|
36
47
|
case key
|
37
48
|
when Regexp
|
38
49
|
key.match(path)
|
@@ -41,14 +52,6 @@ class SiteHub
|
|
41
52
|
end
|
42
53
|
end
|
43
54
|
end
|
44
|
-
|
45
|
-
def forward_proxies
|
46
|
-
@forward_proxies ||= begin
|
47
|
-
{}.tap do |hash|
|
48
|
-
hash.default = NIL_PROXY
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
55
|
end
|
53
56
|
end
|
54
57
|
end
|
data/lib/sitehub/version.rb
CHANGED
@@ -28,7 +28,7 @@ class SiteHub
|
|
28
28
|
sitehub_cookie_name: RECORDED_ROUTES_COOKIE).tap do |route|
|
29
29
|
route.default(url: :endpoint)
|
30
30
|
end
|
31
|
-
expect(subject.forward_proxies
|
31
|
+
expect(subject.forward_proxies['/app1']).to eq(expected_proxy)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -47,7 +47,7 @@ class SiteHub
|
|
47
47
|
route.split url: :endpoint, percentage: 100, label: :label
|
48
48
|
end
|
49
49
|
|
50
|
-
expect(subject.forward_proxies
|
50
|
+
expect(subject.forward_proxies['/app'].endpoints).to eq(expected_route.endpoints)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
@@ -81,10 +81,18 @@ class SiteHub
|
|
81
81
|
expect(subject.sitehub_cookie_name).to eq(RECORDED_ROUTES_COOKIE)
|
82
82
|
end
|
83
83
|
|
84
|
-
|
85
|
-
subject
|
86
|
-
|
87
|
-
|
84
|
+
context 'forward_proxies' do
|
85
|
+
subject do
|
86
|
+
described_class.new do
|
87
|
+
sitehub_cookie_name :expected_cookie_name
|
88
|
+
proxy '/app1' => :endpoint
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'forwards it' do
|
93
|
+
proxy = subject.proxy '/app1' => :endpoint
|
94
|
+
expect(proxy.sitehub_cookie_name).to eq(:expected_cookie_name)
|
95
|
+
end
|
88
96
|
end
|
89
97
|
end
|
90
98
|
|
@@ -116,8 +124,22 @@ class SiteHub
|
|
116
124
|
expect(subject.build).to be_using(Middleware::TransactionId)
|
117
125
|
end
|
118
126
|
|
119
|
-
|
120
|
-
|
127
|
+
context 'forward proxies' do
|
128
|
+
subject do
|
129
|
+
described_class.new do
|
130
|
+
sitehub_cookie_name :custom_cookie_name
|
131
|
+
proxy '/app1' => :endpoint
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'adds a forward proxies' do
|
136
|
+
expect(subject.build).to be_using(Middleware::ForwardProxies)
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'configures it with the sitehub_cookie_name' do
|
140
|
+
forward_proxies = find_middleware(subject.build, Middleware::ForwardProxies)
|
141
|
+
expect(forward_proxies.sitehub_cookie_name).to eq(:custom_cookie_name)
|
142
|
+
end
|
121
143
|
end
|
122
144
|
|
123
145
|
it 'adds a AccessLogger' do
|
@@ -51,8 +51,8 @@ class SiteHub
|
|
51
51
|
context 'cookie already set' do
|
52
52
|
let(:rack_headers) { { 'HTTP_COOKIE' => 'cookie_name=existing_value' } }
|
53
53
|
|
54
|
-
it '
|
55
|
-
expect(last_response.cookies[:cookie_name.to_s]).to eq(value: :
|
54
|
+
it 'replaces the value as this is the proxy it should stick with' do
|
55
|
+
expect(last_response.cookies[:cookie_name.to_s]).to eq(value: :id.to_s, path: mapped_path)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -5,15 +5,17 @@ class SiteHub
|
|
5
5
|
describe ForwardProxies do
|
6
6
|
let(:base_url) { 'http://google.com' }
|
7
7
|
let(:application_root) { '/application_url' }
|
8
|
+
|
8
9
|
let(:forward_proxy_builder) do
|
9
|
-
|
10
|
-
builder.split url: base_url, label: :current, percentage: 100
|
11
|
-
end
|
10
|
+
subject.values.first
|
12
11
|
end
|
13
12
|
|
14
13
|
subject do
|
15
|
-
|
16
|
-
|
14
|
+
base_url = base_url()
|
15
|
+
described_class.new(:cookie_name).tap do |route_set|
|
16
|
+
route_set.add_proxy(mapped_path: application_root) do |builder|
|
17
|
+
builder.split url: base_url, label: :current, percentage: 100
|
18
|
+
end
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
@@ -23,24 +25,28 @@ class SiteHub
|
|
23
25
|
|
24
26
|
describe '#init' do
|
25
27
|
it 'builds all of the forward_proxies' do
|
26
|
-
expect(subject
|
28
|
+
expect(subject[application_root]).to receive(:build).and_call_original
|
27
29
|
subject.init
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
31
33
|
describe '#mapped_route' do
|
32
34
|
let(:request) { Rack::Request.new({}) }
|
33
|
-
|
34
|
-
|
35
|
+
|
36
|
+
it 'uses the id in the sitehub_cookie to resolve the correct route' do
|
37
|
+
request.cookies[:cookie_name] = :preset_id
|
38
|
+
expect(forward_proxy_builder).to receive(:resolve).with(id: :preset_id, env: request.env).and_call_original
|
39
|
+
subject.mapped_proxy(path: application_root, request: request)
|
35
40
|
end
|
36
41
|
|
37
42
|
context 'regex match on path' do
|
38
43
|
let(:fuzzy_matcher) do
|
39
|
-
|
44
|
+
subject.values.first
|
40
45
|
end
|
46
|
+
|
41
47
|
subject do
|
42
|
-
described_class.new.tap do |route_set|
|
43
|
-
route_set
|
48
|
+
described_class.new(:cookie_name).tap do |route_set|
|
49
|
+
route_set.add_proxy url: "#{base_url}/$1/view", mapped_path: %r{#{application_root}/(.*)/view}
|
44
50
|
end
|
45
51
|
end
|
46
52
|
|
@@ -60,10 +66,14 @@ class SiteHub
|
|
60
66
|
end
|
61
67
|
|
62
68
|
context 'when more specific route is configured first' do
|
69
|
+
let(:more_specific_proxy_builder) do
|
70
|
+
subject.values.first
|
71
|
+
end
|
72
|
+
|
63
73
|
subject do
|
64
|
-
described_class.new.tap do |route_set|
|
65
|
-
route_set
|
66
|
-
route_set
|
74
|
+
described_class.new(:cookie_name).tap do |route_set|
|
75
|
+
route_set.add_proxy(url: "#{base_url}/sub_url", mapped_path: "#{application_root}/sub_url")
|
76
|
+
route_set.add_proxy(mapped_path: application_root, url: base_url)
|
67
77
|
end
|
68
78
|
end
|
69
79
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sitehub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ladtech
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|