diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index f1525e9..5935e89 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,11 +2,16 @@ namespace App\Providers; +use App\Support\ManagedCredentialFiles; +use App\Support\ManagedPhpRedisConnector; use Carbon\CarbonImmutable; +use Illuminate\Database\DatabaseManager; +use Illuminate\Redis\RedisManager; use Illuminate\Support\Facades\Date; use Illuminate\Support\Facades\DB; use Illuminate\Support\ServiceProvider; use Illuminate\Validation\Rules\Password; +use RuntimeException; class AppServiceProvider extends ServiceProvider { @@ -15,7 +20,30 @@ class AppServiceProvider extends ServiceProvider */ public function register(): void { - // + $config = $this->app->make('config'); + if ($config->get('database.managed_credentials') !== true) { + return; + } + + if ($config->get('database.default') !== 'mysql' + || $config->get('database.redis.client') !== 'phpredis' + || $config->get('database.connections.mysql.url') + || $config->get('database.redis.default.url') + || $config->get('database.redis.cache.url')) { + throw new RuntimeException('Managed credentials require the supported MySQL and standalone PhpRedis connections.'); + } + + $this->callAfterResolving('db', function (DatabaseManager $manager): void { + $manager->extend('mysql', function (array $connection, string $name) { + $connection['password'] = ManagedCredentialFiles::databasePassword(); + + return $this->app->make('db.factory')->make($connection, $name); + }); + }); + + $this->callAfterResolving('redis', function (RedisManager $manager): void { + $manager->extend('phpredis', fn () => new ManagedPhpRedisConnector); + }); } /** diff --git a/app/Support/ManagedCredentialFiles.php b/app/Support/ManagedCredentialFiles.php new file mode 100644 index 0000000..a8b6922 --- /dev/null +++ b/app/Support/ManagedCredentialFiles.php @@ -0,0 +1,40 @@ + 4096) { + throw new RuntimeException('Managed credential file could not be read.'); + } + + $password = rtrim($password, "\r\n"); + if ($password === '' || strpbrk($password, "\r\n\0") !== false) { + throw new RuntimeException('Managed credential file is invalid.'); + } + + return $password; + } +} diff --git a/app/Support/ManagedPhpRedisConnector.php b/app/Support/ManagedPhpRedisConnector.php new file mode 100644 index 0000000..53eb637 --- /dev/null +++ b/app/Support/ManagedPhpRedisConnector.php @@ -0,0 +1,31 @@ + $config + * @param array $options + */ + public function connect(array $config, array $options) + { + $config['password'] = ManagedCredentialFiles::redisPassword(); + unset($options['password']); + + return parent::connect($config, $options); + } + + /** + * @param array $config + * @param array $clusterOptions + * @param array $options + */ + public function connectToCluster(array $config, array $clusterOptions, array $options): never + { + throw new RuntimeException('Managed credential delivery requires standalone Redis.'); + } +} diff --git a/config/database.php b/config/database.php index abbb88e..a43f7d4 100644 --- a/config/database.php +++ b/config/database.php @@ -5,6 +5,8 @@ use Pdo\Mysql; return [ + 'managed_credentials' => env('ABSDEV_MANAGED_CREDENTIALS', false), + /* |-------------------------------------------------------------------------- | Default Database Connection Name