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.
98 lines
3.1 KiB
Go
98 lines
3.1 KiB
Go
package controlplanestore
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
|
)
|
|
|
|
// IdentityGrantsKind is the resource naming the credential services each
|
|
// identity may use. Applied live on each launch.
|
|
const IdentityGrantsKind = "identity-grants"
|
|
|
|
// identityGrants is the document: per identity ID the credential services it
|
|
// may use. System's entry is the default a configured identity with none of
|
|
// its own inherits; an empty list grants none.
|
|
type identityGrants struct {
|
|
Identities map[string][]string `json:"identities,omitempty"`
|
|
}
|
|
|
|
func identityGrantsDefinition() Definition {
|
|
return Definition{
|
|
Kind: IdentityGrantsKind,
|
|
Title: "Identity grants",
|
|
ApplyMode: "live",
|
|
Document: identityGrants{},
|
|
Seed: seedIdentityGrants,
|
|
Validate: validateIdentityGrants,
|
|
}
|
|
}
|
|
|
|
func seedIdentityGrants(cfg config.Config) any {
|
|
doc := identityGrants{Identities: map[string][]string{string(identity.SystemID): cfg.GrantedCredentials}}
|
|
for _, id := range cfg.Identities {
|
|
if id.GrantedCredentials != nil {
|
|
doc.Identities[id.Name] = *id.GrantedCredentials
|
|
}
|
|
}
|
|
return doc
|
|
}
|
|
|
|
func validateIdentityGrants(input []byte) error {
|
|
return validateAs(input, func(doc identityGrants) error {
|
|
for name, services := range doc.Identities {
|
|
if strings.TrimSpace(name) == "" {
|
|
return fmt.Errorf("identity grants: an identity name is empty")
|
|
}
|
|
if err := validServices(name, services); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func validServices(owner string, services []string) error {
|
|
seen := make(map[string]bool, len(services))
|
|
for _, service := range services {
|
|
if strings.TrimSpace(service) == "" || seen[service] {
|
|
return fmt.Errorf("identity grants for %s: services must be non-empty and listed once", owner)
|
|
}
|
|
seen[service] = true
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// layerCredentials layers the credential bindings and the identity grants:
|
|
// each is its own resource, applies without a restart, and a stored value
|
|
// replaces the file's outright.
|
|
func layerCredentials(ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, out *config.Config) error {
|
|
if err := layerResource(ctx, reader, versions, CredentialBindingsKind, func(bindings []credentialBinding) error {
|
|
out.Containers.Credentials = credentialBindingsSettings(bindings)
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
return layerResource(ctx, reader, versions, IdentityGrantsKind, func(doc identityGrants) error {
|
|
applyIdentityGrants(out, doc)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// applyIdentityGrants replaces every identity's grants with the document's.
|
|
func applyIdentityGrants(out *config.Config, doc identityGrants) {
|
|
out.GrantedCredentials = doc.Identities[string(identity.SystemID)]
|
|
out.IdentityGrants = doc.Identities
|
|
for i := range out.Identities {
|
|
if services, ok := doc.Identities[out.Identities[i].Name]; ok {
|
|
out.Identities[i].GrantedCredentials = &services
|
|
} else {
|
|
out.Identities[i].GrantedCredentials = nil
|
|
}
|
|
}
|
|
}
|