1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\monitor\TestSuite\AuthSource; |
4
|
|
|
|
5
|
|
|
use \SimpleSAML\Module\monitor\State as State; |
6
|
|
|
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration; |
7
|
|
|
use \SimpleSAML\Module\monitor\TestCase as TestCase; |
8
|
|
|
use \SimpleSAML\Module\monitor\TestData as TestData; |
9
|
|
|
use \SimpleSAML\Module\monitor\TestResult as TestResult; |
10
|
|
|
|
11
|
|
|
final class Ldap extends \SimpleSAML\Module\monitor\TestSuiteFactory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $authSourceData; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string[] |
20
|
|
|
*/ |
21
|
|
|
private $hosts; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param integer|null; |
25
|
|
|
*/ |
26
|
|
|
private $certExpirationWarning = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param TestConfiguration $configuration |
30
|
|
|
* @param TestData $testData |
31
|
|
|
*/ |
32
|
|
|
public function __construct($configuration, $testData) |
33
|
|
|
{ |
34
|
|
|
$moduleConfig = $configuration->getModuleConfig(); |
35
|
|
|
$authSourceData = $testData->getInputItem('authSourceData'); |
36
|
|
|
assert(is_array($authSourceData)); |
37
|
|
|
|
38
|
|
|
$this->authSourceData = $authSourceData; |
39
|
|
|
$this->hosts = explode(' ', $authSourceData['hostname']); |
40
|
|
|
$this->certExpirationWarning = $moduleConfig->getValue('certExpirationWarning', 28); |
41
|
|
|
$this->setCategory('LDAP authentication source'); |
42
|
|
|
|
43
|
|
|
parent::__construct($configuration); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function invokeTest() |
50
|
|
|
{ |
51
|
|
|
// Test LDAP configuration |
52
|
|
|
$confTest = new TestCase\AuthSource\Ldap\Configuration( |
53
|
|
|
new TestData(['authSourceData' => $this->authSourceData]) |
54
|
|
|
); |
55
|
|
|
$confTestResult = $confTest->getTestResult(); |
56
|
|
|
$this->addTestResult($confTestResult); |
57
|
|
|
|
58
|
|
|
if ($confTestResult->getState() === State::OK) { |
59
|
|
|
$connection = $confTestResult->getOutput('connection'); |
60
|
|
|
|
61
|
|
|
// Test connection for each configured LDAP-server |
62
|
|
|
foreach ($this->hosts as $hostname) { |
63
|
|
|
$preparedTestData = $this->prepareConnection($hostname, $this->authSourceData); |
64
|
|
|
$connTest = new TestCase\Network\ConnectUri( |
65
|
|
|
new TestData($preparedTestData) |
66
|
|
|
); |
67
|
|
|
$connTestResult = $connTest->getTestResult(); |
68
|
|
|
$this->addTestResult($connTestResult); |
69
|
|
|
|
70
|
|
|
if ($connTestResult->getState() === State::OK) { |
71
|
|
|
$certData = $connTestResult->getOutput('certData'); |
72
|
|
|
|
73
|
|
|
// Test certificate when available |
74
|
|
|
if ($certData !== null) { |
75
|
|
|
$certTest = new TestCase\Cert( |
76
|
|
|
new TestData([ |
77
|
|
|
'certData' => $certData, |
78
|
|
|
'category' => 'LDAP Server Certificate', |
79
|
|
|
'certExpirationWarning' => $this->certExpirationWarning, |
80
|
|
|
]) |
81
|
|
|
); |
82
|
|
|
$certTestResult = $certTest->getTestResult(); |
83
|
|
|
$this->addTestResult($certTestResult); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Test bind |
89
|
|
|
$testData = new TestData([ |
90
|
|
|
'authSourceData' => $this->authSourceData, |
91
|
|
|
'connection' => $connection |
92
|
|
|
]); |
93
|
|
|
$bindTest = new TestCase\AuthSource\Ldap\Bind( |
94
|
|
|
$testData |
95
|
|
|
); |
96
|
|
|
$bindTestResult = $bindTest->getTestResult(); |
97
|
|
|
$this->addTestResult($bindTestResult); |
98
|
|
|
|
99
|
|
|
if ($bindTestResult->getState() === State::OK) { |
100
|
|
|
// Test search |
101
|
|
|
$testData = new TestData([ |
102
|
|
|
'authSourceData' => $this->authSourceData, |
103
|
|
|
'connection' => $connection |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
$searchTest = new TestCase\AuthSource\Ldap\Search( |
107
|
|
|
$testData |
108
|
|
|
); |
109
|
|
|
$searchTestResult = $searchTest->getTestResult(); |
110
|
|
|
$this->addTestResult($searchTestResult); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$state = $this->calculateState(); |
115
|
|
|
|
116
|
|
|
$testResult = new TestResult('LDAP Authentication'); |
117
|
|
|
$testResult->setState($state); |
118
|
|
|
$this->setTestResult($testResult); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $connectString |
123
|
|
|
* @param array $authSourceData |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
private function prepareConnection($connectString, $authSourceData) |
128
|
|
|
{ |
129
|
|
|
$hostname = parse_url($connectString, PHP_URL_HOST); |
130
|
|
|
|
131
|
|
|
if (preg_match('/^(ldaps:\/\/(.*))$/', $connectString, $matches)) { |
132
|
|
|
$port = parse_url($connectString, PHP_URL_PORT); |
133
|
|
|
$port = $port ?: $authSourceData['port']; |
134
|
|
|
$uri = 'ssl://' . $hostname . ':' . $port; |
135
|
|
|
$context = stream_context_create(array("ssl" => array("capture_peer_cert" => true, "verify_peer" => true))); |
136
|
|
|
} else { |
137
|
|
|
$port = $authSourceData['port']; |
138
|
|
|
$uri = 'tcp://' . $hostname . ':' . $port; |
139
|
|
|
$context = stream_context_create(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$timeout = isSet($authSourceData['timeout']) ? $authSourceData['timeout'] : null; |
143
|
|
|
return ['uri' => $uri, 'context' => $context, 'timeout' => $timeout]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|