Files
archie/internal/app/controlplane/forge_connections.go
T
sam c56caa45c2 feat(controlplane): the instance forge is its root connection
Layering reads forge-connections and takes the root connection for the
instance's own forge, keeping the file's webhook intake and secret, which are
instance infrastructure rather than an account. A document with no root
connection disables the forge instead of falling back to a credential the
operator may have withdrawn.
2026-10-17 03:07:09 +11:00

140 lines
4.9 KiB
Go

package controlplane
import (
"fmt"
"maps"
"slices"
"strings"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
// ForgeConnectionsKind is the resource holding an organisation's external
// forge connections. Its document is []config.ForgeConnection, the same shape
// the runtime resolves, so a stored connection and a resolved one cannot
// disagree about what a connection is.
const ForgeConnectionsKind = "forge-connections"
// rootConnectionID names the connection the file's [forge] section migrates
// into: the instance root's own account, used by root tasks.
const rootConnectionID = "default"
func forgeConnectionsDefinition() Definition {
return Definition{
Kind: ForgeConnectionsKind,
Title: "Forge connections",
ApplyMode: "live",
Document: []config.ForgeConnection{},
Seed: seedForgeConnections,
Validate: validateForgeConnections,
}
}
// seedForgeConnections migrates the file's forge configuration into
// connections: the root's [forge] becomes the root connection, and each
// identity that declared its own forge becomes that identity's connection.
// The permissions reproduce what the file already granted -- the root
// connection to root tasks, each identity's own forge to that identity -- so
// the migration neither widens nor narrows access. The file never recorded
// which external account a credential belongs to, so Account is seeded empty
// and is filled by a verification.
func seedForgeConnections(cfg config.Config) any {
out := []config.ForgeConnection{}
if c, ok := connectionFromForge(rootConnectionID, "Instance forge", cfg.Forge); ok {
// Root tasks act as the instance, not as an organisation execution
// identity, so they take this connection without a permit entry. An
// execution identity needs one, and the file granted it none.
out = append(out, c)
}
for _, id := range cfg.Identities {
c, ok := connectionFromForge("identity-"+id.Name, id.Name, id.Forge)
if !ok {
continue
}
// The file gave this identity its own credential and no limit on what
// it did with it; keeping that exact grant is what a migration owes.
c.Permits = map[string][]string{string(identity.StableID(id.Name)): config.Operations()}
out = append(out, c)
}
return out
}
// connectionFromForge reads one file forge section as a connection. A forge
// with no type is not configured and seeds nothing.
func connectionFromForge(id, name string, cfg config.Forge) (config.ForgeConnection, bool) {
if strings.TrimSpace(cfg.Type) == "" {
return config.ForgeConnection{}, false
}
return config.ForgeConnection{
ID: id, Name: name, Type: cfg.Type, Host: cfg.Host, Token: cfg.Token,
}, true
}
// rootConnection is the connection an instance's own tasks act through: the
// one the file's [forge] section migrated into.
func rootConnection(connections []config.ForgeConnection) (config.ForgeConnection, bool) {
for _, c := range connections {
if c.ID == rootConnectionID {
return c, true
}
}
return config.ForgeConnection{}, false
}
func validateForgeConnections(input []byte) error {
return validateAs(input, func(connections []config.ForgeConnection) error {
seen := make(map[string]struct{}, len(connections))
for _, c := range connections {
if err := validateForgeConnection(c); err != nil {
return err
}
if _, dup := seen[c.ID]; dup {
return fmt.Errorf("duplicate connection %q", c.ID)
}
seen[c.ID] = struct{}{}
}
return nil
})
}
func validateForgeConnection(c config.ForgeConnection) error {
// A slash would let an ID spell another resource's audit record key, the
// same reason resource keys refuse one.
if strings.TrimSpace(c.ID) == "" || strings.Contains(c.ID, "/") {
return fmt.Errorf("connection ID is required and may not contain %q", "/")
}
if strings.TrimSpace(c.Name) == "" {
return fmt.Errorf("connection %q needs a name", c.ID)
}
if strings.TrimSpace(c.Type) == "" {
return fmt.Errorf("connection %q needs a forge type", c.ID)
}
if strings.TrimSpace(c.Host) == "" {
return fmt.Errorf("connection %q needs a host", c.ID)
}
if (c.Token.Engine == "") != (c.Token.Key == "") {
return fmt.Errorf("connection %q: the credential reference needs both an engine and a key", c.ID)
}
for _, id := range slices.Sorted(maps.Keys(c.Permits)) {
operations := c.Permits[id]
if strings.TrimSpace(id) == "" {
return fmt.Errorf("connection %q: a permitted identity is empty", c.ID)
}
if len(operations) == 0 {
return fmt.Errorf("connection %q: identity %s is permitted nothing; remove the entry instead", c.ID, id)
}
seen := make(map[string]struct{}, len(operations))
for _, op := range operations {
if !config.ValidOperation(op) {
return fmt.Errorf("connection %q: identity %s: unknown operation %q (want clone, push or pr)", c.ID, id, op)
}
if _, dup := seen[op]; dup {
return fmt.Errorf("connection %q: identity %s lists %q twice", c.ID, id, op)
}
seen[op] = struct{}{}
}
}
return nil
}