package forge import ( "amadan.net/oficina/equipo/internal/wire/forgekey" "context" "encoding/json" "os" "strings" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/runnerproto" ) // FileCredentials reads the runner's forge credential file: one agent // entry ({agent, api, git}) serving every repository the hub says the // agent can reach, and optional per-repository entries that override it. type FileCredentials struct{ Path string } type forgeCredentialFile struct { Agent string `json:"agent"` API string `json:"api"` Git string `json:"git"` Repositories map[string]json.RawMessage `json:"repositories"` } // Agent is the agent-wide credential: the file's own api and git. In a // legacy file, with per-repository entries only, one agent account serves // them all, so it is the one bearer every usable entry shares (with the // one git password they give); entries that disagree name no agent // credential, never whichever sorts first. A fallback write with it still // needs the hub's list, fetched with that bearer, to name the repository. func (f FileCredentials) Agent(ctx context.Context) (Credential, error) { var file forgeCredentialFile if err := readCredentialFile(f.Path, &file); err != nil { return Credential{}, err } if file.API != "" { return agentCredential(file, "", file.API, file.Git) } names, err := repositoryNames(file) if err != nil || len(names) == 0 { return Credential{}, provider.ErrIdentity } var out Credential for _, name := range names { c, err := entryCredential(file, name) switch { case err != nil: case out.Bearer == "": out = c case c.Bearer != out.Bearer: return Credential{}, provider.ErrIdentity case c.GitPassword != "" && out.GitPassword == "": out.GitPassword = c.GitPassword case c.GitPassword != "" && c.GitPassword != out.GitPassword: return Credential{}, provider.ErrIdentity } } if out.Bearer == "" { return Credential{}, provider.ErrIdentity } out.Remote = "" return out, nil } // Legacy reports a file with per-repository entries and no agent-wide // api: its agent credential comes from Agent's legacy rule. func (f FileCredentials) Legacy() bool { var file forgeCredentialFile return readCredentialFile(f.Path, &file) == nil && file.API == "" && len(file.Repositories) > 0 } func agentCredential(file forgeCredentialFile, repo, api, git string) (Credential, error) { if strings.ContainsAny(repo+api+git+file.Agent, "\r\n\x00") || api == "" || file.Agent == "" { return Credential{}, provider.ErrIdentity } remote := "" if repo != "" { remote = "https://" + repo } return Credential{Remote: remote, Agent: file.Agent, Bearer: api, GitPassword: git}, nil } func (f FileCredentials) Credential(_ context.Context, repo string) (Credential, error) { var file forgeCredentialFile if err := readCredentialFile(f.Path, &file); err != nil { return Credential{}, err } return entryCredential(file, repo) } // entryCredential is repo's credential in a read file: its own entry, or // the agent-wide api when the file has one. func entryCredential(file forgeCredentialFile, repo string) (Credential, error) { var entry struct { API string `json:"api"` Git string `json:"git"` } raw, ok := file.Repositories[repo] if !ok && file.API != "" { // The agent-wide entry: the hub decides what the agent reaches. if !validRepository(repo) { return Credential{}, provider.ErrIdentity } return agentCredential(file, repo, file.API, file.Git) } // Legacy entries grant API access only; an agent bearer is not a git password. if json.Unmarshal(raw, &entry.API) != nil { if err := runnerproto.DecodeLimit(strings.NewReader(string(raw)), &entry, 1<<20); err != nil { return Credential{}, provider.ErrIdentity } } token := entry.API if !forgekey.HasHubPrefix(repo) || strings.ContainsAny(repo+token+entry.Git, "\r\n\x00") || token == "" || file.Agent == "" { return Credential{}, provider.ErrIdentity } return Credential{Remote: "https://" + repo, Agent: file.Agent, Bearer: token, GitPassword: entry.Git}, nil } func (f FileCredentials) Resolve(ctx context.Context, repo string) (workspace.Remote, error) { c, err := f.Credential(ctx, repo) if err != nil { return workspace.Remote{}, err } if c.GitPassword == "" { return workspace.Remote{}, workspace.ErrGitPassword } return workspace.Remote{URL: c.Remote, GitPassword: c.GitPassword}, nil } func readCredentialFile(path string, out *forgeCredentialFile) error { info, err := os.Lstat(path) if err != nil { return err } if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size() > 1<<20 { return provider.ErrPolicy } f, err := os.Open(path) if err != nil { return err } defer f.Close() return runnerproto.DecodeLimit(f, out, 1<<20) }