CluPilotCloud/tests/Feature/DeploymentRunsAsTheAppUserT...

205 lines
9.6 KiB
PHP

<?php
use App\Support\NextcloudOcc;
use Illuminate\Support\Facades\File;
/**
* Nothing runs inside a container without saying who it is running as.
*
* `docker compose exec` is root unless told otherwise, and the deployment
* scripts relied on that default. When `artisan optimize` failed during a
* deployment it wrote its error into storage/logs/laravel.log AS ROOT — and
* from that moment the application could not append to its own log. Monolog
* threw on every attempt, and a throw while logging is a 500 on every page that
* logs, with nothing written down to say why.
*
* It surfaced days later as a 500 on the VPN config download, that being one of
* the few pages writing a log line on its way through, and was chased through
* the VPN code, the encryption key and the session before anyone looked at the
* owner of a file.
*
* docker/entrypoint.sh had it right all along: it drops to www-data for exactly
* these commands. The rule is only that the user is named — `-u root` is a fine
* answer where root is what is wanted (the ownership repair in update.sh), as
* long as somebody chose it.
*/
it('never runs a command in the app container as whoever docker felt like', function () {
$offenders = [];
foreach (File::glob(base_path('deploy/*.sh')) as $path) {
foreach (preg_split('/\R/', File::get($path)) ?: [] as $i => $line) {
if (! str_contains($line, 'docker compose exec')) {
continue;
}
// Only the app container. The gateway and the queues are other
// images with other users, and none of them own this checkout.
if (! preg_match('/docker compose exec\b[^|]*?\bapp\b/', $line)) {
continue;
}
if (preg_match('/\s-u\s+\S+/', $line)) {
continue;
}
// Help text counts. Telling an operator to run it as root is how
// the file ends up owned by root in the first place.
$offenders[] = basename($path).':'.($i + 1).' — '.trim($line);
}
}
expect($offenders)->toBe([]);
});
it('never runs occ in a customer guest as whoever docker felt like either', function () {
// The same default, the same lesson, a different subsystem — and it was learned
// here second. `docker compose exec` is root, the official Nextcloud image's
// console.php exits 1 unless the caller owns config/config.php (www-data), and
// the prefix was pasted into five separate files, so every occ call in the
// product was failing on every instance. The ones that go through
// CustomerStep::guest() turned that into a retry and then a failed run for a
// machine that was otherwise finished.
//
// So there is one builder, and this refuses a second: nothing in app/ may spell
// the invocation out by hand, which is the only way a sixth call site can be
// stopped from getting it wrong again.
$offenders = [];
foreach (File::allFiles(app_path()) as $file) {
if ($file->getExtension() !== 'php'
|| $file->getRelativePathname() === 'Support/NextcloudOcc.php') {
continue;
}
if (str_contains(File::get($file->getRealPath()), 'docker compose exec')) {
$offenders[] = $file->getRelativePathname();
}
}
expect($offenders)->toBe([])
// And the one builder names the user Nextcloud insists on.
->and(NextcloudOcc::command('status'))
->toContain('docker compose exec -T -u www-data ');
});
it('repairs ownership before it needs it, not after', function () {
// A server already carrying the damage has to heal on its next deployment.
// Nothing else ever will: a root-owned log file stays root-owned, and the
// page that trips over it is nowhere near the deployment that caused it.
$update = File::get(base_path('deploy/update.sh'));
expect($update)->toContain('normalise_ownership')
// Every directory the deployment writes into, and node_modules by name:
// the first version sampled the top-level owner and skipped the
// recursion when it matched, which is how a root-owned
// node_modules/.vite-temp under a www-data-owned node_modules failed
// the build in maintenance mode.
->and($update)->toContain('! -user www-data -exec chown www-data:www-data')
->and($update)->toContain('node_modules')
// Und das Heimatverzeichnis von www-data. Es stand nicht auf der Liste,
// weil es außerhalb des Checkouts liegt — und genau daran brach ein
// Deployment mit Code 243 ab: npm wollte ~/.npm anlegen und durfte
// nicht (EACCES, errno -13; 256 minus 13 ist 243). Der Server blieb im
// Wartungsmodus stehen, die Kundenseite antwortete mit 500.
->and($update)->toContain('/var/www/.npm');
// Before the first unprivileged step, or it cannot help: composer and npm
// would already have failed on a root-owned vendor directory.
$repair = strpos($update, "\nnormalise_ownership\n");
$maintenance = strpos($update, 'php artisan down --retry=60');
expect($repair)->not->toBeFalse()
->and($maintenance)->not->toBeFalse()
->and($repair)->toBeLessThan($maintenance);
});
it('gibt www-data sein eigenes Heimatverzeichnis, statt ihm nur eine neue Nummer zu geben', function () {
// `usermod -o -u` vergibt die Nummer und schreibt KEINE vorhandene Datei
// um. Ohne das chown danach gehört /var/www weiter der Nummer 33 aus dem
// Basis-Abbild, und www-data kann in seinem eigenen Zuhause nichts anlegen
// — weder ~/.npm noch ~/.composer.
$dockerfile = File::get(base_path('docker/php/Dockerfile'));
$usermod = strpos($dockerfile, 'usermod');
$chown = strpos($dockerfile, 'chown -R www-data:www-data /var/www');
expect($usermod)->not->toBeFalse()
->and($chown)->not->toBeFalse()
// Danach, nicht davor: vor der Umnummerierung würde es auf die alte
// Nummer chownen und wäre unmittelbar wieder falsch.
->and($chown)->toBeGreaterThan($usermod);
});
/**
* Das Update darf sich nicht selbst unter den Füßen wegziehen.
*
* `update.sh` checkt einen neuen Stand in denselben Baum aus, aus dem es läuft
* — es ersetzt also SICH SELBST auf der Platte. Bash liest ein Skript aber
* nachlaufend ab einer Byte-Position; ändert sich die Datei mitten im Lauf,
* liest es an derselben Position im neuen Text weiter. Liegt die dann vor dem
* Punkt, an dem der Lauf schon war, führt das Skript einen Abschnitt ein
* zweites Mal aus.
*
* Genau das war v1.3.97: die Freigabe fügte 23 Zeilen oberhalb des Checkouts
* ein, alles danach rutschte, und das Update drehte sich im Kreis. v1.3.96 lief
* sauber — sie hatte diese Datei nicht angefasst. Der Fehler trifft also nur
* Freigaben, die das Update selbst ändern, und blieb deshalb lange unsichtbar.
*/
it('runs from a copy of itself, so a checkout cannot rewrite it mid-flight', function () {
$update = File::get(base_path('deploy/update.sh'));
$reexec = strpos($update, 'exec bash "$CLUPILOT_UPDATE_COPY"');
$checkout = strpos($update, 'git checkout --quiet --detach');
expect($reexec)->not->toBeFalse('update.sh startet sich nicht aus einer Kopie neu')
->and($checkout)->not->toBeFalse()
// Vor dem Checkout, sonst hilft es nicht: die Kopie muss stehen, BEVOR
// die Originaldatei getauscht werden kann.
->and($reexec)->toBeLessThan($checkout);
// Und die Kopie liegt außerhalb des Checkouts — läge sie darin, träfe sie
// derselbe Tausch.
expect($update)->toContain('mktemp /tmp/clupilot-update-');
});
it('reicht den Terminal-Pfad durch, ohne ihn weiter zu oeffnen als noetig', function () {
$nginx = File::get(base_path('docker/nginx/default.conf'));
// Genau dieser eine Ort, kein Präfix: ein `location /terminal` machte
// jeden Unterpfad zum Weg in den Container.
expect($nginx)->toContain('location = /terminal/ws')
->and($nginx)->not->toContain('location /terminal')
->and($nginx)->toContain('terminal:8082')
->and($nginx)->toContain('proxy_pass http://$terminal_upstream')
->and($nginx)->toContain('Upgrade $http_upgrade');
// Und das Ticket reist im Kopf, nicht in der Adresszeile — sonst stünde es
// im Zugriffsprotokoll jedes Reverse Proxy auf der Strecke. Der Browser ist
// die Seite, die das entscheidet.
expect(File::get(base_path('resources/js/terminal.js')))
->not->toContain('/terminal/ws?');
});
it('laesst die Terminal-Bruecke dort stehen, wo ein Host ueberhaupt erreichbar ist', function () {
// Der app-Container erreicht keinen einzigen Host — gemessen, nicht
// vermutet: wg0 lebt im Netz-Namensraum von queue-provisioning. Wer diese
// Zeile für Aufräumen hält, bekommt einen Container, der startet, gesund
// aussieht und bei jeder Sitzung in eine Zeitüberschreitung läuft.
$compose = File::get(base_path('docker-compose.yml'));
$block = preg_split('/^ terminal:$/m', $compose)[1] ?? '';
$block = preg_split('/^ \S/m', $block)[0] ?? '';
expect($block)->not->toBe('')
->and($block)->toContain('network_mode: "service:queue-provisioning"')
// Kein eigener Port nach außen: erreichbar allein über nginx. Ein
// Dienst in fremdem Namensraum könnte ihn ohnehin nicht veröffentlichen
// — Docker lehnt das Compose-File dann komplett ab.
->and($block)->not->toContain('ports:');
// Und der Name, unter dem nginx ihn anspricht, muss der Alias des
// Namensraum-Eigentümers sein: ein Container ohne eigenes Netz hat keinen
// eigenen DNS-Eintrag.
expect($compose)->toContain(' - terminal');
});