The seeded root connection was called "Instance forge", which names two archie concepts an operator never chose. Name it for the host it points at instead.
156 lines
5.5 KiB
Go
156 lines
5.5 KiB
Go
package controlplane
|
|
|
|
import (
|
|
"fmt"
|
|
"maps"
|
|
"net/url"
|
|
"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, connectionDisplayName(cfg.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
|
|
}
|
|
|
|
// connectionDisplayName is the label an operator sees, so it names the thing
|
|
// that tells one connection from another -- the host it points at -- rather
|
|
// than the archie concept it was migrated from. The file never named the
|
|
// forge, so there is nothing better to carry over.
|
|
func connectionDisplayName(cfg config.Forge) string {
|
|
host := strings.TrimSpace(cfg.Host)
|
|
if u, err := url.Parse(host); err == nil && u.Hostname() != "" {
|
|
host = u.Hostname()
|
|
}
|
|
if host != "" {
|
|
return host
|
|
}
|
|
return strings.TrimSpace(cfg.Type)
|
|
}
|
|
|
|
// 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
|
|
}
|