Skip to content

fix: reconcile webhook token rotation for group and project Hook - #375

Merged
markussiebert merged 2 commits into
masterfrom
feat/hook-token-rotation
Jul 3, 2026
Merged

fix: reconcile webhook token rotation for group and project Hook#375
markussiebert merged 2 commits into
masterfrom
feat/hook-token-rotation

Conversation

@markussiebert

@markussiebert markussiebert commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

Description of your changes

Fixes #374. Reconciles webhook token rotation for both the group Hook and the project Hook, plus two related project-Hook consistency fixes.

Token rotation (group + project)

GitLab never returns the webhook secret, so IsHookUpToDate can't compare it and Update only ran when some other field drifted. Rotating only the referenced secret was therefore never propagated.

The change detects rotation on the desired side:

  • New status.atProvider.tokenHash holds a SHA-256 digest of the token last pushed (never the raw token), via a shared clients.TokenHash helper.
  • Observe resolves the secret — only when every other field already matches — and compares its hash to the stored one; a mismatch drives Update.
  • Update re-pushes the token and rewrites the digest.
  • The hash is written in Update (crossplane-runtime reverts status changes made during Create), so a freshly created hook with a token self-corrects via a single Update.

Project Hook consistency (mirroring the group Hook)

  • Enforce projectId immutability with a CEL XValidation rule.
  • Remove the dead errHookNotFound / IsErrorHookNotFound matcher (GitLab returns "404 Not Found"; not-found is already handled by clients.IsResponseNotFound on the response status).

How has this code been tested

go build ./..., go vet ./..., go test ./... all pass; new unit tests cover the token-rotation path (TokenRotated) and hash persistence for both hooks (namespaced + generated cluster variants).

Live e2e on a self-managed GitLab 18.11.5-ee group (provider out-of-cluster against a kwok control plane): created a group hook with a token, then rotated only the referenced secret. The reconciler detected the change and issued an Updatestatus.atProvider.tokenHash moved from sha256(v1) to sha256(v2) — confirming the previously-broken path now works. All sandbox resources were cleaned up afterward.

Delete by external-name (group + project)

Delete used status.atProvider.ID for the hook id while Observe/Update derive it from the external-name. If the resource was deleted before a successful Observe populated atProvider.ID, that id is 0 and Delete would call DeleteHook(..., 0), orphaning the real webhook. Delete now parses the hook id from the external-name, matching Observe/Update.

GitLab never returns the webhook secret, so IsHookUpToDate cannot compare
it and Update only ran when some other field drifted. Rotating only the
referenced secret was therefore never propagated to GitLab.

Detect rotation on the desired side: store a SHA-256 digest of the token
last pushed in status.atProvider.tokenHash (never the raw token). Observe
resolves the secret (only when every other field already matches) and
compares its hash to the stored one; a mismatch drives Update, which
re-pushes the token and rewrites the digest. The hash is written in
Update (crossplane-runtime reverts status changes made during Create), so
a freshly created hook with a token self-corrects via a single Update.

Also for the project Hook:
- enforce projectId immutability with a CEL XValidation rule, matching the
  group Hook.
- remove the dead errHookNotFound/IsErrorHookNotFound string matcher
  (GitLab returns "404 Not Found"; not-found is handled by
  clients.IsResponseNotFound on the response status).

Fixes #374

Signed-off-by: Markus Siebert <markus.siebert@deutschebahn.com>
Delete used cr.Status.AtProvider.ID for the hook id while Observe and
Update derive it from the external-name. If the resource was deleted
before a successful Observe ever populated atProvider.ID (created then
deleted quickly, or status lost/restored while the external-name
annotation survives), atProvider.ID is 0 and Delete would call
DeleteHook(..., 0), failing to remove the real webhook and orphaning it.

Parse the hook id from the external-name in Delete, matching
Observe/Update. Applies to both the group and project Hook.

Addresses review feedback on #372.

Signed-off-by: Markus Siebert <markus.siebert@deutschebahn.com>
@markussiebert
markussiebert requested a review from dariozachow July 3, 2026 08:51
@markussiebert markussiebert self-assigned this Jul 3, 2026
@markussiebert
markussiebert merged commit 6afc5df into master Jul 3, 2026
8 checks passed
@markussiebert
markussiebert deleted the feat/hook-token-rotation branch July 3, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Webhook token rotation is not reconciled (group & project Hook)

2 participants