TestConfiguration::getGlobalConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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