Skip to content

fix: retry transient Cloud KMS failures with backoff - #12

Merged
deansheather merged 2 commits into
mainfrom
deansheather/ple-146-retry-kms
Nov 25, 2026
Merged

deansheather merged 2 commits into
mainfrom
deansheather/ple-146-retry-kms

Conversation

@deansheather

Copy link
Copy Markdown
Contributor

Fixes PLE-146.

What and why

A coder/coder build failed because every gpgkms call hit a transient 503 while fetching workload identity credentials:

gpgkms: get public key for "***": Get "https://cloudkms.googleapis.com/v1/***/publicKey?alt=json&prettyPrint=false": credentials: status code 503: upstream connect error or disconnect/reset before headers. retried and the latest reset reason: connection timeout

The generated cloudkms/v1 REST client does not retry, so one blip failed the whole run.

GetPublicKey and AsymmetricSign now go through retryKMS:

  • up to 5 attempts, waiting 1s, 2s, 4s and 8s between them (about 15s of waiting in total); each attempt keeps its own 1-minute timeout;
  • retried: KMS 5xx/408/429, and any error that is not a *googleapi.Error. That covers network errors, per-attempt timeouts and credential fetch failures, which the auth library returns as plain fmt.Errorf strings with no type to match on;
  • not retried: other KMS 4xx errors (permission denied, not found, bad request), a cancelled parent context, and everything after the RPC (algorithm/HSM checks, PEM parsing, the local signature check). A corrupt signature still fails closed.
  • after more than one attempt, the error says failed N attempts: ….

Retrying AsymmetricSign is safe: signing the same digest has no side effects, and every returned signature is still checked locally.

The darwin failure in the same run came from kmsp11 (rcodesign), not gpgkms, so this PR does not fix it.

Can the exported public key or fingerprint change?

No. Key derivation and signature construction are unchanged; the golden and compat tests pass unchanged.

Validation

  • gofmt -l . (clean), go vet ./...
  • go test -race -shuffle=on -count=1 ./.... New fake-KMS tests cover recovery from transient errors, giving up after 5 attempts, no retries on 400/403/404, and stopping when the context is cancelled. A table test covers retryable.
  • go mod tidy -diff (root and tools/), go mod verify
  • golangci-lint: 0 issues
  • govulncheck: exit 0 (it reports an existing, uncalled x/crypto advisory, GO-2026-5932, that this PR does not touch)

@deansheather
deansheather requested a review from a team as a code owner November 25, 2026 09:32
@linear-code

linear-code Bot commented Nov 25, 2026

Copy link
Copy Markdown

PLE-146

@deansheather

Copy link
Copy Markdown
Contributor Author

@codex review

@deansheather

Copy link
Copy Markdown
Contributor Author

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T09:40:12.534859Z 442ebb1 Manual request
🔒 Security Review ✅ Completed 2026-11-25T09:36:23.352649Z 2153108 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2153108604

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kms.go Outdated
Comment on lines +158 to +159
case <-ctx.Done():
return v, attemptsError(attempt, err)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Return the context error when backoff is canceled

When the parent context is canceled or reaches its deadline while waiting between retries, this branch returns the previous transient KMS error instead of ctx.Err(). Consequently, callers cannot detect cancellation with errors.Is(err, context.Canceled) or errors.Is(err, context.DeadlineExceeded); return or wrap the context error here while retaining any useful attempt context.

AGENTS.md reference: AGENTS.md:L118-L119

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 442ebb1. When the context ends, either right after a failed attempt or while waiting before a retry, retryKMS now wraps both ctx.Err() and the last KMS error, so errors.Is works for context.Canceled/DeadlineExceeded and the logs still show the cause. Added TestRetryKMSReturnsContextErrorDuringBackoff, and the existing cancellation test now checks errors.Is(err, context.Canceled).

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 2153108604

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@deansheather

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 442ebb1198

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@deansheather
deansheather merged commit c4dfbf6 into main Nov 25, 2026
8 checks passed
deansheather added a commit to coder/coder that referenced this pull request Nov 25, 2026
Updates the gpgkms tool dependency from v0.2.0 to v0.2.1 (`c4dfbf6`,
coder/gpgkms#12).

## What changes in v0.2.1
- Cloud KMS calls (`GetPublicKey` and `AsymmetricSign`) are now tried up
to 5 times, waiting 1s, 2s, 4s and 8s in between, when they fail with a
transient error. That covers KMS 5xx/408/429, network errors,
per-request timeouts, and failures to fetch workload identity
credentials.
- Other KMS 4xx errors (permission denied, not found) still fail
immediately. If the job is cancelled, gpgkms stops retrying and returns
the context error.
- Every KMS signature is still checked locally before it is used.

This fixes [PLE-146](https://linear.app/codercom/issue/PLE-146). In that
run, every gpgkms call failed with `credentials: status code 503:
upstream connect error …` and the build failed.

## Does the signing key change?
No. Key derivation is unchanged: gpgkms's golden and compat tests pass
unchanged at v0.2.1.

## Not covered
In the same run, darwin_amd64 failed in `kmsp11` (rcodesign's PKCS#11
library, `Error occurred when fetching oauth2 token`), not in gpgkms.
This PR does not add retries there.

## Validation
- `go get -tool github.com/coder/gpgkms/cmd/gpgkms@v0.2.1`, then `go mod
tidy` (only gpgkms lines change), `go mod tidy -diff` and `go mod
verify`.
- `go tool github.com/coder/gpgkms/cmd/gpgkms` builds and runs.
- The real KMS signing path runs in the next `build` workflow.

---
_Opened by Xum on behalf of @deansheather._
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.

2 participants