Skip to content

Commit 8693eeb

Browse files
aledbfclaude
andcommitted
fix(cli): fail when --secrets-file can't be read or parsed
A missing or unparsable --secrets-file was ignored: up, run-user-commands and build ran with no secrets injected or masked and could exit 0. As the reference CLI's readSecretsFromFile, the file is now read once, before anything else, and a failure ends the command with {"outcome":"error","message":<Node's fs error or "Invalid json data">,"description":"Failed to read/parse secrets"} and exit 1. The file is parsed as jsonc.parse does: comments and any JSON value are accepted, and trailing commas, a BOM and trailing data are not. Values become env vars as JS String() makes them, in JS key order. build, whose --secrets-file the reference lacks, fails the same way. Eight hermetic parity cases compare the error and acceptance paths with the reference for up and run-user-commands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a5b7cbe commit 8693eeb

10 files changed

Lines changed: 575 additions & 103 deletions

‎docs/go-only-features.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ with a warning.
202202
`RUN --mount=type=secret` the same way, and the lifecycle commands as environment
203203
variables. Neither leaves the value in a layer, the image config or a container label.
204204

205+
The file is read as the reference CLI reads it for `up` and `run-user-commands`: JSON with
206+
comments, and a file that cannot be read or parsed (a missing file, a syntax error, a
207+
trailing comma) fails the command with exit code 1 and
208+
`{"outcome":"error","message":…,"description":"Failed to read/parse secrets"}` before
209+
anything is built. `build` fails the same way.
210+
205211
## `--ca-bundle`
206212

207213
Trust extra certificate authorities, such as a TLS-intercepting proxy's, in image builds

‎docs/parity/parity-matrix.yaml‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,56 @@ initial_cases:
21462146
class: required-args
21472147
current_status: match
21482148

2149+
- id: run-user-commands.secrets-file-missing
2150+
lane: contract
2151+
command: run-user-commands
2152+
priority: p1
2153+
docker_required: false
2154+
ts_cmd: "run-user-commands --workspace-folder path-that-does-not-exist --secrets-file path-that-does-not-exist.json"
2155+
asserts: [exit_code, stdout_normalized]
2156+
class: secrets-file
2157+
current_status: match
2158+
notes: "readSecretsFromFile throws ContainerError{description: 'Failed to read/parse secrets'} with Node's ENOENT message, before the configuration is looked up. Go used to ignore an unreadable file and run with no secrets."
2159+
2160+
- id: run-user-commands.secrets-file-invalid-json
2161+
lane: contract
2162+
command: run-user-commands
2163+
priority: p1
2164+
docker_required: false
2165+
setup_cmd: "D=/tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf $D; mkdir -p $D; printf '%s' '{\"A\":\"1\",}' > $D/secrets.json; echo SECRETS_FILE=$D/secrets.json"
2166+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2167+
ts_cmd: "run-user-commands --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2168+
asserts: [exit_code, stdout_normalized]
2169+
class: secrets-file
2170+
current_status: match
2171+
notes: "jsonc.parse reports a trailing comma as a parse error, so the message is 'Invalid json data'. Go used to ignore a malformed file and run with no secrets."
2172+
2173+
- id: run-user-commands.secrets-file-jsonc-config-not-found
2174+
lane: contract
2175+
command: run-user-commands
2176+
priority: p1
2177+
docker_required: false
2178+
setup_cmd: "D=/tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf $D; mkdir -p $D; printf '%s' '{ /* c */ \"A\": 1, \"B\": [1, 2], \"C\": {\"x\": true} }' > $D/secrets.json; echo SECRETS_FILE=$D/secrets.json"
2179+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2180+
ts_cmd: "run-user-commands --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2181+
asserts: [exit_code, stdout_normalized]
2182+
class: secrets-file
2183+
current_status: match
2184+
notes: "jsonc.parse accepts comments and non-string values, so the command gets past the secrets file to the configuration lookup. Go json.Unmarshal into map[string]string used to reject non-string values."
2185+
2186+
- id: run-user-commands.secrets-file-null-value-failure
2187+
lane: contract
2188+
command: run-user-commands
2189+
priority: p1
2190+
docker_required: false
2191+
setup_cmd: "D=/tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf $D; mkdir -p $D; printf '%s' '{\"A\": null}' > $D/secrets.json; echo SECRETS_FILE=$D/secrets.json"
2192+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2193+
ts_cmd: "run-user-commands --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2194+
asserts: [exit_code, stdout_normalized]
2195+
class: secrets-file
2196+
current_status: match
2197+
notes: "createLog masks Object.values(secrets).filter(v => v.length), which throws a TypeError on a null value; the command's catch-all reports it."
2198+
21492199
- id: set-up.missing-container-id
21502200
lane: contract
21512201
command: set-up
@@ -2429,6 +2479,56 @@ initial_cases:
24292479
class: required-args
24302480
current_status: match
24312481

