From 6f4c03896a10cd286e2bbccfa226746d1fbf79a4 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 17 Oct 2026 17:15:39 +1100 Subject: [PATCH] fix(identity): emit RFC 4122 shaped stable IDs and migrate stored ones --- internal/domain/identity/identity.go | 11 +++- internal/domain/identity/stableid_test.go | 17 +++++ .../domain/workflow/shipped/feasibility.yaml | 2 +- .../domain/workflow/shipped/implement.yaml | 2 +- .../domain/workflow/shipped/pr-review.yaml | 2 +- .../domain/workflow/shipped/remediate.yaml | 2 +- internal/domain/workflow/shipped/tdd.yaml | 2 +- internal/domain/workflow/shipped/triage.yaml | 2 +- .../migrations/0051_stable_id_shape.sql | 62 +++++++++++++++++++ internal/webui/actor_names_test.go | 4 +- 10 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 internal/domain/identity/stableid_test.go create mode 100644 internal/infrastructure/postgres/migrations/0051_stable_id_shape.sql diff --git a/internal/domain/identity/identity.go b/internal/domain/identity/identity.go index 439bb8355..8231a6ed4 100644 --- a/internal/domain/identity/identity.go +++ b/internal/domain/identity/identity.go @@ -8,6 +8,8 @@ import ( "fmt" "strings" "time" + + "github.com/google/uuid" ) type ( @@ -82,9 +84,16 @@ func System() Identity { return Identity{ID: SystemID, Kind: KindSystem, DisplayName: "System", Lifecycle: LifecycleActive, Version: 1} } +// StableID derives a version 5 shaped UUID from a name. It keeps SHA-256 +// rather than uuid.NewSHA1 so IDs minted before the shape fix map to the +// current ones by a string rewrite (migration 0051). func StableID(legacyName string) IdentityID { sum := sha256.Sum256([]byte("archie.identity.v1\x00" + strings.ToLower(strings.TrimSpace(legacyName)))) - return IdentityID(fmt.Sprintf("%08x-%04x-5%03x-%04x-%012x", sum[:4], sum[4:6], sum[6:8], uint16(sum[8])<<8|uint16(sum[9])&0x3fff|0x8000, sum[10:16])) + var id uuid.UUID + copy(id[:], sum[:16]) + id[6] = id[6]&0x0f | 0x50 + id[8] = id[8]&0x3f | 0x80 + return IdentityID(id.String()) } // OperatorID is the person identity the dashboard acts as when no provider diff --git a/internal/domain/identity/stableid_test.go b/internal/domain/identity/stableid_test.go new file mode 100644 index 000000000..34c5f52ee --- /dev/null +++ b/internal/domain/identity/stableid_test.go @@ -0,0 +1,17 @@ +package identity + +import ( + "testing" + + "github.com/google/uuid" +) + +func TestStableIDIsRFC4122(t *testing.T) { + for _, name := range []string{"agent", "operator", "archie", "Some Bot", ""} { + got := string(StableID(name)) + id, err := uuid.Parse(got) + if err != nil || id.String() != got || id.Version() != 5 || id.Variant() != uuid.RFC4122 { + t.Errorf("StableID(%q) = %q, not an RFC 4122 v5 UUID (err %v)", name, got, err) + } + } +} diff --git a/internal/domain/workflow/shipped/feasibility.yaml b/internal/domain/workflow/shipped/feasibility.yaml index b6bf71457..cec71205e 100644 --- a/internal/domain/workflow/shipped/feasibility.yaml +++ b/internal/domain/workflow/shipped/feasibility.yaml @@ -1,5 +1,5 @@ id: feasibility -identity: 6f129e29-0e26-53246-99bd-74cb4496203b +identity: 6f129e29-0e26-5246-99bd-74cb4496203b steps: - type: repo.prepare - id: assess diff --git a/internal/domain/workflow/shipped/implement.yaml b/internal/domain/workflow/shipped/implement.yaml index 6d4b263fb..1a682e98e 100644 --- a/internal/domain/workflow/shipped/implement.yaml +++ b/internal/domain/workflow/shipped/implement.yaml @@ -1,5 +1,5 @@ id: implement -identity: 6f129e29-0e26-53246-99bd-74cb4496203b +identity: 6f129e29-0e26-5246-99bd-74cb4496203b steps: - type: repo.prepare - type: gate.repository diff --git a/internal/domain/workflow/shipped/pr-review.yaml b/internal/domain/workflow/shipped/pr-review.yaml index 9d26a18e1..efbb1dc52 100644 --- a/internal/domain/workflow/shipped/pr-review.yaml +++ b/internal/domain/workflow/shipped/pr-review.yaml @@ -1,5 +1,5 @@ id: pr-review -identity: 6f129e29-0e26-53246-99bd-74cb4496203b +identity: 6f129e29-0e26-5246-99bd-74cb4496203b inputs: pr_number: type: number diff --git a/internal/domain/workflow/shipped/remediate.yaml b/internal/domain/workflow/shipped/remediate.yaml index 991f90d14..12fccb228 100644 --- a/internal/domain/workflow/shipped/remediate.yaml +++ b/internal/domain/workflow/shipped/remediate.yaml @@ -1,5 +1,5 @@ id: remediate -identity: 6f129e29-0e26-53246-99bd-74cb4496203b +identity: 6f129e29-0e26-5246-99bd-74cb4496203b steps: - type: review.start-round - type: repo.prepare diff --git a/internal/domain/workflow/shipped/tdd.yaml b/internal/domain/workflow/shipped/tdd.yaml index 892da32f0..938c4771e 100644 --- a/internal/domain/workflow/shipped/tdd.yaml +++ b/internal/domain/workflow/shipped/tdd.yaml @@ -1,5 +1,5 @@ id: tdd -identity: 6f129e29-0e26-53246-99bd-74cb4496203b +identity: 6f129e29-0e26-5246-99bd-74cb4496203b steps: - type: repo.prepare - type: gate.repository diff --git a/internal/domain/workflow/shipped/triage.yaml b/internal/domain/workflow/shipped/triage.yaml index a3074bc37..3dad89ed5 100644 --- a/internal/domain/workflow/shipped/triage.yaml +++ b/internal/domain/workflow/shipped/triage.yaml @@ -1,5 +1,5 @@ id: triage -identity: 6f129e29-0e26-53246-99bd-74cb4496203b +identity: 6f129e29-0e26-5246-99bd-74cb4496203b steps: - type: repo.prepare - id: classify diff --git a/internal/infrastructure/postgres/migrations/0051_stable_id_shape.sql b/internal/infrastructure/postgres/migrations/0051_stable_id_shape.sql new file mode 100644 index 000000000..b092cbc8c --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0051_stable_id_shape.sql @@ -0,0 +1,62 @@ +-- Name-derived identity IDs used to carry a 5-character third group and an +-- unmasked variant nibble. The current form drops the extra character and +-- clears bit 2 of the variant nibble; this rewrites every stored copy. + +-- +goose Up +-- +goose StatementBegin +CREATE FUNCTION fix_stable_ids(s text) RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + m text[]; +BEGIN + LOOP + m := regexp_match(s, '(([0-9a-f]{8}-[0-9a-f]{4}-5)[0-9a-f]([0-9a-f]{3}-)([89a-f])([0-9a-f]{3}-[0-9a-f]{12}))'); + EXIT WHEN m IS NULL; + s := replace(s, m[1], m[2] || m[3] || translate(m[4], 'cdef', '89ab') || m[5]); + END LOOP; + RETURN s; +END $$; +-- +goose StatementEnd + +-- +goose StatementBegin +DO $$ +DECLARE + pat constant text := '[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{4}-[89a-f][0-9a-f]{3}-[0-9a-f]{12}'; + c record; +BEGIN + -- Referencing rows need the new identity to exist before they move to it. + INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at) + SELECT fix_stable_ids(id), kind, display_name, lifecycle, version, created_at, updated_at + FROM identities WHERE id ~ pat; + + FOR c IN + SELECT col.table_name, col.column_name, col.data_type + FROM information_schema.columns col + JOIN information_schema.tables t USING (table_schema, table_name) + WHERE col.table_schema = current_schema() AND t.table_type = 'BASE TABLE' + AND col.data_type IN ('text', 'character varying', 'json', 'jsonb', 'bytea') + AND col.is_generated = 'NEVER' + AND NOT (col.table_name = 'identities' AND col.column_name = 'id') + AND col.table_name NOT LIKE 'goose%' + LOOP + IF c.data_type = 'bytea' THEN + -- Only the resource documents are text; other bytea is binary. + CONTINUE WHEN c.table_name NOT IN ('resources', 'resource_history'); + EXECUTE format('UPDATE %I SET %I = convert_to(fix_stable_ids(convert_from(%I, ''UTF8'')), ''UTF8'') WHERE convert_from(%I, ''UTF8'') ~ %L', + c.table_name, c.column_name, c.column_name, c.column_name, pat); + ELSIF c.data_type IN ('json', 'jsonb') THEN + EXECUTE format('UPDATE %I SET %I = fix_stable_ids(%I::text)::%s WHERE %I::text ~ %L', + c.table_name, c.column_name, c.column_name, c.data_type, c.column_name, pat); + ELSE + EXECUTE format('UPDATE %I SET %I = fix_stable_ids(%I) WHERE %I ~ %L', + c.table_name, c.column_name, c.column_name, c.column_name, pat); + END IF; + END LOOP; + + DELETE FROM identities WHERE id ~ pat; +END $$; +-- +goose StatementEnd + +DROP FUNCTION fix_stable_ids(text); + +-- +goose Down +SELECT 1; diff --git a/internal/webui/actor_names_test.go b/internal/webui/actor_names_test.go index 98a8beb07..4c0f5c1bd 100644 --- a/internal/webui/actor_names_test.go +++ b/internal/webui/actor_names_test.go @@ -10,7 +10,7 @@ import ( type namedActors struct{ identity.Repository } func (namedActors) Get(_ context.Context, id identity.IdentityID) (identity.Identity, error) { - if id == "11111111-1111-4111-8111-111111111111" || id == "d898c8c8-47f4-57d6c-945a-2e7dc98ca1c1" { + if id == "11111111-1111-4111-8111-111111111111" || id == "d898c8c8-47f4-5d6c-945a-2e7dc98ca1c1" { return identity.Identity{ID: id, DisplayName: "Sam"}, nil } return identity.Identity{}, identity.ErrNotFound @@ -20,7 +20,7 @@ func TestAuditActorNames(t *testing.T) { name := (&Server{Identities: namedActors{}}).actorNames(t.Context()) for _, tc := range []struct{ id, want string }{ {"11111111-1111-4111-8111-111111111111", "Sam"}, - {"d898c8c8-47f4-57d6c-945a-2e7dc98ca1c1", "Sam"}, + {"d898c8c8-47f4-5d6c-945a-2e7dc98ca1c1", "Sam"}, {"22222222-2222-4222-8222-222222222222", "Unavailable identity"}, {"system:migration", "system:migration"}, } {