Repository navigation
feat(auth): initiate CLI login and seal cli_login ticket cookie - #1348
Conversation
Register `GET /api/cli/login` on the experiment state server when production authentication and Cloud Spanner are enabled. Validate the loopback port, OAuth state, S256 PKCE challenge, and optional `login_hint`, enforce `Sec-Fetch-Dest: document` and `Sec-Fetch-Site: none`, seal the pending login ticket cookie (`aud: xmanager-cloud-cli-login`) bound to `oauth_state`, clear any stale `cli_login` cookie on `/api/login`, and redirect to the OIDC provider. Fixes #1309 TAG=agy CONV=6dd806a4-f14e-436b-bef8-6b53e044b43c
There was a problem hiding this comment.
Code Review
This pull request implements the backend support for browser-based CLI login (xmc login). It introduces a new endpoint /api/cli/login that initiates the OAuth flow for the CLI, utilizing PKCE and storing state in a signed cli_login cookie. The endpoints are registered only under production authentication when a Spanner client is available. Security measures such as fetch metadata checks (Sec-Fetch-Site and Sec-Fetch-Dest) are implemented to prevent CSRF. I have no feedback to provide as there are no review comments.
andrzejfudro
left a comment
There was a problem hiding this comment.
LGTM but please drop the explciit spannerClient and replace it with storage DI (either in this PR or in the follow up). There is also an AI suggestion that can be done in the follow up.
…itiation slice Drop `*spanner.Client` from `registerCLILogin` in the initiation slice (`#1348`) since login ticket initiation does not persist `CliLoginCodes` yet (and `#1314` injects `storage.Storage` instead of `*spanner.Client`). Also clarify the `Sec-Fetch-Site: none` threat-model scope in `docs/design/gateway.md` and `deploy/helm/README.md`. Comment link: #1348 (comment) Comment link: #1348 (comment) TAG=agy CONV=6dd806a4-f14e-436b-bef8-6b53e044b43c
There was a problem hiding this comment.
🤖 Incremental PR Approval
PR author is a code owner and changes since last approval (28 lines, excluding 36 test file lines) are within the 50-line grace delta.
Preserving prior approval by @andrzejfudro on commit 3a4ae4f.
There was a problem hiding this comment.
🤖 Incremental PR Approval
PR author is a code owner and changes since last approval (32 lines, excluding 36 test file lines) are within the 50-line grace delta.
Preserving prior approval by @andrzejfudro on commit 3a4ae4f.
Summary
Introduces the
GET /api/cli/logininitiation endpoint on the experiment state server and seals the pending CLI login state into a signedcli_logincookie (aud: xmanager-cloud-cli-login) bound to theoauth_stateof the OIDC round trip, then redirects the browser to the identity provider.Motivation & Context
xmanager loginsigns the user in through the browser using the existing Dashboard OAuth client and/api/callbackredirect URI (google/xmc#1309). Splitting the authorization initiation (GET /api/cli/login) from the callback completion (GET /api/callbackandCliLoginCodespersistence) keeps each vertical slice focused and under the repository diff size thresholds with zero dead code.Key Changes
pkg/auth/oauth2/cli_login.go: ImplementsAuthenticator.EnableCLILogin()andAuthenticator.HandleCLILogin, validatingport(1024..65535),state, S256code_challenge,code_challenge_method=S256, and optional printable-ASCIIlogin_hint, while enforcingSec-Fetch-Dest: documentandSec-Fetch-Site: nonewhen fetch metadata headers are present.pkg/auth/oauth2/cli_login_ticket.go: ImplementscliLoginTicket,hashOAuthState, andsealCLILoginTicket(sealSigned) to issue HS256-signedcli_logincookies scoped to/api/callback.pkg/auth/oauth2/handlers.go&provider.go: Clears any stalecli_logincookie on Dashboard/api/loginrequests and forwards optionaloauth2.AuthCodeOptionparameters (login_hint) throughOAuthProvider.AuthCodeURL.cmd/experiment_state_server/cli_login.go&main.go: Registers/api/cli/loginon the HTTP mux only when production authentication (!MockAuth && !DisableAuthentication) and a Cloud Spanner client are configured.Assumptions
None — requirements were fully specified.
Verification & Testing
bazel test --test_env=SPANNER_EMULATOR_HOST=localhost:9010 //cmd/experiment_state_server/... //pkg/auth/oauth2/...and./.githooks/pre-commit//cmd/experiment_state_server:cli_login_testand//pkg/auth/oauth2:oauth2_testpass, covering parameter validation,Sec-Fetch-*CSRF/embedding rejections,login_hintforwarding, signedcli_loginticket cookie attributes, and/api/loginstale cookie deletion.Related Issues / Bugs
Part of #1309
TAG=agy
CONV=6dd806a4-f14e-436b-bef8-6b53e044b43c