Skip to content

Support signing commits with SSH key - #1035

Merged
hiddeco merged 14 commits into
mainfrom
feat/ssh-commit-signing
Jun 15, 2026
Merged

hiddeco merged 14 commits into
mainfrom
feat/ssh-commit-signing

Conversation

@hiddeco

@hiddeco hiddeco commented May 29, 2026

Copy link
Copy Markdown
Member

@matheuscscp matheuscscp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM, a few important nits in the API package

Comment thread api/v1/git.go Resolved
Comment thread api/v1beta2/git.go Resolved
@hiddeco
hiddeco force-pushed the feat/ssh-commit-signing branch 4 times, most recently from bbca0c7 to 479b089 Compare June 15, 2026 06:38
hiddeco added 13 commits June 15, 2026 08:40
Add an optional Type field (gpg|ssh) to SigningKey on api/v1. When
empty the controller defaults to 'gpg' so existing manifests behave
identically; selecting 'ssh' reads an SSH private key from the
'identity' Secret data key, matching the convention used by
GitRepository SSH transport auth.

Fix a pre-existing inconsistency on SecretRef: it was tagged
+required but used json:",omitempty", which let a zero-value omit a
CRD-required field on marshal. Drop the omitempty.

Signed-off-by: Hidde Beydals <[email protected]>
Mechanical regeneration after adding the Type field on SigningKey.

Only the CRD changed: deepcopy is unaffected (SigningKeyType is a
string alias that copies by value) and go.mod/go.sum were already
tidy. The api-docs target (`make api-docs`) was already broken on
main before this branch due to the pre-existing ObservedPolicies
map type alias tripping gen-crd-api-reference-docs; it is not a
regression of this task.

Signed-off-by: Hidde Beydals <[email protected]>
Generates ed25519 and ECDSA P-256 SSH keypairs at test time, returning
PEM-encoded private keys plus the matching gossh.PublicKey for verify
assertions. Mirrors the existing GetSigningKeyPair helpers so the new
SSH-signing tests do not require checked-in key files.

Signed-off-by: Hidde Beydals <[email protected]>
ioutil has been deprecated since Go 1.16. Switch the in-tree directory
copy helper to os.ReadDir, which returns fs.DirEntry; the symlink check
moves from Mode() to Type() (DirEntry exposes only the file-type bits).

Signed-off-by: Hidde Beydals <[email protected]>
The function always populates 'password'; drop the misleading 'when provided' qualifier.

Signed-off-by: Hidde Beydals <[email protected]>
Extracts the signing-key constants and the type-detection pure
function into a dedicated file inside internal/source.
detectSigningType validates that the referenced Secret contains the
key expected for the declared signing-key type (gpg -> git.asc,
ssh -> identity), with table-tested coverage of every case.

The resolveSigner dispatcher is also added with placeholder leaf
loaders; Tasks 6 and 7 fill them in.

Signed-off-by: Hidde Beydals <[email protected]>
Lifts the existing OpenPGP load path from getSigningEntity in git.go
into a dedicated leaf function in signing.go. Preserves the existing
error strings verbatim so downstream log-scraping is unaffected.
Returns a signature.Signer via the new signature.NewOpenPGPSigner
constructor instead of the typed *openpgp.Entity.

Signed-off-by: Hidde Beydals <[email protected]>
Reads the SSH private key from the 'identity' field and the optional
passphrase from 'password' (matching the Flux SSH-auth Secret format),
then delegates to signature.NewSSHSigner from pkg/git. Surfaces the
encrypted-without-passphrase case with a message symmetric to the GPG
path so operators have one common runbook entry.

Signed-off-by: Hidde Beydals <[email protected]>
Exercises the full path through resolveSigner against a fake client:
the no-SigningKey case, missing-Secret case, GPG and SSH happy paths,
and both type-mismatch error cases.

Signed-off-by: Hidde Beydals <[email protected]>
GetSigningKeyPair previously serialized the OpenPGP private key before
encrypting it, so the returned bytes were always unencrypted regardless
of the passphrase argument. Encrypt first, then serialize via
SerializePrivateWithoutSigning (the variant that does not re-sign
identities, which would fail on an encrypted key).

