Drop the bot_user-derived root identity and the empty-identity sentinel. System polls forge connections and owns instance-level work; a polled task stores the polling identity, task_queued and work_request_submitted carry task id, actor and principal, and an empty task identity is refused. Migration 0072 moves empty tasks.identity, the identity-grants root key and the old root's connection permits to System so a re-poll dedups.
181 lines
6.3 KiB
Go
181 lines
6.3 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"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. System leads the system org's 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 {
|
|
if principal, err := directory.PrincipalFor(ctx, identity.SystemID); 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)
|
|
}
|
|
rule, err := b.discoveryRule(octx, o.owner)
|
|
if err != nil {
|
|
log.WarnContext(ctx, "issue discovery: scheduling policy unavailable", "err", err)
|
|
continue
|
|
}
|
|
for _, connection := range connections {
|
|
bound := byConnection[connection.ID]
|
|
if len(bound) == 0 {
|
|
continue
|
|
}
|
|
if source, ok := b.connectionSource(octx, o, connection, bound); ok {
|
|
source.Dispatch, source.Label = rule.Dispatch, rule.Label
|
|
out = append(out, source)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// discoveryRule is the dispatch rule an organisation's repositories are
|
|
// polled with: its own scheduling policy over the daemon's, which already
|
|
// carries the system organisation's.
|
|
func (b *boot) discoveryRule(ctx context.Context, owner org.OrgID) (config.Config, error) {
|
|
rule := b.cfgHolder.Get()
|
|
if owner == org.DefaultOrgID {
|
|
return rule, nil
|
|
}
|
|
_, _, err := b.controlPlane.Query(ctx, controlplanestore.SchedulingPolicyKind, func(value []byte) (err error) {
|
|
rule, err = controlplanestore.ApplyOrgScheduling(rule, value)
|
|
return err
|
|
})
|
|
return rule, err
|
|
}
|
|
|
|
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
|
|
}
|