package runnersetup import ( "context" "encoding/json" "io" "os" "path/filepath" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" "golang.org/x/sys/unix" ) type AccountBinding struct { Label string `json:"label,omitempty"` PersonID string `json:"person_id,omitempty"` ProviderHome string `json:"provider_home,omitempty"` ExpectedIdentity provider.Identity `json:"expected_identity,omitempty"` ID string `json:"id"` LocalID string `json:"local_id"` Provider string `json:"provider"` Family string `json:"family"` Model string `json:"model"` AccountID string `json:"account_id"` CredentialHome string `json:"credential_home"` Kind string `json:"kind"` } // SaveAccountBinding appends a verified account atomically and refuses any // existing account identity or home. func SaveAccountBinding(ctx context.Context, path, person, model string, s runnerproto.AccountSetup, b provider.Binding) error { if err := ctx.Err(); err != nil { return err } 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) { return provider.ErrIdentity } dir := filepath.Dir(path) real, err := filepath.EvalSymlinks(dir) if err != nil || real != dir { return provider.ErrPolicy } f, err := os.Open(dir) if err != nil { return provider.ErrPolicy } defer f.Close() st, err := f.Stat() if err != nil || st.Mode().Perm() != 0700 { return provider.ErrPolicy } if err = unix.Flock(int(f.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil { return provider.ErrAccountBusy } defer unix.Flock(int(f.Fd()), unix.LOCK_UN) var rows []AccountBinding st, err = os.Lstat(path) if err == nil { if !st.Mode().IsRegular() || st.Mode().Perm() != 0600 || st.Size() > 1<<20 { return provider.ErrPolicy } in, err := os.Open(path) if err != nil { return provider.ErrPolicy } err = runnerproto.DecodeLimit(io.LimitReader(in, 1<<20), &rows, 1<<20) in.Close() if err != nil { return provider.ErrIdentity } } else if !os.IsNotExist(err) { return provider.ErrPolicy } if len(rows) >= 256 { return provider.ErrIdentity } for _, row := range rows { 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 { return provider.ErrIdentity } } kind := "subscription" if b.Token { if b.Provider != "claude" { return provider.ErrIdentity } kind = "token" } 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}) out, err := os.CreateTemp(dir, ".accounts-") if err != nil { return provider.ErrAccountMaintenance } defer os.Remove(out.Name()) defer out.Close() if err = json.NewEncoder(out).Encode(rows); err != nil { return provider.ErrAccountMaintenance } if err = out.Sync(); err != nil { return provider.ErrAccountMaintenance } if err = out.Close(); err != nil { return provider.ErrAccountMaintenance } if err = os.Rename(out.Name(), path); err != nil { return provider.ErrAccountMaintenance } return f.Sync() } // RemoveAccountBinding drops one binding (by id) from the bindings file, // under the same directory lock and atomic replacement as saving. It // reports whether the binding was there. func RemoveAccountBinding(ctx context.Context, path, id string) (bool, error) { if err := ctx.Err(); err != nil { return false, err } dir := filepath.Dir(path) f, err := os.Open(dir) if err != nil { return false, provider.ErrPolicy } defer f.Close() if err = unix.Flock(int(f.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil { return false, provider.ErrAccountBusy } defer unix.Flock(int(f.Fd()), unix.LOCK_UN) st, err := os.Lstat(path) if err != nil || !st.Mode().IsRegular() || st.Mode().Perm() != 0600 || st.Size() > 1<<20 { return false, provider.ErrPolicy } in, err := os.Open(path) if err != nil { return false, provider.ErrPolicy } var rows []AccountBinding err = runnerproto.DecodeLimit(io.LimitReader(in, 1<<20), &rows, 1<<20) in.Close() if err != nil { return false, provider.ErrIdentity } keep := rows[:0] found := false for _, row := range rows { if row.ID == id { found = true continue } keep = append(keep, row) } if !found { return false, nil } out, err := os.CreateTemp(dir, ".accounts-") if err != nil { return false, provider.ErrAccountMaintenance } defer os.Remove(out.Name()) defer out.Close() if err = json.NewEncoder(out).Encode(keep); err == nil { err = out.Sync() } if err == nil { err = out.Chmod(0600) } if err == nil { err = out.Close() } if err == nil { err = os.Rename(out.Name(), path) } if err != nil { return false, provider.ErrAccountMaintenance } return true, f.Sync() }