-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 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 2e2bd35
improve comments, refcator tests and remove unused field
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
Some comments aren't visible on the classic Files Changed page.
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
143 changes: 143 additions & 0 deletions
143
...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,143 @@ | ||
| 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 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. | ||
tejaskochar-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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"), | ||
| // Order should not matter. | ||
| Arguments.of( | ||
| Collections.singletonMap("scope", "clusters sql"), | ||
| Arrays.asList("sql", "clusters"), | ||
| false, | ||
| "multiple_scopes_order_independent"), | ||
| // Partial overlap is still a mismatch. | ||
| Arguments.of( | ||
| Collections.singletonMap("scope", "sql clusters"), | ||
| Arrays.asList("sql", "compute"), | ||
| true, | ||
| "multiple_scopes_partial_overlap_mismatch"), | ||
| // No scope claim in token — validation is skipped. | ||
| Arguments.of( | ||
| Collections.singletonMap("sub", "user@example.com"), | ||
| Collections.singletonList("sql"), | ||
| false, | ||
| "no_scope_claim_skips_validation")); | ||
| } | ||
|
|
||
| @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)); | ||
| } else { | ||
| assertDoesNotThrow( | ||
| () -> DatabricksCliCredentialsProvider.validateTokenScopes(token, configuredScopes)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testNonJwtTokenSkipsValidation() { | ||
| Token token = new Token("opaque-token-string", "Bearer", Instant.now().plusSeconds(3600)); | ||
| assertDoesNotThrow( | ||
| () -> | ||
| DatabricksCliCredentialsProvider.validateTokenScopes( | ||
| token, Collections.singletonList("sql"))); | ||
| } | ||
|
|
||
| @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"))); | ||
| 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()); | ||
| } | ||
|
|
||
| @Test | ||
| void testScopesExplicitlySetFlag() { | ||
| DatabricksConfig config = new DatabricksConfig(); | ||
| assertFalse(config.isScopesExplicitlySet()); | ||
|
|
||
| config.setScopes(Arrays.asList("sql", "clusters")); | ||
| assertTrue(config.isScopesExplicitlySet()); | ||
|
|
||
| config.setScopes(Collections.emptyList()); | ||
| assertFalse(config.isScopesExplicitlySet(), "Empty list should not count as explicitly set"); | ||
|
|
||
| config.setScopes(null); | ||
| assertFalse(config.isScopesExplicitlySet(), "null should not count as explicitly set"); | ||
| } | ||
| } | ||
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.
PR description:
Are we supporting JSON array?
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.
No, I checked manually what databricks uses, we follow the RFC and use space-delimited strings. Forgot to update the PR description.