Skip to content

Commit b355f7d

Browse files
authored
direct: Fix perpetual update on grants with ALL_PRIVILEGES (databricks#6064)
## Changes When a principal is granted `ALL_PRIVILEGES`, `normalizeAssignments` collapses its privilege list down to just `ALL_PRIVILEGES`, on both the config side and the value read back from the backend, so the two compare equal. ## Why Fixes databricks#6030. On the direct engine `bundle plan` reported `update` on every catalog/schema/volume `.grants` sub-resource that granted `ALL_PRIVILEGES`: the backend reports `ALL_PRIVILEGES` plus the concrete privileges it implies, which never matched the config's lone `ALL_PRIVILEGES`, and the deploy never converged. ## Tests New acceptance test `grants/schemas/all_privileges_coexist` (local + cloud, both engines); verified on aws-prod-ucws.
1 parent 2b377af commit b355f7d

11 files changed

Lines changed: 154 additions & 4 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes [#6030](https://github.com/databricks/cli/issues/6030): spurious `update` on catalog/schema/volume grants (direct engine); a principal granted `ALL_PRIVILEGES` no longer drifts when the backend also reports the concrete privileges it implies ([#6064](https://github.com/databricks/cli/pull/6064)).
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
bundle:
2+
name: schema-grants-all-privileges-coexist-$UNIQUE_NAME
3+
4+
resources:
5+
schemas:
6+
grants_schema:
7+
name: schema_all_priv_coexist_$UNIQUE_NAME
8+
catalog_name: main
9+
grants:
10+
# Config declares only ALL_PRIVILEGES for the principal. Reproduces
11+
# issue #6030: the backend also reports a concrete privilege for this
12+
# principal (added out of band below / materialized for owners), and
13+
# buildGrantChanges skips the ALL_PRIVILEGES removal, so the concrete
14+
# privilege is never reconciled and the plan reports perpetual update.
15+
- principal: $CURRENT_USER_NAME
16+
privileges:
17+
- ALL_PRIVILEGES
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
>>> [CLI] bundle plan
3+
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
>>> [CLI] bundle plan
3+
update schemas.grants_schema.grants
4+
5+
Plan: 0 to add, 1 to change, 0 to delete, 1 unchanged

‎acceptance/bundle/resources/grants/schemas/all_privileges_coexist/out.test.toml‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
>>> [CLI] bundle deploy
3+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/schema-grants-all-privileges-coexist-[UNIQUE_NAME]/default/files...
4+
Deploying resources...
5+
Updating deployment state...
6+
Deployment complete!
7+
8+
>>> [CLI] grants update schema main.schema_all_priv_coexist_[UNIQUE_NAME] --json @update.resolved.json
9+
10+
>>> [CLI] bundle deploy
11+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/schema-grants-all-privileges-coexist-[UNIQUE_NAME]/default/files...
12+
Deploying resources...
13+
Updating deployment state...
14+
Deployment complete!
15+
16+
>>> [CLI] bundle plan
17+
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
18+
19+
>>> errcode [CLI] bundle destroy --auto-approve
20+
The following resources will be deleted:
21+
delete resources.schemas.grants_schema
22+
23+
This action will result in the deletion of the following UC schemas. Any underlying data may be lost:
24+
delete resources.schemas.grants_schema
25+
26+
All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/schema-grants-all-privileges-coexist-[UNIQUE_NAME]/default
27+
28+
Deleting files...
29+
Destroy complete!
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
SCHEMA_FULL_NAME=main.schema_all_priv_coexist_$UNIQUE_NAME
2+
3+
envsubst < databricks.yml.tmpl > databricks.yml
4+
5+
cleanup() {
6+
trace errcode $CLI bundle destroy --auto-approve
7+
rm -f out.requests.txt
8+
}
9+
trap cleanup EXIT
10+
11+
trace $CLI bundle deploy
12+
13+
# Out of band, grant a concrete privilege in addition to the ALL_PRIVILEGES that
14+
# the config declares for the same principal. This reproduces issue #6030: the
15+
# backend then reports both ALL_PRIVILEGES and the concrete privilege.
16+
envsubst < update.json > update.resolved.json
17+
trace $CLI grants update schema "$SCHEMA_FULL_NAME" --json @update.resolved.json > /dev/null
18+
19+
# The first plan differs between engines: the direct engine treats ALL_PRIVILEGES
20+
# as implying every concrete privilege, so it reports no drift (issue #6030),
21+
# while terraform revokes the extra privilege to match the config exactly.
22+
trace $CLI bundle plan > out.plan.$DATABRICKS_BUNDLE_ENGINE.txt 2>&1
23+
24+
# After deploying, both engines converge: the follow-up plan reports no changes.
25+
trace $CLI bundle deploy
26+
trace $CLI bundle plan
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ignore = ['update.resolved.json']
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"changes": [
3+
{
4+
"principal": "$CURRENT_USER_NAME",
5+
"add": ["USE_SCHEMA"]
6+
}
7+
]
8+
}

‎bundle/direct/dresources/grants.go‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ func PrepareGrantsInputConfig(inputConfig any, node string) (*structvar.StructVa
4848
return nil, fmt.Errorf("expected *[]catalog.PrivilegeAssignment, got %T", inputConfig)
4949
}
5050

51-
// Backend sorts privileges, so we sort here as well.
52-
for i := range *grantsPtr {
53-
slices.Sort((*grantsPtr)[i].Privileges)
54-
}
51+
// Normalize the same way as DoRead (sort, collapse ALL_PRIVILEGES) so the
52+
// config and the value read back compare equal.
53+
normalizeAssignments(*grantsPtr)
5554

5655
return &structvar.StructVar{
5756
Value: &GrantsState{
@@ -222,9 +221,28 @@ func (r *ResourceGrants) listGrants(ctx context.Context, securableType, fullName
222221
}
223222
pageToken = resp.NextPageToken
224223
}
224+
// Normalize the same way as the config side (sort, collapse ALL_PRIVILEGES)
225+
// so the two compare equal and we don't report false drift.
226+
normalizeAssignments(assignments)
225227
return assignments, nil
226228
}
227229

230+
// normalizeAssignments sorts each assignment's privileges (the backend sorts
231+
// them, so we match that) and collapses a principal holding ALL_PRIVILEGES down
232+
// to just ALL_PRIVILEGES. The collapse is applied to both the config and read
233+
// sides, so config granting only ALL_PRIVILEGES matches a backend that reports
234+
// ALL_PRIVILEGES plus the concrete privileges it implies, instead of reporting a
235+
// perpetual update.
236+
func normalizeAssignments(assignments []catalog.PrivilegeAssignment) {
237+
for i := range assignments {
238+
if slices.Contains(assignments[i].Privileges, catalog.PrivilegeAllPrivileges) {
239+
assignments[i].Privileges = []catalog.Privilege{catalog.PrivilegeAllPrivileges}
240+
continue
241+
}
242+
slices.Sort(assignments[i].Privileges)
243+
}
244+
}
245+
228246
func extractGrantResourceType(node string) (string, error) {
229247
rest, ok := strings.CutPrefix(node, "resources.")
230248
if !ok {

0 commit comments

Comments
 (0)