Skip to content

Commit cf21a9c

Browse files
aledbfclaude
andcommitted
fix(cli): let --secrets-file values win over remoteEnv in hooks
The reference runs every lifecycle hook with {...remoteEnv, ...secrets}, where remoteEnv is the probed env, then --remote-env, then the config's remoteEnv. up and run-user-commands put the secrets before both remoteEnv sources, so a remoteEnv entry of the same name hid the secret. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4660f5f commit cf21a9c

6 files changed

Lines changed: 45 additions & 42 deletions

File tree

‎docs/parity/parity-matrix.yaml‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3061,6 +3061,20 @@ initial_cases:
30613061
class: lifecycle-runtime
30623062
current_status: match
30633063

3064+
- id: run-user-commands.secrets-override-remote-env-success
3065+
lane: runtime
3066+
command: run-user-commands
3067+
priority: p1
3068+
docker_required: true
3069+
ts_cmd: "run-user-commands --workspace-folder ${WORKSPACE} --secrets-file ${SECRETS_FILE} --remote-env TOKEN=cli --remote-env CONFIG_WINS=cli --id-label parity.case=${PARITY_CASE_ID}-${PARITY_SIDE}"
3070+
setup_cmd: "WORKSPACE=/tmp/parity-secrets-env-run-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf ${WORKSPACE}; mkdir -p ${WORKSPACE}; printf '%s' '{\"image\":\"alpine:3.20\",\"overrideCommand\":true,\"remoteEnv\":{\"TOKEN\":\"config\",\"CONFIG_WINS\":\"config\"},\"postCreateCommand\":\"printenv TOKEN CONFIG_WINS > env.postCreateCommand.testMarker\"}' > ${WORKSPACE}/.devcontainer.json; SECRETS_FILE=${WORKSPACE}/test-secrets-temp.json; printf '%s' '{\"TOKEN\":\"SecretValue1\"}' > ${SECRETS_FILE}; if [ \"${PARITY_SIDE}\" = \"ts\" ]; then ${PARITY_CLI_TS} up --workspace-folder ${WORKSPACE} --skip-post-create --id-label parity.case=${PARITY_CASE_ID}-${PARITY_SIDE} --remove-existing-container >/dev/null; else ${PARITY_CLI_GO} up --workspace-folder ${WORKSPACE} --skip-post-create --id-label parity.case=${PARITY_CASE_ID}-${PARITY_SIDE} --remove-existing-container >/dev/null; fi; echo WORKSPACE=$WORKSPACE; echo SECRETS_FILE=$SECRETS_FILE"
3071+
verify_cmd: "cat ${WORKSPACE}/env.postCreateCommand.testMarker"
3072+
cleanup_cmd: "IDS=$(docker ps -aq --filter label=parity.case=${PARITY_CASE_ID}-${PARITY_SIDE}); [ -n \"$IDS\" ] && docker rm -f $IDS >/dev/null 2>&1 || true; rm -rf ${WORKSPACE}"
3073+
asserts: [exit_code, stdout_normalized]
3074+
class: lifecycle-runtime
3075+
current_status: match
3076+
notes: "Same precedence as up.secrets-override-remote-env-success: run-user-commands hands runLifecycleHooks the probed remoteEnv and the secrets, and each hook runs with {...remoteEnv, ...secrets}."
3077+
30643078
- id: run-user-commands.workspace-subfolder-config-success
30653079
lane: runtime
30663080
command: run-user-commands
@@ -3794,6 +3808,20 @@ initial_cases:
37943808
current_status: match
37953809
arm64_required: true
37963810

3811+
- id: up.secrets-override-remote-env-success
3812+
lane: runtime
3813+
command: up
3814+
priority: p1
3815+
docker_required: true
3816+
ts_cmd: "up --workspace-folder ${WORKSPACE} --secrets-file ${SECRETS_FILE} --remote-env TOKEN=cli --remote-env CONFIG_WINS=cli --id-label parity.case=${PARITY_CASE_ID}-${PARITY_SIDE} --remove-existing-container"
3817+
setup_cmd: "WORKSPACE=/tmp/parity-secrets-env-up-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf ${WORKSPACE}; mkdir -p ${WORKSPACE}; printf '%s' '{\"image\":\"alpine:3.20\",\"overrideCommand\":true,\"remoteEnv\":{\"TOKEN\":\"config\",\"CONFIG_WINS\":\"config\"},\"postCreateCommand\":\"printenv TOKEN CONFIG_WINS > env.postCreateCommand.testMarker\"}' > ${WORKSPACE}/.devcontainer.json; SECRETS_FILE=${WORKSPACE}/test-secrets-temp.json; printf '%s' '{\"TOKEN\":\"SecretValue1\"}' > ${SECRETS_FILE}; echo WORKSPACE=$WORKSPACE; echo SECRETS_FILE=$SECRETS_FILE"
3818+
verify_cmd: "cat ${WORKSPACE}/env.postCreateCommand.testMarker"
3819+
cleanup_cmd: "docker rm -f ${CONTAINER_ID} >/dev/null 2>&1 || true; rm -rf ${WORKSPACE}"
3820+
asserts: [exit_code, stdout_normalized]
3821+
class: up-runtime
3822+
current_status: match
3823+
notes: "Lifecycle hooks see {...remoteEnv, ...secrets} (injectHeadless.ts runLifecycleCommand), where remoteEnv is the probed env, then --remote-env, then the config's remoteEnv (probeRemoteEnv): a --secrets-file value beats both remoteEnv sources of the same name, and the config's remoteEnv beats --remote-env. Go put the secrets before both, so remoteEnv won."
3824+
37973825
- id: up.workspace-secrets-success
37983826
lane: runtime
37993827
command: up

‎internal/cli/run_user_commands.go‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ func runUserCommandsInContainer(ctx context.Context, out Output, opts *runUserCo
222222

223223
remoteUser := lifecycleRemoteUser(merged, cfg)
224224
probed := lifecycle.ProbedRemoteEnv(ctx, engine, logger, containerID, remoteUser, lifecycleUserEnvProbe(opts.defaultUserEnvProbe, cfg), opts.containerSessionDataFolder)
225-
remoteEnv := lifecycleRemoteEnv(probed, opts.secrets.env(), opts.remoteEnvs, cfg)
225+
var cfgRemoteEnv map[string]*string
226+
if cfg != nil {
227+
cfgRemoteEnv = cfg.RemoteEnv
228+
}
229+
remoteEnv := hookRemoteEnv(probed, opts.remoteEnvs, cfgRemoteEnv, opts.secrets.env())
226230

227231
shellServer := lifecycle.NewShellServer(ctx, engine, containerID, remoteUser, remoteEnv...)
228232
defer shellServer.Close()
@@ -280,19 +284,3 @@ func lifecycleUserEnvProbe(defaultProbe string, cfg *config.DevContainer) lifecy
280284
return lifecycle.UserEnvProbeStrategy(defaultProbe)
281285
}
282286

283-
func lifecycleRemoteEnv(probed map[string]string, secrets, remoteEnvs []string, cfg *config.DevContainer) []string {
284-
var env []string
285-
for k, v := range probed {
286-
env = append(env, fmt.Sprintf("%s=%s", k, v))
287-
}
288-
env = append(env, secrets...)
289-
env = append(env, remoteEnvs...)
290-
if cfg != nil {
291-
for k, v := range cfg.RemoteEnv {
292-
if v != nil {
293-
env = append(env, fmt.Sprintf("%s=%s", k, *v))
294-
}
295-
}
296-
}
297-
return env
298-
}

‎internal/cli/run_user_commands_held_test.go‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cli
33
import (
44
"os"
55
"path/filepath"
6-
"slices"
76
"strings"
87
"testing"
98

@@ -183,18 +182,3 @@ func TestLifecycleUserEnvProbe(t *testing.T) {
183182
}
184183
}
185184
}
186-
187-
func TestLifecycleRemoteEnvOrder(t *testing.T) {
188-
probed := map[string]string{"PATH": "/probed"}
189-
secrets := []string{"TOKEN=s"}
190-
cli := []string{"PATH=/cli"}
191-
value := "/config"
192-
cfg := &config.DevContainer{RemoteEnv: map[string]*string{"PATH": &value, "UNSET": nil}}
193-
194-
if got, want := lifecycleRemoteEnv(probed, secrets, cli, cfg), []string{"PATH=/probed", "TOKEN=s", "PATH=/cli", "PATH=/config"}; !slices.Equal(got, want) {
195-
t.Errorf("with config: %v, want %v", got, want)
196-
}
197-
if got, want := lifecycleRemoteEnv(probed, secrets, cli, nil), []string{"PATH=/probed", "TOKEN=s", "PATH=/cli"}; !slices.Equal(got, want) {
198-
t.Errorf("without config: %v, want %v", got, want)
199-
}
200-
}

