| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "slices" |
| 11 | "sort" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 14 | runnersetup "amadan.net/oficina/equipo/internal/runner/setup" |
| 15 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 16 | ) |
| 17 | |
| 18 | type AccountBinding = runnersetup.AccountBinding |
| 19 | |
| 20 | func (c Config) securityPaths(bs []AccountBinding) workspace.SecurityPaths { |
| 21 | home, _ := os.UserHomeDir() |
| 22 | homes := []string{filepath.Join(home, ".claude"), filepath.Join(home, ".codex")} |
| 23 | for _, b := range bs { |
| 24 | if b.ProviderHome != "" { |
| 25 | homes = append(homes, b.ProviderHome) |
| 26 | } else { |
| 27 | homes = append(homes, b.CredentialHome) |
| 28 | } |
| 29 | } |
| 30 | return workspace.SecurityPaths{RunnerState: c.StateDir, RunnerConfig: c.AccountBindingsPath, Journal: filepath.Join(c.StateDir, "journal.db"), Bearer: filepath.Join(c.StateDir, "credentials.json"), Cleanup: filepath.Join(c.StateDir, "credentials.json"), ForgeCredential: c.AmadanCredentialPath, LegacyHome: filepath.Join(home, ".paseo"), SSH: filepath.Join(home, ".ssh"), ProviderHomes: homes, PreviewCredential: c.PreviewCredentialPath} |
| 31 | } |
| 32 | func bindingPolicy(c Config, bs []AccountBinding, b AccountBinding) (provider.ReadPolicy, error) { |
| 33 | s := c.securityPaths(bs) |
| 34 | paths := append(s.Credentials(), s.ProviderHomes...) |
| 35 | paths = append(paths, filepath.Join(c.StateDir, "private")) |
| 36 | var denied []string |
| 37 | for _, p := range paths { |
| 38 | real, err := workspace.CanonicalSecurityPath(p, provider.ErrPolicy) |
| 39 | if err != nil { |
| 40 | return provider.ReadPolicy{}, err |
| 41 | } |
| 42 | denied = append(denied, p, real) |
| 43 | } |
| 44 | sort.Strings(denied) |
| 45 | denied = slices.Compact(denied) |
| 46 | for _, root := range []string{c.WorkspaceRoot, c.ScratchRoot} { |
| 47 | for _, p := range denied { |
| 48 | if containsPath(p, root) { |
| 49 | return provider.ReadPolicy{}, provider.ErrPolicy |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | b.Model = "" |
| 54 | b.Label = "" |
| 55 | raw, _ := json.Marshal(struct { |
| 56 | Binding AccountBinding |
| 57 | Mode string |
| 58 | Denied []string |
| 59 | }{b, "default", denied}) |
| 60 | sum := sha256.Sum256(raw) |
| 61 | return provider.ReadPolicy{DeniedPaths: denied, ConfigHash: hex.EncodeToString(sum[:])}, nil |
| 62 | } |
| 63 | |
| 64 | // DirectAccount preserves the configured person's LocalID and model order. |
| 65 | // It contains no credentials and does not grant dispatch authority. |
| 66 | type DirectAccount struct { |
| 67 | Binding AccountBinding |
| 68 | Identity provider.Identity |
| 69 | Available bool |
| 70 | Failure error |
| 71 | } |
| 72 | |
| 73 | func loadDirectBindings(path string) ([]AccountBinding, error) { |
| 74 | var bs []AccountBinding |
| 75 | if e := privateJSON(path, &bs); e != nil { |
| 76 | if os.IsNotExist(e) { |
| 77 | return nil, nil |
| 78 | } |
| 79 | return nil, e |
| 80 | } |
| 81 | if len(bs) > 256 { |
| 82 | return nil, provider.ErrIdentity |
| 83 | } |
| 84 | seen := map[string]bool{} |
| 85 | accounts := map[string]AccountBinding{} |
| 86 | homes := map[string]AccountBinding{} |
| 87 | homeIDs := map[string]AccountBinding{} |
| 88 | accountIDs := map[string]AccountBinding{} |
| 89 | rules := provider.DefaultHomeRules() |
| 90 | for _, b := range bs { |
| 91 | key := b.LocalID + "/" + b.Model |
| 92 | if b.ID == "" || b.LocalID == "" || b.Model == "" || b.AccountID == "" || b.ExpectedIdentity.AccountID != b.AccountID || b.ExpectedIdentity.HomeID == "" || b.ExpectedIdentity.ProviderVersion == "" || (provider.SubscriptionFamily(b.Provider) == "" || b.Family != provider.SubscriptionFamily(b.Provider)) || !(b.Kind == "subscription" || b.Kind == "api" || b.Kind == "token" && b.Provider == "claude") || rules.RefusesPath(b.ProviderHome) || seen[key] || b.CredentialHome != "" && b.CredentialHome != b.ProviderHome { |
| 93 | return nil, provider.ErrIdentity |
| 94 | } |
| 95 | if prior, ok := accounts[b.LocalID]; ok && (prior.ID != b.ID || prior.ProviderHome != b.ProviderHome || prior.AccountID != b.AccountID || prior.PersonID != b.PersonID || prior.ExpectedIdentity != b.ExpectedIdentity) { |
| 96 | return nil, provider.ErrIdentity |
| 97 | } |
| 98 | for _, prior := range []AccountBinding{homes[b.ProviderHome], homeIDs[b.ExpectedIdentity.HomeID], accountIDs[b.Provider+"/"+b.AccountID]} { |
| 99 | if prior.LocalID != "" && (prior.LocalID != b.LocalID || prior.PersonID != b.PersonID) { |
| 100 | return nil, provider.ErrIdentity |
| 101 | } |
| 102 | } |
| 103 | homes[b.ProviderHome] = b |
| 104 | homeIDs[b.ExpectedIdentity.HomeID] = b |
| 105 | accountIDs[b.Provider+"/"+b.AccountID] = b |
| 106 | seen[key] = true |
| 107 | accounts[b.LocalID] = b |
| 108 | } |
| 109 | return bs, nil |
| 110 | } |
| 111 | |
| 112 | // DiscoverDirect uses only configured dedicated homes. Adapters supply |
| 113 | // a no-prompt account-status probe. The dispatch path must open a fresh lease. |
| 114 | func DiscoverDirect(ctx context.Context, c Config, probe func(context.Context, provider.Binding) (provider.Identity, error)) ([]DirectAccount, error) { |
| 115 | return discoverDirect(ctx, c, func(ctx context.Context, b provider.Binding) (provider.Identity, error) { |
| 116 | l, e := provider.OpenAccountRefreshing(ctx, b, probe) |
| 117 | if e != nil { |
| 118 | return provider.Identity{}, e |
| 119 | } |
| 120 | defer l.Close() |
| 121 | return l.Identity(), nil |
| 122 | }) |
| 123 | } |
| 124 | func discoverDirect(ctx context.Context, c Config, check func(context.Context, provider.Binding) (provider.Identity, error)) ([]DirectAccount, error) { |
| 125 | bs, e := loadDirectBindings(c.AccountBindingsPath) |
| 126 | if e != nil { |
| 127 | return nil, e |
| 128 | } |
| 129 | reports := make([]DirectAccount, 0, len(bs)) |
| 130 | for _, b := range bs { |
| 131 | if e = ctx.Err(); e != nil { |
| 132 | return nil, e |
| 133 | } |
| 134 | binding, e := c.DirectBinding(b) |
| 135 | if e != nil { |
| 136 | return nil, e |
| 137 | } |
| 138 | id, e := check(ctx, binding) |
| 139 | reports = append(reports, DirectAccount{Binding: b, Identity: id, Available: e == nil && id == b.ExpectedIdentity, Failure: e}) |
| 140 | } |
| 141 | return reports, nil |
| 142 | } |
| 143 | |