1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\monitor\TestSuite\AuthSource; |
4
|
|
|
|
5
|
|
|
use SimpleSAML\Assert\Assert; |
|
|
|
|
6
|
|
|
use SimpleSAML\Configuration; |
7
|
|
|
use SimpleSAML\Module\monitor\State; |
|
|
|
|
8
|
|
|
use SimpleSAML\Module\monitor\TestConfiguration; |
|
|
|
|
9
|
|
|
use SimpleSAML\Module\monitor\TestCase; |
|
|
|
|
10
|
|
|
use SimpleSAML\Module\monitor\TestData; |
|
|
|
|
11
|
|
|
use SimpleSAML\Module\monitor\TestResult; |
|
|
|
|
12
|
|
|
|
13
|
|
|
use function array_key_exists; |
14
|
|
|
use function array_replace; |
15
|
|
|
use function explode; |
16
|
|
|
use function is_null; |
17
|
|
|
use function parse_url; |
18
|
|
|
use function preg_match; |
19
|
|
|
use function preg_split; |
20
|
|
|
use function stream_context_create; |
21
|
|
|
|
22
|
|
|
final class Ldap extends \SimpleSAML\Module\monitor\TestSuiteFactory |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** @var ApplicationConfiguration */ |
|
|
|
|
25
|
|
|
private $authSourceData; |
26
|
|
|
|
27
|
|
|
/** @var array|null */ |
28
|
|
|
private $authSourceSpecifics; |
29
|
|
|
|
30
|
|
|
/** @var string[] */ |
31
|
|
|
private $hosts; |
32
|
|
|
|
33
|
|
|
/** @var integer|null */ |
34
|
|
|
private $certExpirationWarning = null; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param TestConfiguration $configuration |
39
|
|
|
* @param TestData $testData |
40
|
|
|
*/ |
41
|
|
|
public function __construct(TestConfiguration $configuration, TestData $testData) |
42
|
|
|
{ |
43
|
|
|
$moduleConfig = $configuration->getModuleConfig(); |
44
|
|
|
$authSourceData = $testData->getInputItem('authSourceData'); |
45
|
|
|
$authSourceSpecifics = $testData->getInputItem('authSourceSpecifics'); |
46
|
|
|
|
47
|
|
|
assert(is_array($authSourceData)); |
48
|
|
|
assert(is_array($authSourceSpecifics) || is_null($authSourceSpecifics)); |
49
|
|
|
|
50
|
|
|
$authSourceData = Configuration::loadFromArray($authSourceData); |
51
|
|
|
$this->hosts = preg_split('/\s+/', $authSourceData->getString('hostname'), -1, PREG_SPLIT_NO_EMPTY); |
52
|
|
|
$this->authSourceData = $authSourceData; |
|
|
|
|
53
|
|
|
$this->authSourceSpecifics = $authSourceSpecifics; |
54
|
|
|
$this->certExpirationWarning = $moduleConfig->getValue('certExpirationWarning', 28); |
55
|
|
|
$this->setCategory('LDAP authentication source'); |
56
|
|
|
|
57
|
|
|
parent::__construct($configuration); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function invokeTest() |
65
|
|
|
{ |
66
|
|
|
// Test LDAP configuration |
67
|
|
|
$confTest = new TestCase\AuthSource\Ldap\Configuration( |
|
|
|
|
68
|
|
|
new TestData(['authSourceData' => $this->authSourceData]) |
69
|
|
|
); |
70
|
|
|
$confTestResult = $confTest->getTestResult(); |
71
|
|
|
$this->addTestResult($confTestResult); |
72
|
|
|
|
73
|
|
|
if ($confTestResult->getState() === State::OK) { |
74
|
|
|
$connection = $confTestResult->getOutput('connection'); |
75
|
|
|
|
76
|
|
|
// Test connection for each configured LDAP-server |
77
|
|
|
$failure = count($this->hosts); |
78
|
|
|
foreach ($this->hosts as $hostname) { |
79
|
|
|
$preparedTestData = $this->prepareConnection($hostname); |
80
|
|
|
$connTest = new TestCase\Network\ConnectUri( |
|
|
|
|
81
|
|
|
new TestData($preparedTestData) |
82
|
|
|
); |
83
|
|
|
$connTestResult = $connTest->getTestResult(); |
84
|
|
|
$this->addTestResult($connTestResult); |
85
|
|
|
|
86
|
|
|
if ($connTestResult->getState() === State::OK) { |
87
|
|
|
$certData = $connTestResult->getOutput('certData'); |
88
|
|
|
|
89
|
|
|
// Test certificate when available |
90
|
|
|
if ($certData !== null) { |
91
|
|
|
$certTest = new TestCase\Cert( |
|
|
|
|
92
|
|
|
new TestData([ |
93
|
|
|
'certData' => $certData, |
94
|
|
|
'category' => 'LDAP Server Certificate', |
95
|
|
|
'certExpirationWarning' => $this->certExpirationWarning, |
96
|
|
|
]) |
97
|
|
|
); |
98
|
|
|
$certTestResult = $certTest->getTestResult(); |
99
|
|
|
$this->addTestResult($certTestResult); |
100
|
|
|
} |
101
|
|
|
$failure--; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($failure === 0) { |
106
|
|
|
// Test bind |
107
|
|
|
$bindTest = new TestCase\AuthSource\Ldap\Bind( |
|
|
|
|
108
|
|
|
new TestData([ |
109
|
|
|
'authSourceData' => $this->authSourceData, |
110
|
|
|
'connection' => $connection |
111
|
|
|
]) |
112
|
|
|
); |
113
|
|
|
$bindTestResult = $bindTest->getTestResult(); |
114
|
|
|
$this->addTestResult($bindTestResult); |
115
|
|
|
|
116
|
|
|
if ($bindTestResult->getState() === State::OK) { |
117
|
|
|
// Test search |
118
|
|
|
$searchTest = new TestCase\AuthSource\Ldap\Search( |
|
|
|
|
119
|
|
|
new TestData([ |
120
|
|
|
'authSourceData' => $this->authSourceData, |
121
|
|
|
'connection' => $connection |
122
|
|
|
]) |
123
|
|
|
); |
124
|
|
|
$searchTestResult = $searchTest->getTestResult(); |
125
|
|
|
$this->addTestResult($searchTestResult); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
unset($connection); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$state = $this->calculateState(); |
132
|
|
|
|
133
|
|
|
$testResult = new TestResult('LDAP Authentication'); |
134
|
|
|
$testResult->setState($state); |
135
|
|
|
$this->setTestResult($testResult); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $connectString |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
private function prepareConnection($connectString) |
145
|
|
|
{ |
146
|
|
|
$hostname = parse_url($connectString, PHP_URL_HOST); |
147
|
|
|
$authSourceData = $this->authSourceData; |
148
|
|
|
$authSourceSpecifics = $this->authSourceSpecifics; |
149
|
|
|
|
150
|
|
|
if (preg_match('/^(ldaps:\/\/(.*))$/', $connectString, $matches)) { |
151
|
|
|
// The default context |
152
|
|
|
$sslContext = ['capture_peer_cert' => true, 'verify_peer' => true]; |
153
|
|
|
|
154
|
|
|
// The non-default context, if configured ... |
155
|
|
|
if (!is_null($authSourceSpecifics) && array_key_exists('ssl', $authSourceSpecifics)) { |
156
|
|
|
$sslContext = array_replace($sslContext, $authSourceSpecifics['ssl']); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$port = parse_url($connectString, PHP_URL_PORT); |
160
|
|
|
$port = $port ?: $authSourceData->getInteger('port', 636); |
161
|
|
|
|
162
|
|
|
$uri = 'ssl://' . $hostname . ':' . $port; |
163
|
|
|
$context = stream_context_create(['ssl' => $sslContext]); |
164
|
|
|
} else { |
165
|
|
|
$port = $authSourceData->getInteger('port', 389); |
166
|
|
|
$uri = 'tcp://' . $hostname . ':' . $port; |
167
|
|
|
$context = stream_context_create(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$timeout = $authSourceData->getInteger('timeout', null); |
171
|
|
|
return ['uri' => $uri, 'context' => $context, 'timeout' => $timeout]; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths