GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Google Drive: a 401 is sealed as a refusal, reported until Google acc… · gitmoot/keyring@a7b80fd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • deploy/cloudflare
    • README.md
    • src
      • index.ts
  • docs
    • google-gcloud.md
  • internal
    • dashboard
      • dashboard.go
    • googlelogin
      • controller.go
      • controller_test.go
      • setup.go
      • state.go
    • instance
      • google_test.go
      • instance.go
    • server
      • google.go
      • google_diagnostics_test.go
      • google_test.go
      • server.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎deploy/cloudflare/README.md‎

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 numberDiff line numberDiff line change
@@ -491,6 +491,23 @@ proxy requests. `GET /admin/instances/<id>/services` reports the four services a
491491
connected only after login/project setup is saved, with a safe failure hint;
492492
it exposes no Google account, project, authorization URL, code, or credential.
493493

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+
494511
SDK credentials are bounded opaque database snapshots (256 KiB total), in a
495512
dedicated field of the instance's encrypted state, never in forwarded keys.
496513
Only the fixed image executable and a generated private directory beneath
Collapse file

‎deploy/cloudflare/src/index.ts‎

Copy file name to clipboardExpand all lines: deploy/cloudflare/src/index.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ export class KeyringInstance extends DurableObject<Env> {
610610
.map(([name, svc]) => {
611611
const google = svc.google_account === "me" && (svc.auth === "gcloud" || svc.auth === "gcloud-login");
612612
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],
614615
failing: google ? googleError ?? s.failing?.[name] ?? (s.google?.phase === "connected" ? null : "Google connection is not ready") : s.failing?.[name] ?? null };
615616
})
616617
.sort((a, b) => a.name.localeCompare(b.name));
Collapse file

‎docs/google-gcloud.md‎

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 numberDiff line numberDiff line change
@@ -98,7 +98,17 @@ removing and re-adding the same binding. A failed renewal fails closed (502), wi
9898
the existing 10-second error-cache delay; **no background retry or diagnostic
9999
probe runs**. It never uses another account or a stale token. An upstream 401
100100
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
102112
to sign in; runtime, network and unknown failures must not be treated as login
103113
problems. Host logout alone does not erase an already minted in-memory access
104114
token: revoke the role first to stop new proxy calls immediately, then perform
Collapse file

‎internal/dashboard/dashboard.go‎

Copy file name to clipboardExpand all lines: internal/dashboard/dashboard.go
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,18 +933,18 @@ func (b *Backend) keysPage(w http.ResponseWriter, r *http.Request, sid string) {
933933
var accounts []googleRow
934934
cfg := b.Proxy.Config()
935935
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)}
938938
history := b.Proxy.GoogleHistory(name)
939939
row.History, row.HistoryUnavailable = history.Incidents, history.Unavailable
940940
if !history.LastSuccess.IsZero() {
941941
row.LastSuccess = history.LastSuccess.UTC().Format(time.RFC3339)
942942
}
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" {
944944
row.Status = "Last recorded failure: " + history.Incidents[n-1].Failure.Error()
945945
}
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)
948948
}
949949
for service, svc := range cfg.Services {
950950
if svc.Auth == policy.AuthGcloud && svc.GoogleAccount == name {
Collapse file

‎internal/googlelogin/controller.go‎

Copy file name to clipboardExpand all lines: internal/googlelogin/controller.go
+105-6Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ type View struct {
3232
// take an existing project (see ChooseProject). Only the consent page
3333
// of that flow sees it.
3434
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:"-"`
3539
}
3640

3741
type job struct {
@@ -62,6 +66,17 @@ type Controller struct {
6266
executable string
6367
root string
6468
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
6580
}
6681

6782
func New(save func(context.Context, string, *Snapshot) error) *Controller {
@@ -109,13 +124,15 @@ func (c *Controller) Restore(next *Snapshot) error {
109124
return err
110125
}
111126

112-
func stateView(s *Snapshot) View {
127+
func stateView(s *Snapshot, refused bool) View {
113128
v := View{Phase: "disconnected"}
114129
if s == nil {
115130
return v
116131
}
117132
v.Account, v.Project = s.Account, s.Project
118133
switch {
134+
case s.Ready && refused:
135+
v.Phase = "reconnect_required"
119136
case s.Ready:
120137
v.Phase = "connected"
121138
case s.NeedsLogin || s.Account == "":
@@ -126,10 +143,25 @@ func stateView(s *Snapshot) View {
126143
return v
127144
}
128145

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+
129159
func (c *Controller) Status() View {
130160
c.mu.Lock()
131161
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
133165
if c.closing {
134166
v.Phase, v.Busy = "disconnecting", true
135167
} else if c.job != nil && !c.job.done {
@@ -265,6 +297,67 @@ func (c *Controller) chooseLocked(ctx context.Context, project string) error {
265297
return c.save(ctx, current.Generation, &next)
266298
}
267299

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+
268361
func (c *Controller) acquire(ctx context.Context) error {
269362
select {
270363
case c.ops <- struct{}{}:
@@ -327,12 +420,17 @@ func (c *Controller) connect(ctx context.Context, j *job) {
327420
}
328421

329422
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 {
332430
return nil
333431
}
334432
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 {
336434
var random [16]byte
337435
if _, err := rand.Read(random[:]); err != nil {
338436
return err
@@ -446,7 +544,8 @@ func (c *Controller) Token(ctx context.Context, binding policy.GoogleAccount, tr
446544
}
447545
if errors.Is(runErr, errLogin) {
448546
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
450549
next = &invalid
451550
}
452551
if next != s {
Collapse file

‎internal/googlelogin/controller_test.go‎

Copy file name to clipboardExpand all lines: internal/googlelogin/controller_test.go
+113Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,119 @@ func TestLoginTermsRetryRefreshAndRestore(t *testing.T) {
193193
}
194194
}
195195

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+
196309
func TestDisconnectCancelsRefreshAndRevokesOldFlows(t *testing.T) {
197310
c, saved, _ := controllerFixture(t)
198311
if err := c.Start("browser", c.Epoch(), ""); err != nil {
Collapse file

‎internal/googlelogin/setup.go‎

Copy file name to clipboardExpand all lines: internal/googlelogin/setup.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (c *Controller) projectCommand(ctx context.Context, s *Snapshot, args ...st
3131
}
3232
if errors.Is(runErr, errLogin) {
3333
invalid := *next
34-
invalid.Ready, invalid.NeedsLogin = false, true
34+
invalid.Ready, invalid.NeedsLogin, invalid.Refused = false, true, false
3535
next = &invalid
3636
}
3737
if next != current {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.