--- title: "ADR 0022: GitLab token rotation verifies the Secret write first and caches introspection" --- * Status: accepted * Code: [`pkg/provider/gitlab/token_rotation.go`](https://github.com/tektoncd/pipelines-as-code/blob/main/pkg/provider/gitlab/token_rotation.go) ## Context Rotating a GitLab token revokes the old one and returns the new one exactly once. If storing it in the Kubernetes Secret then fails, the new token is lost and the repository loses access. Introspecting the token on every webhook or watcher reconcile would also hit the GitLab API each time. ## Decision * `verifySecretWriteAccess` does a server-side dry-run update of the git provider Secret with unchanged data before rotating. A permission failure aborts the rotation while the old token is still valid. * Rotation tries personal access token self-rotate first. A `403` maps to `errMissingSelfRotateScope`; any other 4xx falls back to project access token self-rotate when a target project ID is known. * Introspection results are cached per `namespace/name` of the Repository. An entry is reused only if it is younger than `introspectionCacheTTL`, was recorded for the same token (a manually updated secret invalidates it) and its expiry is not within the rotation threshold. A token without an expiry counts as not needing rotation. ## Consequences * The dry-run proves RBAC only; a real update can still fail afterwards and then surfaces as `errTokenRotatedSecretUpdateFailed`. * A token expiring inside the threshold is re-introspected on every call until it is rotated.