| 1 | package runnersetup |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | "golang.org/x/sys/unix" |
| 13 | ) |
| 14 | |
| 15 | type AccountBinding struct { |
| 16 | Label string `json:"label,omitempty"` |
| 17 | PersonID string `json:"person_id,omitempty"` |
| 18 | ProviderHome string `json:"provider_home,omitempty"` |
| 19 | ExpectedIdentity provider.Identity `json:"expected_identity,omitempty"` |
| 20 | ID string `json:"id"` |
| 21 | LocalID string `json:"local_id"` |
| 22 | Provider string `json:"provider"` |
| 23 | Family string `json:"family"` |
| 24 | Model string `json:"model"` |
| 25 | AccountID string `json:"account_id"` |
| 26 | CredentialHome string `json:"credential_home"` |
| 27 | Kind string `json:"kind"` |
| 28 | } |
| 29 | |
| 30 | // SaveAccountBinding appends a verified account atomically and refuses any |
| 31 | // existing account identity or home. |
| 32 | func SaveAccountBinding(ctx context.Context, path, person, model string, s runnerproto.AccountSetup, b provider.Binding) error { |
| 33 | if err := ctx.Err(); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | if person == "" || model == "" || s.ID == "" || s.ID != b.ID || s.Provider != b.Provider || b.AccountID == "" || b.Expected.AccountID != b.AccountID || b.Expected.HomeID == "" || b.Expected.ProviderVersion == "" || provider.DefaultHomeRules().RefusesPath(b.Home) { |
| 37 | return provider.ErrIdentity |
| 38 | } |
| 39 | dir := filepath.Dir(path) |
| 40 | real, err := filepath.EvalSymlinks(dir) |
| 41 | if err != nil || real != dir { |
| 42 | return provider.ErrPolicy |
| 43 | } |
| 44 | f, err := os.Open(dir) |
| 45 | if err != nil { |
| 46 | return provider.ErrPolicy |
| 47 | } |
| 48 | defer f.Close() |
| 49 | st, err := f.Stat() |
| 50 | if err != nil || st.Mode().Perm() != 0700 { |
| 51 | return provider.ErrPolicy |
| 52 | } |
| 53 | if err = unix.Flock(int(f.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil { |
| 54 | return provider.ErrAccountBusy |
| 55 | } |
| 56 | defer unix.Flock(int(f.Fd()), unix.LOCK_UN) |
| 57 | var rows []AccountBinding |
| 58 | st, err = os.Lstat(path) |
| 59 | if err == nil { |
| 60 | if !st.Mode().IsRegular() || st.Mode().Perm() != 0600 || st.Size() > 1<<20 { |
| 61 | return provider.ErrPolicy |
| 62 | } |
| 63 | in, err := os.Open(path) |
| 64 | if err != nil { |
| 65 | return provider.ErrPolicy |
| 66 | } |
| 67 | err = runnerproto.DecodeLimit(io.LimitReader(in, 1<<20), &rows, 1<<20) |
| 68 | in.Close() |
| 69 | if err != nil { |
| 70 | return provider.ErrIdentity |
| 71 | } |
| 72 | } else if !os.IsNotExist(err) { |
| 73 | return provider.ErrPolicy |
| 74 | } |
| 75 | if len(rows) >= 256 { |
| 76 | return provider.ErrIdentity |
| 77 | } |
| 78 | for _, row := range rows { |
| 79 | if row.ID == b.ID || row.LocalID == s.ID || row.ProviderHome == b.Home || row.ExpectedIdentity.HomeID == b.Expected.HomeID || row.Provider == b.Provider && row.AccountID == b.AccountID { |
| 80 | return provider.ErrIdentity |
| 81 | } |
| 82 | } |
| 83 | kind := "subscription" |
| 84 | if b.Token { |
| 85 | if b.Provider != "claude" { |
| 86 | return provider.ErrIdentity |
| 87 | } |
| 88 | kind = "token" |
| 89 | } |
| 90 | rows = append(rows, AccountBinding{ID: b.ID, LocalID: s.ID, Label: s.Label, PersonID: person, ProviderHome: b.Home, ExpectedIdentity: b.Expected, Provider: b.Provider, Family: provider.SubscriptionFamily(b.Provider), Model: model, AccountID: b.AccountID, Kind: kind}) |
| 91 | out, err := os.CreateTemp(dir, ".accounts-") |
| 92 | if err != nil { |
| 93 | return provider.ErrAccountMaintenance |
| 94 | } |
| 95 | defer os.Remove(out.Name()) |
| 96 | defer out.Close() |
| 97 | if err = json.NewEncoder(out).Encode(rows); err != nil { |
| 98 | return provider.ErrAccountMaintenance |
| 99 | } |
| 100 | if err = out.Sync(); err != nil { |
| 101 | return provider.ErrAccountMaintenance |
| 102 | } |
| 103 | if err = out.Close(); err != nil { |
| 104 | return provider.ErrAccountMaintenance |
| 105 | } |
| 106 | if err = os.Rename(out.Name(), path); err != nil { |
| 107 | return provider.ErrAccountMaintenance |
| 108 | } |
| 109 | return f.Sync() |
| 110 | } |
| 111 | |
| 112 | // RemoveAccountBinding drops one binding (by id) from the bindings file, |
| 113 | // under the same directory lock and atomic replacement as saving. It |
| 114 | // reports whether the binding was there. |
| 115 | func RemoveAccountBinding(ctx context.Context, path, id string) (bool, error) { |
| 116 | if err := ctx.Err(); err != nil { |
| 117 | return false, err |
| 118 | } |
| 119 | dir := filepath.Dir(path) |
| 120 | f, err := os.Open(dir) |
| 121 | if err != nil { |
| 122 | return false, provider.ErrPolicy |
| 123 | } |
| 124 | defer f.Close() |
| 125 | if err = unix.Flock(int(f.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil { |
| 126 | return false, provider.ErrAccountBusy |
| 127 | } |
| 128 | defer unix.Flock(int(f.Fd()), unix.LOCK_UN) |
| 129 | st, err := os.Lstat(path) |
| 130 | if err != nil || !st.Mode().IsRegular() || st.Mode().Perm() != 0600 || st.Size() > 1<<20 { |
| 131 | return false, provider.ErrPolicy |
| 132 | } |
| 133 | in, err := os.Open(path) |
| 134 | if err != nil { |
| 135 | return false, provider.ErrPolicy |
| 136 | } |
| 137 | var rows []AccountBinding |
| 138 | err = runnerproto.DecodeLimit(io.LimitReader(in, 1<<20), &rows, 1<<20) |
| 139 | in.Close() |
| 140 | if err != nil { |
| 141 | return false, provider.ErrIdentity |
| 142 | } |
| 143 | keep := rows[:0] |
| 144 | found := false |
| 145 | for _, row := range rows { |
| 146 | if row.ID == id { |
| 147 | found = true |
| 148 | continue |
| 149 | } |
| 150 | keep = append(keep, row) |
| 151 | } |
| 152 | if !found { |
| 153 | return false, nil |
| 154 | } |
| 155 | out, err := os.CreateTemp(dir, ".accounts-") |
| 156 | if err != nil { |
| 157 | return false, provider.ErrAccountMaintenance |
| 158 | } |
| 159 | defer os.Remove(out.Name()) |
| 160 | defer out.Close() |
| 161 | if err = json.NewEncoder(out).Encode(keep); err == nil { |
| 162 | err = out.Sync() |
| 163 | } |
| 164 | if err == nil { |
| 165 | err = out.Chmod(0600) |
| 166 | } |
| 167 | if err == nil { |
| 168 | err = out.Close() |
| 169 | } |
| 170 | if err == nil { |
| 171 | err = os.Rename(out.Name(), path) |
| 172 | } |
| 173 | if err != nil { |
| 174 | return false, provider.ErrAccountMaintenance |
| 175 | } |
| 176 | return true, f.Sync() |
| 177 | } |
| 178 | |