127 lines
5.6 KiB
PHP
127 lines
5.6 KiB
PHP
<?php
|
||
|
||
namespace App\Process\Tester;
|
||
|
||
use App\Channels\ChannelRegistry;
|
||
use App\Channels\Mail\MailboxReader;
|
||
use Illuminate\Cache\CacheManager;
|
||
use Illuminate\Database\Connection;
|
||
use Illuminate\Database\DatabaseManager;
|
||
use Illuminate\Filesystem\FilesystemManager;
|
||
use Illuminate\Http\Client\Request;
|
||
use Illuminate\Mail\MailManager;
|
||
use Illuminate\Support\Facades\Http;
|
||
use Illuminate\Support\ServiceProvider;
|
||
|
||
/**
|
||
* Bindings of a tester process (only registered by SandboxBootstrapper): managers that refuse every name except the
|
||
* sandbox's own – recording the attempt in the run's ledger before they throw –, a guard on the sandbox connection that
|
||
* verifies the database file, a mailbox that cannot be read, no real connectors and no network without a scripted answer.
|
||
*/
|
||
final class SandboxServiceProvider extends ServiceProvider
|
||
{
|
||
public function register(): void
|
||
{
|
||
EffectLedger::open(config('sandbox.directory').'/ledger.jsonl');
|
||
|
||
// The file the run may use, fixed now: later changes of the configuration cannot widen it.
|
||
$file = (string) config('database.connections.sandbox.database');
|
||
$this->app->singleton('db', fn ($app): DatabaseManager => new class($app, $app['db.factory'], $file) extends DatabaseManager
|
||
{
|
||
public function __construct($app, $factory, private string $file)
|
||
{
|
||
parent::__construct($app, $factory);
|
||
}
|
||
|
||
public function connection($name = null)
|
||
{
|
||
$name ??= $this->getDefaultConnection();
|
||
[$database] = $this->parseConnectionName($name);
|
||
if ($database !== 'sandbox') {
|
||
throw SandboxViolation::record('database', "Verbindung [{$name}] ist im Prozess-Tester nicht erlaubt.");
|
||
}
|
||
|
||
return parent::connection($name);
|
||
}
|
||
|
||
/** Every (re)connected sandbox connection gets the guard: each statement must go to the run's own file. */
|
||
protected function configure(Connection $connection, $type)
|
||
{
|
||
$connection = parent::configure($connection, $type);
|
||
$expected = realpath($this->file) ?: $this->file;
|
||
$connection->beforeExecuting(function (string $query, array $bindings, Connection $connection) use ($expected): void {
|
||
$actual = (string) $connection->getConfig('database');
|
||
if ((realpath($actual) ?: $actual) !== $expected) {
|
||
throw SandboxViolation::record('database', 'Abfrage an eine andere Datenbankdatei: '.basename($actual));
|
||
}
|
||
});
|
||
|
||
return $connection;
|
||
}
|
||
});
|
||
|
||
$this->app->singleton('filesystem', fn ($app): FilesystemManager => new class($app) extends FilesystemManager
|
||
{
|
||
public function build($config)
|
||
{
|
||
$root = is_array($config) ? (string) ($config['root'] ?? '') : '';
|
||
if (! str_starts_with(str_replace('\\', '/', $root), str_replace('\\', '/', (string) config('sandbox.directory')))) {
|
||
throw SandboxViolation::record('file', 'Dateiablage außerhalb des Laufverzeichnisses ist im Prozess-Tester nicht erlaubt.');
|
||
}
|
||
|
||
return parent::build($config);
|
||
}
|
||
});
|
||
|
||
// Mail and cache come from deferred providers, which would register their managers again on first use: they are
|
||
// loaded and replaced just before the application boots, when the list of deferred services is complete.
|
||
$this->app->booting(function (): void {
|
||
foreach (['mail.manager', 'cache'] as $service) {
|
||
$this->app->loadDeferredProvider($service);
|
||
}
|
||
|
||
$this->app->singleton('mail.manager', fn ($app): MailManager => new class($app) extends MailManager
|
||
{
|
||
public function mailer($name = null)
|
||
{
|
||
if (($name ?? $this->getDefaultDriver()) !== 'array') {
|
||
throw SandboxViolation::record('mail', "Mailer [{$name}] ist im Prozess-Tester nicht erlaubt.");
|
||
}
|
||
|
||
return parent::mailer($name);
|
||
}
|
||
});
|
||
|
||
$this->app->singleton('cache', fn ($app): CacheManager => new class($app) extends CacheManager
|
||
{
|
||
public function store($name = null)
|
||
{
|
||
if (($name ?? $this->getDefaultDriver()) !== 'array') {
|
||
throw SandboxViolation::record('cache', "Cache [{$name}] ist im Prozess-Tester nicht erlaubt.");
|
||
}
|
||
|
||
return parent::store($name);
|
||
}
|
||
});
|
||
});
|
||
|
||
$this->app->singleton(MailboxReader::class, fn (): MailboxReader => new SandboxMailboxReader);
|
||
$this->app->singleton(ChannelRegistry::class, fn (): ChannelRegistry => new SandboxChannelRegistry);
|
||
}
|
||
|
||
public function boot(): void
|
||
{
|
||
// No network: only the scenario's scripted answers (ProcessSandboxDriver::script) are served.
|
||
Http::preventStrayRequests();
|
||
Http::fake(function (Request $request) {
|
||
$answer = SandboxNetwork::answer($request);
|
||
if ($answer === null) {
|
||
throw SandboxViolation::record('network', 'Netzzugriff ohne Szenario-Antwort: '.$request->method().' '.parse_url($request->url(), PHP_URL_HOST));
|
||
}
|
||
EffectLedger::record('network', ['method' => $request->method(), 'host' => parse_url($request->url(), PHP_URL_HOST), 'scripted' => true]);
|
||
|
||
return $answer;
|
||
});
|
||
}
|
||
}
|