GeezerVison is showing github.com — Google Drive: a 401 is sealed as a refusal, reported until Google acc… · gitmoot/keyring@a7b80fd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: deploy/cloudflare/README.md +17Lines changed: 17 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -491,6 +491,23 @@ proxy requests. `GET /admin/instances/<id>/services` reports the four services a | 491 | 491 | connected only after login/project setup is saved, with a safe failure hint; | 492 | 492 | it exposes no Google account, project, authorization URL, code, or credential. | 493 | 493 |
| | 494 | +gcloud can keep minting access tokens that Google refuses, for example with | | 495 | +"Active session is invalid" after the account signed out everywhere or changed | | 496 | +its password. A Google 401 to any proxied call therefore drops that token (the | | 497 | +next call mints a new one) and records the refusal, sealed with the login: all | | 498 | +four services report `connected: false` with `failing: "Google refused the | | 499 | +sign-in (HTTP 401, session invalid); open a new Drive link to sign in again"` | | 500 | +(`reconnect_required` in the container's `/status`, Google phase | | 501 | +`reconnect_required`). Only a call Google accepts clears it; a container restart | | 502 | +or a failed token mint does not. Credentials are kept until the next connection: | | 503 | +a new `drive` link then signs in again (same account; the project is kept) | | 504 | +instead of reporting the refused login as connected. The refusal is sealed | | 505 | +before the proxied 401 is returned (waiting at most 10 seconds), so the Worker's | | 506 | +state fetch after that response already carries it; if it cannot be sealed in | | 507 | +time, the running container still reports it and the next link still signs in | | 508 | +again. Answers to concurrent calls count in the order the calls took their | | 509 | +token: a late answer to an earlier call never overrides a later call's. | | 510 | + | 494 | 511 | SDK credentials are bounded opaque database snapshots (256 KiB total), in a | 495 | 512 | dedicated field of the instance's encrypted state, never in forwarded keys. | 496 | 513 | Only the fixed image executable and a generated private directory beneath | |
Collapse file Copy file name to clipboardExpand all lines: deploy/cloudflare/src/index.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -610,7 +610,8 @@ export class KeyringInstance extends DurableObject<Env> { | 610 | 610 | .map(([name, svc]) => { | 611 | 611 | const google = svc.google_account === "me" && (svc.auth === "gcloud" || svc.auth === "gcloud-login"); | 612 | 612 | const credentialsPresent = keys.has(svc.key) && Object.values(svc.extra_headers ?? {}).every((key) => keys.has(key)); | 613 | | - return { name, connected: google ? s.google?.phase === "connected" : credentialsPresent && !s.reconnect_required?.[name], | | 613 | + // Google refusing the login's tokens (401) is not connected, even with a sealed login. | | 614 | + return { name, connected: google ? s.google?.phase === "connected" && !s.reconnect_required?.[name] : credentialsPresent && !s.reconnect_required?.[name], | 614 | 615 | failing: google ? googleError ?? s.failing?.[name] ?? (s.google?.phase === "connected" ? null : "Google connection is not ready") : s.failing?.[name] ?? null }; | 615 | 616 | }) | 616 | 617 | .sort((a, b) => a.name.localeCompare(b.name)); | |
Collapse file Copy file name to clipboardExpand all lines: docs/google-gcloud.md +11-1Lines changed: 11 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -98,7 +98,17 @@ removing and re-adding the same binding. A failed renewal fails closed (502), wi | 98 | 98 | the existing 10-second error-cache delay; **no background retry or diagnostic | 99 | 99 | probe runs**. It never uses another account or a stale token. An upstream 401 | 100 | 100 | invalidates only the token used and is returned **without replay**, including | 101 | | -POST/PATCH writes. Only a confirmed reauthentication requirement asks the owner | | 101 | +POST/PATCH writes. The next call mints a new token. Because gcloud can keep | | 102 | +minting tokens that Google refuses (for example "Active session is invalid" | | 103 | +after the account signed out everywhere), a 401 also marks the account refused: | | 104 | +the dashboard status reads "Google refused the gcloud token (HTTP 401); run | | 105 | +gcloud auth login for this account" until Google accepts a call again; a failed | | 106 | +or new mint does not clear it. On a host this mark lives in the running proxy | | 107 | +(a restart forgets it until the next 401); in the container it is sealed with | | 108 | +the Drive login before the 401 is returned. Answers to concurrent calls count in | | 109 | +the order the calls took their token, so a late answer to an earlier call never | | 110 | +overrides a later call's. Nothing is deleted and the 401 alone is not treated as a login | | 111 | +requirement. Only a confirmed reauthentication requirement asks the owner | 102 | 112 | to sign in; runtime, network and unknown failures must not be treated as login | 103 | 113 | problems. Host logout alone does not erase an already minted in-memory access | 104 | 114 | token: revoke the role first to stop new proxy calls immediately, then perform | |
Collapse file Copy file name to clipboardExpand all lines: internal/dashboard/dashboard.go +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -933,18 +933,18 @@ func (b *Backend) keysPage(w http.ResponseWriter, r *http.Request, sid string) { | 933 | 933 | var accounts []googleRow | 934 | 934 | cfg := b.Proxy.Config() | 935 | 935 | for name, a := range cfg.GoogleAccounts { | 936 | | - status, expires := b.Proxy.GoogleStatus(name) | 937 | | - row := googleRow{Name: name, Account: a.Account, Status: status} | | 936 | + google := b.Proxy.GoogleStatus(name) | | 937 | + row := googleRow{Name: name, Account: a.Account, Status: google.Text(cfg.ContainerMode)} | 938 | 938 | history := b.Proxy.GoogleHistory(name) | 939 | 939 | row.History, row.HistoryUnavailable = history.Incidents, history.Unavailable | 940 | 940 | if !history.LastSuccess.IsZero() { | 941 | 941 | row.LastSuccess = history.LastSuccess.UTC().Format(time.RFC3339) | 942 | 942 | } | 943 | | - if n := len(history.Incidents); status == "not checked" && n != 0 && history.Incidents[n-1].Outcome == "active" { | | 943 | + if n := len(history.Incidents); google.Mint == server.GoogleNotChecked && n != 0 && history.Incidents[n-1].Outcome == "active" { | 944 | 944 | row.Status = "Last recorded failure: " + history.Incidents[n-1].Failure.Error() | 945 | 945 | } | 946 | | - if !expires.IsZero() { | 947 | | - row.Expires = expires.UTC().Format(time.RFC3339) | | 946 | + if !google.Expires.IsZero() && !google.Refused { | | 947 | + row.Expires = google.Expires.UTC().Format(time.RFC3339) | 948 | 948 | } | 949 | 949 | for service, svc := range cfg.Services { | 950 | 950 | if svc.Auth == policy.AuthGcloud && svc.GoogleAccount == name { | |
Collapse file Copy file name to clipboardExpand all lines: internal/googlelogin/controller.go +105-6Lines changed: 105 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,10 @@ type View struct { | 32 | 32 | // take an existing project (see ChooseProject). Only the consent page | 33 | 33 | // of that flow sees it. | 34 | 34 | ProjectChoosable bool `json:"-"` | | 35 | + // Refused: Google refuses the connected login's tokens (it answered 401 | | 36 | + // and has accepted no call since), whatever the phase shows. Status | | 37 | + // sets it. | | 38 | + Refused bool `json:"-"` | 35 | 39 | } | 36 | 40 |
| 37 | 41 | type job struct { | @@ -62,6 +66,17 @@ type Controller struct { | 62 | 66 | executable string | 63 | 67 | root string | 64 | 68 | client *http.Client | | 69 | + // answer is Google's latest answer for a ready login's tokens. It | | 70 | + // counts in this process even when sealing it failed. | | 71 | + answer *answer | | 72 | +} | | 73 | + | | 74 | +// answer is Google's answer to a call with a binding's token: refused (401) | | 75 | +// or accepted. seq orders answers; an older one never overrides a newer one. | | 76 | +type answer struct { | | 77 | + binding policy.GoogleAccount | | 78 | + refused bool | | 79 | + seq uint64 | 65 | 80 | } | 66 | 81 |
| 67 | 82 | func New(save func(context.Context, string, *Snapshot) error) *Controller { | @@ -109,13 +124,15 @@ func (c *Controller) Restore(next *Snapshot) error { | 109 | 124 | return err | 110 | 125 | } | 111 | 126 |
| 112 | | -func stateView(s *Snapshot) View { | | 127 | +func stateView(s *Snapshot, refused bool) View { | 113 | 128 | v := View{Phase: "disconnected"} | 114 | 129 | if s == nil { | 115 | 130 | return v | 116 | 131 | } | 117 | 132 | v.Account, v.Project = s.Account, s.Project | 118 | 133 | switch { | | 134 | + case s.Ready && refused: | | 135 | + v.Phase = "reconnect_required" | 119 | 136 | case s.Ready: | 120 | 137 | v.Phase = "connected" | 121 | 138 | case s.NeedsLogin || s.Account == "": | @@ -126,10 +143,25 @@ func stateView(s *Snapshot) View { | 126 | 143 | return v | 127 | 144 | } | 128 | 145 |
| | 146 | +// refusedLocked reports whether Google refuses the tokens of s, a ready | | 147 | +// login: the latest answer seen for its binding in this process, else the | | 148 | +// sealed one. c.mu is held. | | 149 | +func (c *Controller) refusedLocked(s *Snapshot) bool { | | 150 | + if s == nil || !s.Ready { | | 151 | + return false | | 152 | + } | | 153 | + if a := c.answer; a != nil && a.binding == s.Binding() { | | 154 | + return a.refused | | 155 | + } | | 156 | + return s.Refused | | 157 | +} | | 158 | + | 129 | 159 | func (c *Controller) Status() View { | 130 | 160 | c.mu.Lock() | 131 | 161 | defer c.mu.Unlock() | 132 | | - v := stateView(c.state) | | 162 | + refused := c.refusedLocked(c.state) | | 163 | + v := stateView(c.state, refused) | | 164 | + v.Refused = refused | 133 | 165 | if c.closing { | 134 | 166 | v.Phase, v.Busy = "disconnecting", true | 135 | 167 | } else if c.job != nil && !c.job.done { | @@ -265,6 +297,67 @@ func (c *Controller) chooseLocked(ctx context.Context, project string) error { | 265 | 297 | return c.save(ctx, current.Generation, &next) | 266 | 298 | } | 267 | 299 |
| | 300 | +// RecordRefusal records Google's answer to call seq, made with a token of the | | 301 | +// connected login: refused (it answered 401) or accepted. seq orders the | | 302 | +// answers; one to a call made before the latest recorded one's is ignored. A | | 303 | +// changed answer is sealed before RecordRefusal returns (within ctx), so it | | 304 | +// survives a restart and failed token mints and is saved with the response | | 305 | +// that carried it. Status reports a refusal until Google accepts a call | | 306 | +// again, and the next connection signs in again instead of keeping the | | 307 | +// refused login. Credentials are kept. If sealing fails, the answer still | | 308 | +// counts in this process and the next answer seals it. An answer that | | 309 | +// changes neither the latest answer nor the sealed one costs one lock. | | 310 | +func (c *Controller) RecordRefusal(ctx context.Context, binding policy.GoogleAccount, refused bool, seq uint64) { | | 311 | + c.mu.Lock() | | 312 | + s := c.state | | 313 | + if c.closing || s == nil || !s.Ready || s.Binding() != binding { | | 314 | + c.mu.Unlock() | | 315 | + return | | 316 | + } | | 317 | + prev := c.answer | | 318 | + if prev != nil && prev.binding == binding && prev.seq > seq { | | 319 | + c.mu.Unlock() | | 320 | + return | | 321 | + } | | 322 | + c.answer = &answer{binding, refused, seq} | | 323 | + // A seal of the previous answer may still run: its result is checked | | 324 | + // against this answer once it is done. | | 325 | + unchanged := s.Refused == refused && (prev == nil || prev.binding != binding || prev.refused == refused) | | 326 | + c.mu.Unlock() | | 327 | + if !unchanged { | | 328 | + c.sealRefusal(ctx) | | 329 | + } | | 330 | +} | | 331 | + | | 332 | +// sealRefusal seals the latest answer for the ready login, serialized with | | 333 | +// the other SDK writers. It returns once ctx is done even while the save | | 334 | +// still waits for the instance transaction; that save then fails on ctx. | | 335 | +func (c *Controller) sealRefusal(ctx context.Context) { | | 336 | + if c.acquire(ctx) != nil { | | 337 | + return | | 338 | + } | | 339 | + c.mu.Lock() | | 340 | + s := c.state | | 341 | + refused := c.refusedLocked(s) | | 342 | + c.mu.Unlock() | | 343 | + if s == nil || !s.Ready || s.Refused == refused { | | 344 | + <-c.ops | | 345 | + return | | 346 | + } | | 347 | + next := *s | | 348 | + next.Refused = refused | | 349 | + saved := make(chan struct{}) | | 350 | + go func() { | | 351 | + defer func() { <-c.ops }() | | 352 | + defer close(saved) | | 353 | + _ = c.save(ctx, s.Generation, &next) | | 354 | + }() | | 355 | + select { | | 356 | + case <-saved: | | 357 | + case <-ctx.Done(): | | 358 | + } | | 359 | +} | | 360 | + | 268 | 361 | func (c *Controller) acquire(ctx context.Context) error { | 269 | 362 | select { | 270 | 363 | case c.ops <- struct{}{}: | @@ -327,12 +420,17 @@ func (c *Controller) connect(ctx context.Context, j *job) { | 327 | 420 | } | 328 | 421 |
| 329 | 422 | func (c *Controller) connectLocked(ctx context.Context, j *job) error { | 330 | | - current := c.snapshot() | 331 | | - if current != nil && current.Ready { | | 423 | + c.mu.Lock() | | 424 | + current := c.state | | 425 | + refused := c.refusedLocked(current) | | 426 | + c.mu.Unlock() | | 427 | + // A refused login signs in again: gcloud would keep minting tokens | | 428 | + // Google refuses. | | 429 | + if current != nil && current.Ready && !refused { | 332 | 430 | return nil | 333 | 431 | } | 334 | 432 | next := current | 335 | | - if current == nil || current.NeedsLogin || current.Account == "" || len(current.Files["credentials.db"]) == 0 { | | 433 | + if current == nil || refused || current.NeedsLogin || current.Account == "" || len(current.Files["credentials.db"]) == 0 { | 336 | 434 | var random [16]byte | 337 | 435 | if _, err := rand.Read(random[:]); err != nil { | 338 | 436 | return err | @@ -446,7 +544,8 @@ func (c *Controller) Token(ctx context.Context, binding policy.GoogleAccount, tr | 446 | 544 | } | 447 | 545 | if errors.Is(runErr, errLogin) { | 448 | 546 | invalid := *next | 449 | | - invalid.Ready, invalid.NeedsLogin = false, true | | 547 | + // NeedsLogin already asks for the sign-in a refusal asked for. | | 548 | + invalid.Ready, invalid.NeedsLogin, invalid.Refused = false, true, false | 450 | 549 | next = &invalid | 451 | 550 | } | 452 | 551 | if next != s { | |
Collapse file Copy file name to clipboardExpand all lines: internal/googlelogin/controller_test.go +113Lines changed: 113 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -193,6 +193,119 @@ func TestLoginTermsRetryRefreshAndRestore(t *testing.T) { | 193 | 193 | } | 194 | 194 | } | 195 | 195 |
| | 196 | +// Google refusing a connected login's tokens (401) is sealed before | | 197 | +// RecordRefusal returns: it survives a restart and failed mints and is | | 198 | +// cleared only by an accepted call, and the next connection signs in again | | 199 | +// with the same account and project instead of reporting the refused login | | 200 | +// as connected (#184). | | 201 | +func TestRefusedLoginSignsInAgain(t *testing.T) { | | 202 | + c, saved, _ := controllerFixture(t) | | 203 | + ctx := context.Background() | | 204 | + connected := connectFixture(t, c, "browser") | | 205 | + if saved() != connected { | | 206 | + t.Fatal("connection not sealed") | | 207 | + } | | 208 | + binding := connected.Binding() | | 209 | + if c.RecordRefusal(ctx, binding, false, 1); saved() != connected { | | 210 | + t.Fatal("an accepted call on a healthy login changed it") | | 211 | + } | | 212 | + c.RecordRefusal(ctx, binding, true, 2) | | 213 | + if s := saved(); !s.Refused || !s.Ready || s.Generation != connected.Generation || string(s.Files["credentials.db"]) != string(connected.Files["credentials.db"]) || c.Status().Phase != "reconnect_required" || !c.Status().Refused { | | 214 | + t.Fatalf("refused login: sealed=%v ready=%v generation kept=%v phase=%s", s.Refused, s.Ready, s.Generation == connected.Generation, c.Status().Phase) | | 215 | + } | | 216 | + if c.RecordRefusal(ctx, binding, false, 3); saved().Refused || c.Status().Phase != "connected" || c.Status().Refused { | | 217 | + t.Fatalf("accepted call left sealed=%v phase=%s", saved().Refused, c.Status().Phase) | | 218 | + } | | 219 | + c.RecordRefusal(ctx, binding, true, 4) | | 220 | + // A failed mint is no accepted call. | | 221 | + failure := filepath.Join(c.root, "failure") | | 222 | + if err := os.WriteFile(failure, []byte("backend error SECRET-CANARY"), 0600); err != nil { | | 223 | + t.Fatal(err) | | 224 | + } | | 225 | + if _, err := c.Token(ctx, binding, nil); err == nil || !saved().Refused { | | 226 | + t.Fatalf("failed mint cleared the refusal: %v", err) | | 227 | + } | | 228 | + if err := os.Remove(failure); err != nil { | | 229 | + t.Fatal(err) | | 230 | + } | | 231 | + restarted, _, _ := controllerFixture(t) | | 232 | + if err := restarted.Restore(saved()); err != nil { | | 233 | + t.Fatal(err) | | 234 | + } | | 235 | + restarted.SetState(saved()) | | 236 | + if v := restarted.Status(); v.Phase != "reconnect_required" || !v.Refused { | | 237 | + t.Fatalf("restart lost the refusal: %+v", v) | | 238 | + } | | 239 | + // A new link signs in again instead of reporting the refused login. | | 240 | + again := connectFixture(t, c, "again") | | 241 | + if again.Generation == connected.Generation || again.Refused || !again.Ready || again.Account != connected.Account || again.Project != connected.Project { | | 242 | + t.Fatalf("new sign-in: new generation=%v refused=%v ready=%v same account=%v same project=%v", again.Generation != connected.Generation, again.Refused, again.Ready, again.Account == connected.Account, again.Project == connected.Project) | | 243 | + } | | 244 | + // Answers for the replaced login change nothing. | | 245 | + if c.RecordRefusal(ctx, binding, true, 5); saved() != again || c.Status().Refused { | | 246 | + t.Fatal("an answer for the replaced login was recorded") | | 247 | + } | | 248 | +} | | 249 | + | | 250 | +// connectFixture connects Drive through the consent flow id, signing in, and | | 251 | +// returns the sealed login. | | 252 | +func connectFixture(t *testing.T, c *Controller, id string) *Snapshot { | | 253 | + t.Helper() | | 254 | + if err := c.Start(id, c.Epoch(), ""); err != nil { | | 255 | + t.Fatal(err) | | 256 | + } | | 257 | + waitView(t, c, id, "awaiting_code") | | 258 | + if err := c.Submit(id, "fixture-code"); err != nil { | | 259 | + t.Fatal(err) | | 260 | + } | | 261 | + waitView(t, c, id, "connected") | | 262 | + return c.snapshot() | | 263 | +} | | 264 | + | | 265 | +// A refusal whose seal fails still counts in this process: status reports | | 266 | +// it and the next connection signs in again instead of reporting the | | 267 | +// refused login as connected. | | 268 | +func TestUnsealedRefusalStillSignsInAgain(t *testing.T) { | | 269 | + c, saved, failSave := controllerFixture(t) | | 270 | + connected := connectFixture(t, c, "browser") | | 271 | + *failSave = true | | 272 | + c.RecordRefusal(context.Background(), connected.Binding(), true, 1) | | 273 | + *failSave = false | | 274 | + if saved().Refused { | | 275 | + t.Fatal("fixture sealed the refusal despite the failed save") | | 276 | + } | | 277 | + if v := c.Status(); !v.Refused || v.Phase != "reconnect_required" { | | 278 | + t.Fatalf("unsealed refusal not reported: %s", c.Status().Phase) | | 279 | + } | | 280 | + if err := c.Start("again", c.Epoch(), ""); err != nil { | | 281 | + t.Fatal(err) | | 282 | + } | | 283 | + // A flow that skipped the sign-in ends connected before awaiting_code. | | 284 | + waitView(t, c, "again", "awaiting_code") | | 285 | + if err := c.Submit("again", "fixture-code"); err != nil { | | 286 | + t.Fatal(err) | | 287 | + } | | 288 | + waitView(t, c, "again", "connected") | | 289 | + if again := saved(); again.Generation == connected.Generation || again.Refused || c.Status().Refused { | | 290 | + t.Fatal("new sign-in did not replace the refused login") | | 291 | + } | | 292 | +} | | 293 | + | | 294 | +// Answers are ordered by their calls: one to an earlier call, arriving | | 295 | +// after a later call's answer, changes neither the sealed login nor status. | | 296 | +func TestRefusalAnswersOrderedByCall(t *testing.T) { | | 297 | + for _, later := range []bool{false, true} { | | 298 | + c, saved, _ := controllerFixture(t) | | 299 | + binding := connectFixture(t, c, "browser").Binding() | | 300 | + ctx := context.Background() | | 301 | + c.RecordRefusal(ctx, binding, later, 2) | | 302 | + c.RecordRefusal(ctx, binding, !later, 1) | | 303 | + if saved().Refused != later || c.Status().Refused != later { | | 304 | + t.Fatalf("later call refused=%v: sealed=%v status=%v after an earlier call's answer", later, saved().Refused, c.Status().Refused) | | 305 | + } | | 306 | + } | | 307 | +} | | 308 | + | 196 | 309 | func TestDisconnectCancelsRefreshAndRevokesOldFlows(t *testing.T) { | 197 | 310 | c, saved, _ := controllerFixture(t) | 198 | 311 | if err := c.Start("browser", c.Epoch(), ""); err != nil { | |
Collapse file Copy file name to clipboardExpand all lines: internal/googlelogin/setup.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,7 +31,7 @@ func (c *Controller) projectCommand(ctx context.Context, s *Snapshot, args ...st | 31 | 31 | } | 32 | 32 | if errors.Is(runErr, errLogin) { | 33 | 33 | invalid := *next | 34 | | - invalid.Ready, invalid.NeedsLogin = false, true | | 34 | + invalid.Ready, invalid.NeedsLogin, invalid.Refused = false, true, false | 35 | 35 | next = &invalid | 36 | 36 | } | 37 | 37 | if next != current { | |
|