package forge import ( "context" "os" "path/filepath" "strings" "testing" ) func TestForgeCredentialsSplitAndLegacy(t *testing.T) { for _, tc := range []struct { name, entry string git bool }{ {"split", `{"api":"api-canary","git":"git-canary"}`, true}, {"legacy", `"api-canary"`, false}, {"api only", `{"api":"api-canary"}`, false}, } { t.Run(tc.name, func(t *testing.T) { path := filepath.Join(t.TempDir(), "amadan.json") raw := `{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/repo":` + tc.entry + `}}` must(t, os.WriteFile(path, []byte(raw), 0600)) f := FileCredentials{Path: path} c, err := f.Credential(context.Background(), "amadan.net/ns/repo") if err != nil || c.Bearer != "api-canary" { t.Fatal("API credential unavailable", err) } remote, err := f.Resolve(context.Background(), "amadan.net/ns/repo") if tc.git && (err != nil || c.GitPassword != "git-canary" || remote.GitPassword != "git-canary") { t.Fatal("git credential unavailable", err) } if !tc.git && (err == nil || !strings.Contains(err.Error(), "git password")) { t.Fatal("missing PAT needs a clear configuration error", err) } if _, err := f.Credential(context.Background(), "amadan.net/ns/other"); err == nil { t.Fatal("credential crossed repository boundary") } }) } } func TestForgeCredentialFileProtection(t *testing.T) { path := filepath.Join(t.TempDir(), "amadan.json") raw := `{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/repo":{"api":"api-canary","git":"git-canary"}}}` must(t, os.WriteFile(path, []byte(raw), 0600)) must(t, os.Chmod(path, 0644)) if _, err := (FileCredentials{Path: path}).Credential(context.Background(), "amadan.net/ns/repo"); err == nil { t.Fatal("public credential file accepted") } must(t, os.Chmod(path, 0600)) alias := filepath.Join(t.TempDir(), "alias") must(t, os.Symlink(path, alias)) if _, err := (FileCredentials{Path: alias}).Credential(context.Background(), "amadan.net/ns/repo"); err == nil { t.Fatal("symlink credential file accepted") } } func TestForgeCredentialInvalidEntries(t *testing.T) { for _, entry := range []string{`null`, `42`, `[]`, `{}`, `{"git":"git-canary"}`, `{"api":"api-canary","git":123}`, `{"api":"api-canary","git":"git-canary","extra":"secret-canary"}`, `{"api":"api-canary\r\nInjected: secret-canary","git":"git-canary"}`, `{"api":"api-canary","git":"git-canary\u0000"}`} { t.Run(entry, func(t *testing.T) { path := filepath.Join(t.TempDir(), "amadan.json") raw := `{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/repo":` + entry + `}}` must(t, os.WriteFile(path, []byte(raw), 0600)) _, err := (FileCredentials{Path: path}).Resolve(context.Background(), "amadan.net/ns/repo") if err == nil { t.Fatal("invalid credential accepted") } for _, secret := range []string{"api-canary", "git-canary", "secret-canary"} { if strings.Contains(err.Error(), secret) { t.Fatal("credential leaked in error") } } }) } } // TestForgeCredentialAgentWide: one {agent, api, git} entry serves any // repository; a per-repository entry overrides it for that repository. func TestForgeCredentialAgentWide(t *testing.T) { path := filepath.Join(t.TempDir(), "amadan.json") raw := `{"agent":"equipo~owner@example.test","api":"api-agent","git":"git-agent","repositories":{"amadan.net/ns/special":{"api":"api-special","git":"git-special"}}}` must(t, os.WriteFile(path, []byte(raw), 0600)) f := FileCredentials{Path: path} ctx := context.Background() c, err := f.Credential(ctx, "amadan.net/woodstar/woodstar") if err != nil || c.Bearer != "api-agent" || c.GitPassword != "git-agent" || c.Remote != "https://amadan.net/woodstar/woodstar" || c.Agent != "equipo~owner@example.test" { t.Fatalf("agent-wide %+v %v", c, err) } if c, err = f.Credential(ctx, "amadan.net/ns/special"); err != nil || c.GitPassword != "git-special" { t.Fatalf("override %+v %v", c, err) } if c, err = f.Agent(ctx); err != nil || c.Bearer != "api-agent" || c.Remote != "" { t.Fatalf("agent %+v %v", c, err) } for _, bad := range []string{"github.com/x/y", "amadan.net/../x", "amadan.net/ns"} { if _, err := f.Credential(ctx, bad); err == nil { t.Fatalf("agent credential for %q", bad) } } names, err := f.RepositoryNames() if err != nil || len(names) != 1 { t.Fatalf("names %v %v", names, err) } // A file of per-repository entries only still has an agent credential. legacy := filepath.Join(t.TempDir(), "legacy.json") must(t, os.WriteFile(legacy, []byte(`{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/a":{"api":"api-a","git":"git-a"}}}`), 0600)) if c, err := (FileCredentials{Path: legacy}).Agent(ctx); err != nil || c.GitPassword != "git-a" { t.Fatalf("legacy agent %+v %v", c, err) } if _, err := (FileCredentials{Path: legacy}).Credential(ctx, "amadan.net/ns/b"); err == nil { t.Fatal("a per-repository file crossed repositories") } } // TestForgeCredentialLegacyAgentRule: in a file without an agent-wide api // the agent credential is the one bearer all usable entries share, never // whichever entry sorts first; entries with different bearers give none. func TestForgeCredentialLegacyAgentRule(t *testing.T) { ctx := context.Background() write := func(raw string) FileCredentials { path := filepath.Join(t.TempDir(), "amadan.json") must(t, os.WriteFile(path, []byte(raw), 0600)) return FileCredentials{Path: path} } shared := write(`{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/a":{"git":"git-broken"},"amadan.net/ns/b":{"api":"api-one","git":"git-one"},"amadan.net/ns/c":"api-one"}}`) if c, err := shared.Agent(ctx); err != nil || c.Bearer != "api-one" || c.GitPassword != "git-one" || c.Remote != "" { t.Fatalf("shared legacy %+v %v", c.Remote, err) } if !shared.Legacy() { t.Fatal("legacy file not reported") } mixed := write(`{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/a":{"api":"api-a","git":"git-a"},"amadan.net/ns/b":{"api":"api-b","git":"git-b"}}}`) if c, err := mixed.Agent(ctx); err == nil { t.Fatalf("ambiguous legacy file chose %q", c.Bearer) } wide := write(`{"agent":"equipo~owner@example.test","api":"api-agent","git":"git-agent","repositories":{"amadan.net/ns/a":{"api":"api-a","git":"git-a"}}}`) if c, err := wide.Agent(ctx); err != nil || c.Bearer != "api-agent" || wide.Legacy() { t.Fatalf("agent-wide %+v %v", c.Remote, err) } }