fix(compose): strip and resolve pre-deploy hook secrets like service env (#424)

* fix(compose): strip and resolve pre-deploy hook secrets like service env (fixes #422)

normaliseContainerForStore only cleared Config.Env and
ServiceSpec.Container.Env before writing a container record to the
replicated Corrosion store, leaving ServiceSpec.PreDeploy.Env in place.
A secret set via x-pre_deploy.environment therefore replicated to
every machine in the cluster, unlike the same value set via
environment.

ResolveSecrets had the same asymmetry in the other direction: it only
resolved secret://name references in service.Environment, so a
reference placed in the pre-deploy hook's environment passed through
unresolved as a literal string. HasCommandSecretRefs is extended the
same way for consistency.

Add tests pinning both behaviours.

* update docs that secret references are available for pre-deploy hooks

---------

Co-authored-by: Pasha Sviderski <me@psviderski.name>
This commit is contained in:
Vaibhav Yadav
2026-10-12 15:48:47 +10:00
committed by GitHub
co-authored by Pasha Sviderski
parent ce1c6bd05a
commit 8ddbe3c606
6 changed files with 217 additions and 32 deletions
+54 -23
View File
@@ -89,40 +89,53 @@ func ResolveSecrets(ctx context.Context, project *types.Project) error {
return v, nil
}
// Resolve only secret references set as values for environment variables in enabled services.
for _, service := range project.Services {
for k, v := range service.Environment {
if v == nil {
continue
}
secretName, ok := secretRefName(*v)
if !ok {
continue
}
value, err := resolve(secretName)
if err != nil {
// Resolve secret references set as values for environment variables in the service.
if err := resolveEnvSecrets(service.Environment, resolve); err != nil {
return err
}
// Resolve secret references set as values for the pre-deploy hook's environment variables, if any.
if hook, ok := service.Extensions[PreDeployHookExtensionKey].(PreDeployHook); ok {
if err := resolveEnvSecrets(hook.Environment, resolve); err != nil {
return err
}
service.Environment[k] = &value
}
}
return nil
}
// HasCommandSecretRefs reports whether any service environment references a secret that is resolved by running
// a command.
// resolveEnvSecrets resolves 'secret://name' references found as values in env to actual secret values using
// resolve, setting each referenced variable to the secret's value in place. Variables that are not secret
// references are left untouched.
func resolveEnvSecrets(env types.MappingWithEquals, resolve func(string) (string, error)) error {
for k, v := range env {
if v == nil {
continue
}
secretName, ok := secretRefName(*v)
if !ok {
continue
}
value, err := resolve(secretName)
if err != nil {
return err
}
env[k] = &value
}
return nil
}
// HasCommandSecretRefs reports whether any service environment, including a pre-deploy hook's environment,
// references a secret that is resolved by running a command.
func HasCommandSecretRefs(project *types.Project) bool {
for _, service := range project.Services {
for _, v := range service.Environment {
if v == nil {
continue
}
name, ok := secretRefName(*v)
if !ok {
continue
}
if secret, ok := project.Secrets[name]; ok && secret.Driver == secretExecDriver {
if envHasCommandSecretRef(service.Environment, project) {
return true
}
if hook, ok := service.Extensions[PreDeployHookExtensionKey].(PreDeployHook); ok {
if envHasCommandSecretRef(hook.Environment, project) {
return true
}
}
@@ -130,6 +143,24 @@ func HasCommandSecretRefs(project *types.Project) bool {
return false
}
// envHasCommandSecretRef reports whether env contains a 'secret://name' reference to a secret resolved
// by running a command.
func envHasCommandSecretRef(env types.MappingWithEquals, project *types.Project) bool {
for _, v := range env {
if v == nil {
continue
}
name, ok := secretRefName(*v)
if !ok {
continue
}
if secret, ok := project.Secrets[name]; ok && secret.Driver == secretExecDriver {
return true
}
}
return false
}
// secretValue resolves a secret to its value depending on its source: an 'exec' driver command,
// an environment variable, or a file. A single trailing newline ('\n' or '\r\n') is stripped from the command
// output as command-line tools commonly append one. All other whitespace is kept. Environment and file values are