package archied import ( "context" "encoding/json" "github.com/samcharles93/archie-core/internal/app/servicekit" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/daemon" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/infrastructure/controlplanestore" ) // orgAgents is every organisation with the principals that may act in it, // system org first. The system org's own root identity leads its list. type orgAgents struct { owner org.OrgID principals []access.Principal } // act returns ctx acting in the organisation. The system org is the // daemon's own, so it needs no agent principal. func (o orgAgents) act(ctx context.Context) context.Context { if o.owner == org.DefaultOrgID { return ctx } return org.WithOrg(access.WithPrincipal(ctx, o.principals[0]), o.owner) } // orgDirectory finds every organisation and an agent principal to act as in // each, because the control plane serves a caller only its own org. type orgDirectory interface { access.PrincipalSource ListOrgs(ctx context.Context) ([]org.Org, error) ListOrgAgents(ctx context.Context, id org.OrgID) ([]org.AgentAssignment, error) } // owners lists every org, system org first. Without the directory the system // org still runs. func (b *boot) owners(ctx context.Context, directory orgDirectory) []org.OrgID { owners := []org.OrgID{org.DefaultOrgID} if directory == nil { return owners } list, err := directory.ListOrgs(ctx) if err != nil { b.log.WarnContext(ctx, "organisations unavailable; acting in the system organisation only", "err", err) return owners } for _, o := range list { if o.ID != org.DefaultOrgID { owners = append(owners, o.ID) } } return owners } func (b *boot) organisations(ctx context.Context) []orgAgents { directory, _ := b.stateStore.(orgDirectory) var out []orgAgents for _, owner := range b.owners(ctx, directory) { entry := orgAgents{owner: owner} if owner == org.DefaultOrgID && directory != nil { root := identity.StableID(servicekit.IdentityNames(b.cfgHolder.Get())[0]) if principal, err := directory.PrincipalFor(ctx, root); err == nil { entry.principals = append(entry.principals, principal) } } if directory != nil { agents, err := directory.ListOrgAgents(ctx, owner) if err != nil { b.log.WarnContext(ctx, "organisation agents unavailable", "org", owner, "err", err) } for _, agent := range agents { principal, err := directory.PrincipalFor(ctx, agent.IdentityID) if err == nil && principal.Org == owner { entry.principals = append(entry.principals, principal) } } } if owner != org.DefaultOrgID && len(entry.principals) == 0 { b.log.WarnContext(ctx, "organisation skipped: no agent to act as", "org", owner) continue } out = append(out, entry) } return out } // orgPrincipals is the first acting principal of every organisation. func (b *boot) orgPrincipals(ctx context.Context) []access.Principal { var out []access.Principal for _, o := range b.organisations(ctx) { if len(o.principals) > 0 { out = append(out, o.principals[0]) } } return out } // issueSources resolves every organisation's repository bindings to their // connections, each read through the first agent its permits allow to clone. // It runs every poll cycle, so a rotated credential applies to the next poll. func (b *boot) issueSources(ctx context.Context) []daemon.IssueSource { var out []daemon.IssueSource for _, o := range b.organisations(ctx) { log := b.log.With("org", o.owner) octx := o.act(ctx) var repos []config.Repo if _, found, err := b.controlPlane.Query(octx, controlplanestore.RepositoryPoliciesKind, func(value []byte) error { return json.Unmarshal(value, &repos) }); err != nil || !found { log.WarnContext(ctx, "issue discovery: repositories unavailable", "err", err) continue } connections, err := b.repositoryConnections(octx) if err != nil { log.WarnContext(ctx, "issue discovery: connections unavailable", "err", err) continue } byConnection := map[string][]config.Repo{} for _, repo := range repos { if repo.Connection == "" { log.WarnContext(ctx, "issue discovery: repository has no connection", "repo", repo.FullName()) continue } byConnection[repo.Connection] = append(byConnection[repo.Connection], repo) } for _, connection := range connections { bound := byConnection[connection.ID] if len(bound) == 0 { continue } if source, ok := b.connectionSource(octx, o, connection, bound); ok { out = append(out, source) } } } return out } func (b *boot) connectionSource(ctx context.Context, o orgAgents, connection config.ForgeConnection, repos []config.Repo) (daemon.IssueSource, bool) { log := b.log.With("org", o.owner, "connection", connection.ID) for _, principal := range o.principals { if !connection.Permitted(string(principal.IdentityID), config.OperationClone) { continue } resolved, err := b.openRepositoryConnection(ctx, principal, repos[0], connection, config.OperationClone) if err != nil { log.WarnContext(ctx, "issue discovery: connection unavailable", "err", err) return daemon.IssueSource{}, false } user := resolved.Trees.AuthUser if user == "" && o.owner == org.DefaultOrgID { user = b.cfgHolder.Get().BotUser } return daemon.IssueSource{Identity: principal.IdentityID, Forge: resolved.Forge, BotUser: user, Repos: repos}, true } log.WarnContext(ctx, "issue discovery: no agent may clone through this connection") return daemon.IssueSource{}, false }