Read Testrig backend credentials from managed files #1
@@ -2,11 +2,16 @@
|
|||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Support\ManagedCredentialFiles;
|
||||||
|
use App\Support\ManagedPhpRedisConnector;
|
||||||
use Carbon\CarbonImmutable;
|
use Carbon\CarbonImmutable;
|
||||||
|
use Illuminate\Database\DatabaseManager;
|
||||||
|
use Illuminate\Redis\RedisManager;
|
||||||
use Illuminate\Support\Facades\Date;
|
use Illuminate\Support\Facades\Date;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Illuminate\Validation\Rules\Password;
|
use Illuminate\Validation\Rules\Password;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
@@ -15,7 +20,30 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
*/
|
*/
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
//
|
$config = $this->app['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['db.factory']->make($connection, $name);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->callAfterResolving('redis', function (RedisManager $manager): void {
|
||||||
|
$manager->extend('phpredis', fn () => new ManagedPhpRedisConnector);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Support;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
final class ManagedCredentialFiles
|
||||||
|
{
|
||||||
|
public static function databasePassword(): string
|
||||||
|
{
|
||||||
|
return self::read('absdev-db-password');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function redisPassword(): string
|
||||||
|
{
|
||||||
|
return self::read('absdev-redis-password');
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function read(string $filename): string
|
||||||
|
{
|
||||||
|
$path = '/run/secrets/'.$filename;
|
||||||
|
clearstatcache(true, $path);
|
||||||
|
|
||||||
|
if (is_link($path) || ! is_file($path) || (fileperms($path) & 0777) !== 0600) {
|
||||||
|
throw new RuntimeException('Managed credential file is missing or unsafe.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$password = @file_get_contents($path, false, null, 0, 4097);
|
||||||
|
if ($password === false || strlen($password) > 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Support;
|
||||||
|
|
||||||
|
use Illuminate\Redis\Connectors\PhpRedisConnector;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
final class ManagedPhpRedisConnector extends PhpRedisConnector
|
||||||
|
{
|
||||||
|
public function connect(array $config, array $options)
|
||||||
|
{
|
||||||
|
$config['password'] = ManagedCredentialFiles::redisPassword();
|
||||||
|
unset($options['password']);
|
||||||
|
|
||||||
|
return parent::connect($config, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function connectToCluster(array $config, array $clusterOptions, array $options)
|
||||||
|
{
|
||||||
|
throw new RuntimeException('Managed credential delivery requires standalone Redis.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,8 @@ use Pdo\Mysql;
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
|
'managed_credentials' => env('ABSDEV_MANAGED_CREDENTIALS', false),
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Default Database Connection Name
|
| Default Database Connection Name
|
||||||
|
|||||||
Reference in New Issue
Block a user