lib.ec.secret_store.Sops: Don't encrypt annotations #168

Merged
Jan Lindemann merged 1 commit from jan/feature/20261107-lib-ec-secret-store-sops-don-t-encrypt-annotations into master 2026-11-07 14:25:47 +01:00 AGit
2 changed files with 30 additions and 0 deletions

View file

@ -21,6 +21,13 @@ from ...Result import Result
from .Base import Base
from .layout import FT_SECRET, FT_SECRET_FILE, FT_SECRET_KEY
# -- SERVICE_SECRET[1] ... entries are used for secret rotation by
# downstream software. That software is specific to janware
# workflows, so the regex should be made configurable, at the
# latest when the need arises, but for now hard-coding it here
# is the safest variant from a security perspective.
UNENCRYPTED_ANNOTATION_REGEX = 'SERVICE_SECRET'
def _sops_dotenv_norm(content: bytes) -> bytes:
# -- The form sops' dotenv serialization produces: blank
# lines dropped, a single trailing newline, everything
@ -263,6 +270,9 @@ class Sops(Base):
'--encrypt',
'--age',
recipient,
'--unencrypted-regex',
UNENCRYPTED_ANNOTATION_REGEX,
'--mac-only-encrypted',
'--output',
path,
tmp,

View file

@ -541,6 +541,26 @@ async def run_tests() -> None:
await store.put('etc/nlc.jw-secret', b'k=1\n', ftype = FT_SECRET)
with open(repo_file, 'rb') as fb:
assert fb.read() == first
# -- The house annotation convention: the SERVICE_SECRET[n]
# keys of an entry are rotation metadata, stored
# unencrypted in the repository file so it stays greppable
# without the age key, while the values stay encrypted
ann = b'__PW__ = hunter2\nSERVICE_SECRET[1] = "__PW__ -> root@localhost"\n'
await store.put('etc/ann.jw-secret', ann, ftype = FT_SECRET)
assert (await store.get('etc/ann.jw-secret')).stdout == ann
repo_file = os.path.join(repo, 'com/example/host/etc/ann.jw-secret')
with open(repo_file) as f:
raw = f.read()
assert 'SERVICE_SECRET[1] = "__PW__ -> root@localhost"' in raw
assert '__PW__ =ENC[' in raw
# -- A key put merges through the stored content: the
# annotation survives the round trip, with the key
# spacing normalized as for all keys of the merge
await store.put('etc/ann.jw-secret/k', b'v', ftype = FT_SECRET_KEY)
with open(repo_file) as f:
raw = f.read()
assert 'SERVICE_SECRET[1]' in raw
assert '"__PW__ -> root@localhost"' in raw
# -- A blob is byte-exact: dropping the trailing newline is
# a change, not a no-op
repo_file = os.path.join(repo, 'com/example/host/etc/nlb.pem.jw-secret-file')