‎internal/cli/setup.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func setUpContainer(ctx context.Context, out Output, engine *docker.EngineClient
159159
}
160160
probeStrategy := lifecycle.UserEnvProbeStrategy(cmp.Or(cfgProbe, opts.defaultUserEnvProbe))
161161
probed := lifecycle.ProbedRemoteEnv(ctx, engine, logger, containerID, merged.RemoteUser, probeStrategy, opts.containerSessionDataFolder)
162-
shellServer := lifecycle.NewShellServer(ctx, engine, containerID, merged.RemoteUser, hookRemoteEnv(probed, opts.remoteEnvs, cfgRemoteEnv)...)
162+
shellServer := lifecycle.NewShellServer(ctx, engine, containerID, merged.RemoteUser, hookRemoteEnv(probed, opts.remoteEnvs, cfgRemoteEnv, nil)...)
163163
defer shellServer.Close()
164164

165165
executor := &lifecycle.ShellExecutor{Server: shellServer, Log: logger}

‎internal/cli/up.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,7 @@ func (r *upRunner) runLifecycleForUp(ctx context.Context, containerID string, cf
986986
logger, engine, opts := r.log, r.engine, r.opts
987987
probeStrategy := lifecycle.UserEnvProbeStrategy(cmp.Or(cfg.UserEnvProbe, opts.defaultUserEnvProbe))
988988
probed := lifecycle.ProbedRemoteEnv(ctx, engine, logger, containerID, remoteUser, probeStrategy, opts.containerSessionDataFolder)
989-
extra := append(opts.secrets.env(), opts.remoteEnvs...)
990-
mergedRemoteEnv := hookRemoteEnv(probed, extra, cfg.RemoteEnv)
989+
mergedRemoteEnv := hookRemoteEnv(probed, opts.remoteEnvs, cfg.RemoteEnv, opts.secrets.env())
991990

992991
shellServer := lifecycle.NewShellServer(ctx, engine, containerID, remoteUser, mergedRemoteEnv...)
993992
defer shellServer.Close()
@@ -1033,18 +1032,18 @@ func (r *upRunner) checkContainerSubstitution(inspectResp container.InspectRespo
10331032
return checkContainerSubstitution(containerEnv, cfg.ConfigFilePath, raw, imagemeta.MergeConfiguration(entries))
10341033
}
10351034

1036-
func hookRemoteEnv(probed map[string]string, extra []string, cfgRemoteEnv map[string]*string) []string {
1035+
func hookRemoteEnv(probed map[string]string, remoteEnvs []string, cfgRemoteEnv map[string]*string, secrets []string) []string {
10371036
var env []string
10381037
for k, v := range probed {
10391038
env = append(env, fmt.Sprintf("%s=%s", k, v))
10401039
}
1041-
env = append(env, extra...)
1040+
env = append(env, remoteEnvs...)
10421041
for k, v := range cfgRemoteEnv {
10431042
if v != nil {
10441043
env = append(env, fmt.Sprintf("%s=%s", k, *v))
10451044
}
10461045
}
1047-
return env
1046+
return append(env, secrets...)
10481047
}
10491048

10501049
func hookContainerError(hookErr error, containerID, fallback string) *coreerrors.ContainerError {

‎internal/cli/up_held_test.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,16 @@ func TestParseComposeServiceBuildReadsTheServicesBuild(t *testing.T) {
467467
func TestHookRemoteEnvOrdersLaterSourcesLast(t *testing.T) {
468468
got := hookRemoteEnv(
469469
map[string]string{"P": "probed"},
470-
[]string{"S=secret", "R=cli"},
471-
map[string]*string{"C": strPtr("config"), "UNSET": nil},
470+
[]string{"R=cli", "TOKEN=cli"},
471+
map[string]*string{"TOKEN": strPtr("config"), "UNSET": nil},
472+
[]string{"TOKEN=secret"},
472473
)
473-
if want := []string{"P=probed", "S=secret", "R=cli", "C=config"}; !reflect.DeepEqual(got, want) {
474+
if want := []string{"P=probed", "R=cli", "TOKEN=cli", "TOKEN=config", "TOKEN=secret"}; !reflect.DeepEqual(got, want) {
474475
t.Errorf("env = %q, want %q", got, want)
475476
}
477+
if got, want := hookRemoteEnv(nil, []string{"R=cli"}, nil, nil), []string{"R=cli"}; !reflect.DeepEqual(got, want) {
478+
t.Errorf("without config or secrets: env = %q, want %q", got, want)
479+
}
476480
}
477481

478482
func TestDropRanHooksAsksEveryMarkerAndDropsWhatRan(t *testing.T) {

0 commit comments

Comments
 (0)