57 lines
2.5 KiB
PHP
57 lines
2.5 KiB
PHP
<?php // tests/Feature/Security/HostFirewallTest.php
|
|
|
|
use App\Models\Host;
|
|
use App\Provisioning\Steps\Host\SecureHostFirewall;
|
|
use App\Services\Security\HostFirewall;
|
|
use App\Services\Ssh\FakeRemoteShell;
|
|
|
|
it('stellt die Sperrregel UNTER die Regel fuer bestehende Verbindungen', function () {
|
|
// Das ist die eigentliche Zusage des ganzen Systems: „wer drin ist, bleibt
|
|
// drin". Sie hängt an dieser Reihenfolge und an nichts sonst. Geprüft am
|
|
// ERZEUGTEN Regelwerk, nicht am Quelltext — ein Test gegen den Quelltext
|
|
// wäre auch dann grün, wenn die Zeilen im Ergebnis anders herum stünden.
|
|
$shell = new FakeRemoteShell;
|
|
app()->instance(\App\Services\Ssh\RemoteShell::class, $shell);
|
|
|
|
app(SecureHostFirewall::class)->execute(
|
|
\App\Models\ProvisioningRun::factory()->forHost(Host::factory()->create())->create()
|
|
);
|
|
|
|
$regelwerk = $shell->files()['/etc/nftables.conf'] ?? '';
|
|
|
|
$established = strpos($regelwerk, 'ct state established,related accept');
|
|
$sperre = strpos($regelwerk, '@clupilot_blocked');
|
|
|
|
expect($established)->not->toBeFalse()
|
|
->and($sperre)->not->toBeFalse()
|
|
->and($established)->toBeLessThan($sperre);
|
|
|
|
// Beide Mengen, und beide mit Ablaufzeit — ohne `flags timeout` nimmt
|
|
// nftables die Zeitangabe beim Eintragen gar nicht an.
|
|
expect($regelwerk)->toContain('set clupilot_blocked')
|
|
->and($regelwerk)->toContain('set clupilot_blocked6')
|
|
->and(substr_count($regelwerk, 'flags timeout'))->toBe(2);
|
|
});
|
|
|
|
it('traegt eine Adresse mit Ablaufzeit ein und nimmt sie wieder heraus', function () {
|
|
$shell = new FakeRemoteShell;
|
|
app()->instance(\App\Services\Ssh\RemoteShell::class, $shell);
|
|
$host = Host::factory()->active()->create(['ssh_host_key' => 'SHA256:abc']);
|
|
|
|
app(HostFirewall::class)->block($host, '203.0.113.7', 3600);
|
|
app(HostFirewall::class)->release($host, '203.0.113.7');
|
|
|
|
expect($shell->ran('add element inet clupilot_filter clupilot_blocked { 203.0.113.7 timeout 3600s }'))->toBeTrue()
|
|
->and($shell->ran('delete element inet clupilot_filter clupilot_blocked { 203.0.113.7 }'))->toBeTrue();
|
|
});
|
|
|
|
it('waehlt fuer eine IPv6-Adresse die zweite Menge', function () {
|
|
$shell = new FakeRemoteShell;
|
|
app()->instance(\App\Services\Ssh\RemoteShell::class, $shell);
|
|
$host = Host::factory()->active()->create(['ssh_host_key' => 'SHA256:abc']);
|
|
|
|
app(HostFirewall::class)->block($host, '2001:db8::1', 3600);
|
|
|
|
expect($shell->ran('clupilot_blocked6 { 2001:db8::1 timeout 3600s }'))->toBeTrue();
|
|
});
|