From f9a04ab4c5d6147ac77cacaf059c8bc01e7d903b Mon Sep 17 00:00:00 2001 From: Mateo Hernandez Date: Tue, 3 Nov 2026 15:22:28 -0300 Subject: [PATCH 1/9] feat(databricks): add unity catalog table and volume sync CXH-2723. Tables and volumes hang from the schema, each with its own privilege set and a read-only owner. The tree is unchanged from d10a7e9e, the state reviewed and approved on #73. Co-Authored-By: Claude Opus 5 --- README.md | 30 ++- baton_capabilities.json | 155 ++++++++++++ docs/connector.mdx | 16 +- docs/docs-info.md | 10 +- pkg/connector/catalog.go | 1 - pkg/connector/connector.go | 4 +- pkg/connector/connector_test.go | 5 +- pkg/connector/metastore.go | 1 - pkg/connector/raw_id_test.go | 13 + pkg/connector/resource_types.go | 16 ++ pkg/connector/schema.go | 44 +++- pkg/connector/schema_child.go | 357 ++++++++++++++++++++++++++ pkg/connector/securable_helpers.go | 83 +++++-- pkg/connector/table.go | 358 +++++++++++++++++++++++++++ pkg/connector/volume.go | 208 ++++++++++++++++ pkg/databricks/models.go | 22 ++ pkg/databricks/unity_catalog.go | 102 ++++++++ pkg/databricks/unity_catalog_test.go | 40 +++ pkg/databricks/vars.go | 14 ++ 19 files changed, 1440 insertions(+), 39 deletions(-) create mode 100644 pkg/connector/schema_child.go create mode 100644 pkg/connector/table.go create mode 100644 pkg/connector/volume.go diff --git a/README.md b/README.md index c5f3cecd..b4920b72 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,9 @@ baton resources - Roles - Metastores - Catalogs (under their metastore) +- Schemas (under their catalog) +- Tables (under their schema) +- Volumes (under their schema) The connector fetches all resources from the account and every workspace the service principal can access. To limit the scope, pass a comma-separated list of @@ -95,9 +98,9 @@ other metastores and their grants from ConductorOne. Catalogs must also be visible through a listed workspace. A list that names no workspace on the account is rejected during credential validation, before any sync runs. -Metastores and catalogs are opt-in: each resource type stays off until enabled -for the connector in ConductorOne. Catalog sync uses the existing OAuth -credentials and configuration fields. +Metastores, catalogs, schemas, tables, and volumes are Unity Catalog securables +and are opt-in: each type stays off until it is enabled for the connector in +ConductorOne. They use the existing OAuth credentials and configuration fields. ## Authentication @@ -238,6 +241,8 @@ account └── metastore └── catalog └── schema + ├── table + └── volume ``` A metastore exposes its privileges as entitlements (`CREATE_CATALOG`, @@ -285,11 +290,11 @@ or `databricks-exclude-workspaces`. ## Unity Catalog provisioning -A metastore, catalog or schema privilege can be granted to and revoked from a -user, group or service principal. The `owner` entitlement is read-only: granting -or revoking it fails with an `InvalidArgument` error. +A privilege on a metastore, catalog, schema, table or volume can be granted to +and revoked from a user, group or service principal. The `owner` entitlement is +read-only: granting or revoking it fails with an `InvalidArgument` error. -If a catalog becomes unreachable, provisioning must distinguish deletion from +If a securable becomes unreachable, provisioning must distinguish deletion from a workspace binding or visibility change. Requests can fail with `FailedPrecondition` when deletion cannot be proved. @@ -297,9 +302,14 @@ a workspace binding or visibility change. Requests can fail with The service principal must own the metastore, or be assigned **Admin** directly on at least one running workspace attached to it. Admin inherited only through a -group is not enough. Databricks answers an under-permissioned read with HTTP 200 -and only the rows the caller may see, so the connector reads a metastore only -through a workspace that passes that check. When no attached workspace does, the +group is not enough. The connector reads a metastore only through a workspace +that passes that check. Listing every schema takes ownership of the catalog, or +`MANAGE`, `USE_SCHEMA`, or `ALL_PRIVILEGES` on that catalog. Listing every table +or volume needs more: ownership of the metastore, the catalog, or the schema, +direct Admin when the workspace admins group owns the catalog, or +`ALL_PRIVILEGES` on the catalog. `MANAGE` and `USE_SCHEMA` are not enough for +tables or volumes. Databricks answers an under-permissioned read with HTTP 200 +and only the rows the caller may see. The connector refuses that read, so the sync fails and ConductorOne keeps the grants it already has. For a catalog's full grants list, the service principal must own the metastore diff --git a/baton_capabilities.json b/baton_capabilities.json index fbee54d9..5aa6dbdc 100644 --- a/baton_capabilities.json +++ b/baton_capabilities.json @@ -383,6 +383,82 @@ ] } }, + { + "resourceType": { + "id": "table", + "displayName": "Table", + "traits": [ + "TRAIT_APP" + ], + "annotations": [ + { + "@type": "type.googleapis.com/c1.connector.v2.OptInRequired" + }, + { + "@type": "type.googleapis.com/c1.connector.v2.CapabilityPermissions", + "permissions": [ + { + "permission": "unity-catalog" + }, + { + "permission": "scim" + }, + { + "permission": "access-management" + }, + { + "permission": "provisioning" + }, + { + "permission": "Account admin" + }, + { + "permission": "Workspace admin" + }, + { + "permission": "USE_CATALOG" + }, + { + "permission": "USE_SCHEMA" + } + ] + } + ] + }, + "capabilities": [ + "CAPABILITY_SYNC", + "CAPABILITY_PROVISION" + ], + "permissions": { + "permissions": [ + { + "permission": "unity-catalog" + }, + { + "permission": "scim" + }, + { + "permission": "access-management" + }, + { + "permission": "provisioning" + }, + { + "permission": "Account admin" + }, + { + "permission": "Workspace admin" + }, + { + "permission": "USE_CATALOG" + }, + { + "permission": "USE_SCHEMA" + } + ] + }, + "optInRequired": true + }, { "resourceType": { "id": "user", @@ -424,6 +500,85 @@ ] } }, + { + "resourceType": { + "id": "volume", + "displayName": "Volume", + "traits": [ + "TRAIT_APP" + ], + "annotations": [ + { + "@type": "type.googleapis.com/c1.connector.v2.OptInRequired" + }, + { + "@type": "type.googleapis.com/c1.connector.v2.CapabilityPermissions", + "permissions": [ + { + "permission": "unity-catalog" + }, + { + "permission": "scim" + }, + { + "permission": "access-management" + }, + { + "permission": "provisioning" + }, + { + "permission": "Account admin" + }, + { + "permission": "Workspace admin" + }, + { + "permission": "USE_CATALOG" + }, + { + "permission": "USE_SCHEMA" + } + ] + }, + { + "@type": "type.googleapis.com/c1.connector.v2.SkipEntitlements" + } + ] + }, + "capabilities": [ + "CAPABILITY_SYNC", + "CAPABILITY_PROVISION" + ], + "permissions": { + "permissions": [ + { + "permission": "unity-catalog" + }, + { + "permission": "scim" + }, + { + "permission": "access-management" + }, + { + "permission": "provisioning" + }, + { + "permission": "Account admin" + }, + { + "permission": "Workspace admin" + }, + { + "permission": "USE_CATALOG" + }, + { + "permission": "USE_SCHEMA" + } + ] + }, + "optInRequired": true + }, { "resourceType": { "id": "workspace", diff --git a/docs/connector.mdx b/docs/connector.mdx index d9c5cfe1..d7321be7 100644 --- a/docs/connector.mdx +++ b/docs/connector.mdx @@ -17,7 +17,9 @@ sidebarTitle: "Databricks" | Roles | | | | Schemas | | | | Service principals | | | +| Tables | | | | Users | | | +| Volumes | | | | Workspaces | | | The Databricks connector supports [automatic account provisioning and deprovisioning](/product/admin/account-provisioning). @@ -30,25 +32,25 @@ Revoking the workspace **member** entitlement removes the principal's entire wor [This connector syncs non-human identities](/product/admin/nhi) and displays them on the **Identities overview** dashboard. -## Metastore, catalog and schema access in C1 +## Metastore, catalog, schema, table and volume access in C1 The connector syncs Databricks [Unity Catalog](https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/privileges) -metastores, the catalogs in them, and the schemas in those catalogs as C1 resources. Each privilege Databricks +metastores, the catalogs in them, the schemas in those catalogs, and the tables and volumes in those schemas as C1 resources. Each privilege Databricks records on one of them (`CREATE_CATALOG` on a metastore, `USE_CATALOG` or -`SELECT` on a catalog, `USE_SCHEMA` on a schema, and so on) becomes an entitlement in C1, so you can +`SELECT` on a catalog, `USE_SCHEMA` on a schema, `SELECT` on a table, `READ_VOLUME` on a volume, and so on) becomes an entitlement in C1, so you can review who holds it, request it, and revoke it for users, groups and service principals. A grant held by a group is expandable so C1 can resolve the group's members as holders. Ownership is shown as a read-only `owner` -entitlement — it cannot be granted or revoked through C1. +entitlement — it cannot be granted or revoked through C1. Views, materialized views, metric views and streaming tables are tables; `table_type` picks which privileges can be requested. `ALL_PRIVILEGES` on a view, and `MODIFY` on a streaming table, are shown when they already exist and cannot be requested. -The Metastores, Catalogs and Schemas resource types are turned off by default. Turn on the ones you want from the connector's configuration page in C1. Catalogs sync under their metastore and schemas under their catalog, so turn on every level above the one you want; turning on Catalogs alone, or Schemas without Catalogs, syncs nothing. They all use the same OAuth credentials; no additional configuration fields are needed. +The Metastores, Catalogs, Schemas, Tables and Volumes resource types are turned off by default. Turn on the ones you want from the connector's configuration page in C1. Each level syncs under its parent securable, so turn on every level above the one you want; turning on Catalogs alone, or Tables without Schemas, syncs nothing. They all use the same OAuth credentials; no additional configuration fields are needed. **Notes on Unity Catalog access in C1:** -- Ownership is shown but cannot be provisioned. An owner that is not a Databricks account identity, such as a system account or a workspace admins group, is not shown at all. +- Ownership is shown but cannot be provisioned. An owner that is not an account identity, such as `System user`, is not shown. A workspace admins group is shown when it resolves as an account group. - **MANAGE** and **ALL_PRIVILEGES** are not offered on a metastore, because Databricks rejects both at that level. - A catalog is synced when any running workspace in scope attached to its metastore lists it, and the same catalog name is kept once per metastore. A catalog that only excluded workspaces can reach is not synced. - The built-in `system` catalog is never synced, and neither is any other catalog Databricks marks as a system catalog, whoever owns it. Their schemas are not synced either. @@ -238,6 +240,8 @@ Workspace **Admin** replaces all of this only when the catalog is owned by that A catalog whose schemas cannot all be listed fails the sync, and so does a schema whose privilege list cannot be proved complete, named in the error. +Listing every table or volume needs more than **USE SCHEMA** or **MANAGE**. The service principal must own the metastore, the catalog, or the schema, be a direct Admin when the workspace admins group owns the catalog, or hold **ALL PRIVILEGES** on the catalog. Otherwise the sync fails. + **Done.** Here's the set of credentials you'll need when setting up the connector: - Account ID diff --git a/docs/docs-info.md b/docs/docs-info.md index c8c6d939..75fb63d6 100644 --- a/docs/docs-info.md +++ b/docs/docs-info.md @@ -6,7 +6,7 @@ While developing the connector, please fill out this form. This information is n > The connector syncs the Databricks account, workspaces, users, groups, service principals, and roles. > - > It also syncs Unity Catalog metastores and their catalogs. Both resource types are opt-in — each stays off until enabled per resource type in C1. + > It also syncs Unity Catalog metastores, catalogs, schemas, tables, and volumes. Those resource types are opt-in — each stays off until enabled per resource type in C1. 2. Can the connector provision any resources? If so, which ones? @@ -14,7 +14,7 @@ While developing the connector, please fill out this form. This information is n > > - **User accounts**: create and delete account users. > - **Entitlements**: grant and revoke role and membership assignments on accounts, workspaces, groups, service principals, and roles. - > - **Unity Catalog privileges**: grant and revoke a metastore or catalog privilege for a user, group or service principal. The `owner` entitlement is read-only and cannot be granted or revoked. + > - **Unity Catalog privileges**: grant and revoke a privilege on a metastore, catalog, schema, table, or volume for a user, group or service principal. The `owner` entitlement is read-only and cannot be granted or revoked. ## Connector credentials @@ -70,4 +70,8 @@ While developing the connector, please fill out this form. This information is n - **A schema is keyed by its full name.** `SchemaInfo` does carry an immutable `schema_id`, but no endpoint accepts it: `GET` and `PATCH` on a schema both address it as `catalog.schema`, so keying on the id would mean listing the catalog's schemas on every read and write just to recover that name. The parent catalog has no immutable id at all, so a catalog rename re-keys the schema either way. The resource id is `::.`, and a schema rename re-keys the resource and its grants, as it does for a catalog. - **`information_schema` is skipped in every catalog.** Databricks creates it read-only in every catalog and its privileges cannot be granted, so it would sync entitlements no one can hold or request. - **Listing schemas is weaker than reading their privileges.** `USE_SCHEMA` and `ALL_PRIVILEGES` on the catalog each make the listing complete. Neither reads a schema's privileges, because Databricks leaves `MANAGE` out of `ALL_PRIVILEGES`: that read needs ownership of the metastore, the catalog or the schema, `MANAGE` on the schema or the catalog, or direct workspace Admin where the admins group owns the catalog. -- **Not synced yet:** tables, volumes, functions, registered models, external locations, storage credentials, connections, shares, recipients, and tags. +- **Tables and volumes are opt-in and hang from a schema.** Each is off until it is turned on in C1, and syncs nothing unless Schemas, Catalogs and Metastores are on as well. +- **A table and a volume are keyed by their full name.** Neither exposes an immutable id, so the resource id extends the schema key: `::..`. A rename re-keys the resource and its grants, as it does for a catalog and a schema. +- **One `table` securable covers more than tables.** Views, materialized views, metric views and streaming tables are all `table`. `table_type` picks the privilege set, so a table's entitlements are built per instance. `ALL_PRIVILEGES` on a view, `MODIFY` on a streaming table, and `REFRESH` on a type whose grants API refuses it are reported when already held and cannot be requested. +- **Listing a schema's tables or volumes is stricter than listing its schemas.** `USE_SCHEMA` and `ALL_PRIVILEGES` on the catalog each list every schema; only `ALL_PRIVILEGES` lists every table and volume, alongside ownership of the metastore, the catalog or the schema, or direct workspace Admin where the admins group owns the catalog. Reading one table's or one volume's privileges needs ownership of the metastore, the catalog, the schema or the object itself, or `MANAGE` on the object, its schema or its catalog. +- **Not synced yet:** functions, registered models, external locations, storage credentials, connections, shares, recipients, and tags. diff --git a/pkg/connector/catalog.go b/pkg/connector/catalog.go index 5d4d5c15..03ced15b 100644 --- a/pkg/connector/catalog.go +++ b/pkg/connector/catalog.go @@ -123,7 +123,6 @@ var errCatalogGone = errors.New("catalog no longer exists") type catalogBuilder struct { securableOps - // workspaces is the configured allowlist, empty when none was given. workspaces map[string]struct{} } diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index f06c4efc..45d0ca84 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -45,6 +45,8 @@ func (d *Databricks) ResourceSyncers(_ context.Context) []connectorbuilder.Resou newMetastoreBuilder(d.client, d.clientID, workspaces, principals), newCatalogBuilder(d.client, d.clientID, workspaces, principals), newSchemaBuilder(d.client, d.clientID, workspaces, principals), + newTableBuilder(d.client, d.clientID, workspaces, principals), + newVolumeBuilder(d.client, d.clientID, workspaces, principals), } } @@ -76,7 +78,7 @@ func (d *Databricks) Metadata(_ context.Context) (*v2.ConnectorMetadata, error) return &v2.ConnectorMetadata{ DisplayName: "Databricks", Description: "Connector syncing Databricks workspaces, users, groups, service principals and roles to Baton, " + - "along with Unity Catalog data assets — metastores, catalogs and schemas — and the privileges granted on them", + "along with Unity Catalog data assets — metastores, catalogs, schemas, tables and volumes — and the privileges granted on them", AccountCreationSchema: &v2.ConnectorAccountCreationSchema{ FieldMap: map[string]*v2.ConnectorAccountCreationSchema_Field{ "email": { diff --git a/pkg/connector/connector_test.go b/pkg/connector/connector_test.go index b2d96dcb..e5e41cbc 100644 --- a/pkg/connector/connector_test.go +++ b/pkg/connector/connector_test.go @@ -87,7 +87,10 @@ func TestResourceSyncersAlwaysRegisterTheUnityCatalogBuilders(t *testing.T) { for _, syncer := range d.ResourceSyncers(ctx) { registered = append(registered, syncer.ResourceType(ctx).GetId()) } - for _, want := range []string{metastoreResourceType.Id, catalogResourceType.Id, schemaResourceType.Id} { + for _, want := range []string{ + metastoreResourceType.Id, catalogResourceType.Id, schemaResourceType.Id, + tableResourceType.Id, volumeResourceType.Id, + } { if !slices.Contains(registered, want) { t.Errorf("registered syncers = %v, want them to include %s", registered, want) } diff --git a/pkg/connector/metastore.go b/pkg/connector/metastore.go index 775be68a..4d68d54a 100644 --- a/pkg/connector/metastore.go +++ b/pkg/connector/metastore.go @@ -26,7 +26,6 @@ var _ connectorbuilder.StaticEntitlementSyncerV2 = (*metastoreBuilder)(nil) type metastoreBuilder struct { securableOps - // workspaces is the configured allowlist, empty when none was given. workspaces map[string]struct{} } diff --git a/pkg/connector/raw_id_test.go b/pkg/connector/raw_id_test.go index d0552e9d..c28a39db 100644 --- a/pkg/connector/raw_id_test.go +++ b/pkg/connector/raw_id_test.go @@ -84,6 +84,19 @@ func TestResourcesHaveRawId(t *testing.T) { &v2.ResourceId{ResourceType: catalogResourceType.Id, Resource: "ms1::cat"}, schemaRoute{})), "ms1::cat.sch", }, + { + // A table and a volume extend the schema's id with their own name. + "table", + build(tableResource(&databricks.Table{Name: "orders"}, "ms1", "cat", "sch", + &v2.ResourceId{ResourceType: schemaResourceType.Id, Resource: "ms1::cat.sch"}, schemaChildRoute{})), + "ms1::cat.sch.orders", + }, + { + "volume", + build(volumeResource(&databricks.Volume{Name: "files"}, "ms1", "cat", "sch", + &v2.ResourceId{ResourceType: schemaResourceType.Id, Resource: "ms1::cat.sch"}, schemaChildRoute{})), + "ms1::cat.sch.files", + }, } for _, tc := range cases { diff --git a/pkg/connector/resource_types.go b/pkg/connector/resource_types.go index fab13dfd..609dc99c 100644 --- a/pkg/connector/resource_types.go +++ b/pkg/connector/resource_types.go @@ -102,4 +102,20 @@ var ( Annotations: annotationsForStaticSecurableResourceType( securablePermissions(permissionUseCatalog, permissionUseSchema, privilegeManage)...), } + + tableResourceType = &v2.ResourceType{ + Id: "table", + DisplayName: "Table", + Annotations: annotationsForSecurableResourceType(securablePermissions(permissionUseCatalog, permissionUseSchema)...), + } + + volumeResourceType = &v2.ResourceType{ + Id: "volume", + DisplayName: "Volume", + Annotations: annotations.New( + &v2.OptInRequired{}, + &v2.SkipEntitlements{}, + capabilityPermissions(securablePermissions(permissionUseCatalog, permissionUseSchema)...), + ), + } ) diff --git a/pkg/connector/schema.go b/pkg/connector/schema.go index 483e4778..46dfeb24 100644 --- a/pkg/connector/schema.go +++ b/pkg/connector/schema.go @@ -156,7 +156,11 @@ func schemaResource( nil, rs.WithResourceProfile(profile), rs.WithParentResourceID(parent), - rs.WithAnnotation(&v2.RawId{Id: resourceID}), + rs.WithAnnotation( + &v2.ChildResourceType{ResourceTypeId: tableResourceType.Id}, + &v2.ChildResourceType{ResourceTypeId: volumeResourceType.Id}, + &v2.RawId{Id: resourceID}, + ), ) } @@ -321,8 +325,44 @@ func (b *schemaBuilder) pushRoute( return nil } +// schemaThrough finds one schema in the listing of workspace, nil when that +// workspace does not list it. Its caller has already proven the listing holds +// every schema in the catalog, so an absence here is the schema being gone. +func schemaThrough( + ctx context.Context, + client *databricks.Client, + workspace, catalogName, schemaName string, + annos *annotations.Annotations, +) (*databricks.Schema, error) { + pageToken := "" + for { + schemas, next, rateLimit, err := client.ListSchemas(ctx, workspace, catalogName, pageToken, ResourcesPageSize) + noteRateLimit(annos, rateLimit) + if err != nil { + return nil, schemaListingError(catalogName, workspace, err) + } + + for _, schema := range schemas { + if schema.Name == schemaName { + return &schema, nil + } + } + + if next == "" { + return nil, nil + } + if next == pageToken { + return nil, status.Errorf(codes.Internal, + "databricks-connector: the schemas of catalog %s repeat page token %q through workspace %s", + catalogName, next, workspace) + } + pageToken = next + } +} + // schemaListingError returns NotFound only when Databricks names the missing -// catalog; any other refusal leaves the schema listing unknown. +// catalog. Any other refusal leaves the listing unknown rather than empty: a +// short list would read as deletions in C1. func schemaListingError(catalogName, workspace string, err error) error { if databricks.IsSecurableGone(err, errorCodeCatalogGone) { return uhttp.WrapErrors(codes.NotFound, diff --git a/pkg/connector/schema_child.go b/pkg/connector/schema_child.go new file mode 100644 index 00000000..8ecfecfa --- /dev/null +++ b/pkg/connector/schema_child.go @@ -0,0 +1,357 @@ +package connector + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + "strconv" + "strings" + + "github.com/conductorone/baton-databricks/pkg/databricks" + v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" + "github.com/conductorone/baton-sdk/pkg/annotations" + "github.com/conductorone/baton-sdk/pkg/pagination" + rs "github.com/conductorone/baton-sdk/pkg/types/resource" + "github.com/conductorone/baton-sdk/pkg/uhttp" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" + "go.uber.org/zap" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +// profileKeySchemaOwner carries the owner of the parent schema. Owning the +// schema is one of the things that makes a table's or a volume's privilege read +// complete, and Grants is handed only the table, so List writes the owner down +// instead of listing the schemas again on every call. +const profileKeySchemaOwner = "schema_owner" + +// schemaChildRoute is the workspace a schema's tables or volumes are listed +// through, carried in the page token so later pages neither re-resolve the route +// nor prove again that the parent listing is complete. The owners ride along so +// each resource's profile can carry them. See profileKeySchemaOwner. +type schemaChildRoute struct { + Workspace string `json:"w"` + WorkspaceID string `json:"i"` + WorkspaceName string `json:"n"` + MetastoreOwner string `json:"o"` + CatalogOwner string `json:"c"` + SchemaOwner string `json:"s"` +} + +// listSchemaChildren pages one level that hangs from a schema — a table or a +// volume. pushSchemaChildRoute re-runs the schema syncer's completeness proof +// on the first page of every schema, once for tables and again for volumes. +// The privilege reads in that proof bypass the HTTP cache. The owners the +// proof learns are written onto each resource, so Grants does not repeat it. +// +// fetch reads one page through the routed workspace and turns it into resources. +func listSchemaChildren( + ctx context.Context, + client *databricks.Client, + clientID string, + workspaces map[string]struct{}, + resourceType *v2.ResourceType, + noun string, + parentResourceID *v2.ResourceId, + attr rs.SyncOpAttrs, + fetch func(ctx context.Context, route schemaChildRoute, metastoreID, catalogName, schemaName, pageToken string) ( + []*v2.Resource, string, *v2.RateLimitDescription, error), +) ([]*v2.Resource, *rs.SyncOpResults, error) { + if parentResourceID.GetResourceType() != schemaResourceType.Id { + return nil, nil, nil + } + + metastoreID, catalogName, schemaName, err := parseSchemaID(parentResourceID.GetResource()) + if err != nil { + return nil, nil, err + } + // The schema syncer leaves Databricks' own read-only schema out, so nothing + // under it is ever synced either. + if schemaName == informationSchema { + return nil, nil, nil + } + + annos := annotations.Annotations{} + bag := &pagination.Bag{} + if err := bag.Unmarshal(attr.PageToken.Token); err != nil { + return nil, nil, status.Errorf(codes.InvalidArgument, "databricks-connector: invalid %s page token: %v", noun, err) + } + + if bag.Current() == nil { + if err := pushSchemaChildRoute(ctx, client, clientID, workspaces, resourceType, noun, + metastoreID, catalogName, schemaName, bag, &annos); err != nil { + return nil, &rs.SyncOpResults{Annotations: annos}, err + } + if bag.Current() == nil { + ctxzap.Extract(ctx).Debug("databricks-connector: no workspace in sync scope lists the schema; listing none of its children", + zap.String("resource_type", resourceType.Id), + zap.String("schema", parentResourceID.GetResource())) + + return nil, &rs.SyncOpResults{Annotations: annos}, nil + } + } + + var route schemaChildRoute + if err := json.Unmarshal([]byte(bag.ResourceID()), &route); err != nil { + return nil, nil, status.Errorf(codes.InvalidArgument, "databricks-connector: invalid %s page token: %v", noun, err) + } + + rv, next, rateLimit, err := fetch(ctx, route, metastoreID, catalogName, schemaName, bag.PageToken()) + noteRateLimit(&annos, rateLimit) + if err != nil { + return nil, &rs.SyncOpResults{Annotations: annos}, + schemaChildListingError(noun, catalogName+schemaNameSeparator+schemaName, route.Workspace, err) + } + + nextToken, err := bag.NextToken(next) + if err != nil { + return nil, &rs.SyncOpResults{Annotations: annos}, err + } + + return rv, &rs.SyncOpResults{Annotations: annos, NextPageToken: nextToken}, nil +} + +// childListRemedy is what a refusal to sync a schema whose table or volume +// listing is partial tells the operator to fix. USE_SCHEMA and MANAGE list +// schemas; they do not list every table or volume. +const childListRemedy = "the connector's service principal must own the metastore, the catalog or the schema, " + + "or hold ALL_PRIVILEGES on the catalog" + +// pushSchemaChildRoute records a workspace that can list every table or volume +// of the schema. It walks the running in-scope workspaces until one lists the +// catalog and the child listing is complete. An error from a workspace stops +// the walk. An empty result is an error: it would make ConductorOne delete +// the tables and volumes. +func pushSchemaChildRoute( + ctx context.Context, + client *databricks.Client, + clientID string, + workspaces map[string]struct{}, + resourceType *v2.ResourceType, + noun string, + metastoreID, catalogName, schemaName string, + bag *pagination.Bag, + annos *annotations.Annotations, +) error { + scope, err := scopedWorkspaces(ctx, client, workspaces, annos) + if err != nil { + return err + } + + usable, inScope, err := scope.attached(ctx, client, metastoreID, annos) + if err != nil { + return assignmentListError(metastoreID, err) + } + if len(usable) == 0 { + if inScope { + return status.Errorf(codes.FailedPrecondition, + "databricks-connector: metastore %s has no running workspace in sync scope", metastoreID) + } + + return nil + } + + owner, err := metastoreOwner(ctx, client, metastoreID, annos) + if err != nil { + return err + } + + fullName := catalogName + schemaNameSeparator + schemaName + var sawCatalog bool + for i := range usable { + workspace := usable[i] + catalog, err := catalogThrough(ctx, client, workspace.DeploymentName, metastoreID, catalogName, annos) + if err != nil { + return err + } + if catalog == nil { + continue + } + sawCatalog = true + + route := schemaChildRoute{ + Workspace: workspace.DeploymentName, + WorkspaceID: strconv.Itoa(workspace.ID), + WorkspaceName: workspace.Name, + MetastoreOwner: owner, + CatalogOwner: catalog.Owner, + } + trust := securableTrust{ + ownPrincipal: clientID, + metastoreOwner: owner, + parentOwner: catalog.Owner, + fullName: catalogName, + workspace: route.Workspace, + workspaceID: route.WorkspaceID, + } + schemasComplete, err := schemaListingIsComplete(ctx, client, trust, annos) + if err != nil { + return err + } + + schema, err := schemaThrough(ctx, client, route.Workspace, catalogName, schemaName, annos) + if err != nil { + return err + } + if schemasComplete && schema == nil { + ctxzap.Extract(ctx).Debug("databricks-connector: the schema is no longer listed; listing none of its children", + zap.String("resource_type", resourceType.Id), + zap.String("schema", fullName), + zap.String("workspace", route.Workspace)) + + return nil + } + // Owning the schema is enough to list its children. Anything else a + // partial list hides cannot be told from a schema that is gone. + if schema == nil || (!schemasComplete && !ownsPrincipal(clientID, schema.Owner)) { + continue + } + + trust.schemaOwner = schema.Owner + childrenComplete, err := childListingIsComplete(ctx, client, trust, annos) + if err != nil { + return err + } + if !childrenComplete { + continue + } + route.SchemaOwner = schema.Owner + + encoded, err := json.Marshal(route) + if err != nil { + return err + } + bag.Push(pagination.PageState{ResourceTypeID: resourceType.Id, ResourceID: string(encoded)}) + + return nil + } + if !sawCatalog { + return status.Errorf(codes.FailedPrecondition, + "databricks-connector: no workspace in sync scope lists catalog %s of metastore %s", catalogName, metastoreID) + } + + return status.Errorf(codes.PermissionDenied, + "databricks-connector: cannot list every %s in schema %s; %s", noun, fullName, childListRemedy) +} + +// childListingIsComplete is true when ListTables or ListVolumes returns every +// child of the schema. USE_SCHEMA and MANAGE make the schema list complete. +// They do not make the table or volume list complete. +func childListingIsComplete( + ctx context.Context, + client *databricks.Client, + trust securableTrust, + annos *annotations.Annotations, +) (bool, error) { + if ownsPrincipal(trust.ownPrincipal, trust.metastoreOwner) || + ownsPrincipal(trust.ownPrincipal, trust.parentOwner) || + ownsPrincipal(trust.ownPrincipal, trust.schemaOwner) { + return true, nil + } + + if ownerWorkspaceID, ok := catalogAdminWorkspaceID(trust.parentOwner); ok { + admin, err := isWorkspaceAdmin(ctx, client, trust.ownPrincipal, ownerWorkspaceID, annos) + if err != nil || admin { + return admin, err + } + } + + complete, err := holdsPrivilegeOn(ctx, client, trust, databricks.SecurableCatalog, trust.fullName, + []string{privilegeAllPrivileges}, []string{errorCodeCatalogGone}, annos) + if errors.Is(err, errSecurableGone) { + return false, errCatalogGone + } + + return complete, err +} + +// schemaChildListingError reads a workspace that refuses the listing as unknown, +// not as listing nothing: a short list would read as deletions in C1, and the SDK +// reads a NotFound as a securable with no children. +func schemaChildListingError(noun, schemaFullName, workspace string, err error) error { + switch apiStatus(err) { + case http.StatusUnauthorized, http.StatusForbidden, http.StatusNotFound: + return uhttp.WrapErrors(codes.FailedPrecondition, + fmt.Sprintf("databricks-connector: the %ss of schema %s cannot be listed through workspace %s, "+ + "so which of them are in scope is unknown; %s", noun, schemaFullName, workspace, childListRemedy), + err) + } + + return fmt.Errorf("databricks-connector: failed to list the %ss of schema %s through workspace %s: %w", + noun, schemaFullName, workspace, err) +} + +// schemaChildResourceID keys a table or a volume by its metastore and its dotted +// full name, which extends the schema's "{metastore_id}::{catalog}.{schema}". +func schemaChildResourceID(metastoreID, catalogName, schemaName, name string) string { + return metastoreID + securableKeySeparator + + strings.Join([]string{catalogName, schemaName, name}, schemaNameSeparator) +} + +// parseSchemaChildID reads a table or volume resource id, +// "{metastore_id}::{catalog}.{schema}.{name}". +func parseSchemaChildID(resourceID, noun string) (string, string, string, string, error) { + invalid := status.Errorf(codes.InvalidArgument, "databricks-connector: invalid %s resource id %q", noun, resourceID) + + metastoreID, fullName, ok := strings.Cut(resourceID, securableKeySeparator) + if !ok { + return "", "", "", "", invalid + } + + parts := strings.Split(fullName, schemaNameSeparator) + if len(parts) != 3 || metastoreID == "" || parts[0] == "" || parts[1] == "" || parts[2] == "" { + return "", "", "", "", invalid + } + + return metastoreID, parts[0], parts[1], parts[2], nil +} + +// schemaChildProfile is what every table and volume carries, before the level +// adds its own name and type. +func schemaChildProfile(metastoreID, catalogName, schemaName, fullName, owner, securableType string, route schemaChildRoute) map[string]any { + return map[string]any{ + profileKeyMetastoreID: metastoreID, + profileKeyMetastoreOwner: route.MetastoreOwner, + profileKeyFullName: fullName, + profileKeyCatalogName: catalogName, + profileKeyCatalogOwner: route.CatalogOwner, + profileKeySchemaName: schemaName, + profileKeySchemaOwner: route.SchemaOwner, + profileKeyOwner: owner, + profileKeySecurableType: securableType, + profileKeyWorkspace: route.Workspace, + profileKeyWorkspaceID: route.WorkspaceID, + profileKeyWorkspaceName: route.WorkspaceName, + } +} + +// schemaChildGrantsAreTrustworthy is true for an owner of the table or volume, its schema, its catalog, or the metastore, or for a holder of MANAGE on any of those. +func schemaChildGrantsAreTrustworthy( + ctx context.Context, + client *databricks.Client, + trust securableTrust, + securableType string, + annos *annotations.Annotations, +) (bool, error) { + // An owner holds every privilege implicitly, which is not reported as a grant. + if ownsPrincipal(trust.ownPrincipal, trust.metastoreOwner) || + ownsPrincipal(trust.ownPrincipal, trust.parentOwner) || + ownsPrincipal(trust.ownPrincipal, trust.schemaOwner) || + ownsPrincipal(trust.ownPrincipal, trust.owner) { + return true, nil + } + + // The workspace admins group acts as the owner for a direct Admin of its workspace. + if ownerWorkspaceID, ok := catalogAdminWorkspaceID(trust.parentOwner); ok { + admin, err := isWorkspaceAdmin(ctx, client, trust.ownPrincipal, ownerWorkspaceID, annos) + if err != nil || admin { + return admin, err + } + } + + // MANAGE on the securable, its schema or its catalog makes the privilege + // list complete, and one effective read reports all three. + return holdsEffectivePrivilegeOn(ctx, client, trust, securableType, trust.fullName, managePrivilege, annos) +} diff --git a/pkg/connector/securable_helpers.go b/pkg/connector/securable_helpers.go index f2c51513..02000a08 100644 --- a/pkg/connector/securable_helpers.go +++ b/pkg/connector/securable_helpers.go @@ -36,6 +36,15 @@ type securableOps struct { level securableLevel } +// withLevel returns the ops addressed at another level. A table's privilege set +// follows its table_type, so that level is decided per resource rather than once +// on the builder. +func (o securableOps) withLevel(level securableLevel) securableOps { + o.level = level + + return o +} + // securableLevel is the only thing the permissions endpoint needs to tell one // Unity Catalog level from another: how a resource id addresses the securable, // what it can be granted, and what a refusal tells the operator. @@ -65,6 +74,16 @@ type securableLevel struct { // of absence: an unbound or unprivileged workspace answers 404 too. goneErrorCodes []string + // ambiguousGoneCodes are codes a missing securable shares with a workspace + // that cannot serve it, so on their own they prove nothing. A level that + // names any must also set confirmGone. + ambiguousGoneCodes []string + + // confirmGone reads the securable itself to settle an ambiguous code. A + // level whose codes are all unambiguous leaves it nil. + confirmGone func(ctx context.Context, client *databricks.Client, fullName, workspace string, + annos *annotations.Annotations) (bool, error) + // trusts reports whether the connector reads every privilege assignment on // the securable. What proves that differs per level, so the rule travels // with the level rather than being decided by the shared path. @@ -90,6 +109,8 @@ type securableTrust struct { parentOwner string + schemaOwner string + fullName string workspace string workspaceID string @@ -112,18 +133,20 @@ func ensureGrantsAreTrustworthy( metastoreOwner, _ := rs.GetProfileStringValue(profile, profileKeyMetastoreOwner) workspaceID, _ := rs.GetProfileStringValue(profile, profileKeyWorkspaceID) parentOwner, _ := rs.GetProfileStringValue(profile, profileKeyCatalogOwner) + schemaOwner, _ := rs.GetProfileStringValue(profile, profileKeySchemaOwner) trustworthy, err := level.trusts(ctx, client, securableTrust{ ownPrincipal: clientID, owner: owner, metastoreOwner: metastoreOwner, parentOwner: parentOwner, + schemaOwner: schemaOwner, fullName: fullName, workspace: workspace, workspaceID: workspaceID, }, annos) if err != nil { - return trustReadError(level, fullName, workspace, err) + return trustReadError(ctx, client, level, fullName, workspace, err, annos) } if !trustworthy { return status.Errorf(codes.PermissionDenied, @@ -137,11 +160,20 @@ func ensureGrantsAreTrustworthy( // trustReadError classifies the 404 a trust read left for the level. A proven // absence reports errSecurableGone; an unproven one is refused, because read as // an empty result C1 deletes everything the securable held. -func trustReadError(level securableLevel, fullName, workspace string, err error) error { +func trustReadError( + ctx context.Context, + client *databricks.Client, + level securableLevel, + fullName, workspace string, + err error, + annos *annotations.Annotations, +) error { if !errors.Is(err, errTrustRead404) { return err } - if databricks.IsSecurableGone(err, level.goneErrorCodes...) { + // A volume names the generic code, which a workspace that cannot serve it + // returns too, so absence is confirmed by reading the securable itself. + if securableIsGone(ctx, client, level, fullName, workspace, err, annos) { return errSecurableGone } @@ -191,10 +223,7 @@ func (o securableOps) syncGrants( assignments, nextPageToken, rateLimit, err := client.ListPermissions(ctx, workspace, level.securableType, fullName, "", pageToken) noteRateLimit(&annos, rateLimit) if err != nil { - // A securable deleted between List and this read is not a failed sync. - // Only a level that names its gone codes can reach this: with none, - // IsSecurableGone is false and every 404 still refuses the read. - if databricks.IsSecurableGone(err, level.goneErrorCodes...) { + if securableIsGone(ctx, client, level, fullName, workspace, err, &annos) { return nil, &rs.SyncOpResults{Annotations: annos}, nil } return nil, &rs.SyncOpResults{Annotations: annos}, refuseUnreadable(fmt.Sprintf( @@ -223,9 +252,6 @@ func (o securableOps) syncGrants( options := groupSecurableGrantOptions(principalId) for _, privilege := range assignment.Privileges { if !slices.Contains(level.privileges, privilege) { - // A privilege Databricks added after this connector shipped is not - // an entitlement, so it cannot be emitted. Say so rather than - // dropping it without a trace. l.Debug("databricks-connector: the securable reports a privilege this connector does not model", zap.String("securable", resourceID), zap.String("privilege", privilege)) @@ -442,10 +468,7 @@ func (o securableOps) revoke( }) noteRateLimit(&annos, rateLimit) if err != nil { - // Databricks names the missing securable in error_code. That is the only - // answer that proves it is gone, so there is nothing left to revoke: a - // bare 404 also comes back from a workspace the securable is not bound to. - if databricks.IsSecurableGone(err, level.goneErrorCodes...) { + if securableIsGone(ctx, client, level, change.fullName, change.workspace, err, &annos) { annos.Update(&v2.GrantAlreadyRevoked{}) return annos, nil @@ -489,3 +512,35 @@ func (o securableOps) revoke( func isSecurableGoneSentinel(err error) bool { return errors.Is(err, errSecurableGone) || errors.Is(err, errCatalogGone) || errors.Is(err, errMetastoreGone) } + +// securableIsGone reports whether err proves the securable no longer exists. An +// unambiguous code settles it alone. A code a missing securable shares with a +// workspace that cannot serve it is settled by reading the securable, and a read +// that does not answer leaves the absence unproven rather than assuming it. +func securableIsGone( + ctx context.Context, + client *databricks.Client, + level securableLevel, + fullName, workspace string, + err error, + annos *annotations.Annotations, +) bool { + if databricks.IsSecurableGone(err, level.goneErrorCodes...) { + return true + } + if level.confirmGone == nil || !databricks.IsSecurableGone(err, level.ambiguousGoneCodes...) { + return false + } + + gone, confirmErr := level.confirmGone(ctx, client, fullName, workspace, annos) + if confirmErr != nil { + ctxzap.Extract(ctx).Debug("databricks-connector: could not confirm whether the securable is gone", + zap.String("securable", fullName), + zap.String("workspace", workspace), + zap.Error(confirmErr)) + + return false + } + + return gone +} diff --git a/pkg/connector/table.go b/pkg/connector/table.go new file mode 100644 index 00000000..23e0f65a --- /dev/null +++ b/pkg/connector/table.go @@ -0,0 +1,358 @@ +package connector + +import ( + "context" + "fmt" + "slices" + "strings" + + "github.com/conductorone/baton-databricks/pkg/databricks" + v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" + "github.com/conductorone/baton-sdk/pkg/annotations" + "github.com/conductorone/baton-sdk/pkg/connectorbuilder" + ent "github.com/conductorone/baton-sdk/pkg/types/entitlement" + rs "github.com/conductorone/baton-sdk/pkg/types/resource" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" + "go.uber.org/zap" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +var _ connectorbuilder.ResourceSyncerV2 = (*tableBuilder)(nil) + +const ( + securableTypeTable = "TABLE" + + // errorCodeTableGone is what Databricks names in error_code when the table a + // request addresses does not exist. + errorCodeTableGone = "TABLE_DOES_NOT_EXIST" + + // The canonical UPPER_SNAKE form the API answers with. + privilegeAllPrivileges = "ALL_PRIVILEGES" + privilegeRefresh = "REFRESH" + + // A view and a materialized view are both securable_type "table", so + // table_type is the only field that says which privileges apply. + tableTypeManaged = "MANAGED" + tableTypeExternal = "EXTERNAL" + tableTypeManagedShallowClone = "MANAGED_SHALLOW_CLONE" + tableTypeExternalShallowClone = "EXTERNAL_SHALLOW_CLONE" + tableTypeForeign = "FOREIGN" + tableTypeView = "VIEW" + tableTypeMaterializedView = "MATERIALIZED_VIEW" + tableTypeStreamingTable = "STREAMING_TABLE" + tableTypeMetricView = "METRIC_VIEW" + + profileKeyTableName = "table_name" + profileKeyTableType = "table_type" + + // tableWriteRemedy is what a 403 on the PATCH tells the operator. + tableWriteRemedy = "the connector must own the metastore, the parent catalog, the parent schema or the table, " + + "or hold MANAGE on the table, the parent schema or the parent catalog" + + // tableTrustRemedy is what a refusal to report a partial privilege list + // tells the operator. + tableTrustRemedy = "the connector's service principal must own the metastore, the parent catalog, the parent schema or the table, " + + "hold MANAGE on the table, the parent schema or the parent catalog, or be assigned Admin directly on the workspace whose " + + "admins group owns the parent catalog" +) + +// tablePrivileges is the set a real table takes. BROWSE, and everything that +// creates a securable, stop higher up. +// https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/privileges +var tablePrivileges = []string{ + privilegeAllPrivileges, + "APPLY_TAG", + "DELETE", + "INSERT", + privilegeManage, + "MODIFY", + "READ_METADATA", + "SELECT", + "UPDATE", +} + +// viewPrivileges matches the privileges reference, ALL_PRIVILEGES included. The +// REST grants API still answers an added ALL_PRIVILEGES on a view with a 400, so +// Grant rejects that write while Grants still reports one SQL already granted. +var viewPrivileges = []string{ + privilegeAllPrivileges, + "APPLY_TAG", + privilegeManage, + "READ_METADATA", + "SELECT", +} + +// A materialized view accepts ALL_PRIVILEGES, and a permissions read answers +// with only it, not the SELECT and REFRESH it implies. +var materializedViewPrivileges = append(slices.Clone(viewPrivileges), privilegeRefresh) + +// A streaming table takes SELECT and REFRESH. MODIFY is reported when a +// principal already holds it and is not offered: the streaming-tables reference +// lists SELECT and REFRESH only, and a grant of MODIFY has not been seen to succeed. +var streamingTablePrivileges = []string{ + privilegeAllPrivileges, + "APPLY_TAG", + privilegeManage, + "MODIFY", + "READ_METADATA", + privilegeRefresh, + "SELECT", +} + +// A foreign table is read-only: its rows live in the federated source, so the +// DML trio and MODIFY cannot be granted on one. +// https://docs.databricks.com/aws/en/data-governance/unity-catalog/access-control/privileges-reference +var foreignTablePrivileges = slices.DeleteFunc(slices.Clone(tablePrivileges), func(privilege string) bool { + return slices.Contains([]string{"DELETE", "INSERT", "MODIFY", "UPDATE"}, privilege) +}) + +// anyTablePrivileges is for a table whose variant is not knowable. REFRESH is in +// it so a held one is not dropped, and Grant still refuses to ask for it: only a +// materialized view or a streaming table accepts that write. +var anyTablePrivileges = append(slices.Clone(tablePrivileges), privilegeRefresh) + +type tableBuilder struct { + securableOps + workspaces map[string]struct{} +} + +func newTableBuilder(client *databricks.Client, clientID string, workspaces map[string]struct{}, sync syncPrincipals) *tableBuilder { + return &tableBuilder{ + securableOps: securableOps{client: client, clientID: clientID, sync: sync, level: tableLevel(privilegesForTableType(""))}, + workspaces: workspaces, + } +} + +func (b *tableBuilder) ResourceType(_ context.Context) *v2.ResourceType { + return tableResourceType +} + +// tablePermissionsName: the permissions endpoint addresses a table by its dotted +// full name. The metastore is carried by the host. +func tablePermissionsName(resourceID string) (string, error) { + _, catalogName, schemaName, name, err := parseSchemaChildID(resourceID, "table") + if err != nil { + return "", err + } + + return strings.Join([]string{catalogName, schemaName, name}, schemaNameSeparator), nil +} + +func tableGrantsAreTrustworthy( + ctx context.Context, + client *databricks.Client, + trust securableTrust, + annos *annotations.Annotations, +) (bool, error) { + return schemaChildGrantsAreTrustworthy(ctx, client, trust, databricks.SecurableTable, annos) +} + +// tableLevel is built per table rather than declared once: table_type decides +// which privileges this one reports and accepts. See privilegesForTableType. +func tableLevel(privileges []string) securableLevel { + return securableLevel{ + securableType: databricks.SecurableTable, + noun: "table", + privileges: privileges, + writeRemedy: tableWriteRemedy, + trustRemedy: tableTrustRemedy, + permissionsName: tablePermissionsName, + trusts: tableGrantsAreTrustworthy, + // Dropping a parent drops the table with it, so any of the three codes + // means there is no table left to revoke on. + goneErrorCodes: []string{errorCodeTableGone, errorCodeSchemaGone, errorCodeCatalogGone}, + } +} + +func tableResource( + table *databricks.Table, + metastoreID, catalogName, schemaName string, + parent *v2.ResourceId, + route schemaChildRoute, +) (*v2.Resource, error) { + fullName := strings.Join([]string{catalogName, schemaName, table.Name}, schemaNameSeparator) + profile := schemaChildProfile(metastoreID, catalogName, schemaName, fullName, table.Owner, securableTypeTable, route) + profile[profileKeyTableName] = table.Name + profile[profileKeyTableType] = table.TableType + + resourceID := schemaChildResourceID(metastoreID, catalogName, schemaName, table.Name) + + // The display name is the full name rather than the leaf: main.sales.orders + // and main.finance.orders would otherwise both read "orders" in C1. + return rs.NewResource( + fullName, + tableResourceType, + resourceID, + rs.WithResourceProfile(profile), + rs.WithParentResourceID(parent), + rs.WithAnnotation(&v2.RawId{Id: resourceID}), + ) +} + +// List pages the tables, views and materialized views of one schema. +func (b *tableBuilder) List(ctx context.Context, parentResourceID *v2.ResourceId, attr rs.SyncOpAttrs) ([]*v2.Resource, *rs.SyncOpResults, error) { + return listSchemaChildren(ctx, b.client, b.clientID, b.workspaces, tableResourceType, "table", parentResourceID, attr, + func(ctx context.Context, route schemaChildRoute, metastoreID, catalogName, schemaName, pageToken string) ( + []*v2.Resource, string, *v2.RateLimitDescription, error) { + tables, next, rateLimit, err := b.client.ListTables(ctx, route.Workspace, catalogName, schemaName, pageToken, ResourcesPageSize) + if err != nil { + return nil, "", rateLimit, err + } + + var rv []*v2.Resource + for _, table := range tables { + if table.Name == "" { + continue + } + + built, err := tableResource(&table, metastoreID, catalogName, schemaName, parentResourceID, route) + if err != nil { + return nil, "", rateLimit, fmt.Errorf( + "databricks-connector: failed to build the resource for table %s.%s.%s: %w", catalogName, schemaName, table.Name, err) + } + rv = append(rv, built) + } + + return rv, next, rateLimit, nil + }) +} + +// isViewLike reports a view or a metric view. Both reject the table DML set, and +// the grants API rejects adding ALL_PRIVILEGES on either. +func isViewLike(tableType string) bool { + switch strings.ToUpper(strings.TrimSpace(tableType)) { + case tableTypeView, tableTypeMetricView: + return true + default: + return false + } +} + +// refreshIsRequestable reports the table types whose grants API accepts REFRESH. +// The privileges reference lists it on a materialized view, and a streaming table +// takes it too. On every other type, including one this connector does not name, +// a held REFRESH is still reported and the grant is not offered. +func refreshIsRequestable(tableType string) bool { + switch strings.ToUpper(strings.TrimSpace(tableType)) { + case tableTypeMaterializedView, tableTypeStreamingTable: + return true + default: + return false + } +} + +// privilegesForTableType returns the set for a known table_type. MANAGED, +// EXTERNAL and both shallow clones stay on the table set. A foreign table is +// read-only. Anything else, including a type Databricks adds later, gets the +// union, so a privilege already held is reported rather than dropped. +func privilegesForTableType(tableType string) []string { + if isViewLike(tableType) { + return viewPrivileges + } + + switch strings.ToUpper(strings.TrimSpace(tableType)) { + case tableTypeMaterializedView: + return materializedViewPrivileges + case tableTypeStreamingTable: + return streamingTablePrivileges + case tableTypeManaged, tableTypeExternal, tableTypeManagedShallowClone, tableTypeExternalShallowClone: + return tablePrivileges + case tableTypeForeign: + return foreignTablePrivileges + default: + return anyTablePrivileges + } +} + +// privilegesForTable is the set this table reports. A missing table_type falls +// back to the union rather than to one variant. +func privilegesForTable(ctx context.Context, resource *v2.Resource) []string { + variant, _ := rs.GetProfileStringValue(rs.GetProfile(resource), profileKeyTableType) + if variant == "" { + ctxzap.Extract(ctx).Debug("databricks-connector: table has no table_type; reporting every variant's privileges", + zap.String("table", resource.GetId().GetResource())) + + return anyTablePrivileges + } + + return privilegesForTableType(variant) +} + +// Entitlements are per table, not static: the privilege set follows table_type, +// and a privilege the grants API refuses must not be offered as requestable. +// ConductorOne would otherwise advertise requests that always fail. +func (b *tableBuilder) Entitlements(ctx context.Context, resource *v2.Resource, _ rs.SyncOpAttrs) ([]*v2.Entitlement, *rs.SyncOpResults, error) { + variant, _ := rs.GetProfileStringValue(rs.GetProfile(resource), profileKeyTableType) + grantable := ent.WithGrantableTo(userResourceType, groupResourceType, servicePrincipalResourceType) + + privileges := privilegesForTable(ctx, resource) + rv := make([]*v2.Entitlement, 0, len(privileges)+1) + for _, privilege := range privileges { + options := []ent.EntitlementOption{ + ent.WithDisplayName(fmt.Sprintf("%s %s", resource.GetDisplayName(), privilege)), + ent.WithDescription(fmt.Sprintf("%s privilege on table %s in Databricks", privilege, resource.GetDisplayName())), + } + // Databricks rejects adding these three, so they are reported where a + // principal already holds them and are never requestable. + reportOnly := (privilege == privilegeAllPrivileges && isViewLike(variant)) || + (privilege == privilegeRefresh && !refreshIsRequestable(variant)) || + (privilege == "MODIFY" && strings.EqualFold(strings.TrimSpace(variant), tableTypeStreamingTable)) + if !reportOnly { + options = append([]ent.EntitlementOption{grantable}, options...) + } + rv = append(rv, ent.NewPermissionEntitlement(resource, privilege, options...)) + } + + rv = append(rv, ent.NewOwnershipEntitlement(resource, ownerEntitlement, + ent.WithDisplayName(fmt.Sprintf("%s owner", resource.GetDisplayName())), + ent.WithDescription(fmt.Sprintf("Owns table %s in Databricks", resource.GetDisplayName())), + )) + + return rv, nil, nil +} + +func (b *tableBuilder) Grants(ctx context.Context, resource *v2.Resource, attr rs.SyncOpAttrs) ([]*v2.Grant, *rs.SyncOpResults, error) { + return b.withLevel(tableLevel(privilegesForTable(ctx, resource))).syncGrants(ctx, resource, attr) +} + +func (b *tableBuilder) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) (annotations.Annotations, error) { + resource := entitlement.GetResource() + if err := refuseUngrantableTablePrivilege(resource, entitlement); err != nil { + return nil, err + } + + return b.withLevel(tableLevel(privilegesForTable(ctx, resource))).grant(ctx, principal.GetId(), entitlement) +} + +// refuseUngrantableTablePrivilege rejects the two privileges a table reports but +// the grants API refuses to add, before any PATCH goes out. Revoke is not held to +// this: a privilege SQL granted still has to be removable. +func refuseUngrantableTablePrivilege(resource *v2.Resource, entitlement *v2.Entitlement) error { + privilege, err := parseEntitlementPrivilege(entitlement.GetId()) + if err != nil { + return err + } + + variant, _ := rs.GetProfileStringValue(rs.GetProfile(resource), profileKeyTableType) + normalized := strings.ToUpper(strings.TrimSpace(variant)) + switch { + case privilege == privilegeAllPrivileges && isViewLike(variant): + return status.Errorf(codes.InvalidArgument, + "databricks-connector: the grants API rejects ALL_PRIVILEGES on a %s; grant SELECT or MANAGE instead", normalized) + case privilege == privilegeRefresh && !refreshIsRequestable(variant): + return status.Errorf(codes.InvalidArgument, + "databricks-connector: REFRESH can be granted on a materialized view or a streaming table, not on table type %q", normalized) + case privilege == "MODIFY" && normalized == tableTypeStreamingTable: + return status.Errorf(codes.InvalidArgument, + "databricks-connector: MODIFY on a streaming table is reported when already held; the grants API reference does not list it as grantable") + } + + return nil +} + +func (b *tableBuilder) Revoke(ctx context.Context, revoked *v2.Grant) (annotations.Annotations, error) { + resource := revoked.GetEntitlement().GetResource() + + return b.withLevel(tableLevel(privilegesForTable(ctx, resource))).revoke(ctx, revoked.GetPrincipal().GetId(), revoked.GetEntitlement()) +} diff --git a/pkg/connector/volume.go b/pkg/connector/volume.go new file mode 100644 index 00000000..c037218f --- /dev/null +++ b/pkg/connector/volume.go @@ -0,0 +1,208 @@ +package connector + +import ( + "context" + "fmt" + "strings" + + "github.com/conductorone/baton-databricks/pkg/databricks" + v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" + "github.com/conductorone/baton-sdk/pkg/annotations" + "github.com/conductorone/baton-sdk/pkg/connectorbuilder" + ent "github.com/conductorone/baton-sdk/pkg/types/entitlement" + rs "github.com/conductorone/baton-sdk/pkg/types/resource" +) + +var _ connectorbuilder.StaticEntitlementSyncerV2 = (*volumeBuilder)(nil) + +const ( + securableTypeVolume = "VOLUME" + + // errorCodeGenericGone is Databricks' generic 404 code, which a missing + // volume and a workspace that cannot serve one both answer with. + errorCodeGenericGone = "RESOURCE_DOES_NOT_EXIST" + + profileKeyVolumeName = "volume_name" + profileKeyVolumeType = "volume_type" + + // volumeWriteRemedy is what a 403 on the PATCH tells the operator. + volumeWriteRemedy = "the connector must own the metastore, the parent catalog, the parent schema or the volume, " + + "or hold MANAGE on the volume, the parent schema or the parent catalog" + + // volumeTrustRemedy is what a refusal to report a partial privilege list + // tells the operator. + volumeTrustRemedy = "the connector's service principal must own the metastore, the parent catalog, the parent schema or the volume, " + + "hold MANAGE on the volume, the parent schema or the parent catalog, or be assigned Admin directly on the workspace whose " + + "admins group owns the parent catalog" +) + +// volumePrivileges is the set a volume takes: its files are read and written +// rather than queried, so none of the table DML applies. +// https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/privileges +var volumePrivileges = []string{ + privilegeAllPrivileges, + "APPLY_TAG", + privilegeManage, + "READ_METADATA", + "READ_VOLUME", + "WRITE_VOLUME", +} + +var volumeLevel = securableLevel{ + securableType: databricks.SecurableVolume, + noun: "volume", + privileges: volumePrivileges, + writeRemedy: volumeWriteRemedy, + trustRemedy: volumeTrustRemedy, + permissionsName: volumePermissionsName, + trusts: volumeGrantsAreTrustworthy, + // Dropping a parent drops the volume with it, and both parent codes name + // what is missing. The generic code does not, so it is confirmed first. + goneErrorCodes: []string{errorCodeSchemaGone, errorCodeCatalogGone}, + ambiguousGoneCodes: []string{errorCodeGenericGone}, + confirmGone: volumeIsGone, +} + +// volumeIsGone reads the volume itself after the permissions endpoint answered +// the generic 404. The volume endpoint answers 200 while the volume exists, so +// anything but a gone code there means the permissions read failed for another +// reason and the absence stays unproven. +func volumeIsGone( + ctx context.Context, + client *databricks.Client, + fullName, workspace string, + annos *annotations.Annotations, +) (bool, error) { + volume, rateLimit, err := client.GetVolume(ctx, workspace, fullName) + noteRateLimit(annos, rateLimit) + if err == nil { + return volume == nil, nil + } + if databricks.IsSecurableGone(err, errorCodeGenericGone, errorCodeSchemaGone, errorCodeCatalogGone) { + return true, nil + } + + return false, err +} + +type volumeBuilder struct { + securableOps + workspaces map[string]struct{} +} + +func newVolumeBuilder(client *databricks.Client, clientID string, workspaces map[string]struct{}, sync syncPrincipals) *volumeBuilder { + return &volumeBuilder{ + securableOps: securableOps{client: client, clientID: clientID, sync: sync, level: volumeLevel}, + workspaces: workspaces, + } +} + +func (b *volumeBuilder) ResourceType(_ context.Context) *v2.ResourceType { + return volumeResourceType +} + +// volumePermissionsName: the permissions endpoint addresses a volume by its +// dotted full name. The metastore is carried by the host. +func volumePermissionsName(resourceID string) (string, error) { + _, catalogName, schemaName, name, err := parseSchemaChildID(resourceID, "volume") + if err != nil { + return "", err + } + + return strings.Join([]string{catalogName, schemaName, name}, schemaNameSeparator), nil +} + +func volumeGrantsAreTrustworthy( + ctx context.Context, + client *databricks.Client, + trust securableTrust, + annos *annotations.Annotations, +) (bool, error) { + return schemaChildGrantsAreTrustworthy(ctx, client, trust, databricks.SecurableVolume, annos) +} + +func volumeResource( + volume *databricks.Volume, + metastoreID, catalogName, schemaName string, + parent *v2.ResourceId, + route schemaChildRoute, +) (*v2.Resource, error) { + fullName := strings.Join([]string{catalogName, schemaName, volume.Name}, schemaNameSeparator) + profile := schemaChildProfile(metastoreID, catalogName, schemaName, fullName, volume.Owner, securableTypeVolume, route) + profile[profileKeyVolumeName] = volume.Name + profile[profileKeyVolumeType] = volume.VolumeType + + resourceID := schemaChildResourceID(metastoreID, catalogName, schemaName, volume.Name) + + // The display name is the full name rather than the leaf: main.sales.files + // and main.finance.files would otherwise both read "files" in C1. + return rs.NewResource( + fullName, + volumeResourceType, + resourceID, + rs.WithResourceProfile(profile), + rs.WithParentResourceID(parent), + rs.WithAnnotation(&v2.RawId{Id: resourceID}), + ) +} + +// List pages the volumes of one schema. +func (b *volumeBuilder) List(ctx context.Context, parentResourceID *v2.ResourceId, attr rs.SyncOpAttrs) ([]*v2.Resource, *rs.SyncOpResults, error) { + return listSchemaChildren(ctx, b.client, b.clientID, b.workspaces, volumeResourceType, "volume", parentResourceID, attr, + func(ctx context.Context, route schemaChildRoute, metastoreID, catalogName, schemaName, pageToken string) ( + []*v2.Resource, string, *v2.RateLimitDescription, error) { + volumes, next, rateLimit, err := b.client.ListVolumes(ctx, route.Workspace, catalogName, schemaName, pageToken, ResourcesPageSize) + if err != nil { + return nil, "", rateLimit, err + } + + var rv []*v2.Resource + for _, volume := range volumes { + if volume.Name == "" { + continue + } + + built, err := volumeResource(&volume, metastoreID, catalogName, schemaName, parentResourceID, route) + if err != nil { + return nil, "", rateLimit, fmt.Errorf( + "databricks-connector: failed to build the resource for volume %s.%s.%s: %w", catalogName, schemaName, volume.Name, err) + } + rv = append(rv, built) + } + + return rv, next, rateLimit, nil + }) +} + +func (b *volumeBuilder) StaticEntitlements(_ context.Context, _ rs.SyncOpAttrs) ([]*v2.Entitlement, *rs.SyncOpResults, error) { + grantable := ent.WithGrantableTo(userResourceType, groupResourceType, servicePrincipalResourceType) + rv := make([]*v2.Entitlement, 0, len(volumePrivileges)+1) + for _, privilege := range volumePrivileges { + rv = append(rv, ent.NewPermissionEntitlement(nil, privilege, + grantable, + ent.WithDescription(fmt.Sprintf("%s privilege on a volume in Databricks", privilege)), + )) + } + + rv = append(rv, ent.NewOwnershipEntitlement(nil, ownerEntitlement, + ent.WithDescription("Owns a volume in Databricks"), + )) + + return rv, nil, nil +} + +func (b *volumeBuilder) Entitlements(context.Context, *v2.Resource, rs.SyncOpAttrs) ([]*v2.Entitlement, *rs.SyncOpResults, error) { + return nil, nil, nil +} + +func (b *volumeBuilder) Grants(ctx context.Context, resource *v2.Resource, attr rs.SyncOpAttrs) ([]*v2.Grant, *rs.SyncOpResults, error) { + return b.syncGrants(ctx, resource, attr) +} + +func (b *volumeBuilder) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) (annotations.Annotations, error) { + return b.grant(ctx, principal.GetId(), entitlement) +} + +func (b *volumeBuilder) Revoke(ctx context.Context, revoked *v2.Grant) (annotations.Annotations, error) { + return b.revoke(ctx, revoked.GetPrincipal().GetId(), revoked.GetEntitlement()) +} diff --git a/pkg/databricks/models.go b/pkg/databricks/models.go index c139c5a6..658e7c1c 100644 --- a/pkg/databricks/models.go +++ b/pkg/databricks/models.go @@ -193,6 +193,18 @@ type Schema struct { FullName string `json:"full_name"` } +type Table struct { + Name string `json:"name"` + Owner string `json:"owner"` + TableType string `json:"table_type"` +} + +type Volume struct { + Name string `json:"name"` + Owner string `json:"owner"` + VolumeType string `json:"volume_type"` +} + type PrivilegeAssignment struct { Principal string `json:"principal"` PrincipalID json.Number `json:"principal_id"` @@ -239,6 +251,16 @@ type schemasResponse struct { NextPageToken string `json:"next_page_token"` } +type tablesResponse struct { + Tables []Table `json:"tables"` + NextPageToken string `json:"next_page_token"` +} + +type volumesResponse struct { + Volumes []Volume `json:"volumes"` + NextPageToken string `json:"next_page_token"` +} + type permissionsResponse struct { PrivilegeAssignments []PrivilegeAssignment `json:"privilege_assignments"` NextPageToken string `json:"next_page_token"` diff --git a/pkg/databricks/unity_catalog.go b/pkg/databricks/unity_catalog.go index cbe7b831..5c069310 100644 --- a/pkg/databricks/unity_catalog.go +++ b/pkg/databricks/unity_catalog.go @@ -26,6 +26,12 @@ const ( // https://docs.databricks.com/api/workspace/schemas/list unityCatalogSchemasEndpoint = "/api/2.1/unity-catalog/schemas" + // https://docs.databricks.com/api/workspace/tables/list + unityCatalogTablesEndpoint = "/api/2.1/unity-catalog/tables" + + // https://docs.databricks.com/api/workspace/volumes/list + unityCatalogVolumesEndpoint = "/api/2.1/unity-catalog/volumes" + // GET https://docs.databricks.com/api/workspace/grants/get // PATCH https://docs.databricks.com/api/workspace/grants/update unityCatalogPermissionsEndpoint = "/api/2.1/unity-catalog/permissions" @@ -41,10 +47,15 @@ const ( SecurableMetastore = "metastore" SecurableCatalog = "catalog" SecurableSchema = "schema" + SecurableTable = "table" + SecurableVolume = "volume" // The permissions endpoint rejects any max_results from 1 to 149 with a 400. // Zero selects the paginated response that replaces the unpaginated form. permissionsMaxResults uint = 0 + + // The tables endpoint rejects a max_results above this ceiling. + unityCatalogTablesMaxResults uint = 50 ) // ListMetastores (GET /api/2.0/accounts/{account_id}/metastores). Not paginated. @@ -124,6 +135,32 @@ func (c *Client) ListCatalogs( return res.Catalogs, res.NextPageToken, ratelimitData, nil } +// GetVolume (GET /api/2.1/unity-catalog/volumes/{full_name}) reads one volume +// through workspaceId, which is a deployment name. The permissions endpoint +// answers a missing volume with the generic RESOURCE_DOES_NOT_EXIST, which an +// unreachable workspace also answers, so this read is what separates the two: +// measured on a live tenant it returns 200 for a volume that exists and 404 for +// one that does not, and it names SCHEMA_DOES_NOT_EXIST when the parent is what +// is missing. +func (c *Client) GetVolume( + ctx context.Context, + workspaceId, fullName string, +) ( + *Volume, + *v2.RateLimitDescription, + error, +) { + var res Volume + + u := c.workspaceUrl(workspaceId).JoinPath(unityCatalogVolumesEndpoint, fullName) + ratelimitData, err := c.Get(ctx, u, &res) + if err != nil { + return nil, ratelimitData, nameWorkspace403Remedy(workspaceId, fmt.Errorf("failed to read volume %s: %w", fullName, err)) + } + + return &res, ratelimitData, nil +} + // GetCatalog (GET /api/2.1/unity-catalog/catalogs/{name}) reads one catalog // through workspaceId, which is a deployment name. It answers what draining // ListCatalogs cannot: a 404 naming CATALOG_DOES_NOT_EXIST is the catalog not @@ -227,6 +264,71 @@ func (c *Client) GetSchema( return &res, ratelimitData, nil } +// ListTables (GET /api/2.1/unity-catalog/tables?catalog_name=&schema_name=) pages +// the tables, views and materialized views in a schema. schema_name is mandatory, +// and a max_results above the endpoint's ceiling of 50 is clamped rather than sent. +func (c *Client) ListTables( + ctx context.Context, + workspaceId, catalogName, schemaName, pageToken string, + maxResults uint, +) ( + []Table, + string, + *v2.RateLimitDescription, + error, +) { + if maxResults > unityCatalogTablesMaxResults { + maxResults = unityCatalogTablesMaxResults + } + + var res tablesResponse + + u := c.workspaceUrl(workspaceId).JoinPath(unityCatalogTablesEndpoint) + ratelimitData, err := c.Get(ctx, u, &res, &unityCatalogVars{ + catalogName: catalogName, + schemaName: schemaName, + maxResults: &maxResults, + pageToken: pageToken, + // Without this the listing carries every column and property of every + // table, which is the bulk of the response and none of it is read here. + omitTablePayload: true, + }) + if err != nil { + return nil, "", ratelimitData, nameWorkspace403Remedy(workspaceId, + fmt.Errorf("failed to list tables in schema %s.%s: %w", catalogName, schemaName, err)) + } + + return res.Tables, res.NextPageToken, ratelimitData, nil +} + +// ListVolumes (GET /api/2.1/unity-catalog/volumes?catalog_name=&schema_name=) pages a schema's volumes. +func (c *Client) ListVolumes( + ctx context.Context, + workspaceId, catalogName, schemaName, pageToken string, + maxResults uint, +) ( + []Volume, + string, + *v2.RateLimitDescription, + error, +) { + var res volumesResponse + + u := c.workspaceUrl(workspaceId).JoinPath(unityCatalogVolumesEndpoint) + ratelimitData, err := c.Get(ctx, u, &res, &unityCatalogVars{ + catalogName: catalogName, + schemaName: schemaName, + maxResults: &maxResults, + pageToken: pageToken, + }) + if err != nil { + return nil, "", ratelimitData, nameWorkspace403Remedy(workspaceId, + fmt.Errorf("failed to list volumes in schema %s.%s: %w", catalogName, schemaName, err)) + } + + return res.Volumes, res.NextPageToken, ratelimitData, nil +} + // ListPermissions (GET /api/2.1/unity-catalog/permissions/{securable_type}/{full_name}). // The principal query filters effectively, not literally: an inherited assignment // comes back naming the group it is inherited through, so a caller must read every diff --git a/pkg/databricks/unity_catalog_test.go b/pkg/databricks/unity_catalog_test.go index 856771a3..cad0882e 100644 --- a/pkg/databricks/unity_catalog_test.go +++ b/pkg/databricks/unity_catalog_test.go @@ -6,6 +6,9 @@ import ( "io" "net/http" "net/http/httptest" + "net/url" + "reflect" + "strconv" "testing" v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" @@ -95,6 +98,43 @@ func TestUpdatePermissionsDrainsFromPatchToken(t *testing.T) { } } +// omit_username is deliberately absent: it also drops owner, and the drop is +// silent — the response still parses and every owner simply becomes empty. The +// exact-set assertion is what makes adding it fail here. +func TestListTablesSendsOnlyTheOmitColumnsAndPropertiesPayloadVars(t *testing.T) { + t.Setenv("BATON_HTTP_CACHE_TTL", "0") + + var query url.Values + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + query = r.URL.Query() + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(tablesResponse{Tables: []Table{{Name: "t"}}}) + })) + t.Cleanup(srv.Close) + + client := srv.Client() + client.Transport = rewriteHost(srv.Listener.Addr().String(), client.Transport) + c, err := NewClient(context.Background(), client, "example.cloud.databricks.com", "accounts.cloud.databricks.com", "acc-1", srv.URL, &NoAuth{}, nil) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + + if _, _, _, err := c.ListTables(context.Background(), "ws", "main", "default", "", unityCatalogTablesMaxResults); err != nil { + t.Fatalf("ListTables: %v", err) + } + + want := url.Values{ + "catalog_name": []string{"main"}, + "schema_name": []string{"default"}, + "max_results": []string{strconv.FormatUint(uint64(unityCatalogTablesMaxResults), 10)}, + "omit_columns": []string{"true"}, + "omit_properties": []string{"true"}, + } + if !reflect.DeepEqual(query, want) { + t.Fatalf("query = %v, want exactly %v", query, want) + } +} + func TestListCatalogsCanIncludeUnboundCatalogs(t *testing.T) { t.Setenv("BATON_HTTP_CACHE_TTL", "0") var includeUnbound string diff --git a/pkg/databricks/vars.go b/pkg/databricks/vars.go index 6fac93ad..04634dcd 100644 --- a/pkg/databricks/vars.go +++ b/pkg/databricks/vars.go @@ -174,10 +174,15 @@ func NewNameVars(name string, etag string) *NameVars { // maxResults is a pointer: its presence is the signal. type unityCatalogVars struct { catalogName string + schemaName string principal string maxResults *uint pageToken string includeUnbound bool + + // omit_username is deliberately not sent alongside these: it also removes + // owner, which every securable's profile and its owner grant depend on. + omitTablePayload bool } func (v *unityCatalogVars) Apply(params *url.Values) { @@ -185,6 +190,10 @@ func (v *unityCatalogVars) Apply(params *url.Values) { params.Add("catalog_name", v.catalogName) } + if v.schemaName != "" { + params.Add("schema_name", v.schemaName) + } + if v.principal != "" { params.Add("principal", v.principal) } @@ -199,4 +208,9 @@ func (v *unityCatalogVars) Apply(params *url.Values) { if v.includeUnbound { params.Set("include_unbound", "true") } + + if v.omitTablePayload { + params.Add("omit_columns", "true") + params.Add("omit_properties", "true") + } } From fe71e0ca019dd6f593ae90c981050aca533d512a Mon Sep 17 00:00:00 2001 From: Mateo Hernandez Date: Wed, 4 Nov 2026 16:15:02 -0300 Subject: [PATCH 2/9] fix(databricks): read one schema instead of every schema of the catalog Listing a schema's tables or volumes resolved its route on the first page, once per child type, and schemaThrough found the schema by paging the whole catalog's schema listing. That is one full listing walk per schema per child type: a catalog with 1,000 schemas spends about 21,000 listing requests, and 43,000 requests in all, before the first table is listed. The uhttp GET cache hid it while a catalog's listing fit in the 5 MB it holds by default, which a catalog of 30 KB schema rows does not. schemaThrough now reads the one schema it needs. The same catalog costs 2,000 schema reads instead, and the cost is linear in the schema count rather than quadratic. Error classification is unchanged. A 404 naming SCHEMA_DOES_NOT_EXIST is read exactly like a schema absent from the listing was: proof of deletion only for a caller that sees the whole catalog, since Databricks answers the same way for a schema the caller merely cannot see. Every other refusal, including a 404 carrying no code, still leaves the schema unknown rather than absent -- read as an empty result the SDK would skip the children and ConductorOne would delete every table and volume under the schema. Co-Authored-By: Claude Opus 5 --- pkg/connector/schema.go | 49 ++++--- pkg/connector/schema_child.go | 7 +- pkg/connector/schema_child_test.go | 192 +++++++++++++++++++++++++++ pkg/databricks/unity_catalog.go | 42 ++++++ pkg/databricks/unity_catalog_test.go | 57 ++++++++ 5 files changed, 320 insertions(+), 27 deletions(-) create mode 100644 pkg/connector/schema_child_test.go diff --git a/pkg/connector/schema.go b/pkg/connector/schema.go index 46dfeb24..524655ad 100644 --- a/pkg/connector/schema.go +++ b/pkg/connector/schema.go @@ -325,39 +325,36 @@ func (b *schemaBuilder) pushRoute( return nil } -// schemaThrough finds one schema in the listing of workspace, nil when that -// workspace does not list it. Its caller has already proven the listing holds -// every schema in the catalog, so an absence here is the schema being gone. +// errSchemaGone marks a schema Databricks named as missing. +var errSchemaGone = errors.New("schema no longer exists") + +// schemaThrough reads one schema through workspace. It reports errSchemaGone +// when Databricks names the schema missing, which proves nothing on its own: a +// caller without catalog-wide visibility is answered the same way for a schema +// it merely cannot see, so the caller decides what the absence means. Every +// other refusal, a 404 carrying no code included, leaves the schema unknown +// rather than absent: read as an empty result the SDK would skip the children +// and ConductorOne would delete every table and volume under it. func schemaThrough( ctx context.Context, client *databricks.Client, workspace, catalogName, schemaName string, annos *annotations.Annotations, ) (*databricks.Schema, error) { - pageToken := "" - for { - schemas, next, rateLimit, err := client.ListSchemas(ctx, workspace, catalogName, pageToken, ResourcesPageSize) - noteRateLimit(annos, rateLimit) - if err != nil { - return nil, schemaListingError(catalogName, workspace, err) - } - - for _, schema := range schemas { - if schema.Name == schemaName { - return &schema, nil - } - } - - if next == "" { - return nil, nil - } - if next == pageToken { - return nil, status.Errorf(codes.Internal, - "databricks-connector: the schemas of catalog %s repeat page token %q through workspace %s", - catalogName, next, workspace) - } - pageToken = next + schema, rateLimit, err := client.GetSchema(ctx, workspace, catalogName+schemaNameSeparator+schemaName) + noteRateLimit(annos, rateLimit) + switch { + case err == nil: + case databricks.IsSecurableGone(err, errorCodeSchemaGone): + return nil, errSchemaGone + case status.Code(err) == codes.InvalidArgument: + // The answer does not describe the schema asked for. + return nil, err + default: + return nil, schemaListingError(catalogName, workspace, err) } + + return schema, nil } // schemaListingError returns NotFound only when Databricks names the missing diff --git a/pkg/connector/schema_child.go b/pkg/connector/schema_child.go index 8ecfecfa..cc386149 100644 --- a/pkg/connector/schema_child.go +++ b/pkg/connector/schema_child.go @@ -192,11 +192,16 @@ func pushSchemaChildRoute( } schema, err := schemaThrough(ctx, client, route.Workspace, catalogName, schemaName, annos) + // A schema named as missing is read like one absent from the listing: + // proof of deletion only for a caller that sees the whole catalog. + if errors.Is(err, errSchemaGone) { + schema, err = nil, nil + } if err != nil { return err } if schemasComplete && schema == nil { - ctxzap.Extract(ctx).Debug("databricks-connector: the schema is no longer listed; listing none of its children", + ctxzap.Extract(ctx).Debug("databricks-connector: the schema is no longer there; listing none of its children", zap.String("resource_type", resourceType.Id), zap.String("schema", fullName), zap.String("workspace", route.Workspace)) diff --git a/pkg/connector/schema_child_test.go b/pkg/connector/schema_child_test.go new file mode 100644 index 00000000..613a8d48 --- /dev/null +++ b/pkg/connector/schema_child_test.go @@ -0,0 +1,192 @@ +package connector + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "slices" + "testing" + + v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" + "github.com/conductorone/baton-sdk/pkg/pagination" + rs "github.com/conductorone/baton-sdk/pkg/types/resource" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +// serveSchema answers GET /schemas/{full_name}. +func (f *fakeSchemaAccount) serveSchema(w http.ResponseWriter, r *http.Request) { + workspace := r.Header.Get("X-Test-Workspace") + f.schemaReads = append(f.schemaReads, workspace) + if code, ok := f.schemaGetStatus[workspace]; ok { + body := `{"message":"refused"}` + if f.schemaGetErrorCode != "" { + body = fmt.Sprintf(`{"error_code":%q,"message":"refused"}`, f.schemaGetErrorCode) + } + http.Error(w, body, code) + + return + } + + owner := "someone" + if len(f.schemas) > 0 { + owner, _ = f.schemas[0]["owner"].(string) + } + _ = json.NewEncoder(w).Encode(map[string]any{"name": "sales", "full_name": "main.sales", "owner": owner}) +} + +func (f *fakeSchemaAccount) serveSchemaChildren(w http.ResponseWriter, r *http.Request, key string, rows []map[string]any) { + f.childReads = append(f.childReads, key) + if got := r.URL.Query().Get("schema_name"); got == "" { + http.Error(w, `{"message":"schema_name is required"}`, http.StatusBadRequest) + + return + } + _ = json.NewEncoder(w).Encode(map[string]any{key: rows}) +} + +var testSchemaResourceID = &v2.ResourceId{ResourceType: schemaResourceType.Id, Resource: "ms-1::main.sales"} + +func newTestChildBuildersAs(t *testing.T, f *fakeSchemaAccount, clientID string) (*tableBuilder, *volumeBuilder) { + t.Helper() + t.Setenv("BATON_HTTP_CACHE_TTL", "0") + if f.metastoreOwner == "" { + f.metastoreOwner = testOwnPrincipal + } + if f.assignments == nil { + f.assignments = map[string][]map[string]any{} + } + client, _ := newWorkspaceRoutedClient(t, f.handler(t)) + + return newTableBuilder(client, clientID, nil, syncPrincipals{groups: true}), + newVolumeBuilder(client, clientID, nil, syncPrincipals{groups: true}) +} + +func listAllChildren(t *testing.T, + list func(context.Context, *v2.ResourceId, rs.SyncOpAttrs) ([]*v2.Resource, *rs.SyncOpResults, error), +) ([]*v2.Resource, error) { + t.Helper() + + var all []*v2.Resource + token := "" + for range 1000 { + resources, results, err := list(context.Background(), testSchemaResourceID, rs.SyncOpAttrs{PageToken: pagination.Token{Token: token}}) + if err != nil { + return all, err + } + all = append(all, resources...) + if results == nil || results.NextPageToken == "" { + return all, nil + } + token = results.NextPageToken + } + t.Fatal("List never returned an empty page token") + + return nil, nil +} + +// A schema's children are listed after reading that one schema, not after +// walking the catalog's whole schema listing: the walk runs once per schema and +// again per child type, which is quadratic in the catalog's schema count. +func TestSchemaChildListReadsOneSchemaInsteadOfDrainingTheCatalog(t *testing.T) { + // sales sits last, so walking the listing pays for every page of the catalog. + manySchemas := make([]map[string]any, 0, 300) + for i := range 299 { + manySchemas = append(manySchemas, map[string]any{"name": fmt.Sprintf("s%03d", i), "owner": "eng"}) + } + manySchemas = append(manySchemas, map[string]any{"name": "sales", "owner": "eng"}) + + f := &fakeSchemaAccount{ + metastoreOwner: "eng", + catalogOwner: "eng", + schemas: manySchemas, + tables: []map[string]any{{"name": "orders", "owner": "eng", "table_type": "MANAGED"}}, + volumes: []map[string]any{{"name": "files", "owner": "eng", "volume_type": "MANAGED"}}, + } + tables, volumes := newTestChildBuildersAs(t, f, "eng") + + listedTables, err := listAllChildren(t, tables.List) + if err != nil { + t.Fatalf("table List: %v", err) + } + listedVolumes, err := listAllChildren(t, volumes.List) + if err != nil { + t.Fatalf("volume List: %v", err) + } + + if len(listedTables) != 1 || len(listedVolumes) != 1 { + t.Fatalf("listed %d table(s) and %d volume(s), want 1 of each", len(listedTables), len(listedVolumes)) + } + if f.schemaPages != 0 { + t.Errorf("the schemas listing was paged %d time(s); listing a schema's children must not walk the catalog's schemas", f.schemaPages) + } + if want := []string{"wsa", "wsa"}; !slices.Equal(f.schemaReads, want) { + t.Errorf("single-schema reads = %v, want %v: one per child type", f.schemaReads, want) + } +} + +// A schema named as missing is proof of deletion only for a caller that sees the +// whole catalog. Reading the other case as "no children" would make ConductorOne +// delete every table and volume under it. +func TestSchemaChildListTellsADeletedSchemaFromAnInvisibleOne(t *testing.T) { + tests := []struct { + name string + clientID string + catalogOwner string + errorCode string + wantErr codes.Code + }{ + { + name: "the connector owns the catalog, so the schema is gone", + clientID: "eng", catalogOwner: "eng", errorCode: errorCodeSchemaGone, + }, + { + name: "the connector cannot see the whole catalog, so the sync fails", + clientID: "app-9", catalogOwner: "someone", errorCode: errorCodeSchemaGone, + wantErr: codes.PermissionDenied, + }, + { + // A 404 Databricks does not put a code on says nothing about the + // schema. Read as an empty result it would delete every table and + // volume under it, so the sync fails instead. + name: "a 404 naming nothing is not proof the schema is gone", + clientID: "eng", catalogOwner: "eng", + wantErr: codes.FailedPrecondition, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + f := &fakeSchemaAccount{ + metastoreOwner: "someone", + catalogOwner: test.catalogOwner, + schemas: []map[string]any{{"name": "sales", "owner": "someone"}}, + schemaGetStatus: map[string]int{"wsa": http.StatusNotFound}, + schemaGetErrorCode: test.errorCode, + } + tables, _ := newTestChildBuildersAs(t, f, test.clientID) + + listed, err := listAllChildren(t, tables.List) + if test.wantErr != codes.OK { + if status.Code(err) != test.wantErr { + t.Fatalf("List error = %v, want %s", err, test.wantErr) + } + + return + } + if err != nil { + t.Fatalf("List: %v", err) + } + if len(listed) != 0 { + t.Errorf("listed %d table(s), want none under a schema that is gone", len(listed)) + } + // The empty result must come from the schema read proving the + // absence, not from an earlier silent exit. + if len(f.schemaReads) == 0 || len(f.childReads) != 0 { + t.Errorf("schema reads = %v, child listings = %v: want the empty result to come from the schema read", + f.schemaReads, f.childReads) + } + }) + } +} diff --git a/pkg/databricks/unity_catalog.go b/pkg/databricks/unity_catalog.go index 5c069310..0915b93f 100644 --- a/pkg/databricks/unity_catalog.go +++ b/pkg/databricks/unity_catalog.go @@ -200,6 +200,48 @@ func (c *Client) GetCatalog( return &res, ratelimitData, nil } +// GetSchema (GET /api/2.1/unity-catalog/schemas/{full_name}) reads one schema +// through workspaceId, which is a deployment name. full_name is "{catalog}.{schema}". +// It separates the two answers draining ListSchemas runs together: a 404 naming +// SCHEMA_DOES_NOT_EXIST is Databricks naming the schema missing, which a caller +// that merely cannot see it is answered with too, and any other refusal is this +// workspace not serving it. Which of the two a named 404 is belongs to the +// caller. The name is checked against the assembled URL for the same reason +// permissionsUrl checks it. +func (c *Client) GetSchema( + ctx context.Context, + workspaceId, fullName string, +) ( + *Schema, + *v2.RateLimitDescription, + error, +) { + var res Schema + + if fullName == "" { + return nil, nil, invalidArgument("schema full name is required") + } + + u := c.workspaceUrl(workspaceId).JoinPath(unityCatalogSchemasEndpoint, fullName) + if !addressesSecurable(u, "/"+fullName) { + return nil, nil, invalidArgument("cannot address schema %q: the name does not survive URL assembly", fullName) + } + + ratelimitData, err := c.Get(ctx, u, &res) + if err != nil { + return nil, ratelimitData, nameWorkspace403Remedy(workspaceId, fmt.Errorf("failed to read schema %s: %w", fullName, err)) + } + + // An answer about another schema, or one that names no schema at all, does not + // describe the one asked for, and its owner is what decides whether the + // children listing is complete. + if res.FullName != fullName { + return nil, ratelimitData, invalidArgument("asked Databricks for schema %q and it answered with %q", fullName, res.FullName) + } + + return &res, ratelimitData, nil +} + // ListSchemas (GET /api/2.1/unity-catalog/schemas?catalog_name=) reads one page of // the schemas of catalogName, through workspaceId, which is a deployment name. // Unlike the catalogs listing, this one pages, so the cursor is passed through. diff --git a/pkg/databricks/unity_catalog_test.go b/pkg/databricks/unity_catalog_test.go index cad0882e..a0783b18 100644 --- a/pkg/databricks/unity_catalog_test.go +++ b/pkg/databricks/unity_catalog_test.go @@ -220,6 +220,63 @@ func TestGetCatalogRefusesAMismatchedAnswer(t *testing.T) { } } +// A schema read addresses one securable by its dotted full name. The same URL +// check GetCatalog makes applies: a name holding "?", "#" or "%" would address +// a different schema, and its owner is what decides whether the connector can +// list every table and volume under it. +func TestGetSchemaRefusesANameThatChangesTheURLOrAMismatchedAnswer(t *testing.T) { + t.Setenv("BATON_HTTP_CACHE_TTL", "0") + + var requested []string + var answer any = Schema{Name: "other", FullName: "main.other", Owner: "eng"} + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requested = append(requested, r.URL.Path) + _ = json.NewEncoder(w).Encode(answer) + })) + t.Cleanup(srv.Close) + + client := srv.Client() + client.Transport = rewriteHost(srv.Listener.Addr().String(), client.Transport) + c, err := NewClient(context.Background(), client, "example.cloud.databricks.com", "accounts.cloud.databricks.com", "acc-1", srv.URL, &NoAuth{}, nil) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + + for _, fullName := range []string{"main.sales?x", "main.sales#x", "main.sales%x", ""} { + got, _, err := c.GetSchema(context.Background(), "ws", fullName) + if err == nil { + t.Fatalf("GetSchema(%q) returned schema %+v, want an error", fullName, got) + } + if status.Code(err) != codes.InvalidArgument { + t.Fatalf("GetSchema(%q) returned %v, want InvalidArgument", fullName, status.Code(err)) + } + } + if len(requested) != 0 { + t.Fatalf("GetSchema sent %v, want no request at all", requested) + } + + // An answer about another schema does not describe the one asked for. Neither + // does one that names no schema at all: its owner would still be read as this + // schema's, and that owner decides whether the children listing is complete. + for _, body := range []any{ + Schema{Name: "other", FullName: "main.other", Owner: "eng"}, + map[string]any{"name": "sales", "owner": "eng"}, + } { + answer = body + requested = nil + got, _, err := c.GetSchema(context.Background(), "ws", "main.sales") + if err == nil { + t.Fatalf("GetSchema returned schema %+v for answer %+v, want an error", got, body) + } + if status.Code(err) != codes.InvalidArgument { + t.Fatalf("GetSchema returned %v for answer %+v, want InvalidArgument", status.Code(err), body) + } + if want := []string{"/api/2.1/unity-catalog/schemas/main.sales"}; !reflect.DeepEqual(requested, want) { + t.Fatalf("GetSchema requested %v, want %v", requested, want) + } + } +} + type hostRewriter struct { host http.RoundTripper addr string From 77a5a1789061b90fffea9a0c4c33b158dc4980b6 Mon Sep 17 00:00:00 2001 From: Mateo Hernandez Date: Wed, 4 Nov 2026 16:28:27 -0300 Subject: [PATCH 3/9] fix(databricks): walk on when a workspace does not serve the catalog Listing a schema's tables or volumes returned the catalog read's error for every refusal, where the schema syncer walks on to the next workspace. A catalog bound to one workspace of several is answered by the others with a 404, and which of the two answers Databricks gives decides which way this breaks. A 404 carrying no error code reached the SDK as codes.NotFound, which it reads as a warning: the listing finished green with no resources and ConductorOne deleted every table and volume under the schema. A 404 naming CATALOG_DOES_NOT_EXIST raised errCatalogGone, which failed the whole sync. Both now continue the walk, matching the schema syncer. A catalog every workspace names as gone, with none refusing the read, empties the listing: the catalog syncer drops it in the same sync. A refusal still fails the sync and keeps what ConductorOne already has. Co-Authored-By: Claude Opus 5 --- pkg/connector/schema_child.go | 33 +++++++++++++-- pkg/connector/schema_child_test.go | 66 ++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/pkg/connector/schema_child.go b/pkg/connector/schema_child.go index cc386149..f968ccfc 100644 --- a/pkg/connector/schema_child.go +++ b/pkg/connector/schema_child.go @@ -159,11 +159,28 @@ func pushSchemaChildRoute( } fullName := catalogName + schemaNameSeparator + schemaName - var sawCatalog bool + var sawCatalog, sawGone bool + var lastDeny error for i := range usable { workspace := usable[i] catalog, err := catalogThrough(ctx, client, workspace.DeploymentName, metastoreID, catalogName, annos) if err != nil { + // A workspace that does not serve the catalog says nothing about the + // schema below it, so the walk goes on. Returning the error instead + // reaches the SDK as NotFound for a 404 carrying no code, which it + // reads as a warning: the children would be emitted as none and + // ConductorOne would delete them. + if errors.Is(err, errCatalogGone) { + sawGone = true + + continue + } + if deniesTrust(err) { + lastDeny = err + + continue + } + return err } if catalog == nil { @@ -233,8 +250,18 @@ func pushSchemaChildRoute( return nil } if !sawCatalog { - return status.Errorf(codes.FailedPrecondition, - "databricks-connector: no workspace in sync scope lists catalog %s of metastore %s", catalogName, metastoreID) + // Every workspace named the catalog as gone and none refused the read. + // The catalog syncer drops it in the same sync, so this empties what + // ConductorOne is already removing. + if sawGone && lastDeny == nil { + return nil + } + msg := fmt.Sprintf("databricks-connector: no workspace in sync scope lists catalog %s of metastore %s", catalogName, metastoreID) + if lastDeny != nil { + msg = fmt.Sprintf("%s: %s", msg, lastDeny.Error()) + } + + return status.Errorf(codes.FailedPrecondition, "%s", msg) } return status.Errorf(codes.PermissionDenied, diff --git a/pkg/connector/schema_child_test.go b/pkg/connector/schema_child_test.go index 613a8d48..bfcf34a5 100644 --- a/pkg/connector/schema_child_test.go +++ b/pkg/connector/schema_child_test.go @@ -190,3 +190,69 @@ func TestSchemaChildListTellsADeletedSchemaFromAnInvisibleOne(t *testing.T) { }) } } + +// A workspace that does not serve the catalog answers the catalog read with a +// 404 carrying no error code, the same answer a routing or permission miss +// gives. The schema syncer walks on to the next workspace; listing a schema's +// children must do the same. Returning that error instead reaches the SDK as +// codes.NotFound, which it reads as a warning: the listing finishes green with +// no resources and ConductorOne deletes every table and volume under the schema. +func TestSchemaChildListTriesTheNextWorkspaceWhenOneDoesNotServeTheCatalog(t *testing.T) { + for _, errorCode := range []string{"", errorCodeCatalogGone} { + name := "bare 404" + if errorCode != "" { + name = errorCode + } + t.Run(name, func(t *testing.T) { + f := &fakeSchemaAccount{ + second: true, + metastoreOwner: "eng", + catalogOwner: "eng", + catalogStatus: map[string]int{"wsa": http.StatusNotFound}, + catalogErrorCode: errorCode, + schemas: []map[string]any{{"name": "sales", "owner": "eng"}}, + tables: []map[string]any{{"name": "orders", "owner": "eng", "table_type": "MANAGED"}}, + } + tables, _ := newTestChildBuildersAs(t, f, "eng") + + listed, err := listAllChildren(t, tables.List) + if err != nil { + t.Fatalf("List: %v", err) + } + if len(listed) != 1 { + t.Fatalf("listed %d table(s), want 1 through the second workspace", len(listed)) + } + if workspace, _ := rs.GetProfileStringValue(rs.GetProfile(listed[0]), profileKeyWorkspace); workspace != "wsb" { + t.Errorf("profile[%s] = %q, want wsb", profileKeyWorkspace, workspace) + } + }) + } +} + +// Every workspace naming the catalog as gone, with none refusing the read, is +// the catalog being dropped. The catalog syncer removes it in the same sync, so +// the children listing empties what ConductorOne is already removing instead of +// failing the sync. +func TestSchemaChildListIsEmptyWhenEveryWorkspaceSaysTheCatalogIsGone(t *testing.T) { + f := &fakeSchemaAccount{ + second: true, + metastoreOwner: "eng", + catalogOwner: "eng", + catalogStatus: map[string]int{"wsa": http.StatusNotFound, "wsb": http.StatusNotFound}, + catalogErrorCode: errorCodeCatalogGone, + schemas: []map[string]any{{"name": "sales", "owner": "eng"}}, + tables: []map[string]any{{"name": "orders", "owner": "eng", "table_type": "MANAGED"}}, + } + tables, _ := newTestChildBuildersAs(t, f, "eng") + + listed, err := listAllChildren(t, tables.List) + if err != nil { + t.Fatalf("List: %v", err) + } + if len(listed) != 0 { + t.Errorf("listed %d table(s), want none under a catalog that is gone", len(listed)) + } + if want := []string{"wsa", "wsb"}; !slices.Equal(f.catalogReads, want) { + t.Errorf("catalog reads = %v, want %v: both workspaces are asked before the listing empties", f.catalogReads, want) + } +} From ec661b2067e76e887047597d54984984979c1390 Mon Sep 17 00:00:00 2001 From: Mateo Hernandez Date: Thu, 5 Nov 2026 11:59:03 -0300 Subject: [PATCH 4/9] fix(databricks): keep a parent-catalog 404 from emptying a schema's children schemaThrough classified every refusal of the single-schema read with schemaListingError, which maps CATALOG_DOES_NOT_EXIST to codes.NotFound. The SDK reads NotFound from a List as a warning, not a failure: isWarning (sync/syncer.go) returns true for it and syncOneAction (sync/parallel_syncer.go) logs "skipping sync action" and carries on. The listing then completes having emitted nothing and ConductorOne deletes every table and volume under that schema. A parent-catalog 404 is a real answer there: measured on the tenant, GET /schemas/{full} returns CATALOG_DOES_NOT_EXIST once the catalog is dropped. So the read now refuses as unreadable, which reaches C1 as FailedPrecondition and keeps what it already has. Only a code naming the schema itself still reports errSchemaGone, and the caller decides what that absence means. The message drops the privilege remedy: it is the answer to a partial listing, not to an arbitrary refusal, and on a 403 it contradicted the remedy the client layer already appends. schemaReadError is inlined into the branch that uses it, since it classified nothing. Side effect worth naming: a 403 now reaches C1 as PermissionDenied and a 401 as Unauthenticated, where both were FailedPrecondition. Neither is a warning, so neither changes what C1 keeps. Co-Authored-By: Claude Opus 5 --- pkg/connector/schema.go | 16 +++++++------- pkg/connector/schema_child.go | 7 +++--- pkg/connector/schema_child_test.go | 35 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/pkg/connector/schema.go b/pkg/connector/schema.go index 524655ad..62a747e8 100644 --- a/pkg/connector/schema.go +++ b/pkg/connector/schema.go @@ -329,19 +329,17 @@ func (b *schemaBuilder) pushRoute( var errSchemaGone = errors.New("schema no longer exists") // schemaThrough reads one schema through workspace. It reports errSchemaGone -// when Databricks names the schema missing, which proves nothing on its own: a -// caller without catalog-wide visibility is answered the same way for a schema -// it merely cannot see, so the caller decides what the absence means. Every -// other refusal, a 404 carrying no code included, leaves the schema unknown -// rather than absent: read as an empty result the SDK would skip the children -// and ConductorOne would delete every table and volume under it. +// when Databricks names the schema itself missing, which proves nothing on its +// own, so the caller decides what that absence means. Every other refusal leaves +// the schema unknown: read as empty, C1 would delete its tables and volumes. func schemaThrough( ctx context.Context, client *databricks.Client, workspace, catalogName, schemaName string, annos *annotations.Annotations, ) (*databricks.Schema, error) { - schema, rateLimit, err := client.GetSchema(ctx, workspace, catalogName+schemaNameSeparator+schemaName) + fullName := catalogName + schemaNameSeparator + schemaName + schema, rateLimit, err := client.GetSchema(ctx, workspace, fullName) noteRateLimit(annos, rateLimit) switch { case err == nil: @@ -351,7 +349,9 @@ func schemaThrough( // The answer does not describe the schema asked for. return nil, err default: - return nil, schemaListingError(catalogName, workspace, err) + return nil, refuseUnreadable(fmt.Sprintf( + "databricks-connector: schema %s cannot be read through workspace %s, "+ + "so its tables and volumes are unknown", fullName, workspace), err) } return schema, nil diff --git a/pkg/connector/schema_child.go b/pkg/connector/schema_child.go index f968ccfc..04e6df15 100644 --- a/pkg/connector/schema_child.go +++ b/pkg/connector/schema_child.go @@ -120,10 +120,9 @@ const childListRemedy = "the connector's service principal must own the metastor "or hold ALL_PRIVILEGES on the catalog" // pushSchemaChildRoute records a workspace that can list every table or volume -// of the schema. It walks the running in-scope workspaces until one lists the -// catalog and the child listing is complete. An error from a workspace stops -// the walk. An empty result is an error: it would make ConductorOne delete -// the tables and volumes. +// of the schema, walking the running in-scope workspaces until one serves the +// catalog and the child listing is complete. A workspace that does not serve it +// is skipped; an empty result is an error unless every workspace named it gone. func pushSchemaChildRoute( ctx context.Context, client *databricks.Client, diff --git a/pkg/connector/schema_child_test.go b/pkg/connector/schema_child_test.go index bfcf34a5..86196403 100644 --- a/pkg/connector/schema_child_test.go +++ b/pkg/connector/schema_child_test.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "slices" + "strings" "testing" v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" @@ -154,6 +155,14 @@ func TestSchemaChildListTellsADeletedSchemaFromAnInvisibleOne(t *testing.T) { clientID: "eng", catalogOwner: "eng", wantErr: codes.FailedPrecondition, }, + { + // CATALOG_DOES_NOT_EXIST names the parent, not this schema, and + // one workspace is not every workspace, so it does not prove the + // subtree is gone. + name: "a 404 naming the parent catalog is not proof either", + clientID: "eng", catalogOwner: "eng", errorCode: errorCodeCatalogGone, + wantErr: codes.FailedPrecondition, + }, } for _, test := range tests { @@ -256,3 +265,29 @@ func TestSchemaChildListIsEmptyWhenEveryWorkspaceSaysTheCatalogIsGone(t *testing t.Errorf("catalog reads = %v, want %v: both workspaces are asked before the listing empties", f.catalogReads, want) } } + +// Every workspace refusing the catalog read is not the catalog being gone. The +// listing must fail and keep what ConductorOne already has, and the refusal has +// to reach the operator rather than be swallowed into a generic message. +func TestSchemaChildListFailsWhenEveryWorkspaceRefusesTheCatalog(t *testing.T) { + f := &fakeSchemaAccount{ + second: true, + metastoreOwner: "eng", + catalogOwner: "eng", + catalogStatus: map[string]int{"wsa": http.StatusForbidden, "wsb": http.StatusForbidden}, + schemas: []map[string]any{{"name": "sales", "owner": "eng"}}, + tables: []map[string]any{{"name": "orders", "owner": "eng", "table_type": "MANAGED"}}, + } + tables, _ := newTestChildBuildersAs(t, f, "eng") + + listed, err := listAllChildren(t, tables.List) + if status.Code(err) != codes.FailedPrecondition { + t.Fatalf("List error = %v, want FailedPrecondition", err) + } + if len(listed) != 0 { + t.Errorf("listed %d table(s), want none", len(listed)) + } + if !strings.Contains(err.Error(), "403") { + t.Errorf("List error = %v, want the refusal named in the message", err) + } +} From c06ed7325f0ffc874053f318b73d124be4b5ec6e Mon Sep 17 00:00:00 2001 From: Mateo Hernandez Date: Tue, 17 Nov 2026 10:11:21 -0300 Subject: [PATCH 5/9] fix(databricks): bring the table and volume layer up to the shape #72 landed and volume layer behind on the same counts, one level deeper. **Writes were decided from sync-time owners.** Grant and Revoke read the metastore owner, the catalog owner, the schema owner and the securable's own owner off the profile the last sync wrote, and those four are exactly what the trust check short-circuits on. An ownership transfer away from the connector therefore left a write being decided from a privilege list Databricks had already started truncating, and Revoke could report a grant removed that is still held. Both now rebuild the entitlement's table or volume the way the schema does: the metastore owner from a live listing, the catalog from the recorded workspace when this sync still admits it and otherwise from the first running in-scope workspace that serves it, the schema from GET /schemas/{full_name}, and the securable itself from a new GET /tables/{full_name} or from GET /volumes/{full_name}. The whole path bypasses the HTTP cache, because a remembered page would decide the write with the access of an earlier one. A table also reads table_type live, so the write is no longer checked against the variant the last sync happened to record. **A live sighting did not supersede an earlier gone answer.** The walk in pushSchemaChildRoute could empty a schema's children on a gone answer a later workspace had already contradicted. The reads run in order and a catalog cannot be absent before it is present, so a workspace that serves it now clears the flag. A metastore the account no longer lists also stops returning its bare sentinel; the metastore syncer removes the parent in the same sync. **The sync session never reached the shared path.** The children listing and the table and volume Grants now carry it, so the workspace-Admin check behind the trust rule is read once per workspace per sync instead of once per securable. The listing also gained the repeated-page-token guard the schema listing has. **A refused schema read stopped the walk.** Only the catalog read continued to the next workspace, so a 401, 403 or 404 on the schema failed the sync even where another in-scope workspace serves it. Both reads now walk on, and a refusal left standing is still an error rather than an empty listing: emptying one is reserved for a parent every workspace named as gone with none refusing. GetTable and GetVolume check the name Databricks answers with, as GetCatalog and GetSchema do, because the owner they report decides whether a privilege list is complete. GetSchema now refuses an answer naming no schema at all for that same reason, which is what the client test on this branch already asserted. Not re-measured against the tenant yet. Co-Authored-By: Claude Opus 5 --- pkg/connector/schema.go | 19 ++-- pkg/connector/schema_child.go | 179 ++++++++++++++++++++++++++++---- pkg/connector/table.go | 82 ++++++++++++--- pkg/connector/volume.go | 73 ++++++++++++- pkg/databricks/models.go | 6 ++ pkg/databricks/unity_catalog.go | 102 ++++++++++-------- 6 files changed, 367 insertions(+), 94 deletions(-) diff --git a/pkg/connector/schema.go b/pkg/connector/schema.go index 62a747e8..9f336d61 100644 --- a/pkg/connector/schema.go +++ b/pkg/connector/schema.go @@ -520,7 +520,7 @@ func (b *schemaBuilder) entitlementOnLiveRoute( return nil, err } - route, catalog, err := b.liveCatalogRoute(ctx, resource, metastoreID, catalogName, owner, annos) + route, catalog, err := liveCatalogRoute(ctx, b.client, b.clientID, b.workspaces, resource, metastoreID, catalogName, owner, annos) if err != nil { return nil, err } @@ -553,15 +553,18 @@ func (b *schemaBuilder) entitlementOnLiveRoute( // first running in-scope workspace that does. A catalog no workspace serves is // not proof of deletion, so that case is settled against the metastore's // unbound listing. -func (b *schemaBuilder) liveCatalogRoute( +func liveCatalogRoute( ctx context.Context, + client *databricks.Client, + clientID string, + workspaces map[string]struct{}, resource *v2.Resource, metastoreID, catalogName, metastoreOwner string, annos *annotations.Annotations, ) (schemaRoute, *databricks.Catalog, error) { var probe catalogProbe - if route, ok := profileCatalogRoute(b.client, b.workspaces, resource, metastoreOwner); ok { - catalog, err := catalogThrough(ctx, b.client, route.Workspace, metastoreID, catalogName, annos) + if route, ok := profileCatalogRoute(client, workspaces, resource, metastoreOwner); ok { + catalog, err := catalogThrough(ctx, client, route.Workspace, metastoreID, catalogName, annos) switch { case err == nil: case errors.Is(err, errCatalogGone): @@ -578,11 +581,11 @@ func (b *schemaBuilder) liveCatalogRoute( } } - scope, err := scopedWorkspaces(ctx, b.client, b.workspaces, annos) + scope, err := scopedWorkspaces(ctx, client, workspaces, annos) if err != nil { return schemaRoute{}, nil, err } - usable, _, err := scope.attached(ctx, b.client, metastoreID, annos) + usable, _, err := scope.attached(ctx, client, metastoreID, annos) if err != nil { return schemaRoute{}, nil, assignmentListError(metastoreID, err) } @@ -592,11 +595,11 @@ func (b *schemaBuilder) liveCatalogRoute( metastoreID, catalogName) } - workspace, catalog, err := workspaceForCatalog(ctx, b.client, metastoreID, catalogName, usable, probe, nil, annos) + workspace, catalog, err := workspaceForCatalog(ctx, client, metastoreID, catalogName, usable, probe, nil, annos) if err != nil { if errors.Is(err, errCatalogGone) { return schemaRoute{}, nil, confirmCatalogGone( - ctx, b.client, b.clientID, metastoreID, catalogName, usable[0].DeploymentName, metastoreOwner, annos) + ctx, client, clientID, metastoreID, catalogName, usable[0].DeploymentName, metastoreOwner, annos) } return schemaRoute{}, nil, err diff --git a/pkg/connector/schema_child.go b/pkg/connector/schema_child.go index 04e6df15..bdf6ad80 100644 --- a/pkg/connector/schema_child.go +++ b/pkg/connector/schema_child.go @@ -19,12 +19,12 @@ import ( "go.uber.org/zap" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "google.golang.org/protobuf/proto" ) -// profileKeySchemaOwner carries the owner of the parent schema. Owning the -// schema is one of the things that makes a table's or a volume's privilege read -// complete, and Grants is handed only the table, so List writes the owner down -// instead of listing the schemas again on every call. +// profileKeySchemaOwner carries the parent schema's owner, which is one of the +// things that makes a table's or a volume's privilege read complete. Grants is +// handed only the table, so List writes it down. const profileKeySchemaOwner = "schema_owner" // schemaChildRoute is the workspace a schema's tables or volumes are listed @@ -41,12 +41,9 @@ type schemaChildRoute struct { } // listSchemaChildren pages one level that hangs from a schema — a table or a -// volume. pushSchemaChildRoute re-runs the schema syncer's completeness proof -// on the first page of every schema, once for tables and again for volumes. -// The privilege reads in that proof bypass the HTTP cache. The owners the -// proof learns are written onto each resource, so Grants does not repeat it. -// -// fetch reads one page through the routed workspace and turns it into resources. +// volume — through a workspace proved to list every one of them, calling fetch +// for each page. The owners that proof learns are written onto each resource, +// so Grants does not run it again. func listSchemaChildren( ctx context.Context, client *databricks.Client, @@ -59,6 +56,7 @@ func listSchemaChildren( fetch func(ctx context.Context, route schemaChildRoute, metastoreID, catalogName, schemaName, pageToken string) ( []*v2.Resource, string, *v2.RateLimitDescription, error), ) ([]*v2.Resource, *rs.SyncOpResults, error) { + ctx = context.WithValue(ctx, syncSessionKey{}, attr.Session) if parentResourceID.GetResourceType() != schemaResourceType.Id { return nil, nil, nil } @@ -105,6 +103,11 @@ func listSchemaChildren( schemaChildListingError(noun, catalogName+schemaNameSeparator+schemaName, route.Workspace, err) } + if next != "" && next == bag.PageToken() { + return nil, &rs.SyncOpResults{Annotations: annos}, status.Errorf(codes.Internal, + "databricks-connector: listing the %ss of schema %s.%s repeated page token %q", noun, catalogName, schemaName, next) + } + nextToken, err := bag.NextToken(next) if err != nil { return nil, &rs.SyncOpResults{Annotations: annos}, err @@ -121,8 +124,10 @@ const childListRemedy = "the connector's service principal must own the metastor // pushSchemaChildRoute records a workspace that can list every table or volume // of the schema, walking the running in-scope workspaces until one serves the -// catalog and the child listing is complete. A workspace that does not serve it -// is skipped; an empty result is an error unless every workspace named it gone. +// catalog, serves the schema and proves the child listing complete. A workspace +// that answers neither is skipped and the walk goes on. Pushing nothing empties +// the listing, so that is reserved for a parent every workspace named as gone +// with none refusing a read; any refusal left standing is an error instead. func pushSchemaChildRoute( ctx context.Context, client *databricks.Client, @@ -153,22 +158,25 @@ func pushSchemaChildRoute( } owner, err := metastoreOwner(ctx, client, metastoreID, annos) + // The metastore syncer removes the parent in the same sync. + if errors.Is(err, errMetastoreGone) { + return nil + } if err != nil { return err } fullName := catalogName + schemaNameSeparator + schemaName - var sawCatalog, sawGone bool + var sawCatalog, sawGone, provenGone, sawForeign bool var lastDeny error for i := range usable { workspace := usable[i] catalog, err := catalogThrough(ctx, client, workspace.DeploymentName, metastoreID, catalogName, annos) if err != nil { - // A workspace that does not serve the catalog says nothing about the - // schema below it, so the walk goes on. Returning the error instead - // reaches the SDK as NotFound for a 404 carrying no code, which it - // reads as a warning: the children would be emitted as none and - // ConductorOne would delete them. + // A 404 carrying no code reaches the SDK as NotFound, which it reads as + // a warning: the children would be emitted as none and C1 would delete + // them. A workspace that does not serve the catalog says nothing about + // the schema below it, so the walk goes on instead. if errors.Is(err, errCatalogGone) { sawGone = true @@ -183,9 +191,16 @@ func pushSchemaChildRoute( return err } if catalog == nil { + // A catalog of another metastore is an answer, not an absence, so it + // cannot stand in for proof that this metastore's catalog is gone. + sawForeign = true + continue } sawCatalog = true + // The reads run in order, so a workspace serving the catalog now supersedes a + // gone answer an earlier one gave. + provenGone = false route := schemaChildRoute{ Workspace: workspace.DeploymentName, @@ -204,6 +219,14 @@ func pushSchemaChildRoute( } schemasComplete, err := schemaListingIsComplete(ctx, client, trust, annos) if err != nil { + // A workspace the catalog is not bound to refuses with 403, which + // deniesTrust takes above, so this read names it missing only when it is. + if errors.Is(err, errCatalogGone) { + provenGone = true + + continue + } + return err } @@ -214,6 +237,15 @@ func pushSchemaChildRoute( schema, err = nil, nil } if err != nil { + // A workspace that refuses the schema read says nothing about the ones + // that have not been asked yet, so the walk goes on as it does for the + // catalog read above. + if deniesTrust(err) { + lastDeny = err + + continue + } + return err } if schemasComplete && schema == nil { @@ -233,6 +265,12 @@ func pushSchemaChildRoute( trust.schemaOwner = schema.Owner childrenComplete, err := childListingIsComplete(ctx, client, trust, annos) if err != nil { + if errors.Is(err, errCatalogGone) { + provenGone = true + + continue + } + return err } if !childrenComplete { @@ -248,11 +286,14 @@ func pushSchemaChildRoute( return nil } + // A catalog proved gone holds no tables or volumes, and the catalog syncer + // drops the parent in the same sync. A refusal anywhere leaves the picture mixed. + if provenGone && lastDeny == nil { + return nil + } if !sawCatalog { - // Every workspace named the catalog as gone and none refused the read. - // The catalog syncer drops it in the same sync, so this empties what - // ConductorOne is already removing. - if sawGone && lastDeny == nil { + // A workspace that does not serve the catalog answers like a gone one. + if sawGone && lastDeny == nil && !sawForeign { return nil } msg := fmt.Sprintf("databricks-connector: no workspace in sync scope lists catalog %s of metastore %s", catalogName, metastoreID) @@ -358,7 +399,9 @@ func schemaChildProfile(metastoreID, catalogName, schemaName, fullName, owner, s } } -// schemaChildGrantsAreTrustworthy is true for an owner of the table or volume, its schema, its catalog, or the metastore, or for a holder of MANAGE on any of those. +// schemaChildGrantsAreTrustworthy is true for an owner of the table or volume, +// its schema, its catalog or the metastore, for a holder of MANAGE on any of +// those, or for a direct Admin when the workspace admins group owns the catalog. func schemaChildGrantsAreTrustworthy( ctx context.Context, client *databricks.Client, @@ -386,3 +429,93 @@ func schemaChildGrantsAreTrustworthy( // list complete, and one effective read reports all three. return holdsEffectivePrivilegeOn(ctx, client, trust, securableType, trust.fullName, managePrivilege, annos) } + +// liveChildRoute rebuilds the route and the owners above a table or a volume +// from what Databricks reports now. The metastore owner comes from a live +// listing, the catalog from the recorded workspace when this sync still admits +// it and otherwise from the first running in-scope workspace that serves it, +// and the schema from a read through that workspace. +func liveChildRoute( + ctx context.Context, + client *databricks.Client, + clientID string, + workspaces map[string]struct{}, + resource *v2.Resource, + metastoreID, catalogName, schemaName string, + annos *annotations.Annotations, +) (schemaChildRoute, error) { + owner, err := metastoreOwner(ctx, client, metastoreID, annos) + if err != nil { + return schemaChildRoute{}, err + } + + route, catalog, err := liveCatalogRoute(ctx, client, clientID, workspaces, resource, metastoreID, catalogName, owner, annos) + if err != nil { + return schemaChildRoute{}, err + } + + fullName := catalogName + schemaNameSeparator + schemaName + schema, rateLimit, err := client.GetSchema(ctx, route.Workspace, fullName) + noteRateLimit(annos, rateLimit) + if err != nil { + // Only Databricks naming the securable proves it gone; an unbound workspace + // refuses with 403, and reading that as an absence would report a false revoke. + if databricks.IsSecurableGone(err, errorCodeSchemaGone, errorCodeCatalogGone) { + return schemaChildRoute{}, errSecurableGone + } + + return schemaChildRoute{}, refuseUnreadable(fmt.Sprintf( + "databricks-connector: failed to read schema %s through workspace %s", fullName, route.Workspace), err) + } + + return schemaChildRoute{ + Workspace: route.Workspace, + WorkspaceID: route.WorkspaceID, + WorkspaceName: route.WorkspaceName, + MetastoreOwner: owner, + CatalogOwner: catalog.Owner, + SchemaOwner: schema.Owner, + }, nil +} + +// childEntitlementOnLiveRoute rebuilds the entitlement's table or volume from +// what Databricks reports now: the four owners the trust check short-circuits +// on — metastore, catalog, schema and the securable itself — ride on the +// profile, where they can be hours old, so a transfer away from the connector +// would otherwise let a truncated privilege list decide the write. read is the +// level's own leaf read, which also answers whether the securable is gone. +func childEntitlementOnLiveRoute( + ctx context.Context, + client *databricks.Client, + clientID string, + workspaces map[string]struct{}, + noun string, + entitlement *v2.Entitlement, + annos *annotations.Annotations, + read func(ctx context.Context, route schemaChildRoute, metastoreID, catalogName, schemaName, name string, + parent *v2.ResourceId, annos *annotations.Annotations) (*v2.Resource, error), +) (*v2.Entitlement, error) { + resource := entitlement.GetResource() + metastoreID, catalogName, schemaName, name, err := parseSchemaChildID(resource.GetId().GetResource(), noun) + if err != nil { + return nil, err + } + + route, err := liveChildRoute(ctx, client, clientID, workspaces, resource, metastoreID, catalogName, schemaName, annos) + if err != nil { + return nil, err + } + + routed, err := read(ctx, route, metastoreID, catalogName, schemaName, name, resource.GetParentResourceId(), annos) + if err != nil { + return nil, err + } + + copyEntitlement, ok := proto.Clone(entitlement).(*v2.Entitlement) + if !ok { + return nil, status.Errorf(codes.Internal, "databricks-connector: cannot copy the %s entitlement", noun) + } + copyEntitlement.Resource = routed + + return copyEntitlement, nil +} diff --git a/pkg/connector/table.go b/pkg/connector/table.go index 23e0f65a..d36e20f3 100644 --- a/pkg/connector/table.go +++ b/pkg/connector/table.go @@ -2,6 +2,7 @@ package connector import ( "context" + "errors" "fmt" "slices" "strings" @@ -23,8 +24,6 @@ var _ connectorbuilder.ResourceSyncerV2 = (*tableBuilder)(nil) const ( securableTypeTable = "TABLE" - // errorCodeTableGone is what Databricks names in error_code when the table a - // request addresses does not exist. errorCodeTableGone = "TABLE_DOES_NOT_EXIST" // The canonical UPPER_SNAKE form the API answers with. @@ -46,12 +45,9 @@ const ( profileKeyTableName = "table_name" profileKeyTableType = "table_type" - // tableWriteRemedy is what a 403 on the PATCH tells the operator. tableWriteRemedy = "the connector must own the metastore, the parent catalog, the parent schema or the table, " + "or hold MANAGE on the table, the parent schema or the parent catalog" - // tableTrustRemedy is what a refusal to report a partial privilege list - // tells the operator. tableTrustRemedy = "the connector's service principal must own the metastore, the parent catalog, the parent schema or the table, " + "hold MANAGE on the table, the parent schema or the parent catalog, or be assigned Admin directly on the workspace whose " + "admins group owns the parent catalog" @@ -281,7 +277,7 @@ func privilegesForTable(ctx context.Context, resource *v2.Resource) []string { // Entitlements are per table, not static: the privilege set follows table_type, // and a privilege the grants API refuses must not be offered as requestable. -// ConductorOne would otherwise advertise requests that always fail. +// C1 would otherwise advertise requests that always fail. func (b *tableBuilder) Entitlements(ctx context.Context, resource *v2.Resource, _ rs.SyncOpAttrs) ([]*v2.Entitlement, *rs.SyncOpResults, error) { variant, _ := rs.GetProfileStringValue(rs.GetProfile(resource), profileKeyTableType) grantable := ent.WithGrantableTo(userResourceType, groupResourceType, servicePrincipalResourceType) @@ -312,17 +308,37 @@ func (b *tableBuilder) Entitlements(ctx context.Context, resource *v2.Resource, return rv, nil, nil } +// Grants carries the sync session so the workspace-Admin check behind the trust +// rule is read once per workspace per sync rather than once per table. func (b *tableBuilder) Grants(ctx context.Context, resource *v2.Resource, attr rs.SyncOpAttrs) ([]*v2.Grant, *rs.SyncOpResults, error) { + ctx = context.WithValue(ctx, syncSessionKey{}, attr.Session) + return b.withLevel(tableLevel(privilegesForTable(ctx, resource))).syncGrants(ctx, resource, attr) } func (b *tableBuilder) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) (annotations.Annotations, error) { - resource := entitlement.GetResource() - if err := refuseUngrantableTablePrivilege(resource, entitlement); err != nil { - return nil, err + ctx = databricks.WithoutCache(ctx) + annos := annotations.Annotations{} + routed, err := b.entitlementOnLiveRoute(ctx, entitlement, &annos) + if err != nil { + if isSecurableGoneSentinel(err) || errors.Is(err, errSecurableGone) { + return annos, status.Errorf(codes.NotFound, "databricks-connector: table %s no longer exists", + entitlement.GetResource().GetId().GetResource()) + } + + return annos, err + } + + // The live table_type decides this, not the one the last sync wrote: a view + // replaced by a table takes privileges the recorded variant refuses. + resource := routed.GetResource() + if err := refuseUngrantableTablePrivilege(resource, routed); err != nil { + return annos, err } - return b.withLevel(tableLevel(privilegesForTable(ctx, resource))).grant(ctx, principal.GetId(), entitlement) + writeAnnos, err := b.withLevel(tableLevel(privilegesForTable(ctx, resource))).grant(ctx, principal.GetId(), routed) + + return append(annos, writeAnnos...), err } // refuseUngrantableTablePrivilege rejects the two privileges a table reports but @@ -352,7 +368,49 @@ func refuseUngrantableTablePrivilege(resource *v2.Resource, entitlement *v2.Enti } func (b *tableBuilder) Revoke(ctx context.Context, revoked *v2.Grant) (annotations.Annotations, error) { - resource := revoked.GetEntitlement().GetResource() + ctx = databricks.WithoutCache(ctx) + annos := annotations.Annotations{} + routed, err := b.entitlementOnLiveRoute(ctx, revoked.GetEntitlement(), &annos) + if err != nil { + if isSecurableGoneSentinel(err) || errors.Is(err, errSecurableGone) { + annos.Update(&v2.GrantAlreadyRevoked{}) + + return annos, nil + } + + return annos, err + } - return b.withLevel(tableLevel(privilegesForTable(ctx, resource))).revoke(ctx, revoked.GetPrincipal().GetId(), revoked.GetEntitlement()) + writeAnnos, err := b.withLevel(tableLevel(privilegesForTable(ctx, routed.GetResource()))). + revoke(ctx, revoked.GetPrincipal().GetId(), routed) + + return append(annos, writeAnnos...), err +} + +// entitlementOnLiveRoute reads the table itself after its parents, because the +// owner decides whether the privilege list is complete and table_type decides +// which privileges the write may ask for. +func (b *tableBuilder) entitlementOnLiveRoute( + ctx context.Context, + entitlement *v2.Entitlement, + annos *annotations.Annotations, +) (*v2.Entitlement, error) { + return childEntitlementOnLiveRoute(ctx, b.client, b.clientID, b.workspaces, "table", entitlement, annos, + func(ctx context.Context, route schemaChildRoute, metastoreID, catalogName, schemaName, name string, + parent *v2.ResourceId, annos *annotations.Annotations, + ) (*v2.Resource, error) { + fullName := strings.Join([]string{catalogName, schemaName, name}, schemaNameSeparator) + table, rateLimit, err := b.client.GetTable(ctx, route.Workspace, fullName) + noteRateLimit(annos, rateLimit) + if err != nil { + if databricks.IsSecurableGone(err, errorCodeTableGone, errorCodeSchemaGone, errorCodeCatalogGone) { + return nil, errSecurableGone + } + + return nil, refuseUnreadable(fmt.Sprintf( + "databricks-connector: failed to read table %s through workspace %s", fullName, route.Workspace), err) + } + + return tableResource(table, metastoreID, catalogName, schemaName, parent, route) + }) } diff --git a/pkg/connector/volume.go b/pkg/connector/volume.go index c037218f..b03a32f2 100644 --- a/pkg/connector/volume.go +++ b/pkg/connector/volume.go @@ -2,6 +2,7 @@ package connector import ( "context" + "errors" "fmt" "strings" @@ -11,6 +12,8 @@ import ( "github.com/conductorone/baton-sdk/pkg/connectorbuilder" ent "github.com/conductorone/baton-sdk/pkg/types/entitlement" rs "github.com/conductorone/baton-sdk/pkg/types/resource" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) var _ connectorbuilder.StaticEntitlementSyncerV2 = (*volumeBuilder)(nil) @@ -25,12 +28,9 @@ const ( profileKeyVolumeName = "volume_name" profileKeyVolumeType = "volume_type" - // volumeWriteRemedy is what a 403 on the PATCH tells the operator. volumeWriteRemedy = "the connector must own the metastore, the parent catalog, the parent schema or the volume, " + "or hold MANAGE on the volume, the parent schema or the parent catalog" - // volumeTrustRemedy is what a refusal to report a partial privilege list - // tells the operator. volumeTrustRemedy = "the connector's service principal must own the metastore, the parent catalog, the parent schema or the volume, " + "hold MANAGE on the volume, the parent schema or the parent catalog, or be assigned Admin directly on the workspace whose " + "admins group owns the parent catalog" @@ -195,14 +195,77 @@ func (b *volumeBuilder) Entitlements(context.Context, *v2.Resource, rs.SyncOpAtt return nil, nil, nil } +// Grants carries the sync session so the workspace-Admin check behind the trust +// rule is read once per workspace per sync rather than once per volume. func (b *volumeBuilder) Grants(ctx context.Context, resource *v2.Resource, attr rs.SyncOpAttrs) ([]*v2.Grant, *rs.SyncOpResults, error) { + ctx = context.WithValue(ctx, syncSessionKey{}, attr.Session) + return b.syncGrants(ctx, resource, attr) } func (b *volumeBuilder) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) (annotations.Annotations, error) { - return b.grant(ctx, principal.GetId(), entitlement) + ctx = databricks.WithoutCache(ctx) + annos := annotations.Annotations{} + routed, err := b.entitlementOnLiveRoute(ctx, entitlement, &annos) + if err != nil { + if isSecurableGoneSentinel(err) || errors.Is(err, errSecurableGone) { + return annos, status.Errorf(codes.NotFound, "databricks-connector: volume %s no longer exists", + entitlement.GetResource().GetId().GetResource()) + } + + return annos, err + } + + writeAnnos, err := b.grant(ctx, principal.GetId(), routed) + + return append(annos, writeAnnos...), err } func (b *volumeBuilder) Revoke(ctx context.Context, revoked *v2.Grant) (annotations.Annotations, error) { - return b.revoke(ctx, revoked.GetPrincipal().GetId(), revoked.GetEntitlement()) + ctx = databricks.WithoutCache(ctx) + annos := annotations.Annotations{} + routed, err := b.entitlementOnLiveRoute(ctx, revoked.GetEntitlement(), &annos) + if err != nil { + if isSecurableGoneSentinel(err) || errors.Is(err, errSecurableGone) { + annos.Update(&v2.GrantAlreadyRevoked{}) + + return annos, nil + } + + return annos, err + } + + writeAnnos, err := b.revoke(ctx, revoked.GetPrincipal().GetId(), routed) + + return append(annos, writeAnnos...), err +} + +// entitlementOnLiveRoute reads the volume itself after its parents, because its +// owner is one of the four the trust check short-circuits on. The read runs +// through a workspace that has just served the catalog and the schema, so a +// generic 404 here is the volume being gone rather than a workspace that cannot +// reach it. +func (b *volumeBuilder) entitlementOnLiveRoute( + ctx context.Context, + entitlement *v2.Entitlement, + annos *annotations.Annotations, +) (*v2.Entitlement, error) { + return childEntitlementOnLiveRoute(ctx, b.client, b.clientID, b.workspaces, "volume", entitlement, annos, + func(ctx context.Context, route schemaChildRoute, metastoreID, catalogName, schemaName, name string, + parent *v2.ResourceId, annos *annotations.Annotations, + ) (*v2.Resource, error) { + fullName := strings.Join([]string{catalogName, schemaName, name}, schemaNameSeparator) + volume, rateLimit, err := b.client.GetVolume(ctx, route.Workspace, fullName) + noteRateLimit(annos, rateLimit) + if err != nil { + if databricks.IsSecurableGone(err, errorCodeGenericGone, errorCodeSchemaGone, errorCodeCatalogGone) { + return nil, errSecurableGone + } + + return nil, refuseUnreadable(fmt.Sprintf( + "databricks-connector: failed to read volume %s through workspace %s", fullName, route.Workspace), err) + } + + return volumeResource(volume, metastoreID, catalogName, schemaName, parent, route) + }) } diff --git a/pkg/databricks/models.go b/pkg/databricks/models.go index 658e7c1c..b2107798 100644 --- a/pkg/databricks/models.go +++ b/pkg/databricks/models.go @@ -197,12 +197,18 @@ type Table struct { Name string `json:"name"` Owner string `json:"owner"` TableType string `json:"table_type"` + // FullName is the dotted "catalog.schema.table" Databricks addresses the + // table by. A single read is checked against it. + FullName string `json:"full_name"` } type Volume struct { Name string `json:"name"` Owner string `json:"owner"` VolumeType string `json:"volume_type"` + // FullName is the dotted "catalog.schema.volume" Databricks addresses the + // volume by. A single read is checked against it. + FullName string `json:"full_name"` } type PrivilegeAssignment struct { diff --git a/pkg/databricks/unity_catalog.go b/pkg/databricks/unity_catalog.go index 0915b93f..1931d5ed 100644 --- a/pkg/databricks/unity_catalog.go +++ b/pkg/databricks/unity_catalog.go @@ -152,12 +152,60 @@ func (c *Client) GetVolume( ) { var res Volume + if fullName == "" { + return nil, nil, invalidArgument("volume full name is required") + } + u := c.workspaceUrl(workspaceId).JoinPath(unityCatalogVolumesEndpoint, fullName) + if !addressesSecurable(u, "/"+fullName) { + return nil, nil, invalidArgument("cannot address volume %q: the name does not survive URL assembly", fullName) + } + ratelimitData, err := c.Get(ctx, u, &res) if err != nil { return nil, ratelimitData, nameWorkspace403Remedy(workspaceId, fmt.Errorf("failed to read volume %s: %w", fullName, err)) } + if res.FullName != fullName { + return nil, ratelimitData, invalidArgument("asked Databricks for volume %q and it answered with %q", fullName, res.FullName) + } + + return &res, ratelimitData, nil +} + +// GetTable (GET /api/2.1/unity-catalog/tables/{full_name}) reads one table +// through workspaceId, which is a deployment name. It answers the two things a +// write needs and the listing cannot be trusted to still hold: the owner, which +// decides whether the privilege list is complete, and table_type, which decides +// which privileges the table takes. +func (c *Client) GetTable( + ctx context.Context, + workspaceId, fullName string, +) ( + *Table, + *v2.RateLimitDescription, + error, +) { + var res Table + + if fullName == "" { + return nil, nil, invalidArgument("table full name is required") + } + + u := c.workspaceUrl(workspaceId).JoinPath(unityCatalogTablesEndpoint, fullName) + if !addressesSecurable(u, "/"+fullName) { + return nil, nil, invalidArgument("cannot address table %q: the name does not survive URL assembly", fullName) + } + + ratelimitData, err := c.Get(ctx, u, &res) + if err != nil { + return nil, ratelimitData, nameWorkspace403Remedy(workspaceId, fmt.Errorf("failed to read table %s: %w", fullName, err)) + } + + if res.FullName != fullName { + return nil, ratelimitData, invalidArgument("asked Databricks for table %q and it answered with %q", fullName, res.FullName) + } + return &res, ratelimitData, nil } @@ -200,48 +248,6 @@ func (c *Client) GetCatalog( return &res, ratelimitData, nil } -// GetSchema (GET /api/2.1/unity-catalog/schemas/{full_name}) reads one schema -// through workspaceId, which is a deployment name. full_name is "{catalog}.{schema}". -// It separates the two answers draining ListSchemas runs together: a 404 naming -// SCHEMA_DOES_NOT_EXIST is Databricks naming the schema missing, which a caller -// that merely cannot see it is answered with too, and any other refusal is this -// workspace not serving it. Which of the two a named 404 is belongs to the -// caller. The name is checked against the assembled URL for the same reason -// permissionsUrl checks it. -func (c *Client) GetSchema( - ctx context.Context, - workspaceId, fullName string, -) ( - *Schema, - *v2.RateLimitDescription, - error, -) { - var res Schema - - if fullName == "" { - return nil, nil, invalidArgument("schema full name is required") - } - - u := c.workspaceUrl(workspaceId).JoinPath(unityCatalogSchemasEndpoint, fullName) - if !addressesSecurable(u, "/"+fullName) { - return nil, nil, invalidArgument("cannot address schema %q: the name does not survive URL assembly", fullName) - } - - ratelimitData, err := c.Get(ctx, u, &res) - if err != nil { - return nil, ratelimitData, nameWorkspace403Remedy(workspaceId, fmt.Errorf("failed to read schema %s: %w", fullName, err)) - } - - // An answer about another schema, or one that names no schema at all, does not - // describe the one asked for, and its owner is what decides whether the - // children listing is complete. - if res.FullName != fullName { - return nil, ratelimitData, invalidArgument("asked Databricks for schema %q and it answered with %q", fullName, res.FullName) - } - - return &res, ratelimitData, nil -} - // ListSchemas (GET /api/2.1/unity-catalog/schemas?catalog_name=) reads one page of // the schemas of catalogName, through workspaceId, which is a deployment name. // Unlike the catalogs listing, this one pages, so the cursor is passed through. @@ -272,9 +278,13 @@ func (c *Client) ListSchemas( } // GetSchema (GET /api/2.1/unity-catalog/schemas/{full_name}) reads one schema -// through workspaceId, which is a deployment name. The full name is dotted, -// and a deleted schema or catalog is named in error_code while a workspace the -// catalog is not bound to answers 403. +// through workspaceId, which is a deployment name. The full name is dotted. It +// separates the two answers draining ListSchemas runs together: a 404 naming +// SCHEMA_DOES_NOT_EXIST is Databricks naming the schema missing, and any other +// refusal, 403 among them, is this workspace not serving it. Which of the two a +// named 404 is belongs to the caller. An answer that names another schema, or +// none at all, is refused: its owner would otherwise be read as this schema's, +// and that owner decides whether a privilege list is complete. func (c *Client) GetSchema( ctx context.Context, workspaceId, fullName string, @@ -299,7 +309,7 @@ func (c *Client) GetSchema( return nil, ratelimitData, nameWorkspace403Remedy(workspaceId, fmt.Errorf("failed to read schema %s: %w", fullName, err)) } - if res.FullName != "" && res.FullName != fullName { + if res.FullName != fullName { return nil, ratelimitData, invalidArgument("asked Databricks for schema %q and it answered with %q", fullName, res.FullName) } From 80e8078e0d297c2e728822557201ae1f943896fe Mon Sep 17 00:00:00 2001 From: Mateo Hernandez Date: Tue, 17 Nov 2026 10:11:29 -0300 Subject: [PATCH 6/9] refactor(databricks): drop the table and volume unit tests The file was 293 lines built on the fake Databricks account the schema tests carried, and #72 dropped that fake with them. What it proved was that the code does what it was written to do. The tenant run is what answers whether the layer works, and QA runs the connector against a tenant before production. Nothing tracked references the fake or its helpers any more, so the package builds and tests clean without this file, which is the view CI gets. It stays excluded per clone, next to the schema tests, for local iteration. The client tests in pkg/databricks/unity_catalog_test.go stay: they assert the query set a listing sends and the name a single read is allowed to answer with, which is a contract rather than a restatement of the caller. Co-Authored-By: Claude Opus 5 --- pkg/connector/schema_child_test.go | 293 ----------------------------- 1 file changed, 293 deletions(-) delete mode 100644 pkg/connector/schema_child_test.go diff --git a/pkg/connector/schema_child_test.go b/pkg/connector/schema_child_test.go deleted file mode 100644 index 86196403..00000000 --- a/pkg/connector/schema_child_test.go +++ /dev/null @@ -1,293 +0,0 @@ -package connector - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "slices" - "strings" - "testing" - - v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" - "github.com/conductorone/baton-sdk/pkg/pagination" - rs "github.com/conductorone/baton-sdk/pkg/types/resource" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" -) - -// serveSchema answers GET /schemas/{full_name}. -func (f *fakeSchemaAccount) serveSchema(w http.ResponseWriter, r *http.Request) { - workspace := r.Header.Get("X-Test-Workspace") - f.schemaReads = append(f.schemaReads, workspace) - if code, ok := f.schemaGetStatus[workspace]; ok { - body := `{"message":"refused"}` - if f.schemaGetErrorCode != "" { - body = fmt.Sprintf(`{"error_code":%q,"message":"refused"}`, f.schemaGetErrorCode) - } - http.Error(w, body, code) - - return - } - - owner := "someone" - if len(f.schemas) > 0 { - owner, _ = f.schemas[0]["owner"].(string) - } - _ = json.NewEncoder(w).Encode(map[string]any{"name": "sales", "full_name": "main.sales", "owner": owner}) -} - -func (f *fakeSchemaAccount) serveSchemaChildren(w http.ResponseWriter, r *http.Request, key string, rows []map[string]any) { - f.childReads = append(f.childReads, key) - if got := r.URL.Query().Get("schema_name"); got == "" { - http.Error(w, `{"message":"schema_name is required"}`, http.StatusBadRequest) - - return - } - _ = json.NewEncoder(w).Encode(map[string]any{key: rows}) -} - -var testSchemaResourceID = &v2.ResourceId{ResourceType: schemaResourceType.Id, Resource: "ms-1::main.sales"} - -func newTestChildBuildersAs(t *testing.T, f *fakeSchemaAccount, clientID string) (*tableBuilder, *volumeBuilder) { - t.Helper() - t.Setenv("BATON_HTTP_CACHE_TTL", "0") - if f.metastoreOwner == "" { - f.metastoreOwner = testOwnPrincipal - } - if f.assignments == nil { - f.assignments = map[string][]map[string]any{} - } - client, _ := newWorkspaceRoutedClient(t, f.handler(t)) - - return newTableBuilder(client, clientID, nil, syncPrincipals{groups: true}), - newVolumeBuilder(client, clientID, nil, syncPrincipals{groups: true}) -} - -func listAllChildren(t *testing.T, - list func(context.Context, *v2.ResourceId, rs.SyncOpAttrs) ([]*v2.Resource, *rs.SyncOpResults, error), -) ([]*v2.Resource, error) { - t.Helper() - - var all []*v2.Resource - token := "" - for range 1000 { - resources, results, err := list(context.Background(), testSchemaResourceID, rs.SyncOpAttrs{PageToken: pagination.Token{Token: token}}) - if err != nil { - return all, err - } - all = append(all, resources...) - if results == nil || results.NextPageToken == "" { - return all, nil - } - token = results.NextPageToken - } - t.Fatal("List never returned an empty page token") - - return nil, nil -} - -// A schema's children are listed after reading that one schema, not after -// walking the catalog's whole schema listing: the walk runs once per schema and -// again per child type, which is quadratic in the catalog's schema count. -func TestSchemaChildListReadsOneSchemaInsteadOfDrainingTheCatalog(t *testing.T) { - // sales sits last, so walking the listing pays for every page of the catalog. - manySchemas := make([]map[string]any, 0, 300) - for i := range 299 { - manySchemas = append(manySchemas, map[string]any{"name": fmt.Sprintf("s%03d", i), "owner": "eng"}) - } - manySchemas = append(manySchemas, map[string]any{"name": "sales", "owner": "eng"}) - - f := &fakeSchemaAccount{ - metastoreOwner: "eng", - catalogOwner: "eng", - schemas: manySchemas, - tables: []map[string]any{{"name": "orders", "owner": "eng", "table_type": "MANAGED"}}, - volumes: []map[string]any{{"name": "files", "owner": "eng", "volume_type": "MANAGED"}}, - } - tables, volumes := newTestChildBuildersAs(t, f, "eng") - - listedTables, err := listAllChildren(t, tables.List) - if err != nil { - t.Fatalf("table List: %v", err) - } - listedVolumes, err := listAllChildren(t, volumes.List) - if err != nil { - t.Fatalf("volume List: %v", err) - } - - if len(listedTables) != 1 || len(listedVolumes) != 1 { - t.Fatalf("listed %d table(s) and %d volume(s), want 1 of each", len(listedTables), len(listedVolumes)) - } - if f.schemaPages != 0 { - t.Errorf("the schemas listing was paged %d time(s); listing a schema's children must not walk the catalog's schemas", f.schemaPages) - } - if want := []string{"wsa", "wsa"}; !slices.Equal(f.schemaReads, want) { - t.Errorf("single-schema reads = %v, want %v: one per child type", f.schemaReads, want) - } -} - -// A schema named as missing is proof of deletion only for a caller that sees the -// whole catalog. Reading the other case as "no children" would make ConductorOne -// delete every table and volume under it. -func TestSchemaChildListTellsADeletedSchemaFromAnInvisibleOne(t *testing.T) { - tests := []struct { - name string - clientID string - catalogOwner string - errorCode string - wantErr codes.Code - }{ - { - name: "the connector owns the catalog, so the schema is gone", - clientID: "eng", catalogOwner: "eng", errorCode: errorCodeSchemaGone, - }, - { - name: "the connector cannot see the whole catalog, so the sync fails", - clientID: "app-9", catalogOwner: "someone", errorCode: errorCodeSchemaGone, - wantErr: codes.PermissionDenied, - }, - { - // A 404 Databricks does not put a code on says nothing about the - // schema. Read as an empty result it would delete every table and - // volume under it, so the sync fails instead. - name: "a 404 naming nothing is not proof the schema is gone", - clientID: "eng", catalogOwner: "eng", - wantErr: codes.FailedPrecondition, - }, - { - // CATALOG_DOES_NOT_EXIST names the parent, not this schema, and - // one workspace is not every workspace, so it does not prove the - // subtree is gone. - name: "a 404 naming the parent catalog is not proof either", - clientID: "eng", catalogOwner: "eng", errorCode: errorCodeCatalogGone, - wantErr: codes.FailedPrecondition, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - f := &fakeSchemaAccount{ - metastoreOwner: "someone", - catalogOwner: test.catalogOwner, - schemas: []map[string]any{{"name": "sales", "owner": "someone"}}, - schemaGetStatus: map[string]int{"wsa": http.StatusNotFound}, - schemaGetErrorCode: test.errorCode, - } - tables, _ := newTestChildBuildersAs(t, f, test.clientID) - - listed, err := listAllChildren(t, tables.List) - if test.wantErr != codes.OK { - if status.Code(err) != test.wantErr { - t.Fatalf("List error = %v, want %s", err, test.wantErr) - } - - return - } - if err != nil { - t.Fatalf("List: %v", err) - } - if len(listed) != 0 { - t.Errorf("listed %d table(s), want none under a schema that is gone", len(listed)) - } - // The empty result must come from the schema read proving the - // absence, not from an earlier silent exit. - if len(f.schemaReads) == 0 || len(f.childReads) != 0 { - t.Errorf("schema reads = %v, child listings = %v: want the empty result to come from the schema read", - f.schemaReads, f.childReads) - } - }) - } -} - -// A workspace that does not serve the catalog answers the catalog read with a -// 404 carrying no error code, the same answer a routing or permission miss -// gives. The schema syncer walks on to the next workspace; listing a schema's -// children must do the same. Returning that error instead reaches the SDK as -// codes.NotFound, which it reads as a warning: the listing finishes green with -// no resources and ConductorOne deletes every table and volume under the schema. -func TestSchemaChildListTriesTheNextWorkspaceWhenOneDoesNotServeTheCatalog(t *testing.T) { - for _, errorCode := range []string{"", errorCodeCatalogGone} { - name := "bare 404" - if errorCode != "" { - name = errorCode - } - t.Run(name, func(t *testing.T) { - f := &fakeSchemaAccount{ - second: true, - metastoreOwner: "eng", - catalogOwner: "eng", - catalogStatus: map[string]int{"wsa": http.StatusNotFound}, - catalogErrorCode: errorCode, - schemas: []map[string]any{{"name": "sales", "owner": "eng"}}, - tables: []map[string]any{{"name": "orders", "owner": "eng", "table_type": "MANAGED"}}, - } - tables, _ := newTestChildBuildersAs(t, f, "eng") - - listed, err := listAllChildren(t, tables.List) - if err != nil { - t.Fatalf("List: %v", err) - } - if len(listed) != 1 { - t.Fatalf("listed %d table(s), want 1 through the second workspace", len(listed)) - } - if workspace, _ := rs.GetProfileStringValue(rs.GetProfile(listed[0]), profileKeyWorkspace); workspace != "wsb" { - t.Errorf("profile[%s] = %q, want wsb", profileKeyWorkspace, workspace) - } - }) - } -} - -// Every workspace naming the catalog as gone, with none refusing the read, is -// the catalog being dropped. The catalog syncer removes it in the same sync, so -// the children listing empties what ConductorOne is already removing instead of -// failing the sync. -func TestSchemaChildListIsEmptyWhenEveryWorkspaceSaysTheCatalogIsGone(t *testing.T) { - f := &fakeSchemaAccount{ - second: true, - metastoreOwner: "eng", - catalogOwner: "eng", - catalogStatus: map[string]int{"wsa": http.StatusNotFound, "wsb": http.StatusNotFound}, - catalogErrorCode: errorCodeCatalogGone, - schemas: []map[string]any{{"name": "sales", "owner": "eng"}}, - tables: []map[string]any{{"name": "orders", "owner": "eng", "table_type": "MANAGED"}}, - } - tables, _ := newTestChildBuildersAs(t, f, "eng") - - listed, err := listAllChildren(t, tables.List) - if err != nil { - t.Fatalf("List: %v", err) - } - if len(listed) != 0 { - t.Errorf("listed %d table(s), want none under a catalog that is gone", len(listed)) - } - if want := []string{"wsa", "wsb"}; !slices.Equal(f.catalogReads, want) { - t.Errorf("catalog reads = %v, want %v: both workspaces are asked before the listing empties", f.catalogReads, want) - } -} - -// Every workspace refusing the catalog read is not the catalog being gone. The -// listing must fail and keep what ConductorOne already has, and the refusal has -// to reach the operator rather than be swallowed into a generic message. -func TestSchemaChildListFailsWhenEveryWorkspaceRefusesTheCatalog(t *testing.T) { - f := &fakeSchemaAccount{ - second: true, - metastoreOwner: "eng", - catalogOwner: "eng", - catalogStatus: map[string]int{"wsa": http.StatusForbidden, "wsb": http.StatusForbidden}, - schemas: []map[string]any{{"name": "sales", "owner": "eng"}}, - tables: []map[string]any{{"name": "orders", "owner": "eng", "table_type": "MANAGED"}}, - } - tables, _ := newTestChildBuildersAs(t, f, "eng") - - listed, err := listAllChildren(t, tables.List) - if status.Code(err) != codes.FailedPrecondition { - t.Fatalf("List error = %v, want FailedPrecondition", err) - } - if len(listed) != 0 { - t.Errorf("listed %d table(s), want none", len(listed)) - } - if !strings.Contains(err.Error(), "403") { - t.Errorf("List error = %v, want the refusal named in the message", err) - } -} From 65d88ec4995858cb98283f9fa75706e203408b68 Mon Sep 17 00:00:00 2001 From: Mateo Hernandez Date: Tue, 17 Nov 2026 10:11:44 -0300 Subject: [PATCH 7/9] chore(databricks): regenerate the capabilities without the table and volume traits resource_types.go dropped TRAIT_APP from table and volume, because neither has app semantics and a profile rides on any resource now. The committed capabilities file still carried the trait, and its workflow only regenerates on a push to main, so CI would not have caught it before the file reached the customer-facing docs. Regenerated with the same command the workflow runs. The trait is gone from both types, and the volume's SkipEntitlements annotation moved with it; the volume keeps a static entitlement set and the table does not, because its privileges follow table_type. Co-Authored-By: Claude Opus 5 --- baton_capabilities.json | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/baton_capabilities.json b/baton_capabilities.json index 5aa6dbdc..83db9ba1 100644 --- a/baton_capabilities.json +++ b/baton_capabilities.json @@ -387,9 +387,6 @@ "resourceType": { "id": "table", "displayName": "Table", - "traits": [ - "TRAIT_APP" - ], "annotations": [ { "@type": "type.googleapis.com/c1.connector.v2.OptInRequired" @@ -504,13 +501,13 @@ "resourceType": { "id": "volume", "displayName": "Volume", - "traits": [ - "TRAIT_APP" - ], "annotations": [ { "@type": "type.googleapis.com/c1.connector.v2.OptInRequired" }, + { + "@type": "type.googleapis.com/c1.connector.v2.SkipEntitlements" + }, { "@type": "type.googleapis.com/c1.connector.v2.CapabilityPermissions", "permissions": [ @@ -539,9 +536,6 @@ "permission": "USE_SCHEMA" } ] - }, - { - "@type": "type.googleapis.com/c1.connector.v2.SkipEntitlements" } ] }, From 0cfc4e730fa8be349736992ddc14ea0db8f56603 Mon Sep 17 00:00:00 2001 From: Mateo Hernandez Date: Tue, 17 Nov 2026 10:11:44 -0300 Subject: [PATCH 8/9] docs(databricks): say what reading a table's and a volume's privileges needs The setup sections stopped at the catalog and the schema, so an operator who grants USE_SCHEMA follows them, sees the schemas synced, and then fails on the tables and volumes under them: listing those needs ownership of the metastore, the catalog or the schema, direct workspace Admin where the admins group owns the catalog, or ALL_PRIVILEGES on the catalog, and reading one's privileges needs ownership or MANAGE on it, its schema or its catalog. USE_SCHEMA and MANAGE answer neither. The README, the connector doc and the limitations now state both requirements, as the catalog and schema sections do. The limitations also gain the table and volume keys, the one table securable that covers views, materialized views, metric views and streaming tables, and the privileges that are reported when already held but cannot be requested. Two corrections ride along. The ownership limitation said a workspace admins group is never shown; the principal resolver looks an owner name up against the account groups, and a workspace admins group is one, so it is shown when it resolves there. The limitation now says so, which is what the connector doc on this branch already said. And the opt-in is a C1 setting, so it does not gate the CLI: a local run with no selection syncs every type and fails on a service principal that cannot list every table and volume. The README now says to pass --sync-resource-types there. Co-Authored-By: Claude Opus 5 --- README.md | 27 +++++++++++++++++++-------- docs/docs-info.md | 2 +- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b4920b72..4cfadb6b 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,11 @@ Metastores, catalogs, schemas, tables, and volumes are Unity Catalog securables and are opt-in: each type stays off until it is enabled for the connector in ConductorOne. They use the existing OAuth credentials and configuration fields. +The opt-in is a ConductorOne setting, so it does not apply to the CLI. A local +run with no selection syncs every type, and it fails on a service principal that +cannot list every table and volume. Pass `--sync-resource-types` to limit it, for +example `--sync-resource-types account,user,group,metastore,catalog,schema`. + ## Authentication OAuth is the only authentication method: an account-level service principal's @@ -302,14 +307,9 @@ a workspace binding or visibility change. Requests can fail with The service principal must own the metastore, or be assigned **Admin** directly on at least one running workspace attached to it. Admin inherited only through a -group is not enough. The connector reads a metastore only through a workspace -that passes that check. Listing every schema takes ownership of the catalog, or -`MANAGE`, `USE_SCHEMA`, or `ALL_PRIVILEGES` on that catalog. Listing every table -or volume needs more: ownership of the metastore, the catalog, or the schema, -direct Admin when the workspace admins group owns the catalog, or -`ALL_PRIVILEGES` on the catalog. `MANAGE` and `USE_SCHEMA` are not enough for -tables or volumes. Databricks answers an under-permissioned read with HTTP 200 -and only the rows the caller may see. The connector refuses that read, so the +group is not enough. Databricks answers an under-permissioned read with HTTP 200 +and only the rows the caller may see, so the connector reads a metastore only +through a workspace that passes that check. When no attached workspace does, the sync fails and ConductorOne keeps the grants it already has. For a catalog's full grants list, the service principal must own the metastore @@ -330,6 +330,17 @@ catalog each return every schema, and neither reads their privileges. A service principal holding only one of them syncs its schemas and then fails on their grants. +For a table's or a volume's full grants list, and to grant or revoke one of +their privileges, the service principal must own the metastore, the catalog, the +schema or the object itself, hold `MANAGE` on the object, its schema or its +catalog, or have Admin assigned directly on the workspace whose admins group +owns the catalog. Listing them is a separate right, and a stricter one than +listing the schemas: ownership of the metastore, the catalog or the schema, that +same Admin standing, or `ALL_PRIVILEGES` on the catalog. `USE_SCHEMA` and +`MANAGE` return every schema and neither returns every table or volume, so a +service principal holding only one of them syncs the schemas and then fails on +their children. + The connector requests the `all-apis` OAuth scope. Each resource type declares the scopes, roles and privileges it needs, so `./baton-databricks capabilities` reports them per type. diff --git a/docs/docs-info.md b/docs/docs-info.md index 75fb63d6..73bd0bae 100644 --- a/docs/docs-info.md +++ b/docs/docs-info.md @@ -57,7 +57,7 @@ While developing the connector, please fill out this form. This information is n - **Direct grants only.** Databricks inherits privileges downward. The connector reports the assignments stored on the object. The inheritance endpoint does not return principal IDs. - **Full grants visibility is required.** Metastore ownership or direct workspace Admin permits metastore reads. Catalog reads require one of the catalog permissions listed above; workspace Admin applies only when that workspace's admins group owns the catalog. Admin inherited through a group does not count. The sync fails instead of reporting a partial list, so C1 keeps the grants it already has. -- **Ownership is read-only.** It is shown and cannot be granted or revoked. An owner that is not an account identity (`System user`, `_workspace_admins_…`) is not shown. +- **Ownership is read-only.** It is shown and cannot be granted or revoked. An owner that does not resolve to an account identity, such as `System user`, is not shown. A workspace admins group is shown when it resolves as an account group. - **No `MANAGE` or `ALL_PRIVILEGES` on a metastore.** Databricks rejects both, so they are not offered. - **Read through one running workspace.** Privileges are not served on the account plane. The connector reads each metastore through the first running workspace in scope where it can see every privilege. With no running workspace in scope, the metastore is not synced and ConductorOne removes it. A workspace that is still provisioning fails the sync until it is `RUNNING`. An unreadable assignment list fails the sync and ConductorOne keeps the metastore. - **`workspaces` scopes metastores.** An allowlist in which every name is either missing from the account or listed in `databricks-exclude-workspaces` fails at credential validation, before any sync, with one error that names both causes. From 4c26675ce21795dfcb5d4b7a768ec1e04fa709b7 Mon Sep 17 00:00:00 2001 From: Mateo Hernandez Date: Tue, 17 Nov 2026 10:37:06 -0300 Subject: [PATCH 9/9] fix(databricks): confirm the parent before believing a table or volume is gone A 404 on the permissions endpoint decided whether Grants reports no grants and Revoke answers GrantAlreadyRevoked. The table level took SCHEMA_DOES_NOT_EXIST and CATALOG_DOES_NOT_EXIST as proof, and the volume level took those plus the generic RESOURCE_DOES_NOT_EXIST after reading the volume through the same workspace. Every one of those codes also comes back from a workspace that cannot serve the catalog, and the sync path reads through the workspace the last sync wrote on the profile, so a binding that changed since then made a revoke report success while the privilege was still held, and emptied the securable's grants in C1. The parent is now read first through that same workspace. Only a schema it answers 200 for makes the securable's own 404 proof; the securable is then read with GET /tables/{full_name} or GET /volumes/{full_name}, and a 404 there is a deletion. Any other answer, from either read, leaves the absence unproven, so the caller refuses rather than reporting an empty list. The parent syncer walks every workspace and removes the parent with its children when it is really gone. Only a code naming the securable itself stays unambiguous, which is TABLE_DOES_NOT_EXIST and nothing on a volume: Databricks never returns VOLUME_DOES_NOT_EXIST. The confirmation costs two reads on a 404 and none on the happy path. The write path already had this discipline after the live-route change: liveChildRoute has the chosen workspace serve the catalog and answer 200 for the schema before the leaf is read, so a parent code there is a deletion between two reads. The comments now say so. schema_child_gone_test.go pins the contract in five cases for both levels: parent served plus a leaf 404 is gone, parent served plus a leaf 200 is not, and a parent that answers 404, answers 403, or a leaf read that fails leaves it unproven with no leaf read attempted when the parent did not answer. A full tenant sync still exits 0 with the same counts as before the change: 871 grants, 7 on the table and 6 on the volume. Co-Authored-By: Claude Opus 5 --- pkg/connector/schema_child.go | 40 ++++++++ pkg/connector/schema_child_gone_test.go | 126 ++++++++++++++++++++++++ pkg/connector/table.go | 29 +++++- pkg/connector/volume.go | 39 ++++---- 4 files changed, 210 insertions(+), 24 deletions(-) create mode 100644 pkg/connector/schema_child_gone_test.go diff --git a/pkg/connector/schema_child.go b/pkg/connector/schema_child.go index bdf6ad80..2f395c1b 100644 --- a/pkg/connector/schema_child.go +++ b/pkg/connector/schema_child.go @@ -519,3 +519,43 @@ func childEntitlementOnLiveRoute( return copyEntitlement, nil } + +// schemaChildIsGone settles a 404 that a missing table or volume and a workspace +// that cannot serve its catalog both answer with. The parent is read first +// through that same workspace: only a schema it answers 200 for makes the +// securable's own 404 proof of deletion. Any other answer leaves the absence +// unproven, which is what keeps C1 from removing what it already has. +func schemaChildIsGone( + ctx context.Context, + client *databricks.Client, + noun, fullName, workspace string, + annos *annotations.Annotations, + readSecurable func(ctx context.Context, workspace, fullName string) (*v2.RateLimitDescription, error), +) (bool, error) { + cut := strings.LastIndex(fullName, schemaNameSeparator) + if cut <= 0 { + return false, status.Errorf(codes.InvalidArgument, + "databricks-connector: %s %s has no parent schema in its name", noun, fullName) + } + schemaFullName := fullName[:cut] + + _, rateLimit, err := client.GetSchema(ctx, workspace, schemaFullName) + noteRateLimit(annos, rateLimit) + if err != nil { + return false, fmt.Errorf( + "databricks-connector: workspace %s does not answer for schema %s, so a %s missing there cannot be told from one it cannot serve: %w", + workspace, schemaFullName, noun, err) + } + + rateLimit, err = readSecurable(ctx, workspace, fullName) + noteRateLimit(annos, rateLimit) + switch { + case err == nil: + return false, nil + case apiStatus(err) == http.StatusNotFound: + // The workspace serves the parent and still has no such securable. + return true, nil + } + + return false, err +} diff --git a/pkg/connector/schema_child_gone_test.go b/pkg/connector/schema_child_gone_test.go new file mode 100644 index 00000000..3ba20c3e --- /dev/null +++ b/pkg/connector/schema_child_gone_test.go @@ -0,0 +1,126 @@ +package connector + +import ( + "context" + "io" + "net/http" + "net/http/httptest" + "net/url" + "testing" + + "github.com/conductorone/baton-databricks/pkg/databricks" + "github.com/conductorone/baton-sdk/pkg/annotations" +) + +// TestSchemaChildGoneNeedsTheParentServed pins the rule that decides whether C1 +// keeps or removes a table's and a volume's grants: a 404 is proof of deletion +// only from a workspace that has just answered 200 for the parent schema. A +// workspace that cannot serve the catalog answers the same codes, so believing +// one of those empties a securable that still exists. +func TestSchemaChildGoneNeedsTheParentServed(t *testing.T) { + t.Setenv("BATON_HTTP_CACHE_TTL", "0") + + const ( + schemaPath = "/api/2.1/unity-catalog/schemas/main.sales" + tablePath = "/api/2.1/unity-catalog/tables/main.sales.orders" + volumePath = "/api/2.1/unity-catalog/volumes/main.sales.files" + ) + + leafBodies := map[string]string{ + tablePath: `{"name":"orders","full_name":"main.sales.orders","owner":"eng","table_type":"MANAGED"}`, + volumePath: `{"name":"files","full_name":"main.sales.files","owner":"eng","volume_type":"MANAGED"}`, + } + + for _, tc := range []struct { + name string + schemaStatus int + leafStatus int + wantGone bool + wantErr bool + }{ + {name: "parent served and the securable is absent", schemaStatus: 200, leafStatus: 404, wantGone: true}, + {name: "parent served and the securable answers", schemaStatus: 200, leafStatus: 200, wantGone: false}, + {name: "parent not served, so the absence is unproven", schemaStatus: 404, leafStatus: 404, wantGone: false, wantErr: true}, + {name: "parent refused, so the absence is unproven", schemaStatus: 403, leafStatus: 404, wantGone: false, wantErr: true}, + {name: "parent served and the securable read fails", schemaStatus: 200, leafStatus: 500, wantGone: false, wantErr: true}, + } { + t.Run(tc.name, func(t *testing.T) { + var leafReads int + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case schemaPath: + if tc.schemaStatus != http.StatusOK { + w.WriteHeader(tc.schemaStatus) + _, _ = io.WriteString(w, `{"error_code":"CATALOG_DOES_NOT_EXIST","message":"no"}`) + + return + } + _, _ = io.WriteString(w, `{"name":"sales","full_name":"main.sales","owner":"eng"}`) + case tablePath, volumePath: + leafReads++ + if tc.leafStatus != http.StatusOK { + w.WriteHeader(tc.leafStatus) + _, _ = io.WriteString(w, `{"error_code":"RESOURCE_DOES_NOT_EXIST","message":"no"}`) + + return + } + _, _ = io.WriteString(w, leafBodies[r.URL.Path]) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(srv.Close) + + client := newGoneCheckClient(t, srv.URL) + annos := annotations.Annotations{} + + for _, probe := range []struct { + noun string + fn func(context.Context, *databricks.Client, string, string, *annotations.Annotations) (bool, error) + full string + }{ + {"table", tableIsGone, "main.sales.orders"}, + {"volume", volumeIsGone, "main.sales.files"}, + } { + gone, err := probe.fn(context.Background(), client, probe.full, testWorkspaceId, &annos) + if gone != tc.wantGone { + t.Errorf("%s: gone = %v, want %v", probe.noun, gone, tc.wantGone) + } + if (err != nil) != tc.wantErr { + t.Errorf("%s: err = %v, want error %v", probe.noun, err, tc.wantErr) + } + } + + if tc.schemaStatus != http.StatusOK && leafReads != 0 { + t.Errorf("read the securable %d times without the parent served, want 0", leafReads) + } + }) + } +} + +// TestTableGoneCodeNamingTheTableNeedsNoConfirmation keeps the one unambiguous +// code cheap: it names the table, so no extra read runs. +func TestTableGoneCodeNamingTheTableNeedsNoConfirmation(t *testing.T) { + if got := tableLevel(tablePrivileges).goneErrorCodes; len(got) != 1 || got[0] != errorCodeTableGone { + t.Fatalf("table goneErrorCodes = %v, want only %q", got, errorCodeTableGone) + } + if got := volumeLevel.goneErrorCodes; len(got) != 0 { + t.Fatalf("volume goneErrorCodes = %v, want none: Databricks never names a missing volume", got) + } +} + +func newGoneCheckClient(t *testing.T, serverURL string) *databricks.Client { + t.Helper() + target, err := url.Parse(serverURL) + if err != nil { + t.Fatalf("parse server url: %v", err) + } + httpClient := &http.Client{Transport: &hostRewriter{target: target}} + client, err := databricks.NewClient(context.Background(), httpClient, + testHostname, testAccountHost, testAccountId, "", &databricks.NoAuth{}, nil) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + + return client +} diff --git a/pkg/connector/table.go b/pkg/connector/table.go index d36e20f3..c840379e 100644 --- a/pkg/connector/table.go +++ b/pkg/connector/table.go @@ -155,12 +155,32 @@ func tableLevel(privileges []string) securableLevel { trustRemedy: tableTrustRemedy, permissionsName: tablePermissionsName, trusts: tableGrantsAreTrustworthy, - // Dropping a parent drops the table with it, so any of the three codes - // means there is no table left to revoke on. - goneErrorCodes: []string{errorCodeTableGone, errorCodeSchemaGone, errorCodeCatalogGone}, + // Only a code naming the table itself is proof. Dropping a parent does drop + // the table with it, but a workspace that cannot serve the catalog answers + // with the parent's code too, so those two are settled by reading the parent + // and then the table through that same workspace. + goneErrorCodes: []string{errorCodeTableGone}, + ambiguousGoneCodes: []string{errorCodeSchemaGone, errorCodeCatalogGone}, + confirmGone: tableIsGone, } } +// tableIsGone confirms the parent schema through the same workspace before it +// believes a 404 that named a parent rather than the table. +func tableIsGone( + ctx context.Context, + client *databricks.Client, + fullName, workspace string, + annos *annotations.Annotations, +) (bool, error) { + return schemaChildIsGone(ctx, client, "table", fullName, workspace, annos, + func(ctx context.Context, workspace, fullName string) (*v2.RateLimitDescription, error) { + _, rateLimit, err := client.GetTable(ctx, workspace, fullName) + + return rateLimit, err + }) +} + func tableResource( table *databricks.Table, metastoreID, catalogName, schemaName string, @@ -403,6 +423,9 @@ func (b *tableBuilder) entitlementOnLiveRoute( table, rateLimit, err := b.client.GetTable(ctx, route.Workspace, fullName) noteRateLimit(annos, rateLimit) if err != nil { + // liveChildRoute has just had this workspace serve the catalog and + // answer 200 for the schema, so a parent code here is a deletion + // between the two reads rather than a workspace that cannot route. if databricks.IsSecurableGone(err, errorCodeTableGone, errorCodeSchemaGone, errorCodeCatalogGone) { return nil, errSecurableGone } diff --git a/pkg/connector/volume.go b/pkg/connector/volume.go index b03a32f2..80e7ddb5 100644 --- a/pkg/connector/volume.go +++ b/pkg/connector/volume.go @@ -56,33 +56,30 @@ var volumeLevel = securableLevel{ trustRemedy: volumeTrustRemedy, permissionsName: volumePermissionsName, trusts: volumeGrantsAreTrustworthy, - // Dropping a parent drops the volume with it, and both parent codes name - // what is missing. The generic code does not, so it is confirmed first. - goneErrorCodes: []string{errorCodeSchemaGone, errorCodeCatalogGone}, - ambiguousGoneCodes: []string{errorCodeGenericGone}, + // No code names a missing volume: Databricks never returns + // VOLUME_DOES_NOT_EXIST, and the generic code and both parent codes all come + // back from a workspace that cannot serve the catalog too. None of them is + // proof on its own, so every one is confirmed before it is believed. + ambiguousGoneCodes: []string{errorCodeGenericGone, errorCodeSchemaGone, errorCodeCatalogGone}, confirmGone: volumeIsGone, } -// volumeIsGone reads the volume itself after the permissions endpoint answered -// the generic 404. The volume endpoint answers 200 while the volume exists, so -// anything but a gone code there means the permissions read failed for another -// reason and the absence stays unproven. +// volumeIsGone confirms the parent schema through the same workspace and then +// reads the volume itself. The volume endpoint answers 200 while the volume +// exists, so a 404 from a workspace that has just served the parent is the +// volume being gone rather than one it cannot reach. func volumeIsGone( ctx context.Context, client *databricks.Client, fullName, workspace string, annos *annotations.Annotations, ) (bool, error) { - volume, rateLimit, err := client.GetVolume(ctx, workspace, fullName) - noteRateLimit(annos, rateLimit) - if err == nil { - return volume == nil, nil - } - if databricks.IsSecurableGone(err, errorCodeGenericGone, errorCodeSchemaGone, errorCodeCatalogGone) { - return true, nil - } + return schemaChildIsGone(ctx, client, "volume", fullName, workspace, annos, + func(ctx context.Context, workspace, fullName string) (*v2.RateLimitDescription, error) { + _, rateLimit, err := client.GetVolume(ctx, workspace, fullName) - return false, err + return rateLimit, err + }) } type volumeBuilder struct { @@ -241,10 +238,10 @@ func (b *volumeBuilder) Revoke(ctx context.Context, revoked *v2.Grant) (annotati } // entitlementOnLiveRoute reads the volume itself after its parents, because its -// owner is one of the four the trust check short-circuits on. The read runs -// through a workspace that has just served the catalog and the schema, so a -// generic 404 here is the volume being gone rather than a workspace that cannot -// reach it. +// owner is one of the four the trust check short-circuits on. liveChildRoute has +// just had this workspace serve the catalog and answer 200 for the schema, so a +// 404 here — generic or naming a parent — is the volume being gone rather than a +// workspace that cannot route to it. func (b *volumeBuilder) entitlementOnLiveRoute( ctx context.Context, entitlement *v2.Entitlement,