Skip to content

Commit bf3e795

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 6806c35 commit bf3e795

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
@@ -2079,6 +2079,56 @@ initial_cases:
20792079
class: required-args
20802080
current_status: match
20812081

2082+
- id: run-user-commands.secrets-file-missing
2083+
lane: contract
2084+
command: run-user-commands
2085+
priority: p1
2086+
docker_required: false
2087+
ts_cmd: "run-user-commands --workspace-folder path-that-does-not-exist --secrets-file path-that-does-not-exist.json"
2088+
asserts: [exit_code, stdout_normalized]
2089+
class: secrets-file
2090+
current_status: match
2091+
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."
2092+
2093+
- id: run-user-commands.secrets-file-invalid-json
2094+
lane: contract
2095+
command: run-user-commands
2096+
priority: p1
2097+
docker_required: false
2098+
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"
2099+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2100+
ts_cmd: "run-user-commands --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2101+
asserts: [exit_code, stdout_normalized]
2102+
class: secrets-file
2103+
current_status: match
2104+
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."
2105+
2106+
- id: run-user-commands.secrets-file-jsonc-config-not-found
2107+
lane: contract
2108+
command: run-user-commands
2109+
priority: p1
2110+
docker_required: false
2111+
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"
2112+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2113+
ts_cmd: "run-user-commands --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2114+
asserts: [exit_code, stdout_normalized]
2115+
class: secrets-file
2116+
current_status: match
2117+
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."
2118+
2119+
- id: run-user-commands.secrets-file-null-value-failure
2120+
lane: contract
2121+
command: run-user-commands
2122+
priority: p1
2123+
docker_required: false
2124+
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"
2125+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2126+
ts_cmd: "run-user-commands --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2127+
asserts: [exit_code, stdout_normalized]
2128+
class: secrets-file
2129+
current_status: match
2130+
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."
2131+
20822132
- id: set-up.missing-container-id
20832133
lane: contract
20842134
command: set-up
@@ -2362,6 +2412,56 @@ initial_cases:
23622412
class: required-args
23632413
current_status: match
23642414

2415+
- id: up.secrets-file-missing
2416+
lane: contract
2417+
command: up
2418+
priority: p1
2419+
docker_required: false
2420+
ts_cmd: "up --workspace-folder path-that-does-not-exist --secrets-file path-that-does-not-exist.json"
2421+
asserts: [exit_code, stdout_normalized]
2422+
class: secrets-file
2423+
current_status: match
2424+
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."
2425+
2426+
- id: up.secrets-file-invalid-json
2427+
lane: contract
2428+
command: up
2429+
priority: p1
2430+
docker_required: false
2431+
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"
2432+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2433+
ts_cmd: "up --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2434+
asserts: [exit_code, stdout_normalized]
2435+
class: secrets-file
2436+
current_status: match
2437+
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."
2438+
2439+
- id: up.secrets-file-jsonc-config-not-found
2440+
lane: contract
2441+
command: up
2442+
priority: p1
2443+
docker_required: false
2444+
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"
2445+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2446+
ts_cmd: "up --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2447+
asserts: [exit_code, stdout_normalized]
2448+
class: secrets-file
2449+
current_status: match
2450+
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."
2451+
2452+
- id: up.secrets-file-null-value-failure
2453+
lane: contract
2454+
command: up
2455+
priority: p1
2456+
docker_required: false
2457+
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"
2458+
cleanup_cmd: "rm -rf /tmp/parity-${PARITY_CASE_ID}-${PARITY_SIDE}"
2459+
ts_cmd: "up --workspace-folder path-that-does-not-exist --secrets-file ${SECRETS_FILE}"
2460+
asserts: [exit_code, stdout_normalized]
2461+
class: secrets-file
2462+
current_status: match
2463+
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."
2464+
23652465
- id: up.override-config-missing-error
23662466
lane: contract
23672467
command: up

‎internal/cli/build.go‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type buildOpts struct {
4444
noLockfile bool
4545
frozenLockfile bool
4646
secretsFile string
47+
secrets fileSecrets
4748
caBundle string
4849
lockfileExcludeIDs map[string]bool
4950
}
@@ -150,6 +151,10 @@ func runBuild(ctx context.Context, out Output, opts *buildOpts) error {
150151
configPath = resolvePath(opts.configPath)
151152
}
152153

154+
if opts.secrets, err = loadSecretsFile(opts.secretsFile); err != nil {
155+
return writeErrorJSON(out, coreerrors.ToErrorOutput(err))
156+
}
157+
153158
logger := log.New(log.Options{
154159
Version: cliVersion(),
155160
Level: log.ParseLevel(opts.logLevel),
@@ -390,14 +395,7 @@ func (r *buildRunner) buildDockerfile(ctx context.Context, cfg *config.DevContai
390395
authEnv, authCleanup := bridgeBuildAuth(logger, baseImage, imageNames, cacheFrom, opts.cacheTo)
391396
defer authCleanup()
392397

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

403401
buildOpts := docker.BuildOptions{
@@ -829,7 +827,7 @@ func labelComposeImage(ctx context.Context, dockerClient *docker.Client, imageNa
829827
func (r *buildRunner) featureBuildOptions(cfg *config.DevContainer, hostSub *config.HostSubContext) *FeatureBuildOptions {
830828
opts := r.opts
831829
return &FeatureBuildOptions{
832-
Secrets: buildSecretsFromFile(opts.secretsFile),
830+
Secrets: opts.secrets.env(),
833831
CABundle: opts.caBundle,
834832
OverrideFeatureInstallOrder: cfg.OverrideFeatureInstallOrder,
835833
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)