Repository navigation
Expand file tree
/
Copy pathconfig_drift.php
More file actions
359 lines (331 loc) · 17.2 KB
/
Copy pathconfig_drift.php
File metadata and controls
359 lines (331 loc) · 17.2 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/**
* Configuration Drift.
*
* Shows, for every managed firewall, whether its current configuration still
* matches the one an operator approved as the baseline.
*
* Drift is never acted on automatically. Nothing on this page restores a
* configuration; the restore path is the existing, separately confirmed one.
*/
require_once __DIR__ . '/inc/bootstrap.php';
require_once __DIR__ . '/inc/config_drift.php';
require_permission('drift.view');
$message = '';
$can_manage = can('drift.acknowledge');
$detail_id = (int)($_GET['firewall'] ?? 0);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!csrf_verify($_POST['csrf'] ?? '')) {
$message = '<div class="alert alert-danger">Invalid CSRF token.</div>';
} elseif (!$can_manage) {
$message = '<div class="alert alert-danger">Your role does not permit changing drift state.</div>';
} else {
$fwId = (int)($_POST['firewall_id'] ?? 0);
if (isset($_POST['acknowledge'])) {
$r = drift_acknowledge($fwId, trim($_POST['note'] ?? ''));
$message = $r['ok']
? '<div class="alert alert-success">Drift acknowledged.</div>'
: '<div class="alert alert-warning">' . htmlspecialchars($r['error']) . '</div>';
} elseif (isset($_POST['set_baseline'])) {
if (!can('drift.set_baseline')) {
$message = '<div class="alert alert-danger">Your role does not permit setting a baseline.</div>';
} else {
$r = drift_set_baseline($fwId, (int)$_POST['backup_id'], trim($_POST['note'] ?? ''));
$message = $r['ok']
? '<div class="alert alert-success">Baseline updated. This configuration is now the approved one.</div>'
: '<div class="alert alert-danger">' . htmlspecialchars($r['error']) . '</div>';
}
} elseif (isset($_POST['recheck'])) {
drift_evaluate($fwId);
$message = '<div class="alert alert-info">Re-checked.</div>';
} elseif (isset($_POST['recheck_all'])) {
$n = 0;
foreach (db()->query('SELECT id FROM firewalls')->fetchAll(PDO::FETCH_COLUMN) as $id) {
drift_evaluate((int)$id);
$n++;
}
$message = '<div class="alert alert-info">Re-checked ' . $n . ' firewall(s).</div>';
}
}
}
$fleet = drift_fleet_status();
$counts = ['drifted' => 0, 'match' => 0, 'unknown' => 0, 'error' => 0];
foreach ($fleet as $row) {
$st = $row['status'] ?: 'unknown';
$counts[$st] = ($counts[$st] ?? 0) + 1;
}
// --- detail view -----------------------------------------------------------
$detail = null;
if ($detail_id > 0) {
$baseline = drift_get_baseline($detail_id);
$latest = drift_latest_backup($detail_id);
$fwStmt = db()->prepare('SELECT id, hostname FROM firewalls WHERE id = ?');
$fwStmt->execute([$detail_id]);
$fwRow = $fwStmt->fetch(PDO::FETCH_ASSOC);
if ($fwRow) {
$detail = ['firewall' => $fwRow, 'baseline' => $baseline, 'latest' => $latest, 'diff' => [], 'error' => ''];
if ($baseline && $latest) {
$baseXml = drift_load_backup_xml((int)$baseline['backup_id']);
$curXml = drift_load_backup_xml((int)$latest['id']);
if ($baseXml['ok'] && $curXml['ok']) {
$cmp = drift_compare($baseXml['xml'], $curXml['xml']);
$detail['compare'] = $cmp;
foreach (array_merge($cmp['modified'], $cmp['added'], $cmp['removed']) as $section) {
$detail['diff'][$section] = drift_section_diff($baseXml['xml'], $curXml['xml'], $section, 60);
}
} else {
$detail['error'] = $baseXml['ok'] ? $curXml['error'] : $baseXml['error'];
}
}
// Backups that could be promoted to baseline.
$bk = db()->prepare(
'SELECT id, backup_file, file_size, COALESCE(uploaded_at, created_at) AS at
FROM backups WHERE firewall_id = ?
ORDER BY COALESCE(uploaded_at, created_at) DESC, id DESC LIMIT 200'
);
$bk->execute([$detail_id]);
$detail['backups'] = array_values(array_filter(
$bk->fetchAll(PDO::FETCH_ASSOC),
fn($b) => resolve_backup_path($b) !== null
));
}
}
/** Badge colour for a drift status. */
function drift_badge(string $status): string {
return match ($status) {
'drifted' => 'warning text-dark',
'match' => 'success',
'error' => 'danger',
default => 'secondary',
};
}
/** "3 days ago" style age. */
function drift_age(?string $ts): string {
if (!$ts) return '—';
$d = time() - strtotime($ts);
if ($d < 3600) return max(1, (int)($d / 60)) . 'm';
if ($d < 86400) return (int)($d / 3600) . 'h';
return (int)($d / 86400) . 'd';
}
include __DIR__ . '/inc/header.php';
?>
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-start mb-3">
<div>
<h4 class="mb-0"><i class="fas fa-code-compare me-2"></i>Configuration Drift</h4>
<small class="text-muted">
Compares each firewall's newest configuration backup against the baseline you approved.
Serialisation noise and volatile fields are ignored. Nothing here restores anything.
</small>
</div>
<?php if ($can_manage): ?>
<form method="post">
<input type="hidden" name="csrf" value="<?php echo htmlspecialchars(csrf_token()); ?>">
<button class="btn btn-sm btn-outline-secondary" name="recheck_all">
<i class="fas fa-rotate me-1"></i>Re-check all
</button>
</form>
<?php endif; ?>
</div>
<?php echo $message; ?>
<div class="row g-2 mb-3">
<?php foreach ([
['Drifted', $counts['drifted'] ?? 0, 'warning'],
['Matching baseline', $counts['match'] ?? 0, 'success'],
['No baseline', $counts['unknown'] ?? 0, 'secondary'],
['Error', $counts['error'] ?? 0, 'danger'],
] as [$label, $n, $colour]): ?>
<div class="col-6 col-md-3">
<div class="card h-100"><div class="card-body py-2">
<div class="text-muted small"><?php echo $label; ?></div>
<div class="fs-4 text-<?php echo $colour; ?>"><?php echo $n; ?></div>
</div></div>
</div>
<?php endforeach; ?>
</div>
<div class="card mb-4">
<div class="table-responsive">
<table class="table table-sm table-hover align-middle mb-0">
<thead>
<tr>
<th>Firewall</th>
<th>Customer / Site</th>
<th>Drift</th>
<th>Sections changed</th>
<th>Detected</th>
<th>Baseline age</th>
<th>Latest config</th>
<th></th>
</tr>
</thead>
<tbody>
<?php if (!$fleet): ?>
<tr><td colspan="8" class="text-center text-muted py-4">No firewalls.</td></tr>
<?php else: foreach ($fleet as $row):
$status = $row['status'] ?: 'unknown';
$sections = json_decode((string)$row['sections_changed'], true) ?: [];
$names = array_merge($sections['modified'] ?? [], $sections['added'] ?? [], $sections['removed'] ?? []);
?>
<tr>
<td>
<a href="firewall_details.php?id=<?php echo (int)$row['id']; ?>">
<?php echo htmlspecialchars($row['hostname']); ?>
</a>
</td>
<td class="small text-muted">
<?php echo htmlspecialchars($row['customer_name'] ?: 'Unassigned'); ?>
<?php if ($row['site_name']): ?>/ <?php echo htmlspecialchars($row['site_name']); ?><?php endif; ?>
</td>
<td>
<span class="badge bg-<?php echo drift_badge($status); ?>">
<?php echo htmlspecialchars(ucfirst($status)); ?>
</span>
<?php if ($row['acknowledged_at']): ?>
<i class="fas fa-check text-success ms-1"
title="Acknowledged by <?php echo htmlspecialchars($row['acknowledged_by'] ?? ''); ?>"></i>
<?php endif; ?>
</td>
<td class="small">
<?php if ($names): ?>
<?php foreach (array_slice($names, 0, 4) as $n): ?>
<span class="badge bg-secondary"><?php echo htmlspecialchars($n); ?></span>
<?php endforeach; ?>
<?php if (count($names) > 4): ?>
<span class="text-muted">+<?php echo count($names) - 4; ?></span>
<?php endif; ?>
<?php elseif ($row['detail']): ?>
<span class="text-muted"><?php echo htmlspecialchars($row['detail']); ?></span>
<?php else: ?>
<span class="text-muted">—</span>
<?php endif; ?>
</td>
<td class="small text-muted"><?php echo drift_age($row['first_detected_at']); ?></td>
<td class="small text-muted"><?php echo drift_age($row['baseline_set_at']); ?></td>
<td class="small text-muted"><?php echo drift_age($row['latest_backup_at']); ?></td>
<td class="text-end">
<a class="btn btn-sm btn-outline-primary"
href="?firewall=<?php echo (int)$row['id']; ?>">Review</a>
</td>
</tr>
<?php endforeach; endif; ?>
</tbody>
</table>
</div>
</div>
<?php if ($detail): ?>
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<strong><?php echo htmlspecialchars($detail['firewall']['hostname']); ?></strong>
<a class="btn btn-sm btn-outline-secondary" href="config_drift.php">Close</a>
</div>
<div class="card-body">
<?php if ($detail['error']): ?>
<div class="alert alert-warning"><?php echo htmlspecialchars($detail['error']); ?></div>
<?php endif; ?>
<div class="row small mb-3">
<div class="col-md-6">
<strong>Baseline:</strong>
<?php if ($detail['baseline']): ?>
backup #<?php echo (int)$detail['baseline']['backup_id']; ?>,
set <?php echo htmlspecialchars($detail['baseline']['set_at']); ?>
by <?php echo htmlspecialchars($detail['baseline']['set_by_username'] ?? 'unknown'); ?>
<?php if ($detail['baseline']['notes']): ?>
<br><span class="text-muted"><?php echo htmlspecialchars($detail['baseline']['notes']); ?></span>
<?php endif; ?>
<?php else: ?>
<span class="text-muted">none set</span>
<?php endif; ?>
</div>
<div class="col-md-6">
<strong>Current:</strong>
<?php if ($detail['latest']): ?>
backup #<?php echo (int)$detail['latest']['id']; ?>,
<?php echo htmlspecialchars($detail['latest']['uploaded_at'] ?: $detail['latest']['created_at']); ?>
<?php else: ?>
<span class="text-muted">no usable backup</span>
<?php endif; ?>
</div>
</div>
<?php if ($detail['diff']): ?>
<h6 class="small text-uppercase text-muted">Differences from baseline</h6>
<?php foreach ($detail['diff'] as $section => $changes): ?>
<div class="mb-3">
<div class="fw-bold small mb-1">
<?php echo htmlspecialchars($section); ?>
<span class="text-muted">(<?php echo count($changes); ?>)</span>
</div>
<div class="table-responsive">
<table class="table table-sm mb-0">
<tbody>
<?php foreach ($changes as $c): ?>
<tr>
<td style="width:90px">
<span class="badge bg-<?php
echo $c['change'] === 'added' ? 'success'
: ($c['change'] === 'removed' ? 'danger' : 'warning text-dark'); ?>">
<?php echo htmlspecialchars($c['change']); ?>
</span>
</td>
<td class="small font-monospace"><?php echo htmlspecialchars($c['path']); ?></td>
<td class="small">
<?php if ($c['from'] !== null): ?>
<span class="text-danger"><?php echo htmlspecialchars($c['from']); ?></span>
<?php endif; ?>
<?php if ($c['from'] !== null && $c['to'] !== null): ?> → <?php endif; ?>
<?php if ($c['to'] !== null): ?>
<span class="text-success"><?php echo htmlspecialchars($c['to']); ?></span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php endforeach; ?>
<?php elseif ($detail['baseline'] && $detail['latest'] && !$detail['error']): ?>
<div class="alert alert-success mb-3">
This configuration matches the approved baseline.
</div>
<?php endif; ?>
<?php if ($can_manage): ?>
<hr>
<div class="row g-3">
<div class="col-md-6">
<form method="post" class="d-flex gap-2">
<input type="hidden" name="csrf" value="<?php echo htmlspecialchars(csrf_token()); ?>">
<input type="hidden" name="firewall_id" value="<?php echo (int)$detail['firewall']['id']; ?>">
<input type="text" name="note" class="form-control form-control-sm" placeholder="Acknowledgement note (optional)">
<button class="btn btn-sm btn-warning text-nowrap" name="acknowledge">
<i class="fas fa-check me-1"></i>Acknowledge
</button>
</form>
<div class="form-text">Marks the change as reviewed. The baseline is unchanged.</div>
</div>
<?php if (can('drift.set_baseline') && $detail['backups']): ?>
<div class="col-md-6">
<form method="post" class="d-flex gap-2"
onsubmit="return confirm('Make this configuration the approved baseline? Existing drift findings for this firewall will be cleared.')">
<input type="hidden" name="csrf" value="<?php echo htmlspecialchars(csrf_token()); ?>">
<input type="hidden" name="firewall_id" value="<?php echo (int)$detail['firewall']['id']; ?>">
<select name="backup_id" class="form-select form-select-sm">
<?php foreach ($detail['backups'] as $b): ?>
<option value="<?php echo (int)$b['id']; ?>">
#<?php echo (int)$b['id']; ?> · <?php echo htmlspecialchars($b['at']); ?>
</option>
<?php endforeach; ?>
</select>
<button class="btn btn-sm btn-primary text-nowrap" name="set_baseline">
<i class="fas fa-flag me-1"></i>Set baseline
</button>
</form>
<div class="form-text">Promotes a backup to the approved configuration.</div>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div>
<?php include __DIR__ . '/inc/footer.php'; ?>