2482+
- id: up.secrets-file-missing
2483+
lane: contract
2484+
command: up
2485+
priority: p1
2486+
docker_required: false
2487+
ts_cmd: "up --workspace-folder path-that-does-not-exist --secrets-file path-that-does-not-exist.json"
2488+
asserts: [exit_code, stdout_normalized]
2489+
class: secrets-file
2490+
current_status: match
2491+
notes: "readSecretsFromFile throws ContainerError{description: 'Failed to read/parse secrets'} with Node's ENOENT message, before the configuration is looked up. Go used to ignore an unreadable file and run with no secrets."
2492+
2493+
- id: up.secrets-file-invalid-json
2494+
lane: contract
2495+
command: up
2496+
priority: p1
2497+
docker_required: false
2498+
setup_cmd: "D=/tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf $D; mkdir -p $D; printf '%s' '{\"A\":\"1\",}' > $D/secrets.json; echo SECRETS_FILE=$D/secrets.json"
2499+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2500+
ts_cmd: "up --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2501+
asserts: [exit_code, stdout_normalized]
2502+
class: secrets-file
2503+
current_status: match
2504+
notes: "jsonc.parse reports a trailing comma as a parse error, so the message is 'Invalid json data'. Go used to ignore a malformed file and run with no secrets."
2505+
2506+
- id: up.secrets-file-jsonc-config-not-found
2507+
lane: contract
2508+
command: up
2509+
priority: p1
2510+
docker_required: false
2511+
setup_cmd: "D=/tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf $D; mkdir -p $D; printf '%s' '{ /* c */ \"A\": 1, \"B\": [1, 2], \"C\": {\"x\": true} }' > $D/secrets.json; echo SECRETS_FILE=$D/secrets.json"
2512+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2513+
ts_cmd: "up --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2514+
asserts: [exit_code, stdout_normalized]
2515+
class: secrets-file
2516+
current_status: match
2517+
notes: "jsonc.parse accepts comments and non-string values, so the command gets past the secrets file to the configuration lookup. Go json.Unmarshal into map[string]string used to reject non-string values."
2518+
2519+
- id: up.secrets-file-null-value-failure
2520+
lane: contract
2521+
command: up
2522+
priority: p1
2523+
docker_required: false
2524+
setup_cmd: "D=/tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}; rm -rf $D; mkdir -p $D; printf '%s' '{\"A\": null}' > $D/secrets.json; echo SECRETS_FILE=$D/secrets.json"
2525+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2526+
ts_cmd: "up --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2527+
asserts: [exit_code, stdout_normalized]
2528+
class: secrets-file
2529+
current_status: match
2530+
notes: "createLog masks Object.values(secrets).filter(v => v.length), which throws a TypeError on a null value; the command's catch-all reports it."
2531+
24322532
- id: up.override-config-missing-error
24332533
lane: contract
24342534
command: up

‎internal/cli/build.go‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type buildOpts struct {
4242
noLockfile bool
4343
frozenLockfile bool
4444
secretsFile string
45+
secrets fileSecrets
4546
caBundle string
4647
lockfileExcludeIDs map[string]bool
4748
}
@@ -148,6 +149,10 @@ func runBuild(ctx context.Context, out Output, opts *buildOpts) error {
148149
configPath = resolvePath(opts.configPath)
149150
}
150151

