Skip to content

[DependencyInjection] Fix loading all env vars from secrets when only a subset is needed #53631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Secrets/SodiumVault.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Secrets;

use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
use Symfony\Component\String\LazyString;
use Symfony\Component\VarExporter\VarExporter;

/**
Expand Down Expand Up @@ -169,7 +170,14 @@ public function list(bool $reveal = false): array

public function loadEnvVars(): array
{
return $this->list(true);
$envs = [];
$reveal = $this->reveal(...);

foreach ($this->list() as $name => $value) {
$envs[$name] = LazyString::fromCallable($reveal, $name);
}

return $envs;
}

private function loadKeys(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
interface EnvVarLoaderInterface
{
/**
* @return string[] Key/value pairs that can be accessed using the regular "%env()%" syntax
* @return array<string|\Stringable> Key/value pairs that can be accessed using the regular "%env()%" syntax
*/
public function loadEnvVars(): array;
}
20 changes: 16 additions & 4 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,16 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
if (false !== $i || 'string' !== $prefix) {
$env = $getEnv($name);
} elseif ('' === ($env = $_ENV[$name] ?? (str_starts_with($name, 'HTTP_') ? null : ($_SERVER[$name] ?? null)))
|| (false !== $env && false === ($env = $env ?? getenv($name) ?? false)) // null is a possible value because of thread safety issues
|| (false !== $env && false === $env ??= getenv($name) ?? false) // null is a possible value because of thread safety issues
) {
foreach ($this->loadedVars as $vars) {
if (false !== ($env = ($vars[$name] ?? $env)) && '' !== $env) {
foreach ($this->loadedVars as $i => $vars) {
if (false === $env = $vars[$name] ?? $env) {
continue;
}
if ($env instanceof \Stringable) {
$this->loadedVars[$i][$name] = $env = (string) $env;
}
if ('' !== ($env ?? '')) {
break;
}
}
Expand All @@ -185,7 +191,13 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
continue;
}
$this->loadedVars[] = $vars = $loader->loadEnvVars();
if (false !== ($env = ($vars[$name] ?? $env)) && '' !== $env) {
if (false === $env = $vars[$name] ?? $env) {
continue;
}
if ($env instanceof \Stringable) {
$this->loadedVars[array_key_last($this->loadedVars)][$name] = $env = (string) $env;
}
if ('' !== ($env ?? '')) {
$ended = false;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,12 @@ public function loadEnvVars(): array
return [
'FOO_ENV_LOADER' => '123',
'BAZ_ENV_LOADER' => '',
'LAZY_ENV_LOADER' => new class() {
public function __toString()
{
return '';
}
},
];
}
};
Expand All @@ -819,6 +825,12 @@ public function loadEnvVars(): array
'FOO_ENV_LOADER' => '234',
'BAR_ENV_LOADER' => '456',
'BAZ_ENV_LOADER' => '567',
'LAZY_ENV_LOADER' => new class() {
public function __toString()
{
return '678';
}
},
];
}
};
Expand All @@ -841,6 +853,9 @@ public function loadEnvVars(): array
$result = $processor->getEnv('string', 'FOO_ENV_LOADER', function () {});
$this->assertSame('123', $result); // check twice

$result = $processor->getEnv('string', 'LAZY_ENV_LOADER', function () {});
$this->assertSame('678', $result);

unset($_ENV['BAZ_ENV_LOADER']);
unset($_ENV['BUZ_ENV_LOADER']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/String/LazyString.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function fromCallable(callable|array $callback, mixed ...$argument
$callback[1] ??= '__invoke';
}
$value = $callback(...$arguments);
$callback = self::getPrettyName($callback);
$callback = !\is_scalar($value) && !$value instanceof \Stringable ? self::getPrettyName($callback) : 'callable';
$arguments = null;
}

Expand Down