feat(secret): add inject command to substitute secret references in template files#5747
Open
Andarius wants to merge 9 commits into
Open
feat(secret): add inject command to substitute secret references in template files#5747Andarius wants to merge 9 commits into
Andarius wants to merge 9 commits into
Conversation
Adds `scw secret inject` which substitutes `{{ scw://REFERENCE }}` placeholders
in a template file with the actual secret values fetched from Scaleway Secret Manager.
- Reads from --in-file or stdin, writes to --out-file or stdout
- Output files default to 0600 permissions (configurable via --file-mode)
- Supports lookup by secret ID (UUID) or by path/name
- Supports optional @revision and :FIELD (JSON key extraction) suffixes
- Deduplicates identical references — one API call per unique reference
Claude-Session: https://claude.ai/code/session_013hva3ABc7raJdEDkrW33N1
- Use errors.New for static error messages (perfsprint) - Add blank lines before return statements (nlreturn) - Extract revisionLatest constant (goconst) - Move test to package secret_test via export_test.go (testpackage) - Add constants for repeated strings in tests (goconst) - Fix long line in secretInjectRun (golines) - Run scw-doc-gen to update docs/commands/secret.md Claude-Session: https://claude.ai/code/session_013hva3ABc7raJdEDkrW33N1
- Reorder test imports alphabetically in single external block (gci) - Split long fmt.Errorf call across lines (golines) Claude-Session: https://claude.ai/code/session_013hva3ABc7raJdEDkrW33N1
- Fix golden file: help outputs to STDERR in fork (no --list-sub-commands flag) - Fix autocomplete.md: use /bin/bash as shell default to match CI env Claude-Session: https://claude.ai/code/session_013hva3ABc7raJdEDkrW33N1
Unique references are now resolved in parallel using errgroup, reducing latency from O(N) sequential API calls to O(1) wall-clock time for N independent secrets. Claude-Session: https://claude.ai/code/session_013hva3ABc7raJdEDkrW33N1
- Move 'sync' into stdlib import block (no blank line separator) - Remove p := p loop copy (unnecessary since Go 1.22) Claude-Session: https://claude.ai/code/session_013hva3ABc7raJdEDkrW33N1
jremy42
reviewed
Jun 29, 2026
| ) | ||
| } | ||
|
|
||
| if err := os.WriteFile(args.OutFile, []byte(rendered), os.FileMode(mode)); err != nil { |
Contributor
There was a problem hiding this comment.
os.WriteFile only applies the 0600 mode when it creates the file. If out-file already exists (e.g. 0644), it's overwritten but keeps its old permissions, so the plaintext secret can leak into a world-readable file despite the requested 0600.
Enforce it explicitly:
f, err := os.OpenFile(args.OutFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(mode))
if err != nil {
return nil, fmt.Errorf("writing output file: %w", err)
}
defer f.Close()
if err := f.Chmod(os.FileMode(mode)); err != nil { // O_CREATE doesn't re-chmod existing files
return nil, fmt.Errorf("setting output file permissions: %w", err)
}
if _, err := f.Write([]byte(rendered)); err != nil {
return nil, fmt.Errorf("writing output file: %w", err)
}
Contributor
Author
There was a problem hiding this comment.
Hi @jremy42, should be ok in latest commit.
`os.WriteFile` only applies the mode when it creates the file, so writing to a pre-existing `out-file` (e.g. `0644`) kept its old, possibly world-readable permissions and could leak the rendered secret. - replace `os.WriteFile` with a `writeOutputFile` helper that opens with `O_CREATE|O_TRUNC` and `Chmod`s before writing, enforcing the requested mode even when the file already exists - add `Test_WriteOutputFile_EnforcesModeOnExistingFile` regression test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Community Note
Summary
Adds a
scw secret injectcommand that substitutes{{ scw://REFERENCE }}placeholders in template files or stdin with live secret values fetched from the Scaleway Secret Manager API. Multiple distinct secrets are fetched concurrently.Similar to op inject and pass-cli inject
Usage
Reference syntax
UUIDUUID@REVISIONUUID:FIELDNAMEorPATH/NAMEPATH/NAME@REVISION:FIELDREVISIONaccepts an integer,latest, orlatest_enabled.FIELDextracts a top-level string key from a JSON-encoded secret value.Changes
internal/namespaces/secret/v1beta1/custom_inject.go— newsecret injectcommand; parses all{{ scw://... }}references, fetches them concurrently viaerrgroup, then renders the outputinternal/namespaces/secret/v1beta1/custom_inject_test.go— unit tests for parsing, resolution, and renderingdocs/commands/secret.md— generated docs for the new command