Skip to content

Trial: hegel-go property-based tests - #279

Draft
wallrj wants to merge 4 commits into
cert-manager:mainfrom
wallrj:hegel-go-trial
Draft

wallrj wants to merge 4 commits into
cert-manager:mainfrom
wallrj:hegel-go-trial

Conversation

@wallrj

@wallrj wallrj commented Aug 15, 2026

Copy link
Copy Markdown
Member

Not for merge as-is — this is the csi-lib instalment of the property-based-testing trial previously run on cert-manager (cert-manager/cert-manager#9149, which describes how candidates are chosen), approver-policy (cert-manager/approver-policy#989), trust-manager (cert-manager/trust-manager#1064), istio-csr (cert-manager/istio-csr#891) and csi-driver-spiffe (cert-manager/csi-driver-spiffe#586). If maintainers want any of it, it should be split per package, and the new test-only dependency (hegel-go, a Hypothesis-style property-testing library with a native shrinking engine, pulled in via hegel.dev/go/hegel + purego) needs discussing first.

What's here

Four commits, each putting properties in separate *_hegel_test.go files so the diff reads as deleted tables and new properties. Tables are replaced only where their rows merely instantiate a recomputable rule; tests that pin end-to-end flows, external bytes or exact output are kept.

  1. manager calculateNextIssuanceTime — the single-example Test_calculateNextIssuanceTime becomes a property: for any leaf certificate (validity drawn 1970–2100, lifetimes up to a century) the next issuance time is exactly 2/3 through the certificate's lifetime, recomputed independently of the PEM/X.509 pipeline, and only the first PEM block governs.
  2. storage fsGroupForMetadata — the nine-row table becomes a property recomputing the precedence and range rule: the FSGroupVolumeAttributeKey volume attribute overrides VolumeMountGroup; empty means no ownership change; anything else must parse as a decimal integer in [1, 4294967295]. Group strings are drawn as wide-ranging integers (crossing both boundaries and int64 overflow) or arbitrary Unicode text.
  3. manager/util token request — the five-row Test_restConfigForMetadataTokenRequestEmptyAud table becomes a property over kubelet-style audience→token maps: the getter succeeds iff a non-empty empty-audience ("") token is present, and on success the rest config is exactly the seed's Host/TLS/UserAgent/Timeout plus that token — pinning that the seed's own BearerToken never leaks into the per-volume config. A second property feeds arbitrary text as the volume-context value: error, never panic.
  4. New properties for untested/security-sensitive helpers — internal/api/util.HashIdentifier (no tests at all; for any input the output must be a valid Kubernetes label value, checked with the apimachinery validator, and deterministic) and metadata.Metadata.MarshalLog (over arbitrary volume contexts: the SA token entry becomes exactly [REDACTED], every other entry is preserved, the original is never mutated — alongside the example-based pins from the token-redaction fix, which are kept).

Kept as-is: all the manager issuance/backoff/cleanup tests, the filesystem read/list tests, the metrics exact-output tests, the driver redaction pins and the integration tests.

What it found

No production behaviour bugs, but two long-standing flaws worth one-line follow-ups:

  • The latent calculateNextIssuanceTime panic found in csi-driver-spiffe originates here. manager.go line 983 at b7fffde ignores pem.Decode's nil return: a certificate chain with no PEM block dereferences a nil pointer instead of returning an error. The chain comes from the issued CertificateRequest's status, so it takes a misbehaving issuer or someone with status-write access to trigger. csi-driver-spiffe copied this function and inherited the flaw (written up in Trial hegel-go property-based tests on the SPIFFE approver and driver csi-driver-spiffe#586); fixing it here and there are both one-line nil checks. Documented in the property's doc comment; not fixed here since the trial is test-only.
  • fsGroupForMetadata's range error message is off by one. filesystem.go line 352 at b7fffde says the gid "must be greater than 0 and less than 4294967295", but the check on line 351 (and the comment above it) accepts exactly 4294967295. The property pins the code's actual behaviour (inclusive upper bound).

Coverage (main at b7fffde → branch, go test -coverprofile, unit packages)

package main branch
internal/api/util no tests 100%
manager 74.2% 74.0%¹
manager/util 77.8% 77.8%
metadata 88.9% 88.9%
storage 24.6% 24.6%

¹ Block-level diff shows the only "lost" block (issue()'s ReadMetadata error branch) flickers run-to-run on identical code — it is covered nondeterministically by the concurrent manager tests, not by anything this branch removed. No other blocks lost; the flat percentages reflect that the replaced tables already touched every line — the properties add adversarial input coverage, not line coverage.

with claude fable-5

wallrj and others added 4 commits August 15, 2026 13:15
Test_calculateNextIssuanceTime checked a single hand-computed example of
the 2/3-lifetime rule. The property in manager_hegel_test.go draws
validity periods from 1970 to 2100 with lifetimes up to a century and
recomputes the expected next issuance time independently of the PEM /
X.509 parsing pipeline. It also checks that only the first PEM block
governs the result.

This adds the hegel.dev/go/hegel test dependency and gitignores its
.hegel example database.

The property's doc comment records a latent panic found while writing
it: calculateNextIssuanceTime dereferences the nil block returned by
pem.Decode when the chain contains no PEM block. csi-driver-spiffe
copied this code and inherited the same flaw.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
The nine rows of Test_fsGroupForMetadata were hand-picked instances of
one rule: the volume attribute named by FSGroupVolumeAttributeKey takes
precedence over VolumeMountGroup, an empty effective value means no
ownership change, and anything else must parse as a decimal integer in
[1, 4294967295]. The property recomputes that rule from drawn parts,
with group strings drawn as wide-ranging integers (crossing both range
boundaries and int64 overflow) or arbitrary Unicode text.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
Test_restConfigForMetadataTokenRequestEmptyAud's five rows were
instances of one rule: the getter succeeds iff the kubelet token-request
volume context key carries a non-empty token for the empty audience, and
on success the rest config is exactly the seed's Host, TLSClientConfig,
UserAgent and Timeout plus that token as the bearer token. The property
draws kubelet-style audience/token maps (empty audience present or not,
empty or non-empty tokens, extra audiences) and recomputes the rule. It
also pins that the seed config's own BearerToken never leaks into the
per-volume config.

A second property feeds arbitrary Unicode text as the volume context
value: EmptyAudienceTokenFromMetadata must error, never panic and never
return an empty token without an error.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
HashIdentifier turns arbitrary node and volume IDs into
CertificateRequest label values but had no tests. The property checks
that for any input — including empty strings and arbitrary Unicode —
the output is a valid Kubernetes label value (using the apimachinery
validator the API server itself uses) and is deterministic.

Metadata.MarshalLog redacts the kubelet-injected ServiceAccount token
from log output. The example-based tests pin the formatted-output
security property; the new property checks the redaction rule over
arbitrary volume contexts: the token entry (if present) becomes exactly
"[REDACTED]", every other entry is preserved unchanged, and the
original Metadata is never mutated.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
@cert-manager-prow cert-manager-prow Bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 15, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@cert-manager-prow cert-manager-prow Bot added the dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. label Aug 15, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign jakexks for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cert-manager-prow cert-manager-prow Bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Aug 15, 2026
@cert-manager-prow cert-manager-prow Bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 29, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

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

Labels

dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant