@@ -12,80 +12,80 @@ |
||
12 | 12 | */ |
13 | 13 | class InitialisationMiddleware implements HTTPMiddleware |
14 | 14 | { |
15 | - use Configurable; |
|
15 | + use Configurable; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Enable egress proxy. This works on the principle of setting http(s)_proxy environment variables, |
|
19 | - * which will be automatically picked up by curl. This means RestfulService and raw curl |
|
20 | - * requests should work out of the box. Stream-based requests need extra manual configuration. |
|
21 | - * Refer to https://www.cwp.govt.nz/guides/core-technical-documentation/common-web-platform-core/en/how-tos/external_http_requests_with_proxy |
|
22 | - * |
|
23 | - * @config |
|
24 | - * @var bool |
|
25 | - */ |
|
26 | - private static $egress_proxy_default_enabled = true; |
|
17 | + /** |
|
18 | + * Enable egress proxy. This works on the principle of setting http(s)_proxy environment variables, |
|
19 | + * which will be automatically picked up by curl. This means RestfulService and raw curl |
|
20 | + * requests should work out of the box. Stream-based requests need extra manual configuration. |
|
21 | + * Refer to https://www.cwp.govt.nz/guides/core-technical-documentation/common-web-platform-core/en/how-tos/external_http_requests_with_proxy |
|
22 | + * |
|
23 | + * @config |
|
24 | + * @var bool |
|
25 | + */ |
|
26 | + private static $egress_proxy_default_enabled = true; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Configure the list of domains to bypass proxy by setting the NO_PROXY environment variable. |
|
30 | - * 'services.cwp.govt.nz' needs to be present for Solr and Docvert internal CWP integration. |
|
31 | - * 'localhost' is necessary for accessing services on the same instance such as tika-server for text extraction. |
|
32 | - * |
|
33 | - * @config |
|
34 | - * @var string[] |
|
35 | - */ |
|
36 | - private static $egress_proxy_exclude_domains = [ |
|
37 | - 'services.cwp.govt.nz', |
|
38 | - 'localhost', |
|
39 | - ]; |
|
28 | + /** |
|
29 | + * Configure the list of domains to bypass proxy by setting the NO_PROXY environment variable. |
|
30 | + * 'services.cwp.govt.nz' needs to be present for Solr and Docvert internal CWP integration. |
|
31 | + * 'localhost' is necessary for accessing services on the same instance such as tika-server for text extraction. |
|
32 | + * |
|
33 | + * @config |
|
34 | + * @var string[] |
|
35 | + */ |
|
36 | + private static $egress_proxy_exclude_domains = [ |
|
37 | + 'services.cwp.govt.nz', |
|
38 | + 'localhost', |
|
39 | + ]; |
|
40 | 40 | |
41 | - public function process(HTTPRequest $request, callable $delegate) |
|
42 | - { |
|
43 | - if ($this->config()->get('egress_proxy_default_enabled')) { |
|
44 | - $this->configureEgressProxy(); |
|
45 | - } |
|
46 | - $this->configureProxyDomainExclusions(); |
|
41 | + public function process(HTTPRequest $request, callable $delegate) |
|
42 | + { |
|
43 | + if ($this->config()->get('egress_proxy_default_enabled')) { |
|
44 | + $this->configureEgressProxy(); |
|
45 | + } |
|
46 | + $this->configureProxyDomainExclusions(); |
|
47 | 47 | |
48 | - return $delegate($request); |
|
49 | - } |
|
48 | + return $delegate($request); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * If the outbound egress proxy details have been defined in environment variables, configure the proxy |
|
53 | - * variables that are used to configure it. |
|
54 | - */ |
|
55 | - protected function configureEgressProxy() |
|
56 | - { |
|
57 | - if (!Environment::getEnv('SS_OUTBOUND_PROXY') |
|
58 | - || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
|
59 | - ) { |
|
60 | - return; |
|
61 | - } |
|
51 | + /** |
|
52 | + * If the outbound egress proxy details have been defined in environment variables, configure the proxy |
|
53 | + * variables that are used to configure it. |
|
54 | + */ |
|
55 | + protected function configureEgressProxy() |
|
56 | + { |
|
57 | + if (!Environment::getEnv('SS_OUTBOUND_PROXY') |
|
58 | + || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
|
59 | + ) { |
|
60 | + return; |
|
61 | + } |
|
62 | 62 | |
63 | - $proxy = Environment::getEnv('SS_OUTBOUND_PROXY'); |
|
64 | - $proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
63 | + $proxy = Environment::getEnv('SS_OUTBOUND_PROXY'); |
|
64 | + $proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
65 | 65 | |
66 | - Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort); |
|
67 | - Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort); |
|
68 | - } |
|
66 | + Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort); |
|
67 | + Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Configure any domains that should be excluded from egress proxy rules and provide them to the environment |
|
72 | - */ |
|
73 | - protected function configureProxyDomainExclusions() |
|
74 | - { |
|
75 | - $noProxy = $this->config()->get('egress_proxy_exclude_domains'); |
|
76 | - if (empty($noProxy)) { |
|
77 | - return; |
|
78 | - } |
|
70 | + /** |
|
71 | + * Configure any domains that should be excluded from egress proxy rules and provide them to the environment |
|
72 | + */ |
|
73 | + protected function configureProxyDomainExclusions() |
|
74 | + { |
|
75 | + $noProxy = $this->config()->get('egress_proxy_exclude_domains'); |
|
76 | + if (empty($noProxy)) { |
|
77 | + return; |
|
78 | + } |
|
79 | 79 | |
80 | - if (!is_array($noProxy)) { |
|
81 | - $noProxy = [$noProxy]; |
|
82 | - } |
|
80 | + if (!is_array($noProxy)) { |
|
81 | + $noProxy = [$noProxy]; |
|
82 | + } |
|
83 | 83 | |
84 | - // Merge with exsiting if needed. |
|
85 | - if (Environment::getEnv('NO_PROXY')) { |
|
86 | - $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); |
|
87 | - } |
|
84 | + // Merge with exsiting if needed. |
|
85 | + if (Environment::getEnv('NO_PROXY')) { |
|
86 | + $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); |
|
87 | + } |
|
88 | 88 | |
89 | - Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy))); |
|
90 | - } |
|
89 | + Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy))); |
|
90 | + } |
|
91 | 91 | } |
@@ -10,91 +10,91 @@ |
||
10 | 10 | |
11 | 11 | class InitialisationMiddlewareTest extends SapphireTest |
12 | 12 | { |
13 | - /** |
|
14 | - * @var HTTPRequest |
|
15 | - */ |
|
16 | - protected $request; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var InitialisationMiddleware |
|
20 | - */ |
|
21 | - protected $middleware; |
|
22 | - |
|
23 | - protected function setUp() |
|
24 | - { |
|
25 | - parent::setUp(); |
|
26 | - |
|
27 | - $this->request = new HTTPRequest('GET', '/'); |
|
28 | - $this->middleware = new InitialisationMiddleware(); |
|
29 | - |
|
30 | - Environment::setEnv('SS_OUTBOUND_PROXY', ''); |
|
31 | - Environment::setEnv('SS_OUTBOUND_PROXY_PORT', ''); |
|
32 | - Environment::setEnv('NO_PROXY', ''); |
|
33 | - } |
|
34 | - |
|
35 | - public function testDoNotConfigureProxyIfNoEnvironmentVarsAreSet() |
|
36 | - { |
|
37 | - $this->runMiddleware(); |
|
38 | - |
|
39 | - $this->assertEmpty( |
|
40 | - Environment::getEnv('http_proxy'), |
|
41 | - 'Proxy information is not set if no outbound proxy is configured' |
|
42 | - ); |
|
43 | - } |
|
44 | - |
|
45 | - public function testConfigureEgressProxyWhenVarsAreSet() |
|
46 | - { |
|
47 | - Environment::setEnv('SS_OUTBOUND_PROXY', 'http://example.com'); |
|
48 | - Environment::setEnv('SS_OUTBOUND_PROXY_PORT', '8024'); |
|
49 | - |
|
50 | - $this->runMiddleware(); |
|
51 | - |
|
52 | - $this->assertEquals( |
|
53 | - 'http://example.com:8024', |
|
54 | - Environment::getEnv('http_proxy'), |
|
55 | - 'Proxy is configured with proxy and port' |
|
56 | - ); |
|
57 | - } |
|
58 | - |
|
59 | - public function testDoNotConfigureProxyDomainExclusionsWhenNoneAreDefined() |
|
60 | - { |
|
61 | - Config::modify()->remove(InitialisationMiddleware::class, 'egress_proxy_exclude_domains'); |
|
62 | - |
|
63 | - $this->runMiddleware(); |
|
64 | - |
|
65 | - $this->assertSame( |
|
66 | - '', |
|
67 | - Environment::getEnv('NO_PROXY'), |
|
68 | - 'No domain exclusions are set when none are defined' |
|
69 | - ); |
|
70 | - } |
|
71 | - |
|
72 | - public function testConfigureEgressProxyDomainExclusions() |
|
73 | - { |
|
74 | - Config::modify()->set( |
|
75 | - InitialisationMiddleware::class, |
|
76 | - 'egress_proxy_exclude_domains', |
|
77 | - 'example.com' |
|
78 | - ); |
|
79 | - |
|
80 | - Environment::setEnv('NO_PROXY', 'foo.com,bar.com'); |
|
81 | - |
|
82 | - $this->runMiddleware(); |
|
83 | - |
|
84 | - $this->assertSame( |
|
85 | - 'foo.com,bar.com,example.com', |
|
86 | - Environment::getEnv('NO_PROXY'), |
|
87 | - 'Domain exclusions are combined with existing values and configuration settings' |
|
88 | - ); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Runs the middleware with a stubbed delegate |
|
93 | - */ |
|
94 | - protected function runMiddleware() |
|
95 | - { |
|
96 | - $this->middleware->process($this->request, function () { |
|
97 | - // no op |
|
98 | - }); |
|
99 | - } |
|
13 | + /** |
|
14 | + * @var HTTPRequest |
|
15 | + */ |
|
16 | + protected $request; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var InitialisationMiddleware |
|
20 | + */ |
|
21 | + protected $middleware; |
|
22 | + |
|
23 | + protected function setUp() |
|
24 | + { |
|
25 | + parent::setUp(); |
|
26 | + |
|
27 | + $this->request = new HTTPRequest('GET', '/'); |
|
28 | + $this->middleware = new InitialisationMiddleware(); |
|
29 | + |
|
30 | + Environment::setEnv('SS_OUTBOUND_PROXY', ''); |
|
31 | + Environment::setEnv('SS_OUTBOUND_PROXY_PORT', ''); |
|
32 | + Environment::setEnv('NO_PROXY', ''); |
|
33 | + } |
|
34 | + |
|
35 | + public function testDoNotConfigureProxyIfNoEnvironmentVarsAreSet() |
|
36 | + { |
|
37 | + $this->runMiddleware(); |
|
38 | + |
|
39 | + $this->assertEmpty( |
|
40 | + Environment::getEnv('http_proxy'), |
|
41 | + 'Proxy information is not set if no outbound proxy is configured' |
|
42 | + ); |
|
43 | + } |
|
44 | + |
|
45 | + public function testConfigureEgressProxyWhenVarsAreSet() |
|
46 | + { |
|
47 | + Environment::setEnv('SS_OUTBOUND_PROXY', 'http://example.com'); |
|
48 | + Environment::setEnv('SS_OUTBOUND_PROXY_PORT', '8024'); |
|
49 | + |
|
50 | + $this->runMiddleware(); |
|
51 | + |
|
52 | + $this->assertEquals( |
|
53 | + 'http://example.com:8024', |
|
54 | + Environment::getEnv('http_proxy'), |
|
55 | + 'Proxy is configured with proxy and port' |
|
56 | + ); |
|
57 | + } |
|
58 | + |
|
59 | + public function testDoNotConfigureProxyDomainExclusionsWhenNoneAreDefined() |
|
60 | + { |
|
61 | + Config::modify()->remove(InitialisationMiddleware::class, 'egress_proxy_exclude_domains'); |
|
62 | + |
|
63 | + $this->runMiddleware(); |
|
64 | + |
|
65 | + $this->assertSame( |
|
66 | + '', |
|
67 | + Environment::getEnv('NO_PROXY'), |
|
68 | + 'No domain exclusions are set when none are defined' |
|
69 | + ); |
|
70 | + } |
|
71 | + |
|
72 | + public function testConfigureEgressProxyDomainExclusions() |
|
73 | + { |
|
74 | + Config::modify()->set( |
|
75 | + InitialisationMiddleware::class, |
|
76 | + 'egress_proxy_exclude_domains', |
|
77 | + 'example.com' |
|
78 | + ); |
|
79 | + |
|
80 | + Environment::setEnv('NO_PROXY', 'foo.com,bar.com'); |
|
81 | + |
|
82 | + $this->runMiddleware(); |
|
83 | + |
|
84 | + $this->assertSame( |
|
85 | + 'foo.com,bar.com,example.com', |
|
86 | + Environment::getEnv('NO_PROXY'), |
|
87 | + 'Domain exclusions are combined with existing values and configuration settings' |
|
88 | + ); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Runs the middleware with a stubbed delegate |
|
93 | + */ |
|
94 | + protected function runMiddleware() |
|
95 | + { |
|
96 | + $this->middleware->process($this->request, function () { |
|
97 | + // no op |
|
98 | + }); |
|
99 | + } |
|
100 | 100 | } |
@@ -93,7 +93,7 @@ |
||
93 | 93 | */ |
94 | 94 | protected function runMiddleware() |
95 | 95 | { |
96 | - $this->middleware->process($this->request, function () { |
|
96 | + $this->middleware->process($this->request, function() { |
|
97 | 97 | // no op |
98 | 98 | }); |
99 | 99 | } |