Skip to content

Commit ee789e5

Browse files
error if scopes set explicitly with databricks-cli auth
1 parent cbe7ea3 commit ee789e5

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksCliCredentialsProvider.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public class DatabricksCliCredentialsProvider implements CredentialsProvider {
1515

1616
public static final String DATABRICKS_CLI = "databricks-cli";
1717

18+
static final String ERR_CUSTOM_SCOPES_NOT_SUPPORTED =
19+
"custom scopes are not supported with databricks-cli auth; "
20+
+ "scopes are determined by what was last used when logging in with `databricks auth login`";
21+
1822
@Override
1923
public String authType() {
2024
return DATABRICKS_CLI;
@@ -74,6 +78,10 @@ public OAuthHeaderFactory configure(DatabricksConfig config) {
7478
return null;
7579
}
7680

81+
if (config.isScopesExplicitlySet()) {
82+
throw new DatabricksException(ERR_CUSTOM_SCOPES_NOT_SUPPORTED);
83+
}
84+
7785
CachedTokenSource cachedTokenSource =
7886
new CachedTokenSource.Builder(tokenSource)
7987
.setAsyncDisabled(config.getDisableAsyncTokenRefresh())

databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public class DatabricksConfig {
5050
@ConfigAttribute(auth = "oauth")
5151
private List<String> scopes;
5252

53+
// Temporary field to track if scopes were explicitly set by the user.
54+
// This is used to ensure users don't set explicit scopes when using
55+
// `databricks-cli` auth, as it does not respect the scopes.
56+
// TODO: Remove this field once the `auth token` command supports scopes.
57+
private boolean scopesExplicitlySet = false;
58+
5359
@ConfigAttribute(env = "DATABRICKS_REDIRECT_URL", auth = "oauth")
5460
private String redirectUrl;
5561

@@ -430,9 +436,14 @@ public List<String> getScopes() {
430436

431437
public DatabricksConfig setScopes(List<String> scopes) {
432438
this.scopes = scopes;
439+
this.scopesExplicitlySet = true;
433440
return this;
434441
}
435442

443+
public boolean isScopesExplicitlySet() {
444+
return scopesExplicitlySet;
445+
}
446+
436447
public String getProfile() {
437448
return profile;
438449
}

databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksCliCredentialsProviderTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.databricks.sdk.core;
22

3+
import static com.databricks.sdk.core.DatabricksCliCredentialsProvider.ERR_CUSTOM_SCOPES_NOT_SUPPORTED;
34
import static org.junit.jupiter.api.Assertions.*;
45

56
import java.util.Arrays;
@@ -139,4 +140,50 @@ void testBuildCliCommand_UnifiedHostFalse_WithAccountHost() {
139140
CLI_PATH, "auth", "token", "--host", ACCOUNT_HOST, "--account-id", ACCOUNT_ID),
140141
cmd);
141142
}
143+
144+
@Test
145+
void testConfigure_ErrorsWhenScopesExplicitlySet() {
146+
DatabricksConfig config =
147+
new DatabricksConfig()
148+
.setHost(HOST)
149+
.setDatabricksCliPath(CLI_PATH)
150+
.setScopes(Arrays.asList("sql"));
151+
152+
DatabricksException e =
153+
assertThrows(DatabricksException.class, () -> provider.configure(config));
154+
assertEquals(ERR_CUSTOM_SCOPES_NOT_SUPPORTED, e.getMessage());
155+
}
156+
157+
@Test
158+
void testConfigure_SkipsWhenCliNotFoundEvenWithScopes() {
159+
// When CLI is not available, the provider should return null (skip)
160+
// rather than throwing an error about scopes.
161+
DatabricksConfig config =
162+
new DatabricksConfig()
163+
.setHost(HOST)
164+
.setScopes(Arrays.asList("sql"));
165+
166+
assertNull(provider.configure(config));
167+
}
168+
169+
@Test
170+
void testConfigure_NoErrorWhenNoScopes() {
171+
DatabricksConfig config = new DatabricksConfig().setHost(HOST);
172+
173+
try {
174+
provider.configure(config);
175+
} catch (Exception e) {
176+
// May fail for other reasons (CLI not found, env not set), but must not be the scope error
177+
assertNotEquals(ERR_CUSTOM_SCOPES_NOT_SUPPORTED, e.getMessage());
178+
}
179+
}
180+
181+
@Test
182+
void testScopesExplicitlySetFlag() {
183+
DatabricksConfig config = new DatabricksConfig();
184+
assertFalse(config.isScopesExplicitlySet());
185+
186+
config.setScopes(Arrays.asList("sql", "clusters"));
187+
assertTrue(config.isScopesExplicitlySet());
188+
}
142189
}

0 commit comments

Comments
 (0)