mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-06 13:10:37 -04:00
fix(postgresql): set PGPASSWORD for healthchecks and backups (#12224)
This commit is contained in:
@@ -263,6 +263,7 @@ class StartPostgresql
|
||||
$environment_variables = collect();
|
||||
$this->resolvedPostgresUser = (string) $this->database->postgres_user;
|
||||
$this->resolvedPostgresDatabase = (string) $this->database->postgres_db;
|
||||
$postgresPassword = (string) $this->database->postgres_password;
|
||||
foreach ($this->database->runtime_environment_variables as $env) {
|
||||
$rawValue = (string) $this->database->resolveSecretManagerEnvironmentVariableValue($env);
|
||||
$resolvedValue = (string) $this->database->formatEnvironmentVariableValue($env, $rawValue);
|
||||
@@ -273,6 +274,8 @@ class StartPostgresql
|
||||
$this->resolvedPostgresUser = $composeFileValue;
|
||||
} elseif ($env->key === 'POSTGRES_DB') {
|
||||
$this->resolvedPostgresDatabase = $composeFileValue;
|
||||
} elseif ($env->key === 'POSTGRES_PASSWORD') {
|
||||
$postgresPassword = $resolvedValue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,6 +289,10 @@ class StartPostgresql
|
||||
if (! $this->hasEnvironmentVariable($environment_variables, 'POSTGRES_PASSWORD')) {
|
||||
$environment_variables->push("POSTGRES_PASSWORD={$this->database->postgres_password}");
|
||||
}
|
||||
// psql in the healthcheck, backups, and restores reads PGPASSWORD when pg_hba.conf requires a password for local connections.
|
||||
if (! $this->hasEnvironmentVariable($environment_variables, 'PGPASSWORD')) {
|
||||
$environment_variables->push("PGPASSWORD={$postgresPassword}");
|
||||
}
|
||||
|
||||
if (! $this->hasEnvironmentVariable($environment_variables, 'POSTGRES_DB')) {
|
||||
$environment_variables->push("POSTGRES_DB={$this->database->postgres_db}");
|
||||
|
||||
@@ -91,6 +91,26 @@ it('does not add a Postgres default when the user sets the exact key', function
|
||||
expect($postgresDbEntries)->toBe(['POSTGRES_DB=customdb']);
|
||||
});
|
||||
|
||||
it('sets PGPASSWORD to the Postgres password so psql works when pg_hba.conf requires a password', function (array $userEnvironment, string $expected) {
|
||||
$database = create_standalone_postgresql($this->environment->id, $this->destination, ['postgres_password' => 'model-secret']);
|
||||
foreach ($userEnvironment as $key => $value) {
|
||||
$database->runtime_environment_variables()->create(['key' => $key, 'value' => $value]);
|
||||
}
|
||||
|
||||
StartPostgresql::run($database->fresh(), new Activity);
|
||||
|
||||
$pgPasswordEntries = collect(databaseStartComposeEnvironment($this->executor, $database->uuid))
|
||||
->filter(fn (string $entry) => str($entry)->startsWith('PGPASSWORD='))
|
||||
->values()
|
||||
->all();
|
||||
|
||||
expect($pgPasswordEntries)->toBe([$expected]);
|
||||
})->with([
|
||||
'model password' => [[], 'PGPASSWORD=model-secret'],
|
||||
'user POSTGRES_PASSWORD' => [['POSTGRES_PASSWORD' => 'user-secret'], 'PGPASSWORD=user-secret'],
|
||||
'user PGPASSWORD' => [['PGPASSWORD' => 'own-secret'], 'PGPASSWORD=own-secret'],
|
||||
]);
|
||||
|
||||
it('adds the password default when the user only sets a *_FILE variant', function (string $action, string $factory, array $data, string $userKey, string $expected) {
|
||||
$database = $factory($this->environment->id, $this->destination, $data);
|
||||
$database->runtime_environment_variables()->create(['key' => $userKey, 'value' => '/run/secrets/x']);
|
||||
|
||||
Reference in New Issue
Block a user