Skip to content

feat: accept SSH key and known_hosts as env content, not just file paths - #267

Open
venkatamutyala wants to merge 2 commits into
mainfrom
feat/ssh-material-from-env
Open

feat: accept SSH key and known_hosts as env content, not just file paths#267
venkatamutyala wants to merge 2 commits into
mainfrom
feat/ssh-material-from-env

Conversation

@venkatamutyala

Copy link
Copy Markdown
Contributor

The sftp backend required RESTIC_SSH_KEY and RESTIC_SSH_KNOWN_HOSTS as paths to mounted files. That forces 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, 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

  • The key is validated with ssh-keygen -y before 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.
  • Trailing newline normalised — env round-trips routinely drop the final one and ssh rejects malformed PEM armour.
  • Setting both forms is a hard error, not a silent precedence rule.
  • known_hosts gets 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.
  • The "no key" error now says plainly that restic cannot use password auth, citing its own requirement for a passwordless key. That's the assumption people arrive with for SFTP, and the old message didn't address it.

Why now

A Storage Box deployment put the key in the same Vault path as RESTIC_REPOSITORY and RESTIC_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 local sftp-server via sftp.command, so no network or server needed).

CI runs both suites on this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MM8CswSJktsdaWuPVuZ16r

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.
@github-actions github-actions Bot added the patch label Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant