Skip to content

Pr2 credential chain - #464

Draft
patelspratik wants to merge 11 commits into
mainfrom
pr2-credential-chain
Draft

Pr2 credential chain#464
patelspratik wants to merge 11 commits into
mainfrom
pr2-credential-chain

Conversation

@patelspratik

@patelspratik patelspratik commented Aug 27, 2026

Copy link
Copy Markdown
Contributor
  • add global flag for --api-key (still hidden)
  • add BREV_API_KEY env var support

@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 59.61538% with 63 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (pr1-register-resiliency@9ebe927). Learn more about missing BASE report.

Files with missing lines Patch % Lines
pkg/store/organization.go 33.33% 23 Missing and 5 partials ⚠️
pkg/cmd/cmd.go 35.71% 26 Missing and 1 partial ⚠️
pkg/cmd/register/register.go 83.87% 3 Missing and 2 partials ⚠️
pkg/cmd/login/login.go 87.50% 0 Missing and 2 partials ⚠️
pkg/cmd/ls/ls.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@                    Coverage Diff                     @@
##             pr1-register-resiliency     #464   +/-   ##
==========================================================
  Coverage                           ?   29.00%           
==========================================================
  Files                              ?      140           
  Lines                              ?    19795           
  Branches                           ?        0           
==========================================================
  Hits                               ?     5741           
  Misses                             ?    13464           
  Partials                           ?      590           
Flag Coverage Δ
Linux 29.00% <59.61%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread pkg/cmd/register/register.go Outdated
Comment on lines 177 to 180
// Verify the user is authenticated before performing any local side effects.
if _, err := s.GetCurrentUser(); err != nil {
return breverrors.WrapAndTrace(err)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Only do this if we're in the normal non-key mode?

@patelspratik patelspratik Aug 28, 2026

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.

this should really be something like "isAuthenticated". I'll fix this with a better sentinel.

Base automatically changed from pr1-register-resiliency to main August 28, 2026 18:26
Rebased onto pr1-register-resiliency (post ssh-certs uptake).

- BREV_ACCESS_KEY renamed to BREV_API_KEY (never released); all
  access-key identifiers and messages renamed to API key
- Env-key orgs resolve in real time from the backend: the key is bound
  to exactly one org server-side, so no persisted org is consulted and
  staleness is impossible. Established logins keep persisted org
  behavior (APIKeyOrgID / active-org cache)
- Single-org invariant hoisted to auth.SingleOrgForAPIKey, shared by
  register and the general resolution path
- GetActiveOrganizationOrNil branches on credential source; env path
  returns the full org (name included) without a second GetOrganization
  round-trip
- Register with no env key and no persisted credential now prompts the
  device-flow login (externalNodeAuth fallback) instead of erroring;
  tokens stay in memory, the login email is cached for pre-fill
- Register tests moved/renamed to the APIKey convention
A declined login prompt used to spray stack-traced WARN/ERROR retry
chatter through resty and print a multi-frame trace at exit.

- auth: return DeclineToLoginError unwrapped from PromptForLogin,
  GetFreshAccessTokenOrLogin, and LoginAuth.GetAccessToken so the
  sentinel travels without trace wrapping
- store: pass the sentinel through AuthHTTPStore/OnBeforeRequest
  unwrapped; quiet resty logger suppresses retry chatter for the
  decline message while logging everything else
- cmderrors: friendly message + directive for declined login; no
  Sentry report, no stack trace
- errors: export DeclineToLoginMessage const used by the log filter
The GetCurrentUser probe existed to catch stale JWTs before local side
effects. With an API key present that probe is redundant — the key is
validated for real by the first API call it authenticates
(ResolveOrgForAPIKey) — so skip it and let the org resolution surface
any auth failure with a key-specific message.
A declined login prompt is not an error: the user deliberately said no,
and the prompt itself is all the context they need. Handle it before
any Sentry breadcrumb or switch processing — no breadcrumb, no message,
no directive. Remove the now-redundant DeclineToLoginError case from
the display switch.

Also update TestGetAPIKeyOrgID_MissingPersistedOrgReturnsError to match
the renamed MissingAPIKeyOrgIDMessage wording.
The analytics flag serialization sent every explicitly-set flag's value
to PostHog, including the hidden global --api-key flag — disclosing the
full credential on both success and failure capture paths.

Fix generically:
- redactFlagValue: values matching a Brev API key (bak- prefix) or a
  JWT shape (three dot-separated segments) are replaced with
  "[redacted]"; benign values pass through
- MarkFlagSensitive: pflag annotation for flags that must always be
  redacted regardless of value shape
- applied to the global --api-key flag and login's --token/--api-key
  flags; redaction is centralized in one visitor shared by both
  capture paths

Flag names are still reported, so usage analytics survive.
The factory wired quietRestyLogger over discardLogger, so while the
filter itself correctly forwarded non-decline messages, its sink
swallowed everything: real HTTP errors, unexpected auth failures, and
all debug output were silently dropped. The declined-login test masked
this by replacing the sink after construction.

Replace discardLogger with stderrLogger (mirrors resty's default:
stderr, date+micro, WARN/ERROR RESTY prefixes) and test the full
factory chain without sink replacement: declined login stays quiet,
unrelated auth errors still reach stderr with their message.
- goprintffuncname: output -> outputf, write -> writef
- wrapcheck: the two DeclineToLoginError passthrough returns are
  deliberate (the sentinel must reach DisplayAndHandleError unwrapped
  so it renders silently); annotate with //nolint:wrapcheck
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