-
Notifications
You must be signed in to change notification settings - Fork 36
Validate Databricks CLI token scopes against SDK configuration #689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tejaskochar-db
wants to merge
6
commits into
main
Choose a base branch
from
scopes-cli-auth-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+288
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ee789e5
error if scopes set explicitly with databricks-cli auth
tejaskochar-db dd13d45
Merge branch 'main' into scopes-cli-auth-error
tejaskochar-db 2a1667e
Validate Databricks CLI token scopes against SDK configuration
tejaskochar-db dd6ee4e
Fix formatting and add changelog entry
tejaskochar-db 1cb43a3
fixes / improvements
tejaskochar-db b19225a
fix fmt
tejaskochar-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...icks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksCliScopeValidationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package com.databricks.sdk.core; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import com.databricks.sdk.core.oauth.Token; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Instant; | ||
| import java.util.*; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| class DatabricksCliScopeValidationTest { | ||
|
|
||
| private static final String HOST = "https://my-workspace.cloud.databricks.com"; | ||
| private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
|
||
| /** Builds a fake JWT (header.payload.signature) with the given claims. */ | ||
| private static String makeJwt(Map<String, Object> claims) { | ||
| try { | ||
| String header = | ||
| Base64.getUrlEncoder() | ||
| .withoutPadding() | ||
| .encodeToString("{\"alg\":\"none\"}".getBytes(StandardCharsets.UTF_8)); | ||
| String payload = | ||
| Base64.getUrlEncoder().withoutPadding().encodeToString(MAPPER.writeValueAsBytes(claims)); | ||
| return header + "." + payload + ".sig"; | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private static Token makeToken(Map<String, Object> claims) { | ||
| return new Token(makeJwt(claims), "Bearer", Instant.now().plusSeconds(3600)); | ||
| } | ||
|
|
||
| static List<Arguments> scopeValidationCases() { | ||
| return Arrays.asList( | ||
| // Exact match (offline_access filtered out). | ||
| Arguments.of( | ||
| Collections.singletonMap("scope", "sql offline_access"), | ||
| Collections.singletonList("sql"), | ||
| false, | ||
| "match"), | ||
| // Mismatch throws. | ||
| Arguments.of( | ||
| Collections.singletonMap("scope", "all-apis offline_access"), | ||
| Collections.singletonList("sql"), | ||
| true, | ||
| "mismatch"), | ||
| // offline_access on token only — still equivalent. | ||
| Arguments.of( | ||
| Collections.singletonMap("scope", "all-apis offline_access"), | ||
| Collections.singletonList("all-apis"), | ||
| false, | ||
| "offline_access_on_token_only"), | ||
| // offline_access in config only — still equivalent. | ||
| Arguments.of( | ||
| Collections.singletonMap("scope", "all-apis"), | ||
| Arrays.asList("all-apis", "offline_access"), | ||
| false, | ||
| "offline_access_in_config_only"), | ||
| // Scope claim as list instead of string. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we handle both list and string? |
||
| Arguments.of( | ||
| new HashMap<String, Object>() { | ||
| { | ||
| put("scope", Arrays.asList("sql", "offline_access")); | ||
| } | ||
| }, | ||
| Collections.singletonList("sql"), | ||
| false, | ||
| "scope_as_list")); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{3}") | ||
| @MethodSource("scopeValidationCases") | ||
| void testScopeValidation( | ||
| Map<String, Object> tokenClaims, | ||
| List<String> configuredScopes, | ||
| boolean expectError, | ||
| String testName) { | ||
| Token token = makeToken(tokenClaims); | ||
|
|
||
| if (expectError) { | ||
| assertThrows( | ||
| DatabricksCliCredentialsProvider.ScopeMismatchException.class, | ||
| () -> | ||
| DatabricksCliCredentialsProvider.validateTokenScopes(token, configuredScopes, HOST)); | ||
| } else { | ||
tejaskochar-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assertDoesNotThrow( | ||
| () -> | ||
| DatabricksCliCredentialsProvider.validateTokenScopes(token, configuredScopes, HOST)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testNoScopeClaimSkipsValidation() { | ||
| Token token = makeToken(Collections.singletonMap("sub", "user@example.com")); | ||
| assertDoesNotThrow( | ||
| () -> | ||
| DatabricksCliCredentialsProvider.validateTokenScopes( | ||
| token, Collections.singletonList("sql"), HOST)); | ||
| } | ||
|
|
||
| @Test | ||
| void testNonJwtTokenSkipsValidation() { | ||
| Token token = new Token("opaque-token-string", "Bearer", Instant.now().plusSeconds(3600)); | ||
| assertDoesNotThrow( | ||
| () -> | ||
| DatabricksCliCredentialsProvider.validateTokenScopes( | ||
| token, Collections.singletonList("sql"), HOST)); | ||
| } | ||
|
|
||
| @Test | ||
| void testErrorMessageContainsReauthCommand() { | ||
| Token token = makeToken(Collections.singletonMap("scope", "all-apis")); | ||
| DatabricksCliCredentialsProvider.ScopeMismatchException e = | ||
| assertThrows( | ||
| DatabricksCliCredentialsProvider.ScopeMismatchException.class, | ||
| () -> | ||
| DatabricksCliCredentialsProvider.validateTokenScopes( | ||
| token, Arrays.asList("sql", "offline_access"), HOST)); | ||
| assertTrue( | ||
| e.getMessage().contains("databricks auth login"), | ||
| "Expected re-auth command in error message, got: " + e.getMessage()); | ||
| assertTrue( | ||
| e.getMessage().contains("do not match the configured scopes"), | ||
| "Expected scope mismatch details in error message, got: " + e.getMessage()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this case different from the first case?