feat(controlplane): identity credential grants edited from the dashboard and applied live
This commit is contained in:
1 parent
cc5e0076af
commit
06b8aeebeb
8 files changed
+298
-70
No files matched your search
@@ -264,6 +264,8 @@ var runtimeResourceKinds = []string{
|
||||
controlplane.ReviewSettingsKind,
|
||||
controlplane.ChannelSettingsKind,
|
||||
controlplane.ContainerRuntimePoliciesKind,
|
||||
controlplane.CredentialBindingsKind,
|
||||
controlplane.IdentityGrantsKind,
|
||||
}
|
||||
|
||||
// startRuntimeResourceWatches keeps a watch per live kind established for the
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
// Schemas are not declared here: each Definition names its document type, and
|
||||
// Definition.Descriptor derives the JSON Schema from it (internal/app/controlplane/schema.go).
|
||||
func builtinDefinitions(steps workflow.StepRegistry) []Definition {
|
||||
definitions := []Definition{workflowDefinition(), workflowDefinitionsDefinition(steps), workflowEnablementDefinition(), personaDefinition(), scheduleDefinition(), agentProfileDefinition(), playbooksDefinition(), skillsDefinition(), credentialBindingsDefinition(), reviewSettingsDefinition()}
|
||||
definitions := []Definition{workflowDefinition(), workflowDefinitionsDefinition(steps), workflowEnablementDefinition(), personaDefinition(), scheduleDefinition(), agentProfileDefinition(), playbooksDefinition(), skillsDefinition(), credentialBindingsDefinition(), identityGrantsDefinition(), reviewSettingsDefinition()}
|
||||
definitions = append(definitions, modelDefinitions()...)
|
||||
definitions = append(definitions, operationalDefinitions()...)
|
||||
return definitions
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package controlplane
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"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: the root identity's services, and per named
|
||||
// identity a list that replaces the root's. An identity with no entry
|
||||
// inherits the root's; an empty list grants none.
|
||||
type identityGrants struct {
|
||||
Root []string `json:"root"`
|
||||
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{Root: cfg.GrantedCredentials, Identities: map[string][]string{}}
|
||||
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 {
|
||||
if err := validServices("root", doc.Root); err != nil {
|
||||
return err
|
||||
}
|
||||
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(value []byte) error {
|
||||
var bindings []credentialBinding
|
||||
if err := json.Unmarshal(value, &bindings); err != nil {
|
||||
return err
|
||||
}
|
||||
out.Containers.Credentials = credentialBindingsSettings(bindings)
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return layerResource(ctx, reader, versions, IdentityGrantsKind, func(value []byte) error {
|
||||
var doc identityGrants
|
||||
if err := json.Unmarshal(value, &doc); err != nil {
|
||||
return err
|
||||
}
|
||||
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.Root
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package controlplane
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
// TestIdentityGrantsReplaceTheFiles pins that the stored grants replace the
|
||||
// file's: a listed identity gets exactly its list, an unlisted one is left to
|
||||
// inherit the root's (nil), and an empty list grants nothing.
|
||||
func TestIdentityGrantsReplaceTheFiles(t *testing.T) {
|
||||
fileList := []string{"from-file"}
|
||||
cfg := config.Config{
|
||||
GrantedCredentials: []string{"file-root"},
|
||||
Identities: []config.IdentityConfig{
|
||||
{Name: "listed", GrantedCredentials: &fileList},
|
||||
{Name: "unlisted", GrantedCredentials: &fileList},
|
||||
{Name: "none"},
|
||||
},
|
||||
}
|
||||
applyIdentityGrants(&cfg, identityGrants{
|
||||
Root: []string{"github"},
|
||||
Identities: map[string][]string{"listed": {"anthropic"}, "none": {}},
|
||||
})
|
||||
if !slices.Equal(cfg.GrantedCredentials, []string{"github"}) {
|
||||
t.Fatalf("root granted %v", cfg.GrantedCredentials)
|
||||
}
|
||||
tests := []struct {
|
||||
identity string
|
||||
want []string // nil means inherit the root
|
||||
}{
|
||||
{"listed", []string{"anthropic"}},
|
||||
{"unlisted", nil},
|
||||
{"none", []string{}},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
t.Run(tt.identity, func(t *testing.T) {
|
||||
got := cfg.Identities[i].GrantedCredentials
|
||||
if (got == nil) != (tt.want == nil) || (got != nil && !slices.Equal(*got, tt.want)) {
|
||||
t.Fatalf("granted %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -213,19 +213,8 @@ func runtimeToolConfigFrom(ctx context.Context, reader controlplanerpc.ResourceR
|
||||
}); err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
// CredentialBindingsKind is likewise its own resource: a binding applies
|
||||
// without a restart, and a stored value replaces the file's outright.
|
||||
if err := layerResource(ctx, reader, versions, CredentialBindingsKind, func(value []byte) error {
|
||||
var bindings []credentialBinding
|
||||
if err := json.Unmarshal(value, &bindings); err != nil {
|
||||
return err
|
||||
}
|
||||
out.Containers.Credentials = credentialBindingsSettings(bindings)
|
||||
return nil
|
||||
}); err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
return out, versions, nil
|
||||
err := layerCredentials(ctx, reader, versions, &out)
|
||||
return out, versions, err
|
||||
}
|
||||
|
||||
// layerResource decodes a stored resource and records its version. A kind
|
||||
|
||||
Reference in new issue
Block a user