135 lines
4.7 KiB
Go
135 lines
4.7 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
|
"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"
|
|
)
|
|
|
|
// 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)
|
|
}
|
|
|
|
func (b *boot) organisations(ctx context.Context) []orgAgents {
|
|
directory, _ := b.stateStore.(orgDirectory)
|
|
store := scheduleResourceStore{orgs: directory, log: b.log}
|
|
var out []orgAgents
|
|
for _, owner := range store.owners(ctx) {
|
|
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.Warn("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.Warn("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, controlplane.RepositoryPoliciesKind, func(value []byte) error { return json.Unmarshal(value, &repos) }); err != nil || !found {
|
|
log.Warn("issue discovery: repositories unavailable", "err", err)
|
|
continue
|
|
}
|
|
connections, err := b.repositoryConnections(octx)
|
|
if err != nil {
|
|
log.Warn("issue discovery: connections unavailable", "err", err)
|
|
continue
|
|
}
|
|
byConnection := map[string][]config.Repo{}
|
|
for _, repo := range repos {
|
|
if repo.Connection == "" {
|
|
log.Warn("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.Warn("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.Warn("issue discovery: no agent may clone through this connection")
|
|
return daemon.IssueSource{}, false
|
|
}
|