← Back to Pull requests

feat: retry 403 on GET for eventually consistent reads #112

Closed
unknown closed this pull request without merging on Sep 08, 2026 15:59 UTC
View on GitHub
unknown unknown commented on Sep 03, 2026 21:58 UTC
Author

Pull Request

Description

Reads are eventually consistent: an object that was just written may not yet be visible to an immediately following GET, and the API reports that as 403 rather than 404. Previously these surfaced as generic errors and failed an apply intermittently, with nothing the operator did to cause them.

DoRequest now retries GET on the secrets path when it fails with 403, backing off exponentially up to a 500ms cap:

| attempt | nominal sleep | probe at |
|---|---|---|
| 1 | — | 0ms |
| 2 | 25ms | 25ms |
| 3 | 50ms | 75ms |
| 4 | 100ms | 175ms |
| 5 | 200ms | 375ms |
| 6 | 400ms | 775ms |
| 7 | 500ms | 1275ms |
| 8 | 500ms | 1775ms |

The schedule is front-loaded because reads normally converge quickly, so nearly every stale read clears on the second or third attempt. The long tail exists only to absorb rare outliers and costs nothing in the common case. The bounds were chosen from internal service latency measurements.

Capping a single sleep at 500ms rather than letting it double to 800ms lets the same budget fit one more probe, so the schedule both reaches further (1775ms, up from 1575ms) and leaves a smaller worst-case gap between probes (500ms, down from 800ms), for one extra request.

Every duration above is nominal. Each sleep is then scaled by a random factor within 25% of it, because Terraform applies resources in parallel and without jitter every worker that hits a stale read would retry in lockstep. Jitter is proportional rather than full — a uniform value in 0, d) would halve the expected schedule and with it the tail coverage the budget was sized for. The budget is accounted against the nominal schedule, so the attempt count is fixed at 8 and only the timing varies; a single sleep can reach 625ms and the total 2218ms.

Scope and deliberate omissions

  • GET on the secrets path only. Writes do not observe eventually consistent reads, and other surfaces of the API do not share the behaviour — a 403 from the auth service behind BuildAuthPath fails on the first attempt. The one external non-GET caller of DoRequest (a PATCH) is unaffected.

  • 403 only, checked directly rather than via IsPermissionError, which also matches 401. An invalid token will not become valid, so retrying it would only add latency — expired tokens still fail on the first attempt.

  • A 403 that outlives the budget is returned unchanged. It is deliberately not mapped to not-found: that would conflate a genuine authorization failure with a deleted object, and could empty state on a permission regression.

  • *Cancelling the context returns the last APIError rather than ctx.Err(), so callers keep the typed error they classify failures on.
  • Retries rebuild the request per attempt instead of replaying one, which is free for GET and keeps body rewinding correct if this is later extended to writes.

    StaleReadInitialBackoff and StaleReadMaxElapsed are exposed on Client so the budget is tunable without touching the loop; a zero backoff disables retrying entirely.

    Reviewer notes

  • Request volume. 8 attempts against Terraform's default parallelism of 10 means a broadly-misconfigured token generates ~80 GETs where it previously generated 10. If the API rate-limits, that is the realistic failure mode of this change rather than the added latency.

  • Known unhandled. An object deleted outside Terraform also returns 403, and the not-found helpers (resource_common.go, workload_identity_helpers.go) remain 404-only, so drift detection still cannot self-heal from out-of-band deletion. Pre-existing and unchanged here.

  • Follow-up.* Retrying 429/502/503/504 and network-level errors is a separate change; go-retryablehttp is already an indirect dependency. Note 504 on POST must not be retried, since a duplicate create is not recoverable.
  • Testing

    7 new tests in internal/client/client_test.go, following the existing httptest + table-driven style with atomic attempt counters:

  • 403 → 200 succeeds on retry (2 requests)

  • terminal 403 returns typed APIError after exactly 7 attempts

  • POST/PUT/PATCH/DELETE never retry (1 request each)

  • 400/401/404/409/500 never retry (1 request each)

  • context cancellation stops the loop early, still yields *APIError

  • zero backoff disables retrying

  • NewClient wires the budget; schedule fits inside it
  • go build ./..., go vet, gofmt, and the full go test ./... all pass. No existing test required changes.

    One test sleeps through the real schedule (~1.6s), taking the package from 0.5s to ~1.9s. That is deliberate — it is the only end-to-end check of the production budget.

    Jira tickets and links

  • Jira: [SMOP-1690
  • Checklist


  • Jira: If this PR has a change associated with a ticket, include it in the section above

  • RFC: If this change is related to an RFC, include it as a link above
  • 🤖 Generated with Claude Code

    [SMOP-1690]: https://beyondtrust.atlassian.net/browse/SMOP-1690?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: sdahal-bt Review duration: 6840m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #112"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%