fix(identity): use System as the one root identity and record who created every task (#1851)
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.
This commit is contained in:
36 files changed
+283
-189
No files matched your search
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
)
|
||||
|
||||
@@ -13,11 +14,10 @@ import (
|
||||
// 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.
|
||||
// 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 {
|
||||
Root []string `json:"root"`
|
||||
Identities map[string][]string `json:"identities,omitempty"`
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func identityGrantsDefinition() Definition {
|
||||
}
|
||||
|
||||
func seedIdentityGrants(cfg config.Config) any {
|
||||
doc := identityGrants{Root: cfg.GrantedCredentials, Identities: map[string][]string{}}
|
||||
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
|
||||
@@ -44,9 +44,6 @@ func seedIdentityGrants(cfg config.Config) any {
|
||||
|
||||
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")
|
||||
@@ -88,7 +85,7 @@ func layerCredentials(ctx context.Context, reader controlplanerpc.ResourceReader
|
||||
|
||||
// applyIdentityGrants replaces every identity's grants with the document's.
|
||||
func applyIdentityGrants(out *config.Config, doc identityGrants) {
|
||||
out.GrantedCredentials = doc.Root
|
||||
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 {
|
||||
|
||||
@@ -5,11 +5,12 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
)
|
||||
|
||||
// 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.
|
||||
// inherit System's (nil), and an empty list grants nothing.
|
||||
func TestIdentityGrantsReplaceTheFiles(t *testing.T) {
|
||||
fileList := []string{"from-file"}
|
||||
cfg := config.Config{
|
||||
@@ -21,15 +22,14 @@ func TestIdentityGrantsReplaceTheFiles(t *testing.T) {
|
||||
},
|
||||
}
|
||||
applyIdentityGrants(&cfg, identityGrants{
|
||||
Root: []string{"github"},
|
||||
Identities: map[string][]string{"listed": {"anthropic"}, "none": {}},
|
||||
Identities: map[string][]string{string(identity.SystemID): {"github"}, "listed": {"anthropic"}, "none": {}},
|
||||
})
|
||||
if !slices.Equal(cfg.GrantedCredentials, []string{"github"}) {
|
||||
t.Fatalf("root granted %v", cfg.GrantedCredentials)
|
||||
t.Fatalf("System granted %v", cfg.GrantedCredentials)
|
||||
}
|
||||
tests := []struct {
|
||||
identity string
|
||||
want []string // nil means inherit the root
|
||||
want []string // nil means inherit System
|
||||
}{
|
||||
{"listed", []string{"anthropic"}},
|
||||
{"unlisted", nil},
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
-- System is the one root identity. Tasks that stored '' for the root, the
|
||||
-- identity-grants "root" key and the connection permits of the identity the
|
||||
-- config's bot_user used to seed all move to System, so the poller's new
|
||||
-- identity matches the stored rows and a re-poll dedups.
|
||||
-- The old root is the first identity imported from the config.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
UPDATE tasks SET identity = '00000000-0000-5000-8000-000000000001' WHERE identity = '';
|
||||
ALTER TABLE tasks ADD CONSTRAINT tasks_identity_set CHECK (identity <> '');
|
||||
|
||||
UPDATE resources r
|
||||
SET value = convert_to(m.doc::text, 'UTF8')
|
||||
FROM (
|
||||
SELECT g.org_id, g.kind, (g.v - 'root') || jsonb_build_object('identities',
|
||||
CASE WHEN jsonb_typeof(g.v -> 'identities') = 'object' THEN g.v -> 'identities' ELSE '{}'::jsonb END
|
||||
|| jsonb_build_object('00000000-0000-5000-8000-000000000001',
|
||||
CASE WHEN jsonb_typeof(g.v -> 'root') = 'array' THEN g.v -> 'root' ELSE '[]'::jsonb END)) AS doc
|
||||
FROM (
|
||||
SELECT org_id, kind, convert_from(value, 'UTF8')::jsonb AS v FROM resources WHERE kind = 'identity-grants'
|
||||
) g
|
||||
WHERE g.v ? 'root'
|
||||
) m
|
||||
WHERE r.org_id = m.org_id AND r.kind = m.kind;
|
||||
|
||||
UPDATE resources r
|
||||
SET value = convert_to(m.doc::text, 'UTF8')
|
||||
FROM (
|
||||
SELECT f.org_id, f.kind, (
|
||||
SELECT COALESCE(jsonb_agg(
|
||||
CASE WHEN c -> 'permits' ? old.id THEN jsonb_set(c, '{permits}',
|
||||
((c -> 'permits') - old.id) || jsonb_build_object('00000000-0000-5000-8000-000000000001',
|
||||
(SELECT jsonb_agg(DISTINCT op) FROM jsonb_array_elements_text(
|
||||
COALESCE(c -> 'permits' -> '00000000-0000-5000-8000-000000000001', '[]'::jsonb) || (c -> 'permits' -> old.id)) op)))
|
||||
ELSE c END), '[]'::jsonb)
|
||||
FROM jsonb_array_elements(f.v) c
|
||||
) AS doc
|
||||
FROM (
|
||||
SELECT org_id, kind, convert_from(value, 'UTF8')::jsonb AS v FROM resources WHERE kind = 'forge-connections'
|
||||
) f,
|
||||
(
|
||||
SELECT identity_id AS id FROM identity_events
|
||||
WHERE request_id LIKE 'identity-import:%' AND identity_id <> '00000000-0000-5000-8000-000000000001'
|
||||
ORDER BY id LIMIT 1
|
||||
) old
|
||||
WHERE jsonb_typeof(f.v) = 'array'
|
||||
) m
|
||||
WHERE r.org_id = m.org_id AND r.kind = m.kind;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
ALTER TABLE tasks DROP CONSTRAINT tasks_identity_set;
|
||||
@@ -358,7 +358,7 @@ func (s *Store) EnsureAgentOrgMembership(ctx context.Context) error {
|
||||
// PrincipalFor assembles the access principal for one identity: the org it
|
||||
// serves and its memberships (internal/domain/access). An identity in no org
|
||||
// is the default org with no role, which the shipped role policies never
|
||||
// permit.
|
||||
// permit. System owns the default org without a membership row.
|
||||
func (s *Store) PrincipalFor(ctx context.Context, id identity.IdentityID) (access.Principal, error) {
|
||||
resolved, err := s.OrgForIdentity(ctx, id)
|
||||
if err != nil {
|
||||
@@ -385,6 +385,9 @@ func (s *Store) PrincipalFor(ctx context.Context, id identity.IdentityID) (acces
|
||||
if err != nil {
|
||||
return access.Principal{}, err
|
||||
}
|
||||
if id == identity.SystemID {
|
||||
memberships = access.SharedTokenOwner().Memberships
|
||||
}
|
||||
return access.Principal{IdentityID: id, Org: resolved, Memberships: memberships, Workspaces: workspaces}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -159,9 +159,16 @@ var (
|
||||
_ workflow.Store = (*Store)(nil)
|
||||
)
|
||||
|
||||
// errTaskIdentity refuses a task nobody created: every task records the
|
||||
// identity that created it.
|
||||
var errTaskIdentity = errors.New("task identity is required")
|
||||
|
||||
// EnqueueIssue inserts a new queued task for the issue; returns false if the
|
||||
// issue is already tracked (the idempotency key is owner/repo/number).
|
||||
func (s *Store) EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels, identity string) (bool, error) {
|
||||
if identity == "" {
|
||||
return false, errTaskIdentity
|
||||
}
|
||||
n, err := s.queries().EnqueueIssue(ctx, postgresdb.EnqueueIssueParams{
|
||||
Owner: owner, Repo: repo, IssueNumber: int64(number),
|
||||
Title: title, Body: body, Labels: labels, Identity: identity,
|
||||
@@ -179,6 +186,9 @@ func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, w
|
||||
}
|
||||
|
||||
func insertChatTask(ctx context.Context, q *postgresdb.Queries, owner, repo, title, body, wf, identity, origin, draft string, inputs map[string]any) (*workflow.Task, error) {
|
||||
if identity == "" {
|
||||
return nil, errTaskIdentity
|
||||
}
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -48,7 +48,7 @@ func TestIdentityStartIsAudited(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tasks := &auditTasks{failInsert: tt.failInsert}
|
||||
grants := &TaskGrants{Store: credentials{}}
|
||||
s := &taskGrantServer{server: &server{deps: Deps{Tasks: tasks, Grants: grants, RootIdentity: "root", Identities: executionIdentityRepo{}}}}
|
||||
s := &taskGrantServer{server: &server{deps: Deps{Tasks: tasks, Grants: grants, Identities: executionIdentityRepo{}}}}
|
||||
ctx := access.WithActor(context.Background(), "archied")
|
||||
_, err := s.RegisterTaskGrant(ctx, &pb.RegisterTaskGrantRequest{TaskId: 7, Token: token, LifetimeSeconds: 60})
|
||||
live, _ := grants.taskFor(ctx, token)
|
||||
|
||||
@@ -133,8 +133,6 @@ type Deps struct {
|
||||
// serves and its memberships. Optional: nil answers GetPrincipal with
|
||||
// codes.Unavailable.
|
||||
Principals access.PrincipalSource
|
||||
// RootIdentity is the identity a task with no identity name runs as.
|
||||
RootIdentity identity.IdentityID
|
||||
// Policies is the stored policy chain.
|
||||
// Optional: nil answers the access RPCs with codes.Unavailable. The reset
|
||||
// surface is deliberately not an RPC: `archie-state-store access reset` runs on the
|
||||
|
||||
@@ -275,11 +275,7 @@ func (s *taskGrantServer) ResolveTaskGrant(ctx context.Context, r *pb.ResolveTas
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unavailable, "execution identity unavailable")
|
||||
}
|
||||
route := string(principal)
|
||||
if principal == s.deps.RootIdentity {
|
||||
route = ""
|
||||
}
|
||||
return &pb.ResolveTaskGrantResponse{TaskId: id, ExecutionIdentity: route, PrincipalId: string(principal)}, nil
|
||||
return &pb.ResolveTaskGrantResponse{TaskId: id, ExecutionIdentity: string(principal), PrincipalId: string(principal)}, nil
|
||||
}
|
||||
|
||||
// RegisterTaskGrant creates fresh random material at the daemon and registers
|
||||
@@ -402,9 +398,6 @@ func (s *server) executionPrincipal(ctx context.Context, t *task.Task) (identity
|
||||
return "", err
|
||||
}
|
||||
id := identity.IdentityID(ref)
|
||||
if ref == "" {
|
||||
id = s.deps.RootIdentity
|
||||
}
|
||||
if s.deps.Identities == nil {
|
||||
return "", fmt.Errorf("identity authority unavailable")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user