Completed
Push — master ( 3655f7...92824b )
by
unknown
03:30 queued 12s
created

InitialisationMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 77
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configureEgressProxy() 0 13 3
A configureProxyDomainExclusions() 0 17 4
A process() 0 8 2
1
<?php
2
3
namespace CWP\Core\Control;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Control\Middleware\HTTPMiddleware;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\Core\Environment;
9
10
/**
11
 * Initialises CWP-specific configuration settings, to avoid _config.php.
12
 */
13
class InitialisationMiddleware implements HTTPMiddleware
14
{
15
    use Configurable;
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;
0 ignored issues
show
introduced by
The private property $egress_proxy_default_enabled is not used, and could be removed.
Loading history...
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 = [
0 ignored issues
show
introduced by
The private property $egress_proxy_exclude_domains is not used, and could be removed.
Loading history...
37
        'services.cwp.govt.nz',
38
        'localhost',
39
    ];
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();
47
48
        return $delegate($request);
49
    }
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
        }
62
63
        $proxy = Environment::getEnv('SS_OUTBOUND_PROXY');
64
        $proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT');
65
66
        Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort);
67
        Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort);
68
    }
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
        }
79
80
        if (!is_array($noProxy)) {
81
            $noProxy = [$noProxy];
82
        }
83
84
        // Merge with exsiting if needed.
85
        if (Environment::getEnv('NO_PROXY')) {
86
            $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy);
87
        }
88
89
        Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy)));
90
    }
91
}
92