diff --git a/.nextchanges/air/7714.md b/.nextchanges/air/7714.md new file mode 100644 index 00000000000..0e2678dbc1b --- /dev/null +++ b/.nextchanges/air/7714.md @@ -0,0 +1 @@ +* `databricks air run`, `air list` and `air cancel --all` fail with a clear error when the caller is an assumed group, since they act as a user. ([#7714](https://github.com/databricks/cli/pull/7714)) diff --git a/.nextchanges/bundles/7714-group-principal.md b/.nextchanges/bundles/7714-group-principal.md new file mode 100644 index 00000000000..d6ea48d01d0 --- /dev/null +++ b/.nextchanges/bundles/7714-group-principal.md @@ -0,0 +1 @@ +* Bundle commands fail with a clear error when the caller is an assumed group, because they deploy as a user or service principal. ([#7714](https://github.com/databricks/cli/pull/7714)) diff --git a/.nextchanges/bundles/7714.md b/.nextchanges/bundles/7714.md new file mode 100644 index 00000000000..32ce445d901 --- /dev/null +++ b/.nextchanges/bundles/7714.md @@ -0,0 +1 @@ +* `bundle validate` and `bundle summary` no longer include `current_user.roles` or `current_user.entitlements`. In `current_user`, `name` now holds the given and family names, `displayName` is those names joined with a space, `emails` holds the user name, `active` comes from the account status, and `groups` lists the transitive parent groups with only `value` and `display`. ([#7714](https://github.com/databricks/cli/pull/7714)) diff --git a/.nextchanges/cli/7714.md b/.nextchanges/cli/7714.md new file mode 100644 index 00000000000..d77a4f3156f --- /dev/null +++ b/.nextchanges/cli/7714.md @@ -0,0 +1 @@ +* `databricks auth describe` works when the caller is an assumed group. ([#7714](https://github.com/databricks/cli/pull/7714)) diff --git a/acceptance/air/cancel/test.toml b/acceptance/air/cancel/test.toml index ac19a88d6ad..09bb7d57a3a 100644 --- a/acceptance/air/cancel/test.toml +++ b/acceptance/air/cancel/test.toml @@ -22,7 +22,7 @@ Pattern = "POST /api/2.2/jobs/runs/cancel" Response.Body = '{}' # Jobs runs/list backs `cancel --all`: two active AIR runs for the current user -# (tester@databricks.com, from the built-in scim/v2/Me handler). +# (tester@databricks.com, from the built-in identity/me handler). [[Server]] Pattern = "GET /api/2.2/jobs/runs/list" Response.Body = ''' diff --git a/acceptance/air/list/test.toml b/acceptance/air/list/test.toml index 009458a44e4..81b9e306314 100644 --- a/acceptance/air/list/test.toml +++ b/acceptance/air/list/test.toml @@ -10,7 +10,7 @@ Response.Body = '' # `air list` reads Jobs runs/list directly (expand_tasks), filtering to AIR runs. # The current runs carry an ai_runtime_task (not modeled by the typed SDK), which # is why the CLI parses the response raw. Both runs belong to the default user -# (tester@databricks.com, from the built-in scim/v2/Me handler). The second run +# (tester@databricks.com, from the built-in identity/me handler). The second run # has the shape of the official multi-task DAB (a notebook prep task before the # ai_runtime_task) and must be listed from its AIR task. The third run has two # AI Runtime tasks that ended differently: it lists as two rows, each naming its diff --git a/acceptance/auth/bundle_default_profile/output.txt b/acceptance/auth/bundle_default_profile/output.txt index f8e8a3c0062..557b4f3eaac 100644 --- a/acceptance/auth/bundle_default_profile/output.txt +++ b/acceptance/auth/bundle_default_profile/output.txt @@ -11,7 +11,7 @@ >>> errcode [CLI] bundle validate -p other -o json Warn: [hostmetadata] failed to fetch host metadata for https://other.test, will skip for 1m0s -Error: Get "https://other.test/api/2.0/preview/scim/v2/Me": (redacted) +Error: Get "https://other.test/api/2.0/identity/me": (redacted) Exit code: 1 @@ -31,7 +31,7 @@ Exit code: 1 === Bundle with workspace.profile: pinned profile wins over default_profile >>> errcode [CLI] bundle validate -o json -Error: Get "https://other.test/api/2.0/preview/scim/v2/Me": (redacted) +Error: Get "https://other.test/api/2.0/identity/me": (redacted) Exit code: 1 diff --git a/acceptance/auth/bundle_default_profile/test.toml b/acceptance/auth/bundle_default_profile/test.toml index 5d5b0283f42..31a933f5d06 100644 --- a/acceptance/auth/bundle_default_profile/test.toml +++ b/acceptance/auth/bundle_default_profile/test.toml @@ -9,3 +9,7 @@ Ignore = [ [[Repls]] Old = 'Get "https://other.test/api/2.0/preview/scim/v2/Me\??": .*' New = 'Get "https://other.test/api/2.0/preview/scim/v2/Me": (redacted)' + +[[Repls]] +Old = 'Get "https://other.test/api/2.0/identity/me": .*' +New = 'Get "https://other.test/api/2.0/identity/me": (redacted)' diff --git a/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json b/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json index 6d538af432a..81747e50a45 100644 --- a/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json +++ b/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json @@ -38,7 +38,20 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/test-pipeline-recreate/default/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt b/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt index a649ac03b5f..31aea7e2616 100644 --- a/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt +++ b/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt @@ -57,14 +57,14 @@ [ 0, 1, - 880 + 1009 ] ], "resources.jobs": [ [ 0, 1, - 889 + 1018 ] ], "resources.jobs.my_job": [ diff --git a/acceptance/bundle/python/experimental-compatibility/output.txt b/acceptance/bundle/python/experimental-compatibility/output.txt index 40a5754e118..a65ebdef2dc 100644 --- a/acceptance/bundle/python/experimental-compatibility/output.txt +++ b/acceptance/bundle/python/experimental-compatibility/output.txt @@ -50,14 +50,14 @@ [ 0, 1, - 825 + 954 ] ], "resources.jobs": [ [ 0, 1, - 834 + 963 ] ], "resources.jobs.my_job": [ diff --git a/acceptance/bundle/resources/jobs/request-summary/output.txt b/acceptance/bundle/resources/jobs/request-summary/output.txt index a05f10b342f..8a934a53bcf 100644 --- a/acceptance/bundle/resources/jobs/request-summary/output.txt +++ b/acceptance/bundle/resources/jobs/request-summary/output.txt @@ -6,7 +6,8 @@ Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged >>> print_requests GET /.well-known/databricks-config -GET /api/2.0/preview/scim/v2/Me +GET /api/2.0/identity/me +GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deployment.json GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/resources.json GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/terraform.tfstate diff --git a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt index 067706823b6..df938243ff7 100644 --- a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt +++ b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt @@ -14,7 +14,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { diff --git a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt index dcc90ffb748..9162d8cd412 100644 --- a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt +++ b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt @@ -42,7 +42,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { diff --git a/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt b/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt index 2d9c8b6521d..937c71a3259 100644 --- a/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt +++ b/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt @@ -31,7 +31,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "method": "GET", diff --git a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt index 64073379aa6..78850a81445 100644 --- a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt +++ b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt @@ -14,7 +14,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "method": "GET", @@ -27,7 +36,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { diff --git a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt index 91d2b39a321..ef0b8f0eb7d 100644 --- a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt +++ b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt @@ -28,7 +28,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "method": "GET", @@ -55,7 +64,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { diff --git a/acceptance/bundle/telemetry/deploy-workspace-folder-permissions/output.txt b/acceptance/bundle/telemetry/deploy-workspace-folder-permissions/output.txt index 32b813560a0..c45fd8fd743 100644 --- a/acceptance/bundle/telemetry/deploy-workspace-folder-permissions/output.txt +++ b/acceptance/bundle/telemetry/deploy-workspace-folder-permissions/output.txt @@ -97,18 +97,6 @@ state_path_is_shared false state_path_other true >>> [CLI] bundle deploy -t shared_users_can_manage -Recommendation: permissions section should explicitly include the current deployment identity '[USERNAME]' or one of its groups -If it is not included, CAN_MANAGE permissions are only applied if the present identity is used to deploy. - -Consider using a adding a top-level permissions section such as the following: - - permissions: - - user_name: [USERNAME] - level: CAN_MANAGE - -See https://docs.databricks.com/dev-tools/bundles/permissions.html to learn more about permission configuration. - in databricks.yml:47:7 - Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/shared_users_can_manage/files... Files: 1 uploaded, 0 deleted Resources: 0 created, 0 changed, 0 deleted, 0 unchanged diff --git a/acceptance/bundle/templates-machinery/helpers-error/test.toml b/acceptance/bundle/templates-machinery/helpers-error/test.toml index 3839635db08..faa704eb91b 100644 --- a/acceptance/bundle/templates-machinery/helpers-error/test.toml +++ b/acceptance/bundle/templates-machinery/helpers-error/test.toml @@ -1,6 +1,7 @@ Badness = '''(minor) error message is not great: executing "" at : error calling user_name:''' +# The 400 keeps the golden error. The 500 retry path is not covered by this test. [[Server]] -Pattern = "GET /api/2.0/preview/scim/v2/Me" +Pattern = "GET /api/2.0/identity/me" Response.Body = '{}' -Response.StatusCode = 500 +Response.StatusCode = 400 diff --git a/acceptance/bundle/user_agent/output.txt b/acceptance/bundle/user_agent/output.txt index cf26d5ea549..8551f9c5f38 100644 --- a/acceptance/bundle/user_agent/output.txt +++ b/acceptance/bundle/user_agent/output.txt @@ -1,4 +1,5 @@ -OK deploy /api/2.0/preview/scim/v2/Me engine/direct +OK deploy /api/2.0/identity/me engine/direct +OK deploy /api/2.0/identity/me/transitive-parent-groups engine/direct OK deploy /api/2.0/workspace/export engine/direct OK deploy /api/2.0/workspace/export engine/direct OK deploy /api/2.0/workspace/export engine/direct @@ -20,7 +21,8 @@ OK deploy /api/2.0/workspace/mkdirs engine/direct OK deploy /api/2.0/workspace/mkdirs engine/direct OK deploy /api/2.1/unity-catalog/schemas engine/direct MISS deploy /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -OK destroy /api/2.0/preview/scim/v2/Me engine/direct +OK destroy /api/2.0/identity/me engine/direct +OK destroy /api/2.0/identity/me/transitive-parent-groups engine/direct OK destroy /api/2.0/workspace/export engine/direct OK destroy /api/2.0/workspace/export engine/direct OK destroy /api/2.1/unity-catalog/schemas/mycatalog.myschema engine/direct @@ -33,27 +35,32 @@ OK destroy /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bu OK destroy /api/2.0/workspace/delete engine/direct OK destroy /api/2.0/workspace/delete engine/direct MISS destroy /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -OK plan /api/2.0/preview/scim/v2/Me engine/direct +OK plan /api/2.0/identity/me engine/direct +OK plan /api/2.0/identity/me/transitive-parent-groups engine/direct OK plan /api/2.0/workspace/export engine/direct OK plan /api/2.0/workspace/export engine/direct OK plan /api/2.0/workspace/export engine/direct MISS plan /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -OK plan2 /api/2.0/preview/scim/v2/Me engine/direct +OK plan2 /api/2.0/identity/me engine/direct +OK plan2 /api/2.0/identity/me/transitive-parent-groups engine/direct OK plan2 /api/2.0/workspace/export engine/direct OK plan2 /api/2.0/workspace/export engine/direct OK plan2 /api/2.0/workspace/export engine/direct OK plan2 /api/2.1/unity-catalog/schemas/mycatalog.myschema engine/direct MISS plan2 /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -OK run /api/2.0/preview/scim/v2/Me engine/direct +OK run /api/2.0/identity/me engine/direct +OK run /api/2.0/identity/me/transitive-parent-groups engine/direct OK run /api/2.0/workspace/export engine/direct OK run /api/2.0/workspace/export engine/direct MISS run /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -OK summary /api/2.0/preview/scim/v2/Me engine/direct +OK summary /api/2.0/identity/me engine/direct +OK summary /api/2.0/identity/me/transitive-parent-groups engine/direct OK summary /api/2.0/preview/scim/v2/Me engine/direct OK summary /api/2.0/workspace/export engine/direct OK summary /api/2.0/workspace/export engine/direct MISS summary /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -MISS validate /api/2.0/preview/scim/v2/Me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' +MISS validate /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' +MISS validate /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' MISS validate /api/2.0/workspace/get-status 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' MISS validate /api/2.0/workspace/get-status 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' MISS validate /api/2.0/workspace/mkdirs 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' diff --git a/acceptance/bundle/user_agent/simple/out.requests.deploy.json b/acceptance/bundle/user_agent/simple/out.requests.deploy.json index aaca239572d..27bd9e3c57b 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.deploy.json +++ b/acceptance/bundle/user_agent/simple/out.requests.deploy.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none engine/direct auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { diff --git a/acceptance/bundle/user_agent/simple/out.requests.destroy.json b/acceptance/bundle/user_agent/simple/out.requests.destroy.json index 7a9f294f930..2737407ed80 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.destroy.json +++ b/acceptance/bundle/user_agent/simple/out.requests.destroy.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none engine/direct auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { diff --git a/acceptance/bundle/user_agent/simple/out.requests.plan.json b/acceptance/bundle/user_agent/simple/out.requests.plan.json index c9ec4d2edcf..b6ac45c5c0f 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.plan.json +++ b/acceptance/bundle/user_agent/simple/out.requests.plan.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none engine/direct auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { diff --git a/acceptance/bundle/user_agent/simple/out.requests.plan2.json b/acceptance/bundle/user_agent/simple/out.requests.plan2.json index 1abfbea97c9..2166ee462f7 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.plan2.json +++ b/acceptance/bundle/user_agent/simple/out.requests.plan2.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none engine/direct auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { diff --git a/acceptance/bundle/user_agent/simple/out.requests.run.json b/acceptance/bundle/user_agent/simple/out.requests.run.json index 7faea6514ab..0fb0e198050 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.run.json +++ b/acceptance/bundle/user_agent/simple/out.requests.run.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none engine/direct auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { diff --git a/acceptance/bundle/user_agent/simple/out.requests.summary.json b/acceptance/bundle/user_agent/simple/out.requests.summary.json index 31dc3daf318..24b332ef131 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.summary.json +++ b/acceptance/bundle/user_agent/simple/out.requests.summary.json @@ -5,10 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me", - "q": { - "excludedAttributes": "entitlements" - } + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none engine/direct auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { @@ -17,7 +23,10 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/preview/scim/v2/Me", + "q": { + "excludedAttributes": "entitlements" + } } { "headers": { diff --git a/acceptance/bundle/user_agent/simple/out.requests.validate.json b/acceptance/bundle/user_agent/simple/out.requests.validate.json index 5750f79f473..514e222fb97 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.validate.json +++ b/acceptance/bundle/user_agent/simple/out.requests.validate.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { diff --git a/acceptance/bundle/validate/group-principal/databricks.yml b/acceptance/bundle/validate/group-principal/databricks.yml new file mode 100644 index 00000000000..98a9ce6d2b2 --- /dev/null +++ b/acceptance/bundle/validate/group-principal/databricks.yml @@ -0,0 +1,2 @@ +bundle: + name: group-principal diff --git a/acceptance/bundle/validate/group-principal/out.test.toml b/acceptance/bundle/validate/group-principal/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/validate/group-principal/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/validate/group-principal/output.txt b/acceptance/bundle/validate/group-principal/output.txt new file mode 100644 index 00000000000..d3f20ba3dff --- /dev/null +++ b/acceptance/bundle/validate/group-principal/output.txt @@ -0,0 +1,6 @@ +Error: the current identity is an assumed group, but this command needs a user or service principal + +Name: group-principal +Target: default + +Found 1 error diff --git a/acceptance/bundle/validate/group-principal/script b/acceptance/bundle/validate/group-principal/script new file mode 100644 index 00000000000..26b424b480e --- /dev/null +++ b/acceptance/bundle/validate/group-principal/script @@ -0,0 +1 @@ +musterr $CLI bundle validate diff --git a/acceptance/bundle/validate/group-principal/test.toml b/acceptance/bundle/validate/group-principal/test.toml new file mode 100644 index 00000000000..ac4175e5ee6 --- /dev/null +++ b/acceptance/bundle/validate/group-principal/test.toml @@ -0,0 +1,3 @@ +# The caller is an assumed group. Bundle commands act as a user, so validate fails +# early instead of using the group name as a user name. +IsAssumedGroup = true diff --git a/acceptance/bundle/validate/identity-me-cloud/databricks.yml b/acceptance/bundle/validate/identity-me-cloud/databricks.yml new file mode 100644 index 00000000000..26d7f37b53f --- /dev/null +++ b/acceptance/bundle/validate/identity-me-cloud/databricks.yml @@ -0,0 +1,2 @@ +bundle: + name: identity-me-cloud diff --git a/acceptance/bundle/validate/identity-me-cloud/out.test.toml b/acceptance/bundle/validate/identity-me-cloud/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/validate/identity-me-cloud/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/validate/identity-me-cloud/output.txt b/acceptance/bundle/validate/identity-me-cloud/output.txt new file mode 100644 index 00000000000..458096907c0 --- /dev/null +++ b/acceptance/bundle/validate/identity-me-cloud/output.txt @@ -0,0 +1,2 @@ +[USERNAME] +true diff --git a/acceptance/bundle/validate/identity-me-cloud/script b/acceptance/bundle/validate/identity-me-cloud/script new file mode 100644 index 00000000000..0073d671602 --- /dev/null +++ b/acceptance/bundle/validate/identity-me-cloud/script @@ -0,0 +1,2 @@ +trace $CLI bundle validate -o json 2>LOG.validate | jq -r '.workspace.current_user | .userName, (.groups | map(.display) | contains(["users"]))' +contains.py '!Could not read the groups' < LOG.validate > /dev/null diff --git a/acceptance/bundle/validate/identity-me-cloud/test.toml b/acceptance/bundle/validate/identity-me-cloud/test.toml new file mode 100644 index 00000000000..03e36f135d4 --- /dev/null +++ b/acceptance/bundle/validate/identity-me-cloud/test.toml @@ -0,0 +1,3 @@ +# Runs on a real workspace too. The check is that the caller's parent groups are read without +# a warning and include the workspace users group, which every workspace identity belongs to. +Cloud = true diff --git a/acceptance/bundle/validate/immutable_workspace_paths/output.txt b/acceptance/bundle/validate/immutable_workspace_paths/output.txt index e7ed88a8087..ff3b19dd6b6 100644 --- a/acceptance/bundle/validate/immutable_workspace_paths/output.txt +++ b/acceptance/bundle/validate/immutable_workspace_paths/output.txt @@ -4,7 +4,20 @@ "workspace": { "artifact_path": "${resources.internal_immutable_snapshots.immutable.full_path}/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/job-references/output.txt b/acceptance/bundle/validate/job-references/output.txt index 8f88ccee391..22a6b7ebbc2 100644 --- a/acceptance/bundle/validate/job-references/output.txt +++ b/acceptance/bundle/validate/job-references/output.txt @@ -55,7 +55,20 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/foobar/default/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json index 258b6b938ab..637af50b367 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json @@ -76,7 +76,20 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/BUNDLE/dev/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json index 258b6b938ab..637af50b367 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json @@ -76,7 +76,20 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/BUNDLE/dev/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json index 3fe9aeea34a..285b887bbfd 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json @@ -70,7 +70,20 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/BUNDLE/dev/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json index 258b6b938ab..637af50b367 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json @@ -76,7 +76,20 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/BUNDLE/dev/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/workspace-local-groups/databricks.yml b/acceptance/bundle/validate/workspace-local-groups/databricks.yml new file mode 100644 index 00000000000..dda6e6d0f88 --- /dev/null +++ b/acceptance/bundle/validate/workspace-local-groups/databricks.yml @@ -0,0 +1,8 @@ +bundle: + name: workspace-local-groups + +permissions: + - group_name: admins + level: CAN_MANAGE + - group_name: users + level: CAN_VIEW diff --git a/acceptance/bundle/validate/workspace-local-groups/out.test.toml b/acceptance/bundle/validate/workspace-local-groups/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/validate/workspace-local-groups/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/validate/workspace-local-groups/output.txt b/acceptance/bundle/validate/workspace-local-groups/output.txt new file mode 100644 index 00000000000..5ad04d57180 --- /dev/null +++ b/acceptance/bundle/validate/workspace-local-groups/output.txt @@ -0,0 +1,25 @@ + +>>> [CLI] api post /api/2.0/preview/scim/v2/Groups --json {"displayName": "admins", "members": [{"value": "[USERID]"}]} + +>>> [CLI] bundle validate -o json +[ + "admins", + "users" +] + +>>> print_requests.py --get //api/2.0/identity/me/transitive-parent-groups +{ + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" +} + +>>> [CLI] bundle validate +Name: workspace-local-groups +Target: default +Workspace: + User: [USERNAME] + Path: /Workspace/Users/[USERNAME]/.bundle/workspace-local-groups/default + +Validation OK! + +>>> print_requests.py --get //api/2.0/identity/me/transitive-parent-groups diff --git a/acceptance/bundle/validate/workspace-local-groups/script b/acceptance/bundle/validate/workspace-local-groups/script new file mode 100644 index 00000000000..ed8ee16a355 --- /dev/null +++ b/acceptance/bundle/validate/workspace-local-groups/script @@ -0,0 +1,9 @@ +ME=$($CLI current-user me | jq -r .id) +MSYS_NO_PATHCONV=1 trace $CLI api post /api/2.0/preview/scim/v2/Groups --json "{\"displayName\": \"admins\", \"members\": [{\"value\": \"$ME\"}]}" > LOG.groups.admins + +trace $CLI bundle validate -o json | jq '.workspace.current_user.groups | map(.display)' +trace print_requests.py --get //api/2.0/identity/me/transitive-parent-groups + +# The groups come from the cache, so the second command reads no parent groups. +trace $CLI bundle validate +trace print_requests.py --get //api/2.0/identity/me/transitive-parent-groups diff --git a/acceptance/bundle/validate/workspace-local-groups/test.toml b/acceptance/bundle/validate/workspace-local-groups/test.toml new file mode 100644 index 00000000000..afc30ab11e4 --- /dev/null +++ b/acceptance/bundle/validate/workspace-local-groups/test.toml @@ -0,0 +1,4 @@ +# The current user belongs to the workspace-local groups admins and users. The script creates +# admins; the fake lists users for every identity, as a workspace does. Their CAN_MANAGE grant +# counts as manageable. +RecordRequests = true diff --git a/acceptance/cmd/auth/describe/account-host-with-workspace-id/test.toml b/acceptance/cmd/auth/describe/account-host-with-workspace-id/test.toml index 55541c52a5d..8a921507856 100644 --- a/acceptance/cmd/auth/describe/account-host-with-workspace-id/test.toml +++ b/acceptance/cmd/auth/describe/account-host-with-workspace-id/test.toml @@ -5,7 +5,7 @@ Ignore = [ # Simulate an account console host: it cannot serve workspace APIs, so the # workspace-side verification call fails even though the credentials are valid. [[Server]] -Pattern = "GET /api/2.0/preview/scim/v2/Me" +Pattern = "GET /api/2.0/identity/me" Response.Body = 'Unable to load OAuth Config' Response.StatusCode = 400 diff --git a/acceptance/cmd/auth/describe/identity-me-cloud/out.test.toml b/acceptance/cmd/auth/describe/identity-me-cloud/out.test.toml new file mode 100644 index 00000000000..c7c6f58ed6e --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-cloud/out.test.toml @@ -0,0 +1 @@ +Cloud = true diff --git a/acceptance/cmd/auth/describe/identity-me-cloud/output.txt b/acceptance/cmd/auth/describe/identity-me-cloud/output.txt new file mode 100644 index 00000000000..27a03fe6a21 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-cloud/output.txt @@ -0,0 +1,8 @@ + +=== auth describe resolves the current user through identity/me + +>>> [CLI] auth describe -o json +{ + "status": "success", + "username": "[USERNAME]" +} diff --git a/acceptance/cmd/auth/describe/identity-me-cloud/script b/acceptance/cmd/auth/describe/identity-me-cloud/script new file mode 100644 index 00000000000..39590e7c461 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-cloud/script @@ -0,0 +1,2 @@ +title "auth describe resolves the current user through identity/me" +trace $CLI auth describe -o json | jq '{status, username}' diff --git a/acceptance/cmd/auth/describe/identity-me-cloud/test.toml b/acceptance/cmd/auth/describe/identity-me-cloud/test.toml new file mode 100644 index 00000000000..c585363eb38 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-cloud/test.toml @@ -0,0 +1,6 @@ +# Runs on a real workspace too: auth describe must resolve the caller through +# identity/me there, not only against the testserver. +Cloud = true +Ignore = [ + "home" +] diff --git a/acceptance/cmd/auth/describe/identity-me-group/out.test.toml b/acceptance/cmd/auth/describe/identity-me-group/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-group/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/auth/describe/identity-me-group/output.txt b/acceptance/cmd/auth/describe/identity-me-group/output.txt new file mode 100644 index 00000000000..4076eb6af18 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-group/output.txt @@ -0,0 +1,18 @@ + +=== Describe succeeds with an assumed group and shows the group as the user + +>>> [CLI] auth describe +Host: [DATABRICKS_URL] +User: data-engineers +Authenticated with: pat +----- +Current configuration: + ✓ host: [DATABRICKS_URL] (from DATABRICKS_HOST environment variable) + ✓ workspace_id: [WORKSPACE_ID] + ✓ token: ******** (from DATABRICKS_TOKEN environment variable) + ✓ profile: default + ✓ databricks_cli_path: [CLI] + ✓ auth_type: pat + ✓ rate_limit: 1000000000 (from DATABRICKS_RATE_LIMIT environment variable) + ✓ cloud: AWS + ✓ discovery_url: [DATABRICKS_URL]/oidc/.well-known/oauth-authorization-server diff --git a/acceptance/cmd/auth/describe/identity-me-group/script b/acceptance/cmd/auth/describe/identity-me-group/script new file mode 100644 index 00000000000..0cd8e6235a1 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-group/script @@ -0,0 +1,2 @@ +title "Describe succeeds with an assumed group and shows the group as the user" +trace $CLI auth describe diff --git a/acceptance/cmd/auth/describe/identity-me-group/test.toml b/acceptance/cmd/auth/describe/identity-me-group/test.toml new file mode 100644 index 00000000000..ff2551b71d2 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-group/test.toml @@ -0,0 +1,6 @@ +Ignore = [ + "home" +] + +# The caller is an assumed group, which identity/me returns as the principal. +IsAssumedGroup = true diff --git a/acceptance/cmd/liteswap-target/output.txt b/acceptance/cmd/liteswap-target/output.txt index b8b59b78545..e20c89613b4 100644 --- a/acceptance/cmd/liteswap-target/output.txt +++ b/acceptance/cmd/liteswap-target/output.txt @@ -46,7 +46,7 @@ Workspace: Validation OK! ->>> print_requests.py --get //scim/v2/Me +>>> print_requests.py --get //identity/me { "headers": { "X-Databricks-Traffic-Id": [ @@ -54,7 +54,16 @@ Validation OK! ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "X-Databricks-Traffic-Id": [ + "testenv://liteswap/dms" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } === No header when unset diff --git a/acceptance/cmd/liteswap-target/script b/acceptance/cmd/liteswap-target/script index f69acefb698..c0003285c0c 100644 --- a/acceptance/cmd/liteswap-target/script +++ b/acceptance/cmd/liteswap-target/script @@ -8,7 +8,7 @@ trace print_requests.py --get //network-policies title "Bundle command" DATABRICKS_LITESWAP_TARGET=dms trace $CLI bundle validate -trace print_requests.py --get //scim/v2/Me +trace print_requests.py --get //identity/me title "No header when unset" trace $CLI clusters list diff --git a/acceptance/cmd/skills-invoked/output.txt b/acceptance/cmd/skills-invoked/output.txt index 50d0025864a..9370602eced 100644 --- a/acceptance/cmd/skills-invoked/output.txt +++ b/acceptance/cmd/skills-invoked/output.txt @@ -15,7 +15,7 @@ >>> [CLI] bundle validate ->>> print_requests.py --get //scim/v2/Me +>>> print_requests.py --get //identity/me === Invalid entries are dropped and the rest is normalized diff --git a/acceptance/cmd/skills-invoked/script b/acceptance/cmd/skills-invoked/script index dffdd34109b..d6724c2c110 100644 --- a/acceptance/cmd/skills-invoked/script +++ b/acceptance/cmd/skills-invoked/script @@ -14,7 +14,7 @@ contains.py " skill/databricks-core" < LOG.api > /dev/null title "Env var adds the skills from bundle commands" DATABRICKS_SKILLS_INVOKED=databricks-dabs trace $CLI bundle validate > /dev/null -trace print_requests.py --get //scim/v2/Me > LOG.bundle +trace print_requests.py --get //identity/me > LOG.bundle contains.py " skill/databricks-dabs" < LOG.bundle > /dev/null title "Invalid entries are dropped and the rest is normalized" diff --git a/acceptance/internal/config.go b/acceptance/internal/config.go index 6ea79a9578a..8dc3ccda4bf 100644 --- a/acceptance/internal/config.go +++ b/acceptance/internal/config.go @@ -59,6 +59,9 @@ type TestConfig struct { // If set, current user will be set to a service principal-like UUID instead of email (default is false) IsServicePrincipal *bool + // If set, the caller is an assumed group: identity/me returns the group as the principal (default is false) + IsAssumedGroup *bool + // List of additional replacements to apply on this test. // Old is a regexp, New is a replacement expression. Repls []testdiff.Replacement diff --git a/acceptance/internal/prepare_server.go b/acceptance/internal/prepare_server.go index 57904951238..52d89d29548 100644 --- a/acceptance/internal/prepare_server.go +++ b/acceptance/internal/prepare_server.go @@ -145,6 +145,9 @@ func PrepareServerAndClient(t *testing.T, config TestConfig, logRequests bool, o if isTruePtr(config.IsServicePrincipal) { token = testserver.ServicePrincipalTokenPrefix + tokenSuffix testUser = testserver.TestUserSP + } else if isTruePtr(config.IsAssumedGroup) { + token = testserver.AssumedGroupTokenPrefix + tokenSuffix + testUser = testserver.TestUser } else if staleOnceEnabled(testEnv) { // Use the eventual-consistency token so the testserver returns 404 on the // first GET after a create, matching real cloud propagation delays. @@ -216,7 +219,7 @@ func PrepareServerAndClient(t *testing.T, config TestConfig, logRequests bool, o } // For the purposes of replacements, use testUser for local runs. - // Note, users might have overridden /api/2.0/preview/scim/v2/Me but that should not affect the replacement: + // Note, users might have overridden /api/2.0/identity/me, but that should not affect the replacement: return cfg, testUser, strconv.Itoa(testserver.TestWorkspaceID) } diff --git a/bundle/bundle.go b/bundle/bundle.go index 9f686a7e466..3c0f9966fa3 100644 --- a/bundle/bundle.go +++ b/bundle/bundle.go @@ -229,6 +229,11 @@ type Bundle struct { // By default, caching is enabled. Set DATABRICKS_CACHE_ENABLED=false to disable caching. Cache *cache.Cache + // CurrentUserGroupsUnknown is set when PopulateCurrentUser could not read the groups of + // the current user, so a grant through a group cannot be ruled out. It lives here rather + // than on the config because config is converted to and from its dynamic form between mutators. + CurrentUserGroupsUnknown bool + Metrics Metrics } diff --git a/bundle/config/mutator/populate_current_user.go b/bundle/config/mutator/populate_current_user.go index a00234912fa..800829563c0 100644 --- a/bundle/config/mutator/populate_current_user.go +++ b/bundle/config/mutator/populate_current_user.go @@ -2,18 +2,35 @@ package mutator import ( "context" + "errors" "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/libs/cache" "github.com/databricks/cli/libs/diag" "github.com/databricks/cli/libs/iamutil" + "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/tags" "github.com/databricks/databricks-sdk-go/service/iam" ) type populateCurrentUser struct{} +// currentUserCacheKey keys the cached principal and groups. Version keeps entries +// written before the groups were cached, which have another shape, from being read. +type currentUserCacheKey struct { + Fingerprint bundle.UserFingerprint + Version int +} + +const currentUserCacheVersion = 2 + +// cachedCurrentUser is the principal with its groups, cached as one entry. +type cachedCurrentUser struct { + User *iam.User `json:"user"` + Groups []iam.ComplexValue `json:"groups"` +} + // PopulateCurrentUser sets the `current_user` property on the workspace. func PopulateCurrentUser() bundle.Mutator { return &populateCurrentUser{} @@ -28,19 +45,48 @@ func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) diag. return nil } w := b.WorkspaceClient(ctx) + key := currentUserCacheKey{Fingerprint: b.GetUserFingerprint(ctx), Version: currentUserCacheVersion} - fingerprint := b.GetUserFingerprint(ctx) - me, err := cache.GetOrCompute(ctx, b.Cache, fingerprint, func(ctx context.Context) (*iam.User, error) { - return iamutil.GetCurrentUser(ctx, w) + // The groups are part of the cached value, so a cache hit makes no identity/me or + // parent-groups request. A failed groups read is not cached, so the next command reads + // them again; the principal is kept for the fallback below. + var principal *iam.User + var groupsErr error + cached, err := cache.GetOrCompute(ctx, b.Cache, key, func(ctx context.Context) (cachedCurrentUser, error) { + me, err := iamutil.GetUserPrincipal(ctx, w) + if err != nil { + return cachedCurrentUser{}, err + } + principal = me + groups, err := iamutil.CurrentUserGroups(ctx, w) + if err != nil { + groupsErr = err + return cachedCurrentUser{}, err + } + return cachedCurrentUser{User: me, Groups: groups}, nil }) - if err != nil { + + user := cached.User + switch { + case err == nil: + user.Groups = cached.Groups + case groupsErr != nil && errors.Is(err, groupsErr): + // Not a warning: `bundle validate --strict` would fail on it. The permission + // recommendation is withheld through CurrentUserGroupsUnknown instead. + log.Infof(ctx, "Could not read the groups of the current user: %v", groupsErr) + user = principal + b.CurrentUserGroupsUnknown = true + case errors.Is(err, iamutil.ErrMeNotEnabled): + // diag.FromErr would show only the API fields, which drop this message. + return diag.Errorf("%v", err) + default: return diag.FromErr(err) } b.Config.Workspace.CurrentUser = &config.User{ - ShortName: iamutil.GetShortUserName(me), - DomainFriendlyName: iamutil.GetShortUserDomainFriendlyName(me), - User: me, + ShortName: iamutil.GetShortUserName(user), + DomainFriendlyName: iamutil.GetShortUserDomainFriendlyName(user), + User: user, } // Configure tagging object now that we know we have a valid client. diff --git a/bundle/config/mutator/populate_current_user_test.go b/bundle/config/mutator/populate_current_user_test.go index 1475055dc35..515f59475c0 100644 --- a/bundle/config/mutator/populate_current_user_test.go +++ b/bundle/config/mutator/populate_current_user_test.go @@ -1,9 +1,139 @@ package mutator import ( + "net/http" + "sync/atomic" "testing" + "time" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/libs/cache" + "github.com/databricks/cli/libs/diag" + "github.com/databricks/cli/libs/iamutil" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -func TestPopulateCurrentUser(t *testing.T) { - // We need to implement workspace client mocking to implement this test. +// newPopulateBundle returns a bundle whose workspace serves the default identity and +// membership in the groups named by groups. The handlers registered on server before +// this call take precedence, so a test can override one endpoint. +func newPopulateBundle(t *testing.T, server *testserver.Server, groups ...string) *bundle.Bundle { + testserver.AddDefaultHandlers(server) + server.AddGroups("test-token", groups, nil) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + b := &bundle.Bundle{Config: config.Root{}} + b.Cache = cache.NewCache(t.Context(), "user", time.Minute, &b.Metrics) + b.SetWorkpaceClient(w) + return b +} + +func TestPopulateCurrentUserSetsGroups(t *testing.T) { + b := newPopulateBundle(t, testserver.New(t), "zeta", "alpha") + + diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) + require.NoError(t, diags.Error()) + var names []string + for _, g := range b.Config.Workspace.CurrentUser.Groups { + names = append(names, g.Display) + } + // The fake lists the built-in users group for every identity. + assert.Equal(t, []string{"alpha", "users", "zeta"}, names) +} + +// Workspace-local groups are named by the parent-groups response, so admins and users +// resolve like account groups. +func TestPopulateCurrentUserSetsWorkspaceLocalGroups(t *testing.T) { + b := newPopulateBundle(t, testserver.New(t), "admins") + + diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) + require.NoError(t, diags.Error()) + var names []string + for _, g := range b.Config.Workspace.CurrentUser.Groups { + names = append(names, g.Display) + } + assert.Equal(t, []string{"admins", "users"}, names) +} + +// A failed parent-groups read leaves the groups empty and only logs, and the current user is still set. +func TestPopulateCurrentUserSkipsGroupsWhenReadFails(t *testing.T) { + server := testserver.New(t) + server.Handle("GET", "/api/2.0/identity/me/transitive-parent-groups", func(testserver.Request) any { + return testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}} + }) + b := newPopulateBundle(t, server, "testgroup") + + // The failure is logged, not a diagnostic, so `bundle validate --strict` still passes. + diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) + require.NoError(t, diags.Error()) + assert.Empty(t, diags) + assert.Nil(t, b.Config.Workspace.CurrentUser.Groups) + assert.True(t, b.CurrentUserGroupsUnknown) + assert.Equal(t, "tester@databricks.com", b.Config.Workspace.CurrentUser.UserName) +} + +// The groups are cached with the principal, so a second command with the same +// fingerprint reads neither identity/me nor the parent groups. +func TestPopulateCurrentUserCachesGroupsWithPrincipal(t *testing.T) { + server := testserver.New(t) + var meCalls, parentCalls atomic.Int32 + server.Handle("GET", "/api/2.0/identity/me", func(req testserver.Request) any { + meCalls.Add(1) + return req.Workspace.IdentityMe(req.Token) + }) + server.Handle("GET", "/api/2.0/identity/me/transitive-parent-groups", func(req testserver.Request) any { + parentCalls.Add(1) + return req.Workspace.IdentityMeTransitiveParentGroups() + }) + first := newPopulateBundle(t, server, "alpha") + + diags := bundle.Apply(t.Context(), first, PopulateCurrentUser()) + require.NoError(t, diags.Error()) + assert.Equal(t, int32(1), meCalls.Load()) + assert.Equal(t, int32(1), parentCalls.Load()) + + second := &bundle.Bundle{Config: config.Root{}} + second.Cache = first.Cache + second.SetWorkpaceClient(first.WorkspaceClient(t.Context())) + diags = bundle.Apply(t.Context(), second, PopulateCurrentUser()) + require.NoError(t, diags.Error()) + assert.Equal(t, int32(1), meCalls.Load()) + assert.Equal(t, int32(1), parentCalls.Load()) + require.Len(t, second.Config.Workspace.CurrentUser.Groups, 2) + assert.Equal(t, "alpha", second.Config.Workspace.CurrentUser.Groups[0].Display) + assert.Equal(t, "users", second.Config.Workspace.CurrentUser.Groups[1].Display) +} + +// An assumed group is not a user: bundle commands that deploy as the current user fail +// early with a clear error rather than using the group name as a user name. +func TestPopulateCurrentUserRejectsGroupPrincipal(t *testing.T) { + server := testserver.New(t) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return map[string]any{"group": map[string]any{"group_id": "3000033333", "group_name": "data-engineers"}} + }) + b := newPopulateBundle(t, server) + + diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) + require.Len(t, diags, 1) + assert.Equal(t, diag.Error, diags[0].Severity) + assert.Equal(t, iamutil.ErrGroupPrincipal.Error(), diags[0].Summary) + assert.Nil(t, b.Config.Workspace.CurrentUser) +} + +// A workspace without identity/me fails the command with the actionable message, not the raw server text. +func TestPopulateCurrentUserReportsIdentityMeNotEnabled(t *testing.T) { + server := testserver.New(t) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return testserver.Response{StatusCode: http.StatusNotFound, Body: map[string]any{"error_code": "ENDPOINT_NOT_FOUND", "message": "Me API is not enabled."}} + }) + b := newPopulateBundle(t, server, "testgroup") + + diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) + require.Error(t, diags.Error()) + assert.ErrorContains(t, diags.Error(), "the identity/me API is not enabled in this workspace") + assert.Nil(t, b.Config.Workspace.CurrentUser) } diff --git a/bundle/permissions/permission_diagnostics.go b/bundle/permissions/permission_diagnostics.go index 0e01f72a0f4..1ed3525092d 100644 --- a/bundle/permissions/permission_diagnostics.go +++ b/bundle/permissions/permission_diagnostics.go @@ -10,6 +10,7 @@ import ( "github.com/databricks/cli/libs/diag" "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/set" + "github.com/databricks/databricks-sdk-go/service/iam" ) type permissionDiagnostics struct{} @@ -39,6 +40,12 @@ func (m *permissionDiagnostics) Apply(ctx context.Context, b *bundle.Bundle) dia return nil } + // Without the groups, a grant through a group may already apply, so suggesting the + // identity could be wrong. The populate step has already logged that the groups could not be read. + if b.CurrentUserGroupsUnknown { + return nil + } + me := b.Config.Workspace.CurrentUser.User identityType := "user_name" if iamutil.IsServicePrincipal(me) { @@ -80,8 +87,7 @@ func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) { } currentUser := b.Config.Workspace.CurrentUser.UserName - targetPermissions := b.Config.Permissions - for _, p := range targetPermissions { + for _, p := range b.Config.Permissions { if p.Level != CAN_MANAGE { continue } @@ -91,7 +97,7 @@ func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) { continue } - if isGroupOfCurrentUser(b, p.GroupName) { + if p.GroupName != "" && isMemberOfGroup(b, p.GroupName) { canManageBundle = true continue } @@ -120,13 +126,10 @@ func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) { return canManageBundle, assistance } -func isGroupOfCurrentUser(b *bundle.Bundle, groupName string) bool { - currentUserGroups := b.Config.Workspace.CurrentUser.Groups - - for _, g := range currentUserGroups { - if g.Display == groupName { - return true - } - } - return false +// isMemberOfGroup reports whether the current user belongs to the group named name, +// by matching the name against the groups read in PopulateCurrentUser. +func isMemberOfGroup(b *bundle.Bundle, name string) bool { + return slices.ContainsFunc(b.Config.Workspace.CurrentUser.Groups, func(g iam.ComplexValue) bool { + return g.Display == name + }) } diff --git a/bundle/permissions/permission_diagnostics_test.go b/bundle/permissions/permission_diagnostics_test.go index e5a9214f437..3276b0bdb2d 100644 --- a/bundle/permissions/permission_diagnostics_test.go +++ b/bundle/permissions/permission_diagnostics_test.go @@ -1,6 +1,7 @@ package permissions_test import ( + "sync/atomic" "testing" "github.com/databricks/cli/bundle" @@ -8,12 +9,16 @@ import ( "github.com/databricks/cli/bundle/config/resources" "github.com/databricks/cli/bundle/permissions" "github.com/databricks/cli/libs/diag" + "github.com/databricks/cli/libs/iamutil" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/iam" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestPermissionDiagnosticsApplySuccess(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_MANAGE", UserName: "testuser@databricks.com"}, }) @@ -21,15 +26,27 @@ func TestPermissionDiagnosticsApplySuccess(t *testing.T) { require.NoError(t, diags.Error()) } +// When the groups could not be read, a grant through a group may already apply, so the +// recommendation to add the identity is withheld. +func TestPermissionDiagnosticsSkipsRecommendationWhenGroupsUnknown(t *testing.T) { + b := mockBundle(t, []resources.Permission{ + {Level: "CAN_VIEW", UserName: "testuser@databricks.com"}, + }) + require.NotEmpty(t, bundle.Apply(t.Context(), b, permissions.PermissionDiagnostics())) + + b.CurrentUserGroupsUnknown = true + require.Empty(t, bundle.Apply(t.Context(), b, permissions.PermissionDiagnostics())) +} + func TestPermissionDiagnosticsEmpty(t *testing.T) { - b := mockBundle(nil) + b := mockBundle(t, nil) diags := bundle.Apply(t.Context(), b, permissions.PermissionDiagnostics()) require.NoError(t, diags.Error()) } func TestPermissionDiagnosticsApplyFail(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_VIEW", UserName: "testuser@databricks.com"}, }) @@ -48,21 +65,139 @@ func TestPermissionDiagnosticsApplyFail(t *testing.T) { require.Contains(t, diags[0].Summary, expectedMsg) } -func mockBundle(permissions []resources.Permission) *bundle.Bundle { - return &bundle.Bundle{ +// parentGroupCalls counts the reads of the current user's parent groups. +type parentGroupCalls struct { + parents atomic.Int32 // GET /api/2.0/identity/me/transitive-parent-groups +} + +// mockBundle returns a bundle whose workspace has one group, testgroup, that the +// current user belongs to. +func mockBundle(t *testing.T, permissions []resources.Permission) *bundle.Bundle { + b, _ := mockBundleWithGroups(t, permissions, []string{"testgroup"}, nil) + return b +} + +// mockBundleWithGroups returns a bundle whose workspace creates the groups in member, +// which the current user belongs to, and the groups in nonMember, which it does not. +// The bundle's current user carries the groups resolved from the membership, as the +// populate mutator would set them. The returned counts cover only the parent-group +// reads the bundle makes after this call returns. +func mockBundleWithGroups(t *testing.T, permissions []resources.Permission, member, nonMember []string) (*bundle.Bundle, *parentGroupCalls) { + server := testserver.New(t) + calls := &parentGroupCalls{} + // Handlers registered before AddDefaultHandlers take precedence, so this one counts + // and then delegates to the fake. + server.Handle("GET", "/api/2.0/identity/me/transitive-parent-groups", func(req testserver.Request) any { + calls.parents.Add(1) + return req.Workspace.IdentityMeTransitiveParentGroups() + }) + testserver.AddDefaultHandlers(server) + server.AddGroups("test-token", member, nonMember) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + groups, err := iamutil.CurrentUserGroups(t.Context(), w) + require.NoError(t, err) + calls.parents.Store(0) + + b := &bundle.Bundle{ Config: config.Root{ Workspace: config.Workspace{ CurrentUser: &config.User{ User: &iam.User{ UserName: "testuser@databricks.com", DisplayName: "Test User", - Groups: []iam.ComplexValue{ - {Display: "testgroup"}, - }, + Groups: groups, }, }, }, Permissions: permissions, }, } + b.SetWorkpaceClient(w) + return b, calls +} + +// A group permission the current user cannot match, with no parent groups in the +// workspace, is reported as not manageable and names the group as a contact. +func TestPermissionDiagnosticsNoGroupMembership(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "testgroup"}, + }, nil, nil) + + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.ErrorContains(t, diags.Error(), "may include: testgroup.") +} + +// A group that exists in the workspace but that the current user is not a member of +// does not grant CAN_MANAGE, and is still named as a contact. +func TestPermissionDiagnosticsGroupExistsNotMember(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "otherteam"}, + }, nil, []string{"otherteam"}) + + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.ErrorContains(t, diags.Error(), "may include: otherteam.") +} + +// A configured group the workspace does not have is named as a contact, not matched. +func TestPermissionDiagnosticsGroupDoesNotExist(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "nosuchgroup"}, + }, nil, nil) + + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.ErrorContains(t, diags.Error(), "may include: nosuchgroup.") +} + +// A group the current user belongs to, matched by name, grants CAN_MANAGE. The report +// is then a stale permission set, not a missing grant. +func TestPermissionDiagnosticsGroupMember(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "testgroup"}, + }, []string{"testgroup"}, nil) + + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.Equal(t, diag.CannotChangePathPermissions, diags[0].ID) +} + +// Matching a configured group is a comparison with the groups already on the current +// user, so a report reads no group from the workspace, whatever the number of +// configured groups. +func TestPermissionDiagnosticsMembershipMakesNoGroupRequests(t *testing.T) { + b, calls := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "testgroup"}, + {Level: "CAN_MANAGE", GroupName: "otherteam"}, + {Level: "CAN_MANAGE", GroupName: "nosuchgroup"}, + }, []string{"testgroup", "joined"}, []string{"otherteam"}) + + permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + assert.Equal(t, int32(0), calls.parents.Load()) +} + +// Workspace-local groups are matched by name like account groups: a CAN_MANAGE grant +// to admins or users that the current user belongs to counts as manageable. +func TestPermissionDiagnosticsWorkspaceLocalGroupGrantsCanManage(t *testing.T) { + for _, name := range []string{"admins", "users"} { + t.Run(name, func(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: name}, + }, []string{name}, nil) + + require.Empty(t, bundle.Apply(t.Context(), b, permissions.PermissionDiagnostics())) + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.Equal(t, diag.CannotChangePathPermissions, diags[0].ID) + }) + } +} + +// A user whose groups were not read, for example because the groups list failed, has +// no group matches, so the configured group is named as a contact. +func TestPermissionDiagnosticsNoGroupsMatchesNothing(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "testgroup"}, + }, nil, nil) + b.Config.Workspace.CurrentUser.Groups = nil + + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.ErrorContains(t, diags.Error(), "may include: testgroup.") } diff --git a/bundle/permissions/permission_report_test.go b/bundle/permissions/permission_report_test.go index 52437aacdf8..781f845614f 100644 --- a/bundle/permissions/permission_report_test.go +++ b/bundle/permissions/permission_report_test.go @@ -9,7 +9,7 @@ import ( ) func TestPermissionsReportPermissionDeniedWithGroup(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_MANAGE", GroupName: "testgroup"}, }) @@ -22,7 +22,7 @@ func TestPermissionsReportPermissionDeniedWithGroup(t *testing.T) { } func TestPermissionsReportPermissionDeniedWithOtherGroup(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_MANAGE", GroupName: "othergroup"}, }) @@ -36,7 +36,7 @@ func TestPermissionsReportPermissionDeniedWithOtherGroup(t *testing.T) { } func TestPermissionsReportPermissionDeniedWithoutPermission(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_VIEW", UserName: "testuser@databricks.com"}, }) @@ -50,7 +50,7 @@ func TestPermissionsReportPermissionDeniedWithoutPermission(t *testing.T) { } func TestPermissionsReportPermissionDeniedNilPermission(t *testing.T) { - b := mockBundle(nil) + b := mockBundle(t, nil) diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") expected := "EPERM1: unable to deploy to testpath as testuser@databricks.com.\n" + @@ -62,7 +62,7 @@ func TestPermissionsReportPermissionDeniedNilPermission(t *testing.T) { } func TestPermissionsReportFindOtherOwners(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_MANAGE", GroupName: "testgroup"}, {Level: "CAN_MANAGE", UserName: "alice@databricks.com"}, }) diff --git a/cmd/air/cancel.go b/cmd/air/cancel.go index 276bf82da2b..d6faa6aabe3 100644 --- a/cmd/air/cancel.go +++ b/cmd/air/cancel.go @@ -13,9 +13,9 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/spf13/cobra" ) @@ -89,7 +89,7 @@ any other task such as a notebook that prepares data. if all { data.All = true - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return renderError(ctx, cmd, "INTERNAL_ERROR", "TRANSIENT", true, fmt.Errorf("failed to resolve current user: %w", err)) diff --git a/cmd/air/cancel_test.go b/cmd/air/cancel_test.go index 4203451662f..47c2936914e 100644 --- a/cmd/air/cancel_test.go +++ b/cmd/air/cancel_test.go @@ -18,7 +18,6 @@ import ( "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/experimental/mocks" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" @@ -288,18 +287,22 @@ func TestCancelAllConfirmReadError(t *testing.T) { } func TestCancelAllMeError(t *testing.T) { - m := mocks.NewMockWorkspaceClient(t) - m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, iam.MeRequest{}).Return(nil, errors.New("nope")) + // identity/me fails with a non-retriable 403. + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusForbidden) + _, _ = w.Write([]byte(`{"error_code":"PERMISSION_DENIED","message":"nope"}`)) + })) + t.Cleanup(srv.Close) var buf bytes.Buffer - err := runCancelAll(t, m.WorkspaceClient, flags.OutputText, nil, &buf) + err := runCancelAll(t, newTestWorkspaceClient(t, srv.URL), flags.OutputText, nil, &buf) require.Error(t, err) assert.Contains(t, err.Error(), "failed to resolve current user") } func TestCancelAllListError(t *testing.T) { - // Me succeeds (default empty user), but listing active runs fails. - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // The current user resolves, but listing active runs fails. + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/2.2/jobs/runs/list" { w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(`{"error_code":"INTERNAL","message":"boom"}`)) @@ -352,7 +355,7 @@ func TestCancelAllCancelsAMultiTaskRunOnce(t *testing.T) { single := airBaseRun(222, "me@example.com", "GPU_1xA10", 1, "/Users/me@example.com/exp-c") list := runsListBody(t, "", multi, single) var cancelled []int64 - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/api/2.2/jobs/runs/list": _, _ = w.Write([]byte(list)) diff --git a/cmd/air/get.go b/cmd/air/get.go index 46b3e3e1e6f..b0e4ff4c4a7 100644 --- a/cmd/air/get.go +++ b/cmd/air/get.go @@ -14,11 +14,11 @@ import ( "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/spf13/cobra" ) @@ -158,7 +158,7 @@ func credentialError(ctx context.Context, w *databricks.WorkspaceClient, err err if !errors.Is(err, apierr.ErrUnauthenticated) && !errors.Is(err, apierr.ErrPermissionDenied) { return nil } - _, meErr := w.CurrentUser.Me(ctx, iam.MeRequest{}) + _, meErr := iamutil.GetCurrentUserOnce(ctx, w) return meErr } diff --git a/cmd/air/get_test.go b/cmd/air/get_test.go index 5fd29635d2b..0d2625d4149 100644 --- a/cmd/air/get_test.go +++ b/cmd/air/get_test.go @@ -16,11 +16,11 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/testserver" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/experimental/mocks" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -91,12 +91,23 @@ func TestGetRunNotFound(t *testing.T) { assert.Contains(t, err.Error(), "run 5 not found") } +// identityMeConfig returns a config whose identity/me call answers with resp. +func identityMeConfig(t *testing.T, resp testserver.Response) *config.Config { + t.Helper() + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return resp + }) + return &config.Config{Host: server.URL, Token: "token"} +} + func TestGetRunAuthFailed(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) - // GetRun's 403 is confirmed as a credential failure by the Me() probe, so + m.WorkspaceClient.Config = identityMeConfig(t, testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}}) + // GetRun's 403 is confirmed as a credential failure by the current-user probe, so // nothing is rendered. m.GetMockJobsAPI().EXPECT().GetRun(mock.Anything, mock.Anything).Return(nil, apierr.ErrPermissionDenied) - m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything).Return(nil, apierr.ErrPermissionDenied) ctx := cmdctx.SetWorkspaceClient(cmdio.MockDiscard(t.Context()), m.WorkspaceClient) cmd := withOutput(newGetCommand(), flags.OutputText) cmd.SetContext(ctx) @@ -108,10 +119,10 @@ func TestGetRunAuthFailed(t *testing.T) { func TestGetRunAuthTransient(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) - // A transient failure at the auth probe must not be misreported as an auth + // A probe failure that is not a 401/403 must not be misreported as an auth // error; it surfaces as a retryable internal error instead. + m.WorkspaceClient.Config = identityMeConfig(t, testserver.Response{StatusCode: http.StatusBadRequest, Body: map[string]any{"error_code": "BAD_REQUEST", "message": "unavailable"}}) m.GetMockJobsAPI().EXPECT().GetRun(mock.Anything, mock.Anything).Return(nil, apierr.ErrUnauthenticated) - m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything).Return(nil, errors.New("connection reset")) ctx := cmdctx.SetWorkspaceClient(cmdio.MockDiscard(t.Context()), m.WorkspaceClient) cmd := withOutput(newGetCommand(), flags.OutputText) cmd.SetContext(ctx) @@ -124,10 +135,10 @@ func TestGetRunAuthTransient(t *testing.T) { func TestGetRunDeniedWithValidCredentials(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) - // A 403 on the run with credentials the Me() probe accepts is missing access - // to the run, not a failed login. + m.WorkspaceClient.Config = identityMeConfig(t, testserver.Response{StatusCode: http.StatusOK, Body: map[string]any{"user": map[string]any{"user_id": "1000012345", "username": "me@example.com"}}}) + // A 403 on the run with credentials the current-user probe accepts is missing + // access to the run, not a failed login. m.GetMockJobsAPI().EXPECT().GetRun(mock.Anything, mock.Anything).Return(nil, apierr.ErrPermissionDenied) - m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything).Return(&iam.User{UserName: "u@example.com"}, nil) ctx := cmdctx.SetWorkspaceClient(cmdio.MockDiscard(t.Context()), m.WorkspaceClient) cmd := withOutput(newGetCommand(), flags.OutputText) cmd.SetContext(ctx) @@ -187,7 +198,7 @@ const succeededRun = `{"run_id": 5, "state": {"life_cycle_state": "TERMINATED", // requests. func getRunServer(t *testing.T, meCalls *atomic.Int32, run string, outputs map[string]string) *httptest.Server { t.Helper() - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch { case r.URL.Path == "/api/2.2/jobs/runs/get": _, _ = w.Write([]byte(run)) diff --git a/cmd/air/list_index_test.go b/cmd/air/list_index_test.go index 955aea31e1a..def7ce22fe0 100644 --- a/cmd/air/list_index_test.go +++ b/cmd/air/list_index_test.go @@ -348,10 +348,8 @@ func TestRegularScanKeepsFullCap(t *testing.T) { func TestListSelfUserFilterUsesIndex(t *testing.T) { var indexHits, listHits atomic.Int32 run := indexRun(7, 3000_000) - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { - case "/api/2.0/preview/scim/v2/Me": - _, _ = w.Write([]byte(`{"userName":"me@example.com"}`)) case aiTrainingWorkflowsPath: indexHits.Add(1) _, _ = w.Write([]byte(`{"training_workflows":[{"job_run_id":"7","submit_time":{"seconds":3000}}]}`)) diff --git a/cmd/air/list_test.go b/cmd/air/list_test.go index 847d1368a92..db9cbf2c41c 100644 --- a/cmd/air/list_test.go +++ b/cmd/air/list_test.go @@ -52,12 +52,27 @@ func runsListBody(t *testing.T, nextToken string, runs ...jobs.BaseRun) string { return string(b) } +// airTestUserBody is the identity/me response for the user that runs are listed for. +const airTestUserBody = `{"name": "accounts/acct-123/me", "user": {"user_id": "1000012345", "username": "me@example.com", "account_user_status": "ACTIVE"}}` + +// withAirIdentityMe answers identity/me with airTestUserBody, the user the air runs are +// listed for, and passes every other request to next. +func withAirIdentityMe(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/2.0/identity/me" { + _, _ = w.Write([]byte(airTestUserBody)) + return + } + next(w, r) + } +} + // runsServer serves one runs/list response body per call, repeating the last // once exhausted, and a stub for any other request (the SDK config probe). func runsServer(t *testing.T, bodies ...string) *httptest.Server { t.Helper() call := 0 - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/2.2/jobs/runs/list" { body := bodies[min(call, len(bodies)-1)] call++ diff --git a/cmd/air/logs_test.go b/cmd/air/logs_test.go index 769ef34abd3..3de81f4fc90 100644 --- a/cmd/air/logs_test.go +++ b/cmd/air/logs_test.go @@ -236,8 +236,8 @@ func completedRunLogsServer(t *testing.T) *httptest.Server { {"time_unix_nano": 2000000000, "body": "line two", "node_index": 0}, {"time_unix_nano": 1000000000, "body": "line one", "node_index": 0} ]}`)) - case strings.HasSuffix(r.URL.Path, "/scim/v2/Me"): - t.Errorf("unexpected Me() probe on a successful logs fetch") + case strings.HasSuffix(r.URL.Path, "/identity/me"): + t.Errorf("unexpected current-user probe on a successful logs fetch") w.WriteHeader(http.StatusInternalServerError) default: // SDK config discovery. diff --git a/cmd/air/run_test.go b/cmd/air/run_test.go index 78f4deb3129..64b648a4071 100644 --- a/cmd/air/run_test.go +++ b/cmd/air/run_test.go @@ -31,7 +31,7 @@ func submitServer(t *testing.T) (*httptest.Server, *submitRequestCounts) { t.Helper() counts := &submitRequestCounts{} runGet := `{"run_id": 555, "tasks": [{"run_id": 556, "attempt_number": 0}]}` - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch { case strings.HasSuffix(r.URL.Path, "/jobs/runs/submit"): _, _ = w.Write([]byte(`{"run_id": 555}`)) @@ -193,7 +193,7 @@ func TestRunDryRunValidatesBackendWithoutMutation(t *testing.T) { if request.path == validateConfigPath { validations = append(validations, request) } - assert.Contains(t, []string{"/.well-known/databricks-config", "/api/2.0/preview/scim/v2/Me", validateConfigPath}, request.path, + assert.Contains(t, []string{"/.well-known/databricks-config", "/api/2.0/identity/me", validateConfigPath}, request.path, "dry-run must not make requests beyond authentication, user lookup, and validation") assert.True(t, request.method == http.MethodGet || request.path == validateConfigPath, "dry-run made unexpected %s request to %s", request.method, request.path) diff --git a/cmd/air/run_watch_test.go b/cmd/air/run_watch_test.go index 210cd33f3b2..95751920e9d 100644 --- a/cmd/air/run_watch_test.go +++ b/cmd/air/run_watch_test.go @@ -36,7 +36,7 @@ func watchServer(t *testing.T, resultState string) *httptest.Server { "state": {"life_cycle_state": "TERMINATED", "result_state": "` + resultState + `"}, "tasks": [{"run_id": 778, "attempt_number": 0}] }` - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch { case strings.HasSuffix(r.URL.Path, "/jobs/runs/submit"): _, _ = w.Write([]byte(`{"run_id": 777}`)) @@ -54,7 +54,7 @@ func watchServer(t *testing.T, resultState string) *httptest.Server { } _, _ = fmt.Fprintf(w, `{"log_records":[%s,%s]}`, first, second) default: - // Me() user lookup, workspace-id, SDK config discovery. + // Workspace-id and SDK config discovery. _, _ = w.Write([]byte(`{"userName": "u@example.com", "workspace_id": 1}`)) } })) @@ -75,7 +75,7 @@ func watchServerMLflow(t *testing.T, resultState string) *httptest.Server { "state": {"life_cycle_state": "TERMINATED", "result_state": "` + resultState + `"}, "tasks": [{"run_id": 778, "attempt_number": 0}] }` - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch { case strings.HasSuffix(r.URL.Path, "/jobs/runs/submit"): _, _ = w.Write([]byte(`{"run_id": 777}`)) @@ -231,6 +231,10 @@ func TestRunWatchDryRunSkipsSubmit(t *testing.T) { var got []string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { got = append(got, r.URL.Path) + if r.URL.Path == "/api/2.0/identity/me" { + _, _ = w.Write([]byte(airTestUserBody)) + return + } _, _ = w.Write([]byte(`{}`)) })) t.Cleanup(srv.Close) diff --git a/cmd/air/runlaunch.go b/cmd/air/runlaunch.go index a888d80d85d..846b68dfb89 100644 --- a/cmd/air/runlaunch.go +++ b/cmd/air/runlaunch.go @@ -9,9 +9,9 @@ import ( "uuid" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/workspace" ) @@ -21,8 +21,8 @@ const userWorkspaceDirEnv = "DATABRICKS_INTERNAL_USER_WORKSPACE_DIR" // currentUserEmail returns the authenticated user's email (works for any domain). func currentUserEmail(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { - // Only userName is needed; the full response also resolves groups and entitlements. - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{Attributes: "userName"}) + // Only userName is needed, so the groups are not read. + me, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return "", fmt.Errorf("failed to resolve current user: %w", err) } diff --git a/cmd/air/runlaunch_test.go b/cmd/air/runlaunch_test.go index edb4803ba65..4434c2ff29d 100644 --- a/cmd/air/runlaunch_test.go +++ b/cmd/air/runlaunch_test.go @@ -9,7 +9,6 @@ import ( "github.com/databricks/cli/libs/filer" "github.com/databricks/cli/libs/testserver" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -46,14 +45,9 @@ func TestUserWorkspaceDir(t *testing.T) { assert.Equal(t, "/Workspace/custom", dir) } -func TestUserWorkspaceDirRequestsOnlyUserName(t *testing.T) { +func TestUserWorkspaceDirUsesCurrentUser(t *testing.T) { server := testserver.New(t) t.Cleanup(server.Close) - var attributes []string - server.Handle("GET", "/api/2.0/preview/scim/v2/Me", func(req testserver.Request) any { - attributes = append(attributes, req.URL.Query().Get("attributes")) - return iam.User{UserName: "me@example.com"} - }) testserver.AddDefaultHandlers(server) w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) require.NoError(t, err) @@ -61,8 +55,7 @@ func TestUserWorkspaceDirRequestsOnlyUserName(t *testing.T) { dir, err := userWorkspaceDir(t.Context(), w) require.NoError(t, err) - assert.Equal(t, "/Workspace/Users/me@example.com", dir) - assert.Equal(t, []string{"userName"}, attributes) + assert.Equal(t, "/Workspace/Users/tester@databricks.com", dir) } func TestUserWorkspaceDirProfilePrecedence(t *testing.T) { diff --git a/cmd/air/runsubmit_test.go b/cmd/air/runsubmit_test.go index 2a5c739f4f5..9168981c119 100644 --- a/cmd/air/runsubmit_test.go +++ b/cmd/air/runsubmit_test.go @@ -769,9 +769,9 @@ func TestSubmitWorkloadWithCodeSource(t *testing.T) { return jobs.SubmitRunResponse{RunId: 555} }) var meCalls atomic.Int32 - server.Handle("GET", "/api/2.0/preview/scim/v2/Me", func(req testserver.Request) any { + server.Handle("GET", "/api/2.0/identity/me", func(req testserver.Request) any { meCalls.Add(1) - return req.Workspace.MeUser(req.Token) + return req.Workspace.IdentityMe(req.Token) }) stubValidateConfig(server) testserver.AddDefaultHandlers(server) diff --git a/cmd/apps/import.go b/cmd/apps/import.go index 7df83287871..2f76f3215d6 100644 --- a/cmd/apps/import.go +++ b/cmd/apps/import.go @@ -30,11 +30,11 @@ import ( "github.com/databricks/cli/libs/dyn/convert" "github.com/databricks/cli/libs/dyn/yamlsaver" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/logdiag" "github.com/databricks/cli/libs/textutil" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/apps" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/workspace" "github.com/spf13/cobra" ) @@ -90,7 +90,7 @@ Examples: w := cmdctx.WorkspaceClient(ctx) // Get current user to filter apps - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/cmd/apps/run_local.go b/cmd/apps/run_local.go index d9671276f0f..4ba97f9d0a0 100644 --- a/cmd/apps/run_local.go +++ b/cmd/apps/run_local.go @@ -17,8 +17,8 @@ import ( "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" ) @@ -117,7 +117,7 @@ func setupProxy(ctx context.Context, cmd *cobra.Command, config *runlocal.Config return err } - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return err } diff --git a/cmd/apps/run_local_test.go b/cmd/apps/run_local_test.go index 94a0b56ec70..de5d57eaa7e 100644 --- a/cmd/apps/run_local_test.go +++ b/cmd/apps/run_local_test.go @@ -8,10 +8,9 @@ import ( "time" "github.com/databricks/cli/libs/apps/runlocal" - "github.com/databricks/databricks-sdk-go/experimental/mocks" - "github.com/databricks/databricks-sdk-go/service/iam" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" "github.com/spf13/cobra" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -21,11 +20,14 @@ func TestSetupProxyPortInUse(t *testing.T) { defer ln.Close() port := ln.Addr().(*net.TCPAddr).Port - m := mocks.NewMockWorkspaceClient(t) - m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything).Return(&iam.User{UserName: "test-user"}, nil) + server := testserver.New(t) + t.Cleanup(server.Close) + testserver.AddDefaultHandlers(server) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) config := runlocal.NewConfig("https://workspace.databricks.test", "123", t.TempDir(), runlocal.DEFAULT_HOST, runlocal.DEFAULT_PORT) - err = setupProxy(t.Context(), &cobra.Command{}, config, m.WorkspaceClient, port, false) + err = setupProxy(t.Context(), &cobra.Command{}, config, w, port, false) require.ErrorContains(t, err, "failed to start app proxy") } diff --git a/cmd/auth/describe.go b/cmd/auth/describe.go index a58e964de27..a2ef92cca14 100644 --- a/cmd/auth/describe.go +++ b/cmd/auth/describe.go @@ -13,6 +13,7 @@ import ( "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/config" @@ -138,6 +139,7 @@ func getAuthStatus(cmd *cobra.Command, args []string, showSensitive bool, fn try // Workspaces.List can fail for reasons other than bad credentials // (e.g. it requires account admin), so verify against the workspace // endpoint with the same resolved config before reporting failure. + // Account-only hosts reject this probe. me, meErr := verifyWorkspaceAuth(ctx, a.Config) if meErr == nil { return successAuthStatus(ctx, cmd, a.Config, showSensitive, me.UserName, ""), nil @@ -146,7 +148,7 @@ func getAuthStatus(cmd *cobra.Command, args []string, showSensitive bool, fn try } w := cmdctx.WorkspaceClient(ctx) - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUserOnce(ctx, w) if err == nil { return successAuthStatus(ctx, cmd, w.Config, showSensitive, me.UserName, ""), nil } @@ -174,7 +176,7 @@ func verifyWorkspaceAuth(ctx context.Context, cfg *config.Config) (*iam.User, er if err != nil { return nil, err } - return w.CurrentUser.Me(ctx, iam.MeRequest{}) + return iamutil.GetCurrentUserOnce(ctx, w) } // verifyAccountAuth checks whether cfg's credentials are accepted by the diff --git a/cmd/auth/describe_test.go b/cmd/auth/describe_test.go index 3d7bd62a0a1..1136e3e1025 100644 --- a/cmd/auth/describe_test.go +++ b/cmd/auth/describe_test.go @@ -7,15 +7,17 @@ import ( "net/http/httptest" "os" "path/filepath" + "sync/atomic" "testing" "github.com/databricks/cli/libs/auth/storage" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/experimental/mocks" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -91,51 +93,46 @@ func TestGetWorkspaceAuthStatus(t *testing.T) { cmd := &cobra.Command{} cmd.SetContext(ctx) - showSensitive := false - - currentUserApi := m.GetMockCurrentUserAPI() - currentUserApi.EXPECT().Me(mock.Anything, mock.Anything).Return(&iam.User{ - UserName: "test-user", - }, nil) - cmd.Flags().String("host", "", "") cmd.Flags().String("profile", "", "") err := cmd.Flag("profile").Value.Set("my-profile") require.NoError(t, err) cmd.Flag("profile").Changed = true + // Only identity/me is served, so the username comes from this fixture rather than the default user. + server := testserver.New(t) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}} + }) + cfg := &config.Config{ Profile: "my-profile", } m.WorkspaceClient.Config = cfg - t.Setenv("DATABRICKS_AUTH_TYPE", "azure-cli") + t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), ".databrickscfg")) err = config.ConfigAttributes.Configure(cfg) require.NoError(t, err) - status, err := getAuthStatus(cmd, []string{}, showSensitive, func(cmd *cobra.Command, args []string) (*config.Config, bool, error) { + status, err := getAuthStatus(cmd, []string{}, false, func(cmd *cobra.Command, args []string) (*config.Config, bool, error) { err := config.ConfigAttributes.ResolveFromStringMap(cfg, map[string]string{ - "host": "https://test.test", + "host": server.URL, "token": "test-token", - "auth_type": "azure-cli", + "auth_type": "pat", }) require.NoError(t, err) return cfg, false, nil }) require.NoError(t, err) require.NotNil(t, status) + require.NoError(t, status.Error) require.Equal(t, "success", status.Status) require.Equal(t, "test-user", status.Username) - require.Equal(t, "https://test.test", status.Details.Host) - require.Equal(t, "azure-cli", status.Details.AuthType) + require.Equal(t, server.URL, status.Details.Host) + require.Equal(t, "pat", status.Details.AuthType) - require.Equal(t, "azure-cli", status.Details.Configuration["auth_type"].Value) - require.Equal(t, "DATABRICKS_AUTH_TYPE environment variable", status.Details.Configuration["auth_type"].Source.String()) + require.Equal(t, "pat", status.Details.Configuration["auth_type"].Value) require.False(t, status.Details.Configuration["auth_type"].AuthTypeMismatch) - require.Equal(t, "********", status.Details.Configuration["token"].Value) - require.Equal(t, "dynamic configuration", status.Details.Configuration["token"].Source.String()) - require.True(t, status.Details.Configuration["token"].AuthTypeMismatch) - require.Equal(t, "my-profile", status.Details.Configuration["profile"].Value) require.Equal(t, "--profile flag", status.Details.Configuration["profile"].Source.String()) require.False(t, status.Details.Configuration["profile"].AuthTypeMismatch) @@ -274,6 +271,7 @@ func TestGetAccountAuthStatus(t *testing.T) { }) require.NoError(t, err) require.NotNil(t, status) + require.NoError(t, status.Error) require.Equal(t, "success", status.Status) require.Equal(t, "test-user", status.Username) @@ -372,8 +370,13 @@ func TestGetWorkspaceAuthStatus_U2M_PopulatesTokenStorage(t *testing.T) { cmd := &cobra.Command{} cmd.SetContext(ctx) - currentUserApi := m.GetMockCurrentUserAPI() - currentUserApi.EXPECT().Me(mock.Anything, mock.Anything).Return(&iam.User{UserName: "u2m-user"}, nil) + // A databricks-cli token cannot be minted in a unit test, so the check fails; + // token storage is reported for the status either way. + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return testserver.Response{StatusCode: http.StatusUnauthorized, Body: map[string]any{"error_code": "UNAUTHENTICATED", "message": "no token"}} + }) cmd.Flags().String("host", "", "") cmd.Flags().String("profile", "", "") @@ -388,8 +391,9 @@ func TestGetWorkspaceAuthStatus_U2M_PopulatesTokenStorage(t *testing.T) { status, err := getAuthStatus(cmd, []string{}, false, func(cmd *cobra.Command, args []string) (*config.Config, bool, error) { require.NoError(t, config.ConfigAttributes.ResolveFromStringMap(cfg, map[string]string{ - "host": "https://test.test", - "auth_type": authTypeDatabricksCLI, + "host": server.URL, + "auth_type": authTypeDatabricksCLI, + "databricks_cli_path": "/nonexistent/databricks", })) return cfg, false, nil }) @@ -401,10 +405,10 @@ func TestGetWorkspaceAuthStatus_U2M_PopulatesTokenStorage(t *testing.T) { assert.Equal(t, "DATABRICKS_AUTH_STORAGE environment variable", status.TokenStorage.Source) } -// describeVerifyServer simulates the host that describe's secondary -// verification calls hit: CurrentUser.Me and account Workspaces.List. -// Statuses other than 200 return an error body with that status code; a zero -// status marks an endpoint that must not be called at all. +// describeVerifyServer simulates the host that describe's verification calls +// hit: identity/me (the workspace check) and account Workspaces.List. Statuses +// other than 200 return an error body with that status code; a zero status marks +// an endpoint that must not be called at all. func describeVerifyServer(t *testing.T, accountID string, meStatus, listStatus int) *httptest.Server { t.Helper() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -416,13 +420,13 @@ func describeVerifyServer(t *testing.T, accountID string, meStatus, listStatus i w.Header().Set("Content-Type", "application/json") if status != http.StatusOK { w.WriteHeader(status) - okBody = map[string]any{"error_code": "TEST_ERROR", "message": "secondary check failed"} + okBody = map[string]any{"error_code": "TEST_ERROR", "message": r.URL.Path + " failed"} } require.NoError(t, json.NewEncoder(w).Encode(okBody)) } switch r.URL.Path { - case "/api/2.0/preview/scim/v2/Me": - respond(meStatus, map[string]any{"userName": "fallback-user"}) + case "/api/2.0/identity/me": + respond(meStatus, map[string]any{"user": map[string]any{"user_id": "1", "username": "fallback-user"}}) case "/api/2.0/accounts/" + accountID + "/workspaces": respond(listStatus, []map[string]any{}) default: @@ -433,15 +437,12 @@ func describeVerifyServer(t *testing.T, accountID string, meStatus, listStatus i return server } -// newDescribeWorkspaceCmd wires a mocked workspace client whose Me call fails -// with meErr, with cfg as its resolved configuration. -func newDescribeWorkspaceCmd(t *testing.T, cfg *config.Config, meErr error) *cobra.Command { +// newDescribeWorkspaceCmd wires a workspace client over cfg, the resolved +// configuration, so its identity/me call goes to the configured host. +func newDescribeWorkspaceCmd(t *testing.T, cfg *config.Config) *cobra.Command { t.Helper() - m := mocks.NewMockWorkspaceClient(t) - m.WorkspaceClient.Config = cfg - m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything).Return(nil, meErr) cmd := newHostProfileCmd(t) - cmd.SetContext(cmdctx.SetWorkspaceClient(t.Context(), m.WorkspaceClient)) + cmd.SetContext(cmdctx.SetWorkspaceClient(t.Context(), &databricks.WorkspaceClient{Config: cfg})) t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), ".databrickscfg")) return cmd } @@ -469,16 +470,17 @@ func resolveCfg(t *testing.T, cfg *config.Config, attrs map[string]string, isAcc } } -// TestGetAuthStatusVerificationFallback covers the fallback when the primary -// verification call (mocked client) fails: describe tries the other endpoint -// over HTTP against describeVerifyServer, and only when both fail does it -// report the primary error. Error rows leave wantUsername/wantAccountID empty -// because errorAuthStatus never sets them. +// TestGetAuthStatusVerificationFallback covers the fallback when the primary check +// fails. The account branch tries identity/me after Workspaces.List fails, and the +// workspace branch tries the account Workspaces list after identity/me fails. Only when +// both fail is the primary error reported. describeVerifyServer answers the HTTP +// fallback endpoint. Error rows leave wantUsername/wantAccountID empty because +// errorAuthStatus never sets them. func TestGetAuthStatusVerificationFallback(t *testing.T) { tests := []struct { name string isAccount bool - primaryErr *apierr.APIError + primaryErr *apierr.APIError // the primary check error that is reported when both checks fail meStatus int listStatus int accountID string @@ -488,7 +490,6 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { }{ { name: "workspace check fails, account check succeeds", - primaryErr: &apierr.APIError{StatusCode: http.StatusBadRequest, Message: "Unable to load OAuth Config"}, meStatus: http.StatusNotFound, listStatus: http.StatusOK, accountID: "test-acct", @@ -497,7 +498,8 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { }, { name: "no second call without an account id", - primaryErr: &apierr.APIError{StatusCode: http.StatusBadRequest, Message: "Unable to load OAuth Config"}, + primaryErr: &apierr.APIError{StatusCode: http.StatusBadRequest, Message: "/api/2.0/identity/me failed"}, + meStatus: http.StatusBadRequest, wantStatus: "error", }, { @@ -512,7 +514,7 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { }, { name: "workspace branch, both checks fail", - primaryErr: &apierr.APIError{StatusCode: http.StatusBadRequest, Message: "Unable to load OAuth Config"}, + primaryErr: &apierr.APIError{StatusCode: http.StatusNotFound, Message: "/api/2.0/identity/me failed"}, meStatus: http.StatusNotFound, listStatus: http.StatusUnauthorized, accountID: "test-acct", @@ -538,7 +540,7 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { if tc.isAccount { cmd = newDescribeAccountCmd(t, cfg, tc.primaryErr) } else { - cmd = newDescribeWorkspaceCmd(t, cfg, tc.primaryErr) + cmd = newDescribeWorkspaceCmd(t, cfg) } attrs := map[string]string{ "host": server.URL, @@ -554,8 +556,13 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { require.Equal(t, tc.wantStatus, status.Status) assert.Equal(t, tc.wantUsername, status.Username) assert.Equal(t, tc.wantAccountID, status.AccountID) - if tc.wantStatus == "error" { - assert.ErrorIs(t, status.Error, tc.primaryErr) + if tc.wantStatus == "error" && tc.primaryErr != nil { + // The reported error is the primary one, found by status and message: an HTTP + // error from the server is a new value, so ErrorIs cannot match it by identity. + var apiErr *apierr.APIError + require.ErrorAs(t, status.Error, &apiErr) + assert.Equal(t, tc.primaryErr.StatusCode, apiErr.StatusCode) + assert.Equal(t, tc.primaryErr.Message, apiErr.Message) } }) } @@ -569,8 +576,9 @@ func TestGetWorkspaceAuthStatus_NonU2M_OmitsTokenStorage(t *testing.T) { cmd := &cobra.Command{} cmd.SetContext(ctx) - currentUserApi := m.GetMockCurrentUserAPI() - currentUserApi.EXPECT().Me(mock.Anything, mock.Anything).Return(&iam.User{UserName: "pat-user"}, nil) + server := testserver.New(t) + t.Cleanup(server.Close) + testserver.AddDefaultHandlers(server) cmd.Flags().String("host", "", "") cmd.Flags().String("profile", "", "") @@ -581,7 +589,7 @@ func TestGetWorkspaceAuthStatus_NonU2M_OmitsTokenStorage(t *testing.T) { status, err := getAuthStatus(cmd, []string{}, false, func(cmd *cobra.Command, args []string) (*config.Config, bool, error) { require.NoError(t, config.ConfigAttributes.ResolveFromStringMap(cfg, map[string]string{ - "host": "https://test.test", + "host": server.URL, "token": "pat-token", "auth_type": "pat", })) @@ -591,3 +599,32 @@ func TestGetWorkspaceAuthStatus_NonU2M_OmitsTokenStorage(t *testing.T) { require.NotNil(t, status) assert.Nil(t, status.TokenStorage) } + +// The account branch probes identity/me once. A 500 is not retried, so describe does not +// wait out the workspace retry window before reporting the failure. +func TestGetAuthStatusAccountProbeDoesNotRetryOn500(t *testing.T) { + t.Setenv("DATABRICKS_ACCOUNT_ID", "") + var meCalls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/2.0/identity/me" { + meCalls.Add(1) + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + _, _ = w.Write([]byte(`{"error_code":"INTERNAL_ERROR","message":"boom"}`)) + })) + t.Cleanup(server.Close) + + cfg := &config.Config{} + cmd := newDescribeAccountCmd(t, cfg, &apierr.APIError{StatusCode: http.StatusForbidden, Message: "not an account admin"}) + attrs := map[string]string{ + "host": server.URL, + "token": "test-token", + "auth_type": "pat", + "account_id": "test-acct", + } + status, err := getAuthStatus(cmd, []string{}, false, resolveCfg(t, cfg, attrs, true)) + require.NoError(t, err) + assert.Equal(t, "error", status.Status) + assert.Equal(t, int32(1), meCalls.Load()) +} diff --git a/cmd/auth/profiles.go b/cmd/auth/profiles.go index e65b11371b7..4da2cd9e088 100644 --- a/cmd/auth/profiles.go +++ b/cmd/auth/profiles.go @@ -13,10 +13,10 @@ import ( "github.com/databricks/cli/libs/databrickscfg" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/config" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" "gopkg.in/ini.v1" ) @@ -110,7 +110,7 @@ func (c *profileMetadata) Load(ctx context.Context, configFilePath string, skipV if err != nil { return } - _, err = w.CurrentUser.Me(callCtx, iam.MeRequest{}) + _, err = iamutil.GetCurrentUserOnce(callCtx, w) c.Host = cfg.Host c.AuthType = cfg.AuthType if err != nil { diff --git a/cmd/auth/profiles_test.go b/cmd/auth/profiles_test.go index 90efa2a99bd..48fafd74ae8 100644 --- a/cmd/auth/profiles_test.go +++ b/cmd/auth/profiles_test.go @@ -112,7 +112,7 @@ func TestProfilesDefaultMarker(t *testing.T) { // newSPOGServer creates a mock SPOG server that returns account-scoped OIDC. // It serves both validation endpoints since SPOG workspace profiles (with a -// real workspace_id) need CurrentUser.Me, while account profiles need +// real workspace_id) need identity/me, while account profiles need // Workspaces.List. The workspace-only newWorkspaceServer omits the account // endpoint to prove routing correctness for non-SPOG hosts. func newSPOGServer(t *testing.T, accountID string) *httptest.Server { @@ -127,9 +127,9 @@ func newSPOGServer(t *testing.T, accountID string) *httptest.Server { }) case "/api/2.0/accounts/" + accountID + "/workspaces": _ = json.NewEncoder(w).Encode([]map[string]any{}) - case "/api/2.0/preview/scim/v2/Me": - // SPOG workspace profiles also need CurrentUser.Me to succeed. - _ = json.NewEncoder(w).Encode(map[string]any{"userName": "test-user"}) + case "/api/2.0/identity/me": + // SPOG workspace profiles also need the current user to resolve. + _ = json.NewEncoder(w).Encode(map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}}) default: w.WriteHeader(http.StatusNotFound) } @@ -151,8 +151,8 @@ func newWorkspaceServer(t *testing.T, accountID string) *httptest.Server { "account_id": accountID, "oidc_endpoint": r.Host + "/oidc", }) - case "/api/2.0/preview/scim/v2/Me": - _ = json.NewEncoder(w).Encode(map[string]any{"userName": "test-user"}) + case "/api/2.0/identity/me": + _ = json.NewEncoder(w).Encode(map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}}) default: w.WriteHeader(http.StatusNotFound) } @@ -250,14 +250,14 @@ func TestClassicAccountsHostConfigType(t *testing.T) { func TestProfileLoadNoDiscoveryStaysWorkspace(t *testing.T) { // When .well-known returns 404 and the unified-host fallback is false, // the SPOG override should NOT trigger even if account_id is set. The - // profile should stay WorkspaceConfig and validate via CurrentUser.Me. + // profile should stay WorkspaceConfig and validate via identity/me. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch r.URL.Path { case "/.well-known/databricks-config": w.WriteHeader(http.StatusNotFound) - case "/api/2.0/preview/scim/v2/Me": - _ = json.NewEncoder(w).Encode(map[string]any{"userName": "test-user"}) + case "/api/2.0/identity/me": + _ = json.NewEncoder(w).Encode(map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}}) default: w.WriteHeader(http.StatusNotFound) } diff --git a/cmd/labs/project/installer_test.go b/cmd/labs/project/installer_test.go index 266d42dbeb8..6a5dc19b1bd 100644 --- a/cmd/labs/project/installer_test.go +++ b/cmd/labs/project/installer_test.go @@ -24,7 +24,6 @@ import ( "github.com/databricks/cli/libs/process" "github.com/databricks/cli/libs/python" "github.com/databricks/databricks-sdk-go/service/compute" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/sql" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -327,12 +326,6 @@ func TestInstallerWorksForDevelopment(t *testing.T) { }) return } - if r.URL.Path == "/api/2.0/preview/scim/v2/Me" { - respondWithJSON(t, w, iam.User{ - UserName: "serge", - }) - return - } if r.URL.Path == "/api/2.1/clusters/spark-versions" { respondWithJSON(t, w, compute.GetSparkVersionsResponse{ Versions: []compute.SparkVersion{ diff --git a/cmd/psql/psql_autoscaling.go b/cmd/psql/psql_autoscaling.go index 331e05b55b5..b43a8787827 100644 --- a/cmd/psql/psql_autoscaling.go +++ b/cmd/psql/psql_autoscaling.go @@ -8,9 +8,9 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/iamutil" libpsql "github.com/databricks/cli/libs/psql" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/postgres" ) @@ -38,7 +38,7 @@ func connectAutoscaling(ctx context.Context, projectID, branchID, endpointID str return err } - user, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + user, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/cmd/psql/psql_provisioned.go b/cmd/psql/psql_provisioned.go index 272dfd7d687..feffd66b80c 100644 --- a/cmd/psql/psql_provisioned.go +++ b/cmd/psql/psql_provisioned.go @@ -8,10 +8,10 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/iamutil" libpsql "github.com/databricks/cli/libs/psql" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/database" - "github.com/databricks/databricks-sdk-go/service/iam" ) // provisionedDefaultDatabase is the default database for Lakebase Provisioned instances. @@ -27,7 +27,7 @@ func connectProvisioned(ctx context.Context, instanceName string, retryConfig li return err } - user, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + user, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/cmd/sync/completion.go b/cmd/sync/completion.go index c82d85a1c38..04d7184d653 100644 --- a/cmd/sync/completion.go +++ b/cmd/sync/completion.go @@ -5,8 +5,8 @@ import ( "path" "strings" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/workspace" "github.com/spf13/cobra" ) @@ -45,7 +45,7 @@ func completeRemotePath( wsc *databricks.WorkspaceClient, toComplete string, ) ([]string, cobra.ShellCompDirective) { - me, err := wsc.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetUserPrincipalOnce(ctx, wsc) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/experimental/postgres/cmd/targeting.go b/experimental/postgres/cmd/targeting.go index cf1290ad578..a7b2d971795 100644 --- a/experimental/postgres/cmd/targeting.go +++ b/experimental/postgres/cmd/targeting.go @@ -7,9 +7,9 @@ import ( "github.com/databricks/cli/experimental/postgres/cmd/internal/target" "github.com/databricks/cli/libs/cmdctx" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/database" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/postgres" ) @@ -114,7 +114,7 @@ func resolveProvisioned(ctx context.Context, w *databricks.WorkspaceClient, inst return nil, fmt.Errorf("database instance %q is not ready for accepting connections (state: %s)", instance.Name, instance.State) } - user, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + user, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return nil, fmt.Errorf("failed to get current user: %w", err) } @@ -175,7 +175,7 @@ func resolveAutoscaling(ctx context.Context, w *databricks.WorkspaceClient, spec return nil, err } - user, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + user, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return nil, fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/client/client.go b/experimental/ssh/internal/client/client.go index 5afd212131f..c1c7b3f6772 100644 --- a/experimental/ssh/internal/client/client.go +++ b/experimental/ssh/internal/client/client.go @@ -30,6 +30,7 @@ import ( "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/telemetry" "github.com/databricks/cli/libs/telemetry/protos" @@ -37,7 +38,6 @@ import ( "github.com/databricks/databricks-sdk-go/retries" "github.com/databricks/databricks-sdk-go/service/compute" "github.com/databricks/databricks-sdk-go/service/environments" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/databricks/databricks-sdk-go/service/settingsv2" "github.com/databricks/databricks-sdk-go/service/workspace" @@ -598,7 +598,7 @@ func runIDE(ctx context.Context, client *databricks.WorkspaceClient, userName, k } // Get Databricks user name for the workspace path - currentUser, err := client.CurrentUser.Me(ctx, iam.MeRequest{}) + currentUser, err := iamutil.GetUserPrincipal(ctx, client) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/keys/secrets.go b/experimental/ssh/internal/keys/secrets.go index 6409770a4d7..45c95e82370 100644 --- a/experimental/ssh/internal/keys/secrets.go +++ b/experimental/ssh/internal/keys/secrets.go @@ -6,15 +6,15 @@ import ( "errors" "fmt" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/workspace" ) // CreateKeysSecretScope creates or retrieves the secret scope for SSH keys. // sessionID is the unique identifier for the session (cluster ID for dedicated clusters, connection name for serverless). func CreateKeysSecretScope(ctx context.Context, client *databricks.WorkspaceClient, sessionID string) (string, error) { - me, err := client.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetUserPrincipal(ctx, client) if err != nil { return "", fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/server/fuse.go b/experimental/ssh/internal/server/fuse.go index 23366728ab0..3e911306331 100644 --- a/experimental/ssh/internal/server/fuse.go +++ b/experimental/ssh/internal/server/fuse.go @@ -9,9 +9,9 @@ import ( "time" "github.com/databricks/cli/experimental/ssh/internal/fuse" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" ) func startFuseRegistration(ctx context.Context, client *databricks.WorkspaceClient, serverless bool) { @@ -44,7 +44,7 @@ func fuseUserInfo(ctx context.Context, client *databricks.WorkspaceClient) (user // User metadata is optional. Limit its lookup before publishing the server port. userCtx, cancel := context.WithTimeout(ctx, 2*time.Second) defer cancel() - me, err := client.CurrentUser.Me(userCtx, iam.MeRequest{}) + me, err := iamutil.GetUserPrincipal(userCtx, client) if err != nil { log.Debugf(ctx, "Registering filesystem credentials without user metadata: %v", err) return "", "" diff --git a/experimental/ssh/internal/server/fuse_test.go b/experimental/ssh/internal/server/fuse_test.go index 393c5ddaa35..eee3fa5a74a 100644 --- a/experimental/ssh/internal/server/fuse_test.go +++ b/experimental/ssh/internal/server/fuse_test.go @@ -89,7 +89,7 @@ func TestFuseUserInfo(t *testing.T) { wantUserID string wantNotebookDir string }{ - {name: "available", status: http.StatusOK, body: `{"id":"12345","userName":"user@example.test"}`, wantUserID: "12345", wantNotebookDir: "/Users/user@example.test"}, + {name: "available", status: http.StatusOK, body: `{"user":{"user_id":"12345","username":"user@example.test"}}`, wantUserID: "12345", wantNotebookDir: "/Users/user@example.test"}, {name: "forbidden", status: http.StatusForbidden, body: `{"message":"forbidden"}`}, {name: "timeout"}, } { @@ -98,7 +98,7 @@ func TestFuseUserInfo(t *testing.T) { c, err := databricks.NewWorkspaceClient(&databricks.Config{ Host: "https://workspace.test", HostMetadataResolver: noHostMetadata, Token: "test-token", HTTPTransport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - assert.Equal(t, "/api/2.0/preview/scim/v2/Me", r.URL.Path) + assert.Equal(t, "/api/2.0/identity/me", r.URL.Path) if tc.status == 0 { <-r.Context().Done() return nil, r.Context().Err() diff --git a/experimental/ssh/internal/workspace/workspace.go b/experimental/ssh/internal/workspace/workspace.go index a6a29ca7bb2..e8936a2b8f1 100644 --- a/experimental/ssh/internal/workspace/workspace.go +++ b/experimental/ssh/internal/workspace/workspace.go @@ -9,8 +9,8 @@ import ( "path/filepath" "github.com/databricks/cli/libs/filer" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" ) const metadataFileName = "metadata.json" @@ -31,7 +31,7 @@ type WorkspaceMetadata struct { // GetWorkspaceHomeDir returns the current user's workspace home folder. func GetWorkspaceHomeDir(ctx context.Context, client *databricks.WorkspaceClient) (string, error) { - me, err := client.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetUserPrincipal(ctx, client) if err != nil { return "", fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/workspace/workspace_test.go b/experimental/ssh/internal/workspace/workspace_test.go new file mode 100644 index 00000000000..6752730115c --- /dev/null +++ b/experimental/ssh/internal/workspace/workspace_test.go @@ -0,0 +1,38 @@ +package workspace + +import ( + "testing" + + "github.com/databricks/cli/libs/iamutil" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetWorkspaceHomeDirReturnsUserFolder(t *testing.T) { + server := testserver.New(t) + testserver.AddDefaultHandlers(server) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + home, err := GetWorkspaceHomeDir(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, "/Workspace/Users/tester@databricks.com", home) +} + +// An assumed group has no /Workspace/Users folder, so the home lookup fails with the +// same error as the bundle commands. +func TestGetWorkspaceHomeDirRejectsAssumedGroup(t *testing.T) { + server := testserver.New(t) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return map[string]any{"group": map[string]any{"group_id": "3000033333", "group_name": "data-engineers"}} + }) + testserver.AddDefaultHandlers(server) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + home, err := GetWorkspaceHomeDir(t.Context(), w) + require.ErrorIs(t, err, iamutil.ErrGroupPrincipal) + assert.Empty(t, home) +} diff --git a/libs/databrickscfg/cfgpickers/clusters.go b/libs/databrickscfg/cfgpickers/clusters.go index 0c752f3a4b1..d5b77691908 100644 --- a/libs/databrickscfg/cfgpickers/clusters.go +++ b/libs/databrickscfg/cfgpickers/clusters.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/compute" "github.com/databricks/databricks-sdk-go/service/iam" @@ -140,7 +141,7 @@ func loadInteractiveClusters(ctx context.Context, w *databricks.WorkspaceClient, if err != nil { return nil, fmt.Errorf("list clusters: %w", err) } - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUser(ctx, w) if err != nil { return nil, fmt.Errorf("current user: %w", err) } diff --git a/libs/databrickscfg/cfgpickers/clusters_test.go b/libs/databrickscfg/cfgpickers/clusters_test.go index 1c462eace05..b57b3157edc 100644 --- a/libs/databrickscfg/cfgpickers/clusters_test.go +++ b/libs/databrickscfg/cfgpickers/clusters_test.go @@ -7,7 +7,6 @@ import ( "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/qa" "github.com/databricks/databricks-sdk-go/service/compute" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -116,13 +115,7 @@ func TestFirstCompatibleCluster(t *testing.T) { }, }, }, - { - Method: "GET", - Resource: "/api/2.0/preview/scim/v2/Me?", - Response: iam.User{ - UserName: "serge", - }, - }, + identityMeFixture(), { Method: "GET", Resource: "/api/2.1/clusters/spark-versions", @@ -163,13 +156,7 @@ func TestNoCompatibleClusters(t *testing.T) { }, }, }, - { - Method: "GET", - Resource: "/api/2.0/preview/scim/v2/Me?", - Response: iam.User{ - UserName: "serge", - }, - }, + identityMeFixture(), { Method: "GET", Resource: "/api/2.1/clusters/spark-versions", @@ -190,3 +177,12 @@ func TestNoCompatibleClusters(t *testing.T) { _, err := AskForCluster(ctx, w, WithDatabricksConnect("13.1")) require.Equal(t, ErrNoCompatibleClusters, err) } + +// identityMeFixture answers the current-user probe as user serge. +func identityMeFixture() qa.HTTPFixture { + return qa.HTTPFixture{ + Method: "GET", + Resource: "/api/2.0/identity/me", + Response: map[string]any{"user": map[string]string{"user_id": "1000012345", "username": "serge"}}, + } +} diff --git a/libs/iamutil/current_user.go b/libs/iamutil/current_user.go index 2908c8e19a2..87d71d4675d 100644 --- a/libs/iamutil/current_user.go +++ b/libs/iamutil/current_user.go @@ -3,36 +3,295 @@ package iamutil import ( "context" "errors" + "fmt" "net/http" + "slices" + "strings" "time" + "github.com/databricks/cli/libs/auth" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" + databricksclient "github.com/databricks/databricks-sdk-go/client" "github.com/databricks/databricks-sdk-go/retries" "github.com/databricks/databricks-sdk-go/service/iam" + "github.com/databricks/databricks-sdk-go/service/iamv2" ) -// meRetryTimeout bounds how long GetCurrentUser retries transient 500s. -const meRetryTimeout = 30 * time.Second +const ( + // meRetryTimeout bounds how long GetCurrentUser retries transient 500s. + meRetryTimeout = 30 * time.Second -// GetCurrentUser returns the current user, retrying on HTTP 500 responses. + identityMePath = "/api/2.0/identity/me" + identityMeTransitiveParentGroupsPath = "/api/2.0/identity/me/transitive-parent-groups" +) + +// ErrMeNotEnabled is wrapped by the identity/me errors of a workspace that does not serve the +// API yet. Callers check it with errors.Is to show the message as the diagnostic. +var ErrMeNotEnabled = errors.New("the identity/me API is not enabled in this workspace, so the current user cannot be read; use an older Databricks CLI version for now") + +// meNotEnabledCodes are the error codes identity/me returns in a workspace that does not serve +// the API yet. The 404 and 501 statuses are matched through apierr.ErrNotFound and ErrNotImplemented. +var meNotEnabledCodes = []string{"ENDPOINT_NOT_FOUND", "FEATURE_DISABLED", "NOT_IMPLEMENTED"} + +// ErrGroupPrincipal is returned by GetUserPrincipal and GetUserPrincipalOnce when the current request is +// authorized by an assumed group, which has no user identity to deploy as. +var ErrGroupPrincipal = errors.New("the current identity is an assumed group, but this command needs a user or service principal") + +// identityMe is the principal returned by GET /api/2.0/identity/me. Exactly one +// of the three fields is set. Groups and parent groups are not returned inline. +type identityMe struct { + User *iamv2.User `json:"user,omitempty"` + Group *iamv2.Group `json:"group,omitempty"` + ServicePrincipal *iamv2.ServicePrincipal `json:"service_principal,omitempty"` +} + +// currentPrincipal is identity/me as an iam.User, with group set for an assumed group. +type currentPrincipal struct { + user *iam.User + group bool +} + +// GetCurrentUser returns the current user, retrying on HTTP 500 responses. It reads the +// authorizing principal of the current request from GET /api/2.0/identity/me, which also +// resolves an assumed group. Groups, Roles and Entitlements are not set; CurrentUserGroups +// reads the groups. // -// CurrentUser.Me is a read-only endpoint, so retrying any 500 is safe. The -// Databricks SCIM API intermittently returns "500 TEMPORARILY_UNAVAILABLE" (a -// condition it otherwise serves as 503), and the SDK retries 503 but not 500 — -// see the "some API's recommend retries on HTTP 500, but we'll add that later" -// comment on (*apierr.APIError).IsRetriable in databricks-sdk-go/apierr. This -// call runs at the start of every bundle command, so an unretried 500 fails the -// whole command; we close that gap here. +// identity/me is read-only, so retrying a 500 is safe. The SDK retries 503 but not 500, and +// this call runs at the start of every bundle command, so an unretried 500 would fail it. func GetCurrentUser(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { - return retries.Poll(ctx, meRetryTimeout, func() (*iam.User, *retries.Err) { - user, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + p, err := pollIdentityMe(ctx, w) + if err != nil { + return nil, err + } + return p.user, nil +} + +// GetCurrentUserOnce is GetCurrentUser with a single attempt, for probes that should +// fail fast rather than wait out transient 500s. +func GetCurrentUserOnce(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { + p, err := getIdentityMeOnce(ctx, w) + if err != nil { + return nil, err + } + return p.user, nil +} + +// GetUserPrincipal is GetCurrentUser for commands that act as the current user, such +// as bundle deployments and paths under /Workspace/Users. It returns ErrGroupPrincipal +// when the caller is an assumed group. +func GetUserPrincipal(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { + p, err := pollIdentityMe(ctx, w) + if err != nil { + return nil, err + } + if p.group { + return nil, ErrGroupPrincipal + } + return p.user, nil +} + +// GetUserPrincipalOnce is GetUserPrincipal with a single attempt, for probes that should +// fail fast rather than wait out transient 500s. +func GetUserPrincipalOnce(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { + p, err := getIdentityMeOnce(ctx, w) + if err != nil { + return nil, err + } + if p.group { + return nil, ErrGroupPrincipal + } + return p.user, nil +} + +// pollIdentityMe is getIdentityMe that retries HTTP 500 responses for meRetryTimeout. +func pollIdentityMe(ctx context.Context, w *databricks.WorkspaceClient) (*currentPrincipal, error) { + c, err := newIdentityClient(w) + if err != nil { + return nil, err + } + return retryOn500(ctx, func() (*currentPrincipal, error) { + return getIdentityMe(ctx, c) + }) +} + +// getIdentityMeOnce is getIdentityMe with a single attempt. +func getIdentityMeOnce(ctx context.Context, w *databricks.WorkspaceClient) (*currentPrincipal, error) { + c, err := newIdentityClient(w) + if err != nil { + return nil, err + } + return getIdentityMe(ctx, c) +} + +// retryOn500 runs get, retrying HTTP 500 responses for meRetryTimeout. Other errors fail at once. +func retryOn500[T any](ctx context.Context, get func() (T, error)) (T, error) { + res, err := retries.Poll(ctx, meRetryTimeout, func() (*T, *retries.Err) { + v, err := get() if err == nil { - return user, nil + return &v, nil } if apiErr, ok := errors.AsType[*apierr.APIError](err); ok && apiErr.StatusCode == http.StatusInternalServerError { return nil, retries.Continue(err) } return nil, retries.Halt(err) }) + if err != nil { + var zero T + return zero, err + } + return *res, nil +} + +// meNotEnabled reports whether err means the workspace does not serve identity/me yet. +func meNotEnabled(err error) bool { + apiErr, ok := errors.AsType[*apierr.APIError](err) + if !ok { + return false + } + return errors.Is(err, apierr.ErrNotFound) || errors.Is(err, apierr.ErrNotImplemented) || + slices.Contains(meNotEnabledCodes, apiErr.ErrorCode) +} + +// identityClient sends GETs to the identity endpoints. Build one per command and reuse it +// across retries and pages. +type identityClient struct { + client *databricksclient.DatabricksClient + headers map[string]string +} + +func newIdentityClient(w *databricks.WorkspaceClient) (*identityClient, error) { + c, err := databricksclient.New(w.Config) + if err != nil { + return nil, err + } + return &identityClient{client: c, headers: auth.WorkspaceIDHeaders(w.Config)}, nil +} + +// get sends a GET to path and decodes the response into out. +func (c *identityClient) get(ctx context.Context, path string, query map[string]any, out any) error { + return c.client.Do(ctx, http.MethodGet, path, c.headers, query, nil, out) +} + +func getIdentityMe(ctx context.Context, c *identityClient) (*currentPrincipal, error) { + var me identityMe + if err := c.get(ctx, identityMePath, nil, &me); err != nil { + if meNotEnabled(err) { + return nil, fmt.Errorf("%w: %w", ErrMeNotEnabled, err) + } + return nil, err + } + user, err := meToUser(&me) + if err != nil { + return nil, err + } + return ¤tPrincipal{user: user, group: me.Group != nil}, nil +} + +// meToUser maps a principal from identity/me onto an iam.User, setting Id, UserName, +// Name, DisplayName, Active, ExternalId and Emails. identity/me returns the given and +// family names separately, so Name is taken as is (nil when both are empty) and +// DisplayName joins them. A service principal's UserName is its application ID, not its +// name. +func meToUser(me *identityMe) (*iam.User, error) { + switch { + case me.User != nil: + u := me.User + var name *iam.Name + if u.FullName.GivenName != "" || u.FullName.FamilyName != "" { + name = &iam.Name{GivenName: u.FullName.GivenName, FamilyName: u.FullName.FamilyName} + } + return &iam.User{ + Id: u.UserId, + UserName: u.Username, + Name: name, + DisplayName: strings.TrimSpace(u.FullName.GivenName + " " + u.FullName.FamilyName), + Active: u.AccountUserStatus == iamv2.StateActive, + ExternalId: u.ExternalId, + Emails: []iam.ComplexValue{{Value: u.Username, Primary: true}}, + }, nil + case me.ServicePrincipal != nil: + sp := me.ServicePrincipal + return &iam.User{ + Id: sp.ServicePrincipalId, + UserName: sp.ApplicationId, + DisplayName: sp.DisplayName, + Active: sp.AccountSpStatus == iamv2.StateActive, + ExternalId: sp.ExternalId, + }, nil + case me.Group != nil: + g := me.Group + return &iam.User{ + Id: g.GroupId, + UserName: g.GroupName, + DisplayName: g.GroupName, + }, nil + default: + return nil, errors.New("identity/me returned no principal") + } +} + +// parentGroup is one entry of ListMeTransitiveParentGroupsResponse. The keys follow the +// IAM v2 proto, not the generic SDK response, which decodes a different key. +type parentGroup struct { + GroupId string `json:"group_id"` + GroupName string `json:"group_name"` +} + +// listCurrentUserParentGroups returns the groups the current principal belongs to, +// directly or through parent groups, following every page. The response also names +// workspace-local groups such as admins and users, which the account group list lacks. +func listCurrentUserParentGroups(ctx context.Context, w *databricks.WorkspaceClient) ([]parentGroup, error) { + c, err := newIdentityClient(w) + if err != nil { + return nil, err + } + var groups []parentGroup + pageToken := "" + seenTokens := map[string]bool{} + for { + query := map[string]any{} + if pageToken != "" { + query["page_token"] = pageToken + } + var page struct { + Groups []parentGroup `json:"me_transitive_parent_groups"` + NextPageToken string `json:"next_page_token,omitempty"` + } + _, err = retryOn500(ctx, func() (struct{}, error) { + return struct{}{}, c.get(ctx, identityMeTransitiveParentGroupsPath, query, &page) + }) + if err != nil { + return nil, err + } + groups = append(groups, page.Groups...) + if page.NextPageToken == "" { + return groups, nil + } + // Following a token the server already sent would loop until the context ends. + if seenTokens[page.NextPageToken] { + return nil, errors.New("identity/me returned a repeated page token for the parent groups") + } + seenTokens[page.NextPageToken] = true + pageToken = page.NextPageToken + } +} + +// CurrentUserGroups returns the groups the current principal belongs to, directly or +// through parent groups, as iam.ComplexValue entries with the group ID as Value and the +// group name as Display, sorted by Display. It is one paged call and never reads the +// account group list. +func CurrentUserGroups(ctx context.Context, w *databricks.WorkspaceClient) ([]iam.ComplexValue, error) { + parents, err := listCurrentUserParentGroups(ctx, w) + if err != nil { + return nil, err + } + var groups []iam.ComplexValue + for _, g := range parents { + groups = append(groups, iam.ComplexValue{Value: g.GroupId, Display: g.GroupName}) + } + slices.SortFunc(groups, func(a, b iam.ComplexValue) int { + return strings.Compare(a.Display, b.Display) + }) + return groups, nil } diff --git a/libs/iamutil/current_user_test.go b/libs/iamutil/current_user_test.go index decf8eef936..7ed8629efee 100644 --- a/libs/iamutil/current_user_test.go +++ b/libs/iamutil/current_user_test.go @@ -2,38 +2,332 @@ package iamutil import ( "net/http" + "sync/atomic" "testing" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" - "github.com/databricks/databricks-sdk-go/experimental/mocks" "github.com/databricks/databricks-sdk-go/service/iam" + "github.com/databricks/databricks-sdk-go/service/iamv2" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) +// newIdentityMeWorkspace returns a workspace client whose identity/me is answered by handler. +func newIdentityMeWorkspace(t *testing.T, handler func(req testserver.Request) testserver.Response) *databricks.WorkspaceClient { + t.Helper() + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMePath, func(req testserver.Request) any { + return handler(req) + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + return w +} + +func TestMeToUser(t *testing.T) { + tests := []struct { + name string + me identityMe + want *iam.User + wantErr bool + }{ + { + name: "user", + me: identityMe{User: &iamv2.User{ + UserId: "1000012345", + Username: "alice@example.com", + FullName: iamv2.UserFullName{GivenName: "Alice", FamilyName: "Smith"}, + AccountUserStatus: iamv2.StateActive, + ExternalId: "ext-1", + }}, + want: &iam.User{ + Id: "1000012345", + UserName: "alice@example.com", + Name: &iam.Name{GivenName: "Alice", FamilyName: "Smith"}, + DisplayName: "Alice Smith", + Active: true, + ExternalId: "ext-1", + Emails: []iam.ComplexValue{{Value: "alice@example.com", Primary: true}}, + }, + }, + { + name: "user without full name and inactive", + me: identityMe{User: &iamv2.User{ + UserId: "1000012345", + Username: "alice@example.com", + AccountUserStatus: iamv2.StateInactive, + }}, + want: &iam.User{ + Id: "1000012345", + UserName: "alice@example.com", + Emails: []iam.ComplexValue{{Value: "alice@example.com", Primary: true}}, + }, + }, + { + name: "service principal reports application id as user name", + me: identityMe{ServicePrincipal: &iamv2.ServicePrincipal{ + ServicePrincipalId: "2000022222", + ApplicationId: "aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee", + DisplayName: "deploy-sp", + AccountSpStatus: iamv2.StateActive, + }}, + want: &iam.User{ + Id: "2000022222", + UserName: "aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee", + DisplayName: "deploy-sp", + Active: true, + }, + }, + { + name: "assumed group", + me: identityMe{Group: &iamv2.Group{GroupId: "3000033333", GroupName: "data-engineers"}}, + want: &iam.User{ + Id: "3000033333", + UserName: "data-engineers", + DisplayName: "data-engineers", + }, + }, + { + name: "no principal", + me: identityMe{}, + wantErr: true, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got, err := meToUser(&tc.me) + if tc.wantErr { + require.Error(t, err) + return + } + require.NoError(t, err) + assert.Equal(t, tc.want, got) + }) + } +} + func TestGetCurrentUserRetriesOn500(t *testing.T) { - m := mocks.NewMockWorkspaceClient(t) - me := m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything) - me.Return(nil, &apierr.APIError{StatusCode: http.StatusInternalServerError, ErrorCode: "TEMPORARILY_UNAVAILABLE"}).Once() - me.Return(&iam.User{UserName: "test-user"}, nil).Once() + var calls atomic.Int32 + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + if calls.Add(1) == 1 { + return testserver.Response{StatusCode: http.StatusInternalServerError, Body: map[string]any{"error_code": "TEMPORARILY_UNAVAILABLE"}} + } + return testserver.Response{Body: map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}}} + }) - user, err := GetCurrentUser(t.Context(), m.WorkspaceClient) + user, err := GetCurrentUser(t.Context(), w) require.NoError(t, err) assert.Equal(t, "test-user", user.UserName) + assert.Equal(t, int32(2), calls.Load()) } func TestGetCurrentUserDoesNotRetryOn403(t *testing.T) { - m := mocks.NewMockWorkspaceClient(t) - // A non-500 error must fail immediately, without a retry (Once enforces it). - m.GetMockCurrentUserAPI().EXPECT(). - Me(mock.Anything, mock.Anything). - Return(nil, &apierr.APIError{StatusCode: http.StatusForbidden, Message: "denied"}). - Once() - - _, err := GetCurrentUser(t.Context(), m.WorkspaceClient) - require.Error(t, err) + var calls atomic.Int32 + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + calls.Add(1) + return testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}} + }) + + _, err := GetCurrentUser(t.Context(), w) var apiErr *apierr.APIError require.ErrorAs(t, err, &apiErr) assert.Equal(t, http.StatusForbidden, apiErr.StatusCode) + // A non-500 error must fail immediately, without a retry. + assert.Equal(t, int32(1), calls.Load()) +} + +func TestListCurrentUserParentGroupsFollowsPages(t *testing.T) { + var pages []string + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMeTransitiveParentGroupsPath, func(req testserver.Request) any { + token := req.URL.Query().Get("page_token") + pages = append(pages, token) + // The fixture bodies use the proto keys; a wrong key decodes to no groups. + if token == "" { + return map[string]any{ + "me_transitive_parent_groups": []map[string]any{{"group_id": "11", "group_name": "eng"}, {"group_id": "12", "group_name": "ops"}}, + "next_page_token": "page2", + } + } + return map[string]any{ + "me_transitive_parent_groups": []map[string]any{{"group_id": "13", "group_name": "users"}}, + } + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + groups, err := listCurrentUserParentGroups(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, []parentGroup{ + {GroupId: "11", GroupName: "eng"}, + {GroupId: "12", GroupName: "ops"}, + {GroupId: "13", GroupName: "users"}, + }, groups) + assert.Equal(t, []string{"", "page2"}, pages) +} + +func TestCurrentUserGroupsResolvesNamesSorted(t *testing.T) { + server := testserver.New(t) + testserver.AddDefaultHandlers(server) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + server.AddGroups("test-token", []string{"zeta", "alpha"}, nil) + + groups, err := CurrentUserGroups(t.Context(), w) + require.NoError(t, err) + require.Len(t, groups, 3) + assert.Equal(t, "alpha", groups[0].Display) + assert.Equal(t, "users", groups[1].Display) + assert.Equal(t, "zeta", groups[2].Display) + assert.NotEmpty(t, groups[0].Value) +} + +// Workspace-local groups such as admins and users are named by the parent-groups +// response itself, so they resolve without any account group read. +func TestCurrentUserGroupsIncludesWorkspaceLocalGroups(t *testing.T) { + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMeTransitiveParentGroupsPath, func(testserver.Request) any { + return map[string]any{"me_transitive_parent_groups": []map[string]any{ + {"group_id": "86546749069356", "group_name": "users"}, + {"group_id": "79621542766305", "group_name": "account users"}, + {"group_id": "90000000000001", "group_name": "admins"}, + }} + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + groups, err := CurrentUserGroups(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, []iam.ComplexValue{ + {Value: "79621542766305", Display: "account users"}, + {Value: "90000000000001", Display: "admins"}, + {Value: "86546749069356", Display: "users"}, + }, groups) +} + +// A failed parent-groups read fails the lookup, so no partial list is returned as the +// complete membership. +func TestCurrentUserGroupsFailsWhenParentGroupsFail(t *testing.T) { + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMeTransitiveParentGroupsPath, func(testserver.Request) any { + return testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}} + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + groups, err := CurrentUserGroups(t.Context(), w) + require.ErrorContains(t, err, "denied") + assert.Nil(t, groups) +} + +// A transient 500 on the parent-groups call is retried, as the identity/me call is. +func TestCurrentUserGroupsRetriesOn500(t *testing.T) { + var calls atomic.Int32 + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMeTransitiveParentGroupsPath, func(testserver.Request) any { + if calls.Add(1) == 1 { + return testserver.Response{StatusCode: http.StatusInternalServerError, Body: map[string]any{"error_code": "TEMPORARILY_UNAVAILABLE"}} + } + return map[string]any{"me_transitive_parent_groups": []map[string]any{{"group_id": "11", "group_name": "eng"}}} + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + groups, err := CurrentUserGroups(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, []iam.ComplexValue{{Value: "11", Display: "eng"}}, groups) + assert.Equal(t, int32(2), calls.Load()) +} + +func TestGetUserPrincipalRejectsAssumedGroup(t *testing.T) { + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + return testserver.Response{Body: map[string]any{"group": map[string]any{"group_id": "3000033333", "group_name": "data-engineers"}}} + }) + + user, err := GetUserPrincipal(t.Context(), w) + require.ErrorIs(t, err, ErrGroupPrincipal) + assert.Nil(t, user) + + // Commands that only identify the caller still get the group. + group, err := GetCurrentUser(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, "data-engineers", group.UserName) +} + +func TestGetUserPrincipalReturnsUser(t *testing.T) { + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + return testserver.Response{Body: map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}}} + }) + + user, err := GetUserPrincipal(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, "test-user", user.UserName) +} + +// GetCurrentUserOnce makes a single attempt, so a probe fails fast on a transient 500. +func TestGetCurrentUserOnceDoesNotRetryOn500(t *testing.T) { + var calls atomic.Int32 + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + calls.Add(1) + return testserver.Response{StatusCode: http.StatusInternalServerError, Body: map[string]any{"error_code": "TEMPORARILY_UNAVAILABLE"}} + }) + + _, err := GetCurrentUserOnce(t.Context(), w) + require.Error(t, err) + assert.Equal(t, int32(1), calls.Load()) +} + +func TestGetCurrentUserReportsIdentityMeNotEnabled(t *testing.T) { + tests := []struct { + name string + status int + errorCode string + wantIs error + }{ + {name: "not found", status: http.StatusNotFound, errorCode: "ENDPOINT_NOT_FOUND", wantIs: apierr.ErrNotFound}, + {name: "not implemented", status: http.StatusNotImplemented, errorCode: "NOT_IMPLEMENTED", wantIs: apierr.ErrNotImplemented}, + {name: "feature disabled", status: http.StatusBadRequest, errorCode: "FEATURE_DISABLED"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + return testserver.Response{StatusCode: tc.status, Body: map[string]any{"error_code": tc.errorCode, "message": "Me API is not enabled."}} + }) + + _, err := GetCurrentUser(t.Context(), w) + assert.ErrorContains(t, err, "the identity/me API is not enabled in this workspace") + assert.ErrorContains(t, err, "use an older Databricks CLI version") + assert.ErrorContains(t, err, "Me API is not enabled.") + if tc.wantIs != nil { + assert.ErrorIs(t, err, tc.wantIs) + } + }) + } +} + +func TestListCurrentUserParentGroupsRejectsRepeatedPageToken(t *testing.T) { + var calls atomic.Int32 + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMeTransitiveParentGroupsPath, func(req testserver.Request) any { + calls.Add(1) + // A server bug that keeps returning the same next page token must not loop forever. + return map[string]any{ + "me_transitive_parent_groups": []map[string]any{{"group_id": "11", "group_name": "eng"}}, + "next_page_token": "page2", + } + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + _, err = listCurrentUserParentGroups(t.Context(), w) + assert.ErrorContains(t, err, "repeated page token") + assert.Equal(t, int32(2), calls.Load()) } diff --git a/libs/sync/path.go b/libs/sync/path.go index d85699cf55c..977ed6b1899 100644 --- a/libs/sync/path.go +++ b/libs/sync/path.go @@ -6,6 +6,7 @@ import ( "path" "strings" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/retry" "github.com/databricks/databricks-sdk-go" @@ -41,7 +42,7 @@ func EnsureRemotePathIsUsable(ctx context.Context, wsc *databricks.WorkspaceClie // TODO: we should cache CurrentUser.Me at the SDK level // for now we let clients pass in any existing user they might already have if me == nil { - me, err = wsc.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err = iamutil.GetUserPrincipal(ctx, wsc) if err != nil { return err } diff --git a/libs/template/helpers.go b/libs/template/helpers.go index 7330a129b41..ad2d9b7160e 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -172,7 +172,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { "user_name": func() (string, error) { if cachedUser == nil { var err error - cachedUser, err = w.CurrentUser.Me(ctx, iam.MeRequest{}) + cachedUser, err = iamutil.GetUserPrincipal(ctx, w) if err != nil { return "", err } @@ -186,7 +186,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { "short_name": func() (string, error) { if cachedUser == nil { var err error - cachedUser, err = w.CurrentUser.Me(ctx, iam.MeRequest{}) + cachedUser, err = iamutil.GetUserPrincipal(ctx, w) if err != nil { return "", err } @@ -217,7 +217,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { } if cachedUser == nil { var err error - cachedUser, err = w.CurrentUser.Me(ctx, iam.MeRequest{}) + cachedUser, err = iamutil.GetUserPrincipal(ctx, w) if err != nil { return false, err } diff --git a/libs/testserver/fake_workspace.go b/libs/testserver/fake_workspace.go index e4a16f275fa..b2320fbb453 100644 --- a/libs/testserver/fake_workspace.go +++ b/libs/testserver/fake_workspace.go @@ -52,10 +52,13 @@ const ( // (not yet visible), and a deleted synced table stays in DELETING instead of // disappearing (so the direct engine's post-delete poll has a teardown to wait out). EventualConsistencyTokenPrefix = "dbapi3" - UserID = "1000012345" - TestDefaultClusterId = "0123-456789-cluster0" - TestDefaultWarehouseId = "8ec9edc1-db0c-40df-af8d-7580020fe61e" - TestDefaultInstancePoolId = "0123-456789-pool0" + // AssumedGroupTokenPrefix marks a caller authorized by an assumed group: identity/me + // returns that group as the principal. + AssumedGroupTokenPrefix = "dbapi4" + UserID = "1000012345" + TestDefaultClusterId = "0123-456789-cluster0" + TestDefaultWarehouseId = "8ec9edc1-db0c-40df-af8d-7580020fe61e" + TestDefaultInstancePoolId = "0123-456789-pool0" ) var TestUser = iam.User{ @@ -78,10 +81,16 @@ func isGuestToken(token string) bool { return strings.HasPrefix(token, GuestServicePrincipalTokenPrefix) } +// isServicePrincipalToken reports whether a token authenticates a service principal +// (primary or guest). +func isServicePrincipalToken(token string) bool { + return strings.HasPrefix(token, ServicePrincipalTokenPrefix) || isGuestToken(token) +} + // userForToken returns the identity behind a token: any service-principal token // (primary or guest) is the SP, otherwise the user. func userForToken(token string) iam.User { - if strings.HasPrefix(token, ServicePrincipalTokenPrefix) || isGuestToken(token) { + if isServicePrincipalToken(token) { return TestUserSP } return TestUser @@ -97,6 +106,34 @@ func (s *FakeWorkspace) MeUser(token string) iam.User { return user } +// AssumedGroupId and AssumedGroupName identify the group an AssumedGroupTokenPrefix token acts as. +const ( + AssumedGroupId = "42" + AssumedGroupName = "data-engineers" +) + +// IdentityMe returns the GET /api/2.0/identity/me body for a token. It describes the +// same identity as MeUser, as a user, a service principal or an assumed group. +func (s *FakeWorkspace) IdentityMe(token string) any { + if strings.HasPrefix(token, AssumedGroupTokenPrefix) { + return map[string]any{"group": map[string]any{"group_id": AssumedGroupId, "group_name": AssumedGroupName}} + } + me := s.MeUser(token) + if isServicePrincipalToken(token) { + return map[string]any{"service_principal": map[string]any{ + "service_principal_id": me.Id, + "application_id": me.UserName, + "display_name": me.DisplayName, + "account_sp_status": "ACTIVE", + }} + } + return map[string]any{"user": map[string]any{ + "user_id": me.Id, + "username": me.UserName, + "account_user_status": "ACTIVE", + }} +} + var ( idMutex sync.Mutex lastNowNano int64 @@ -286,6 +323,9 @@ type FakeWorkspace struct { Permissions map[string]iam.ObjectPermissions Groups map[string]iam.Group + // groupMembers holds the IDs of the groups the caller belongs to; the parent-groups + // endpoint lists exactly these. + groupMembers map[string]bool Repos map[string]workspace.RepoInfo @@ -577,6 +617,7 @@ func NewFakeWorkspace(url, token string) *FakeWorkspace { WorkspaceSettings: map[string]settingsv2.Setting{}, Permissions: map[string]iam.ObjectPermissions{}, Groups: map[string]iam.Group{}, + groupMembers: map[string]bool{}, DatabaseInstances: map[string]database.DatabaseInstance{}, DatabaseCatalogs: map[string]database.DatabaseCatalog{}, SyncedDatabaseTables: map[string]database.SyncedDatabaseTable{}, diff --git a/libs/testserver/groups.go b/libs/testserver/groups.go index 5074708657e..78207254638 100644 --- a/libs/testserver/groups.go +++ b/libs/testserver/groups.go @@ -3,6 +3,8 @@ package testserver import ( "encoding/json" "fmt" + "maps" + "slices" "strconv" "github.com/databricks/databricks-sdk-go/service/iam" @@ -33,7 +35,51 @@ func (s *FakeWorkspace) GroupsCreate(req Request) Response { s.Groups[groupId] = group + // A member that is the caller makes the caller a member of the new group, so acceptance + // tests can set group membership declaratively. + for _, member := range request.Members { + if member.Value == userForToken(req.Token).Id { + s.groupMembers[groupId] = true + } + } + return Response{ Body: group, } } + +// usersGroupId is the workspace-local users group. Every workspace identity is a member of it, +// so the fake lists it for every caller, as a real workspace does. +const usersGroupId = "1" + +// IdentityMeTransitiveParentGroups answers GET /api/2.0/identity/me/transitive-parent-groups +// with the groups the caller belongs to, named as the server names them. +func (s *FakeWorkspace) IdentityMeTransitiveParentGroups() any { + defer s.LockUnlock()() + + ids := slices.Sorted(maps.Keys(s.groupMembers)) + parents := make([]map[string]any, 0, len(ids)+1) + parents = append(parents, map[string]any{"group_id": usersGroupId, "group_name": "users"}) + for _, id := range ids { + parents = append(parents, map[string]any{"group_id": id, "group_name": s.Groups[id].DisplayName}) + } + return map[string]any{"me_transitive_parent_groups": parents} +} + +// AddGroups creates a group for each name in member and each name in nonMember in the +// workspace behind token, and makes that identity a member of the groups in member. +func (s *Server) AddGroups(token string, member, nonMember []string) { + ws := s.getWorkspaceForToken(token) + defer ws.LockUnlock()() + for _, name := range slices.Concat(member, nonMember) { + id := strconv.FormatInt(nextID(), 10) + ws.Groups[id] = iam.Group{Id: id, DisplayName: name} + } + for _, name := range member { + for id, group := range ws.Groups { + if group.DisplayName == name { + ws.groupMembers[id] = true + } + } + } +} diff --git a/libs/testserver/handlers.go b/libs/testserver/handlers.go index 87219299844..0ed4bf6cef7 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -251,6 +251,14 @@ func AddDefaultHandlers(server *Server) { } }) + server.Handle("GET", "/api/2.0/identity/me", func(req Request) any { + return req.Workspace.IdentityMe(req.Token) + }) + + server.Handle("GET", "/api/2.0/identity/me/transitive-parent-groups", func(req Request) any { + return req.Workspace.IdentityMeTransitiveParentGroups() + }) + server.Handle("GET", "/api/2.0/workspace/get-status", func(req Request) any { path := req.URL.Query().Get("path") returnGitInfo := req.URL.Query().Get("return_git_info") == "true"