Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
feat: deploy Metabase for Kubernetes state reporting (#6663)
r33drichardsf0f829b3826b
diff --git a/libs/fleet/backend/config/config.go b/libs/fleet/backend/config/config.goindex 6026218046..7e93ab1811 100644--- a/libs/fleet/backend/config/config.go+++ b/libs/fleet/backend/config/config.go@@ -92,12 +92,9 @@ type StripeConfiguration struct {// GitHub OIDC trust policies). An empty URL disables those routes (503),// keeping the backend bootable without a database — see CUA-675.type DatabaseConfiguration struct {- URL string // DATABASE_URL — migration/control and GitHub trust-policy storage+ URL string // DATABASE_URL — application database and GitHub trust-policy storageStateQueryDSN string // STATE_QUERY_DATABASE_DSN — tenant query connection optionsStateQueryTenantPassword string // STATE_QUERY_TENANT_PASSWORD — shared tenant query login password- StateWriterURL string // STATE_WRITER_DATABASE_URL — restricted projector writer- StateExporterURL string // STATE_EXPORTER_DATABASE_URL — restricted outbox exporter- StateRoleAdminURL string // STATE_ROLE_ADMIN_DATABASE_URL — fixed-role bootstrap only}type MetricsConfiguration struct {@@ -144,9 +141,6 @@ var specs = []flagSpec{{"database.url", "database-url", "DATABASE_URL", "", "Postgres URL for trust-policy storage (enables /api/github-trust-policies)"},{"database.state-query-dsn", "state-query-database-dsn", "STATE_QUERY_DATABASE_DSN", "", "Postgres connection options for tenant state queries"},{"database.state-query-tenant-password", "state-query-tenant-password", "STATE_QUERY_TENANT_PASSWORD", "", "Shared password for tenant query login roles"},- {"database.state-writer-url", "state-writer-database-url", "STATE_WRITER_DATABASE_URL", "", "Restricted Postgres URL for Kubernetes state projection"},- {"database.state-exporter-url", "state-exporter-database-url", "STATE_EXPORTER_DATABASE_URL", "", "Restricted Postgres URL for Kubernetes state history export"},- {"database.state-role-admin-url", "state-role-admin-database-url", "STATE_ROLE_ADMIN_DATABASE_URL", "", "Restricted Postgres URL for Kubernetes tenant-role reconciliation"},{"stripe.secret-key", "stripe-secret-key", "STRIPE_SECRET_KEY", "", "Stripe secret key (server-only)"},{"stripe.webhook-secret", "stripe-webhook-secret", "STRIPE_WEBHOOK_SECRET", "", "Stripe webhook signing secret"},{"stripe.checkout-success-url", "stripe-checkout-success-url", "STRIPE_CHECKOUT_SUCCESS_URL", "", "Stripe Checkout success redirect URL"},@@ -236,9 +230,6 @@ func LoadConfig() (*Configuration, error) {URL: viper.GetString("database.url"),StateQueryDSN: viper.GetString("database.state-query-dsn"),StateQueryTenantPassword: viper.GetString("database.state-query-tenant-password"),- StateWriterURL: viper.GetString("database.state-writer-url"),- StateExporterURL: viper.GetString("database.state-exporter-url"),- StateRoleAdminURL: viper.GetString("database.state-role-admin-url"),},Stripe: StripeConfiguration{SecretKey: viper.GetString("stripe.secret-key"),diff --git a/libs/fleet/backend/config/config_test.go b/libs/fleet/backend/config/config_test.goindex e4693c750a..100401476e 100644--- a/libs/fleet/backend/config/config_test.go+++ b/libs/fleet/backend/config/config_test.go@@ -168,9 +168,6 @@ func TestLoadConfig_StateDatabaseURLs(t *testing.T) {t.Setenv("DATABASE_URL", "postgres://admin@db/cyclops")t.Setenv("STATE_QUERY_DATABASE_DSN", "postgres://db/cyclops?sslmode=require")t.Setenv("STATE_QUERY_TENANT_PASSWORD", "tenant-password")- t.Setenv("STATE_WRITER_DATABASE_URL", "postgres://k8s_state_writer@db/cyclops")- t.Setenv("STATE_EXPORTER_DATABASE_URL", "postgres://k8s_state_exporter@db/cyclops")- t.Setenv("STATE_ROLE_ADMIN_DATABASE_URL", "postgres://k8s_role_admin@db/cyclops")RegisterFlags(pflag.NewFlagSet("state-database-test", pflag.ContinueOnError))cfg, err := LoadConfig()@@ -183,13 +180,4 @@ func TestLoadConfig_StateDatabaseURLs(t *testing.T) {if got, want := cfg.Database.StateQueryTenantPassword, "tenant-password"; got != want {t.Fatalf("Database.StateQueryTenantPassword = %q, want %q", got, want)}- if got, want := cfg.Database.StateWriterURL, "postgres://k8s_state_writer@db/cyclops"; got != want {- t.Fatalf("Database.StateWriterURL = %q, want %q", got, want)- }- if got, want := cfg.Database.StateExporterURL, "postgres://k8s_state_exporter@db/cyclops"; got != want {- t.Fatalf("Database.StateExporterURL = %q, want %q", got, want)- }- if got, want := cfg.Database.StateRoleAdminURL, "postgres://k8s_role_admin@db/cyclops"; got != want {- t.Fatalf("Database.StateRoleAdminURL (fixed-role bootstrap only) = %q, want %q", got, want)- }}diff --git a/libs/fleet/backend/database/migrations/000001_initial_schema.sql b/libs/fleet/backend/database/migrations/000001_initial_schema.sqlindex edf15ab7a0..a2f22c16cc 100644--- a/libs/fleet/backend/database/migrations/000001_initial_schema.sql+++ b/libs/fleet/backend/database/migrations/000001_initial_schema.sql@@ -326,8 +326,8 @@ RESET ROLE;-- 6. Runtime roles may inspect, but never modify, migration state.REVOKE ALL ON SCHEMA cyclops_migrations FROM PUBLIC;REVOKE ALL ON TABLE cyclops_migrations.applied_migrations FROM PUBLIC;-GRANT USAGE ON SCHEMA cyclops_migrations TO cyclops_app, k8s_state_writer, k8s_state_exporter, k8s_role_admin, k8s_metabase;-GRANT SELECT ON TABLE cyclops_migrations.applied_migrations TO cyclops_app, k8s_state_writer, k8s_state_exporter, k8s_role_admin, k8s_metabase;+GRANT USAGE ON SCHEMA cyclops_migrations TO cyclops_app, k8s_state_writer, k8s_state_exporter, k8s_role_admin;+GRANT SELECT ON TABLE cyclops_migrations.applied_migrations TO cyclops_app, k8s_state_writer, k8s_state_exporter, k8s_role_admin;-- 7. Fail closed if reporting reaches state or application data.SET LOCAL ROLE k8s_state_owner;diff --git a/libs/fleet/backend/database/migrator.go b/libs/fleet/backend/database/migrator.goindex 7bd894a034..dab6c57722 100644--- a/libs/fleet/backend/database/migrator.go+++ b/libs/fleet/backend/database/migrator.go@@ -71,7 +71,7 @@ type staticRoleContract struct {createRole boolcreateDB boolconnectionLimit int- validUntil string+ validUntil staticRoleValidUntil}type staticRoleAttributes struct {@@ -140,14 +140,6 @@ var expectedCredentialRoles = map[string]string{"metabase": "k8s_metabase",}-const createAppliedMigrationsTableStatement = `create table if not exists cyclops_migrations.applied_migrations (- application_order bigint generated always as identity unique,- version bigint primary key,- filename text not null unique,- sha256 text not null,- applied_at timestamptz not null default clock_timestamp()-)`-const selectAppliedMigrationsStatement = `select version, filename, sha256 from cyclops_migrations.applied_migrations order by application_order`const insertAppliedMigrationStatement = `insert into cyclops_migrations.applied_migrations (version, filename, sha256) values ($1, $2, $3)`@@ -323,24 +315,20 @@ func Run(ctx context.Context, config Config) (runErr error) {}defer transaction.Rollback(ctx)- ledgerStatements := []string{- `select pg_advisory_xact_lock(hashtext('cyclops-database-migrations'))`,- `create schema if not exists cyclops_migrations`,- createAppliedMigrationsTableStatement,+ if err := validateNoPublicSecurityDefiner(ctx, transaction); err != nil {+ return err}- for index, statement := range ledgerStatements {- started := time.Now()- if _, err := transaction.Exec(ctx, statement); err != nil {- return fmt.Errorf("prepare migration ledger: %w", err)- }- if index == 0 {- slog.Info("database migration advisory lock acquired",- "database_host", connectionConfig.Host,- "database_name", connectionConfig.Database,- "duration_ms", time.Since(started).Milliseconds(),- )- }++ ddl := newRuntimeDDL(transaction)+ started := time.Now()+ if err := ddl.ensureMigrationLedger(ctx); err != nil {+ return fmt.Errorf("prepare migration ledger: %w", err)}+ slog.Info("database migration advisory lock acquired",+ "database_host", connectionConfig.Host,+ "database_name", connectionConfig.Database,+ "duration_ms", time.Since(started).Milliseconds(),+ )rows, err := transaction.Query(ctx, selectAppliedMigrationsStatement)if err != nil {@@ -407,6 +395,9 @@ func Run(ctx context.Context, config Config) (runErr error) {if err != nil {return err}+ if err := reconcileReportingBoundary(ctx, transaction); err != nil {+ return err+ }credentialEvents, err := reconcilePasswords(ctx, transaction, credentials)if err != nil {return err@@ -482,24 +473,24 @@ func parseCredentialURLs(urls CredentialURLs) ([]credential, error) {func staticRoleContracts() []staticRoleContract {return []staticRoleContract{- {role: "cyclops_app", login: true, connectionLimit: -1, validUntil: "infinity"},- {role: "k8s_state_owner", inherit: true, connectionLimit: -1, validUntil: "infinity"},- {role: "k8s_state_writer", login: true, connectionLimit: -1, validUntil: "infinity"},- {role: "k8s_state_exporter", login: true, connectionLimit: -1, validUntil: "infinity"},- {role: "k8s_query_tenant", inherit: true, connectionLimit: -1, validUntil: "infinity"},- {role: "k8s_query_admin", inherit: true, connectionLimit: -1, validUntil: "infinity"},- {role: "k8s_role_admin", login: true, createRole: true, connectionLimit: -1, validUntil: "infinity"},- {role: "k8s_reporting_owner", inherit: true, connectionLimit: -1, validUntil: "infinity"},- {role: "k8s_metabase", login: true, connectionLimit: -1, validUntil: "infinity"},+ {role: "cyclops_app", login: true, connectionLimit: -1, validUntil: staticRoleValidUntilInfinity},+ {role: "k8s_state_owner", inherit: true, connectionLimit: -1, validUntil: staticRoleValidUntilInfinity},+ {role: "k8s_state_writer", login: true, connectionLimit: -1, validUntil: staticRoleValidUntilInfinity},+ {role: "k8s_state_exporter", login: true, connectionLimit: -1, validUntil: staticRoleValidUntilInfinity},+ {role: "k8s_query_tenant", inherit: true, connectionLimit: -1, validUntil: staticRoleValidUntilInfinity},+ {role: "k8s_query_admin", inherit: true, connectionLimit: -1, validUntil: staticRoleValidUntilInfinity},+ {role: "k8s_role_admin", login: true, createRole: true, connectionLimit: -1, validUntil: staticRoleValidUntilInfinity},+ {role: "k8s_reporting_owner", inherit: true, connectionLimit: -1, validUntil: staticRoleValidUntilInfinity},+ {role: "k8s_metabase", login: true, connectionLimit: -1, validUntil: staticRoleValidUntilInfinity},}}-func staticRoleSettingsContracts() map[string]map[string]string {- return map[string]map[string]string{+func staticRoleSettingsContracts() map[string]staticRoleSettingsContract {+ return map[string]staticRoleSettingsContract{"k8s_metabase": {- "default_transaction_read_only": "on",- "statement_timeout": "20000ms",- "idle_in_transaction_session_timeout": "20000ms",+ staticRoleSettingDefaultTransactionReadOnly: "on",+ staticRoleSettingStatementTimeout: "20000ms",+ staticRoleSettingIdleInTransactionSessionTimeout: "20000ms",},}}@@ -513,50 +504,6 @@ func staticMembershipContracts(migrationOwner string) []staticMembershipContract}}-func postgresBoolean(value bool) string {- if value {- return "TRUE"- }- return "FALSE"-}--func staticRoleAlterClauses(contract staticRoleContract, attributes staticRoleAttributes) (string, error) {- unsafeAttributes := make([]string, 0, 4)- if attributes.createDB {- unsafeAttributes = append(unsafeAttributes, "rolcreatedb=true")- }- if attributes.super {- unsafeAttributes = append(unsafeAttributes, "rolsuper=true")- }- if attributes.replication {- unsafeAttributes = append(unsafeAttributes, "rolreplication=true")- }- if attributes.bypassRLS {- unsafeAttributes = append(unsafeAttributes, "rolbypassrls=true")- }- if len(unsafeAttributes) != 0 {- return "", fmt.Errorf("static role %s has unsafe privileged drift: %s; the migration owner cannot safely repair these attributes", contract.role, strings.Join(unsafeAttributes, ", "))- }-- clauses := make([]string, 0, 6)- if attributes.login != contract.login {- clauses = append(clauses, map[bool]string{true: "LOGIN", false: "NOLOGIN"}[contract.login])- }- if attributes.inherit != contract.inherit {- clauses = append(clauses, map[bool]string{true: "INHERIT", false: "NOINHERIT"}[contract.inherit])- }- if attributes.createRole != contract.createRole {- clauses = append(clauses, map[bool]string{true: "CREATEROLE", false: "NOCREATEROLE"}[contract.createRole])- }- if attributes.connectionLimit != contract.connectionLimit {- clauses = append(clauses, "CONNECTION LIMIT "+strconv.Itoa(contract.connectionLimit))- }- if attributes.validUntil != contract.validUntil {- clauses = append(clauses, "VALID UNTIL '"+contract.validUntil+"'")- }- return strings.Join(clauses, " "), nil-}-func readStaticRoleAttributes(ctx context.Context, transaction pgx.Tx, role string) (staticRoleAttributes, error) {var attributes staticRoleAttributeserr := transaction.QueryRow(ctx, `@@ -579,6 +526,246 @@ func readStaticRoleAttributes(ctx context.Context, transaction pgx.Tx, role strireturn attributes, nil}+type reportingACL struct {+ object reportingObject+ owner string+ privilege reportingPrivilege+ grantee string+ grantor string+ grantable bool+}++func reconcileReportingBoundary(ctx context.Context, transaction pgx.Tx) error {+ if err := validateReportingBoundary(ctx, transaction); err != nil {+ return err+ }++ var migrationOwner string+ if err := transaction.QueryRow(ctx, `select current_user`).Scan(&migrationOwner); err != nil {+ return fmt.Errorf("read migration role for reporting ACL reconciliation: %w", err)+ }+ acls, err := readReportingACLs(ctx, transaction)+ if err != nil {+ return err+ }+ for _, acl := range acls {+ if isExpectedReportingACL(acl) {+ continue+ }+ if !isSafeReportingACLAuthority(acl, migrationOwner) {+ return fmt.Errorf("unsafe reporting ACL on %s: privilege=%s grantee=%s grantor=%s owner=%s; migration authority cannot safely revoke this grant", reportingObjectDescription(acl.object), acl.privilege.sqlName(), acl.grantee, acl.grantor, acl.owner)+ }+ }++ for _, acl := range acls {+ if isExpectedReportingACL(acl) {+ continue+ }+ if err := newRuntimeDDL(transaction).revokeReportingACL(ctx, acl); err != nil {+ return err+ }+ }+ for _, acl := range expectedReportingACLs() {+ if err := newRuntimeDDL(transaction).grantReportingACL(ctx, acl); err != nil {+ return fmt.Errorf("reconcile reporting ACLs: %w", err)+ }+ }++ finalACLs, err := readReportingACLs(ctx, transaction)+ if err != nil {+ return err+ }+ for _, acl := range finalACLs {+ if !isExpectedReportingACL(acl) {+ return fmt.Errorf("reporting ACL contract drift remains on %s: privilege=%s grantee=%s grantor=%s owner=%s", reportingObjectDescription(acl.object), acl.privilege.sqlName(), acl.grantee, acl.grantor, acl.owner)+ }+ }+ for _, expected := range expectedReportingACLs() {+ if !containsReportingACL(finalACLs, expected) {+ return fmt.Errorf("reporting ACL contract is missing %s on %s for %s", expected.privilege.sqlName(), reportingObjectDescription(expected.object), expected.grantee)+ }+ }+ return nil+}++func validateReportingBoundary(ctx context.Context, transaction pgx.Tx) error {+ var schemaOwner string+ if err := transaction.QueryRow(ctx, `select nspowner::regrole::text from pg_namespace where nspname = 'k8s_reporting'`).Scan(&schemaOwner); err != nil {+ return fmt.Errorf("read reporting schema: %w", err)+ }+ if schemaOwner != "k8s_reporting_owner" {+ return fmt.Errorf("reporting schema owner is %s, want k8s_reporting_owner", schemaOwner)+ }++ var relationKind, viewOwner string+ if err := transaction.QueryRow(ctx, `+ select relation.relkind::text, relation.relowner::regrole::text+ from pg_class as relation+ join pg_namespace as namespace on namespace.oid = relation.relnamespace+ where namespace.nspname = 'k8s_reporting' and relation.relname = 'current_resources'`).Scan(&relationKind, &viewOwner); err != nil {+ return fmt.Errorf("read reporting relation: %w", err)+ }+ if relationKind != "v" {+ return fmt.Errorf("reporting relation current_resources has kind %s, want view", relationKind)+ }+ if viewOwner != "k8s_reporting_owner" {+ return fmt.Errorf("reporting view owner is %s, want k8s_reporting_owner", viewOwner)+ }+ return nil+}++func readReportingACLs(ctx context.Context, transaction pgx.Tx) ([]reportingACL, error) {+ rows, err := transaction.Query(ctx, `+ select object_type, schema_name, object_name, routine_oid, owner_name, privilege_type, grantee_name, grantor_name, is_grantable+ from (+ select+ 'schema'::text as object_type,+ namespace.nspname as schema_name,+ ''::text as object_name,+ 0::oid as routine_oid,+ namespace.nspowner::regrole::text as owner_name,+ acl.privilege_type,+ case when acl.grantee = 0 then 'PUBLIC' else acl.grantee::regrole::text end as grantee_name,+ case when acl.grantor = 0 then 'PUBLIC' else acl.grantor::regrole::text end as grantor_name,+ acl.is_grantable+ from pg_namespace as namespace+ join lateral aclexplode(namespace.nspacl) as acl on true+ where namespace.nspname !~ '^pg_'+ and namespace.nspname <> 'information_schema'+ and acl.grantee <> namespace.nspowner+ and (acl.grantee in ('k8s_reporting_owner'::regrole, 'k8s_metabase'::regrole)+ or (namespace.nspname = 'k8s_reporting' and acl.grantee <> namespace.nspowner))+ union all+ select+ case when relation.relkind = 'S' then 'sequence' else 'relation' end,+ namespace.nspname,+ relation.relname,+ 0::oid,+ relation.relowner::regrole::text,+ acl.privilege_type,+ case when acl.grantee = 0 then 'PUBLIC' else acl.grantee::regrole::text end,+ case when acl.grantor = 0 then 'PUBLIC' else acl.grantor::regrole::text end,+ acl.is_grantable+ from pg_class as relation+ join pg_namespace as namespace on namespace.oid = relation.relnamespace+ join lateral aclexplode(relation.relacl) as acl on true+ where namespace.nspname !~ '^pg_'+ and namespace.nspname <> 'information_schema'+ and acl.grantee <> relation.relowner+ and relation.relkind in ('r', 'p', 'v', 'm', 'f', 'S')+ and (acl.grantee in ('k8s_reporting_owner'::regrole, 'k8s_metabase'::regrole)+ or (namespace.nspname = 'k8s_reporting' and relation.relname = 'current_resources' and acl.grantee <> relation.relowner))+ union all+ select+ 'routine'::text,+ ''::text,+ ''::text,+ routine.oid,+ routine.proowner::regrole::text,+ acl.privilege_type,+ case when acl.grantee = 0 then 'PUBLIC' else acl.grantee::regrole::text end,+ case when acl.grantor = 0 then 'PUBLIC' else acl.grantor::regrole::text end,+ acl.is_grantable+ from pg_proc as routine+ join pg_namespace as namespace on namespace.oid = routine.pronamespace+ join lateral aclexplode(routine.proacl) as acl on true+ where namespace.nspname !~ '^pg_'+ and namespace.nspname <> 'information_schema'+ and acl.grantee <> routine.proowner+ and acl.grantee in ('k8s_reporting_owner'::regrole, 'k8s_metabase'::regrole)+ ) as reporting_acl+ order by object_type, schema_name, object_name, routine_oid, privilege_type, grantee_name, grantor_name`)+ if err != nil {+ return nil, fmt.Errorf("enumerate reporting ACLs: %w", err)+ }+ defer rows.Close()++ acls := make([]reportingACL, 0)+ for rows.Next() {+ var acl reportingACL+ var objectKindSQL, privilegeSQL string+ if err := rows.Scan(+ &objectKindSQL,+ &acl.object.schema,+ &acl.object.name,+ &acl.object.routineOID,+ &acl.owner,+ &privilegeSQL,+ &acl.grantee,+ &acl.grantor,+ &acl.grantable,+ ); err != nil {+ return nil, fmt.Errorf("read reporting ACL: %w", err)+ }+ var ok bool+ acl.object.kind, ok = reportingObjectKindFromSQL(objectKindSQL)+ if !ok {+ return nil, fmt.Errorf("read reporting ACL with unsupported object kind %q", objectKindSQL)+ }+ acl.privilege, ok = reportingPrivilegeFromSQL(acl.object.kind, privilegeSQL)+ if !ok {+ return nil, fmt.Errorf("read reporting ACL with unsupported %s privilege %q", reportingObjectDescription(acl.object), privilegeSQL)+ }+ acls = append(acls, acl)+ }+ if err := rows.Err(); err != nil {+ return nil, fmt.Errorf("read reporting ACLs: %w", err)+ }+ return acls, nil+}++func expectedReportingACLs() []reportingACL {+ return []reportingACL{+ {object: reportingObject{kind: reportingObjectSchema, schema: "k8s_state"}, owner: "k8s_state_owner", privilege: reportingPrivilegeUsage, grantee: "k8s_reporting_owner", grantor: "k8s_state_owner"},+ {object: reportingObject{kind: reportingObjectRelation, schema: "k8s_state", name: "resource_state"}, owner: "k8s_state_owner", privilege: reportingPrivilegeSelect, grantee: "k8s_reporting_owner", grantor: "k8s_state_owner"},+ {object: reportingObject{kind: reportingObjectSchema, schema: "k8s_reporting"}, owner: "k8s_reporting_owner", privilege: reportingPrivilegeUsage, grantee: "k8s_metabase", grantor: "k8s_reporting_owner"},+ {object: reportingObject{kind: reportingObjectRelation, schema: "k8s_reporting", name: "current_resources"}, owner: "k8s_reporting_owner", privilege: reportingPrivilegeSelect, grantee: "k8s_metabase", grantor: "k8s_reporting_owner"},+ }+}++func isExpectedReportingACL(acl reportingACL) bool {+ return containsReportingACL(expectedReportingACLs(), acl)+}++func containsReportingACL(acls []reportingACL, want reportingACL) bool {+ for _, acl := range acls {+ if acl.object == want.object && acl.owner == want.owner && acl.privilege == want.privilege && acl.grantee == want.grantee && acl.grantor == want.grantor && acl.grantable == want.grantable {+ return true+ }+ }+ return false+}++func isSafeReportingACLAuthority(acl reportingACL, migrationOwner string) bool {+ if acl.grantor != acl.owner {+ return false+ }+ return acl.owner == migrationOwner || acl.owner == "k8s_state_owner" || acl.owner == "k8s_reporting_owner"+}++func validateNoPublicSecurityDefiner(ctx context.Context, transaction pgx.Tx) error {+ var routine string+ err := transaction.QueryRow(ctx, `+ select routine.oid::regprocedure::text+ from pg_proc as routine+ join pg_namespace as namespace on namespace.oid = routine.pronamespace+ join lateral aclexplode(coalesce(routine.proacl, acldefault('f'::"char", routine.proowner))) as acl on true+ where namespace.nspname !~ '^pg_'+ and namespace.nspname <> 'information_schema'+ and routine.prokind in ('f', 'p')+ and routine.prosecdef+ and acl.grantee = 0+ and acl.privilege_type = 'EXECUTE'+ limit 1`).Scan(&routine)+ if errors.Is(err, pgx.ErrNoRows) {+ return nil+ }+ if err != nil {+ return fmt.Errorf("inspect SECURITY DEFINER routines: %w", err)+ }+ return fmt.Errorf("PUBLIC-executable SECURITY DEFINER routine blocks reporting access: %s", routine)+}+func reconcileStaticRoleContracts(ctx context.Context, transaction pgx.Tx) ([]roleReconciliationEvent, []membershipReconciliationEvent, error) {var migrationOwner stringif err := transaction.QueryRow(ctx, `select current_user`).Scan(&migrationOwner); err != nil {@@ -596,15 +783,9 @@ func reconcileStaticRoleContracts(ctx context.Context, transaction pgx.Tx) ([]roif err != nil {return nil, nil, err}- clauses, err := staticRoleAlterClauses(contract, attributes)- if err != nil {+ if err := newRuntimeDDL(transaction).reconcileStaticRole(ctx, contract, attributes); err != nil {return nil, nil, err}- if clauses != "" {- if _, err := transaction.Exec(ctx, "alter role "+pgx.Identifier{contract.role}.Sanitize()+" "+clauses); err != nil {- return nil, nil, fmt.Errorf("reconcile static role %s: %w", contract.role, err)- }- }roleEvents = append(roleEvents, roleReconciliationEvent{Role: contract.role, DurationMS: time.Since(started).Milliseconds()})}@@ -630,21 +811,13 @@ func reconcileStaticRoleContracts(ctx context.Context, transaction pgx.Tx) ([]ro}if migrationOwnerGrant == nil || migrationOwnerGrant.admin != contract.admin || migrationOwnerGrant.inherit != contract.inherit || migrationOwnerGrant.set != contract.set {- roleIdentifier := pgx.Identifier{contract.role}.Sanitize()- memberIdentifier := pgx.Identifier{contract.member}.Sanitize()- grantorIdentifier := pgx.Identifier{migrationOwner}.Sanitize()+ ddl := newRuntimeDDL(transaction)if migrationOwnerGrant != nil {- statement := "revoke " + roleIdentifier + " from " + memberIdentifier + " granted by " + grantorIdentifier + " restrict"- if _, err := transaction.Exec(ctx, statement); err != nil {+ if err := ddl.revokeStaticMembership(ctx, contract, migrationOwner); err != nil {return nil, nil, fmt.Errorf("remove drifted static role membership %s -> %s: %w", contract.role, contract.member, err)}}- statement := "grant " + roleIdentifier + " to " + memberIdentifier +- " with admin " + postgresBoolean(contract.admin) +- ", inherit " + postgresBoolean(contract.inherit) +- ", set " + postgresBoolean(contract.set) +- " granted by " + grantorIdentifier- if _, err := transaction.Exec(ctx, statement); err != nil {+ if err := ddl.grantStaticMembership(ctx, contract, migrationOwner); err != nil {return nil, nil, fmt.Errorf("reconcile static role membership %s -> %s: %w", contract.role, contract.member, err)}}@@ -739,12 +912,13 @@ func validateStaticRoleMembers(ctx context.Context, transaction pgx.Tx, migratio}func validateQueryTenantRoleMembers(ctx context.Context, transaction pgx.Tx, migrationOwner string, contracts []staticMembershipContract) (map[string]dynamicTenantRole, error) {- if _, err := transaction.Exec(ctx, `set local role k8s_state_owner`); err != nil {+ ddl := newRuntimeDDL(transaction)+ if err := ddl.setLocalRole(ctx, "k8s_state_owner"); err != nil {return nil, fmt.Errorf("validate k8s_query_tenant memberships requires SET ROLE k8s_state_owner; restore the declared migration-owner membership before rerunning the migration: %w", err)}dynamicTenants, err := readRegisteredDynamicTenantRoles(ctx, transaction)if err != nil {- _, _ = transaction.Exec(ctx, `reset role`)+ _ = ddl.resetRole(ctx)return nil, err}rows, err := transaction.Query(ctx, `@@ -754,7 +928,7 @@ func validateQueryTenantRoleMembers(ctx context.Context, transaction pgx.Tx, migwhere membership.roleid = 'k8s_query_tenant'::regroleorder by member_role.rolname`)if err != nil {- _, _ = transaction.Exec(ctx, `reset role`)+ _ = ddl.resetRole(ctx)return nil, fmt.Errorf("read k8s_query_tenant members: %w", err)}@@ -763,7 +937,7 @@ func validateQueryTenantRoleMembers(ctx context.Context, transaction pgx.Tx, migvar member stringif err := rows.Scan(&member); err != nil {rows.Close()- _, _ = transaction.Exec(ctx, `reset role`)+ _ = ddl.resetRole(ctx)return nil, fmt.Errorf("scan k8s_query_tenant member: %w", err)}if member == migrationOwner || isDeclaredStaticMembership("k8s_query_tenant", member, contracts) {@@ -776,11 +950,11 @@ func validateQueryTenantRoleMembers(ctx context.Context, transaction pgx.Tx, mig}if err := rows.Err(); err != nil {rows.Close()- _, _ = transaction.Exec(ctx, `reset role`)+ _ = ddl.resetRole(ctx)return nil, fmt.Errorf("iterate k8s_query_tenant members: %w", err)}rows.Close()- if _, err := transaction.Exec(ctx, `reset role`); err != nil {+ if err := ddl.resetRole(ctx); err != nil {return nil, fmt.Errorf("reset role after validating k8s_query_tenant memberships: %w", err)}if len(unexpected) != 0 {@@ -993,39 +1167,17 @@ func reconcileStaticRoleSettings(ctx context.Context, transaction pgx.Tx) errorcontinue}- roleIdentifier := pgx.Identifier{roleContract.role}.Sanitize()- for _, setting := range actual {- statement := "alter role " + roleIdentifier- if setting.database != "" {- statement += " in database " + pgx.Identifier{setting.database}.Sanitize()- }- statement += " reset all"- if _, err := transaction.Exec(ctx, statement); err != nil {- return fmt.Errorf("reset static role settings for %s: %w", roleContract.role, err)- }- }-- settingNames := make([]string, 0, len(desired))- for name := range desired {- settingNames = append(settingNames, name)- }- sort.Strings(settingNames)- for _, name := range settingNames {- var value string- if err := transaction.QueryRow(ctx, `select format('%L', $1::text)`, desired[name]).Scan(&value); err != nil {- return fmt.Errorf("quote static role setting %s for %s: %w", name, roleContract.role, err)- }- if _, err := transaction.Exec(ctx, "alter role "+roleIdentifier+" set "+name+" = "+value); err != nil {- return fmt.Errorf("reconcile static role setting %s for %s: %w", name, roleContract.role, err)- }+ if err := newRuntimeDDL(transaction).reconcileStaticRoleSettings(ctx, roleContract, actual, desired); err != nil {+ return err}}return nil}type staticRoleSetting struct {- database string- values map[string]string+ database string+ values staticRoleSettingsContract+ hasUnknown bool}func readStaticRoleSettings(ctx context.Context, transaction pgx.Tx, role string) ([]staticRoleSetting, error) {@@ -1048,12 +1200,17 @@ func readStaticRoleSettings(ctx context.Context, transaction pgx.Tx, role stringif err := rows.Scan(&setting.database, &values); err != nil {return nil, fmt.Errorf("scan static role settings for %s: %w", role, err)}- setting.values = make(map[string]string, len(values))+ setting.values = make(staticRoleSettingsContract, len(values))for _, value := range values {- name, configuredValue, ok := strings.Cut(value, "=")+ sqlName, configuredValue, ok := strings.Cut(value, "=")if !ok {return nil, fmt.Errorf("static role %s has malformed setting %q", role, value)}+ name, approved := staticRoleSettingNameFromSQL(sqlName)+ if !approved {+ setting.hasUnknown = true+ continue+ }setting.values[name] = configuredValue}settings = append(settings, setting)@@ -1064,19 +1221,15 @@ func readStaticRoleSettings(ctx context.Context, transaction pgx.Tx, role stringreturn settings, nil}-func staticRoleSettingsMatch(actual []staticRoleSetting, desired map[string]string) bool {+func staticRoleSettingsMatch(actual []staticRoleSetting, desired staticRoleSettingsContract) bool {if len(desired) == 0 {return len(actual) == 0}- return len(actual) == 1 && actual[0].database == "" && mapsEqual(actual[0].values, desired)-}--func mapsEqual(left, right map[string]string) bool {- if len(left) != len(right) {+ if len(actual) != 1 || actual[0].database != "" || actual[0].hasUnknown || len(actual[0].values) != len(desired) {return false}- for key, leftValue := range left {- if right[key] != leftValue {+ for name, desiredValue := range desired {+ if actual[0].values[name] != desiredValue {return false}}@@ -1132,11 +1285,7 @@ func reconcilePasswords(ctx context.Context, transaction pgx.Tx, credentials []cevents := make([]credentialEvent, 0, len(credentials))for _, credential := range credentials {started := time.Now()- var quotedPassword string- if err := transaction.QueryRow(ctx, `select format('%L', $1::text)`, credential.Password).Scan("edPassword); err != nil {- return nil, fmt.Errorf("quote password for role %s: %w", credential.Role, err)- }- if _, err := transaction.Exec(ctx, "alter role "+pgx.Identifier{credential.Role}.Sanitize()+" password "+quotedPassword); err != nil {+ if err := newRuntimeDDL(transaction).setPassword(ctx, credential); err != nil {return nil, fmt.Errorf("reconcile password for role %s: %w", credential.Role, err)}events = append(events, credentialEvent{Role: credential.Role, DurationMS: time.Since(started).Milliseconds()})diff --git a/libs/fleet/backend/database/migrator_pg_test.go b/libs/fleet/backend/database/migrator_pg_test.goindex 58571d5722..d0e5d4289f 100644--- a/libs/fleet/backend/database/migrator_pg_test.go+++ b/libs/fleet/backend/database/migrator_pg_test.go@@ -30,6 +30,41 @@ var staticMigrationRoles = []string{"k8s_metabase",}+func TestRequireVersionRejectsAbsentLedger(t *testing.T) {+ maintenanceURL := os.Getenv("CYCLOPS_TEST_DATABASE_URL")+ if maintenanceURL == "" {+ t.Skip("set CYCLOPS_TEST_DATABASE_URL to run PostgreSQL migration tests")+ }+ if os.Getenv(migratorIntegrationOptIn) != "1" {+ t.Skipf("set %s=1 to run against a dedicated empty PostgreSQL cluster", migratorIntegrationOptIn)+ }++ migrationURL, _ := isolatedMigrationDatabase(t, context.Background(), maintenanceURL)+ err := RequireVersion(context.Background(), migrationURL, 1)+ if err == nil || !strings.Contains(err.Error(), "older than required version 1") {+ t.Fatalf("RequireVersion() error = %v, want missing-ledger version error", err)+ }+}++func TestRequireVersionAllowsRestrictedApplicationRoleAfterMigration(t *testing.T) {+ maintenanceURL := os.Getenv("CYCLOPS_TEST_DATABASE_URL")+ if maintenanceURL == "" {+ t.Skip("set CYCLOPS_TEST_DATABASE_URL to run PostgreSQL migration tests")+ }+ if os.Getenv(migratorIntegrationOptIn) != "1" {+ t.Skipf("set %s=1 to run against a dedicated empty PostgreSQL cluster", migratorIntegrationOptIn)+ }++ ctx := context.Background()+ migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatal(err)+ }+ if err := RequireVersion(ctx, credentials.Application, 1); err != nil {+ t.Fatalf("restricted cyclops_app RequireVersion() error = %v", err)+ }+}+func TestInitialMigrationBuildsCompleteDatabase(t *testing.T) {maintenanceURL := os.Getenv("CYCLOPS_TEST_DATABASE_URL")if maintenanceURL == "" {@@ -43,6 +78,7 @@ func TestInitialMigrationBuildsCompleteDatabase(t *testing.T) {migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)inspectionURL := maintenanceURLForDatabase(t, maintenanceURL, migrationURL)assertMigrationOwnerFixture(t, ctx, migrationURL)+ assertInitialMigrationRejectsPublicSecurityDefiner(t, ctx, migrationURL, credentials)if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {t.Fatal(err)}@@ -77,10 +113,377 @@ func TestInitialMigrationBuildsCompleteDatabase(t *testing.T) {assertWriterBoundary(t, ctx, credentials.Writer)assertExporterBoundary(t, ctx, credentials.Exporter)assertTenantReadPath(t, ctx, tenantURL)- assertMetabaseBoundary(t, ctx, credentials.Metabase)+ assertMetabaseBoundary(t, ctx, inspectionURL, credentials.Metabase)assertApplicationBoundary(t, ctx, credentials.Application)}+func TestRunReconcilesReportingDirectACLDrift(t *testing.T) {+ maintenanceURL := requireMigratorIntegration(t)+ ctx := context.Background()+ migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatal(err)+ }++ connection := connect(t, ctx, migrationURL)+ defer connection.Close(ctx)+ if _, err := connection.Exec(ctx, `+ create schema reporting_acl_drift;+ create table reporting_acl_drift.probe (id integer primary key);+ create function reporting_acl_drift.probe_function() returns integer language sql as $$ select 1 $$;+ create procedure reporting_acl_drift.probe_procedure() language sql as $$ select 1 $$;+ grant select on reporting_acl_drift.probe to k8s_metabase, k8s_reporting_owner;+ grant execute on function reporting_acl_drift.probe_function() to k8s_metabase, k8s_reporting_owner;+ grant execute on procedure reporting_acl_drift.probe_procedure() to k8s_metabase, k8s_reporting_owner`); err != nil {+ t.Fatal("introduce reporting direct ACL drift")+ }++ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatalf("Run() reconcile reporting direct ACL drift: %v", err)+ }+ assertExactReportingACLContract(t, ctx, connection)+ for _, role := range []string{"k8s_metabase", "k8s_reporting_owner"} {+ var relationACL, routineACL bool+ if err := connection.QueryRow(ctx, `+ select+ exists (+ select 1 from pg_class relation+ join lateral aclexplode(coalesce(relation.relacl, acldefault('r', relation.relowner))) acl on true+ where relation.oid = 'reporting_acl_drift.probe'::regclass+ and acl.grantee = $1::regrole+ ),+ exists (+ select 1 from pg_proc routine+ join lateral aclexplode(coalesce(routine.proacl, acldefault('f', routine.proowner))) acl on true+ where routine.pronamespace = 'reporting_acl_drift'::regnamespace+ and acl.grantee = $1::regrole+ )`, role).Scan(&relationACL, &routineACL); err != nil {+ t.Fatalf("inspect %s direct ACLs: %v", role, err)+ }+ if relationACL || routineACL {+ t.Errorf("%s retains arbitrary direct ACLs: relation=%t routine=%t", role, relationACL, routineACL)+ }+ }++ var reportingCanReadState bool+ if err := connection.QueryRow(ctx, `+ select has_table_privilege('k8s_reporting_owner'::regrole, relation.oid, 'SELECT')+ from pg_class as relation+ join pg_namespace as namespace on namespace.oid = relation.relnamespace+ where namespace.nspname = 'k8s_state' and relation.relname = 'resource_state'`).Scan(&reportingCanReadState); err != nil || !reportingCanReadState {+ t.Errorf("k8s_reporting_owner state read grant = %t err=%v, want true", reportingCanReadState, err)+ }+}++func TestRunPreservesOwnerDerivedReportingACLsWhileRepairingExplicitDrift(t *testing.T) {+ maintenanceURL := requireMigratorIntegration(t)+ ctx := context.Background()+ migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatalf("initial Run(): %v", err)+ }++ connection := connect(t, ctx, migrationURL)+ defer connection.Close(ctx)+ before := reportingOwnerACLRows(t, ctx, connection)+ if before == "" {+ t.Fatal("reporting fixtures must expose owner-derived ACL rows")+ }+ if _, err := connection.Exec(ctx, `+ set role k8s_reporting_owner;+ grant insert on k8s_reporting.current_resources to k8s_metabase;+ reset role`); err != nil {+ t.Fatal("introduce explicit reporting view write drift")+ }+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatalf("Run() repair explicit reporting view drift: %v", err)+ }+ if after := reportingOwnerACLRows(t, ctx, connection); after != before {+ t.Fatalf("owner-derived reporting ACL rows changed: before=%q after=%q", before, after)+ }+ assertExactReportingACLContract(t, ctx, connection)+}++func TestRunRepairsExactReportingACLContract(t *testing.T) {+ maintenanceURL := requireMigratorIntegration(t)+ ctx := context.Background()+ migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatal(err)+ }++ connection := connect(t, ctx, migrationURL)+ defer connection.Close(ctx)+ t.Cleanup(func() { dropRole(t, context.Background(), maintenanceURL, "reporting_acl_unexpected") })+ if _, err := connection.Exec(ctx, `+ create schema reporting_acl_drift;+ create table reporting_acl_drift.probe (id integer primary key);+ create sequence reporting_acl_drift.probe_sequence;+ create function reporting_acl_drift.probe_function() returns integer language sql as $$ select 1 $$;+ create schema "reporting ACL drift";+ create table "reporting ACL drift"."probe.table" (id integer primary key);+ create sequence "reporting ACL drift"."probe.sequence";+ create function "reporting ACL drift"."probe.function"(integer) returns integer language sql as $$ select $1 $$;+ set role k8s_state_owner;+ grant usage, create on schema k8s_state to k8s_metabase;+ reset role;+ grant create on schema cyclops_migrations to k8s_metabase;+ grant insert, update, delete on cyclops_migrations.applied_migrations to k8s_metabase;+ set role k8s_reporting_owner;+ grant insert, update, delete on k8s_reporting.current_resources to k8s_metabase;+ grant usage, create on schema k8s_reporting to public;+ reset role;+ grant select on reporting_acl_drift.probe to k8s_metabase, k8s_reporting_owner;+ grant usage on sequence reporting_acl_drift.probe_sequence to k8s_metabase, k8s_reporting_owner;+ grant execute on function reporting_acl_drift.probe_function() to k8s_metabase, k8s_reporting_owner;+ grant usage on schema "reporting ACL drift" to k8s_metabase, k8s_reporting_owner;+ grant select on "reporting ACL drift"."probe.table" to k8s_metabase, k8s_reporting_owner;+ grant usage on sequence "reporting ACL drift"."probe.sequence" to k8s_metabase, k8s_reporting_owner;+ grant execute on function "reporting ACL drift"."probe.function"(integer) to k8s_metabase, k8s_reporting_owner;+ create role reporting_acl_unexpected nologin;+ set role k8s_reporting_owner;+ grant usage on schema k8s_reporting to reporting_acl_unexpected;+ grant select on k8s_reporting.current_resources to reporting_acl_unexpected;+ reset role`); err != nil {+ t.Fatalf("introduce recoverable reporting ACL drift: %v", err)+ }++ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatalf("Run() reconcile exact reporting ACL contract: %v", err)+ }+ assertExactReportingACLContract(t, ctx, connection)+}++func TestRunFailsClosedBeforeReportingACLMutation(t *testing.T) {+ maintenanceURL := requireMigratorIntegration(t)+ ctx := context.Background()+ migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatal(err)+ }++ connection := connect(t, ctx, migrationURL)+ defer connection.Close(ctx)+ if _, err := connection.Exec(ctx, `+ create schema reporting_acl_sentinel;+ create table reporting_acl_sentinel.probe (id integer primary key);+ grant select on reporting_acl_sentinel.probe to k8s_metabase;+ set role k8s_reporting_owner;+ drop view k8s_reporting.current_resources;+ create table k8s_reporting.current_resources (id integer);+ reset role`); err != nil {+ t.Fatalf("introduce incompatible reporting relation and ACL sentinel: %v", err)+ }++ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err == nil || !strings.Contains(err.Error(), "reporting relation") {+ t.Fatalf("Run() error = %v, want reporting relation fail-closed error", err)+ }+ var retained bool+ if err := connection.QueryRow(ctx, `select has_table_privilege('k8s_metabase', 'reporting_acl_sentinel.probe', 'SELECT')`).Scan(&retained); err != nil || !retained {+ t.Fatalf("unrelated ACL sentinel changed after rejected reporting drift: retained=%t err=%v", retained, err)+ }+}++func TestRunFailsClosedForExternalReportingACLGrantor(t *testing.T) {+ maintenanceURL := requireMigratorIntegration(t)+ ctx := context.Background()+ migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatal(err)+ }++ connection := connect(t, ctx, migrationURL)+ defer connection.Close(ctx)+ t.Cleanup(func() {+ cleanup := connect(t, context.Background(), migrationURL)+ defer cleanup.Close(context.Background())+ if _, err := cleanup.Exec(context.Background(), `set role reporting_acl_external_owner; drop schema reporting_acl_external cascade; reset role`); err != nil {+ t.Errorf("drop externally owned reporting ACL fixture: %v", err)+ }+ dropRole(t, context.Background(), maintenanceURL, "reporting_acl_external_owner")+ })+ if _, err := connection.Exec(ctx, `+ create role reporting_acl_external_owner nologin;+ grant reporting_acl_external_owner to current_user with inherit false, set true;+ create schema reporting_acl_external;+ alter schema reporting_acl_external owner to reporting_acl_external_owner;+ set role reporting_acl_external_owner;+ create table reporting_acl_external.probe (id integer primary key);+ create function reporting_acl_external.probe_function() returns integer language sql as $$ select 1 $$;+ grant usage on schema reporting_acl_external to k8s_metabase;+ grant select on reporting_acl_external.probe to k8s_reporting_owner;+ grant execute on function reporting_acl_external.probe_function() to k8s_metabase;+ reset role`); err != nil {+ t.Fatalf("introduce externally owned reporting ACL drift: %v", err)+ }++ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err == nil || !strings.Contains(err.Error(), "unsafe reporting ACL") {+ t.Fatalf("Run() error = %v, want unsafe reporting ACL fail-closed error", err)+ }+ for _, statement := range []string{+ `select has_schema_privilege('k8s_metabase', namespace.oid, 'USAGE') from pg_namespace as namespace where namespace.nspname = 'reporting_acl_external'`,+ `select has_table_privilege('k8s_reporting_owner', relation.oid, 'SELECT') from pg_class as relation join pg_namespace as namespace on namespace.oid = relation.relnamespace where namespace.nspname = 'reporting_acl_external' and relation.relname = 'probe'`,+ `select has_function_privilege('k8s_metabase', routine.oid, 'EXECUTE') from pg_proc as routine join pg_namespace as namespace on namespace.oid = routine.pronamespace where namespace.nspname = 'reporting_acl_external' and routine.proname = 'probe_function'`,+ } {+ var retained bool+ if err := connection.QueryRow(ctx, statement).Scan(&retained); err != nil || !retained {+ t.Fatalf("external ACL changed after rejected drift for %q: retained=%t err=%v", statement, retained, err)+ }+ }+}++func TestRunRechecksPublicSecurityDefinerBeforeMutations(t *testing.T) {+ maintenanceURL := requireMigratorIntegration(t)+ ctx := context.Background()+ migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatal(err)+ }++ connection := connect(t, ctx, migrationURL)+ defer connection.Close(ctx)+ if _, err := connection.Exec(ctx, `create function public.repeat_run_public_definer() returns integer language sql security definer as $$ select 1 $$`); err != nil {+ t.Fatal("create public security-definer fixture")+ }+ var before string+ if err := connection.QueryRow(ctx, `select coalesce(proacl::text, '<default>') from pg_proc where oid = 'public.repeat_run_public_definer()'::regprocedure`).Scan(&before); err != nil {+ t.Fatal("read public security-definer ACL before Run")+ }+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err == nil || !strings.Contains(err.Error(), "PUBLIC-executable SECURITY DEFINER routine") {+ t.Fatalf("Run() error = %v, want PUBLIC-executable SECURITY DEFINER routine", err)+ }+ var after string+ if err := connection.QueryRow(ctx, `select coalesce(proacl::text, '<default>') from pg_proc where oid = 'public.repeat_run_public_definer()'::regprocedure`).Scan(&after); err != nil {+ t.Fatal("read public security-definer ACL after Run")+ }+ if after != before {+ t.Fatalf("Run() changed blocked security-definer routine ACL: before=%q after=%q", before, after)+ }+}++func TestRunDeniesMetabaseProcedureWriteAfterReadOnlyGUCBypass(t *testing.T) {+ maintenanceURL := requireMigratorIntegration(t)+ ctx := context.Background()+ migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatal(err)+ }++ connection := connect(t, ctx, migrationURL)+ defer connection.Close(ctx)+ if _, err := connection.Exec(ctx, `+ set role k8s_reporting_owner;+ create procedure k8s_reporting.write_probe() language plpgsql as $$+ begin+ insert into k8s_state.resource_state (+ cluster_id, api_group, resource, namespace, name, schema_hash, watch_epoch, observed_sequence, labels, object+ ) values ('metabase-write-probe', '', 'pods', 'default', 'probe', 'probe', 1, 1, '{}'::jsonb, '{}'::jsonb);+ end+ $$;+ grant execute on procedure k8s_reporting.write_probe() to k8s_metabase;+ reset role`); err != nil {+ t.Fatal("create executable metabase write probe")+ }++ metabase := connect(t, ctx, credentials.Metabase)+ defer metabase.Close(ctx)+ if _, err := metabase.Exec(ctx, `set default_transaction_read_only = off`); err != nil {+ t.Fatal("disable metabase read-only default")+ }+ if _, err := metabase.Exec(ctx, `call k8s_reporting.write_probe()`); err == nil || !strings.Contains(strings.ToLower(err.Error()), "permission denied") {+ t.Fatalf("metabase procedure write error = %v, want privilege denial", err)+ }+}++func TestMetabaseReadOnlyDefaultBlocksExecutableWriteProcedure(t *testing.T) {+ maintenanceURL := requireMigratorIntegration(t)+ ctx := context.Background()+ migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatal(err)+ }++ connection := connect(t, ctx, migrationURL)+ defer connection.Close(ctx)+ if _, err := connection.Exec(ctx, `+ set role k8s_reporting_owner;+ create table k8s_reporting.read_only_probe (id integer primary key);+ create procedure k8s_reporting.read_only_write_probe() language sql as $$ insert into k8s_reporting.read_only_probe values (1) $$;+ grant execute on procedure k8s_reporting.read_only_write_probe() to k8s_metabase;+ reset role`); err != nil {+ t.Fatal("create metabase read-only procedure fixture")+ }++ metabase := connect(t, ctx, credentials.Metabase)+ defer metabase.Close(ctx)+ if _, err := metabase.Exec(ctx, `call k8s_reporting.read_only_write_probe()`); err == nil || !strings.Contains(strings.ToLower(err.Error()), "read-only") {+ t.Fatalf("metabase read-only procedure error = %v, want read-only failure", err)+ }+}++func TestRunFailsClosedForReportingObjectDrift(t *testing.T) {+ maintenanceURL := requireMigratorIntegration(t)+ ctx := context.Background()++ for _, testCase := range []struct {+ name string+ introduce func(t *testing.T, connection *pgx.Conn)+ assertUnchanged func(t *testing.T, connection *pgx.Conn)+ wantError string+ }{+ {+ name: "relation type",+ introduce: func(t *testing.T, connection *pgx.Conn) {+ t.Helper()+ if _, err := connection.Exec(ctx, `drop view k8s_reporting.current_resources; create table k8s_reporting.current_resources (id integer)`); err != nil {+ t.Fatal("replace reporting view with table")+ }+ },+ assertUnchanged: func(t *testing.T, connection *pgx.Conn) {+ t.Helper()+ var relationKind string+ if err := connection.QueryRow(ctx, `select relkind::text from pg_class where oid = 'k8s_reporting.current_resources'::regclass`).Scan(&relationKind); err != nil || relationKind != "r" {+ t.Fatalf("reporting relation kind after rejected drift = %q err=%v, want r", relationKind, err)+ }+ },+ wantError: "reporting relation",+ },++ {+ name: "view owner",+ introduce: func(t *testing.T, connection *pgx.Conn) {+ t.Helper()+ if _, err := connection.Exec(ctx, `alter view k8s_reporting.current_resources owner to current_user`); err != nil {+ t.Fatal("change reporting view owner")+ }+ },+ assertUnchanged: func(t *testing.T, connection *pgx.Conn) {+ t.Helper()+ var owner string+ if err := connection.QueryRow(ctx, `select relowner::regrole::text from pg_class where oid = 'k8s_reporting.current_resources'::regclass`).Scan(&owner); err != nil || owner == "k8s_reporting_owner" {+ t.Fatalf("reporting view owner after rejected drift = %q err=%v, want non-reporting owner", owner, err)+ }+ },+ wantError: "reporting view owner",+ },+ } {+ t.Run(testCase.name, func(t *testing.T) {+ migrationURL, credentials := isolatedMigrationDatabase(t, ctx, maintenanceURL)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err != nil {+ t.Fatal(err)+ }+ inspectionURL := maintenanceURLForDatabase(t, maintenanceURL, migrationURL)+ connection := connect(t, ctx, inspectionURL)+ defer connection.Close(ctx)+ testCase.introduce(t, connection)+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err == nil || !strings.Contains(err.Error(), testCase.wantError) {+ t.Fatalf("Run() error = %v, want fail-closed error containing %q", err, testCase.wantError)+ }+ testCase.assertUnchanged(t, connection)+ })+ }+}+func TestRunReconcilesStaticRoleContractDrift(t *testing.T) {maintenanceURL := os.Getenv("CYCLOPS_TEST_DATABASE_URL")if maintenanceURL == "" {@@ -1105,7 +1508,6 @@ func assertRuntimeLedgerAccess(t *testing.T, ctx context.Context, credentials Cr"writer": credentials.Writer,"exporter": credentials.Exporter,"role-admin": credentials.RoleAdmin,- "metabase": credentials.Metabase,} {connection := connect(t, ctx, databaseURL)var count int@@ -1118,6 +1520,10 @@ func assertRuntimeLedgerAccess(t *testing.T, ctx context.Context, credentials CrassertStatementFails(t, ctx, databaseURL, `update cyclops_migrations.applied_migrations set filename = 'invalid.sql' where version = 1`)assertStatementFails(t, ctx, databaseURL, `delete from cyclops_migrations.applied_migrations where version = 1`)}+ assertStatementFails(t, ctx, credentials.Metabase, `select 1 from cyclops_migrations.applied_migrations limit 1`)+ assertStatementFails(t, ctx, credentials.Metabase, `insert into cyclops_migrations.applied_migrations (version, filename, sha256) values (99, 'invalid.sql', 'invalid')`)+ assertStatementFails(t, ctx, credentials.Metabase, `update cyclops_migrations.applied_migrations set filename = 'invalid.sql' where version = 1`)+ assertStatementFails(t, ctx, credentials.Metabase, `delete from cyclops_migrations.applied_migrations where version = 1`)}func createTenantRolePath(t *testing.T, ctx context.Context, roleAdminURL, tenantRole, tenantPassword, tenant string) string {@@ -1261,14 +1667,43 @@ func tenantResourceNames(t *testing.T, ctx context.Context, tenantURL string) []return names}-func assertMetabaseBoundary(t *testing.T, ctx context.Context, metabaseURL string) {+func assertMetabaseBoundary(t *testing.T, ctx context.Context, inspectionURL, metabaseURL string) {t.Helper()+ inspection := connect(t, ctx, inspectionURL)+ defer inspection.Close(ctx)+ var tenantForRoleOID uint32+ if err := inspection.QueryRow(ctx, `+ select routine.oid+ from pg_proc as routine+ join pg_namespace as namespace on namespace.oid = routine.pronamespace+ where namespace.nspname = 'k8s_state'+ and routine.proname = 'tenant_for_role'+ and routine.proargtypes = '19'::oidvector`).Scan(&tenantForRoleOID); err != nil {+ t.Fatalf("resolve k8s_state.tenant_for_role OID: %v", err)+ }connection := connect(t, ctx, metabaseURL)defer connection.Close(ctx)var count intif err := connection.QueryRow(ctx, `select count(*) from k8s_reporting.current_resources where cluster_id = 'migration-test'`).Scan(&count); err != nil || count != 4 {t.Errorf("metabase cannot read reporting view: count=%d err=%v", count, err)}+ var readOnly string+ if err := connection.QueryRow(ctx, `show default_transaction_read_only`).Scan(&readOnly); err != nil || readOnly != "on" {+ t.Errorf("metabase default_transaction_read_only = %q err=%v, want on", readOnly, err)+ }+ if err := connection.QueryRow(ctx, `select count(*) from pg_auth_members where member = 'k8s_metabase'::regrole`).Scan(&count); err != nil || count != 0 {+ t.Errorf("k8s_metabase membership count = %d err=%v, want 0", count, err)+ }+ var canExecute bool+ if err := connection.QueryRow(ctx, `select has_function_privilege('k8s_metabase'::regrole, $1::oid, 'EXECUTE')`, tenantForRoleOID).Scan(&canExecute); err != nil || canExecute {+ t.Errorf("k8s_metabase execute k8s_state.tenant_for_role = %t err=%v, want false", canExecute, err)+ }+ if _, err := connection.Exec(ctx, `set default_transaction_read_only = off`); err != nil {+ t.Fatalf("disable metabase read-only default: %v", err)+ }+ if _, err := connection.Exec(ctx, `delete from k8s_reporting.current_resources`); err == nil {+ t.Error("metabase can write reporting view after disabling read-only default")+ }for _, statement := range []string{`select 1 from k8s_state.resource_state limit 1`,`select 1 from k8s_api.current_resources limit 1`,@@ -1279,6 +1714,28 @@ func assertMetabaseBoundary(t *testing.T, ctx context.Context, metabaseURL strin}}+func assertInitialMigrationRejectsPublicSecurityDefiner(t *testing.T, ctx context.Context, migrationURL string, credentials CredentialURLs) {+ t.Helper()+ connection := connect(t, ctx, migrationURL)+ defer connection.Close(ctx)+ if _, err := connection.Exec(ctx, `create function public.migration_public_definer() returns integer language sql security definer as $$ select 1 $$`); err != nil {+ t.Fatal("create public security-definer fixture")+ }+ if err := Run(ctx, Config{MigrationURL: migrationURL, Credentials: credentials}); err == nil || !strings.Contains(err.Error(), "PUBLIC-executable SECURITY DEFINER routine") {+ t.Fatalf("Run() error = %v, want PUBLIC-executable SECURITY DEFINER routine", err)+ }+ var reportingSchemaExists, metabaseRoleExists bool+ if err := connection.QueryRow(ctx, `select exists (select 1 from pg_namespace where nspname = 'k8s_reporting'), exists (select 1 from pg_roles where rolname = 'k8s_metabase')`).Scan(&reportingSchemaExists, &metabaseRoleExists); err != nil {+ t.Fatal("check rolled-back reporting migration")+ }+ if reportingSchemaExists || metabaseRoleExists {+ t.Fatalf("failed migration left reporting authority behind: schema=%t role=%t", reportingSchemaExists, metabaseRoleExists)+ }+ if _, err := connection.Exec(ctx, `revoke execute on function public.migration_public_definer() from public`); err != nil {+ t.Fatal("revoke public execute from security-definer fixture")+ }+}+func assertApplicationBoundary(t *testing.T, ctx context.Context, applicationURL string) {t.Helper()connection := connect(t, ctx, applicationURL)@@ -1335,6 +1792,96 @@ func assertRunFailsBeforeStaticRoleRepair(t *testing.T, ctx context.Context, mig}}+func reportingOwnerACLRows(t *testing.T, ctx context.Context, connection *pgx.Conn) string {+ t.Helper()+ var rows string+ if err := connection.QueryRow(ctx, `+ select coalesce(string_agg(object_identity || ':' || privilege_type, ',' order by object_identity, privilege_type), '')+ from (+ select 'schema:' || namespace.nspname as object_identity, acl.privilege_type+ from pg_namespace as namespace+ join lateral aclexplode(namespace.nspacl) as acl on true+ where namespace.nspname = 'k8s_reporting' and acl.grantee = namespace.nspowner+ union all+ select 'relation:' || namespace.nspname || '.' || relation.relname, acl.privilege_type+ from pg_class as relation+ join pg_namespace as namespace on namespace.oid = relation.relnamespace+ join lateral aclexplode(relation.relacl) as acl on true+ where namespace.nspname = 'k8s_reporting' and relation.relname = 'current_resources' and acl.grantee = relation.relowner+ ) as owner_acl`).Scan(&rows); err != nil {+ t.Fatalf("read reporting owner ACL rows: %v", err)+ }+ return rows+}++func assertExactReportingACLContract(t *testing.T, ctx context.Context, connection *pgx.Conn) {+ t.Helper()+ var differences int+ if err := connection.QueryRow(ctx, `+ with actual as (+ select 'schema:' || namespace.nspname as object_identity, acl.privilege_type, acl.grantee::regrole::text, acl.grantor::regrole::text, acl.is_grantable+ from pg_namespace as namespace+ join lateral aclexplode(namespace.nspacl) as acl on true+ where acl.grantee <> namespace.nspowner+ and acl.grantee in ('k8s_reporting_owner'::regrole, 'k8s_metabase'::regrole)+ union all+ select 'relation:' || namespace.nspname || '.' || relation.relname, acl.privilege_type, acl.grantee::regrole::text, acl.grantor::regrole::text, acl.is_grantable+ from pg_class as relation+ join pg_namespace as namespace on namespace.oid = relation.relnamespace+ join lateral aclexplode(relation.relacl) as acl on true+ where relation.relkind in ('r', 'p', 'v', 'm', 'f', 'S')+ and acl.grantee <> relation.relowner+ and acl.grantee in ('k8s_reporting_owner'::regrole, 'k8s_metabase'::regrole)+ union all+ select 'routine:' || namespace.nspname || '.' || routine.oid::regprocedure::text, acl.privilege_type, acl.grantee::regrole::text, acl.grantor::regrole::text, acl.is_grantable+ from pg_proc as routine+ join pg_namespace as namespace on namespace.oid = routine.pronamespace+ join lateral aclexplode(routine.proacl) as acl on true+ where acl.grantee <> routine.proowner+ and acl.grantee in ('k8s_reporting_owner'::regrole, 'k8s_metabase'::regrole)+ ), expected as (+ values+ ('schema:k8s_state', 'USAGE', 'k8s_reporting_owner', 'k8s_state_owner', false),+ ('relation:k8s_state.resource_state', 'SELECT', 'k8s_reporting_owner', 'k8s_state_owner', false),+ ('schema:k8s_reporting', 'USAGE', 'k8s_metabase', 'k8s_reporting_owner', false),+ ('relation:k8s_reporting.current_resources', 'SELECT', 'k8s_metabase', 'k8s_reporting_owner', false)+ )+ select count(*) from (+ (select * from actual except select * from expected)+ union all+ (select * from expected except select * from actual)+ ) as difference`).Scan(&differences); err != nil {+ t.Fatalf("read reporting ACL contract: %v", err)+ }+ if differences != 0 {+ t.Fatalf("reporting ACL contract differences = %d, want 0", differences)+ }++ var reportingObjectDrift bool+ if err := connection.QueryRow(ctx, `+ select exists (+ select 1+ from pg_namespace as namespace+ join lateral aclexplode(namespace.nspacl) as acl on true+ where namespace.nspname = 'k8s_reporting'+ and acl.grantee <> namespace.nspowner+ and (acl.grantee <> 'k8s_metabase'::regrole or acl.privilege_type <> 'USAGE' or acl.is_grantable)+ union all+ select 1+ from pg_class as relation+ join pg_namespace as namespace on namespace.oid = relation.relnamespace+ join lateral aclexplode(relation.relacl) as acl on true+ where namespace.nspname = 'k8s_reporting' and relation.relname = 'current_resources'+ and acl.grantee <> relation.relowner+ and (acl.grantee <> 'k8s_metabase'::regrole or acl.privilege_type <> 'SELECT' or acl.is_grantable)+ )`).Scan(&reportingObjectDrift); err != nil {+ t.Fatalf("read reporting schema and view ACLs: %v", err)+ }+ if reportingObjectDrift {+ t.Fatal("reporting schema or view retains a PUBLIC or unexpected grantee ACL")+ }+}+func assertStatementFails(t *testing.T, ctx context.Context, databaseURL, statement string) {t.Helper()connection := connect(t, ctx, databaseURL)diff --git a/libs/fleet/backend/database/migrator_test.go b/libs/fleet/backend/database/migrator_test.goindex 9f52033351..e6d3b9543d 100644--- a/libs/fleet/backend/database/migrator_test.go+++ b/libs/fleet/backend/database/migrator_test.go@@ -66,7 +66,7 @@ func TestTenantCredentialFingerprintLookupIsParameterized(t *testing.T) {}func TestStaticRoleAlterClausesOnlyContainPermittedDrift(t *testing.T) {- contract := staticRoleContract{role: "cyclops_app", login: true, connectionLimit: -1, validUntil: "infinity"}+ contract := staticRoleContract{role: "cyclops_app", login: true, connectionLimit: -1, validUntil: staticRoleValidUntilInfinity}clauses, err := staticRoleAlterClauses(contract, staticRoleAttributes{login: false,inherit: true,@@ -86,11 +86,11 @@ func TestStaticRoleAlterClausesOnlyContainPermittedDrift(t *testing.T) {}func TestStaticRoleSettingsContractsAreExact(t *testing.T) {- want := map[string]map[string]string{+ want := map[string]staticRoleSettingsContract{"k8s_metabase": {- "default_transaction_read_only": "on",- "statement_timeout": "20000ms",- "idle_in_transaction_session_timeout": "20000ms",+ staticRoleSettingDefaultTransactionReadOnly: "on",+ staticRoleSettingStatementTimeout: "20000ms",+ staticRoleSettingIdleInTransactionSessionTimeout: "20000ms",},}if !reflect.DeepEqual(staticRoleSettingsContracts(), want) {@@ -98,6 +98,16 @@ func TestStaticRoleSettingsContractsAreExact(t *testing.T) {}}+func TestStaticRoleAlterClausesRejectUnsupportedValidUntilContract(t *testing.T) {+ _, err := staticRoleAlterClauses(+ staticRoleContract{role: "cyclops_app", login: true, validUntil: staticRoleValidUntil(255)},+ staticRoleAttributes{login: true, validUntil: "2026-08-10 00:00:00+00"},+ )+ if err == nil || !strings.Contains(err.Error(), "unsupported valid-until contract") {+ t.Fatalf("static role valid-until error = %v, want closed infinity-only contract", err)+ }+}+func TestStaticRoleAlterClausesRejectCreateDBDrift(t *testing.T) {_, err := staticRoleAlterClauses(staticRoleContract{role: "cyclops_app", login: true}, staticRoleAttributes{login: true,@@ -296,6 +306,17 @@ func TestEmbeddedMigrationsAreOrderedAndImmutable(t *testing.T) {t.Fatal("embedded initial schema migration digest does not match its contents")}for _, expected := range []string{+ "CREATE TABLE k8s_state.resource_state",+ "CREATE TABLE k8s_state.watch_checkpoint",+ "CREATE TABLE k8s_state.resource_event_outbox",+ "CREATE TABLE k8s_state.resource_schema",+ "CREATE TABLE k8s_state.query_tenant_role",+ "ALTER TABLE k8s_state.resource_state FORCE ROW LEVEL SECURITY",+ "CREATE POLICY writer_current_state",+ "CREATE POLICY tenant_current_state",+ "resource = 'namespaces'",+ "CREATE VIEW k8s_api.current_resources",+ "GRANT SELECT ON k8s_api.current_resources TO k8s_query_tenant, k8s_query_admin","credential_fingerprint text NOT NULL","CREATE FUNCTION k8s_state.register_tenant_role(p_role_name name, p_capsule_tenant text, p_credential_fingerprint text)","CREATE FUNCTION k8s_state.unregister_tenant_role(p_role_name name)",@@ -444,17 +465,17 @@ func TestStaticRoleContractsAreAllowlistedAndFixed(t *testing.T) {want := map[string]struct {login, inherit, createRole, createDB boolconnectionLimit int- validUntil string+ validUntil staticRoleValidUntil}{- "cyclops_app": {true, false, false, false, -1, "infinity"},- "k8s_state_owner": {false, true, false, false, -1, "infinity"},- "k8s_state_writer": {true, false, false, false, -1, "infinity"},- "k8s_state_exporter": {true, false, false, false, -1, "infinity"},- "k8s_query_tenant": {false, true, false, false, -1, "infinity"},- "k8s_query_admin": {false, true, false, false, -1, "infinity"},- "k8s_role_admin": {true, false, true, false, -1, "infinity"},- "k8s_reporting_owner": {false, true, false, false, -1, "infinity"},- "k8s_metabase": {true, false, false, false, -1, "infinity"},+ "cyclops_app": {true, false, false, false, -1, staticRoleValidUntilInfinity},+ "k8s_state_owner": {false, true, false, false, -1, staticRoleValidUntilInfinity},+ "k8s_state_writer": {true, false, false, false, -1, staticRoleValidUntilInfinity},+ "k8s_state_exporter": {true, false, false, false, -1, staticRoleValidUntilInfinity},+ "k8s_query_tenant": {false, true, false, false, -1, staticRoleValidUntilInfinity},+ "k8s_query_admin": {false, true, false, false, -1, staticRoleValidUntilInfinity},+ "k8s_role_admin": {true, false, true, false, -1, staticRoleValidUntilInfinity},+ "k8s_reporting_owner": {false, true, false, false, -1, staticRoleValidUntilInfinity},+ "k8s_metabase": {true, false, false, false, -1, staticRoleValidUntilInfinity},}if len(contracts) != len(want) {t.Fatalf("static role contract count = %d, want %d", len(contracts), len(want))@@ -465,7 +486,7 @@ func TestStaticRoleContractsAreAllowlistedAndFixed(t *testing.T) {t.Fatalf("static role contract includes non-allowlisted role %q", contract.role)}if contract.login != expected.login || contract.inherit != expected.inherit || contract.createRole != expected.createRole || contract.createDB != expected.createDB || contract.connectionLimit != expected.connectionLimit || contract.validUntil != expected.validUntil {- t.Errorf("role %s contract = login:%t inherit:%t createrole:%t createdb:%t connection_limit:%d valid_until:%q, want login:%t inherit:%t createrole:%t createdb:%t connection_limit:%d valid_until:%q", contract.role, contract.login, contract.inherit, contract.createRole, contract.createDB, contract.connectionLimit, contract.validUntil, expected.login, expected.inherit, expected.createRole, expected.createDB, expected.connectionLimit, expected.validUntil)+ t.Errorf("role %s contract = login:%t inherit:%t createrole:%t createdb:%t connection_limit:%d valid_until:%d, want login:%t inherit:%t createrole:%t createdb:%t connection_limit:%d valid_until:%d", contract.role, contract.login, contract.inherit, contract.createRole, contract.createDB, contract.connectionLimit, contract.validUntil, expected.login, expected.inherit, expected.createRole, expected.createDB, expected.connectionLimit, expected.validUntil)}delete(want, contract.role)}diff --git a/libs/fleet/backend/database/runtime_ddl.go b/libs/fleet/backend/database/runtime_ddl.gonew file mode 100644index 0000000000..6b830f1467--- /dev/null+++ b/libs/fleet/backend/database/runtime_ddl.go@@ -0,0 +1,414 @@+package database++import (+ "context"+ "fmt"+ "sort"+ "strconv"+ "strings"++ "github.com/jackc/pgx/v5"+)++const createAppliedMigrationsTableStatement = `create table if not exists cyclops_migrations.applied_migrations (+ application_order bigint generated always as identity unique,+ version bigint primary key,+ filename text not null unique,+ sha256 text not null,+ applied_at timestamptz not null default clock_timestamp()+)`++type staticRoleValidUntil uint8++const staticRoleValidUntilInfinity staticRoleValidUntil = 1++type staticRoleSettingName uint8++const (+ staticRoleSettingDefaultTransactionReadOnly staticRoleSettingName = iota + 1+ staticRoleSettingStatementTimeout+ staticRoleSettingIdleInTransactionSessionTimeout+)++var staticRoleSettingSQLNames = map[staticRoleSettingName]string{+ staticRoleSettingDefaultTransactionReadOnly: "default_transaction_read_only",+ staticRoleSettingStatementTimeout: "statement_timeout",+ staticRoleSettingIdleInTransactionSessionTimeout: "idle_in_transaction_session_timeout",+}++type staticRoleSettingsContract map[staticRoleSettingName]string++type reportingObjectKind uint8++const (+ reportingObjectSchema reportingObjectKind = iota + 1+ reportingObjectRelation+ reportingObjectSequence+ reportingObjectRoutine+)++type reportingPrivilege uint8++const (+ reportingPrivilegeUsage reportingPrivilege = iota + 1+ reportingPrivilegeCreate+ reportingPrivilegeSelect+ reportingPrivilegeInsert+ reportingPrivilegeUpdate+ reportingPrivilegeDelete+ reportingPrivilegeTruncate+ reportingPrivilegeReferences+ reportingPrivilegeTrigger+ reportingPrivilegeExecute+)++var reportingPrivilegeSQLNames = map[reportingPrivilege]string{+ reportingPrivilegeUsage: "USAGE",+ reportingPrivilegeCreate: "CREATE",+ reportingPrivilegeSelect: "SELECT",+ reportingPrivilegeInsert: "INSERT",+ reportingPrivilegeUpdate: "UPDATE",+ reportingPrivilegeDelete: "DELETE",+ reportingPrivilegeTruncate: "TRUNCATE",+ reportingPrivilegeReferences: "REFERENCES",+ reportingPrivilegeTrigger: "TRIGGER",+ reportingPrivilegeExecute: "EXECUTE",+}++var reportingPrivilegesByKind = map[reportingObjectKind]map[reportingPrivilege]struct{}{+ reportingObjectSchema: {+ reportingPrivilegeUsage: {},+ reportingPrivilegeCreate: {},+ },+ reportingObjectRelation: {+ reportingPrivilegeSelect: {},+ reportingPrivilegeInsert: {},+ reportingPrivilegeUpdate: {},+ reportingPrivilegeDelete: {},+ reportingPrivilegeTruncate: {},+ reportingPrivilegeReferences: {},+ reportingPrivilegeTrigger: {},+ },+ reportingObjectSequence: {+ reportingPrivilegeUsage: {},+ reportingPrivilegeSelect: {},+ reportingPrivilegeUpdate: {},+ },+ reportingObjectRoutine: {+ reportingPrivilegeExecute: {},+ },+}++type reportingObject struct {+ kind reportingObjectKind+ schema string+ name string+ routineOID uint32+}++type runtimeDDL struct {+ transaction pgx.Tx+}++func (name staticRoleSettingName) sqlName() string {+ return staticRoleSettingSQLNames[name]+}++func staticRoleSettingNameFromSQL(sqlName string) (staticRoleSettingName, bool) {+ for name, approvedSQLName := range staticRoleSettingSQLNames {+ if sqlName == approvedSQLName {+ return name, true+ }+ }+ return 0, false+}++func reportingObjectKindFromSQL(sqlName string) (reportingObjectKind, bool) {+ switch sqlName {+ case "schema":+ return reportingObjectSchema, true+ case "relation":+ return reportingObjectRelation, true+ case "sequence":+ return reportingObjectSequence, true+ case "routine":+ return reportingObjectRoutine, true+ default:+ return 0, false+ }+}++func (kind reportingObjectKind) sqlName() string {+ switch kind {+ case reportingObjectSchema:+ return "schema"+ case reportingObjectRelation:+ return "table"+ case reportingObjectSequence:+ return "sequence"+ case reportingObjectRoutine:+ return "routine"+ default:+ return ""+ }+}++func reportingPrivilegeFromSQL(kind reportingObjectKind, sqlName string) (reportingPrivilege, bool) {+ for privilege := range reportingPrivilegesByKind[kind] {+ if reportingPrivilegeSQLNames[privilege] == sqlName {+ return privilege, true+ }+ }+ return 0, false+}++func (privilege reportingPrivilege) sqlName() string {+ return reportingPrivilegeSQLNames[privilege]+}++func reportingObjectSQLName(object reportingObject) (string, error) {+ switch object.kind {+ case reportingObjectSchema:+ if object.schema == "" || object.name != "" || object.routineOID != 0 {+ return "", errorsInvalidReportingObject(object)+ }+ return pgx.Identifier{object.schema}.Sanitize(), nil+ case reportingObjectRelation, reportingObjectSequence:+ if object.schema == "" || object.name == "" || object.routineOID != 0 {+ return "", errorsInvalidReportingObject(object)+ }+ return pgx.Identifier{object.schema, object.name}.Sanitize(), nil+ case reportingObjectRoutine:+ return "", fmt.Errorf("reporting routine OID must be canonicalized through PostgreSQL")+ default:+ return "", errorsInvalidReportingObject(object)+ }+}++func errorsInvalidReportingObject(object reportingObject) error {+ return fmt.Errorf("invalid reporting object identity for kind %d", object.kind)+}++func canonicalReportingObjectSQL(ctx context.Context, transaction pgx.Tx, object reportingObject) (string, error) {+ if object.kind != reportingObjectRoutine {+ return reportingObjectSQLName(object)+ }+ if object.schema != "" || object.name != "" || object.routineOID == 0 {+ return "", errorsInvalidReportingObject(object)+ }+ var canonical string+ if err := transaction.QueryRow(ctx, `select $1::oid::regprocedure::text`, object.routineOID).Scan(&canonical); err != nil {+ return "", fmt.Errorf("canonicalize reporting routine OID %d: %w", object.routineOID, err)+ }+ return canonical, nil+}++func reportingObjectDescription(object reportingObject) string {+ if object.kind == reportingObjectRoutine {+ return fmt.Sprintf("routine oid %d", object.routineOID)+ }+ name, err := reportingObjectSQLName(object)+ if err != nil {+ return fmt.Sprintf("invalid kind %d", object.kind)+ }+ return object.kind.sqlName() + " " + name+}++func postgresBoolean(value bool) string {+ if value {+ return "TRUE"+ }+ return "FALSE"+}++func staticRoleAlterClauses(contract staticRoleContract, attributes staticRoleAttributes) (string, error) {+ unsafeAttributes := make([]string, 0, 4)+ if attributes.createDB {+ unsafeAttributes = append(unsafeAttributes, "rolcreatedb=true")+ }+ if attributes.super {+ unsafeAttributes = append(unsafeAttributes, "rolsuper=true")+ }+ if attributes.replication {+ unsafeAttributes = append(unsafeAttributes, "rolreplication=true")+ }+ if attributes.bypassRLS {+ unsafeAttributes = append(unsafeAttributes, "rolbypassrls=true")+ }+ if len(unsafeAttributes) != 0 {+ return "", fmt.Errorf("static role %s has unsafe privileged drift: %s; the migration owner cannot safely repair these attributes", contract.role, strings.Join(unsafeAttributes, ", "))+ }+ if contract.validUntil != staticRoleValidUntilInfinity {+ return "", fmt.Errorf("static role %s has unsupported valid-until contract %d; only infinity is supported", contract.role, contract.validUntil)+ }++ clauses := make([]string, 0, 6)+ if attributes.login != contract.login {+ clauses = append(clauses, map[bool]string{true: "LOGIN", false: "NOLOGIN"}[contract.login])+ }+ if attributes.inherit != contract.inherit {+ clauses = append(clauses, map[bool]string{true: "INHERIT", false: "NOINHERIT"}[contract.inherit])+ }+ if attributes.createRole != contract.createRole {+ clauses = append(clauses, map[bool]string{true: "CREATEROLE", false: "NOCREATEROLE"}[contract.createRole])+ }+ if attributes.connectionLimit != contract.connectionLimit {+ clauses = append(clauses, "CONNECTION LIMIT "+strconv.Itoa(contract.connectionLimit))+ }+ if attributes.validUntil != "infinity" {+ clauses = append(clauses, "VALID UNTIL 'infinity'")+ }+ return strings.Join(clauses, " "), nil+}++func newRuntimeDDL(transaction pgx.Tx) runtimeDDL {+ return runtimeDDL{transaction: transaction}+}++func (ddl runtimeDDL) ensureMigrationLedger(ctx context.Context) error {+ for _, statement := range []string{+ `select pg_advisory_xact_lock(hashtext('cyclops-database-migrations'))`,+ `create schema if not exists cyclops_migrations`,+ createAppliedMigrationsTableStatement,+ } {+ if _, err := ddl.transaction.Exec(ctx, statement); err != nil {+ return err+ }+ }+ return nil+}++func (ddl runtimeDDL) reconcileStaticRole(ctx context.Context, contract staticRoleContract, attributes staticRoleAttributes) error {+ clauses, err := staticRoleAlterClauses(contract, attributes)+ if err != nil || clauses == "" {+ return err+ }+ if _, err := ddl.transaction.Exec(ctx, "alter role "+pgx.Identifier{contract.role}.Sanitize()+" "+clauses); err != nil {+ return fmt.Errorf("reconcile static role %s: %w", contract.role, err)+ }+ return nil+}++func (ddl runtimeDDL) revokeStaticMembership(ctx context.Context, contract staticMembershipContract, grantor string) error {+ _, err := ddl.transaction.Exec(ctx,+ "revoke "+pgx.Identifier{contract.role}.Sanitize()++ " from "+pgx.Identifier{contract.member}.Sanitize()++ " granted by "+pgx.Identifier{grantor}.Sanitize()+" restrict",+ )+ return err+}++func (ddl runtimeDDL) grantStaticMembership(ctx context.Context, contract staticMembershipContract, grantor string) error {+ _, err := ddl.transaction.Exec(ctx,+ "grant "+pgx.Identifier{contract.role}.Sanitize()++ " to "+pgx.Identifier{contract.member}.Sanitize()++ " with admin "+postgresBoolean(contract.admin)++ ", inherit "+postgresBoolean(contract.inherit)++ ", set "+postgresBoolean(contract.set)++ " granted by "+pgx.Identifier{grantor}.Sanitize(),+ )+ return err+}++func (ddl runtimeDDL) reconcileStaticRoleSettings(ctx context.Context, role staticRoleContract, actual []staticRoleSetting, desired staticRoleSettingsContract) error {+ if staticRoleSettingsMatch(actual, desired) {+ return nil+ }++ roleIdentifier := pgx.Identifier{role.role}.Sanitize()+ for _, setting := range actual {+ statement := "alter role " + roleIdentifier+ if setting.database != "" {+ statement += " in database " + pgx.Identifier{setting.database}.Sanitize()+ }+ if _, err := ddl.transaction.Exec(ctx, statement+" reset all"); err != nil {+ return fmt.Errorf("reset static role settings for %s: %w", role.role, err)+ }+ }++ settingNames := make([]staticRoleSettingName, 0, len(desired))+ for name := range desired {+ if name.sqlName() == "" {+ return fmt.Errorf("unsupported static role setting %d for %s", name, role.role)+ }+ settingNames = append(settingNames, name)+ }+ sort.Slice(settingNames, func(left, right int) bool { return settingNames[left] < settingNames[right] })+ for _, name := range settingNames {+ var value string+ if err := ddl.transaction.QueryRow(ctx, `select format('%L', $1::text)`, desired[name]).Scan(&value); err != nil {+ return fmt.Errorf("quote static role setting %s for %s: %w", name.sqlName(), role.role, err)+ }+ if _, err := ddl.transaction.Exec(ctx, "alter role "+roleIdentifier+" set "+name.sqlName()+" = "+value); err != nil {+ return fmt.Errorf("reconcile static role setting %s for %s: %w", name.sqlName(), role.role, err)+ }+ }+ return nil+}++func (ddl runtimeDDL) setPassword(ctx context.Context, credential credential) error {+ var quotedPassword string+ if err := ddl.transaction.QueryRow(ctx, `select format('%L', $1::text)`, credential.Password).Scan("edPassword); err != nil {+ return fmt.Errorf("quote password for role %s: %w", credential.Role, err)+ }+ _, err := ddl.transaction.Exec(ctx, "alter role "+pgx.Identifier{credential.Role}.Sanitize()+" password "+quotedPassword)+ return err+}++func (ddl runtimeDDL) setLocalRole(ctx context.Context, role string) error {+ _, err := ddl.transaction.Exec(ctx, "set local role "+pgx.Identifier{role}.Sanitize())+ return err+}++func (ddl runtimeDDL) resetRole(ctx context.Context) error {+ _, err := ddl.transaction.Exec(ctx, "reset role")+ return err+}++func (ddl runtimeDDL) grantReportingACL(ctx context.Context, acl reportingACL) (runErr error) {+ if !isExpectedReportingACL(acl) {+ return fmt.Errorf("unsupported reporting ACL grant on %s", reportingObjectDescription(acl.object))+ }+ objectName, err := canonicalReportingObjectSQL(ctx, ddl.transaction, acl.object)+ if err != nil {+ return err+ }+ if err := ddl.setLocalRole(ctx, acl.owner); err != nil {+ return err+ }+ defer func() {+ if err := ddl.resetRole(ctx); err != nil && runErr == nil {+ runErr = err+ }+ }()++ statement := "grant " + acl.privilege.sqlName() + " on " + acl.object.kind.sqlName() + " " + objectName + " to " + pgx.Identifier{acl.grantee}.Sanitize()+ _, runErr = ddl.transaction.Exec(ctx, statement)+ return runErr+}++func (ddl runtimeDDL) revokeReportingACL(ctx context.Context, acl reportingACL) (runErr error) {+ if _, ok := reportingPrivilegesByKind[acl.object.kind][acl.privilege]; !ok {+ return fmt.Errorf("unsupported reporting privilege for %s", reportingObjectDescription(acl.object))+ }+ objectName, err := canonicalReportingObjectSQL(ctx, ddl.transaction, acl.object)+ if err != nil {+ return err+ }+ if err := ddl.setLocalRole(ctx, acl.owner); err != nil {+ return err+ }+ defer func() {+ if err := ddl.resetRole(ctx); err != nil && runErr == nil {+ runErr = err+ }+ }()++ grantee := pgx.Identifier{acl.grantee}.Sanitize()+ if acl.grantee == "PUBLIC" {+ grantee = "public"+ }+ statement := "revoke " + acl.privilege.sqlName() + " on " + acl.object.kind.sqlName() + " " + objectName + " from " + grantee + " granted by " + pgx.Identifier{acl.owner}.Sanitize()+ _, runErr = ddl.transaction.Exec(ctx, statement)+ return runErr+}diff --git a/libs/fleet/backend/database/runtime_ddl_test.go b/libs/fleet/backend/database/runtime_ddl_test.gonew file mode 100644index 0000000000..b9b0052b92--- /dev/null+++ b/libs/fleet/backend/database/runtime_ddl_test.go@@ -0,0 +1,548 @@+package database++import (+ "fmt"+ "go/ast"+ "go/parser"+ "go/token"+ "os"+ "path/filepath"+ "runtime"+ "sort"+ "strconv"+ "strings"+ "testing"+)++const expectedInsertAppliedMigrationStatement = `insert into cyclops_migrations.applied_migrations (version, filename, sha256) values ($1, $2, $3)`++func TestMigrationAuthorityExecArguments(t *testing.T) {+ packageDir := databasePackageDir(t)+ entries, err := os.ReadDir(packageDir)+ if err != nil {+ t.Fatal(err)+ }++ var offenders []string+ for _, entry := range entries {+ name := entry.Name()+ if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") || name == "runtime_ddl.go" {+ continue+ }+ source, err := os.ReadFile(filepath.Join(packageDir, name))+ if err != nil {+ t.Fatal(err)+ }+ offenders = append(offenders, migrationAuthorityOffenders(name, source)...)+ }+ if len(offenders) != 0 {+ t.Fatalf("runtime SQL authority escapes runtime_ddl.go:\n%s", strings.Join(offenders, "\n"))+ }+}++func TestMigrationAuthorityRejectsDDLMutations(t *testing.T) {+ tests := []struct {+ name string+ source string+ want string+ }{+ {+ name: "variable create index",+ source: `package database+func mutate() { statement := "CREATE INDEX sneaky_idx ON public.items (id)"; transaction.Exec(ctx, statement) }`,+ want: "must be a literal non-DDL statement",+ },+ {+ name: "arbitrary alter role superuser",+ source: `package database+func mutate() { transaction.Exec(ctx, "ALTER ROLE victim SUPERUSER") }`,+ want: "DDL statement",+ },+ {+ name: "file SQL outside migration application site",+ source: `package database+func mutate() { transaction.Exec(ctx, file.SQL) }`,+ want: "must be a literal non-DDL statement",+ },+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ offenders := migrationAuthorityOffenders("mutation.go", []byte(test.source))+ if len(offenders) != 1 || !strings.Contains(offenders[0], test.want) {+ t.Fatalf("offenders = %#v, want one containing %q", offenders, test.want)+ }+ })+ }+}++func TestMigrationAuthorityRejectsLedgerConstantBypasses(t *testing.T) {+ baseline, err := os.ReadFile(filepath.Join(databasePackageDir(t), "migrator.go"))+ if err != nil {+ t.Fatal(err)+ }+ expectedLiteral := "const insertAppliedMigrationStatement = `insert into cyclops_migrations.applied_migrations (version, filename, sha256) values ($1, $2, $3)`"+ tests := []struct {+ name string+ source string+ }{+ {+ name: "changed package constant",+ source: strings.Replace(string(baseline), expectedLiteral,+ "const insertAppliedMigrationStatement = `delete from cyclops_migrations.applied_migrations`", 1),+ },+ {+ name: "shadow declaration",+ source: string(baseline) + "\nfunc shadow() { const insertAppliedMigrationStatement = `select 1`; _ = insertAppliedMigrationStatement }\n",+ },+ {+ name: "second non exec use",+ source: string(baseline) + "\nfunc reuseLedgerSQL() { _ = insertAppliedMigrationStatement }\n",+ },+ {+ name: "second exec use",+ source: string(baseline) + "\nfunc reuseLedgerSQL() { transaction.Exec(ctx, insertAppliedMigrationStatement) }\n",+ },+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ if offenders := migrationAuthorityOffenders("migrator.go", []byte(test.source)); len(offenders) == 0 {+ t.Fatal("mutation escaped migration authority contract")+ }+ })+ }+}++func TestMigrationAuthorityRejectsMigrationSQLCallBypasses(t *testing.T) {+ baseline, err := os.ReadFile(filepath.Join(databasePackageDir(t), "migrator.go"))+ if err != nil {+ t.Fatal(err)+ }+ exactCall := "transaction.Exec(ctx, file.SQL)"+ tests := []struct {+ name string+ source string+ }{+ {+ name: "second call inside Run",+ source: strings.Replace(string(baseline),+ "\t\tif _, err := "+exactCall+"; err != nil {",+ "\t\t_, _ = "+exactCall+"\n\t\tif _, err := "+exactCall+"; err != nil {", 1),+ },+ {+ name: "wrong receiver",+ source: strings.Replace(string(baseline), exactCall, "other.Exec(ctx, file.SQL)", 1),+ },+ {+ name: "extra argument",+ source: strings.Replace(string(baseline), exactCall, "transaction.Exec(ctx, file.SQL, sneaky)", 1),+ },+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ if offenders := migrationAuthorityOffenders("migrator.go", []byte(test.source)); len(offenders) == 0 {+ t.Fatal("mutation escaped migration authority contract")+ }+ })+ }+}++func TestMigrationAuthorityIgnoresCommentsAndInertStrings(t *testing.T) {+ source := []byte(`package database+// transaction.Exec(ctx, "CREATE INDEX ignored ON public.items (id)")+var inert = "ALTER ROLE ignored SUPERUSER"+func mutate() { _ = "DROP TABLE ignored" }+`)+ if offenders := migrationAuthorityOffenders("mutation.go", source); len(offenders) != 0 {+ t.Fatalf("offenders = %#v, want none", offenders)+ }+}++func TestStaticRoleSettingNamesAreClosed(t *testing.T) {+ want := map[string]staticRoleSettingName{+ "default_transaction_read_only": staticRoleSettingDefaultTransactionReadOnly,+ "statement_timeout": staticRoleSettingStatementTimeout,+ "idle_in_transaction_session_timeout": staticRoleSettingIdleInTransactionSessionTimeout,+ }+ if len(staticRoleSettingSQLNames) != len(want) {+ t.Fatalf("static role setting SQL name count = %d, want %d", len(staticRoleSettingSQLNames), len(want))+ }+ for sqlName, settingName := range want {+ got, ok := staticRoleSettingNameFromSQL(sqlName)+ if !ok || got != settingName {+ t.Fatalf("staticRoleSettingNameFromSQL(%q) = %v, %t; want %v, true", sqlName, got, ok, settingName)+ }+ if got.sqlName() != sqlName {+ t.Fatalf("static role setting SQL name = %q, want %q", got.sqlName(), sqlName)+ }+ }+ for _, sqlName := range []string{"search_path", "statement_timeout = 0", "unknown"} {+ if _, ok := staticRoleSettingNameFromSQL(sqlName); ok {+ t.Fatalf("staticRoleSettingNameFromSQL(%q) accepted an unapproved setting", sqlName)+ }+ }+}++func TestStaticRoleSettingsMatchRejectsUnknownActualSetting(t *testing.T) {+ desired := staticRoleSettingsContract{staticRoleSettingStatementTimeout: "20000ms"}+ actual := []staticRoleSetting{{+ values: staticRoleSettingsContract{staticRoleSettingStatementTimeout: "20000ms"},+ hasUnknown: true,+ }}+ if staticRoleSettingsMatch(actual, desired) {+ t.Fatal("unknown actual role setting matched the closed contract")+ }+}++func TestReportingObjectKindsAndPrivilegesAreClosed(t *testing.T) {+ kinds := map[string]reportingObjectKind{+ "schema": reportingObjectSchema,+ "relation": reportingObjectRelation,+ "sequence": reportingObjectSequence,+ "routine": reportingObjectRoutine,+ }+ for sqlName, want := range kinds {+ got, ok := reportingObjectKindFromSQL(sqlName)+ if !ok || got != want {+ t.Fatalf("reportingObjectKindFromSQL(%q) = %v, %t; want %v, true", sqlName, got, ok, want)+ }+ }+ if _, ok := reportingObjectKindFromSQL("table; drop schema public"); ok {+ t.Fatal("accepted arbitrary reporting object kind")+ }++ valid := map[reportingObjectKind][]string{+ reportingObjectSchema: {"USAGE", "CREATE"},+ reportingObjectRelation: {"SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER"},+ reportingObjectSequence: {"USAGE", "SELECT", "UPDATE"},+ reportingObjectRoutine: {"EXECUTE"},+ }+ if len(reportingPrivilegesByKind) != len(valid) {+ t.Fatalf("reporting privilege kind count = %d, want %d", len(reportingPrivilegesByKind), len(valid))+ }+ approvedPrivileges := map[reportingPrivilege]struct{}{}+ for kind, privileges := range valid {+ if len(reportingPrivilegesByKind[kind]) != len(privileges) {+ t.Fatalf("reporting privileges for kind %v = %d, want %d", kind, len(reportingPrivilegesByKind[kind]), len(privileges))+ }+ for _, sqlName := range privileges {+ privilege, ok := reportingPrivilegeFromSQL(kind, sqlName)+ if !ok || privilege.sqlName() != sqlName {+ t.Fatalf("reportingPrivilegeFromSQL(%v, %q) = %v, %t", kind, sqlName, privilege, ok)+ }+ approvedPrivileges[privilege] = struct{}{}+ }+ if _, ok := reportingPrivilegeFromSQL(kind, "SELECT; GRANT ALL"); ok {+ t.Fatalf("accepted arbitrary privilege for kind %v", kind)+ }+ }+ if len(reportingPrivilegeSQLNames) != len(approvedPrivileges) {+ t.Fatalf("reporting privilege SQL name count = %d, want %d", len(reportingPrivilegeSQLNames), len(approvedPrivileges))+ }+ if _, ok := reportingPrivilegeFromSQL(reportingObjectSchema, "SELECT"); ok {+ t.Fatal("accepted relation privilege for schema")+ }+}++func TestReportingObjectSQLNameQuotesSeparatedIdentifiers(t *testing.T) {+ tests := []struct {+ name string+ object reportingObject+ want string+ }{+ {name: "schema", object: reportingObject{kind: reportingObjectSchema, schema: `odd schema`}, want: `"odd schema"`},+ {name: "relation", object: reportingObject{kind: reportingObjectRelation, schema: `odd schema`, name: `probe.table`}, want: `"odd schema"."probe.table"`},+ {name: "sequence", object: reportingObject{kind: reportingObjectSequence, schema: `odd schema`, name: `probe.sequence`}, want: `"odd schema"."probe.sequence"`},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ got, err := reportingObjectSQLName(test.object)+ if err != nil || got != test.want {+ t.Fatalf("reportingObjectSQLName(%#v) = %q, %v; want %q, nil", test.object, got, err, test.want)+ }+ })+ }+ if _, err := reportingObjectSQLName(reportingObject{kind: reportingObjectRoutine, routineOID: 1}); err == nil {+ t.Fatal("routine OID must be canonicalized through PostgreSQL")+ }+}++func TestRuntimeDDLHasClosedMethodSet(t *testing.T) {+ source, err := os.ReadFile(filepath.Join(databasePackageDir(t), "runtime_ddl.go"))+ if err != nil {+ t.Fatal(err)+ }+ methods, err := runtimeDDLMethods(source)+ if err != nil {+ t.Fatal(err)+ }+ want := []string{+ "ensureMigrationLedger",+ "grantReportingACL",+ "grantStaticMembership",+ "reconcileStaticRole",+ "reconcileStaticRoleSettings",+ "resetRole",+ "revokeReportingACL",+ "revokeStaticMembership",+ "setLocalRole",+ "setPassword",+ }+ if strings.Join(methods, ",") != strings.Join(want, ",") {+ t.Fatalf("runtime DDL methods = %v, want %v", methods, want)+ }+}++func TestRuntimeDDLRejectsExtraHelperOperation(t *testing.T) {+ source := []byte(`package database+ type runtimeDDL struct{}+ func (ddl runtimeDDL) ensureMigrationLedger() {}+ func (ddl runtimeDDL) grantReportingACL() {}+ func (ddl runtimeDDL) grantStaticMembership() {}+ func (ddl runtimeDDL) reconcileStaticRole() {}+ func (ddl runtimeDDL) reconcileStaticRoleSettings() {}+ func (ddl runtimeDDL) resetRole() {}+ func (ddl runtimeDDL) revokeReportingACL() {}+ func (ddl runtimeDDL) revokeStaticMembership() {}+ func (ddl runtimeDDL) setLocalRole() {}+ func (ddl runtimeDDL) setPassword() {}+ func (ddl runtimeDDL) createIndex() {}+ `)+ methods, err := runtimeDDLMethods(source)+ if err != nil {+ t.Fatal(err)+ }+ if !strings.Contains(strings.Join(methods, ","), "createIndex") {+ t.Fatalf("methods = %v, want extra helper operation", methods)+ }+}++func databasePackageDir(t *testing.T) string {+ t.Helper()+ _, file, _, ok := runtime.Caller(0)+ if !ok {+ t.Fatal("locate database package")+ }+ return filepath.Dir(file)+}++func migrationAuthorityOffenders(filename string, source []byte) []string {+ fileSet := token.NewFileSet()+ file, err := parser.ParseFile(fileSet, filename, source, 0)+ if err != nil {+ return []string{fmt.Sprintf("parse %s: %v", filename, err)}+ }++ var offenders []string+ if filename == "migrator.go" {+ offenders = append(offenders, migratorDeclarationOffenders(fileSet, file)...)+ }+ ast.Inspect(file, func(node ast.Node) bool {+ call, ok := node.(*ast.CallExpr)+ if !ok || !isExecCall(call) || len(call.Args) < 2 {+ return true+ }+ functionName := functionNameAt(file, call.Pos())+ if isExactMigrationFileSQLCall(filename, functionName, call) || isExactLedgerInsertCall(filename, functionName, call) {+ return true+ }+ argument := call.Args[1]+ literal, ok := argument.(*ast.BasicLit)+ if !ok || literal.Kind != token.STRING {+ offenders = append(offenders, fmt.Sprintf("%s: Exec SQL must be a literal non-DDL statement, the exact ledger insert call, or the exact file.SQL migration call", fileSet.Position(argument.Pos())))+ return true+ }+ value, err := strconvUnquote(literal.Value)+ if err != nil {+ offenders = append(offenders, fmt.Sprintf("%s: invalid SQL literal", fileSet.Position(argument.Pos())))+ return true+ }+ if isDDL(value) {+ offenders = append(offenders, fmt.Sprintf("%s: DDL statement outside runtime_ddl.go", fileSet.Position(argument.Pos())))+ }+ return true+ })+ return offenders+}++func migratorDeclarationOffenders(fileSet *token.FileSet, file *ast.File) []string {+ var offenders []string+ packageDeclarations := 0+ validPackageDeclaration := false+ declarations := 0+ uses := 0+ ledgerCalls := 0+ migrationSQLReferences := 0+ migrationSQLCalls := 0++ for _, declaration := range file.Decls {+ general, ok := declaration.(*ast.GenDecl)+ if !ok || general.Tok != token.CONST {+ continue+ }+ for _, specification := range general.Specs {+ value, ok := specification.(*ast.ValueSpec)+ if !ok {+ continue+ }+ for _, name := range value.Names {+ if name.Name != "insertAppliedMigrationStatement" {+ continue+ }+ packageDeclarations+++ if len(value.Names) == 1 && len(value.Values) == 1 {+ literal, ok := value.Values[0].(*ast.BasicLit)+ if ok && literal.Kind == token.STRING {+ statement, err := strconvUnquote(literal.Value)+ validPackageDeclaration = err == nil && statement == expectedInsertAppliedMigrationStatement+ }+ }+ }+ }+ }++ ast.Inspect(file, func(node ast.Node) bool {+ switch node := node.(type) {+ case *ast.Ident:+ if node.Name != "insertAppliedMigrationStatement" {+ return true+ }+ if node.Obj != nil && node.Obj.Pos() == node.Pos() {+ declarations+++ } else {+ uses+++ }+ case *ast.SelectorExpr:+ if isFileSQLSelector(node) {+ migrationSQLReferences+++ }+ case *ast.CallExpr:+ functionName := functionNameAt(file, node.Pos())+ if isExactLedgerInsertCall("migrator.go", functionName, node) {+ ledgerCalls+++ }+ if isExactMigrationFileSQLCall("migrator.go", functionName, node) {+ migrationSQLCalls+++ }+ }+ return true+ })++ if packageDeclarations != 1 || !validPackageDeclaration {+ offenders = append(offenders, "migrator.go: insertAppliedMigrationStatement must have exactly one package-level const declaration with the exact approved DML literal")+ }+ if declarations != 1 {+ offenders = append(offenders, fmt.Sprintf("migrator.go: insertAppliedMigrationStatement declaration count = %d, want 1", declarations))+ }+ if uses != 1 || ledgerCalls != 1 {+ offenders = append(offenders, fmt.Sprintf("migrator.go: insertAppliedMigrationStatement uses = %d and exact Exec calls = %d, want 1 and 1", uses, ledgerCalls))+ }+ if migrationSQLReferences != 1 || migrationSQLCalls != 1 {+ offenders = append(offenders, fmt.Sprintf("migrator.go: file.SQL references = %d and exact Exec calls = %d, want 1 and 1", migrationSQLReferences, migrationSQLCalls))+ }+ _ = fileSet+ return offenders+}++func isExecCall(call *ast.CallExpr) bool {+ selector, ok := call.Fun.(*ast.SelectorExpr)+ return ok && selector.Sel.Name == "Exec"+}++func isExactMigrationFileSQLCall(filename, functionName string, call *ast.CallExpr) bool {+ if filename != "migrator.go" || functionName != "Run" || len(call.Args) != 2 || !isNamedSelectorCall(call, "transaction", "Exec") {+ return false+ }+ context, ok := call.Args[0].(*ast.Ident)+ selector, selectorOK := call.Args[1].(*ast.SelectorExpr)+ return ok && context.Name == "ctx" && selectorOK && isFileSQLSelector(selector)+}++func isExactLedgerInsertCall(filename, functionName string, call *ast.CallExpr) bool {+ if filename != "migrator.go" || functionName != "Run" || len(call.Args) != 5 || !isNamedSelectorCall(call, "transaction", "Exec") {+ return false+ }+ context, contextOK := call.Args[0].(*ast.Ident)+ statement, statementOK := call.Args[1].(*ast.Ident)+ return contextOK && context.Name == "ctx" && statementOK && statement.Name == "insertAppliedMigrationStatement" &&+ isNamedSelector(call.Args[2], "file", "Version") &&+ isNamedSelector(call.Args[3], "file", "Name") &&+ isNamedSelector(call.Args[4], "file", "SHA256")+}++func isNamedSelectorCall(call *ast.CallExpr, receiver, method string) bool {+ selector, ok := call.Fun.(*ast.SelectorExpr)+ if !ok {+ return false+ }+ identifier, ok := selector.X.(*ast.Ident)+ return ok && identifier.Name == receiver && selector.Sel.Name == method+}++func isNamedSelector(expression ast.Expr, receiver, field string) bool {+ selector, ok := expression.(*ast.SelectorExpr)+ if !ok {+ return false+ }+ identifier, ok := selector.X.(*ast.Ident)+ return ok && identifier.Name == receiver && selector.Sel.Name == field+}++func isFileSQLSelector(selector *ast.SelectorExpr) bool {+ identifier, ok := selector.X.(*ast.Ident)+ return ok && identifier.Name == "file" && selector.Sel.Name == "SQL"+}++func functionNameAt(file *ast.File, position token.Pos) string {+ for _, declaration := range file.Decls {+ function, ok := declaration.(*ast.FuncDecl)+ if ok && function.Pos() <= position && position <= function.End() {+ return function.Name.Name+ }+ }+ return ""+}++func isDDL(statement string) bool {+ statement = strings.TrimSpace(strings.ToUpper(statement))+ for _, prefix := range []string{"CREATE ", "ALTER ", "DROP ", "GRANT ", "REVOKE ", "SET ", "RESET "} {+ if strings.HasPrefix(statement, prefix) {+ return true+ }+ }+ return false+}++func runtimeDDLMethods(source []byte) ([]string, error) {+ fileSet := token.NewFileSet()+ file, err := parser.ParseFile(fileSet, "runtime_ddl.go", source, 0)+ if err != nil {+ return nil, err+ }+ methods := []string{}+ for _, declaration := range file.Decls {+ function, ok := declaration.(*ast.FuncDecl)+ if !ok || function.Recv == nil || len(function.Recv.List) != 1 {+ continue+ }+ if receiverName(function.Recv.List[0].Type) == "runtimeDDL" {+ methods = append(methods, function.Name.Name)+ }+ }+ sort.Strings(methods)+ return methods, nil+}++func receiverName(expression ast.Expr) string {+ switch expression := expression.(type) {+ case *ast.Ident:+ return expression.Name+ case *ast.StarExpr:+ return receiverName(expression.X)+ default:+ return ""+ }+}++func strconvUnquote(value string) (string, error) {+ return strconv.Unquote(value)+}diff --git a/libs/fleet/backend/database/test-setup.sh b/libs/fleet/backend/database/test-setup.shnew file mode 100755index 0000000000..06387025e3--- /dev/null+++ b/libs/fleet/backend/database/test-setup.sh@@ -0,0 +1,163 @@+#!/usr/bin/env bash+set -euo pipefail++if [[ "${CYCLOPS_TEST_DATABASE_MIGRATOR_ISOLATED_CLUSTER:-}" != "1" ]]; then+ echo "set CYCLOPS_TEST_DATABASE_MIGRATOR_ISOLATED_CLUSTER=1 for a disposable local PostgreSQL cluster" >&2+ exit 2+fi+: "${CYCLOPS_TEST_DATABASE_ADMIN_URL:?set the PostgreSQL maintenance URL}"++admin_url="$CYCLOPS_TEST_DATABASE_ADMIN_URL"+seed="${CYCLOPS_TEST_DATABASE_SUFFIX:-${GITHUB_RUN_ID:-local}-${GITHUB_JOB:-postgres}-${GITHUB_RUN_ATTEMPT:-1}}"+token="$(printf '%s' "$seed" | sha256sum | cut -c1-16)"+migration_owner="cyclops_test_migrator_${token}"+database_name="cyclops_test_${token}"+migration_password="migration-${token}"+static_roles=(+ cyclops_app+ k8s_state_owner+ k8s_state_writer+ k8s_state_exporter+ k8s_query_tenant+ k8s_query_admin+ k8s_role_admin+ k8s_reporting_owner+ k8s_metabase+)++base_url="${admin_url%%\?*}"+query_suffix="${admin_url#"$base_url"}"+authority="${base_url%/*}"+authority="${authority#*://}"+authority="${authority##*@}"+scheme="${base_url%%://*}"++# Do not allow inherited libpq settings to override this validated maintenance URL.+for variable in "${!PG@}"; do+ unset "$variable"+done++# Keep the maintenance URL out of process arguments; psql reads libpq settings.+if ! connection_settings="$(CYCLOPS_TEST_DATABASE_ADMIN_URL="$admin_url" python3 - <<'PY'+import os+import shlex+from urllib.parse import parse_qs, unquote, urlparse++parsed = urlparse(os.environ["CYCLOPS_TEST_DATABASE_ADMIN_URL"])+if parsed.scheme not in {"postgres", "postgresql"} or not parsed.hostname:+ raise SystemExit("CYCLOPS_TEST_DATABASE_ADMIN_URL must be a PostgreSQL URL")+if parsed.hostname not in {"localhost", "127.0.0.1", "::1"}:+ raise SystemExit("CYCLOPS_TEST_DATABASE_ADMIN_URL host must be loopback")+if unquote(parsed.path.lstrip("/")) != "postgres":+ raise SystemExit("CYCLOPS_TEST_DATABASE_ADMIN_URL must use maintenance database postgres")++settings = {+ "PGHOST": parsed.hostname,+ "PGPORT": str(parsed.port or 5432),+ "PGUSER": unquote(parsed.username or "postgres"),+ "PGPASSWORD": unquote(parsed.password or ""),+ "PGDATABASE": "postgres",+}+sslmode = parse_qs(parsed.query).get("sslmode", [None])[0]+if sslmode:+ settings["PGSSLMODE"] = sslmode+for name, value in settings.items():+ print(f"export {name}={shlex.quote(value)}")+PY+)"; then+ exit 2+fi+eval "$connection_settings"+unset CYCLOPS_TEST_DATABASE_ADMIN_URL admin_url connection_settings++url_for() {+ local role="$1"+ local password="$2"+ printf '%s://%s:%s@%s/%s%s' "$scheme" "$role" "$password" "$authority" "$database_name" "$query_suffix"+}++migration_url="$(url_for "$migration_owner" "$migration_password")"+app_url="$(url_for cyclops_app cyclops-app-password)"+writer_url="$(url_for k8s_state_writer state-writer-password)"+exporter_url="$(url_for k8s_state_exporter state-exporter-password)"+role_admin_url="$(url_for k8s_role_admin role-admin-password)"+metabase_url="$(url_for k8s_metabase metabase-password)"++env_file="${CYCLOPS_TEST_DATABASE_ENV_FILE:-${GITHUB_ENV:-}}"+if [[ -z "$env_file" ]]; then+ echo "set CYCLOPS_TEST_DATABASE_ENV_FILE or GITHUB_ENV" >&2+ exit 2+fi++run_psql() {+ psql --set ON_ERROR_STOP=1 "$@"+}++if [[ "$(printf 'select rolsuper from pg_roles where rolname = current_user;\n' | run_psql -At)" != "t" ]]; then+ echo "CYCLOPS_TEST_DATABASE_ADMIN_URL user must be a true PostgreSQL superuser" >&2+ exit 2+fi++role_literals="'$migration_owner'"+for role in "${static_roles[@]}"; do+ role_literals+=", '$role'"+done+preexisting="$(printf "select exists (select 1 from pg_database where datname = '%s') or exists (select 1 from pg_roles where rolname in (%s));\n" "$database_name" "$role_literals" | run_psql -At)"+if [[ "$preexisting" == "t" ]]; then+ echo "refusing to modify pre-existing Cyclops test database or static roles" >&2+ exit 2+fi++created_migration_owner=0+created_database=0+created_static_roles=0+cleanup() {+ local status=$?+ trap - EXIT+ set +e+ if ((created_database)); then+ {+ printf "select pg_terminate_backend(pid) from pg_stat_activity where datname = '%s' and pid <> pg_backend_pid();\n" "$database_name"+ printf 'drop database "%s";\n' "$database_name"+ } | run_psql+ fi+ if ((created_static_roles)); then+ for role in "${static_roles[@]}"; do+ printf 'drop role "%s";\n' "$role" | run_psql+ done+ fi+ if ((created_migration_owner)); then+ printf 'drop role "%s";\n' "$migration_owner" | run_psql+ fi+ exit "$status"+}+trap cleanup EXIT++run_psql <<EOF_SQL+create role "${migration_owner}" login createrole nosuperuser nocreatedb password '${migration_password}';+EOF_SQL+created_migration_owner=1++run_psql <<EOF_SQL+create database "${database_name}" owner "${migration_owner}";+EOF_SQL+created_database=1++created_static_roles=1+MIGRATION_DATABASE_URL="$migration_url" \+DATABASE_URL="$app_url" \+STATE_WRITER_DATABASE_URL="$writer_url" \+STATE_EXPORTER_DATABASE_URL="$exporter_url" \+STATE_ROLE_ADMIN_DATABASE_URL="$role_admin_url" \+METABASE_DATABASE_URL="$metabase_url" \+go run ./cmd/db-migrate++cat >> "$env_file" <<EOF_ENV+CYCLOPS_TEST_RUNTIME_DATABASE_URL=${app_url}+CYCLOPS_TEST_MIGRATION_DATABASE_URL=${migration_url}+CYCLOPS_TEST_STATE_WRITER_DATABASE_URL=${writer_url}+CYCLOPS_TEST_STATE_ROLE_ADMIN_DATABASE_URL=${role_admin_url}+CYCLOPS_TEST_DATABASE_MIGRATOR_ISOLATED_CLUSTER=1+EOF_ENV++trap - EXITdiff --git a/libs/fleet/backend/docs/docs.go b/libs/fleet/backend/docs/docs.goindex aa164910a5..0775bec6b2 100644--- a/libs/fleet/backend/docs/docs.go+++ b/libs/fleet/backend/docs/docs.go@@ -945,7 +945,7 @@ const docTemplate = `{"tags": ["health"],- "summary": "Liveness/readiness probe",+ "summary": "Liveness probe","responses": {"200": {"description": "OK",diff --git a/libs/fleet/backend/docs/swagger.json b/libs/fleet/backend/docs/swagger.jsonindex f7a4bfe277..1c62e97fb6 100644--- a/libs/fleet/backend/docs/swagger.json+++ b/libs/fleet/backend/docs/swagger.json@@ -938,7 +938,7 @@"tags": ["health"],- "summary": "Liveness/readiness probe",+ "summary": "Liveness probe","responses": {"200": {"description": "OK",diff --git a/libs/fleet/backend/docs/swagger.yaml b/libs/fleet/backend/docs/swagger.yamlindex 2ff2981890..5b73fc72cf 100644--- a/libs/fleet/backend/docs/swagger.yaml+++ b/libs/fleet/backend/docs/swagger.yaml@@ -790,7 +790,7 @@ paths:description: OKschema:$ref: '#/definitions/handlers.HealthResponse'- summary: Liveness/readiness probe+ summary: Liveness probetags:- healthsecurityDefinitions:diff --git a/libs/fleet/backend/githubtrust/store.go b/libs/fleet/backend/githubtrust/store.goindex 94461843b0..c7ea9e85f8 100644--- a/libs/fleet/backend/githubtrust/store.go+++ b/libs/fleet/backend/githubtrust/store.go@@ -2,10 +2,6 @@ package githubtrust// Postgres-backed persistence for GitHub Actions OIDC trust policies.//-// Schema is derived from the Policy struct via GORM AutoMigrate — no manual-// DDL. Queries use pgx directly (the GORM postgres driver also uses pgx-// under the hood, so there is no driver conflict).-//// Listing is indexed by owner_sub (List); ResolveByRepository (the per-request// auth hot path) is indexed by repository. Every read takes the owner so the// storage layer enforces the same tenant isolation the HTTP layer does —@@ -21,8 +17,6 @@ import ("github.com/google/uuid""github.com/jackc/pgx/v5""github.com/jackc/pgx/v5/pgxpool"- "gorm.io/driver/postgres"- "gorm.io/gorm")// ErrNotFound is returned by Update when the policy no longer exists for the@@ -42,9 +36,8 @@ type Store interface {type pgStore struct{ pool *pgxpool.Pool }-// New opens a connection pool against url (a Postgres DSN or URL) and ensures-// the schema exists by running GORM AutoMigrate against the Policy struct. An-// empty url disables the feature (returns nil, nil) so the handlers reply+// New opens and verifies a connection pool against url (a Postgres DSN or URL).+// An empty url disables the feature (returns nil, nil) so the handlers reply// 503, mirroring the previous Redis behaviour. A startup connectivity blip is// non-fatal to the process: initialization returns an error so the caller can// leave the routes disabled rather than exposing a store without its table.@@ -57,8 +50,6 @@ func New(ctx context.Context, url string) (Store, error) {if err != nil {return nil, fmt.Errorf("connect postgres: %w", err)}- s := &pgStore{pool: pool}-initCtx, cancel := context.WithTimeout(ctx, 10*time.Second)defer cancel()@@ -67,27 +58,7 @@ func New(ctx context.Context, url string) (Store, error) {return nil, fmt.Errorf("ping postgres: %w", err)}- // AutoMigrate derives the table schema from the Policy struct — no manual- // DDL. GORM creates the table, columns, and indexes based on struct tags.- // We open a throwaway gorm.DB from the same URL for migration only; pgx- // is used for all queries.- gormDB, err := gorm.Open(postgres.Open(url), &gorm.Config{})- if err != nil {- pool.Close()- return nil, fmt.Errorf("open postgres migrator: %w", err)- }- gormSQLDB, err := gormDB.DB()- if err != nil {- pool.Close()- return nil, fmt.Errorf("get postgres migrator connection: %w", err)- }- defer gormSQLDB.Close()- if err := gormDB.WithContext(initCtx).AutoMigrate(&Policy{}); err != nil {- pool.Close()- return nil, fmt.Errorf("auto-migrate github trust policies: %w", err)- }-- return s, nil+ return &pgStore{pool: pool}, nil}func (s *pgStore) List(ctx context.Context, ownerSub string) ([]*Policy, error) {diff --git a/libs/fleet/backend/githubtrust/store_pg_test.go b/libs/fleet/backend/githubtrust/store_pg_test.goindex c6984fa9b5..7dd96a1a83 100644--- a/libs/fleet/backend/githubtrust/store_pg_test.go+++ b/libs/fleet/backend/githubtrust/store_pg_test.go@@ -4,18 +4,24 @@ import ("context""os""testing"++ "cyclops-cs-backend/database")// TestPostgresStoreRoundTrip exercises the real Postgres-backed store against-// a live database. It is skipped unless CYCLOPS_TEST_DATABASE_URL points at a+// a live database. It is skipped unless CYCLOPS_TEST_RUNTIME_DATABASE_URL points at a// throwaway Postgres (e.g. `docker run --rm -e POSTGRES_PASSWORD=pg -p-// 5432:5432 postgres:16`). The test creates the schema on its own via New.+// 5432:5432 postgres:16`). The database must already have migration version 1+// applied by the standalone migrator.func TestPostgresStoreRoundTrip(t *testing.T) {- url := os.Getenv("CYCLOPS_TEST_DATABASE_URL")+ url := os.Getenv("CYCLOPS_TEST_RUNTIME_DATABASE_URL")if url == "" {- t.Skip("set CYCLOPS_TEST_DATABASE_URL to run the Postgres store test")+ t.Skip("set CYCLOPS_TEST_RUNTIME_DATABASE_URL to run the Postgres store test")}ctx := context.Background()+ if err := database.RequireVersion(ctx, url, 1); err != nil {+ t.Fatalf("database must be migrated to version 1: %v", err)+ }store, err := New(ctx, url)if err != nil {t.Fatalf("New: %v", err)diff --git a/libs/fleet/backend/githubtrust/types.go b/libs/fleet/backend/githubtrust/types.goindex 6674c2c428..d00d1dff39 100644--- a/libs/fleet/backend/githubtrust/types.go+++ b/libs/fleet/backend/githubtrust/types.go@@ -18,23 +18,17 @@ var ()// Policy is the runtime representation of a GitHub Actions OIDC trust policy.-// GORM tags drive AutoMigrate so the schema is derived from this struct —-// no manual DDL. pgx is still used for queries; GORM is migration-only.type Policy struct {- ID string `json:"id" gorm:"primaryKey"`- OwnerSub string `json:"owner_sub" gorm:"column:owner_sub;index"`+ ID string `json:"id"`+ OwnerSub string `json:"owner_sub"`Name string `json:"name"`- Repository string `json:"repository" gorm:"column:repository;index"`- AllowedNamespaces []string `json:"allowed_namespaces" gorm:"type:text[]"`+ Repository string `json:"repository"`+ AllowedNamespaces []string `json:"allowed_namespaces"`Enabled bool `json:"enabled"`CreatedAt time.Time `json:"created_at"`UpdatedAt time.Time `json:"updated_at"`}-// TableName overrides GORM's default pluralisation so the table name matches-// what the pgx queries expect.-func (Policy) TableName() string { return "github_trust_policies" }-type PolicyInput struct {Name string `json:"name"`Repository string `json:"repository"`diff --git a/libs/fleet/backend/go.mod b/libs/fleet/backend/go.modindex ddb78ecb7c..34701ae3a4 100644--- a/libs/fleet/backend/go.mod+++ b/libs/fleet/backend/go.mod@@ -22,8 +22,6 @@ require (go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0golang.org/x/sync v0.17.0- gorm.io/driver/postgres v1.5.11- gorm.io/gorm v1.25.12)require (@@ -38,8 +36,6 @@ require (github.com/jackc/pgpassfile v1.0.0 // indirectgithub.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirectgithub.com/jackc/puddle/v2 v2.2.1 // indirect- github.com/jinzhu/inflection v1.0.0 // indirect- github.com/jinzhu/now v1.1.5 // indirectgithub.com/open-feature/go-sdk-contrib/providers/aws-ssm v1.0.0 // indirectgithub.com/open-feature/go-sdk-contrib/providers/from-env v0.1.6 // indirectgithub.com/pelletier/go-toml/v2 v2.2.4 // indirectdiff --git a/libs/fleet/backend/go.sum b/libs/fleet/backend/go.sumindex d8fa818391..1a87303bba 100644--- a/libs/fleet/backend/go.sum+++ b/libs/fleet/backend/go.sum@@ -129,10 +129,6 @@ github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=-github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=-github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=-github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=-github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=@@ -292,9 +288,5 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+Cgopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=-gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=-gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=-gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=-gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=diff --git a/libs/fleet/backend/handlers/handlers.go b/libs/fleet/backend/handlers/handlers.goindex a0fdbaa133..5608884fd6 100644--- a/libs/fleet/backend/handlers/handlers.go+++ b/libs/fleet/backend/handlers/handlers.go@@ -31,6 +31,8 @@ type Handlers struct {StateQueryExecutor StateQueryExecutor+ Readiness *Readiness+// WorkloadAdmin manages per-tenant clients in the workloads realm so// OSGym pool VMs can obtain a tenant-scoped OIDC token. nil disables// the feature (CreateNamespace then skips OIDC credential provisioning).diff --git a/libs/fleet/backend/handlers/health.go b/libs/fleet/backend/handlers/health.goindex 1b1f87e8a4..d6c5d6481f 100644--- a/libs/fleet/backend/handlers/health.go+++ b/libs/fleet/backend/handlers/health.go@@ -1,15 +1,32 @@package handlers-import "net/http"+import (+ "net/http"+ "sync/atomic"+)-// HealthResponse is the body of GET /healthz.+// HealthResponse is the body of GET /healthz and GET /readyz.type HealthResponse struct {OK bool `json:"ok"`}+// Readiness tracks whether startup dependencies required for serving traffic+// have completed successfully. It starts fail-closed.+type Readiness struct {+ databaseReady atomic.Bool+}++func NewReadiness() *Readiness {+ return &Readiness{}+}++func (r *Readiness) MarkReady() {+ r.databaseReady.Store(true)+}+// GetHealth godoc//-// @Summary Liveness/readiness probe+// @Summary Liveness probe// @Tags health// @Produce json// @Success 200 {object} HealthResponse@@ -17,3 +34,11 @@ type HealthResponse struct {func (h Handlers) GetHealth(w http.ResponseWriter, _ *http.Request) {writeJSON(w, http.StatusOK, HealthResponse{OK: true})}++func (h Handlers) GetReadiness(w http.ResponseWriter, _ *http.Request) {+ if h.Readiness == nil || !h.Readiness.databaseReady.Load() {+ writeJSON(w, http.StatusServiceUnavailable, HealthResponse{OK: false})+ return+ }+ writeJSON(w, http.StatusOK, HealthResponse{OK: true})+}diff --git a/libs/fleet/backend/handlers/health_test.go b/libs/fleet/backend/handlers/health_test.gonew file mode 100644index 0000000000..f7f2adfe11--- /dev/null+++ b/libs/fleet/backend/handlers/health_test.go@@ -0,0 +1,41 @@+package handlers++import (+ "net/http"+ "net/http/httptest"+ "testing"+)++func TestHealthIsLiveWhenDatabaseIsUnavailable(t *testing.T) {+ response := httptest.NewRecorder()++ Handlers{}.GetHealth(response, httptest.NewRequest(http.MethodGet, "/healthz", nil))++ if response.Code != http.StatusOK {+ t.Fatalf("status = %d, want %d", response.Code, http.StatusOK)+ }+}++func TestReadinessFailsClosedUntilDatabaseSchemaIsReady(t *testing.T) {+ readiness := NewReadiness()+ h := Handlers{Readiness: readiness}++ for _, name := range []string{"initial state", "schema check failure"} {+ t.Run(name, func(t *testing.T) {+ response := httptest.NewRecorder()+ h.GetReadiness(response, httptest.NewRequest(http.MethodGet, "/readyz", nil))++ if response.Code < http.StatusBadRequest {+ t.Fatalf("status = %d, want non-2xx", response.Code)+ }+ })+ }++ readiness.MarkReady()+ response := httptest.NewRecorder()+ h.GetReadiness(response, httptest.NewRequest(http.MethodGet, "/readyz", nil))++ if response.Code != http.StatusOK {+ t.Fatalf("status = %d, want %d", response.Code, http.StatusOK)+ }+}diff --git a/libs/fleet/backend/main.go b/libs/fleet/backend/main.goindex f59358b11f..d202fa0447 100644--- a/libs/fleet/backend/main.go+++ b/libs/fleet/backend/main.go@@ -31,6 +31,7 @@ import ("cyclops-cs-backend/auth""cyclops-cs-backend/billing""cyclops-cs-backend/config"+ "cyclops-cs-backend/database"_ "cyclops-cs-backend/docs" // generated by `swag init`"cyclops-cs-backend/githubtrust""cyclops-cs-backend/handlers"@@ -124,6 +125,7 @@ func setupRouter(c handlers.Handlers) http.Handler {r := http.NewServeMux()r.Handle("GET /healthz", onlyLog("/healthz", c.GetHealth))+ r.Handle("GET /readyz", onlyLog("/readyz", c.GetReadiness))// Feature flags / per-user config. SPA-only (same auth as /api/keys).r.Handle("GET /api/config",@@ -308,6 +310,7 @@ func run() error {}h := handlers.New(admin, cfg)+ h.Readiness = handlers.NewReadiness()if cfg.Stripe.SecretKey != "" {h.Billing = billing.NewService(billing.NewStripeGateway(cfg.Stripe.SecretKey))slog.Info("stripe billing: hosted flows enabled")@@ -318,24 +321,18 @@ func run() error {h.WorkloadAdmin = workloadAdminh.WorkloadAudience = cfg.Keycloak.WorkloadAudienceh.WorkloadTokenURL = cfg.Keycloak.WorkloadTokenURL- stateSchemaReady := false- stateRoleURLs := statequery.FixedRoleURLs{- Writer: cfg.Database.StateWriterURL,- Exporter: cfg.Database.StateExporterURL,- RoleAdmin: cfg.Database.StateRoleAdminURL,- }- if stateRoleURLs.Writer != "" || stateRoleURLs.Exporter != "" || stateRoleURLs.RoleAdmin != "" {- if err := statequery.ReconcileFixedRoles(ctx, cfg.Database.URL, stateRoleURLs); err != nil {- slog.Error("kubernetes state query: fixed-role reconciliation failed", "err", err)- } else if err := statequery.Migrate(ctx, cfg.Database.URL); err != nil {- slog.Error("kubernetes state query: schema migration failed", "err", err)+ databaseReady := false+ if cfg.Database.URL != "" {+ if err := database.RequireVersion(ctx, cfg.Database.URL, 1); err != nil {+ slog.Error("postgres database schema unavailable", "err", err)} else {- slog.Info("kubernetes state query: schema ready")- stateSchemaReady = true+ databaseReady = true+ h.Readiness.MarkReady()+ slog.Info("postgres database schema ready", "version", 1)}- } else {- slog.Info("kubernetes state query: disabled (fixed-role database URLs unset)")}++ stateSchemaReady := databaseReady && cfg.Database.StateQueryDSN != "" && cfg.Database.StateQueryTenantPassword != ""if stateSchemaReady {executor, err := statequery.NewExecutor(cfg.Database.StateQueryDSN,@@ -352,14 +349,16 @@ func run() error {// failure is non-fatal — the routes stay registered and reply 503 until// the database is reachable, mirroring how the other optional surfaces// degrade gracefully.- if store, err := githubtrust.New(ctx, cfg.Database.URL); err != nil {- slog.Error("github trust policies: init failed; /api/github-trust-policies will return 503", "err", err)- } else if store != nil {- h.GitHubTrustPolicies = store- auth.SetGitHubTrustResolver(handlers.NewGitHubTrustResolver(store))- slog.Info("github trust policies: enabled")+ if databaseReady {+ if store, err := githubtrust.New(ctx, cfg.Database.URL); err != nil {+ slog.Error("github trust policies: init failed; /api/github-trust-policies will return 503", "err", err)+ } else if store != nil {+ h.GitHubTrustPolicies = store+ auth.SetGitHubTrustResolver(handlers.NewGitHubTrustResolver(store))+ slog.Info("github trust policies: enabled")+ }} else {- slog.Info("github trust policies: disabled (no DATABASE_URL configured)")+ slog.Info("github trust policies: disabled (database schema unavailable)")}router := setupRouter(h)diff --git a/libs/fleet/backend/main_test.go b/libs/fleet/backend/main_test.goindex b6ce882eb7..637ed7da81 100644--- a/libs/fleet/backend/main_test.go+++ b/libs/fleet/backend/main_test.go@@ -43,6 +43,27 @@ func TestGatewayRoutesAreRemoved(t *testing.T) {}}+func TestHealthAndReadinessRoutes(t *testing.T) {+ router := setupRouter(handlers.Handlers{})++ for _, test := range []struct {+ path string+ statusCode int+ }{+ {path: "/healthz", statusCode: http.StatusOK},+ {path: "/readyz", statusCode: http.StatusServiceUnavailable},+ } {+ t.Run(test.path, func(t *testing.T) {+ response := httptest.NewRecorder()+ router.ServeHTTP(response, httptest.NewRequest(http.MethodGet, test.path, nil))++ if response.Code != test.statusCode {+ t.Fatalf("status = %d, want %d", response.Code, test.statusCode)+ }+ })+ }+}+func TestSwaggerOmitsGatewayRoute(t *testing.T) {data, err := os.ReadFile("docs/swagger.json")if err != nil {diff --git a/libs/fleet/backend/startup_migration_contract_test.go b/libs/fleet/backend/startup_migration_contract_test.gonew file mode 100644index 0000000000..4ee174fd50--- /dev/null+++ b/libs/fleet/backend/startup_migration_contract_test.go@@ -0,0 +1,766 @@+package main++import (+ "fmt"+ "go/ast"+ "go/parser"+ "go/token"+ "os"+ "path/filepath"+ "strconv"+ "strings"+ "testing"+)++var databaseExecutionMethods = map[string]struct{}{+ "Exec": {}, "ExecContext": {}, "Query": {}, "QueryContext": {},+ "QueryRow": {}, "QueryRowContext": {}, "Prepare": {}, "PrepareContext": {},+}++func readSource(t *testing.T, name string) string {+ t.Helper()++ contents, err := os.ReadFile(filepath.Clean(name))+ if err != nil {+ t.Fatal(err)+ }+ return string(contents)+}++func isGeneratedGoSource(file *ast.File) bool {+ return ast.IsGenerated(file)+}++type migrationImportSet struct {+ statequeryAliases map[string]bool+ databaseAliases map[string]bool+ sqlAliases map[string]bool+ pgxAliases map[string]bool+ pgxPoolAliases map[string]bool+ statequeryDot bool+ databaseDot bool+}++func migrationImports(file *ast.File) migrationImportSet {+ imports := migrationImportSet{+ statequeryAliases: map[string]bool{},+ databaseAliases: map[string]bool{},+ sqlAliases: map[string]bool{},+ pgxAliases: map[string]bool{},+ pgxPoolAliases: map[string]bool{},+ }+ for _, importSpec := range file.Imports {+ path, err := strconv.Unquote(importSpec.Path.Value)+ if err != nil {+ continue+ }+ alias := filepath.Base(path)+ if importSpec.Name != nil {+ alias = importSpec.Name.Name+ }+ switch path {+ case "cyclops-cs-backend/statequery":+ if alias == "." {+ imports.statequeryDot = true+ } else {+ imports.statequeryAliases[alias] = true+ }+ case "cyclops-cs-backend/database":+ if alias == "." {+ imports.databaseDot = true+ } else {+ imports.databaseAliases[alias] = true+ }+ case "database/sql":+ imports.sqlAliases[alias] = true+ case "github.com/jackc/pgx/v5":+ imports.pgxAliases[alias] = true+ case "github.com/jackc/pgx/v5/pgxpool":+ imports.pgxPoolAliases[alias] = true+ }+ }+ return imports+}++func migrationAuthorityViolation(file *ast.File) string {+ imports := migrationImports(file)+ packageConstants := collectPackageStringConstants(file.Decls)+ var forbidden string++ ast.Inspect(file, func(node ast.Node) bool {+ if forbidden != "" {+ return false+ }+ switch value := node.(type) {+ case *ast.CommentGroup:+ if strings.Contains(value.Text(), "go:embed") && strings.Contains(value.Text(), "migrations/") {+ forbidden = "embedded legacy migration SQL"+ }+ case *ast.BasicLit:+ if value.Kind == token.STRING {+ literal, err := strconv.Unquote(value.Value)+ if err == nil && isLegacyMigrationPath(literal) {+ forbidden = "legacy migration SQL"+ }+ }+ case *ast.FuncDecl:+ constants := collectLocalStringConstants(value.Body.List, packageConstants)+ receivers := databaseReceiverContracts(value.Type, imports)+ if violation := scanFunctionCalls(value.Body, constants, imports, receivers); violation != "" {+ forbidden = violation+ }+ return false+ case *ast.FuncLit:+ constants := collectLocalStringConstants(value.Body.List, packageConstants)+ receivers := databaseReceiverContracts(value.Type, imports)+ if violation := scanFunctionCalls(value.Body, constants, imports, receivers); violation != "" {+ forbidden = violation+ }+ return false+ }+ return true+ })+ if forbidden != "" {+ return forbidden+ }++ var entrypoint string+ ast.Inspect(file, func(node ast.Node) bool {+ if entrypoint != "" {+ return false+ }+ call, ok := node.(*ast.CallExpr)+ if ok && isMigrationEntrypointCall(call, imports) {+ entrypoint = "migration entrypoint"+ }+ return true+ })+ return entrypoint+}++func scanFunctionCalls(body *ast.BlockStmt, constants map[string]string, imports migrationImportSet, receivers map[string]databaseExecutionContract) string {+ var forbidden string+ ast.Inspect(body, func(node ast.Node) bool {+ if forbidden != "" {+ return false+ }+ if function, ok := node.(*ast.FuncLit); ok && function.Body != body {+ return false+ }+ call, ok := node.(*ast.CallExpr)+ if !ok {+ return true+ }+ if violation := scanMigrationCall(call, constants, imports, receivers); violation != "" {+ forbidden = violation+ }+ return true+ })+ return forbidden+}++func scanMigrationCall(call *ast.CallExpr, constants map[string]string, imports migrationImportSet, receivers map[string]databaseExecutionContract) string {+ if identifier, ok := call.Fun.(*ast.Ident); ok {+ switch {+ case identifier.Name == "AutoMigrate":+ return "AutoMigrate"+ case imports.databaseDot && identifier.Name == "Run":+ return "database.Run"+ case imports.statequeryDot && (identifier.Name == "Migrate" || identifier.Name == "ReconcileFixedRoles"):+ return "statequery migration entrypoint"+ }+ }+ if isMigrationEntrypointCall(call, imports) {+ return "migration entrypoint"+ }+ if !isDatabaseExecutionCall(call) {+ return ""+ }+ for _, argument := range sqlArguments(call, receivers) {+ query, ok := foldString(argument, constants)+ if !ok {+ continue+ }+ if isLegacyMigrationPath(query) {+ return "legacy migration SQL"+ }+ if isSchemaOrPrivilegeSQL(query) {+ return "direct schema or privilege SQL"+ }+ }+ return ""+}++func isMigrationEntrypointCall(call *ast.CallExpr, imports migrationImportSet) bool {+ selector, ok := call.Fun.(*ast.SelectorExpr)+ if !ok {+ return false+ }+ identifier, ok := selector.X.(*ast.Ident)+ if !ok {+ return false+ }+ return (imports.databaseAliases[identifier.Name] && selector.Sel.Name == "Run") ||+ (imports.statequeryAliases[identifier.Name] && (selector.Sel.Name == "Migrate" || selector.Sel.Name == "ReconcileFixedRoles")) ||+ (imports.databaseDot && selector.Sel.Name == "Run") ||+ (imports.statequeryDot && (selector.Sel.Name == "Migrate" || selector.Sel.Name == "ReconcileFixedRoles"))+}++type databaseExecutionContract uint8++const (+ unknownDatabaseExecution databaseExecutionContract = iota+ databaseSQLExecution+ pgxExecution+)++func databaseReceiverContracts(function *ast.FuncType, imports migrationImportSet) map[string]databaseExecutionContract {+ receivers := map[string]databaseExecutionContract{}+ if function == nil || function.Params == nil {+ return receivers+ }+ for _, field := range function.Params.List {+ contract := databaseExecutionContractForType(field.Type, imports)+ if contract == unknownDatabaseExecution {+ continue+ }+ for _, name := range field.Names {+ receivers[name.Name] = contract+ }+ }+ return receivers+}++func databaseExecutionContractForType(expression ast.Expr, imports migrationImportSet) databaseExecutionContract {+ for {+ pointer, ok := expression.(*ast.StarExpr)+ if !ok {+ break+ }+ expression = pointer.X+ }+ selector, ok := expression.(*ast.SelectorExpr)+ if !ok {+ return unknownDatabaseExecution+ }+ packageName, ok := selector.X.(*ast.Ident)+ if !ok {+ return unknownDatabaseExecution+ }+ switch {+ case imports.sqlAliases[packageName.Name] && (selector.Sel.Name == "DB" || selector.Sel.Name == "Conn" || selector.Sel.Name == "Tx"):+ return databaseSQLExecution+ case imports.pgxAliases[packageName.Name] && (selector.Sel.Name == "Conn" || selector.Sel.Name == "Tx"):+ return pgxExecution+ case imports.pgxPoolAliases[packageName.Name] && selector.Sel.Name == "Pool":+ return pgxExecution+ default:+ return unknownDatabaseExecution+ }+}++func isDatabaseExecutionCall(call *ast.CallExpr) bool {+ selector, ok := call.Fun.(*ast.SelectorExpr)+ if !ok {+ return false+ }+ _, ok = databaseExecutionMethods[selector.Sel.Name]+ return ok+}++func sqlArguments(call *ast.CallExpr, receivers map[string]databaseExecutionContract) []ast.Expr {+ selector := call.Fun.(*ast.SelectorExpr)+ var contract databaseExecutionContract+ if receiver, ok := selector.X.(*ast.Ident); ok {+ contract = receivers[receiver.Name]+ }+ arguments := make([]ast.Expr, 0, 3)+ for _, index := range sqlArgumentIndexes(selector.Sel.Name, contract) {+ if index < len(call.Args) {+ arguments = append(arguments, call.Args[index])+ }+ }+ return arguments+}++func sqlArgumentIndexes(method string, contract databaseExecutionContract) []int {+ switch contract {+ case pgxExecution:+ if method == "Prepare" {+ return []int{2}+ }+ return []int{1}+ case databaseSQLExecution:+ if strings.HasSuffix(method, "Context") {+ return []int{1}+ }+ return []int{0}+ default:+ if strings.HasSuffix(method, "Context") {+ return []int{1}+ }+ if method == "Prepare" {+ return []int{0, 1, 2}+ }+ return []int{0, 1}+ }+}++func collectPackageStringConstants(declarations []ast.Decl) map[string]string {+ constants := map[string]string{}+ for _, declaration := range declarations {+ gen, ok := declaration.(*ast.GenDecl)+ if !ok || gen.Tok != token.CONST {+ continue+ }+ collectStringValueSpecs(constants, gen.Specs)+ }+ return constants+}++func collectLocalStringConstants(statements []ast.Stmt, base map[string]string) map[string]string {+ constants := mergeStringConstants(base, nil)+ for _, statement := range statements {+ declaration, ok := statement.(*ast.DeclStmt)+ if !ok {+ continue+ }+ gen, ok := declaration.Decl.(*ast.GenDecl)+ if !ok || gen.Tok != token.CONST {+ continue+ }+ collectStringValueSpecs(constants, gen.Specs)+ }+ return constants+}++func collectStringValueSpecs(constants map[string]string, specifications []ast.Spec) {+ for _, specification := range specifications {+ valueSpec, ok := specification.(*ast.ValueSpec)+ if !ok {+ continue+ }+ for index, name := range valueSpec.Names {+ if index >= len(valueSpec.Values) {+ continue+ }+ if value, ok := foldString(valueSpec.Values[index], constants); ok {+ constants[name.Name] = value+ }+ }+ }+}++func mergeStringConstants(base, additional map[string]string) map[string]string {+ merged := make(map[string]string, len(base)+len(additional))+ for name, value := range base {+ merged[name] = value+ }+ for name, value := range additional {+ merged[name] = value+ }+ return merged+}++func foldString(expression ast.Expr, constants map[string]string) (string, bool) {+ switch value := expression.(type) {+ case *ast.BasicLit:+ if value.Kind != token.STRING {+ return "", false+ }+ unquoted, err := strconv.Unquote(value.Value)+ return unquoted, err == nil+ case *ast.ParenExpr:+ return foldString(value.X, constants)+ case *ast.BinaryExpr:+ if value.Op != token.ADD {+ return "", false+ }+ left, leftOK := foldString(value.X, constants)+ right, rightOK := foldString(value.Y, constants)+ return left + right, leftOK && rightOK+ case *ast.Ident:+ constant, ok := constants[value.Name]+ return constant, ok+ default:+ return "", false+ }+}++func isLegacyMigrationPath(value string) bool {+ return strings.Contains(filepath.ToSlash(value), "database/migrations/")+}++func isSchemaOrPrivilegeSQL(query string) bool {+ for _, statement := range splitSQLStatements(query) {+ statement = stripLeadingSQLComments(statement)+ upper := strings.ToUpper(statement)+ switch {+ case startsSQLKeyword(upper, "CREATE"), startsSQLKeyword(upper, "ALTER"), startsSQLKeyword(upper, "DROP"),+ startsSQLKeyword(upper, "GRANT"), startsSQLKeyword(upper, "REVOKE"),+ startsSQLPhrase(upper, "COMMENT ON"), startsSQLPhrase(upper, "SECURITY LABEL"),+ startsSQLPhrase(upper, "REASSIGN OWNED"), startsSQLPhrase(upper, "DROP OWNED"):+ return true+ }+ }+ return false+}++func startsSQLKeyword(statement, keyword string) bool {+ if !strings.HasPrefix(statement, keyword) {+ return false+ }+ return len(statement) == len(keyword) || !isSQLIdentifierByte(statement[len(keyword)])+}++func startsSQLPhrase(statement, phrase string) bool {+ if !strings.HasPrefix(statement, phrase) {+ return false+ }+ return len(statement) == len(phrase) || !isSQLIdentifierByte(statement[len(phrase)])+}++func isSQLIdentifierByte(value byte) bool {+ return value == '_' || value >= 'A' && value <= 'Z' || value >= '0' && value <= '9'+}++func stripLeadingSQLComments(statement string) string {+ for {+ statement = strings.TrimLeft(statement, " \t\r\n\f")+ switch {+ case strings.HasPrefix(statement, "--"):+ if newline := strings.IndexByte(statement, '\n'); newline >= 0 {+ statement = statement[newline+1:]+ continue+ }+ return ""+ case strings.HasPrefix(statement, "/*"):+ end := strings.Index(statement[2:], "*/")+ if end < 0 {+ return ""+ }+ statement = statement[end+4:]+ continue+ default:+ return statement+ }+ }+}++func splitSQLStatements(query string) []string {+ var statements []string+ start := 0+ inSingleQuote := false+ inDoubleQuote := false+ inLineComment := false+ inBlockComment := false+ for index := 0; index < len(query); index++ {+ if inLineComment {+ if query[index] == '\n' {+ inLineComment = false+ }+ continue+ }+ if inBlockComment {+ if query[index] == '*' && index+1 < len(query) && query[index+1] == '/' {+ inBlockComment = false+ index+++ }+ continue+ }+ if inSingleQuote {+ if query[index] == '\'' {+ if index+1 < len(query) && query[index+1] == '\'' {+ index+++ } else {+ inSingleQuote = false+ }+ }+ continue+ }+ if inDoubleQuote {+ if query[index] == '"' {+ if index+1 < len(query) && query[index+1] == '"' {+ index+++ } else {+ inDoubleQuote = false+ }+ }+ continue+ }+ switch query[index] {+ case '-':+ if index+1 < len(query) && query[index+1] == '-' {+ inLineComment = true+ index+++ }+ case '/':+ if index+1 < len(query) && query[index+1] == '*' {+ inBlockComment = true+ index+++ }+ case '\'':+ inSingleQuote = true+ case '"':+ inDoubleQuote = true+ case ';':+ statements = append(statements, query[start:index])+ start = index + 1+ }+ }+ return append(statements, query[start:])+}++func scanActiveBackend(root string) error {+ return filepath.WalkDir(root, func(path string, entry os.DirEntry, err error) error {+ if err != nil {+ return err+ }+ relative, err := filepath.Rel(root, path)+ if err != nil {+ return err+ }+ relative = filepath.ToSlash(relative)+ if entry.IsDir() {+ if relative == "vendor" || relative == "database/migrations" || relative == "cmd/db-migrate" {+ return filepath.SkipDir+ }+ return nil+ }+ if strings.HasSuffix(relative, ".sql") {+ return fmt.Errorf("%s is runtime-loaded SQL outside database/migrations", relative)+ }+ if !strings.HasSuffix(relative, ".go") || strings.HasSuffix(relative, "_test.go") || relative == "database/migrator.go" {+ return nil+ }+ file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ParseComments)+ if err != nil {+ return err+ }+ if isGeneratedGoSource(file) {+ return nil+ }+ if forbidden := migrationAuthorityViolation(file); forbidden != "" {+ return fmt.Errorf("%s retains migration authority via %s", relative, forbidden)+ }+ return nil+ })+}++func parseFixture(t *testing.T, source string) *ast.File {+ t.Helper()+ file, err := parser.ParseFile(token.NewFileSet(), "fixture.go", source, parser.ParseComments)+ if err != nil {+ t.Fatal(err)+ }+ return file+}++func TestGeneratedSourceIsExcludedFromMigrationAuthorityScan(t *testing.T) {+ generated := parseFixture(t, `// Code generated by contract fixture. DO NOT EDIT.+package test+func f() { db.Exec(ctx, "create table x (id int)") }`)+ if !isGeneratedGoSource(generated) {+ t.Fatal("generated source was not excluded")+ }++ spoofed := parseFixture(t, `package test+// Code generated by contract fixture. DO NOT EDIT.+func f() { db.Exec(ctx, "create table x (id int)") }`)+ if isGeneratedGoSource(spoofed) {+ t.Fatal("comment after package must not exempt source")+ }+}++func TestGitHubTrustNewDoesNotMigrate(t *testing.T) {+ source := readSource(t, "githubtrust/store.go")+ for _, forbidden := range []string{"AutoMigrate", "gorm.Open", "gorm.io/driver/postgres", "gorm.io/gorm"} {+ if strings.Contains(source, forbidden) {+ t.Fatalf("store startup still contains %q", forbidden)+ }+ }+}++func TestMigrationAuthorityScannerCatchesForbiddenEntries(t *testing.T) {+ for name, source := range map[string]string{+ "database alias": `package test; import db "cyclops-cs-backend/database"; func f() { db.Run() }`,+ "database dot import": `package test; import . "cyclops-cs-backend/database"; func f() { Run() }`,+ "statequery alias": `package test; import sq "cyclops-cs-backend/statequery"; func f() { sq.Migrate() }`,+ "statequery dot import": `package test; import . "cyclops-cs-backend/statequery"; func f() { Migrate() }`,+ "automigrate": `package test; func f() { AutoMigrate() }`,+ "comment-prefixed DDL": "package test\nfunc f() { db.Exec(ctx, " + strconv.Quote("-- audit\n/* allow */ create table x (id int)") + `) }`,+ "create domain": `package test; func f() { db.Exec(ctx, "create domain email as text") }`,+ "create event trigger": `package test; func f() { db.Exec(ctx, "create event trigger audit on ddl_command_end execute function f()") }`,+ "alter default privileges": `package test; func f() { db.Exec(ctx, "alter default privileges grant select on tables to app") }`,+ "concatenated create table": `package test; const prefix = "create"; func f() { const query = (prefix + " table x (id int)"); db.Exec(ctx, query) }`,+ "legacy migration": `package test; var migration = "database/migrations/000001_initial_schema.sql"`,+ "legacy migration execution": `package test; func f() { db.Exec(ctx, "database/migrations/000001_initial_schema.sql") }`,+ "comment on": `package test; func f() { db.Exec(ctx, "comment on table x is 'x'") }`,+ "security label": `package test; func f() { db.Exec(ctx, "security label on table x is 'x'") }`,+ "reassign owned": `package test; func f() { db.Exec(ctx, "reassign owned by old_role to new_role") }`,+ "grant": `package test; func f() { db.Exec(ctx, "grant select on x to app") }`,+ "revoke": `package test; func f() { db.Exec(ctx, "revoke select on x from app") }`,+ "pgx pool exec": `package test+import (+ "context"+ "github.com/jackc/pgx/v5/pgxpool"+)+func f(pool *pgxpool.Pool, requestContext context.Context) { pool.Exec(requestContext, "create table x (id int)") }`,+ "pgx conn query": `package test+import (+ "context"+ "github.com/jackc/pgx/v5"+)+func f(conn *pgx.Conn, operationContext context.Context) { conn.Query(operationContext, "create table x (id int)") }`,+ "pgx tx query row": `package test+import (+ "context"+ "github.com/jackc/pgx/v5"+)+func f(tx pgx.Tx, callerContext context.Context) { tx.QueryRow(callerContext, "create table x (id int)") }`,+ "pgx prepare": `package test+import (+ "context"+ "github.com/jackc/pgx/v5/pgxpool"+)+func f(pool *pgxpool.Pool, requestContext context.Context) { pool.Prepare(requestContext, "named", "create table x (id int)") }`,+ "pgx tx prepare": `package test+import (+ "context"+ "github.com/jackc/pgx/v5"+)+func f(tx pgx.Tx, requestContext context.Context) { tx.Prepare(requestContext, "named", "create table x (id int)") }`,+ "database sql exec": `package test+import "database/sql"+func f(db *sql.DB) { db.Exec("create table x (id int)", 1) }`,+ "database sql query": `package test+import "database/sql"+func f(db *sql.DB) { db.Query("create table x (id int)", 1) }`,+ "database sql query row": `package test+import "database/sql"+func f(db *sql.DB) { db.QueryRow("create table x (id int)", 1) }`,+ "database sql prepare": `package test+import "database/sql"+func f(db *sql.DB) { db.Prepare("create table x (id int)") }`,+ "database sql exec context": `package test+import (+ "context"+ "database/sql"+)+func f(db *sql.DB, requestContext context.Context) { db.ExecContext(requestContext, "create table x (id int)") }`,+ "database sql query context": `package test+import (+ "context"+ "database/sql"+)+func f(db *sql.DB, requestContext context.Context) { db.QueryContext(requestContext, "create table x (id int)") }`,+ "database sql query row context": `package test+import (+ "context"+ "database/sql"+)+func f(db *sql.DB, requestContext context.Context) { db.QueryRowContext(requestContext, "create table x (id int)") }`,+ "database sql prepare context": `package test+import (+ "context"+ "database/sql"+)+func f(db *sql.DB, requestContext context.Context) { db.PrepareContext(requestContext, "create table x (id int)") }`,+ } {+ t.Run(name, func(t *testing.T) {+ if migrationAuthorityViolation(parseFixture(t, source)) == "" {+ t.Fatal("migration-authority scanner missed forbidden source")+ }+ })+ }+}++func TestMigrationAuthorityScannerAllowsCRUD(t *testing.T) {+ for _, source := range []string{+ `package test+import (+ "context"+ "github.com/jackc/pgx/v5/pgxpool"+)+func f(pool *pgxpool.Pool, requestContext context.Context) { pool.Exec(requestContext, "insert into widgets (id) values ($1)") }`,+ `package test+import "database/sql"+func f(db *sql.DB) { db.Query("select * from widgets where id = $1", 1) }`,+ `package test+import (+ "context"+ "database/sql"+)+func f(db *sql.DB, requestContext context.Context) { db.QueryRowContext(requestContext, "update widgets set name = $1 where id = $2 returning id") }`,+ `package test+import "database/sql"+func f(db *sql.DB) { db.Prepare("delete from widgets where id = $1") }`,+ } {+ if violation := migrationAuthorityViolation(parseFixture(t, source)); violation != "" {+ t.Fatalf("normal CRUD query reported as %s", violation)+ }+ }+}++func TestActiveBackendScannerRejectsRuntimeSQLAndHonorsCanonicalGeneratedFiles(t *testing.T) {+ root := t.TempDir()+ writeFixture := func(name, source string) {+ t.Helper()+ path := filepath.Join(root, name)+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(path, []byte(source), 0o600); err != nil {+ t.Fatal(err)+ }+ }++ writeFixture("runtime.sql", "select 1")+ if err := scanActiveBackend(root); err == nil || !strings.Contains(err.Error(), "runtime.sql") {+ t.Fatalf("runtime SQL was not rejected: %v", err)+ }+ if err := os.Remove(filepath.Join(root, "runtime.sql")); err != nil {+ t.Fatal(err)+ }+ writeFixture("generated.go", "// Code generated by fixture. DO NOT EDIT.\npackage fixture\nfunc f() { db.Exec(ctx, \"create table x (id int)\") }\n")+ if err := scanActiveBackend(root); err != nil {+ t.Fatalf("canonical generated file was not excluded: %v", err)+ }+ writeFixture("spoofed.go", "package fixture\n// Code generated by fixture. DO NOT EDIT.\nfunc f() { db.Exec(ctx, \"create table x (id int)\") }\n")+ if err := scanActiveBackend(root); err == nil || !strings.Contains(err.Error(), "spoofed.go") {+ t.Fatalf("spoofed generated comment was accepted: %v", err)+ }+}++func TestActiveBackendSourcesDoNotOwnMigrations(t *testing.T) {+ if err := scanActiveBackend("."); err != nil {+ t.Fatal(err)+ }+}++func TestMainRequiresMigrationVersionWithoutRunningMigrations(t *testing.T) {+ source := readSource(t, "main.go")+ if !strings.Contains(source, "database.RequireVersion(ctx, cfg.Database.URL, 1)") {+ t.Fatal("backend must require migration version 1")+ }+}++func TestBackendReadinessStartsFailClosedAndRequiresSchema(t *testing.T) {+ source := readSource(t, "main.go")+ if !strings.Contains(source, "h.Readiness = handlers.NewReadiness()") {+ t.Fatal("backend readiness must start fail-closed before the database check")+ }+ if !strings.Contains(source, "h.Readiness.MarkReady()") {+ t.Fatal("backend readiness must become ready after the database schema check")+ }+}++func TestBackendDeploymentUsesSeparateReadinessProbe(t *testing.T) {+ source := readSource(t, filepath.Join("..", "..", "clusters", "kopf-k3s", "cyclops-cs", "backend-deployment.yaml"))+ if !strings.Contains(source, "readinessProbe:\n httpGet:\n path: /readyz") {+ t.Fatal("backend readiness probe must use /readyz")+ }+ if !strings.Contains(source, "livenessProbe:\n httpGet:\n path: /healthz") {+ t.Fatal("backend liveness probe must use /healthz")+ }+}diff --git a/libs/fleet/backend/statequery/executor_test.go b/libs/fleet/backend/statequery/executor_test.goindex 4d1e7c0530..9e76ae313d 100644--- a/libs/fleet/backend/statequery/executor_test.go+++ b/libs/fleet/backend/statequery/executor_test.go@@ -2,9 +2,13 @@ package statequeryimport ("context"+ "crypto/sha256"+ "encoding/hex""os""testing"+ "cyclops-cs-backend/database"+"github.com/jackc/pgx/v5""github.com/jackc/pgx/v5/pgconn""github.com/jackc/pgx/v5/pgxpool"@@ -62,19 +66,15 @@ func TestNewExecutorRequiresDSNAndPassword(t *testing.T) {}func TestExecutorListsOnlyTenantOwnedNamespaces(t *testing.T) {- adminURL := os.Getenv("CYCLOPS_TEST_DATABASE_URL")- if adminURL == "" {- t.Skip("set CYCLOPS_TEST_DATABASE_URL to run the PostgreSQL RLS test")+ runtimeURL := os.Getenv("CYCLOPS_TEST_RUNTIME_DATABASE_URL")+ writerURL := os.Getenv("CYCLOPS_TEST_STATE_WRITER_DATABASE_URL")+ roleAdminURL := os.Getenv("CYCLOPS_TEST_STATE_ROLE_ADMIN_DATABASE_URL")+ if runtimeURL == "" || writerURL == "" || roleAdminURL == "" {+ t.Skip("set CYCLOPS_TEST_RUNTIME_DATABASE_URL, CYCLOPS_TEST_STATE_WRITER_DATABASE_URL, and CYCLOPS_TEST_STATE_ROLE_ADMIN_DATABASE_URL to run the PostgreSQL RLS test")}ctx := context.Background()- urls := fixedTestRoleURLs(t, adminURL)- if err := ReconcileFixedRoles(ctx, adminURL, urls); err != nil {- t.Fatal(err)- }- if err := Migrate(ctx, adminURL); err != nil {- t.Fatal(err)- }- admin, err := pgxpool.New(ctx, adminURL)+ requireMigratedDatabase(t, ctx, runtimeURL)+ admin, err := pgxpool.New(ctx, writerURL)if err != nil {t.Fatal(err)}@@ -96,9 +96,9 @@ func TestExecutorListsOnlyTenantOwnedNamespaces(t *testing.T) {_, _ = admin.Exec(context.Background(), `delete from k8s_state.resource_state where cluster_id = 'rls-test'`)})- reconcileTestTenantRole(t, admin, "user-alice", "test-tenant-password")+ reconcileTestTenantRole(t, roleAdminURL, "user-alice", "test-tenant-password")- executor, err := NewExecutor(adminURL, "test-tenant-password")+ executor, err := NewExecutor(writerURL, "test-tenant-password")if err != nil {t.Fatal(err)}@@ -113,18 +113,15 @@ func TestExecutorListsOnlyTenantOwnedNamespaces(t *testing.T) {}func TestDirectTenantLoginCannotEscapeTenantIsolation(t *testing.T) {- adminURL := os.Getenv("CYCLOPS_TEST_DATABASE_URL")- if adminURL == "" {- t.Skip("set CYCLOPS_TEST_DATABASE_URL to run the PostgreSQL direct-login isolation test")+ runtimeURL := os.Getenv("CYCLOPS_TEST_RUNTIME_DATABASE_URL")+ writerURL := os.Getenv("CYCLOPS_TEST_STATE_WRITER_DATABASE_URL")+ roleAdminURL := os.Getenv("CYCLOPS_TEST_STATE_ROLE_ADMIN_DATABASE_URL")+ if runtimeURL == "" || writerURL == "" || roleAdminURL == "" {+ t.Skip("set CYCLOPS_TEST_RUNTIME_DATABASE_URL, CYCLOPS_TEST_STATE_WRITER_DATABASE_URL, and CYCLOPS_TEST_STATE_ROLE_ADMIN_DATABASE_URL to run the PostgreSQL direct-login isolation test")}ctx := context.Background()- if err := ReconcileFixedRoles(ctx, adminURL, fixedTestRoleURLs(t, adminURL)); err != nil {- t.Fatal(err)- }- if err := Migrate(ctx, adminURL); err != nil {- t.Fatal(err)- }- admin, err := pgxpool.New(ctx, adminURL)+ requireMigratedDatabase(t, ctx, runtimeURL)+ admin, err := pgxpool.New(ctx, writerURL)if err != nil {t.Fatal(err)}@@ -136,8 +133,8 @@ func TestDirectTenantLoginCannotEscapeTenantIsolation(t *testing.T) {bob = "user-bob"cluster = "direct-login-isolation-test")- aliceRole := reconcileTestTenantRole(t, admin, alice, password)- bobRole := reconcileTestTenantRole(t, admin, bob, password)+ aliceRole := reconcileTestTenantRole(t, roleAdminURL, alice, password)+ bobRole := reconcileTestTenantRole(t, roleAdminURL, bob, password)if _, err := admin.Exec(ctx, `delete from k8s_state.resource_state where cluster_id = $1`, cluster); err != nil {t.Fatal(err)}@@ -155,7 +152,7 @@ func TestDirectTenantLoginCannotEscapeTenantIsolation(t *testing.T) {_, _ = admin.Exec(context.Background(), `drop table if exists tenant_escape`)})- aliceConfig, err := pgx.ParseConfig(adminURL)+ aliceConfig, err := pgx.ParseConfig(writerURL)if err != nil {t.Fatal(err)}@@ -200,3 +197,64 @@ func TestDirectTenantLoginCannotEscapeTenantIsolation(t *testing.T) {t.Fatal("tenant role created a table")}}++func requireMigratedDatabase(t *testing.T, ctx context.Context, url string) {+ t.Helper()++ if err := database.RequireVersion(ctx, url, 1); err != nil {+ t.Fatalf("database must be migrated to version 1: %v", err)+ }+}++func reconcileTestTenantRole(t *testing.T, roleAdminURL, tenant, password string) string {+ t.Helper()++ ctx := context.Background()+ admin, err := pgxpool.New(ctx, roleAdminURL)+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(admin.Close)+ role := TenantRoleName(tenant)+ identifier := pgx.Identifier{role}.Sanitize()+ t.Cleanup(func() {+ if _, err := admin.Exec(context.Background(), `select k8s_state.unregister_tenant_role($1)`, role); err != nil {+ t.Errorf("unregister tenant role %s: %v", role, err)+ }+ if _, err := admin.Exec(context.Background(), "drop role if exists "+identifier); err != nil {+ t.Errorf("drop tenant role %s: %v", role, err)+ }+ })+ if _, err := admin.Exec(ctx, "drop role if exists "+identifier); err != nil {+ t.Fatal(err)+ }++ var passwordClause string+ if err := admin.QueryRow(+ ctx,+ `select format(' password %L', $1::text)`,+ password,+ ).Scan(&passwordClause); err != nil {+ t.Fatal(err)+ }+ for _, statement := range []string{+ "create role " + identifier + " login inherit nocreaterole" + passwordClause,+ "grant k8s_query_tenant to " + identifier,+ } {+ if _, err := admin.Exec(ctx, statement); err != nil {+ t.Fatal(err)+ }+ }++ fingerprint := sha256.Sum256([]byte(password))+ if _, err := admin.Exec(+ ctx,+ `select k8s_state.register_tenant_role($1, $2, $3)`,+ role,+ tenant,+ hex.EncodeToString(fingerprint[:]),+ ); err != nil {+ t.Fatal(err)+ }+ return role+}diff --git a/libs/fleet/backend/statequery/migrations/0001_postgres_state.sql b/libs/fleet/backend/statequery/migrations/0001_postgres_state.sqldeleted file mode 100644index f94174d9a3..0000000000--- a/libs/fleet/backend/statequery/migrations/0001_postgres_state.sql+++ /dev/null@@ -1,182 +0,0 @@-CREATE TABLE k8s_state.resource_state (- cluster_id text NOT NULL,- api_group text NOT NULL,- resource text NOT NULL,- namespace text NOT NULL,- name text NOT NULL,- capsule_tenant text,- uid text,- resource_version text,- schema_hash text NOT NULL,- watch_epoch bigint NOT NULL,- observed_sequence bigint NOT NULL,- labels jsonb NOT NULL DEFAULT '{}'::jsonb,- object jsonb NOT NULL,- source_time timestamptz,- ingested_at timestamptz NOT NULL DEFAULT clock_timestamp(),- PRIMARY KEY (cluster_id, api_group, resource, namespace, name),- CHECK (jsonb_typeof(labels) = 'object'),- CHECK (jsonb_typeof(object) = 'object')-);--CREATE INDEX resource_state_tenant_gvr_namespace_idx- ON k8s_state.resource_state- (capsule_tenant, cluster_id, api_group, resource, namespace, name);--CREATE INDEX resource_state_labels_gin_idx- ON k8s_state.resource_state USING gin (labels jsonb_path_ops);--CREATE TABLE k8s_state.watch_checkpoint (- cluster_id text NOT NULL,- api_group text NOT NULL,- resource text NOT NULL,- watch_epoch bigint NOT NULL,- last_resource_version text NOT NULL,- last_observed_sequence bigint NOT NULL,- initial_list_complete boolean NOT NULL,- updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),- PRIMARY KEY (cluster_id, api_group, resource)-);--CREATE TABLE k8s_state.resource_event_outbox (- event_id uuid PRIMARY KEY,- cluster_id text NOT NULL,- api_group text NOT NULL,- resource text NOT NULL,- namespace text NOT NULL,- name text NOT NULL,- capsule_tenant text,- uid text,- resource_version text,- schema_hash text NOT NULL,- event_type text NOT NULL CHECK (event_type IN ('ADDED', 'MODIFIED', 'DELETED')),- watch_epoch bigint NOT NULL,- observed_sequence bigint NOT NULL,- object jsonb NOT NULL,- observed_at timestamptz NOT NULL,- claimed_at timestamptz,- claim_id uuid,- exported_at timestamptz,- UNIQUE (cluster_id, api_group, resource, watch_epoch, observed_sequence),- CHECK (jsonb_typeof(object) = 'object')-);--CREATE INDEX resource_event_outbox_pending_idx- ON k8s_state.resource_event_outbox (observed_at, event_id)- WHERE exported_at IS NULL;--CREATE TABLE k8s_state.resource_schema (- schema_hash text PRIMARY KEY,- api_group text NOT NULL,- api_version text NOT NULL,- kind text NOT NULL,- resource text NOT NULL,- structural_schema jsonb NOT NULL,- first_seen_at timestamptz NOT NULL DEFAULT clock_timestamp(),- CHECK (jsonb_typeof(structural_schema) = 'object')-);--CREATE TABLE k8s_state.query_tenant_role (- role_name name PRIMARY KEY,- capsule_tenant text NOT NULL UNIQUE,- credential_fingerprint text NOT NULL,- created_at timestamptz NOT NULL DEFAULT clock_timestamp()-);--CREATE FUNCTION k8s_state.tenant_for_role(role_name name)-RETURNS text-LANGUAGE sql-STABLE-SECURITY DEFINER-SET search_path = k8s_state, pg_catalog-AS $$- SELECT capsule_tenant- FROM k8s_state.query_tenant_role- WHERE query_tenant_role.role_name = tenant_for_role.role_name-$$;--REVOKE ALL ON FUNCTION k8s_state.tenant_for_role(name) FROM PUBLIC;-GRANT EXECUTE ON FUNCTION k8s_state.tenant_for_role(name) TO k8s_query_tenant;--ALTER TABLE k8s_state.resource_state ENABLE ROW LEVEL SECURITY;-ALTER TABLE k8s_state.resource_state FORCE ROW LEVEL SECURITY;--CREATE POLICY writer_current_state ON k8s_state.resource_state-FOR ALL TO k8s_state_writer-USING (true)-WITH CHECK (true);--CREATE POLICY tenant_current_state ON k8s_state.resource_state-FOR SELECT TO k8s_query_tenant-USING (- capsule_tenant = k8s_state.tenant_for_role(current_user)- AND (- namespace <> ''- OR (api_group = '' AND resource = 'namespaces')- )-);--CREATE POLICY admin_current_state ON k8s_state.resource_state-FOR SELECT TO k8s_query_admin-USING (true);--CREATE VIEW k8s_api.current_resources-WITH (security_barrier = true, security_invoker = true) AS-SELECT- cluster_id,- api_group,- resource,- namespace,- name,- uid,- resource_version,- schema_hash,- labels,- object,- source_time,- ingested_at-FROM k8s_state.resource_state;--REVOKE ALL ON ALL TABLES IN SCHEMA k8s_state FROM PUBLIC;-REVOKE ALL ON ALL TABLES IN SCHEMA k8s_api FROM PUBLIC;-GRANT USAGE ON SCHEMA k8s_state TO k8s_state_writer, k8s_state_exporter;-GRANT SELECT, INSERT, UPDATE, DELETE ON k8s_state.resource_state TO k8s_state_writer;-GRANT SELECT, INSERT, UPDATE ON k8s_state.watch_checkpoint TO k8s_state_writer;-GRANT SELECT, INSERT, UPDATE ON k8s_state.resource_schema TO k8s_state_writer;-GRANT SELECT, INSERT ON k8s_state.resource_event_outbox TO k8s_state_writer;-GRANT SELECT, UPDATE, DELETE ON k8s_state.resource_event_outbox TO k8s_state_exporter;-GRANT USAGE ON SCHEMA k8s_api TO k8s_query_tenant, k8s_query_admin;-GRANT SELECT ON k8s_state.resource_state TO k8s_query_tenant, k8s_query_admin;-GRANT SELECT ON k8s_api.current_resources TO k8s_query_tenant, k8s_query_admin;--CREATE FUNCTION k8s_state.register_tenant_role(p_role_name name, p_capsule_tenant text, p_credential_fingerprint text)-RETURNS void-LANGUAGE sql-VOLATILE-SECURITY DEFINER-SET search_path = k8s_state, pg_catalog-AS $$- INSERT INTO k8s_state.query_tenant_role (role_name, capsule_tenant, credential_fingerprint)- VALUES (p_role_name, p_capsule_tenant, p_credential_fingerprint)- ON CONFLICT (role_name) DO UPDATE- SET capsule_tenant = EXCLUDED.capsule_tenant,- credential_fingerprint = EXCLUDED.credential_fingerprint-$$;--CREATE FUNCTION k8s_state.unregister_tenant_role(p_role_name name)-RETURNS void-LANGUAGE sql-VOLATILE-SECURITY DEFINER-SET search_path = k8s_state, pg_catalog-AS $$- DELETE FROM k8s_state.query_tenant_role- WHERE role_name = p_role_name-$$;--REVOKE ALL ON FUNCTION k8s_state.register_tenant_role(name, text, text) FROM PUBLIC;-REVOKE ALL ON FUNCTION k8s_state.unregister_tenant_role(name) FROM PUBLIC;-GRANT USAGE ON SCHEMA k8s_state TO k8s_role_admin;-GRANT SELECT ON k8s_state.query_tenant_role TO k8s_role_admin;-GRANT EXECUTE ON FUNCTION k8s_state.register_tenant_role(name, text, text) TO k8s_role_admin;-GRANT EXECUTE ON FUNCTION k8s_state.unregister_tenant_role(name) TO k8s_role_admin;diff --git a/libs/fleet/backend/statequery/roles.go b/libs/fleet/backend/statequery/roles.godeleted file mode 100644index 1979c91b8b..0000000000--- a/libs/fleet/backend/statequery/roles.go+++ /dev/null@@ -1,190 +0,0 @@-package statequery--import (- "context"- "fmt"-- "github.com/jackc/pgx/v5"-)--type FixedRoleURLs struct {- Writer string- Exporter string- Query string- RoleAdmin string-}--var fixedLoginRoles = []struct {- name string- url func(FixedRoleURLs) string- attr string-}{- {"k8s_state_writer", func(urls FixedRoleURLs) string { return urls.Writer }, "LOGIN INHERIT NOCREATEROLE"},- {"k8s_state_exporter", func(urls FixedRoleURLs) string { return urls.Exporter }, "LOGIN INHERIT NOCREATEROLE"},- {"k8s_role_admin", func(urls FixedRoleURLs) string { return urls.RoleAdmin }, "LOGIN NOINHERIT CREATEROLE"},-}--func parseFixedRoleURLs(urls FixedRoleURLs) (map[string]string, error) {- configured := 0- for _, role := range fixedLoginRoles {- if role.url(urls) != "" {- configured++- }- }- if configured == 0 {- return map[string]string{}, nil- }- if configured != len(fixedLoginRoles) {- return nil, fmt.Errorf("all fixed state role database URLs must be configured together")- }-- credentials := make(map[string]string, len(fixedLoginRoles))- for _, role := range fixedLoginRoles {- config, err := pgx.ParseConfig(role.url(urls))- if err != nil {- return nil, fmt.Errorf("parse database URL for %s: %w", role.name, err)- }- if config.User != role.name {- return nil, fmt.Errorf("database URL for %s uses unexpected user %q", role.name, config.User)- }- if config.Password == "" {- return nil, fmt.Errorf("database URL for %s has no password", role.name)- }- credentials[role.name] = config.Password- }- return credentials, nil-}--// ReconcileFixedRoles creates the fixed least-privilege roles and rotates their passwords.-func ReconcileFixedRoles(ctx context.Context, adminURL string, urls FixedRoleURLs) error {- credentials, err := parseFixedRoleURLs(urls)- if err != nil {- return err- }- if len(credentials) == 0 {- return nil- }- adminConfig, err := pgx.ParseConfig(adminURL)- if err != nil {- return fmt.Errorf("parse state role administrator URL: %w", err)- }- conn, err := pgx.ConnectConfig(ctx, adminConfig)- if err != nil {- return fmt.Errorf("connect state role administrator: %w", err)- }- defer conn.Close(ctx)-- tx, err := conn.Begin(ctx)- if err != nil {- return fmt.Errorf("begin fixed role reconciliation: %w", err)- }- defer tx.Rollback(ctx)- if _, err := tx.Exec(ctx, `select pg_advisory_xact_lock(hashtext('cyclops-k8s-state-fixed-roles'))`); err != nil {- return fmt.Errorf("lock fixed role reconciliation: %w", err)- }-- for _, role := range []struct{ name, attr string }{- {"k8s_state_owner", "NOLOGIN INHERIT NOCREATEROLE"},- {"k8s_query_tenant", "NOLOGIN INHERIT NOCREATEROLE"},- {"k8s_query_admin", "NOLOGIN INHERIT NOCREATEROLE"},- } {- if err := ensureRole(ctx, tx, role.name, role.attr, ""); err != nil {- return err- }- }- for _, role := range fixedLoginRoles {- if err := ensureRole(ctx, tx, role.name, role.attr, credentials[role.name]); err != nil {- return err- }- }- if err := removeLegacyQueryBroker(ctx, tx); err != nil {- return err- }-- adminIdentifier := pgx.Identifier{adminConfig.User}.Sanitize()- for _, statement := range []string{- "grant k8s_state_owner to " + adminIdentifier,- "grant k8s_query_tenant to k8s_role_admin with admin option",- } {- if _, err := tx.Exec(ctx, statement); err != nil {- return fmt.Errorf("apply fixed role membership: %w", err)- }- }-- if err := tx.Commit(ctx); err != nil {- return fmt.Errorf("commit fixed role reconciliation: %w", err)- }- return nil-}--func removeLegacyQueryBroker(ctx context.Context, tx pgx.Tx) error {- var exists bool- if err := tx.QueryRow(ctx, `select exists(select 1 from pg_roles where rolname = 'k8s_query_broker')`).Scan(&exists); err != nil {- return fmt.Errorf("check legacy query broker: %w", err)- }- if !exists {- return nil- }-- rows, err := tx.Query(ctx, `- select parent.rolname- from pg_auth_members membership- join pg_roles parent on parent.oid = membership.roleid- join pg_roles member on member.oid = membership.member- where member.rolname = 'k8s_query_broker'- `)- if err != nil {- return fmt.Errorf("list legacy query broker memberships: %w", err)- }- memberships := []string{}- for rows.Next() {- var roleName string- if err := rows.Scan(&roleName); err != nil {- return fmt.Errorf("scan legacy query broker membership: %w", err)- }- memberships = append(memberships, roleName)- }- if err := rows.Err(); err != nil {- return fmt.Errorf("iterate legacy query broker memberships: %w", err)- }- rows.Close()-- for _, roleName := range memberships {- statement := "revoke " + pgx.Identifier{roleName}.Sanitize() + " from k8s_query_broker"- if _, err := tx.Exec(ctx, statement); err != nil {- return fmt.Errorf("revoke legacy query broker membership: %w", err)- }- }- if _, err := tx.Exec(ctx, "revoke k8s_query_admin from k8s_query_broker"); err != nil {- return fmt.Errorf("revoke legacy query administrator membership: %w", err)- }- if _, err := tx.Exec(ctx, "drop role k8s_query_broker"); err != nil {- return fmt.Errorf("drop legacy query broker: %w", err)- }- return nil-}--func ensureRole(ctx context.Context, tx pgx.Tx, name, attributes, password string) error {- var exists bool- if err := tx.QueryRow(ctx, `select exists(select 1 from pg_roles where rolname = $1)`, name).Scan(&exists); err != nil {- return fmt.Errorf("check role %s: %w", name, err)- }- identifier := pgx.Identifier{name}.Sanitize()- if !exists {- if _, err := tx.Exec(ctx, "create role "+identifier); err != nil {- return fmt.Errorf("create role %s: %w", name, err)- }- }- statement := "alter role " + identifier + " " + attributes- if password != "" {- var passwordClause string- if err := tx.QueryRow(ctx, `select format(' password %L', $1::text)`, password).Scan(&passwordClause); err != nil {- return fmt.Errorf("quote password for role %s: %w", name, err)- }- statement += passwordClause- }- if _, err := tx.Exec(ctx, statement); err != nil {- return fmt.Errorf("configure role %s: %w", name, err)- }- return nil-}diff --git a/libs/fleet/backend/statequery/roles_pg_test.go b/libs/fleet/backend/statequery/roles_pg_test.godeleted file mode 100644index 022fd38433..0000000000--- a/libs/fleet/backend/statequery/roles_pg_test.go+++ /dev/null@@ -1,210 +0,0 @@-package statequery--import (- "context"- "crypto/sha256"- "encoding/hex"- "net/url"- "os"- "testing"-- "github.com/jackc/pgx/v5"- "github.com/jackc/pgx/v5/pgxpool"-)--func fixedTestRoleURLs(t *testing.T, adminURL string) FixedRoleURLs {- t.Helper()- parsed, err := url.Parse(adminURL)- if err != nil {- t.Fatal(err)- }- withRole := func(role, password string) string {- copy := *parsed- copy.User = url.UserPassword(role, password)- return copy.String()- }- return FixedRoleURLs{- Writer: withRole("k8s_state_writer", "test-writer"),- Exporter: withRole("k8s_state_exporter", "test-exporter"),- RoleAdmin: withRole("k8s_role_admin", "test-role-admin"),- }-}--func TestReconcileFixedRolesCreatesLeastPrivilegeRoles(t *testing.T) {- adminURL := os.Getenv("CYCLOPS_TEST_DATABASE_URL")- if adminURL == "" {- t.Skip("set CYCLOPS_TEST_DATABASE_URL to run the Postgres role test")- }- ctx := context.Background()- pool, err := pgxpool.New(ctx, adminURL)- if err != nil {- t.Fatal(err)- }- defer pool.Close()-- const legacyTenant = "k8s_legacy_query_broker_tenant"- if _, err := pool.Exec(ctx, "drop role if exists k8s_query_broker"); err != nil {- t.Fatalf("drop existing query broker: %v", err)- }- if _, err := pool.Exec(ctx, "drop role if exists "+legacyTenant); err != nil {- t.Fatalf("drop existing legacy tenant: %v", err)- }- defer func() {- if _, err := pool.Exec(ctx, "drop role if exists "+legacyTenant); err != nil {- t.Errorf("drop legacy tenant: %v", err)- }- }()- if _, err := pool.Exec(ctx, "create role "+legacyTenant+" nologin"); err != nil {- t.Fatalf("create legacy tenant: %v", err)- }- if _, err := pool.Exec(ctx, "create role k8s_query_broker login noinherit password 'legacy-broker'"); err != nil {- t.Fatalf("create legacy query broker: %v", err)- }- if _, err := pool.Exec(ctx, "grant "+legacyTenant+" to k8s_query_broker"); err != nil {- t.Fatalf("grant legacy tenant to query broker: %v", err)- }-- if err := ReconcileFixedRoles(ctx, adminURL, fixedTestRoleURLs(t, adminURL)); err != nil {- t.Fatalf("ReconcileFixedRoles: %v", err)- }-- expected := map[string]struct {- login, inherit, createRole bool- }{- "k8s_state_owner": {false, true, false},- "k8s_state_writer": {true, true, false},- "k8s_state_exporter": {true, true, false},- "k8s_query_tenant": {false, true, false},- "k8s_query_admin": {false, true, false},- "k8s_role_admin": {true, false, true},- }- for role, want := range expected {- var login, inherit, createRole bool- err := pool.QueryRow(ctx, `select rolcanlogin, rolinherit, rolcreaterole from pg_roles where rolname = $1`, role).Scan(&login, &inherit, &createRole)- if err != nil {- t.Fatalf("query role %s: %v", role, err)- }- if login != want.login || inherit != want.inherit || createRole != want.createRole {- t.Errorf("role %s flags = login:%t inherit:%t createrole:%t, want %+v", role, login, inherit, createRole, want)- }- }-- var brokerExists bool- if err := pool.QueryRow(ctx, `select exists(select 1 from pg_roles where rolname = 'k8s_query_broker')`).Scan(&brokerExists); err != nil {- t.Fatal(err)- }- if brokerExists {- t.Fatal("legacy query broker still exists")- }-- var legacyMembershipExists bool- if err := pool.QueryRow(ctx, `- select exists (- select 1- from pg_auth_members membership- join pg_roles parent on parent.oid = membership.roleid- join pg_roles member on member.oid = membership.member- where parent.rolname = $1 and member.rolname = 'k8s_query_broker'- )`, legacyTenant).Scan(&legacyMembershipExists); err != nil {- t.Fatal(err)- }- if legacyMembershipExists {- t.Fatal("legacy query broker membership remains")- }-- var tenantAdminOption bool- if err := pool.QueryRow(ctx, `- select membership.admin_option- from pg_auth_members membership- join pg_roles parent on parent.oid = membership.roleid- join pg_roles member on member.oid = membership.member- where parent.rolname = 'k8s_query_tenant' and member.rolname = 'k8s_role_admin'- `).Scan(&tenantAdminOption); err != nil {- t.Fatalf("query role administrator tenant membership: %v", err)- }- if !tenantAdminOption {- t.Fatal("role administrator lacks admin option for tenant role")- }-}--func TestReconcileFixedRolesWithoutLegacyQueryBroker(t *testing.T) {- adminURL := os.Getenv("CYCLOPS_TEST_DATABASE_URL")- if adminURL == "" {- t.Skip("set CYCLOPS_TEST_DATABASE_URL to run the Postgres role test")- }- ctx := context.Background()- pool, err := pgxpool.New(ctx, adminURL)- if err != nil {- t.Fatal(err)- }- defer pool.Close()-- if _, err := pool.Exec(ctx, "drop role if exists k8s_query_broker"); err != nil {- t.Fatalf("drop existing query broker: %v", err)- }- if err := ReconcileFixedRoles(ctx, adminURL, fixedTestRoleURLs(t, adminURL)); err != nil {- t.Fatalf("ReconcileFixedRoles without legacy broker: %v", err)- }-- var tenantAdminOption bool- if err := pool.QueryRow(ctx, `- select membership.admin_option- from pg_auth_members membership- join pg_roles parent on parent.oid = membership.roleid- join pg_roles member on member.oid = membership.member- where parent.rolname = 'k8s_query_tenant' and member.rolname = 'k8s_role_admin'- `).Scan(&tenantAdminOption); err != nil {- t.Fatalf("query role administrator tenant membership: %v", err)- }- if !tenantAdminOption {- t.Fatal("role administrator lacks admin option for tenant role")- }-}--func reconcileTestTenantRole(t *testing.T, admin *pgxpool.Pool, tenant, password string) string {- t.Helper()-- ctx := context.Background()- role := TenantRoleName(tenant)- identifier := pgx.Identifier{role}.Sanitize()- t.Cleanup(func() {- if _, err := admin.Exec(context.Background(), `select k8s_state.unregister_tenant_role($1)`, role); err != nil {- t.Errorf("unregister tenant role %s: %v", role, err)- }- if _, err := admin.Exec(context.Background(), "drop role if exists "+identifier); err != nil {- t.Errorf("drop tenant role %s: %v", role, err)- }- })- if _, err := admin.Exec(ctx, "drop role if exists "+identifier); err != nil {- t.Fatal(err)- }-- var passwordClause string- if err := admin.QueryRow(- ctx,- `select format(' password %L', $1::text)`,- password,- ).Scan(&passwordClause); err != nil {- t.Fatal(err)- }- for _, statement := range []string{- "create role " + identifier + " login inherit nocreaterole" + passwordClause,- "grant k8s_query_tenant to " + identifier,- } {- if _, err := admin.Exec(ctx, statement); err != nil {- t.Fatal(err)- }- }-- fingerprint := sha256.Sum256([]byte(password))- if _, err := admin.Exec(- ctx,- `select k8s_state.register_tenant_role($1, $2, $3)`,- role,- tenant,- hex.EncodeToString(fingerprint[:]),- ); err != nil {- t.Fatal(err)- }- return role-}diff --git a/libs/fleet/backend/statequery/roles_test.go b/libs/fleet/backend/statequery/roles_test.godeleted file mode 100644index aea9645e46..0000000000--- a/libs/fleet/backend/statequery/roles_test.go+++ /dev/null@@ -1,57 +0,0 @@-package statequery--import "testing"--func TestParseFixedRoleURLsRequiresExpectedUsers(t *testing.T) {- urls := FixedRoleURLs{- Writer: "postgres://k8s_state_writer:[email protected]/cyclops",- Exporter: "postgres://k8s_state_exporter:[email protected]/cyclops",- RoleAdmin: "postgres://k8s_role_admin:[email protected]/cyclops",- }- credentials, err := parseFixedRoleURLs(urls)- if err != nil {- t.Fatal(err)- }- if got := credentials["k8s_role_admin"]; got != "roles" {- t.Fatalf("role administrator password = %q", got)- }-- urls.Writer = "postgres://wrong:[email protected]/cyclops"- if _, err := parseFixedRoleURLs(urls); err == nil {- t.Fatal("expected unexpected writer username to fail")- }-}--func TestParseFixedRoleURLsDoesNotRequireQueryBroker(t *testing.T) {- urls := FixedRoleURLs{- Writer: "postgres://k8s_state_writer:writer@db/cyclops",- Exporter: "postgres://k8s_state_exporter:exporter@db/cyclops",- RoleAdmin: "postgres://k8s_role_admin:admin@db/cyclops",- }- credentials, err := parseFixedRoleURLs(urls)- if err != nil {- t.Fatal(err)- }- if _, exists := credentials["k8s_query_broker"]; exists {- t.Fatal("query broker credential remains")- }-}--func TestParseFixedRoleURLsAllowsAllURLsAbsent(t *testing.T) {- credentials, err := parseFixedRoleURLs(FixedRoleURLs{})- if err != nil {- t.Fatal(err)- }- if len(credentials) != 0 {- t.Fatalf("credentials = %#v, want empty", credentials)- }-}--func TestParseFixedRoleURLsRejectsPartialConfiguration(t *testing.T) {- _, err := parseFixedRoleURLs(FixedRoleURLs{- Writer: "postgres://k8s_state_writer:[email protected]/cyclops",- })- if err == nil {- t.Fatal("expected partial fixed-role URL configuration to fail")- }-}diff --git a/libs/fleet/backend/statequery/schema.go b/libs/fleet/backend/statequery/schema.godeleted file mode 100644index b055439e7b..0000000000--- a/libs/fleet/backend/statequery/schema.go+++ /dev/null@@ -1,153 +0,0 @@-package statequery--import (- "context"- "crypto/sha256"- "embed"- "encoding/hex"- "errors"- "fmt"- "io/fs"- "sort"- "strings"-- "github.com/jackc/pgx/v5"-)--//go:embed migrations/*.sql-var migrations embed.FS--type migrationFile struct {- Name string- SQL string- Digest string-}--func migrationSQL(name string) (string, error) {- contents, err := migrations.ReadFile("migrations/" + name)- if err != nil {- return "", fmt.Errorf("read state migration %s: %w", name, err)- }- return string(contents), nil-}--func embeddedMigrations() ([]migrationFile, error) {- entries, err := fs.ReadDir(migrations, "migrations")- if err != nil {- return nil, fmt.Errorf("list state migrations: %w", err)- }-- files := make([]migrationFile, 0, len(entries))- for _, entry := range entries {- if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {- continue- }- sql, err := migrationSQL(entry.Name())- if err != nil {- return nil, err- }- digest := sha256.Sum256([]byte(sql))- files = append(files, migrationFile{- Name: entry.Name(),- SQL: sql,- Digest: hex.EncodeToString(digest[:]),- })- }- sort.Slice(files, func(i, j int) bool { return files[i].Name < files[j].Name })- return files, nil-}--func checkAppliedDigest(name, applied, current string) error {- if applied != current {- return fmt.Errorf("state migration %s changed after application: recorded %s, current %s", name, applied, current)- }- return nil-}--// Migrate applies embedded state-query migrations exactly once in filename order.-func Migrate(ctx context.Context, adminURL string) error {- if adminURL == "" {- return nil- }- files, err := embeddedMigrations()- if err != nil {- return err- }- config, err := pgx.ParseConfig(adminURL)- if err != nil {- return fmt.Errorf("parse state migration database URL: %w", err)- }- conn, err := pgx.ConnectConfig(ctx, config)- if err != nil {- return fmt.Errorf("connect state migration database: %w", err)- }- defer conn.Close(ctx)-- tx, err := conn.Begin(ctx)- if err != nil {- return fmt.Errorf("begin state migration transaction: %w", err)- }- defer tx.Rollback(ctx)-- if _, err := tx.Exec(ctx, `select pg_advisory_xact_lock(hashtext('cyclops-k8s-state-migrations'))`); err != nil {- return fmt.Errorf("lock state migrations: %w", err)- }- if _, err := tx.Exec(ctx, `create schema if not exists k8s_state authorization k8s_state_owner`); err != nil {- return fmt.Errorf("create state migration schema: %w", err)- }- if _, err := tx.Exec(ctx, `alter schema k8s_state owner to k8s_state_owner`); err != nil {- return fmt.Errorf("own state migration schema: %w", err)- }- if _, err := tx.Exec(ctx, `create schema if not exists k8s_api authorization k8s_state_owner`); err != nil {- return fmt.Errorf("create state API schema: %w", err)- }- if _, err := tx.Exec(ctx, `alter schema k8s_api owner to k8s_state_owner`); err != nil {- return fmt.Errorf("own state API schema: %w", err)- }- if _, err := tx.Exec(ctx, `revoke create on schema public, k8s_state, k8s_api from public`); err != nil {- return fmt.Errorf("revoke public state schema creation: %w", err)- }- if _, err := tx.Exec(ctx, `- create table if not exists k8s_state.schema_migrations (- filename text primary key,- sha256 text not null,- applied_at timestamptz not null default clock_timestamp()- )`); err != nil {- return fmt.Errorf("create state migration ledger: %w", err)- }- if _, err := tx.Exec(ctx, `alter table k8s_state.schema_migrations owner to k8s_state_owner`); err != nil {- return fmt.Errorf("own state migration ledger: %w", err)- }- if _, err := tx.Exec(ctx, `set local role k8s_state_owner`); err != nil {- return fmt.Errorf("assume state owner role: %w", err)- }-- for _, file := range files {- var appliedDigest string- err := tx.QueryRow(ctx, `select sha256 from k8s_state.schema_migrations where filename = $1`, file.Name).Scan(&appliedDigest)- switch {- case err == nil:- if err := checkAppliedDigest(file.Name, appliedDigest, file.Digest); err != nil {- return err- }- continue- case !errors.Is(err, pgx.ErrNoRows):- return fmt.Errorf("read state migration %s: %w", file.Name, err)- }-- if _, err := tx.Exec(ctx, file.SQL); err != nil {- return fmt.Errorf("apply state migration %s: %w", file.Name, err)- }- if _, err := tx.Exec(ctx,- `insert into k8s_state.schema_migrations (filename, sha256) values ($1, $2)`,- file.Name, file.Digest,- ); err != nil {- return fmt.Errorf("record state migration %s: %w", file.Name, err)- }- }-- if err := tx.Commit(ctx); err != nil {- return fmt.Errorf("commit state migrations: %w", err)- }- return nil-}diff --git a/libs/fleet/backend/statequery/schema_pg_test.go b/libs/fleet/backend/statequery/schema_pg_test.godeleted file mode 100644index 55a7a53286..0000000000--- a/libs/fleet/backend/statequery/schema_pg_test.go+++ /dev/null@@ -1,82 +0,0 @@-package statequery--import (- "context"- "os"- "testing"-- "github.com/jackc/pgx/v5/pgxpool"-)--func TestMigrateCreatesStateSchemaAndRLS(t *testing.T) {- url := os.Getenv("CYCLOPS_TEST_DATABASE_URL")- if url == "" {- t.Skip("set CYCLOPS_TEST_DATABASE_URL to run the Postgres schema test")- }- ctx := context.Background()- if err := ReconcileFixedRoles(ctx, url, fixedTestRoleURLs(t, url)); err != nil {- t.Fatalf("ReconcileFixedRoles: %v", err)- }- if err := Migrate(ctx, url); err != nil {- t.Fatalf("Migrate: %v", err)- }- if err := Migrate(ctx, url); err != nil {- t.Fatalf("idempotent Migrate: %v", err)- }-- pool, err := pgxpool.New(ctx, url)- if err != nil {- t.Fatal(err)- }- defer pool.Close()-- for _, relation := range []string{- "k8s_state.resource_state",- "k8s_state.watch_checkpoint",- "k8s_state.resource_event_outbox",- "k8s_state.resource_schema",- "k8s_state.query_tenant_role",- "k8s_api.current_resources",- } {- var exists bool- if err := pool.QueryRow(ctx, `select to_regclass($1) is not null`, relation).Scan(&exists); err != nil {- t.Fatal(err)- }- if !exists {- t.Errorf("relation %s does not exist", relation)- }- }-- for _, column := range []string{"role_name", "capsule_tenant", "credential_fingerprint", "created_at"} {- var exists bool- err := pool.QueryRow(ctx, `- select exists (- select 1- from information_schema.columns- where table_schema = 'k8s_state'- and table_name = 'query_tenant_role'- and column_name = $1- )`, column).Scan(&exists)- if err != nil || !exists {- t.Fatalf("query_tenant_role column %s exists=%t err=%v", column, exists, err)- }- }-- for _, function := range []string{- "k8s_state.register_tenant_role(name,text,text)",- "k8s_state.unregister_tenant_role(name)",- } {- var exists bool- if err := pool.QueryRow(ctx, "select to_regprocedure($1) is not null", function).Scan(&exists); err != nil || !exists {- t.Fatalf("function %s exists=%t err=%v", function, exists, err)- }- }-- var forced bool- if err := pool.QueryRow(ctx, `select relforcerowsecurity from pg_class where oid = 'k8s_state.resource_state'::regclass`).Scan(&forced); err != nil {- t.Fatal(err)- }- if !forced {- t.Fatal("resource_state does not FORCE ROW LEVEL SECURITY")- }-}diff --git a/libs/fleet/backend/statequery/schema_test.go b/libs/fleet/backend/statequery/schema_test.godeleted file mode 100644index 89b980d267..0000000000--- a/libs/fleet/backend/statequery/schema_test.go+++ /dev/null@@ -1,67 +0,0 @@-package statequery--import (- "encoding/hex"- "strings"- "testing"-)--func TestMigrationContainsRequiredObjects(t *testing.T) {- migration, err := migrationSQL("0001_postgres_state.sql")- if err != nil {- t.Fatal(err)- }-- for _, required := range []string{- "CREATE TABLE k8s_state.resource_state",- "CREATE TABLE k8s_state.watch_checkpoint",- "CREATE TABLE k8s_state.resource_event_outbox",- "CREATE TABLE k8s_state.resource_schema",- "CREATE TABLE k8s_state.query_tenant_role",- "ALTER TABLE k8s_state.resource_state FORCE ROW LEVEL SECURITY",- "CREATE POLICY writer_current_state",- "CREATE POLICY tenant_current_state",- "resource = 'namespaces'",- "CREATE VIEW k8s_api.current_resources",- "GRANT SELECT ON k8s_api.current_resources TO k8s_query_tenant, k8s_query_admin",- "credential_fingerprint text NOT NULL",- "CREATE FUNCTION k8s_state.register_tenant_role(p_role_name name, p_capsule_tenant text, p_credential_fingerprint text)",- "CREATE FUNCTION k8s_state.unregister_tenant_role(p_role_name name)",- "GRANT SELECT ON k8s_state.query_tenant_role TO k8s_role_admin",- } {- if !strings.Contains(migration, required) {- t.Errorf("migration missing %q", required)- }- }- if strings.Contains(migration, "k8s_query_broker") {- t.Fatal("migration retains the removed query broker")- }-}--func TestEmbeddedMigrationsAreSortedAndDigested(t *testing.T) {- files, err := embeddedMigrations()- if err != nil {- t.Fatal(err)- }- if len(files) != 1 {- t.Fatalf("len(files) = %d, want 1", len(files))- }- if files[0].Name != "0001_postgres_state.sql" {- t.Fatalf("Name = %q", files[0].Name)- }- if len(files[0].Digest) != 64 {- t.Fatalf("digest length = %d, want 64", len(files[0].Digest))- }- if _, err := hex.DecodeString(files[0].Digest); err != nil {- t.Fatalf("digest is not hexadecimal: %v", err)- }-}--func TestCheckAppliedDigestRejectsChangedMigration(t *testing.T) {- if err := checkAppliedDigest("0001.sql", "old", "new"); err == nil {- t.Fatal("expected changed migration digest to fail")- }- if err := checkAppliedDigest("0001.sql", "same", "same"); err != nil {- t.Fatalf("unchanged digest failed: %v", err)- }-}