152+
if opts.secrets, err = loadSecretsFile(opts.secretsFile); err != nil {
153+
return writeErrorJSON(out, coreerrors.ToErrorOutput(err))
154+
}
155+
151156
logger := log.New(log.Options{
152157
Version: cliVersion(),
153158
Level: log.ParseLevel(opts.logLevel),
@@ -388,14 +393,7 @@ func (r *buildRunner) buildDockerfile(ctx context.Context, cfg *config.DevContai
388393
authEnv, authCleanup := bridgeBuildAuth(logger, baseImage, imageNames, cacheFrom, opts.cacheTo)
389394
defer authCleanup()
390395

391-
var buildSecrets []string
392-
if opts.secretsFile != "" {
393-
secrets, secErr := readSecretsFile(opts.secretsFile)
394-
if secErr != nil {
395-
return nil, fmt.Errorf("read secrets file: %w", secErr)
396-
}
397-
buildSecrets = buildxSecrets(secrets, useBuildx, logger)
398-
}
396+
buildSecrets := buildxSecrets(opts.secrets.env(), useBuildx, logger)
399397
caSecrets := caBundleBuildSecrets(opts.caBundle, useBuildx, logger)
400398

401399
buildOpts := docker.BuildOptions{
@@ -821,7 +819,7 @@ func labelComposeImage(ctx context.Context, dockerClient *docker.Client, imageNa
821819
func (r *buildRunner) featureBuildOptions(cfg *config.DevContainer, hostSub *config.HostSubContext) *FeatureBuildOptions {
822820
opts := r.opts
823821
return &FeatureBuildOptions{
824-
Secrets: buildSecretsFromFile(opts.secretsFile),
822+
Secrets: opts.secrets.env(),
825823
CABundle: opts.caBundle,
826824
OverrideFeatureInstallOrder: cfg.OverrideFeatureInstallOrder,
827825
NoCache: opts.noCache,

‎internal/cli/run_user_commands.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type runUserCommandsOpts struct {
3535
defaultUserEnvProbe string
3636
containerSessionDataFolder string
3737
secretsFile string
38+
secrets fileSecrets
3839
terminalColumns givenInt
3940
terminalRows givenInt
4041
}
@@ -97,12 +98,21 @@ func runUserCommands(ctx context.Context, out Output, opts *runUserCommandsOpts)
9798
}
9899
defer closeLog()
99100

101+
var err error
102+
if opts.secrets, err = loadSecretsFile(opts.secretsFile); err != nil {
103+
return writeErrorJSON(out, coreerrors.ToErrorOutput(err))
104+
}
105+
maskValues, err := opts.secrets.maskValues()
106+
if err != nil {
107+
return writeErrorJSON(out, coreerrors.ToErrorOutput(&coreerrors.ContainerError{Description: "An error occurred running user commands in the container.", OriginalError: err}))
108+
}
109+
100110
logger := log.New(log.Options{
101111
Version: cliVersion(),
102112
Level: log.ParseLevel(opts.logLevel),
103113
Format: opts.logFormat,
104114
Writer: logDst,
105-
Secrets: secretValuesFromFile(opts.secretsFile),
115+
Secrets: maskValues,
106116
})
107117

108118
var loadResult *config.LoadResult
@@ -212,7 +222,7 @@ func runUserCommandsInContainer(ctx context.Context, out Output, opts *runUserCo
212222

213223
remoteUser := lifecycleRemoteUser(merged, cfg)
214224
probed := lifecycle.ProbedRemoteEnv(ctx, engine, logger, containerID, remoteUser, lifecycleUserEnvProbe(opts.defaultUserEnvProbe, cfg), opts.containerSessionDataFolder)
215-
remoteEnv := lifecycleRemoteEnv(probed, buildSecretsFromFile(opts.secretsFile), opts.remoteEnvs, cfg)
225+
remoteEnv := lifecycleRemoteEnv(probed, opts.secrets.env(), opts.remoteEnvs, cfg)
216226

217227
shellServer := lifecycle.NewShellServer(ctx, engine, containerID, remoteUser, remoteEnv...)
218228
defer shellServer.Close()

0 commit comments

Comments
 (0)