| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire/forgekey" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "strings" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 12 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 13 | ) |
| 14 | |
| 15 | // FileCredentials reads the runner's forge credential file: one agent |
| 16 | // entry ({agent, api, git}) serving every repository the hub says the |
| 17 | // agent can reach, and optional per-repository entries that override it. |
| 18 | type FileCredentials struct{ Path string } |
| 19 | type forgeCredentialFile struct { |
| 20 | Agent string `json:"agent"` |
| 21 | API string `json:"api"` |
| 22 | Git string `json:"git"` |
| 23 | Repositories map[string]json.RawMessage `json:"repositories"` |
| 24 | } |
| 25 | |
| 26 | // Agent is the agent-wide credential: the file's own api and git. In a |
| 27 | // legacy file, with per-repository entries only, one agent account serves |
| 28 | // them all, so it is the one bearer every usable entry shares (with the |
| 29 | // one git password they give); entries that disagree name no agent |
| 30 | // credential, never whichever sorts first. A fallback write with it still |
| 31 | // needs the hub's list, fetched with that bearer, to name the repository. |
| 32 | func (f FileCredentials) Agent(ctx context.Context) (Credential, error) { |
| 33 | var file forgeCredentialFile |
| 34 | if err := readCredentialFile(f.Path, &file); err != nil { |
| 35 | return Credential{}, err |
| 36 | } |
| 37 | if file.API != "" { |
| 38 | return agentCredential(file, "", file.API, file.Git) |
| 39 | } |
| 40 | names, err := repositoryNames(file) |
| 41 | if err != nil || len(names) == 0 { |
| 42 | return Credential{}, provider.ErrIdentity |
| 43 | } |
| 44 | var out Credential |
| 45 | for _, name := range names { |
| 46 | c, err := entryCredential(file, name) |
| 47 | switch { |
| 48 | case err != nil: |
| 49 | case out.Bearer == "": |
| 50 | out = c |
| 51 | case c.Bearer != out.Bearer: |
| 52 | return Credential{}, provider.ErrIdentity |
| 53 | case c.GitPassword != "" && out.GitPassword == "": |
| 54 | out.GitPassword = c.GitPassword |
| 55 | case c.GitPassword != "" && c.GitPassword != out.GitPassword: |
| 56 | return Credential{}, provider.ErrIdentity |
| 57 | } |
| 58 | } |
| 59 | if out.Bearer == "" { |
| 60 | return Credential{}, provider.ErrIdentity |
| 61 | } |
| 62 | out.Remote = "" |
| 63 | return out, nil |
| 64 | } |
| 65 | |
| 66 | // Legacy reports a file with per-repository entries and no agent-wide |
| 67 | // api: its agent credential comes from Agent's legacy rule. |
| 68 | func (f FileCredentials) Legacy() bool { |
| 69 | var file forgeCredentialFile |
| 70 | return readCredentialFile(f.Path, &file) == nil && file.API == "" && len(file.Repositories) > 0 |
| 71 | } |
| 72 | |
| 73 | func agentCredential(file forgeCredentialFile, repo, api, git string) (Credential, error) { |
| 74 | if strings.ContainsAny(repo+api+git+file.Agent, "\r\n\x00") || api == "" || file.Agent == "" { |
| 75 | return Credential{}, provider.ErrIdentity |
| 76 | } |
| 77 | remote := "" |
| 78 | if repo != "" { |
| 79 | remote = "https://" + repo |
| 80 | } |
| 81 | return Credential{Remote: remote, Agent: file.Agent, Bearer: api, GitPassword: git}, nil |
| 82 | } |
| 83 | |
| 84 | func (f FileCredentials) Credential(_ context.Context, repo string) (Credential, error) { |
| 85 | var file forgeCredentialFile |
| 86 | if err := readCredentialFile(f.Path, &file); err != nil { |
| 87 | return Credential{}, err |
| 88 | } |
| 89 | return entryCredential(file, repo) |
| 90 | } |
| 91 | |
| 92 | // entryCredential is repo's credential in a read file: its own entry, or |
| 93 | // the agent-wide api when the file has one. |
| 94 | func entryCredential(file forgeCredentialFile, repo string) (Credential, error) { |
| 95 | var entry struct { |
| 96 | API string `json:"api"` |
| 97 | Git string `json:"git"` |
| 98 | } |
| 99 | raw, ok := file.Repositories[repo] |
| 100 | if !ok && file.API != "" { |
| 101 | // The agent-wide entry: the hub decides what the agent reaches. |
| 102 | if !validRepository(repo) { |
| 103 | return Credential{}, provider.ErrIdentity |
| 104 | } |
| 105 | return agentCredential(file, repo, file.API, file.Git) |
| 106 | } |
| 107 | // Legacy entries grant API access only; an agent bearer is not a git password. |
| 108 | if json.Unmarshal(raw, &entry.API) != nil { |
| 109 | if err := runnerproto.DecodeLimit(strings.NewReader(string(raw)), &entry, 1<<20); err != nil { |
| 110 | return Credential{}, provider.ErrIdentity |
| 111 | } |
| 112 | } |
| 113 | token := entry.API |
| 114 | if !forgekey.HasHubPrefix(repo) || strings.ContainsAny(repo+token+entry.Git, "\r\n\x00") || token == "" || file.Agent == "" { |
| 115 | return Credential{}, provider.ErrIdentity |
| 116 | } |
| 117 | return Credential{Remote: "https://" + repo, Agent: file.Agent, Bearer: token, GitPassword: entry.Git}, nil |
| 118 | } |
| 119 | func (f FileCredentials) Resolve(ctx context.Context, repo string) (workspace.Remote, error) { |
| 120 | c, err := f.Credential(ctx, repo) |
| 121 | if err != nil { |
| 122 | return workspace.Remote{}, err |
| 123 | } |
| 124 | if c.GitPassword == "" { |
| 125 | return workspace.Remote{}, workspace.ErrGitPassword |
| 126 | } |
| 127 | return workspace.Remote{URL: c.Remote, GitPassword: c.GitPassword}, nil |
| 128 | } |
| 129 | |
| 130 | func readCredentialFile(path string, out *forgeCredentialFile) error { |
| 131 | info, err := os.Lstat(path) |
| 132 | if err != nil { |
| 133 | return err |
| 134 | } |
| 135 | if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size() > 1<<20 { |
| 136 | return provider.ErrPolicy |
| 137 | } |
| 138 | f, err := os.Open(path) |
| 139 | if err != nil { |
| 140 | return err |
| 141 | } |
| 142 | defer f.Close() |
| 143 | return runnerproto.DecodeLimit(f, out, 1<<20) |
| 144 | } |
| 145 | |