The bug went unnoticed because the prior Test_getSigningEntity test
only exercised the encrypted-with-passphrase path, which silently
succeeded as if unencrypted. The new Test_loadGPGSigner subtests added
in the SSH-signing migration exercise both branches and require the
serialized bytes to actually be encrypted.

Signed-off-by: Hidde Beydals <[email protected]>
Bumps fluxcd/pkg/git to the minor that exposes the generic
signature.Signer interface and the NewOpenPGPSigner / NewSSHSigner
constructors. The controller now resolves a Signer via the new
resolveSigner dispatcher in signing.go, removes the old typed
*openpgp.Entity path from git.go, and appends WithSigner conditionally
at the commit call site to avoid the typed-nil interface hazard the
new generic field introduces.

Retires the legacy Test_getSigningEntity unit test; its coverage moved
to signing_test.go alongside the new dispatcher leaves. Updates the
SourceManager.CommitAndPush fixture to set the explicit GPG type now
that detectSigningType requires it (the apiserver default does not
apply to in-memory test objects).

Signed-off-by: Hidde Beydals <[email protected]>
Adds two end-to-end tests that drive the reconciler to push a commit
signed via the new SSH signing path: one for ed25519, one for
ecdsa-sha2-nistp256. Each test in-process-verifies the produced gpgsig
header through sshsig.Verify against the matching public key, so the
namespace ("git"), hash, and armor format are all checked.

The ECDSA case exercises the non-ed25519 SSH path explicitly so the
algorithm allowlist is not silently ed25519-only.

Signed-off-by: Hidde Beydals <[email protected]>
Adds an SSH-signing subsection alongside the existing OpenPGP content
on the v1 spec page. Covers the type discriminator, the SSH-auth
Secret reuse pattern (same-namespace only), the supported algorithm
list, and the OpenSSH-format constraint with a pointer at ssh-keygen
-p for key conversion.

Signed-off-by: Hidde Beydals <[email protected]>
@hiddeco
hiddeco force-pushed the feat/ssh-commit-signing branch from 479b089 to ec7b052 Compare June 15, 2026 06:40

@matheuscscp matheuscscp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀

Comment thread internal/testutil/util.go Resolved
Comment thread internal/source/signing.go Resolved
Adds a third reconciler test that drives a commit signed by a
passphrase-protected ed25519 SSH key, alongside the existing
unencrypted ed25519 and ecdsa-sha2-nistp256 cases. The test
verifies the produced signature in-process via sshsig.Verify so
the decrypt path through signature.NewSSHSigner is actually
covered, and is not just constructed.

The test also picks up the GetSSHSigningKeySecret helper in
internal/testutil, which until now had no callers — so the
passphrase plus identity layout it returns is exercised by the
suite rather than left as dead code.

Teardown is registered through t.Cleanup rather than defer so
that a teardown failure does not short-circuit other cleanups
and is still attributed to this test.

Signed-off-by: Hidde Beydals <[email protected]>
@stefanprodan stefanprodan added the enhancement New feature or request label Jun 15, 2026
@stefanprodan stefanprodan added area/security Security related issues and pull requests area/git Git related issues and pull requests labels Jun 15, 2026

@stefanprodan stefanprodan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks @hiddeco 🥇

@hiddeco
hiddeco merged commit 4ff1e0b into main Jun 15, 2026
8 checks passed
@hiddeco
hiddeco deleted the feat/ssh-commit-signing branch June 15, 2026 14:43
hiddeco added a commit to fluxcd/flux2 that referenced this pull request Jun 18, 2026
Closes a pre-existing gap where the ImageUpdateAutomation SigningKey
field was reachable only by hand-editing the rendered YAML. The two
new flags --signing-key-secret and --signing-key-type populate the
spec.git.commit.signingKey block directly.

When --signing-key-secret is set without --signing-key-type, the run
function leaves spec.git.commit.signingKey.type empty so the
controller's documented default ('gpg' when type is unset[1]) applies
server-side rather than baking the choice into the rendered YAML.
Validation rejects --signing-key-type without --signing-key-secret
and rejects values outside {gpg, ssh}, using the typed
SigningKeyType constants exported from the image-automation-
controller API so the validator and populator share a single source
of truth.

[1]: fluxcd/image-automation-controller#1035

Signed-off-by: Hidde Beydals <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/git Git related issues and pull requests area/security Security related issues and pull requests enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants