Skip to content

lib(sops): make sops_encrypt and bitwarden-to-sops init writes atomic (#6555) - #6565

Merged
dezren39 merged 1 commit into
mainfrom
queue/11219-6555-sops-atomic-writers
Nov 6, 2026
Merged

dezren39 merged 1 commit into
mainfrom
queue/11219-6555-sops-atomic-writers

Conversation

@dezren39

@dezren39 dezren39 commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #6555 · queue task 11219

Problem

Two lib/ sops writers used a plain redirect to the target:

  • lib/sops_encrypt.sh — sops --encrypt <(cat -) >"$output_file" truncated the previous ciphertext before sops ran, and the unconditional echo on the next line masked any failure with status 0 (both the script and sourced forms).
  • lib/bitwarden-to-sops.sh init path — sops -e ... >"$TARGET_FILE" created the target before sops ran; under set -e a failure stopped the script but left a zero-byte target that routed every later run to the sops set update branch instead of retrying init.

Change

Both writers now follow the existing lib/bws-to-sops.sh oracle pattern: encrypt to a mktemp file in the target's directory, require sops exit 0 and non-empty output, then mv over the target.

  • sops_encrypt: on failure the temp file is removed, an error goes to stderr, the function returns non-zero, the previous output stays byte-identical, and "Encrypted output saved" prints only on success. (The renamed file carries mktemp's 0600 permissions — stricter than the previous umask default, appropriate for ciphertext.)
  • bitwarden-to-sops.sh init: on failure the temp file is removed and the script exits non-zero with no file at $TARGET_FILE, so the next run retries the init branch.

Tests

New lib/sops-atomic-writers.test.sh (stubbed sops fail/ok + stubbed bws, oracle = byte-identity of the pre-existing target), wired into scripts-test.yml:

  • sops_encrypt, script and sourced forms: failing sops → previous output byte-identical + non-zero status + no success message; succeeding sops → output replaced.
  • bitwarden-to-sops init: failing sops → no file at the target; succeeding sops → target created with the payload.
  • No leftover temp files in any case.

Verified on the laptop: 23/23 checks pass on this branch; the same suite against the pre-fix files at d54934a99a fails 7 checks (exit 1), so it is a real regression test. bash -n clean on all three scripts.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…#6555)

sops_encrypt wrote ciphertext with a plain redirect: the shell truncated the target before sops ran, so a failed encryption destroyed the previous ciphertext, and the unconditional success echo masked the failure with status 0 in both the script and sourced forms. The bitwarden-to-sops init path had the same redirect shape: set -e stopped the script but the zero-byte target it created routed every later run to the sops set update branch instead of retrying init.

Both writers now follow the lib/bws-to-sops.sh oracle: encrypt to a mktemp file beside the target, require sops exit 0 and non-empty output, then mv over the target. sops_encrypt returns non-zero and prints no success message on failure; a failed init leaves no file at the target so the next run retries init.

Regression coverage: lib/sops-atomic-writers.test.sh stubs sops (fail/ok) and bws, and asserts byte-identity of a pre-existing target after failure, non-zero status in script and sourced forms, replacement on success, and no target file after a failed init. 23/23 pass on the fix; the suite fails 7 checks against the pre-fix files. Wired into scripts-test.yml.
@dezren39
dezren39 requested a review from a team as a code owner November 6, 2026 16:37
@coderabbitai

coderabbitai Bot commented Nov 6, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 4 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 61e53e77-da90-42c6-be51-e97b2fd572f1
Commits

Reviewing files that changed from the base of the PR and between bbbf402 and acb682f.

Files selected for processing: 4

  • .github/workflows/scripts-test.yml
  • lib/bitwarden-to-sops.sh
  • lib/sops-atomic-writers.test.sh
  • lib/sops_encrypt.sh
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T16:39:32.541097Z acb682f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant