diff --git a/README.md b/README.md index 4363f8f88..520219e25 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,11 @@ The connector is configured using a YAML file that defines: - **Entitlements**: Permissions and roles that can be granted to resources - **Provisioning Actions**: SQL queries for granting/revoking entitlements; see [docs/provisioning.md](docs/provisioning.md) for `validation_queries` semantics (a no-rows validation is treated as idempotent success on the DDL engines Db2 and Oracle: on by default for Db2, opt-in via `validation_queries_signal_idempotency` for Oracle) +When `static_entitlements` is configured for a resource type, it takes precedence +over dynamic `entitlements` for both sync and provisioning. A static entitlement +without provisioning configured, or an unknown entitlement in static mode, cannot +fall back to a dynamic mapping's grant/revoke queries. + For Postgres behind a transaction-mode pooler (PgBouncer, Supabase pooler on port 6543, etc.), set `default_query_exec_mode` to `simple_protocol` via the DSN query string or `connect.params` to avoid prepared-statement conflicts (SQLSTATE 42P05). When unset, baton-sql leaves the URL unchanged and pgx uses its default (`cache_statement`). ```yaml diff --git a/pkg/bsql/provisioning.go b/pkg/bsql/provisioning.go index d1d0da26f..956bc74ba 100644 --- a/pkg/bsql/provisioning.go +++ b/pkg/bsql/provisioning.go @@ -33,6 +33,12 @@ func (s *SQLSyncer) getProvisioningConfig(ctx context.Context, entitlementID str } } + // Static entitlements take precedence during sync, so dynamic mappings must + // not supply provisioning for an unsupported or unknown static entitlement. + if s.config.StaticEntitlements != nil { + return nil, false + } + // Check dynamic entitlements if s.config.Entitlements != nil { for _, e := range s.config.Entitlements.Map { diff --git a/pkg/bsql/provisioning_static_test.go b/pkg/bsql/provisioning_static_test.go new file mode 100644 index 000000000..8f874100b --- /dev/null +++ b/pkg/bsql/provisioning_static_test.go @@ -0,0 +1,79 @@ +package bsql + +import ( + "testing" + + v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" + sdkEntitlement "github.com/conductorone/baton-sdk/pkg/types/entitlement" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestStaticProvisioningDoesNotFallThrough(t *testing.T) { + for _, operation := range []string{"grant", "revoke"} { + for _, mode := range []string{"unsupported", "unknown", "empty_static", "supported", "dynamic_only"} { + t.Run(operation+"/"+mode, func(t *testing.T) { + s, db := newRevokeProvisioningTestSyncer(t) + provisioning := func(role string) *EntitlementProvisioning { + return &EntitlementProvisioning{ + Grant: &GrantEntitlementProvisioningQueries{EntitlementProvisioningQueries: EntitlementProvisioningQueries{ + Queries: []string{`INSERT INTO user_roles (user_id, role) VALUES ('user-1', '` + role + `')`}, + }}, + Revoke: &RevokeEntitlementProvisioningQueries{EntitlementProvisioningQueries: EntitlementProvisioningQueries{ + Queries: []string{`DELETE FROM user_roles WHERE user_id = 'user-1' AND role = '` + role + `'`}, + }}, + } + } + static := &EntitlementMapping{Id: "viewer", DisplayName: "'Viewer'"} + s.config.StaticEntitlements = []*EntitlementMapping{static} + s.config.Entitlements = &EntitlementsQuery{Map: []*EntitlementMapping{{Provisioning: provisioning("admin")}}} + switch mode { + case "supported": + static.Provisioning = provisioning("viewer") + case "empty_static": + s.config.StaticEntitlements = []*EntitlementMapping{} + case "dynamic_only": + s.config.StaticEntitlements = nil + } + resource := &v2.Resource{Id: &v2.ResourceId{ResourceType: "role", Resource: "role-1"}} + requested := &v2.Entitlement{Id: sdkEntitlement.NewEntitlementID(resource, "viewer"), Resource: resource} + if mode == "unsupported" || mode == "supported" { + entitlements, _, _, err := s.Entitlements(t.Context(), resource, nil) + require.NoError(t, err) + require.Len(t, entitlements, 1) + requested = entitlements[0] + } + if mode == "unknown" { + requested.Id = sdkEntitlement.NewEntitlementID(resource, "unknown") + } + if operation == "revoke" { + seedUserWithRoles(t, db, "user-1", "admin", "viewer") + } + var err error + if operation == "grant" { + _, err = s.Grant(t.Context(), userPrincipal("user-1"), requested) + } else { + _, err = s.Revoke(t.Context(), &v2.Grant{Entitlement: requested, Principal: userPrincipal("user-1")}) + } + supported := mode == "supported" || mode == "dynamic_only" + if supported { + assert.NoError(t, err) + } else { + assert.Error(t, err) + } + wantAdmin, wantViewer := 0, 0 + if operation == "revoke" { + wantAdmin, wantViewer = 1, 1 + } + if mode == "supported" { + wantViewer = 1 - wantViewer + } + if mode == "dynamic_only" { + wantAdmin = 1 - wantAdmin + } + assert.Equal(t, wantAdmin, countRows(t, db, `SELECT COUNT(*) FROM user_roles WHERE role = 'admin'`)) + assert.Equal(t, wantViewer, countRows(t, db, `SELECT COUNT(*) FROM user_roles WHERE role = 'viewer'`)) + }) + } + } +}