mirror of
https://github.com/coollabsio/coolify.git
synced 2026-10-28 17:44:36 +08:00
- Write remote secret values as exact dotenv literals in the service .env file and escape "$" where they go directly into a generated compose file (environment, start commands, healthchecks). - Resolve remote secrets before a service or database restart stops the running resource, and show the secret manager error instead of a generic start failure (new RemoteSecretException). - Cache fetched key names for the autocomplete and rate limit how often a user can contact the secret manager. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
35 lines
1.2 KiB
PHP
35 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
/**
|
|
* Formats values fetched from a secret manager so that Docker Compose uses them exactly as they are:
|
|
* no interpolation, no comments, and no extra lines.
|
|
*/
|
|
class RemoteSecretValueFormatter
|
|
{
|
|
/**
|
|
* A value for a dotenv file that Docker Compose reads (a project .env or an env_file). Single
|
|
* quotes keep the value literal, also newlines and "#". A value with a single quote, or one that
|
|
* ends with a backslash (which would escape the closing quote), uses double quotes instead,
|
|
* where \, " and $ must be escaped ($$ stops interpolation).
|
|
*/
|
|
public static function dotenv(string $value): string
|
|
{
|
|
if (! str_contains($value, "'") && ! str_ends_with($value, '\\')) {
|
|
return "'".$value."'";
|
|
}
|
|
|
|
return '"'.str_replace(['\\', '"', '$'], ['\\\\', '\\"', '$$'], $value).'"';
|
|
}
|
|
|
|
/**
|
|
* A value written directly into a generated compose file, such as an `environment:` entry,
|
|
* a command, or a healthcheck. Compose does not parse quotes there and only interpolates "$".
|
|
*/
|
|
public static function composeFile(string $value): string
|
|
{
|
|
return str_replace('$', '$$', $value);
|
|
}
|
|
}
|