From 466cd30947810653d153fdc1075bc1b8f3003a3c Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 16:38:50 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20reference=20repos:=20per-hive?= =?UTF-8?q?=20write=20identity=20(bot,=20bot=20only,=20owner)=20(#18516)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit project.remote_write_identity picks whose name a granted write-tier reference repo's issues, PRs and comments carry: - bot (default; a missing key reads as bot): the hive's GitHub App bot on a repo whose owner installed the App themselves (GET /repos/{o}/{r}/installation with the App JWT, cached like org discovery), else the owner's connected account, else read-only. - bot_only: the App bot where installed, read-only elsewhere; the owner token is never read. - owner: the owner's connected account everywhere (the #18197 behaviour). The setting never widens a grant: only entries already at the write tier (hand-written or derived from an accepted request issue, #18139) get a token source, so an App installation shared by many hives is not a grant by itself. The bot token is minted on the installation covering that one repo, scoped to that repo and to the tier's permissions (metadata read; issues write; contents and pull_requests write for prs), cached until near expiry, and injected only by the existing reference-repo proxy path. A transient lookup failure serves the entry read-only for the pass (state unresolved) instead of silently switching identity. write_token_file still wins on hand-written entries. Every proxy refusal is unchanged; a bot login never names a fork, so bot PR branches go to the repo itself under the default-branch guard. The kick names the bot flow (no fork) beside the owner fork flow, the request-issue read-only notice names no cure under bot_only, and the own-item follow-up poller keeps to owner-identity repos. Refs #18516 Signed-off-by: Andy Anderson --- .../added-18516-remote-write-identity.md | 1 + src/cmd/hive/reference_repos.go | 72 +++- src/cmd/hive/reference_repos_identity_test.go | 248 +++++++++++ src/cmd/hive/remote_items.go | 8 +- src/crux/proxy/reference_repos.go | 6 +- .../proxy/reference_repos_identity_test.go | 113 +++++ src/pkg/config/config.go | 8 + src/pkg/config/reference_repos.go | 15 + src/pkg/config/remote_write_identity.go | 84 ++++ src/pkg/config/remote_write_identity_test.go | 35 ++ src/pkg/config/validate.go | 3 + .../dashboard/settings_parity_allowlist.txt | 1 + src/pkg/github/reference_repo_identity.go | 349 ++++++++++++++++ .../github/reference_repo_identity_test.go | 389 ++++++++++++++++++ src/pkg/github/reference_repo_request.go | 21 +- src/pkg/github/reference_repo_token.go | 4 + src/pkg/scheduler/kickmessage.go | 44 +- .../reference_repo_identity_kick_test.go | 42 ++ 18 files changed, 1420 insertions(+), 23 deletions(-) create mode 100644 changelog.d/added-18516-remote-write-identity.md create mode 100644 src/cmd/hive/reference_repos_identity_test.go create mode 100644 src/crux/proxy/reference_repos_identity_test.go create mode 100644 src/pkg/config/remote_write_identity.go create mode 100644 src/pkg/config/remote_write_identity_test.go create mode 100644 src/pkg/github/reference_repo_identity.go create mode 100644 src/pkg/github/reference_repo_identity_test.go create mode 100644 src/pkg/scheduler/reference_repo_identity_kick_test.go diff --git a/changelog.d/added-18516-remote-write-identity.md b/changelog.d/added-18516-remote-write-identity.md new file mode 100644 index 0000000000..0e3a20fbd5 --- /dev/null +++ b/changelog.d/added-18516-remote-write-identity.md @@ -0,0 +1 @@ +- Reference repos: new `project.remote_write_identity` (`bot` default, `bot_only`, `owner`) picks whether a hive's writes on a granted remote repo appear as its GitHub App bot (only where the repo's owner installed the App) or as the hive owner's connected account; without the App, `bot` falls back to the owner and `bot_only` stays read-only (#18516). diff --git a/src/cmd/hive/reference_repos.go b/src/cmd/hive/reference_repos.go index 3891014389..8b7f604730 100644 --- a/src/cmd/hive/reference_repos.go +++ b/src/cmd/hive/reference_repos.go @@ -40,6 +40,29 @@ type referenceRepoDeps struct { // the store) before a pass resolves against it; nil skips. refreshOwner func(ctx context.Context) whoAmI func(ctx context.Context, token string) (string, error) + // writeIdentity reads project.remote_write_identity as of now; nil reads + // as the default (bot). + writeIdentity func() string + // app is the hive's GitHub App seam for the bot identity, read per call + // because the AppAuth is replaced on key rotation; nil, or a zero value, + // means no App path (bot falls back to the owner, bot_only is read-only). + app func() github.ReferenceRepoApp +} + +// appSeam is deps.app with a nil guard. +func (d referenceRepoDeps) appSeam() github.ReferenceRepoApp { + if d.app == nil { + return github.ReferenceRepoApp{} + } + return d.app() +} + +// writeIdentitySetting is deps.writeIdentity with a nil guard. +func (d referenceRepoDeps) writeIdentitySetting() string { + if d.writeIdentity == nil { + return "" + } + return d.writeIdentity() } // ownerCredential is deps.owner with a nil guard. @@ -63,35 +86,40 @@ type referenceRepoResolution struct { // buildReferenceRepoPolicies turns the config list into proxy policies. A // read-only entry needs nothing; a hand-written write-mode entry is verified // now so the failure is a boot-time config error naming the repo. A derived -// entry (hivecommons/hive#18158) is resolved instead: write tier when the -// owner identity is usable, read-only otherwise, never an error. +// entry (hivecommons/hive#18158) is resolved instead, never an error. Which +// identity a write-tier entry writes as follows project.remote_write_identity +// (github.ResolveReferenceRepoCredential): the App bot where the App covers +// the repo, the owner's connected account, or read-only. Only entries in refs +// ever get a token source, so an App installation shared by other hives is +// never a grant by itself. func buildReferenceRepoPolicies(ctx context.Context, refs []config.ReferenceRepo, deps referenceRepoDeps, logger *slog.Logger) (referenceRepoResolution, error) { if deps.refreshOwner != nil { deps.refreshOwner(ctx) } owner := deps.ownerCredential() + setting := deps.writeIdentitySetting() + app := deps.appSeam() res := referenceRepoResolution{ policies: make([]proxy.ReferenceRepoPolicy, 0, len(refs)), repos: make([]config.ReferenceRepo, 0, len(refs)), } for _, rr := range refs { - login := "" - if rr.Derived() { - rr, login = github.ResolveDerivedReferenceRepo(rr, owner) - } else if !rr.ReadOnly() { - l, err := github.VerifyReferenceRepoWriteCredential(ctx, rr, owner, deps.whoAmI) - if err != nil { - return referenceRepoResolution{}, err - } - login = l - rr.CredentialState = config.ReferenceCredentialWriteTier - rr.CredentialReason = "" - rr.CredentialLogin = login + resolved, cred, err := github.ResolveReferenceRepoCredential(ctx, rr, setting, owner, app, deps.whoAmI) + if err != nil { + return referenceRepoResolution{}, err + } + if cred.LookupErr != nil { + logger.Warn("reference repo: App installation lookup failed, serving read-only this pass", "repo", rr.Repo, "error", cred.LookupErr.Error()) } + rr = resolved pol := proxy.ReferenceRepoPolicy{Repo: rr.Repo, Issues: rr.WritesIssues(), PRs: rr.WritesPRs()} if !rr.ReadOnly() { - pol.Login = login - pol.Token = github.ReferenceRepoTokenSource(rr, deps.ownerToken, logger) + pol.Login = cred.Login + if cred.Identity == config.ReferenceIdentityBot { + pol.Token = github.BotReferenceRepoTokenSource(rr, deps.appSeam, logger) + } else { + pol.Token = github.ReferenceRepoTokenSource(rr, deps.ownerToken, logger) + } } res.policies = append(res.policies, pol) res.repos = append(res.repos, rr) @@ -177,6 +205,18 @@ func (b *boot) newReferenceRepoRuntime() *referenceRepoRuntime { rt.deps.whoAmI = func(ctx context.Context, token string) (string, error) { return github.TokenUserLogin(ctx, token, apiURL) } + rt.deps.writeIdentity = func() string { + if b.cfg == nil { + return "" + } + return b.cfg.Project.RemoteWriteIdentity + } + rt.deps.app = func() github.ReferenceRepoApp { + if b.ghClient == nil { + return github.ReferenceRepoApp{} + } + return b.ghClient.AppAuth().ReferenceRepoApp() + } if store := b.ownerIdentityStore(); store != nil { rt.deps.owner = store.Credential rt.deps.ownerToken = store.Token diff --git a/src/cmd/hive/reference_repos_identity_test.go b/src/cmd/hive/reference_repos_identity_test.go new file mode 100644 index 0000000000..4b12398efe --- /dev/null +++ b/src/cmd/hive/reference_repos_identity_test.go @@ -0,0 +1,248 @@ +package main + +import ( + "context" + "errors" + "fmt" + "sort" + "strings" + "sync" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" +) + +// project.remote_write_identity (hivecommons/hive#18516) at the policy +// builder: which credential the proxy will inject on each reference repo, +// and that the App installation on an org is never a grant by itself. + +const testBotLogin = "hivecommons-hive[bot]" + +// orgWideApp is the hivecommons-hive App installed on every repository of +// the owners in installed, the way the hivecommons org installs it for all +// hives. It records every lookup and every mint. +type orgWideApp struct { + mu sync.Mutex + installed map[string]int64 + minted []string +} + +func (a *orgWideApp) seam() github.ReferenceRepoApp { + return github.ReferenceRepoApp{ + Login: testBotLogin, + Installation: func(_ context.Context, owner, _ string) (int64, error) { + if id, ok := a.installed[owner]; ok { + return id, nil + } + return 0, fmt.Errorf("%w: %s", github.ErrNoInstallationForOrg, owner) + }, + Mint: func(_ context.Context, _ int64, rr config.ReferenceRepo) (string, time.Time, error) { + a.mu.Lock() + defer a.mu.Unlock() + a.minted = append(a.minted, rr.Repo) + return "ghs_bot_" + rr.Name(), time.Now().Add(time.Hour), nil + }, + } +} + +func (a *orgWideApp) mints() []string { + a.mu.Lock() + defer a.mu.Unlock() + out := append([]string(nil), a.minted...) + sort.Strings(out) + return out +} + +func withIdentity(deps referenceRepoDeps, setting string, app *orgWideApp) referenceRepoDeps { + deps.writeIdentity = func() string { return setting } + if app != nil { + deps.app = app.seam + } + return deps +} + +// The pilot: a hive granted hivecommons/docs, hivecommons/hivecommons.github.io +// and hivecommons/hive by cncf/techdocs#395, the App installed on hivecommons, +// the owner never connected. Under the default those post as the App bot. +func TestReferenceRepoIdentity_PilotPostsAsBotWithoutOwner(t *testing.T) { + app := &orgWideApp{installed: map[string]int64{"hivecommons": 42}} + var refs []config.ReferenceRepo + for _, repo := range []string{"hivecommons/docs", "hivecommons/hivecommons.github.io", "hivecommons/hive"} { + refs = append(refs, config.ReferenceRepo{Repo: repo, Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}) + } + for _, setting := range []string{"", config.RemoteWriteIdentityBot, config.RemoteWriteIdentityBotOnly} { + res, err := buildReferenceRepoPolicies(context.Background(), refs, withIdentity(testReferenceRepoDepsDisconnected(), setting, app), testLogger()) + if err != nil { + t.Fatal(err) + } + for i, p := range res.policies { + rr := res.repos[i] + if rr.CredentialState != config.ReferenceCredentialWriteTier || rr.CredentialIdentity != config.ReferenceIdentityBot || rr.CredentialLogin != testBotLogin { + t.Fatalf("setting %q: %s must write as the bot: %+v", setting, rr.Repo, rr) + } + if !strings.Contains(rr.CredentialNote, "installed on hivecommons") { + t.Fatalf("setting %q: note must say why: %q", setting, rr.CredentialNote) + } + if p.Login != testBotLogin || p.Token == nil { + t.Fatalf("setting %q: policy %+v", setting, p) + } + tok, ok := p.Token() + if !ok || tok != "ghs_bot_"+rr.Name() { + t.Fatalf("setting %q: %s injects %q, want its own bot token", setting, rr.Repo, tok) + } + } + } +} + +// The grant invariant: the App is installed on all of hivecommons, but only +// the repos this hive is granted at the write tier ever get a bot token, and +// each token is minted for its own repo only. +func TestReferenceRepoIdentity_NoGrantNoBotToken(t *testing.T) { + app := &orgWideApp{installed: map[string]int64{"hivecommons": 42}} + refs := []config.ReferenceRepo{ + {Repo: "hivecommons/docs", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}, + {Repo: "hivecommons/hive"}, // reference, read-only: studied, never written + } + deps := withIdentity(testReferenceRepoDeps(), config.RemoteWriteIdentityBot, app) + res, err := buildReferenceRepoPolicies(context.Background(), refs, deps, testLogger()) + if err != nil { + t.Fatal(err) + } + granted := map[string]bool{} + for _, p := range res.policies { + if p.Token == nil { + continue + } + granted[p.Repo] = true + if _, ok := p.Token(); !ok { + t.Fatalf("%s: granted repo got no token", p.Repo) + } + } + if len(granted) != 1 || !granted["hivecommons/docs"] { + t.Fatalf("only the granted repo may carry a credential, got %v", granted) + } + for _, p := range res.policies { + if p.Repo == "hivecommons/hive" && (p.Token != nil || p.Issues || p.PRs || p.Login != "") { + t.Fatalf("read-only entry on an App-installed org must carry nothing: %+v", p) + } + } + if got := app.mints(); len(got) != 1 || got[0] != "hivecommons/docs" { + t.Fatalf("bot tokens minted for %v, want only hivecommons/docs", got) + } + // hivecommons/website is on the same App-installed org but not granted: + // there is no policy for it, so nothing can ever mint for it. + for _, p := range res.policies { + if strings.EqualFold(p.Repo, "hivecommons/website") { + t.Fatalf("an ungranted repo must have no policy: %+v", p) + } + } +} + +// bot_only with the owner connected and the App missing: read-only, and the +// proxy has no credential to inject, in particular not the owner's. +func TestReferenceRepoIdentity_BotOnlyNeverInjectsOwner(t *testing.T) { + app := &orgWideApp{installed: map[string]int64{"hivecommons": 42}} + ownerTokenReads := 0 + deps := withIdentity(testReferenceRepoDeps(), config.RemoteWriteIdentityBotOnly, app) + deps.ownerToken = func() (string, bool) { + ownerTokenReads++ + return "gho_owner", true + } + refs := []config.ReferenceRepo{ + {Repo: "hivecommons/docs", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}, + {Repo: "acme/site", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}, + } + res, err := buildReferenceRepoPolicies(context.Background(), refs, deps, testLogger()) + if err != nil { + t.Fatal(err) + } + for i, p := range res.policies { + rr := res.repos[i] + switch rr.Repo { + case "hivecommons/docs": + if tok, ok := p.Token(); !ok || tok != "ghs_bot_docs" { + t.Fatalf("App-installed repo must inject the bot token, got %q", tok) + } + case "acme/site": + if p.Token != nil || p.Issues || p.PRs || p.Login != "" { + t.Fatalf("bot_only without the App must be read-only with no credential: %+v", p) + } + if !rr.DerivedReadOnly() || !strings.HasPrefix(rr.CredentialReason, github.ReferenceReadOnlyBotOnlyPrefix) { + t.Fatalf("read-only reason must say bot only: %+v", rr) + } + } + } + if ownerTokenReads != 0 { + t.Fatalf("bot_only must never read the owner token, read %d times", ownerTokenReads) + } +} + +// Mixed set under the default: the bot where the App is installed, the owner +// elsewhere, each policy injecting its own credential; owner keeps today's +// behaviour everywhere. +func TestReferenceRepoIdentity_PerRepoCredential(t *testing.T) { + app := &orgWideApp{installed: map[string]int64{"hivecommons": 42}} + refs := []config.ReferenceRepo{ + {Repo: "hivecommons/docs", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}, + {Repo: "acme/site", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}, + } + cases := []struct { + setting string + wantTok map[string]string + wantUser map[string]string + }{ + {config.RemoteWriteIdentityBot, map[string]string{"hivecommons/docs": "ghs_bot_docs", "acme/site": "gho_owner"}, map[string]string{"hivecommons/docs": testBotLogin, "acme/site": testOwnerLogin}}, + {config.RemoteWriteIdentityOwner, map[string]string{"hivecommons/docs": "gho_owner", "acme/site": "gho_owner"}, map[string]string{"hivecommons/docs": testOwnerLogin, "acme/site": testOwnerLogin}}, + } + for _, tc := range cases { + res, err := buildReferenceRepoPolicies(context.Background(), refs, withIdentity(testReferenceRepoDeps(), tc.setting, app), testLogger()) + if err != nil { + t.Fatal(err) + } + for i, p := range res.policies { + tok, ok := p.Token() + if !ok || tok != tc.wantTok[p.Repo] || p.Login != tc.wantUser[p.Repo] || res.repos[i].CredentialLogin != tc.wantUser[p.Repo] { + t.Fatalf("setting %q: %s injects %q as %q, want %q as %q", tc.setting, p.Repo, tok, p.Login, tc.wantTok[p.Repo], tc.wantUser[p.Repo]) + } + } + } + // The own-item follow-up reads under the owner token, so it watches only + // the owner-identity repos. + res, err := buildReferenceRepoPolicies(context.Background(), refs, withIdentity(testReferenceRepoDeps(), config.RemoteWriteIdentityBot, app), testLogger()) + if err != nil { + t.Fatal(err) + } + repos, login := remoteItemRepos(res.repos) + if len(repos) != 1 || repos[0] != "acme/site" || login != testOwnerLogin { + t.Fatalf("follow-up must watch only owner-identity repos, got %v as %q", repos, login) + } +} + +// A blip on the App lookup keeps the entry read-only for the pass rather +// than quietly posting under the owner's name. +func TestReferenceRepoIdentity_TransientLookupStaysReadOnly(t *testing.T) { + deps := withIdentity(testReferenceRepoDeps(), config.RemoteWriteIdentityBot, nil) + deps.app = func() github.ReferenceRepoApp { + return github.ReferenceRepoApp{ + Login: testBotLogin, + Installation: func(context.Context, string, string) (int64, error) { return 0, errors.New("502 bad gateway") }, + Mint: func(context.Context, int64, config.ReferenceRepo) (string, time.Time, error) { + t.Fatal("must not mint on an unresolved lookup") + return "", time.Time{}, nil + }, + } + } + refs := []config.ReferenceRepo{{Repo: "hivecommons/docs", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}} + res, err := buildReferenceRepoPolicies(context.Background(), refs, deps, testLogger()) + if err != nil { + t.Fatal(err) + } + if p := res.policies[0]; p.Token != nil || p.Issues || p.PRs { + t.Fatalf("unresolved entry must carry no credential: %+v", p) + } + if rr := res.repos[0]; rr.CredentialState != config.ReferenceCredentialUnresolved || rr.DerivedReadOnly() { + t.Fatalf("unresolved entry state = %+v", rr) + } +} diff --git a/src/cmd/hive/remote_items.go b/src/cmd/hive/remote_items.go index e5d3e77876..f501d0bdc9 100644 --- a/src/cmd/hive/remote_items.go +++ b/src/cmd/hive/remote_items.go @@ -66,12 +66,18 @@ func resolveMarkerAgent(cfg *config.Config, name string) string { } // remoteItemRepos returns the reference repos the poller watches: write tier -// with a resolved credential login, which is the owner's login. +// with a resolved credential login, which is the owner's login. Entries that +// write as the App bot (project.remote_write_identity) are left out: the +// poller reads under the owner token and lists creator=, so it would +// never find the bot's items there. func remoteItemRepos(refs []config.ReferenceRepo) (repos []string, login string) { for _, rr := range refs { if rr.ReadOnly() || rr.CredentialState != config.ReferenceCredentialWriteTier || rr.CredentialLogin == "" { continue } + if rr.CredentialIdentity == config.ReferenceIdentityBot { + continue + } repos = append(repos, rr.Repo) if login == "" { login = rr.CredentialLogin diff --git a/src/crux/proxy/reference_repos.go b/src/crux/proxy/reference_repos.go index 9e1bfcaacc..c738fda9f8 100644 --- a/src/crux/proxy/reference_repos.go +++ b/src/crux/proxy/reference_repos.go @@ -74,8 +74,12 @@ func (r ReferenceRepoPolicy) target() string { // forkSlug is the slug of Login's fork of Repo ("" when either half is // unknown). GitHub forks keep the upstream name unless renamed; a renamed // fork is not recognised and its pushes are refused like any other repo. +// +// An App bot login ("[bot]", project.remote_write_identity bot) owns no +// repositories, so a bot-identity policy has no fork: its topic branches go +// to Repo itself, under the same default-branch guard. func (r ReferenceRepoPolicy) forkSlug() string { - if r.Login == "" { + if r.Login == "" || strings.HasSuffix(r.Login, "[bot]") { return "" } _, name, ok := strings.Cut(r.Repo, "/") diff --git a/src/crux/proxy/reference_repos_identity_test.go b/src/crux/proxy/reference_repos_identity_test.go new file mode 100644 index 0000000000..56401ec89c --- /dev/null +++ b/src/crux/proxy/reference_repos_identity_test.go @@ -0,0 +1,113 @@ +package proxy + +import ( + "encoding/base64" + "io" + "log/slog" + "strings" + "testing" +) + +// project.remote_write_identity (hivecommons/hive#18516): the proxy injects +// each reference repo's own credential on each call, whichever identity it +// resolved to, and the gate's refusals do not depend on the identity. + +func identityTestProxy(t *testing.T, lookups *[]string) *GitHubProxy { + t.Helper() + p := &GitHubProxy{logger: slog.New(slog.NewTextHandler(io.Discard, nil))} + p.SetReferenceRepos([]ReferenceRepoPolicy{ + // Written as the App bot: the App is installed on hivecommons. + {Repo: "hivecommons/docs", Issues: true, PRs: true, Login: "hivecommons-hive[bot]", Token: func() (string, bool) { return "ghs_bot_docs", true }}, + // Written as the hive owner: no App on acme. + {Repo: "acme/site", Issues: true, PRs: true, Login: "nate-double-u", Token: func() (string, bool) { return "gho_owner", true }}, + // Left read-only (bot_only without the App, or no credential at all). + {Repo: "other/lib"}, + }) + p.referenceLookup = func(url, token string) ([]byte, error) { + *lookups = append(*lookups, url+" "+token) + return []byte(`{"default_branch":"main"}`), nil + } + p.injectGHAuth = true + p.agentTokenSource = func(string) (string, bool) { return "hive-app-token", true } + return p +} + +func TestReferenceRepoIdentity_ProxyInjectsPerRepoCredential(t *testing.T) { + var lookups []string + p := identityTestProxy(t, &lookups) + + for repo, want := range map[string]string{ + "hivecommons/docs": "token ghs_bot_docs", + "acme/site": "token gho_owner", + } { + req := refReq("POST", "/repos/"+repo+"/issues", "", nil) + req.Header.Set("Authorization", "token agent-held") + if reason, refused := p.referenceRepoRefusal(req); refused { + t.Fatalf("%s: opening an issue refused: %s", repo, reason) + } + p.rewriteGitHubAuth(req, "docs-agent") + if got := req.Header.Get("Authorization"); got != want { + t.Fatalf("%s: Authorization = %q, want %q", repo, got, want) + } + } + + // Read-only: refused, and the agent's credential is stripped, nothing + // injected in its place. + ro := refReq("POST", "/repos/other/lib/issues", "", nil) + ro.Header.Set("Authorization", "token agent-held") + if _, refused := p.referenceRepoRefusal(ro); !refused { + t.Fatal("a read-only reference repo write must be refused") + } + p.rewriteGitHubAuth(ro, "docs-agent") + if got := ro.Header.Get("Authorization"); got != "" { + t.Fatalf("read-only reference repo must carry no credential, got %q", got) + } + + // The bot pushes its topic branch to the reference repo itself (it has no + // fork); the default-branch guard still applies and is read under the + // bot's own token. + zero := strings.Repeat("0", 40) + sha := strings.Repeat("a", 40) + topic := pktLine(zero+" "+sha+" refs/heads/hive/assessment\x00caps") + "0000" + "PACK" + if reason, refused := p.referenceRepoRefusal(refReq("POST", "/hivecommons/docs.git/git-receive-pack", "", []byte(topic))); refused { + t.Fatalf("bot topic push refused: %s", reason) + } + if len(lookups) != 1 || lookups[0] != "https://api.github.com/repos/hivecommons/docs ghs_bot_docs" { + t.Fatalf("default branch must be read under the bot token: %v", lookups) + } + def := pktLine(sha+" "+sha+" refs/heads/main") + "0000" + "PACK" + if _, refused := p.referenceRepoRefusal(refReq("POST", "/hivecommons/docs.git/git-receive-pack", "", []byte(def))); !refused { + t.Fatal("a default-branch push must be refused under the bot identity too") + } + adv := refReq("GET", "/hivecommons/docs.git/info/refs", "service=git-receive-pack", nil) + adv.Header.Set("Authorization", "Basic agent-held") + p.rewriteGitHubAuth(adv, "docs-agent") + if want := "Basic " + base64.StdEncoding.EncodeToString([]byte(gitInjectBasicUser+":ghs_bot_docs")); adv.Header.Get("Authorization") != want { + t.Fatalf("bot push advertisement auth = %q", adv.Header.Get("Authorization")) + } + + // Management stays refused whoever the credential belongs to. + for _, repo := range []string{"hivecommons/docs", "acme/site"} { + for _, path := range []string{ + "/repos/" + repo + "/issues/1/labels", + "/repos/" + repo + "/issues/1/assignees", + "/repos/" + repo + "/pulls/1/reviews", + } { + if _, refused := p.referenceRepoRefusal(refReq("POST", path, "", nil)); !refused { + t.Fatalf("%s must be refused for %s", path, repo) + } + } + if _, refused := p.referenceRepoRefusal(refReq("PUT", "/repos/"+repo+"/pulls/1/merge", "", nil)); !refused { + t.Fatalf("merge must be refused for %s", repo) + } + if _, refused := p.referenceRepoRefusal(refReq("PATCH", "/repos/"+repo+"/issues/1", "", []byte(`{"state":"closed"}`))); !refused { + t.Fatalf("close must be refused for %s", repo) + } + } + + // The bot login is never read as a fork owner: there is no + // "hivecommons-hive[bot]/docs" fork to push to. + if pol, ok := p.referenceRepoFor("/repos/hivecommons-hive[bot]/docs/git/refs"); ok && pol.Fork != "" { + t.Fatalf("bot login must not name a fork: %+v", pol) + } +} diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index 5b0950dfe5..fe2b2580c8 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -1186,6 +1186,14 @@ type ProjectConfig struct { // them, and writes to them are refused unless opted in. See // reference_repos.go. ReferenceRepos []ReferenceRepo `yaml:"reference_repos,omitempty" json:"reference_repos,omitempty"` + // RemoteWriteIdentity picks which GitHub identity the hive writes to its + // write-tier reference repos as (hivecommons/hive#18196 follow-up): + // "bot" (the default when empty) posts as the hive's GitHub App on a + // granted repo whose owner installed that App themselves and as the hive + // owner's connected account elsewhere; "bot_only" posts as the App where + // it is installed and is read-only elsewhere; "owner" always posts as the + // hive owner's connected account. See remote_write_identity.go. + RemoteWriteIdentity string `yaml:"remote_write_identity,omitempty" json:"remote_write_identity,omitempty"` // RepoPolicies stores optional per-repository policy overrides keyed by // repo name. project.repos remains the watched-repo identity list; this // sidecar list lets existing string-list configs keep round-tripping. diff --git a/src/pkg/config/reference_repos.go b/src/pkg/config/reference_repos.go index 65eeeebbc2..1123d43014 100644 --- a/src/pkg/config/reference_repos.go +++ b/src/pkg/config/reference_repos.go @@ -63,6 +63,15 @@ type ReferenceRepo struct { // login, or the write_token_file's user); empty when CredentialState is // not write-tier. The kick names it so agents know whose fork to push to. CredentialLogin string `yaml:"-" json:"credential_login,omitempty"` + // CredentialIdentity is which identity CredentialLogin is + // (ReferenceIdentityBot, ReferenceIdentityOwner or + // ReferenceIdentityTokenFile); empty when CredentialState is not + // write-tier. project.remote_write_identity decides between bot and owner. + CredentialIdentity string `yaml:"-" json:"credential_identity,omitempty"` + // CredentialNote is the one-line why behind CredentialIdentity (or behind + // a read-only state), e.g. "the hive's GitHub App is installed on + // hivecommons", in words the Settings page shows as is. + CredentialNote string `yaml:"-" json:"credential_note,omitempty"` } // Credential states of a write-tier reference repo. @@ -73,6 +82,12 @@ const ( // ReferenceCredentialReadOnly: no credential, the derived entry was // downgraded to read-only in memory and the request issue told why. ReferenceCredentialReadOnly = "read-only" + // ReferenceCredentialUnresolved: project.remote_write_identity is "bot" + // and the App installation lookup on the repo's owner failed for a + // transient reason (network, 5xx). The entry is served read-only for + // this pass without telling the request issue, and is retried on the + // next one, rather than silently posting under a different identity. + ReferenceCredentialUnresolved = "unresolved" ) // Derived reports whether the entry came from an accepted request issue diff --git a/src/pkg/config/remote_write_identity.go b/src/pkg/config/remote_write_identity.go new file mode 100644 index 0000000000..e7e1707212 --- /dev/null +++ b/src/pkg/config/remote_write_identity.go @@ -0,0 +1,84 @@ +package config + +import ( + "fmt" + "strings" +) + +// project.remote_write_identity: which GitHub identity a hive's writes to a +// write-tier reference repo appear under. +// +// A reference repo (#18015) is one the hive does not manage; its write tier +// comes only from a grant (a hand-written entry, or one derived from an +// accepted request issue, #18139). This setting never widens that grant. It +// only picks whose name is on the issues, PRs and comments the grant allows: +// +// - RemoteWriteIdentityBot (the default, and what an empty value means): +// the hive's GitHub App, as "[bot]", on a granted repo whose owner +// has installed that App on their own; the hive never asks anyone to +// install it. Where the App is not installed, the hive owner's connected +// account is used instead, and with no connected account the entry stays +// read-only, exactly as before this setting existed. +// - RemoteWriteIdentityBotOnly: the App bot where it is installed, and +// read-only everywhere else. The owner's connected account is never +// used, even when it is connected. +// - RemoteWriteIdentityOwner: the hive owner's connected account on every +// write-tier reference repo (the behaviour #18197 introduced). +// +// A hand-written entry's write_token_file always wins over all three. +const ( + RemoteWriteIdentityBot = "bot" + RemoteWriteIdentityBotOnly = "bot_only" + RemoteWriteIdentityOwner = "owner" +) + +// RemoteWriteIdentities is the vocabulary, in the order the Settings control +// offers it. +var RemoteWriteIdentities = []string{RemoteWriteIdentityBot, RemoteWriteIdentityBotOnly, RemoteWriteIdentityOwner} + +// NormalizeRemoteWriteIdentity folds case and whitespace, accepts "-" for +// "_", and maps the empty value to the default. An unknown value is returned +// folded so the validator can name it. +func NormalizeRemoteWriteIdentity(v string) string { + v = strings.ReplaceAll(strings.ToLower(strings.TrimSpace(v)), "-", "_") + if v == "" { + return RemoteWriteIdentityBot + } + return v +} + +// ValidateRemoteWriteIdentity accepts "" and the RemoteWriteIdentities. +func ValidateRemoteWriteIdentity(v string) error { + switch NormalizeRemoteWriteIdentity(v) { + case RemoteWriteIdentityBot, RemoteWriteIdentityBotOnly, RemoteWriteIdentityOwner: + return nil + } + return fmt.Errorf("project.remote_write_identity: %q is not one of %q, %q, %q", strings.TrimSpace(v), RemoteWriteIdentityBot, RemoteWriteIdentityBotOnly, RemoteWriteIdentityOwner) +} + +// EffectiveRemoteWriteIdentity is the setting with the default applied. +// Loading validates the value; an unknown one that slipped past (a hand +// edit picked up mid-reload) reads as bot_only, the identity that can never +// put the owner's name on anything. +func (p ProjectConfig) EffectiveRemoteWriteIdentity() string { + switch v := NormalizeRemoteWriteIdentity(p.RemoteWriteIdentity); v { + case RemoteWriteIdentityBot, RemoteWriteIdentityBotOnly, RemoteWriteIdentityOwner: + return v + } + return RemoteWriteIdentityBotOnly +} + +// Credential identities a write-tier reference repo resolves to +// (ReferenceRepo.CredentialIdentity). +const ( + // ReferenceIdentityBot: writes appear as the hive's GitHub App bot, + // through a token minted on the App's installation covering the repo, + // scoped to that one repo. + ReferenceIdentityBot = "bot" + // ReferenceIdentityOwner: writes appear as the hive owner's connected + // GitHub account. + ReferenceIdentityOwner = "owner" + // ReferenceIdentityTokenFile: writes appear as the user of a hand-written + // entry's write_token_file. + ReferenceIdentityTokenFile = "token_file" +) diff --git a/src/pkg/config/remote_write_identity_test.go b/src/pkg/config/remote_write_identity_test.go new file mode 100644 index 0000000000..639c622a5a --- /dev/null +++ b/src/pkg/config/remote_write_identity_test.go @@ -0,0 +1,35 @@ +package config + +import "testing" + +func TestRemoteWriteIdentity(t *testing.T) { + cases := []struct { + in string + valid bool + effective string + }{ + {"", true, RemoteWriteIdentityBot}, + {"bot", true, RemoteWriteIdentityBot}, + {" Bot ", true, RemoteWriteIdentityBot}, + {"bot_only", true, RemoteWriteIdentityBotOnly}, + {"bot-only", true, RemoteWriteIdentityBotOnly}, + {"owner", true, RemoteWriteIdentityOwner}, + {"OWNER", true, RemoteWriteIdentityOwner}, + // An unknown value is refused at load and, if it ever slipped + // through, reads as the identity that never uses the owner's name. + {"me", false, RemoteWriteIdentityBotOnly}, + {"app", false, RemoteWriteIdentityBotOnly}, + } + for _, tc := range cases { + err := ValidateRemoteWriteIdentity(tc.in) + if (err == nil) != tc.valid { + t.Errorf("ValidateRemoteWriteIdentity(%q) err=%v, want valid=%v", tc.in, err, tc.valid) + } + if got := (ProjectConfig{RemoteWriteIdentity: tc.in}).EffectiveRemoteWriteIdentity(); got != tc.effective { + t.Errorf("EffectiveRemoteWriteIdentity(%q) = %q, want %q", tc.in, got, tc.effective) + } + } + if len(RemoteWriteIdentities) != 3 || RemoteWriteIdentities[0] != RemoteWriteIdentityBot { + t.Errorf("vocabulary must lead with the default: %v", RemoteWriteIdentities) + } +} diff --git a/src/pkg/config/validate.go b/src/pkg/config/validate.go index 6ecaceddf6..aefea2ce7b 100644 --- a/src/pkg/config/validate.go +++ b/src/pkg/config/validate.go @@ -39,6 +39,9 @@ func (c *Config) ValidateWithOptions(opts ValidateOptions) error { if err := c.validateReferenceRepos(); err != nil { return err } + if err := ValidateRemoteWriteIdentity(c.Project.RemoteWriteIdentity); err != nil { + return err + } if _, err := c.Ioscan.Policy(); err != nil { return err } diff --git a/src/pkg/dashboard/settings_parity_allowlist.txt b/src/pkg/dashboard/settings_parity_allowlist.txt index 148b747fc2..d4a276c85b 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -621,6 +621,7 @@ data.workflows_dir # workflow-model store root; deployment knob, no Settings con project.workflow_bindings[].name # set through the per-repo workflow select on Settings → Repos via PUT /api/workflows/bindings, not a leaf-path control (RFC #17620) project.workflow_bindings[].repo # see project.workflow_bindings[].name project.workflow_bindings[].version # see project.workflow_bindings[].name +project.remote_write_identity # Settings control lands in the follow-up PR for #18516 workflow.label_gc # operator safety knob for model-label garbage collection on rebind; yaml-only, no Settings control planned (RFC #17620) inbox.sources[] # which voice connector types feed the Inbox; defaults to every configured plaud/wisprflow connector, yaml-only (Inbox RFC) inbox.rules[].source # auto-routing rules are edited as whole rows in Inbox → Auto-routing via POST /api/inbox/settings, not leaf-path controls (Inbox RFC) diff --git a/src/pkg/github/reference_repo_identity.go b/src/pkg/github/reference_repo_identity.go new file mode 100644 index 0000000000..624b0d0ca1 --- /dev/null +++ b/src/pkg/github/reference_repo_identity.go @@ -0,0 +1,349 @@ +package github + +import ( + "context" + "errors" + "fmt" + "log/slog" + "strings" + "sync" + "time" + + gh "github.com/google/go-github/v72/github" + + "github.com/hivecommons/hive/pkg/config" +) + +// Which identity a write-tier reference repo writes as +// (project.remote_write_identity; follow-up to hivecommons/hive#18196). +// +// The grant is unchanged: only an entry already at the write tier (a +// hand-written one, or one derived from an accepted request issue, #18139) +// ever gets a credential, and the proxy's reference-repo gate still allows +// only opening issues and PRs, pushing topic branches and commenting on the +// hive's own items. This file only decides whose NAME those writes carry: +// +// - the App bot, when the setting allows it AND the hive's GitHub App has an +// installation that covers that exact repository (the repo's owner +// installed it on their own; nothing here asks anyone to). The token is +// minted on that installation, scoped to that one repository and to the +// permissions the entry's write tier needs, and is never handed to +// anything but the proxy's reference-repo injection for that repository; +// - the hive owner's connected account, when the setting is "owner", or +// "bot" and the App does not cover the repo; +// - nothing (read-only), otherwise. +// +// An App installation shared by many hives (the hivecommons org installs the +// hivecommons-hive App for every repository) is therefore never a grant by +// itself: a hive without an entry for a repo has no policy for it, so no +// token is ever minted for it. + +// referenceTokenRefreshMargin is how long before expiry a minted bot token is +// replaced. +const referenceTokenRefreshMargin = 5 * time.Minute + +// ReferenceRepoApp is the App seam the identity resolution and the bot token +// source use. Production wires it from the spoke's AppAuth +// (AppAuth.ReferenceRepoApp); tests fake it. +type ReferenceRepoApp struct { + // Login is the App bot login ("[bot]") the minted tokens act as. + Login string + // Installation returns the installation of the App covering owner/name, + // or an error wrapping ErrNoInstallationForOrg when there is none. + Installation func(ctx context.Context, owner, name string) (int64, error) + // Mint mints a token on installationID for rr alone. + Mint func(ctx context.Context, installationID int64, rr config.ReferenceRepo) (string, time.Time, error) +} + +// available reports whether the App path can be tried at all. +func (a ReferenceRepoApp) available() bool { + return a.Login != "" && a.Installation != nil && a.Mint != nil +} + +// ReferenceRepoApp returns the production seam over this AppAuth; the zero +// value (App path unavailable) when the hive has no App key or bot login. +func (a *AppAuth) ReferenceRepoApp() ReferenceRepoApp { + if a == nil || !a.HasKey() || a.BotLogin() == "" { + return ReferenceRepoApp{} + } + return ReferenceRepoApp{Login: a.BotLogin(), Installation: a.RepositoryInstallationID, Mint: a.MintReferenceRepoToken} +} + +// referenceRepoPermissions are the installation permissions a write tier +// needs: metadata always, issues:write for issues, contents:write and +// pull_requests:write for prs (a PR needs its topic branch pushed first). +// Nothing wider; the proxy narrows further to the exact write paths. +func referenceRepoPermissions(rr config.ReferenceRepo) *gh.InstallationPermissions { + perms := &gh.InstallationPermissions{Metadata: gh.Ptr("read"), Contents: gh.Ptr("read")} + if rr.WritesIssues() { + perms.Issues = gh.Ptr("write") + } + if rr.WritesPRs() { + perms.Contents = gh.Ptr("write") + perms.PullRequests = gh.Ptr("write") + } + return perms +} + +// RepositoryInstallationID returns the installation of this App that covers +// owner/name (GET /repos/{owner}/{name}/installation, App JWT), so an +// installation limited to selected repositories is honoured. A 404 wraps +// ErrNoInstallationForOrg. Results, negative ones included, are cached for +// InstallationDiscoveryTTL like DiscoverInstallationID's. +func (a *AppAuth) RepositoryInstallationID(ctx context.Context, owner, name string) (int64, error) { + if a == nil || !a.HasKey() { + return 0, fmt.Errorf("%w: this hive has no GitHub App key", ErrNoInstallationForOrg) + } + owner, name = strings.TrimSpace(owner), strings.TrimSpace(name) + if owner == "" || name == "" { + return 0, fmt.Errorf("repository installation lookup: owner and name are required") + } + key := "repo|" + discoveryCacheKey(a.apiURL, a.appID, owner+"/"+name) + discoveryCacheMu.Lock() + if entry, ok := discoveryCache[key]; ok && time.Since(entry.cachedAt) < InstallationDiscoveryTTL { + discoveryCacheMu.Unlock() + return entry.id, entry.err + } + discoveryCacheMu.Unlock() + + id, err := a.repositoryInstallationUncached(ctx, owner, name) + if err == nil || errors.Is(err, ErrNoInstallationForOrg) { + // Transient failures are not cached: the next pass retries them. + discoveryCacheMu.Lock() + discoveryCache[key] = discoveryCacheEntry{id: id, err: err, cachedAt: time.Now()} + discoveryCacheMu.Unlock() + } + return id, err +} + +func (a *AppAuth) repositoryInstallationUncached(ctx context.Context, owner, name string) (int64, error) { + ctx, cancel := context.WithTimeout(ctx, discoveryTimeout) + defer cancel() + jwtToken, err := a.generateJWT() + if err != nil { + return 0, fmt.Errorf("generating JWT: %w", err) + } + inst, resp, err := newJWTClient(jwtToken, a.apiURL).Apps.FindRepositoryInstallation(ctx, owner, name) + if err != nil { + if resp != nil && resp.StatusCode == 404 { + return 0, fmt.Errorf("%w: app %d is not installed on %s/%s", ErrNoInstallationForOrg, a.appID, owner, name) + } + return 0, fmt.Errorf("repository installation lookup for %s/%s: %w", owner, name, err) + } + if inst == nil || inst.GetID() == 0 { + return 0, fmt.Errorf("%w: app %d returned no installation for %s/%s", ErrNoInstallationForOrg, a.appID, owner, name) + } + if acct := inst.GetAccount().GetLogin(); acct != "" && !strings.EqualFold(acct, owner) { + return 0, fmt.Errorf("%w: installation for %s/%s belongs to %q", ErrNoInstallationForOrg, owner, name, acct) + } + return inst.GetID(), nil +} + +// MintReferenceRepoToken mints a token on installationID scoped to rr's one +// repository and to the permissions its write tier needs. A read-only or +// malformed entry is refused before any request leaves. +func (a *AppAuth) MintReferenceRepoToken(ctx context.Context, installationID int64, rr config.ReferenceRepo) (string, time.Time, error) { + if a == nil { + return "", time.Time{}, ErrNoGitHubClient + } + if rr.ReadOnly() { + return "", time.Time{}, fmt.Errorf("%w: %s is read-only", ErrReferenceRepoNoCredential, rr.Repo) + } + if rr.Owner() == "" || rr.Name() == "" || installationID <= 0 { + return "", time.Time{}, fmt.Errorf("%w: %s: no installation to mint on", ErrReferenceRepoNoCredential, rr.Repo) + } + opts := &gh.InstallationTokenOptions{ + Repositories: []string{rr.Name()}, + Permissions: referenceRepoPermissions(rr), + } + return a.mintInstallationTokenWithOptions(ctx, installationID, opts) +} + +// BotReferenceRepoTokenSource is the proxy's token function for a bot-identity +// entry: a token minted on the installation covering rr, cached until shortly +// before it expires. The installation is looked up again on every mint, so an +// App the repo's owner removed stops yielding tokens. app is taken through a +// getter because the AppAuth is replaced on key rotation. +func BotReferenceRepoTokenSource(rr config.ReferenceRepo, app func() ReferenceRepoApp, logger *slog.Logger) func() (string, bool) { + if rr.ReadOnly() { + return nil + } + if logger == nil { + logger = slog.Default() + } + var ( + mu sync.Mutex + token string + expiry time.Time + ) + return func() (string, bool) { + mu.Lock() + defer mu.Unlock() + if token != "" && time.Now().Add(referenceTokenRefreshMargin).Before(expiry) { + return token, true + } + token, expiry = "", time.Time{} + var a ReferenceRepoApp + if app != nil { + a = app() + } + if !a.available() { + logger.Warn("reference repo bot write: no GitHub App auth available", "repo", rr.Repo) + return "", false + } + ctx, cancel := context.WithTimeout(context.Background(), mintClientTimeout) + defer cancel() + id, err := a.Installation(ctx, rr.Owner(), rr.Name()) + if err != nil { + logger.Warn("reference repo bot write: App installation lookup failed", "repo", rr.Repo, "error", err.Error()) + return "", false + } + tok, exp, err := a.Mint(ctx, id, rr) + if err != nil || tok == "" { + logger.Warn("reference repo bot write: token mint failed", "repo", rr.Repo, "error", fmt.Sprint(err)) + return "", false + } + token, expiry = tok, exp + return token, true + } +} + +// Notes and reasons the resolution records (CredentialNote, +// CredentialReason). They are shown on the Settings page and, for a derived +// read-only entry, on the request issue, so they name no internals and never +// ask anyone to install the App. +const ( + referenceNoteAppInstalledFmt = "the hive's GitHub App is installed on %s" + referenceNoteAppNotInstalledFmt = "the hive's GitHub App is not installed on %s" + referenceNoteNoApp = "this hive has no GitHub App credentials" + referenceNoteOwnerSetting = "this hive posts on other projects as its owner" + referenceNoteTokenFile = "write_token_file" + // ReferenceReadOnlyBotOnlyPrefix starts the read-only reason of an entry + // left read-only by bot_only; the request-issue notice keys on it to + // leave out the connect-your-account cure, which would not help. + ReferenceReadOnlyBotOnlyPrefix = "this hive posts on other projects only as its GitHub App" + referenceReadOnlyBotOnlyFmt = ReferenceReadOnlyBotOnlyPrefix + ", which is not installed on %s" + referenceUnresolvedFmt = "could not check whether the hive's GitHub App is installed on %s; retrying on the next pass" +) + +// ReferenceRepoCredential is how one write-tier entry resolved. +type ReferenceRepoCredential struct { + // Identity is config.ReferenceIdentityBot, ...Owner or ...TokenFile; empty + // when the entry is served read-only. + Identity string + // Login is the GitHub login the writes appear under. + Login string + // LookupErr is the transient App lookup failure behind an unresolved + // entry, for the caller to log; nil otherwise. + LookupErr error +} + +// appLookup is the outcome of the App check for one repo. +type appLookup int + +const ( + appUnavailable appLookup = iota + appInstalled + appNotInstalled + appTransient +) + +func lookupReferenceApp(ctx context.Context, rr config.ReferenceRepo, app ReferenceRepoApp) (appLookup, string, error) { + if !app.available() { + return appUnavailable, referenceNoteNoApp, nil + } + _, err := app.Installation(ctx, rr.Owner(), rr.Name()) + switch { + case err == nil: + return appInstalled, fmt.Sprintf(referenceNoteAppInstalledFmt, rr.Owner()), nil + case errors.Is(err, ErrNoInstallationForOrg): + return appNotInstalled, fmt.Sprintf(referenceNoteAppNotInstalledFmt, rr.Owner()), nil + } + return appTransient, fmt.Sprintf(referenceUnresolvedFmt, rr.Owner()), err +} + +// ResolveReferenceRepoCredential resolves one entry against +// project.remote_write_identity (setting, any spelling; empty is the +// default), the owner identity and the App. It returns the entry as resolved +// (CredentialState, CredentialLogin, CredentialIdentity, CredentialNote, and +// Write cleared when it was served read-only) and the credential. +// +// A read-only entry passes through unchanged (a derived one is marked as +// ResolveDerivedReferenceRepo always did). A hand-written entry's +// write_token_file wins over the setting. A hand-written write entry left +// with no credential is ErrReferenceRepoNoCredential, as before; a derived +// one is downgraded to read-only instead. A transient App lookup failure +// serves the entry read-only for this pass (CredentialState unresolved) +// rather than posting under another identity, and is never an error. +func ResolveReferenceRepoCredential(ctx context.Context, rr config.ReferenceRepo, setting string, owner OwnerCredential, app ReferenceRepoApp, whoAmI func(ctx context.Context, token string) (string, error)) (config.ReferenceRepo, ReferenceRepoCredential, error) { + if rr.ReadOnly() { + if rr.Derived() { + rr, _ = ResolveDerivedReferenceRepo(rr, owner) + } + return rr, ReferenceRepoCredential{}, nil + } + if rr.Derived() { + rr.WriteTokenFile = "" + } else if rr.WriteTokenFile != "" { + login, err := VerifyReferenceRepoWriteCredential(ctx, rr, owner, whoAmI) + if err != nil { + return rr, ReferenceRepoCredential{}, err + } + return writeTier(rr, config.ReferenceIdentityTokenFile, login, referenceNoteTokenFile) + } + + mode := (config.ProjectConfig{RemoteWriteIdentity: setting}).EffectiveRemoteWriteIdentity() + ownerNote := referenceNoteOwnerSetting + if mode != config.RemoteWriteIdentityOwner { + state, note, err := lookupReferenceApp(ctx, rr, app) + switch state { + case appInstalled: + return writeTier(rr, config.ReferenceIdentityBot, app.Login, note) + case appTransient: + rr.Write = nil + rr.CredentialState = config.ReferenceCredentialUnresolved + rr.CredentialReason = note + rr.CredentialLogin, rr.CredentialIdentity, rr.CredentialNote = "", "", note + return rr, ReferenceRepoCredential{LookupErr: err}, nil + } + if mode == config.RemoteWriteIdentityBotOnly { + reason := fmt.Sprintf(referenceReadOnlyBotOnlyFmt, rr.Owner()) + if state == appUnavailable { + reason = ReferenceReadOnlyBotOnlyPrefix + ", and " + referenceNoteNoApp + } + if !rr.Derived() { + return rr, ReferenceRepoCredential{}, fmt.Errorf("%w: project.reference_repos %s: write: %v has no credential: %s; set write_token_file, or change project.remote_write_identity", ErrReferenceRepoNoCredential, rr.Repo, rr.Write, reason) + } + return readOnlyDerived(rr, reason, note) + } + ownerNote = note + } + + if !owner.Usable() { + if !rr.Derived() { + _, err := VerifyReferenceRepoWriteCredential(ctx, rr, owner, whoAmI) + return rr, ReferenceRepoCredential{}, err + } + return readOnlyDerived(rr, DerivedReadOnlyReason(rr, owner), ownerNote) + } + return writeTier(rr, config.ReferenceIdentityOwner, owner.Login, ownerNote) +} + +func writeTier(rr config.ReferenceRepo, identity, login, note string) (config.ReferenceRepo, ReferenceRepoCredential, error) { + rr.CredentialState = config.ReferenceCredentialWriteTier + rr.CredentialReason = "" + rr.CredentialLogin = login + rr.CredentialIdentity = identity + rr.CredentialNote = note + return rr, ReferenceRepoCredential{Identity: identity, Login: login}, nil +} + +func readOnlyDerived(rr config.ReferenceRepo, reason, note string) (config.ReferenceRepo, ReferenceRepoCredential, error) { + rr.Write = nil + rr.CredentialState = config.ReferenceCredentialReadOnly + rr.CredentialReason = reason + rr.CredentialLogin = "" + rr.CredentialIdentity = "" + rr.CredentialNote = note + return rr, ReferenceRepoCredential{}, nil +} diff --git a/src/pkg/github/reference_repo_identity_test.go b/src/pkg/github/reference_repo_identity_test.go new file mode 100644 index 0000000000..45b5bb4772 --- /dev/null +++ b/src/pkg/github/reference_repo_identity_test.go @@ -0,0 +1,389 @@ +package github + +import ( + "context" + "crypto/x509" + "encoding/json" + "encoding/pem" + "errors" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "sort" + "strings" + "sync" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" +) + +// project.remote_write_identity (hivecommons/hive#18516): which identity a +// write-tier reference repo writes as. The invariants these tests hold: +// +// - the setting picks an identity, it never widens a grant: a read-only +// entry gets no credential under any setting; +// - the App bot is used only where an installation covers that exact repo; +// - bot_only never resolves to the owner, even when the owner is connected; +// - a transient App lookup failure never silently switches identity; +// - write_token_file on a hand-written entry wins over every setting. + +const ( + idTestBot = "hivecommons-hive[bot]" + idTestOwner = "nate-double-u" +) + +// fakeReferenceApp is an App installed on the owners in installed (every +// repository of that owner), failing transiently on the owners in flaky. +type fakeReferenceApp struct { + mu sync.Mutex + installed map[string]int64 + flaky map[string]bool + lookups []string + mints []string +} + +func (f *fakeReferenceApp) seam() ReferenceRepoApp { + return ReferenceRepoApp{ + Login: idTestBot, + Installation: func(_ context.Context, owner, name string) (int64, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.lookups = append(f.lookups, owner+"/"+name) + if f.flaky[owner] { + return 0, errors.New("dial tcp: i/o timeout") + } + if id, ok := f.installed[owner]; ok { + return id, nil + } + return 0, fmt.Errorf("%w: not on %s", ErrNoInstallationForOrg, owner) + }, + Mint: func(_ context.Context, id int64, rr config.ReferenceRepo) (string, time.Time, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.mints = append(f.mints, fmt.Sprintf("%d:%s", id, rr.Repo)) + return "ghs_" + rr.Name(), time.Now().Add(time.Hour), nil + }, + } +} + +func idTestDerived(repo string) config.ReferenceRepo { + return config.ReferenceRepo{Repo: repo, Write: []string{config.ReferenceWriteIssues, config.ReferenceWritePRs}, DerivedFrom: "cncf/techdocs#395"} +} + +func TestResolveReferenceRepoCredential_Matrix(t *testing.T) { + ctx := context.Background() + connected := OwnerCredential{Login: idTestOwner} + disconnected := OwnerCredential{Unavailable: OwnerIdentityNotConnected} + type want struct { + identity string + login string + state string + } + writeAs := func(identity, login string) want { + return want{identity, login, config.ReferenceCredentialWriteTier} + } + readOnly := want{state: config.ReferenceCredentialReadOnly} + + cases := []struct { + setting string + installed bool + owner OwnerCredential + want want + }{ + // bot (the default; "" is the same) + {"", true, connected, writeAs(config.ReferenceIdentityBot, idTestBot)}, + {"", true, disconnected, writeAs(config.ReferenceIdentityBot, idTestBot)}, + {"", false, connected, writeAs(config.ReferenceIdentityOwner, idTestOwner)}, + {"", false, disconnected, readOnly}, + {"bot", true, connected, writeAs(config.ReferenceIdentityBot, idTestBot)}, + {"bot", true, disconnected, writeAs(config.ReferenceIdentityBot, idTestBot)}, + {"bot", false, connected, writeAs(config.ReferenceIdentityOwner, idTestOwner)}, + {"bot", false, disconnected, readOnly}, + // bot_only: never the owner. + {"bot_only", true, connected, writeAs(config.ReferenceIdentityBot, idTestBot)}, + {"bot_only", true, disconnected, writeAs(config.ReferenceIdentityBot, idTestBot)}, + {"bot_only", false, connected, readOnly}, + {"bot_only", false, disconnected, readOnly}, + // owner: never the bot, even where the App is installed. + {"owner", true, connected, writeAs(config.ReferenceIdentityOwner, idTestOwner)}, + {"owner", true, disconnected, readOnly}, + {"owner", false, connected, writeAs(config.ReferenceIdentityOwner, idTestOwner)}, + {"owner", false, disconnected, readOnly}, + } + for _, tc := range cases { + name := fmt.Sprintf("setting=%q/app=%v/owner=%v", tc.setting, tc.installed, tc.owner.Usable()) + t.Run(name, func(t *testing.T) { + app := &fakeReferenceApp{installed: map[string]int64{}} + if tc.installed { + app.installed["hivecommons"] = 42 + } + got, cred, err := ResolveReferenceRepoCredential(ctx, idTestDerived("hivecommons/docs"), tc.setting, tc.owner, app.seam(), nil) + if err != nil { + t.Fatalf("a derived entry never errors: %v", err) + } + if cred.Identity != tc.want.identity || cred.Login != tc.want.login || got.CredentialState != tc.want.state { + t.Fatalf("resolved identity=%q login=%q state=%q, want %+v", cred.Identity, cred.Login, got.CredentialState, tc.want) + } + if got.CredentialIdentity != cred.Identity || got.CredentialLogin != cred.Login { + t.Fatalf("entry and credential disagree: entry %q/%q, credential %+v", got.CredentialIdentity, got.CredentialLogin, cred) + } + if tc.want.state == config.ReferenceCredentialReadOnly { + if !got.ReadOnly() || !got.DerivedReadOnly() || got.CredentialReason == "" { + t.Fatalf("read-only resolution must clear Write and say why: %+v", got) + } + } else if !got.WritesIssues() || !got.WritesPRs() { + t.Fatalf("write tier lost its opt-in: %+v", got) + } + if got.CredentialNote == "" { + t.Fatalf("every resolution carries a note for Settings: %+v", got) + } + if tc.setting == config.RemoteWriteIdentityOwner && len(app.lookups) != 0 { + t.Fatalf("owner must not consult the App at all, looked up %v", app.lookups) + } + if len(app.mints) != 0 { + t.Fatalf("resolution must never mint, minted %v", app.mints) + } + }) + } +} + +func TestResolveReferenceRepoCredential_BotOnlyNeverOwner(t *testing.T) { + ctx := context.Background() + app := &fakeReferenceApp{installed: map[string]int64{"hivecommons": 42}} + owner := OwnerCredential{Login: idTestOwner} + got, cred, err := ResolveReferenceRepoCredential(ctx, idTestDerived("acme/site"), "bot_only", owner, app.seam(), nil) + if err != nil { + t.Fatal(err) + } + if cred.Identity != "" || cred.Login != "" || !got.DerivedReadOnly() { + t.Fatalf("bot_only without the App must be read-only, got %+v / %+v", cred, got) + } + if !strings.HasPrefix(got.CredentialReason, ReferenceReadOnlyBotOnlyPrefix) || !strings.Contains(got.CredentialReason, "acme") { + t.Fatalf("reason must say bot only and name the owner: %q", got.CredentialReason) + } + if strings.Contains(strings.ToLower(got.CredentialReason), "install the") || strings.Contains(got.CredentialReason, idTestOwner) { + t.Fatalf("reason must not ask anyone to install the App or name the owner: %q", got.CredentialReason) + } + // No App auth at all is the same verdict. + got, cred, err = ResolveReferenceRepoCredential(ctx, idTestDerived("hivecommons/docs"), "bot_only", owner, ReferenceRepoApp{}, nil) + if err != nil || cred.Identity != "" || !got.DerivedReadOnly() { + t.Fatalf("bot_only with no App auth must be read-only: %+v %+v %v", cred, got, err) + } + // A hand-written entry is an operator error, as it is under owner. + _, _, err = ResolveReferenceRepoCredential(ctx, config.ReferenceRepo{Repo: "acme/site", Write: []string{"issues"}}, "bot_only", owner, app.seam(), nil) + if !errors.Is(err, ErrReferenceRepoNoCredential) { + t.Fatalf("hand-written bot_only entry without the App must refuse, got %v", err) + } +} + +func TestResolveReferenceRepoCredential_ReadOnlyGetsNothing(t *testing.T) { + ctx := context.Background() + app := &fakeReferenceApp{installed: map[string]int64{"hivecommons": 42}} + for _, setting := range []string{"", "bot", "bot_only", "owner"} { + for _, rr := range []config.ReferenceRepo{{Repo: "hivecommons/website"}, {Repo: "hivecommons/website", DerivedFrom: "cncf/techdocs#395"}} { + got, cred, err := ResolveReferenceRepoCredential(ctx, rr, setting, OwnerCredential{Login: idTestOwner}, app.seam(), nil) + if err != nil || cred.Identity != "" || cred.Login != "" || !got.ReadOnly() || got.CredentialState == config.ReferenceCredentialWriteTier { + t.Fatalf("setting %q: a read-only entry must get no credential, got %+v %+v %v", setting, cred, got, err) + } + } + } + if len(app.lookups) != 0 || len(app.mints) != 0 { + t.Fatalf("a read-only entry must not even look the App up: lookups=%v mints=%v", app.lookups, app.mints) + } +} + +func TestResolveReferenceRepoCredential_TransientNeverSwitchesIdentity(t *testing.T) { + ctx := context.Background() + app := &fakeReferenceApp{installed: map[string]int64{"hivecommons": 42}, flaky: map[string]bool{"hivecommons": true}} + for _, setting := range []string{"bot", "bot_only"} { + got, cred, err := ResolveReferenceRepoCredential(ctx, idTestDerived("hivecommons/docs"), setting, OwnerCredential{Login: idTestOwner}, app.seam(), nil) + if err != nil { + t.Fatalf("%s: a blip is never an error: %v", setting, err) + } + if cred.Identity != "" || got.CredentialState != config.ReferenceCredentialUnresolved || !got.ReadOnly() || cred.LookupErr == nil { + t.Fatalf("%s: a blip serves read-only this pass, got %+v %+v", setting, cred, got) + } + if got.DerivedReadOnly() { + t.Fatalf("%s: unresolved must not trigger the request-issue notice", setting) + } + } + // A hand-written entry is not refused on a blip either. + _, cred, err := ResolveReferenceRepoCredential(ctx, config.ReferenceRepo{Repo: "hivecommons/docs", Write: []string{"prs"}}, "bot", OwnerCredential{Login: idTestOwner}, app.seam(), nil) + if err != nil || cred.Identity != "" { + t.Fatalf("hand-written entry on a blip: cred=%+v err=%v", cred, err) + } +} + +func TestResolveReferenceRepoCredential_TokenFileWins(t *testing.T) { + ctx := context.Background() + tokFile := filepath.Join(t.TempDir(), "tok") + if err := os.WriteFile(tokFile, []byte("ghp_pat\n"), 0o600); err != nil { + t.Fatal(err) + } + whoAmI := func(_ context.Context, token string) (string, error) { + if token == "ghp_pat" { + return "maintainer", nil + } + return "", errors.New("bad credentials") + } + app := &fakeReferenceApp{installed: map[string]int64{"hivecommons": 42}} + rr := config.ReferenceRepo{Repo: "hivecommons/docs", Write: []string{"issues"}, WriteTokenFile: tokFile} + for _, setting := range []string{"bot", "bot_only", "owner"} { + _, cred, err := ResolveReferenceRepoCredential(ctx, rr, setting, OwnerCredential{Login: idTestOwner}, app.seam(), whoAmI) + if err != nil || cred.Identity != config.ReferenceIdentityTokenFile || cred.Login != "maintainer" { + t.Fatalf("%s: write_token_file must win, got %+v %v", setting, cred, err) + } + } + // A derived entry never reads one. + d := idTestDerived("hivecommons/docs") + d.WriteTokenFile = tokFile + got, cred, err := ResolveReferenceRepoCredential(ctx, d, "bot", OwnerCredential{}, app.seam(), whoAmI) + if err != nil || cred.Identity != config.ReferenceIdentityBot || got.WriteTokenFile != "" { + t.Fatalf("derived entry must ignore write_token_file: %+v %+v %v", cred, got, err) + } +} + +func TestBotReferenceRepoTokenSource(t *testing.T) { + app := &fakeReferenceApp{installed: map[string]int64{"hivecommons": 42}} + rr := idTestDerived("hivecommons/docs") + src := BotReferenceRepoTokenSource(rr, app.seam, testLogger()) + for i := 0; i < 3; i++ { + if tok, ok := src(); !ok || tok != "ghs_docs" { + t.Fatalf("got %q,%v", tok, ok) + } + } + if len(app.mints) != 1 || app.mints[0] != "42:hivecommons/docs" { + t.Fatalf("token must be minted once, for this repo only, and cached: %v", app.mints) + } + if BotReferenceRepoTokenSource(config.ReferenceRepo{Repo: "hivecommons/docs"}, app.seam, testLogger()) != nil { + t.Fatal("a read-only entry must have no bot token source") + } + // The App removed from the repo: no token, never a fallback credential. + gone := &fakeReferenceApp{installed: map[string]int64{}} + if tok, ok := BotReferenceRepoTokenSource(rr, gone.seam, testLogger())(); ok || tok != "" { + t.Fatalf("no installation must yield no token, got %q", tok) + } + if tok, ok := BotReferenceRepoTokenSource(rr, func() ReferenceRepoApp { return ReferenceRepoApp{} }, testLogger())(); ok || tok != "" { + t.Fatalf("no App auth must yield no token, got %q", tok) + } +} + +// The production mint: the installation is looked up for the exact repo and +// the token is scoped to that repo and to the tier's permissions. +func TestAppAuthMintReferenceRepoToken_Scope(t *testing.T) { + key := discoveryTestKey(t) + pemBytes := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}) + var ( + mu sync.Mutex + bodies []map[string]any + paths []string + ) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + paths = append(paths, r.Method+" "+r.URL.Path) + mu.Unlock() + switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/hivecommons/docs/installation": + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "account": map[string]any{"login": "hivecommons"}}) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/installation"): + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message":"Not Found"}`)) + case r.Method == http.MethodPost && r.URL.Path == "/app/installations/42/access_tokens": + raw, _ := io.ReadAll(r.Body) + var body map[string]any + _ = json.Unmarshal(raw, &body) + mu.Lock() + bodies = append(bodies, body) + mu.Unlock() + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + _ = json.NewEncoder(w).Encode(map[string]any{"token": "ghs_scoped", "expires_at": time.Now().Add(time.Hour).UTC().Format(time.RFC3339)}) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(srv.Close) + ResetInstallationDiscoveryCache() + t.Cleanup(ResetInstallationDiscoveryCache) + auth, err := NewAppAuthFromPEM(1, 7, pemBytes, discoveryTestLogger(), srv.URL) + if err != nil { + t.Fatal(err) + } + auth.SetBotLogin(idTestBot) + app := auth.ReferenceRepoApp() + if app.Login != idTestBot || !app.available() { + t.Fatalf("seam not wired: %+v", app) + } + + ctx := context.Background() + if _, err := app.Installation(ctx, "acme", "site"); !errors.Is(err, ErrNoInstallationForOrg) { + t.Fatalf("a 404 must read as not installed, got %v", err) + } + id, err := app.Installation(ctx, "hivecommons", "docs") + if err != nil || id != 42 { + t.Fatalf("installation = %d, %v", id, err) + } + tok, _, err := app.Mint(ctx, id, idTestDerived("hivecommons/docs")) + if err != nil || tok != "ghs_scoped" { + t.Fatalf("mint = %q, %v", tok, err) + } + if len(bodies) != 1 { + t.Fatalf("want one mint request, got %d (%v)", len(bodies), paths) + } + repos, _ := bodies[0]["repositories"].([]any) + if len(repos) != 1 || repos[0] != "docs" { + t.Fatalf("token must be scoped to the one repo, got repositories=%v", bodies[0]["repositories"]) + } + perms, _ := bodies[0]["permissions"].(map[string]any) + var got []string + for k, v := range perms { + got = append(got, fmt.Sprintf("%s=%v", k, v)) + } + sort.Strings(got) + want := []string{"contents=write", "issues=write", "metadata=read", "pull_requests=write"} + if strings.Join(got, ",") != strings.Join(want, ",") { + t.Fatalf("permissions = %v, want exactly %v", got, want) + } + for _, p := range paths { + if strings.Contains(p, "/app/installations/7/") { + t.Fatalf("the hive's own installation must never be used for a reference repo: %v", paths) + } + } + if _, _, err := auth.MintReferenceRepoToken(ctx, 42, config.ReferenceRepo{Repo: "hivecommons/website"}); !errors.Is(err, ErrReferenceRepoNoCredential) { + t.Fatalf("a read-only entry must be refused before any mint, got %v", err) + } + if len(bodies) != 1 { + t.Fatalf("refused mint must not reach GitHub, got %d mint requests", len(bodies)) + } +} + +func TestReferenceRepoPermissions(t *testing.T) { + p := referenceRepoPermissions(config.ReferenceRepo{Repo: "a/b", Write: []string{"issues"}}) + if p.GetIssues() != "write" || p.GetContents() != "read" || p.GetPullRequests() != "" || p.GetMetadata() != "read" { + t.Errorf("issues-only perms wrong: issues=%q contents=%q prs=%q", p.GetIssues(), p.GetContents(), p.GetPullRequests()) + } + p = referenceRepoPermissions(config.ReferenceRepo{Repo: "a/b", Write: []string{"prs"}}) + if p.GetIssues() != "" || p.GetContents() != "write" || p.GetPullRequests() != "write" { + t.Errorf("prs perms wrong: issues=%q contents=%q prs=%q", p.GetIssues(), p.GetContents(), p.GetPullRequests()) + } + if p.GetAdministration() != "" || p.GetWorkflows() != "" || p.GetMembers() != "" { + t.Error("no management permission may ever be requested") + } +} + +func TestDerivedReadOnlyComment_BotOnlyNamesNoCure(t *testing.T) { + rr := idTestDerived("acme/site") + rr, _, _ = ResolveReferenceRepoCredential(context.Background(), rr, "bot_only", OwnerCredential{Login: idTestOwner}, ReferenceRepoApp{}, nil) + body := DerivedReadOnlyComment([]config.ReferenceRepo{rr}) + if strings.Contains(body, "Connect GitHub account") || strings.Contains(strings.ToLower(body), "install the") { + t.Fatalf("bot_only notice must not name a cure: %s", body) + } + if !strings.Contains(body, "acme/site") { + t.Fatalf("notice must name the repo: %s", body) + } +} diff --git a/src/pkg/github/reference_repo_request.go b/src/pkg/github/reference_repo_request.go index cad00502b4..0f819ad456 100644 --- a/src/pkg/github/reference_repo_request.go +++ b/src/pkg/github/reference_repo_request.go @@ -80,8 +80,14 @@ func DerivedReadOnlyComment(readOnly []config.ReferenceRepo) string { fmt.Fprintf(&b, "- `%s`: %s\n", rr.Repo, reason) } b.WriteString("\nThe hive will study these repositories (clone, read files, read issues and PRs) and keep its findings in the assessment deliverable here. ") - b.WriteString("It will not open issues or pull requests on them. ") - b.WriteString("To let it, the hive owner connects their own GitHub account on the hive's dashboard (Settings → Reference repos → Connect GitHub account); issues and PRs then appear under that account, and the hive picks the change up without a restart. No GitHub App needs to be installed on these repositories.") + b.WriteString("It will not open issues or pull requests on them.") + if derivedReadOnlyBotOnly(readOnly) { + // bot_only (project.remote_write_identity): connecting the owner's + // account would not change anything, and the hive never asks a + // project to install its App, so there is no cure to name. + return AppendTrailer(b.String(), hiveWriteMeta()) + } + b.WriteString(" To let it, the hive owner connects their own GitHub account on the hive's dashboard (Settings → Reference repos → Connect GitHub account); issues and PRs then appear under that account, and the hive picks the change up without a restart. No GitHub App needs to be installed on these repositories.") return AppendTrailer(b.String(), hiveWriteMeta()) } @@ -264,3 +270,14 @@ func (c *Client) RepoPermission(ctx context.Context, owner, repo, login string) } return strings.ToLower(strings.TrimSpace(perm.GetPermission())), nil } + +// derivedReadOnlyBotOnly reports whether every entry was left read-only by +// project.remote_write_identity bot_only. +func derivedReadOnlyBotOnly(readOnly []config.ReferenceRepo) bool { + for _, rr := range readOnly { + if !strings.HasPrefix(strings.TrimSpace(rr.CredentialReason), ReferenceReadOnlyBotOnlyPrefix) { + return false + } + } + return len(readOnly) > 0 +} diff --git a/src/pkg/github/reference_repo_token.go b/src/pkg/github/reference_repo_token.go index 0bb4643f61..3117e81100 100644 --- a/src/pkg/github/reference_repo_token.go +++ b/src/pkg/github/reference_repo_token.go @@ -29,6 +29,10 @@ import ( // so a rotation is picked up without a restart. A derived entry never // reads it. // +// project.remote_write_identity (hivecommons/hive#18516) adds a third +// source in front of the owner: the hive's own GitHub App, only on a granted +// repo whose owner installed it themselves. See reference_repo_identity.go. +// // Boot refuses a hand-written write-mode entry that has neither (see // VerifyReferenceRepoWriteCredential) so the failure is a config error with // the repo named, not a 401 a day later. A derived entry is downgraded to diff --git a/src/pkg/scheduler/kickmessage.go b/src/pkg/scheduler/kickmessage.go index 3a43c812c1..08c7ae2640 100644 --- a/src/pkg/scheduler/kickmessage.go +++ b/src/pkg/scheduler/kickmessage.go @@ -941,17 +941,55 @@ func referenceRepoProvenance(rr config.ReferenceRepo) string { // push to a repo it does not own, so a PR goes through the owner's fork: the // paragraph names the login and the exact three commands. Empty when no entry // has both the prs tier and a resolved credential login. +// +// An entry that writes as the hive's GitHub App bot +// (project.remote_write_identity) is different: the App cannot own a fork, +// and its token is minted on the repo owner's own installation, so the topic +// branch is pushed to the reference repo itself; referenceRepoBotInstruction +// renders that paragraph. func referenceRepoForkInstruction(refs []config.ReferenceRepo, host string) string { login := "" for _, rr := range refs { - if rr.WritesPRs() && rr.CredentialLogin != "" { + if rr.WritesPRs() && rr.CredentialLogin != "" && rr.CredentialIdentity != config.ReferenceIdentityBot { login = rr.CredentialLogin break } } + bot := referenceRepoBotInstruction(refs) if login == "" { - return "" + return bot } host = strings.TrimRight(host, "/") - return fmt.Sprintf(" ✍️ Writes to a reference repo appear under the GitHub account %s (the hive owner), not under the hive. You CANNOT push to a reference repo itself. For a PR: fork it into that account (gh repo fork / --clone=false; already-forked is fine), push your topic branch to %s/%s/ (git remote add fork %s/%s/.git && git push fork ), then open the PR against the reference repo: gh pr create --repo / --head %s:. Never push to the fork's default branch; do nothing else on the fork.\n", login, host, login, host, login, login) + subject, itself := "a reference repo", "a reference repo itself" + if bot != "" { + subject, itself = "any other write-tier reference repo", "those repos themselves" + } + return bot + fmt.Sprintf(" ✍️ Writes to %s appear under the GitHub account %s (the hive owner), not under the hive. You CANNOT push to %s. For a PR: fork it into that account (gh repo fork / --clone=false; already-forked is fine), push your topic branch to %s/%s/ (git remote add fork %s/%s/.git && git push fork ), then open the PR against the reference repo: gh pr create --repo / --head %s:. Never push to the fork's default branch; do nothing else on the fork.\n", subject, login, itself, host, login, host, login, login) +} + +// referenceRepoBotInstruction is the kick's paragraph for the write-tier +// reference repos that write as the hive's GitHub App bot. Empty when there +// are none. +func referenceRepoBotInstruction(refs []config.ReferenceRepo) string { + var repos []string + login := "" + prs := false + for _, rr := range refs { + if rr.ReadOnly() || rr.CredentialIdentity != config.ReferenceIdentityBot || rr.CredentialLogin == "" { + continue + } + repos = append(repos, rr.Repo) + if login == "" { + login = rr.CredentialLogin + } + prs = prs || rr.WritesPRs() + } + if len(repos) == 0 { + return "" + } + line := fmt.Sprintf(" 🤖 Writes to %s appear under the hive's GitHub App, %s (the project installed it), not under a person.", strings.Join(repos, ", "), login) + if prs { + line += " For a PR there, do NOT fork: push your topic branch to the reference repo itself (never its default branch) and open the PR with gh pr create --repo / --head ." + } + return line + "\n" } diff --git a/src/pkg/scheduler/reference_repo_identity_kick_test.go b/src/pkg/scheduler/reference_repo_identity_kick_test.go new file mode 100644 index 0000000000..452f5209d2 --- /dev/null +++ b/src/pkg/scheduler/reference_repo_identity_kick_test.go @@ -0,0 +1,42 @@ +package scheduler + +import ( + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/config" +) + +// hivecommons/hive#18516: a bot-identity reference repo gets its own PR +// instruction (no fork, push the topic branch to the repo itself), and in a +// mixed set the owner's fork paragraph no longer claims to cover it. +func TestBuildReposSectionFor_ReferenceRepoBotInstruction(t *testing.T) { + s := scopeScheduler(t, map[string]config.AgentConfig{"scanner": {Backend: "claude"}}) + s.cfg.Project.ReferenceRepos = []config.ReferenceRepo{ + {Repo: "hivecommons/docs", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395", CredentialState: config.ReferenceCredentialWriteTier, CredentialLogin: "hivecommons-hive[bot]", CredentialIdentity: config.ReferenceIdentityBot}, + {Repo: "acme/site", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395", CredentialState: config.ReferenceCredentialWriteTier, CredentialLogin: "nate-double-u", CredentialIdentity: config.ReferenceIdentityOwner}, + } + section := s.buildReposSectionFor("scanner") + for _, want := range []string{ + "Writes to hivecommons/docs appear under the hive's GitHub App, hivecommons-hive[bot]", + "do NOT fork: push your topic branch to the reference repo itself (never its default branch)", + "--head ", + "Writes to any other write-tier reference repo appear under the GitHub account nate-double-u (the hive owner)", + "You CANNOT push to those repos themselves", + "--head nate-double-u:", + } { + if !strings.Contains(section, want) { + t.Errorf("REFERENCE REPOS block lacks %q:\n%s", want, section) + } + } + if strings.Contains(section, "https://github.com/hivecommons-hive[bot]/") { + t.Error("the bot has no fork; the kick must never point at one") + } + + // Bot only: no fork paragraph at all. + s.cfg.Project.ReferenceRepos = s.cfg.Project.ReferenceRepos[:1] + section = s.buildReposSectionFor("scanner") + if strings.Contains(section, "gh repo fork") || !strings.Contains(section, "do NOT fork") { + t.Errorf("bot-only set must not render the fork flow:\n%s", section) + } +} From 2acf6cbe6a7d417922975c6e6ff6c0f97b415554 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 16:42:51 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20reference=20repos:=20keep=20?= =?UTF-8?q?a=20hand-written=20entry's=20write=20opt-in=20on=20a=20transien?= =?UTF-8?q?t=20App=20lookup=20(#18516)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The resolved list replaces the config in memory and the editor saves from it, so clearing Write on a blip would persist as an edit. The entry keeps Write, is marked unresolved, and gets a read-only proxy policy for the pass; the kick says it is read-only this pass. Signed-off-by: Andy Anderson --- src/cmd/hive/reference_repos.go | 7 ++++++- src/cmd/hive/reference_repos_identity_test.go | 20 +++++++++++++------ src/pkg/github/reference_repo_identity.go | 14 ++++++++++--- .../github/reference_repo_identity_test.go | 10 +++++++--- src/pkg/scheduler/kickmessage.go | 12 +++++++++++ 5 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/cmd/hive/reference_repos.go b/src/cmd/hive/reference_repos.go index 8b7f604730..d86359fa1f 100644 --- a/src/cmd/hive/reference_repos.go +++ b/src/cmd/hive/reference_repos.go @@ -113,7 +113,12 @@ func buildReferenceRepoPolicies(ctx context.Context, refs []config.ReferenceRepo } rr = resolved pol := proxy.ReferenceRepoPolicy{Repo: rr.Repo, Issues: rr.WritesIssues(), PRs: rr.WritesPRs()} - if !rr.ReadOnly() { + if rr.CredentialState == config.ReferenceCredentialUnresolved { + // No credential this pass (a hand-written entry keeps its Write + // opt-in in config): the proxy refuses its writes until the next + // pass resolves it. + pol = proxy.ReferenceRepoPolicy{Repo: rr.Repo} + } else if !rr.ReadOnly() { pol.Login = cred.Login if cred.Identity == config.ReferenceIdentityBot { pol.Token = github.BotReferenceRepoTokenSource(rr, deps.appSeam, logger) diff --git a/src/cmd/hive/reference_repos_identity_test.go b/src/cmd/hive/reference_repos_identity_test.go index 4b12398efe..3e96699607 100644 --- a/src/cmd/hive/reference_repos_identity_test.go +++ b/src/cmd/hive/reference_repos_identity_test.go @@ -234,15 +234,23 @@ func TestReferenceRepoIdentity_TransientLookupStaysReadOnly(t *testing.T) { }, } } - refs := []config.ReferenceRepo{{Repo: "hivecommons/docs", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}} + refs := []config.ReferenceRepo{ + {Repo: "hivecommons/docs", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}, + {Repo: "hivecommons/hive", Write: []string{"issues", "prs"}}, + } res, err := buildReferenceRepoPolicies(context.Background(), refs, deps, testLogger()) if err != nil { - t.Fatal(err) + t.Fatalf("a blip must never refuse boot: %v", err) } - if p := res.policies[0]; p.Token != nil || p.Issues || p.PRs { - t.Fatalf("unresolved entry must carry no credential: %+v", p) + for i, p := range res.policies { + if p.Token != nil || p.Issues || p.PRs || p.Login != "" { + t.Fatalf("unresolved entry must get a read-only policy: %+v", p) + } + if rr := res.repos[i]; rr.CredentialState != config.ReferenceCredentialUnresolved || rr.DerivedReadOnly() { + t.Fatalf("unresolved entry state = %+v", rr) + } } - if rr := res.repos[0]; rr.CredentialState != config.ReferenceCredentialUnresolved || rr.DerivedReadOnly() { - t.Fatalf("unresolved entry state = %+v", rr) + if !res.repos[1].WritesPRs() { + t.Fatalf("a hand-written entry keeps its Write opt-in in config: %+v", res.repos[1]) } } diff --git a/src/pkg/github/reference_repo_identity.go b/src/pkg/github/reference_repo_identity.go index 624b0d0ca1..c6f4409fce 100644 --- a/src/pkg/github/reference_repo_identity.go +++ b/src/pkg/github/reference_repo_identity.go @@ -273,8 +273,9 @@ func lookupReferenceApp(ctx context.Context, rr config.ReferenceRepo, app Refere // write_token_file wins over the setting. A hand-written write entry left // with no credential is ErrReferenceRepoNoCredential, as before; a derived // one is downgraded to read-only instead. A transient App lookup failure -// serves the entry read-only for this pass (CredentialState unresolved) -// rather than posting under another identity, and is never an error. +// serves the entry read-only for this pass (CredentialState unresolved, and +// an empty credential the caller must build a read-only policy from) rather +// than posting under another identity, and is never an error. func ResolveReferenceRepoCredential(ctx context.Context, rr config.ReferenceRepo, setting string, owner OwnerCredential, app ReferenceRepoApp, whoAmI func(ctx context.Context, token string) (string, error)) (config.ReferenceRepo, ReferenceRepoCredential, error) { if rr.ReadOnly() { if rr.Derived() { @@ -300,7 +301,14 @@ func ResolveReferenceRepoCredential(ctx context.Context, rr config.ReferenceRepo case appInstalled: return writeTier(rr, config.ReferenceIdentityBot, app.Login, note) case appTransient: - rr.Write = nil + // A derived entry is rebuilt from its request issue every pass, so + // its Write is cleared like a read-only downgrade. A hand-written + // one keeps Write: the resolved list replaces the config in + // memory, and the editor saves from it, so clearing it would turn + // a blip into a permanent edit. The caller serves it read-only. + if rr.Derived() { + rr.Write = nil + } rr.CredentialState = config.ReferenceCredentialUnresolved rr.CredentialReason = note rr.CredentialLogin, rr.CredentialIdentity, rr.CredentialNote = "", "", note diff --git a/src/pkg/github/reference_repo_identity_test.go b/src/pkg/github/reference_repo_identity_test.go index 45b5bb4772..e4a303c4d2 100644 --- a/src/pkg/github/reference_repo_identity_test.go +++ b/src/pkg/github/reference_repo_identity_test.go @@ -211,11 +211,15 @@ func TestResolveReferenceRepoCredential_TransientNeverSwitchesIdentity(t *testin t.Fatalf("%s: unresolved must not trigger the request-issue notice", setting) } } - // A hand-written entry is not refused on a blip either. - _, cred, err := ResolveReferenceRepoCredential(ctx, config.ReferenceRepo{Repo: "hivecommons/docs", Write: []string{"prs"}}, "bot", OwnerCredential{Login: idTestOwner}, app.seam(), nil) - if err != nil || cred.Identity != "" { + // A hand-written entry is not refused on a blip either, and keeps its + // Write opt-in: the resolved list is what the editor saves from. + hw, cred, err := ResolveReferenceRepoCredential(ctx, config.ReferenceRepo{Repo: "hivecommons/docs", Write: []string{"prs"}}, "bot", OwnerCredential{Login: idTestOwner}, app.seam(), nil) + if err != nil || cred.Identity != "" || cred.Login != "" { t.Fatalf("hand-written entry on a blip: cred=%+v err=%v", cred, err) } + if !hw.WritesPRs() || hw.CredentialState != config.ReferenceCredentialUnresolved { + t.Fatalf("hand-written entry must keep Write and be marked unresolved: %+v", hw) + } } func TestResolveReferenceRepoCredential_TokenFileWins(t *testing.T) { diff --git a/src/pkg/scheduler/kickmessage.go b/src/pkg/scheduler/kickmessage.go index 08c7ae2640..41f313bc76 100644 --- a/src/pkg/scheduler/kickmessage.go +++ b/src/pkg/scheduler/kickmessage.go @@ -922,6 +922,18 @@ const derivedReadOnlyNoReason = "no write credential for this repository" // (hivecommons/hive#18159): where the grant came from, and for one served // read-only, why. Empty for a hand-written entry. func referenceRepoProvenance(rr config.ReferenceRepo) string { + if rr.CredentialState == config.ReferenceCredentialUnresolved { + // hivecommons/hive#18516: the identity could not be resolved this + // pass, so the proxy refuses the writes the tier above names. + why := strings.TrimSpace(rr.CredentialReason) + if why == "" { + why = derivedReadOnlyNoReason + } + if rr.Derived() { + return " (from " + rr.DerivedFrom + "; read-only this pass: " + why + ")" + } + return " (read-only this pass: " + why + ")" + } if !rr.Derived() { return "" }