| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "time" |
| 9 | |
| 10 | "golang.org/x/sys/unix" |
| 11 | ) |
| 12 | |
| 13 | // CopyLifetime is how long a runtime credential copy may be used after the |
| 14 | // source home was refreshed. A copy never writes back and cannot reach |
| 15 | // refresh hosts, so a process lives at most one copy. |
| 16 | const CopyLifetime = 25 * time.Minute |
| 17 | |
| 18 | // OpenRuntimeCopy refreshes the source home with the official CLI under |
| 19 | // the source lock, stages a runtime copy and releases the lock. The lock is |
| 20 | // held only for refresh and staging: copies cannot refresh, so maintenance |
| 21 | // cannot race them. A busy source is retried until wait passes. |
| 22 | func OpenRuntimeCopy(ctx context.Context, b Binding, refresh func(context.Context, Binding) (Identity, error), wait time.Duration) (*AccountLease, error) { |
| 23 | if refresh == nil { |
| 24 | return nil, ErrPolicy |
| 25 | } |
| 26 | deadline := time.Now().Add(wait) |
| 27 | for { |
| 28 | l, err := openAccountRefreshing(ctx, b, DefaultHomeRules(), refresh) |
| 29 | if err == nil { |
| 30 | if err = l.detach(); err != nil { |
| 31 | l.Close() |
| 32 | return nil, err |
| 33 | } |
| 34 | return l, nil |
| 35 | } |
| 36 | if !errors.Is(err, ErrAccountBusy) || time.Now().After(deadline) { |
| 37 | return nil, err |
| 38 | } |
| 39 | select { |
| 40 | case <-ctx.Done(): |
| 41 | return nil, ctx.Err() |
| 42 | case <-time.After(500 * time.Millisecond): |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func (l *AccountLease) detach() error { |
| 48 | l.mu.Lock() |
| 49 | defer l.mu.Unlock() |
| 50 | raw, generation, err := l.snapshot() |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | until := l.sourceFreshUntil |
| 55 | if expires := credentialsExpireAt(raw); !expires.IsZero() && expires.Before(until) { |
| 56 | until = expires |
| 57 | } |
| 58 | l.sourceFreshUntil = until |
| 59 | l.generation = generation |
| 60 | l.detached = true |
| 61 | l.ready = true |
| 62 | return unix.Flock(int(l.dir.Fd()), unix.LOCK_UN) |
| 63 | } |
| 64 | |
| 65 | // FreshUntil is when this runtime copy must stop being used. |
| 66 | func (l *AccountLease) FreshUntil() time.Time { |
| 67 | l.mu.Lock() |
| 68 | defer l.mu.Unlock() |
| 69 | return l.sourceFreshUntil |
| 70 | } |
| 71 | |
| 72 | // CheckCopy is the check before each turn on a detached copy: still fresh, |
| 73 | // the runtime credential unchanged, only expected runtime entries, and the |
| 74 | // policy's generated and pinned files unchanged. |
| 75 | func (l *AccountLease) CheckCopy(ctx context.Context) error { |
| 76 | l.mu.Lock() |
| 77 | defer l.mu.Unlock() |
| 78 | if !l.detached || l.closed { |
| 79 | return ErrAccountMaintenance |
| 80 | } |
| 81 | if err := ctx.Err(); err != nil { |
| 82 | return err |
| 83 | } |
| 84 | if l.contained { |
| 85 | return accountContainment("latched") |
| 86 | } |
| 87 | if !time.Now().Before(l.sourceFreshUntil) { |
| 88 | return ErrPreflightRequired |
| 89 | } |
| 90 | raw, err := l.runtimeSnapshot() |
| 91 | if err != nil { |
| 92 | l.latch(err) |
| 93 | return err |
| 94 | } |
| 95 | if sha256.Sum256(raw) != l.runtimeGeneration { |
| 96 | return ErrRefreshPending |
| 97 | } |
| 98 | if err = l.validatePolicy(); err != nil { |
| 99 | l.latch(err) |
| 100 | return err |
| 101 | } |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | func credentialsExpireAt(raw []byte) time.Time { |
| 106 | var v struct { |
| 107 | Claude struct { |
| 108 | ExpiresAt int64 `json:"expiresAt"` |
| 109 | } `json:"claudeAiOauth"` |
| 110 | } |
| 111 | if json.Unmarshal(raw, &v) != nil || v.Claude.ExpiresAt <= 0 { |
| 112 | return time.Time{} |
| 113 | } |
| 114 | return time.UnixMilli(v.Claude.ExpiresAt) |
| 115 | } |
| 116 | |