fix: extend offline secret caching to token-based auth and fix connection check#272
fix: extend offline secret caching to token-based auth and fix connection check#272devin-ai-integration[bot] wants to merge 2 commits into
Conversation
…tion check - ValidateInfisicalAPIConnection now checks HTTP status code, not just transport errors. A 503 from a load balancer/proxy no longer counts as 'connected', allowing the cache fallback to trigger correctly. - Added secret backup/restore for service token and Universal Auth (machine identity) code paths. Previously, offline caching only worked for infisical-login user auth. Now all auth methods write secrets to the encrypted backup on success and fall back to cached secrets on fetch failure. Co-Authored-By: jake <jake@infisical.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
| Filename | Overview |
|---|---|
| packages/util/common.go | Extends ValidateInfisicalAPIConnection to check for 2xx HTTP status codes rather than just transport-level success; body is now properly closed with defer. |
| packages/util/secrets.go | Adds offline caching (write-on-success / read-on-failure) to the token-based auth branch; caching is silently skipped for service tokens unless --projectId is passed, and when --projectId IS passed, auth errors from CallGetServiceTokenDetailsV2 are wrapped with %v (not %w), breaking the errors.As 4xx-detection that is supposed to prevent stale cache from masking revoked-token errors. |
Reviews (2): Last reviewed commit: "fix: close resp.Body in connection check..." | Re-trigger Greptile
…to non-auth errors Address review feedback: - Close resp.Body in ValidateInfisicalAPIConnection to prevent connection leak - Only fall back to cached secrets on connection/server errors (5xx), not on client errors (4xx) like 401/403 which indicate auth issues (revoked token, permission denied). This prevents silently serving stale secrets when the real problem is invalid credentials. Co-Authored-By: jake <jake@infisical.com>
|
Addressed all Greptile feedback in d7e51f3:
|
|
@greptile re-review please |
| shouldFallback := true | ||
| var apiErr *api.APIError | ||
| if errors.As(errorToReturn, &apiErr) && apiErr.StatusCode >= 400 && apiErr.StatusCode < 500 { | ||
| shouldFallback = false | ||
| } |
There was a problem hiding this comment.
4xx masking still active for service tokens when
--projectId is passed
GetPlainTextSecretsViaServiceToken wraps any error from CallGetServiceTokenDetailsV2 with fmt.Errorf("unable to get service token details. [err=%v]", err) — using %v, not %w. That strips the error chain, so errors.As(errorToReturn, &apiErr) always returns false for this error path, leaving shouldFallback = true. If a user runs infisical run --projectId=<id> --token=<service-token> ... and the token is revoked (the API returns 401), the 4xx guard is bypassed and the fallback silently serves stale cached secrets instead of surfacing the auth error. The fix that was applied to the Universal Auth path (which directly propagates the typed *api.APIError) does not cover service tokens due to this wrapping.
Description 📣
Fixes two issues that prevent the CLI's offline secret caching from working in common scenarios:
1.
ValidateInfisicalAPIConnection()only checked for transport errors, not HTTP status codesWhen a load balancer or reverse proxy returns HTTP 503 (server down), the Go
http.Getsucceeds without error — so the CLI treated the server as "connected" and never fell back to cached secrets. Now checks for a 2xx response:2. Token-based auth (service tokens, Universal Auth / machine identity) had zero offline caching
GetAllEnvironmentVariables()only wrote/read secret backups in theinfisical loginuser-auth code path. Theelsebranch forInfisicalTokenandUniversalAuthAccessTokenhad no caching logic at all — any fetch failure returned an error immediately with no fallback.Added the same backup write-on-success / read-on-failure pattern to the token-based auth branch, using the existing
WriteBackupSecrets/ReadBackupSecrets/GetBackupEncryptionKeyhelpers. The fallback only triggers on connection errors or server errors (5xx) — client errors like 401/403 (revoked token, permission denied) propagate correctly so users see actionable auth failure messages.Type ✨
Tests 🛠️
Scenario to verify:
infisical run --env=prod --path=/test --projectId=<id> -- env— secrets should be fetched and injected as before~/.infisical/secrets-backup/contains an encrypted backup fileLink to Devin session: https://app.devin.ai/sessions/2dfab5dd573b484f84cce72d5f38c757