feat: accept SSH key and known_hosts as env content, not just file paths - #267
Open
venkatamutyala wants to merge 2 commits into
Open
feat: accept SSH key and known_hosts as env content, not just file paths#267venkatamutyala wants to merge 2 commits into
venkatamutyala wants to merge 2 commits into
Conversation
The sftp backend needed RESTIC_SSH_KEY and RESTIC_SSH_KNOWN_HOSTS as PATHS to mounted files. That forced a second Secret and a volume mount on every job, because envFrom cannot deliver files -- awkward when the natural Kubernetes shape is a Secret projected as environment variables. Adds RESTIC_SSH_KEY_CONTENT and RESTIC_SSH_KNOWN_HOSTS_CONTENT, which carry the material itself. Both still end up as a private mode-0600 file, which is what ssh actually reads, so the security posture is unchanged: it is a Kubernetes Secret either way. Details that matter: - The key is validated with `ssh-keygen -y` before use. A PEM that lost its newlines in a secret-manager round trip is the usual failure, and it would otherwise surface much later as an opaque ssh auth error. - A trailing newline is normalised on, and duplicates stripped, since env round-trips commonly drop the final one and ssh rejects malformed armour. - Setting both the path and the content form is a hard error rather than a silent precedence rule. - known_hosts gets the same treatment. Unlike the key it is NOT secret -- it is a public host key -- so the content form is usually cleanest as a literal in deployment config rather than a secret at all. - The "no key" error now states plainly that restic cannot use password authentication, citing its own requirement for a passwordless key, since that is the assumption people arrive with for SFTP. Six new checks: key accepted from env content, known_hosts accepted from env content, the written key is mode 600, a mangled key is rejected with an actionable message, path-and-content together is refused, and a full backup+restore round trip over the real SFTP backend using only env-supplied material.
The refactor reassigned `key` to the mode-0600 copy so it matches what sftp.args actually passes to ssh. That was right, but it also changed the log line to print /tmp/.restic-ssh/id instead of the path the operator configured -- which is the implementation detail, not the answer to "which key is it using?". Keeps a separate key_src for logging: the configured path, or "RESTIC_SSH_KEY_CONTENT (env)" when the key came from an environment variable. This is what the CI failure caught. The assertion was checking the log names the configured key, which is behaviour worth keeping rather than a stale expectation to relax.
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.
The sftp backend required
RESTIC_SSH_KEYandRESTIC_SSH_KNOWN_HOSTSas paths to mounted files. That forces a second Secret and a volume mount on every job, becauseenvFromcannot deliver files — awkward when the natural Kubernetes shape is a Secret projected as environment variables.Adds
RESTIC_SSH_KEY_CONTENTandRESTIC_SSH_KNOWN_HOSTS_CONTENT, carrying the material itself.Both forms still end up as a private mode-0600 file, which is what ssh actually reads — so the security posture is unchanged. It's a Kubernetes Secret either way.
Details that matter
ssh-keygen -ybefore use. A PEM that lost its newlines in a secret-manager round trip is the common failure, and it would otherwise surface much later as an opaque ssh auth error at 01:15.known_hostsgets the same treatment. Unlike the key it is not secret — it's a public host key — so the content form is usually cleanest as a literal in deployment config rather than a secret at all.Why now
A Storage Box deployment put the key in the same Vault path as
RESTIC_REPOSITORYandRESTIC_PASSWORD, as an env-var-named key. That's the simpler layout and there was no good reason to reject it — the file requirement was an artefact of how the code read the material, not a real constraint.Result: one Vault path, no volume mounts, no second ExternalSecret.
Tests
Six new checks in
backend-test.sh: key accepted from env content; known_hosts accepted from env content; the written key is mode 600; a mangled key rejected with an actionable message; path-and-content together refused; and a full backup + restore round trip over the real SFTP backend using only env-supplied material (driven against a localsftp-serverviasftp.command, so no network or server needed).CI runs both suites on this PR.
🤖 Generated with Claude Code
https://claude.ai/code/session_01MM8CswSJktsdaWuPVuZ16r