41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?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;
|
|
}
|
|
}
|