Skip to content

fix(certs): renew leaf certificates that no longer verify against the current CA - #11614

Open
zoepfchen wants to merge 1 commit into
cloudnative-pg:mainfrom
zoepfchen:fix/renew-leaf-on-ca-change
Open

zoepfchen wants to merge 1 commit into
cloudnative-pg:mainfrom
zoepfchen:fix/renew-leaf-on-ca-change

Conversation

@zoepfchen

Copy link
Copy Markdown

Closes #10433

What

RenewLeafCertificate() decided whether to regenerate a leaf certificate (<cluster>-server, <cluster>-replication, webhook certificate) only by two criteria: the leaf is expiring, or its DNS names changed. It never checked whether the leaf still verifies against the current CA.

This PR adds that third criterion: if the leaf cannot be verified against the CA passed in (x509.Verify with ExtKeyUsageAny, the same verification the operator applies when it talks to the instances), the leaf is regenerated even though it is not expiring.

Why

Whenever the CA changes while the leaf is still valid, the operator trusts only the new CA but the instances keep serving the old leaf. The operator then fails every status call with x509: certificate signed by unknown authority and the cluster is stuck (Instance Status Extraction Error / Ready=False), as described in #10433 in several variants (operator-driven CA rotation when leaf and CA lifetimes diverge, stale -replication secret breaking joins, and the latent "running pods fine, next restart = outage" state).

We hit the same thing today on CNPG 1.30.0 through a different entry point: switching an existing cluster to a user-provided spec.certificates.serverCASecret. The leaf was 8 days old and nowhere near expiry, so it was never regenerated; the operator immediately lost the cluster. Deleting <cluster>-server made the operator recreate it from the new CA and everything recovered within seconds — which is exactly what the operator can now do by itself.

How

  • pkg/certs/k8s.go: parse the CA before the early return and require pair.IsValid(caPair, ExtKeyUsageAny) == nil in addition to "not expiring" and "DNS names match".
  • pkg/certs/leaf_renewal_test.go: unit tests — no renewal for server and client leaves signed by the current CA; renewal for both leaf types when the CA differs, with the renewed leaf verifying against the new CA and no longer against the old one.

go test ./pkg/certs/... ./internal/controller/..., gofmt and go vet pass.

Notes

  • The check uses the existing KeyPair.IsValid, so it applies to user-provided CAs as well as operator-managed ones. With a user-provided serverCASecret that has no ca.key, RenewLeafCertificate is not reached (the operator only validates), so behaviour there is unchanged.
  • IsValid runs x509.Verify, which also rejects a leaf whose validity window does not contain "now"; that case already triggers a renewal through IsExpiring, so it does not change behaviour.

… current CA

RenewLeafCertificate only regenerated a leaf certificate when it was
expiring or its DNS names changed. When the CA changes while the leaf is
still valid (operator-driven CA rotation with diverging lifetimes, or a
switch to a different serverCASecret), the operator trusts only the new
CA while the instances keep serving the old leaf, and every status call
fails with "x509: certificate signed by unknown authority" until someone
deletes the leaf secret by hand.

Also require that the leaf verifies against the current CA, using the
same verification the operator applies when talking to the instances.

Closes cloudnative-pg#10433

Signed-off-by: Christian Zöpfchen <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
@zoepfchen
zoepfchen requested a review from a team as a code owner October 6, 2026 07:28
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.30 labels Oct 6, 2026
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

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

Labels

backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.30

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: CA certificate rotation creates deadlock — operator cannot restart pods due to x509 verification failure

2 participants