118 lines
5.3 KiB
PHP
118 lines
5.3 KiB
PHP
<?php
|
||
|
||
namespace App\Process\Tester;
|
||
|
||
use Illuminate\Contracts\Foundation\Application;
|
||
use Illuminate\Support\ServiceProvider;
|
||
use Monolog\Handler\NullHandler;
|
||
use RuntimeException;
|
||
|
||
/**
|
||
* Turns the process that runs a tester scenario into a sandbox before anything is resolved (requirement T2,
|
||
* docs/Prozessarchitektur/Architektur/04 §3.3). Called right after the configuration is loaded and only when the
|
||
* process was started for a run (environment FEWO_SANDBOX_RUN, set by ProcessSandbox for its child process).
|
||
*
|
||
* Default deny: the configuration is replaced, not amended – the only database is the run's own SQLite file, the only
|
||
* mailer `array`, the only cache store `array`, the queue `sync`, every disk lies in the run's directory, logging goes
|
||
* to the run's log, all secrets are blanked and the application key is a fresh one per run (links and encrypted values
|
||
* of a run are worthless outside it). Whatever is not configured does not exist in this process.
|
||
*/
|
||
final class SandboxBootstrapper
|
||
{
|
||
public const ENV = 'FEWO_SANDBOX_RUN';
|
||
|
||
public static function active(): bool
|
||
{
|
||
return filled(getenv(self::ENV) ?: null);
|
||
}
|
||
|
||
public static function directory(string $run): string
|
||
{
|
||
if (preg_match('/^[a-f0-9-]{36}$/', $run) !== 1) {
|
||
throw new RuntimeException('Ungültige Laufkennung.');
|
||
}
|
||
|
||
return storage_path('app/sandbox/runs/'.$run);
|
||
}
|
||
|
||
public static function configure(Application $app): void
|
||
{
|
||
$run = (string) getenv(self::ENV);
|
||
$directory = self::directory($run);
|
||
if (! is_dir($directory) || ! is_file($directory.'/input.json')) {
|
||
throw new RuntimeException('Prozess-Tester: Laufverzeichnis fehlt.');
|
||
}
|
||
|
||
$config = $app['config'];
|
||
|
||
// The run's own database: a copy of the migrated, empty template made at deployment (process:sandbox-template).
|
||
$template = storage_path('app/sandbox/template.sqlite');
|
||
if (! is_file($directory.'/db.sqlite')) {
|
||
if (! is_file($template) || ! copy($template, $directory.'/db.sqlite')) {
|
||
throw new RuntimeException('Prozess-Tester: Vorlage der Testdatenbank fehlt (php artisan process:sandbox-template).');
|
||
}
|
||
}
|
||
|
||
$config->set('database.default', 'sandbox');
|
||
$config->set('database.connections', ['sandbox' => [
|
||
'driver' => 'sqlite',
|
||
'database' => $directory.'/db.sqlite',
|
||
'prefix' => '',
|
||
'foreign_key_constraints' => true,
|
||
]]);
|
||
$config->set('database.redis', []);
|
||
|
||
$config->set('mail.default', 'array');
|
||
$config->set('mail.mailers', ['array' => ['transport' => 'array']]);
|
||
$config->set('mail.from', ['address' => 'tester@sandbox.invalid', 'name' => 'Prozess-Tester']);
|
||
$config->set('fewo.mail_redirect_to', null);
|
||
|
||
$config->set('cache.default', 'array');
|
||
$config->set('cache.stores', ['array' => ['driver' => 'array', 'serialize' => false]]);
|
||
$config->set('session.driver', 'array');
|
||
$config->set('queue.default', 'sync');
|
||
$config->set('queue.connections', ['sync' => ['driver' => 'sync']]);
|
||
$config->set('queue.failed', ['driver' => 'null']);
|
||
|
||
$files = $directory.'/files';
|
||
$config->set('filesystems.default', 'local');
|
||
$config->set('filesystems.disks', [
|
||
'local' => ['driver' => 'local', 'root' => $files.'/private', 'throw' => true],
|
||
'private' => ['driver' => 'local', 'root' => $files.'/private', 'throw' => true],
|
||
'public' => ['driver' => 'local', 'root' => $files.'/public', 'throw' => true],
|
||
'media' => ['driver' => 'local', 'root' => $files.'/media', 'throw' => true],
|
||
]);
|
||
|
||
$config->set('logging.default', 'sandbox');
|
||
$config->set('logging.channels', [
|
||
'sandbox' => ['driver' => 'single', 'path' => $directory.'/log.txt', 'level' => 'debug'],
|
||
'null' => ['driver' => 'monolog', 'handler' => NullHandler::class],
|
||
'emergency' => ['path' => $directory.'/log.txt'],
|
||
]);
|
||
|
||
$config->set('app.key', 'base64:'.base64_encode(random_bytes(32)));
|
||
$config->set('app.previous_keys', []);
|
||
|
||
// Secrets and outside services: blank. The scripted answers of the scenario replace them.
|
||
foreach (['services', 'fewo.mailbox', 'fewo.automation', 'fewo.llm'] as $key) {
|
||
if (is_array($config->get($key))) {
|
||
$config->set($key, array_map(fn (mixed $value): mixed => is_array($value) ? [] : null, (array) $config->get($key)));
|
||
}
|
||
}
|
||
|
||
$config->set('sandbox', ['run' => $run, 'directory' => $directory, 'probe' => self::probe($app)]);
|
||
// Registered after every framework provider, so its bindings replace the managers before anything resolves them.
|
||
$config->set('app.providers', [...($config->get('app.providers') ?? ServiceProvider::defaultProviders()->toArray()), SandboxServiceProvider::class]);
|
||
}
|
||
|
||
/**
|
||
* A probe that deliberately tries forbidden accesses – only for the negative tests of the test suite.
|
||
*/
|
||
private static function probe(Application $app): ?string
|
||
{
|
||
$probe = getenv('FEWO_SANDBOX_PROBE') ?: null;
|
||
|
||
return $probe !== null && $app->environment('testing') && class_exists($probe) ? $probe : null;
|
||
}
|
||
}
|