Passed
Branch master (4b23d6)
by Tim
04:40
created

TestConfiguration::getAvailableApacheModules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SimpleSAML\Module\Monitor;
4
5
use SimpleSAML\Configuration;
6
use SimpleSAML\Metadata\MetaDataStorageSource;
7
use SimpleSAML\Module\Monitor\DependencyInjection;
8
9
final class TestConfiguration
10
{
11
    /** @var \SimpleSAML\Configuration */
12
    private $globalConfig;
13
14
    /** @var \SimpleSAML\Configuration */
15
    private $moduleConfig;
16
17
    /** @var \SimpleSAML\Configuration */
18
    private $authSourceConfig;
19
20
    /** @var array */
21
    private $metadataConfig;
22
23
    /** @var array */
24
    private $availableApacheModules;
25
26
    /** @var array */
27
    private $availablePhpModules;
28
29
    /** @var \SimpleSAML\Module\Monitor\DependencyInjection */
30
    private $serverVars;
31
32
    /** @var \SimpleSAML\Module\Monitor\DependencyInjection */
33
    private $requestVars;
34
35
36
    /**
37
     * @param \SimpleSAML\Module\Monitor\DependencyInjection $serverVars
38
     * @param \SimpleSAML\Module\Monitor\DependencyInjection $requestVars
39
     * @param \SimpleSAML\Configuration $globalConfig
40
     * @param \SimpleSAML\Configuration $authSourceConfig
41
     * @param \SimpleSAML\Configuration $moduleConfig
42
     */
43
    public function __construct(
44
        DependencyInjection $serverVars,
45
        DependencyInjection $requestVars,
46
        Configuration $globalConfig,
47
        Configuration $authSourceConfig,
48
        Configuration $moduleConfig
49
    ) {
50
        $this->serverVars = $serverVars;
51
        $this->requestVars = $requestVars;
52
53
        $this->setAuthsourceConfig($authSourceConfig);
54
        $this->setModuleConfig($moduleConfig);
55
        $this->setGlobalConfig($globalConfig);
56
        $this->setMetadataConfig();
57
        $this->setAvailableApacheModules();
58
        $this->setAvailablePhpModules();
59
    }
60
61
62
    /**
63
     * @param \SimpleSAML\Configuration $authSourceConfig
64
     *
65
     * @return void
66
     */
67
    private function setAuthsourceConfig(Configuration $authSourceConfig): void
68
    {
69
        $this->authSourceConfig = $authSourceConfig;
70
    }
71
72
73
    /**
74
     * @param \SimpleSAML\Configuration $moduleConfig
75
     *
76
     * @return void
77
     */
78
    private function setModuleConfig(Configuration $moduleConfig): void
79
    {
80
        $this->moduleConfig = $moduleConfig;
81
    }
82
83
84
    /**
85
     * @param \SimpleSAML\Configuration $globalConfig
86
     *
87
     * @return void
88
     */
89
    private function setGlobalConfig(Configuration $globalConfig): void
90
    {
91
        $this->globalConfig = $globalConfig;
92
    }
93
94
95
    /**
96
     * @return void
97
     */
98
    private function setMetadataConfig(): void
99
    {
100
        $sets = $this->getAvailableMetadataSets();
101
        $sources = $this->globalConfig->getValue('metadata.sources');
102
        $handlers = MetaDataStorageSource::parseSources($sources);
103
        $metadata = [];
104
        if (!empty($sets)) {
105
            foreach ($handlers as $handler) {
106
                foreach ($sets as $set) {
107
                    $metadata[$set] = $handler->getMetadataSet($set);
108
                }
109
            }
110
        }
111
        assert(is_array($metadata));
112
        $this->metadataConfig = $metadata;
113
    }
114
115
116
    /**
117
     * @return array
118
     */
119
    protected function getAvailableMetadataSets(): array
120
    {
121
        $globalConfig = $this->getGlobalConfig();
122
        $sets = ['shib13-idp-remote', 'saml20-idp-remote'];
123
        if ($globalConfig->getBoolean('enable.saml20-idp', false)) {
124
            $sets = array_merge($sets, ['saml20-idp-hosted', 'saml20-sp-remote']);
125
        }
126
        if ($globalConfig->getBoolean('enable.shib13-idp', false)) {
127
            $sets = array_merge($sets, ['shib13-idp-hosted', 'shib13-sp-hosted', 'shib13-sp-remote']);
128
        }
129
        if ($globalConfig->getBoolean('enable.adfs-idp', false)) {
130
            $sets = array_merge($sets, ['adfs-idp-hosted', 'adfs-sp-remote']);
131
        }
132
        if ($globalConfig->getBoolean('enable.wsfed-sp', false)) {
133
            $sets = array_merge($sets, ['wsfed-sp-hosted', 'wsfed-idp-remote']);
134
        }
135
        return $sets;
136
    }
137
138
139
    /**
140
     * @return void
141
     */
142
    private function setAvailableApacheModules(): void
143
    {
144
        // Determine available Apache-modules
145
        if (function_exists('apache_get_modules')) {
146
            $this->availableApacheModules = apache_get_modules();
147
        } else { // CGI-mode
148
            $this->availableApacheModules = $this->getAvailableApacheModulesCgi();
149
        }
150
    }
151
152
153
    /**
154
     * @return array
155
     */
156
    private function getAvailableApacheModulesCgi(): array
157
    {
158
        $knownLocations = [
159
            '/usr/sbin/httpd',
160
            '/usr/sbin/apache2',
161
            '/opt/rh/httpd24/root/usr/sbin/httpd'
162
        ];
163
164
        $output = null;
165
        foreach ($knownLocations as $location) {
166
            if (file_exists($location)) {
167
                exec("$location -t -D DUMP_MODULES", $output);
168
                break;
169
            }
170
        }
171
172
        if ($output === null) {
173
            return []; // Cannot determine available modules
174
        }
175
        array_shift($output);
176
177
        $modules = [];
178
        foreach ($output as $module) {
179
            $module = ltrim($module);
180
            if (($res = preg_replace('/(_module \((shared|static)\))/', '', $module)) !== $module) {
181
                $modules[] = 'mod_' . $res;
182
            } // else skip
183
        }
184
        return $modules;
185
    }
186
187
188
    /**
189
     * @return void
190
     */
191
    private function setAvailablePhpModules(): void
192
    {
193
        $this->availablePhpModules = array_merge(get_loaded_extensions(), get_loaded_extensions(true));
194
    }
195
196
197
    /**
198
     * @return array
199
     */
200
    public function getAvailableApacheModules(): array
201
    {
202
        return $this->availableApacheModules;
203
    }
204
205
206
    /**
207
     * @return array
208
     */
209
    public function getAvailablePhpModules(): array
210
    {
211
        return $this->availablePhpModules;
212
    }
213
214
215
    /**
216
     * @return \SimpleSAML\Module\Monitor\DependencyInjection
217
     */
218
    public function getServerVars(): DependencyInjection
219
    {
220
        return $this->serverVars;
221
    }
222
223
224
    /**
225
     * @return \SimpleSAML\Module\Monitor\DependencyInjection
226
     */
227
    public function getRequestVars(): DependencyInjection
228
    {
229
        return $this->requestVars;
230
    }
231
232
233
    /**
234
     * @return \SimpleSAML\Configuration
235
     */
236
    public function getGlobalConfig(): Configuration
237
    {
238
        return $this->globalConfig;
239
    }
240
241
242
    /**
243
     * @return \SimpleSAML\Configuration
244
     */
245
    public function getModuleConfig(): Configuration
246
    {
247
        return $this->moduleConfig;
248
    }
249
250
251
    /**
252
     * @return \SimpleSAML\Configuration
253
     */
254
    public function getAuthSourceConfig(): Configuration
255
    {
256
        return $this->authSourceConfig;
257
    }
258
259
260
    /**
261
     * @return array
262
     */
263
    public function getMetadataConfig(): array
264
    {
265
        return $this->metadataConfig;
266
    }
267
}
268