fix(identity): emit RFC 4122 shaped stable IDs and migrate stored ones
This commit is contained in:
10 files changed
+97
-9
No files matched your search
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
id: feasibility
|
||||
identity: 6f129e29-0e26-53246-99bd-74cb4496203b
|
||||
identity: 6f129e29-0e26-5246-99bd-74cb4496203b
|
||||
steps:
|
||||
- type: repo.prepare
|
||||
- id: assess
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
id: pr-review
|
||||
identity: 6f129e29-0e26-53246-99bd-74cb4496203b
|
||||
identity: 6f129e29-0e26-5246-99bd-74cb4496203b
|
||||
inputs:
|
||||
pr_number:
|
||||
type: number
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
id: triage
|
||||
identity: 6f129e29-0e26-53246-99bd-74cb4496203b
|
||||
identity: 6f129e29-0e26-5246-99bd-74cb4496203b
|
||||
steps:
|
||||
- type: repo.prepare
|
||||
- id: classify
|
||||
|
||||
@@ -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;
|
||||
@@ -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"},
|
||||
} {
|
||||
|
||||
Reference in new issue
Block a user