-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceForm.php
More file actions
337 lines (299 loc) · 11 KB
/
Copy pathSourceForm.php
File metadata and controls
337 lines (299 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
// SPDX-FileCopyrightText: 2024 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Notifications\Forms;
use DateTime;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Exception\Http\HttpNotFoundException;
use Icinga\Module\Notifications\Hook\V2\SourceHook;
use Icinga\Module\Notifications\Model\Source;
use ipl\Html\Attributes;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\Sql\Connection;
use ipl\Stdlib\Filter;
use ipl\Validator\CallbackValidator;
use ipl\Web\Common\CsrfCounterMeasure;
use ipl\Web\Compat\CompatForm;
use ipl\Web\Url;
use ipl\Web\Widget\ButtonLink;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;
use Throwable;
class SourceForm extends CompatForm
{
use CsrfCounterMeasure;
/** @var string|int The used password hash algorithm */
public const HASH_ALGORITHM = PASSWORD_BCRYPT;
/** @var string @var The generic source type */
public const TYPE_GENERIC = 'generic';
/** @var Connection */
private $db;
/** @var ?int */
private $sourceId;
public function __construct(Connection $db)
{
$this->db = $db;
}
protected function assemble(): void
{
$this->applyDefaultElementDecorators();
$this->addCsrfCounterMeasure();
$types = [
'' => ' - ' . $this->translate('Please choose') . ' - ',
self::TYPE_GENERIC => $this->translate('Generic')
];
foreach (Hook::all('Notifications/v2/Source') as $hook) {
/** @var SourceHook $hook */
try {
$type = $hook->getSourceType();
$types[$type] = $hook->getSourceLabel();
} catch (Throwable $e) {
Logger::error('Failed to load source integration %s: %s', $hook::class, $e);
}
}
$this->addHtml(new HtmlElement(
'p',
Attributes::create(['class' => 'description']),
Text::create($this->translate(
'Sources are the most vital part of Icinga Notifications. They submit events that will be'
. ' processed to notify users about incidents. You can either configure sources that provide an'
. ' integration in Icinga Web or use the Generic type for sources that communicate directly with the'
. ' Icinga Notifications API. If you cannot choose the desired source below, consult its documentation'
. ' on how to integrate it.'
))
));
$this->addElement(
'text',
'name',
[
'label' => $this->translate('Source Name'),
'required' => true
]
);
$this->addElement(
'select',
'type',
[
'required' => true,
'label' => $this->translate('Source Type'),
'class' => 'autosubmit',
'options' => $types,
'disabledOptions' => ['']
]
);
$this->addElement('fieldset', 'credentials', [
'label' => $this->translate('Source Credentials')
]);
$credentials = $this->getElement('credentials');
$credentials->addHtml(new HtmlElement(
'p',
Attributes::create(['class' => 'description']),
Text::create($this->translate(
'These credentials will be used by the source to authenticate'
. ' against Icinga Notifications when submitting events. You will need to add this to the'
. ' source\'s configuration as well:'
)),
Text::create(' '),
match ($this->getValue('type')) {
'icinga2' => new Link(
[
$this->translate('Icinga DB Documentation'),
' ',
new Icon('arrow-up-right-from-square')
],
Url::fromPath(
'https://icinga.com/docs/icinga-db'
. '/latest/doc/03-Configuration/#notifications-configuration'
),
['target' => '_blank']
),
'kubernetes' => new Link(
[
$this->translate('Icinga for Kubernetes Documentation'),
' ',
new Icon('arrow-up-right-from-square')
],
Url::fromPath(
'https://icinga.com/docs/icinga-for-kubernetes'
. '/latest/doc/03-Configuration/#notifications-configuration'
),
['target' => '_blank']
),
self::TYPE_GENERIC => Text::create($this->translate(
'Consult the documentation of your source for configuration details.'
)),
default => Text::create($this->translate(
'Please choose the source type above to see the required configuration.'
))
}
));
$credentials->addElement(
'text',
'listener_username',
[
'required' => true,
'label' => $this->translate('Username'),
'validators' => [new CallbackValidator(
function ($value, CallbackValidator $validator) {
// Username must be unique
$source = Source::on($this->db)
->filter(Filter::equal('listener_username', $value));
if ($this->sourceId !== null) {
$source->filter(Filter::unequal('id', $this->sourceId));
}
if ($source->first() !== null) {
$validator->addMessage($this->translate('This username is already in use.'));
return false;
}
return true;
}
)]
]
);
$credentials->addElement(
'password',
'listener_password',
[
'required' => $this->sourceId === null,
'label' => $this->sourceId !== null
? $this->translate('New Password')
: $this->translate('Password'),
'autocomplete' => 'new-password',
'validators' => [['name' => 'StringLength', 'options' => ['min' => 16]]]
]
);
$credentials->addElement(
'password',
'listener_password_dupe',
[
'ignore' => true,
'required' => $this->sourceId === null,
'label' => $this->translate('Repeat Password'),
'autocomplete' => 'new-password',
'validators' => [new CallbackValidator(function (string $value, CallbackValidator $validator) {
if ($value !== $this->getElement('credentials')->getValue('listener_password')) {
$validator->addMessage($this->translate('Passwords do not match'));
return false;
}
return true;
})]
]
);
$this->addElement(
'submit',
'save',
[
'label' => $this->sourceId === null ?
$this->translate('Add Source') :
$this->translate('Save Changes')
]
);
if ($this->sourceId !== null) {
$this->getElement('save')->prependWrapper(
(new HtmlDocument())
->addHtml(
(new ButtonLink(
$this->translate('Delete'),
Url::fromPath('notifications/source/delete/', ['id' => $this->sourceId])
))->openInModal()
)
);
}
}
/**
* Load the source with given id
*
* @param int $id
*
* @return $this
*/
public function loadSource(int $id): self
{
$this->sourceId = $id;
$this->populate($this->fetchDbValues());
return $this;
}
/**
* Add the new source
*/
public function addSource(): void
{
$data = $this->getValues();
$source = [
'name' => $data['name'],
'type' => $data['type'],
'listener_username' => $data['credentials']['listener_username'],
// Not using PASSWORD_DEFAULT, as the used algorithm should
// be kept in sync with what the daemon understands
'listener_password_hash' => password_hash(
$data['credentials']['listener_password'],
self::HASH_ALGORITHM
),
'changed_at' => (int) (new DateTime())->format("Uv")
];
$this->db->transaction(function (Connection $db) use ($source): void {
$db->insert('source', $source);
});
}
/**
* Edit the source
*
* @return void
*/
public function editSource(): void
{
$data = $this->getValues();
$source = [
'name' => $data['name'],
'type' => $data['type'],
'listener_username' => $data['credentials']['listener_username']
];
/** @var ?string $listenerPassword */
$listenerPassword = $data['credentials']['listener_password'] ?? null;
if ($listenerPassword) {
// Not using PASSWORD_DEFAULT, as the used algorithm should
// be kept in sync with what the daemon understands
$source['listener_password_hash'] = password_hash($listenerPassword, self::HASH_ALGORITHM);
} elseif (empty(array_diff_assoc($source, $this->fetchDbValues()))) {
return;
}
$source['changed_at'] = (int) (new DateTime())->format("Uv");
$this->db->update('source', $source, ['id = ?' => $this->sourceId]);
}
/**
* Get the source name
*
* @return string
*/
public function getSourceName(): string
{
return $this->getValue('name');
}
/**
* Fetch the values from the database
*
* @return array
*
* @throws HttpNotFoundException
*/
private function fetchDbValues(): array
{
/** @var ?Source $source */
$source = Source::on($this->db)
->filter(Filter::equal('id', $this->sourceId))
->first();
if ($source === null) {
throw new HttpNotFoundException($this->translate('Source not found'));
}
return [
'name' => $source->name,
'type' => $source->type,
'credentials' => [
'listener_username' => $source->listener_username
]
];
}
}