Skip to content

Commit 84da35b

Browse files
authored
Added support for using new databricks CLI with backwards compatibility check (#121)
## Changes <!-- Summary of your changes that are easy to understand --> We changed naming from bricks to databricks, updating references to use new cli -- binary, examples, env variables, methods name ## Tests <!-- How is this tested? --> CI + @cdkrot for verification on resolution with existing issue
1 parent 113bdb3 commit 84da35b

File tree

6 files changed

+109
-75
lines changed

6 files changed

+109
-75
lines changed

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

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.databricks.sdk.core;
2+
3+
import com.databricks.sdk.core.oauth.Token;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.*;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public class DatabricksCliCredentialsProvider implements CredentialsProvider {
14+
15+
private static final Logger LOG = LoggerFactory.getLogger(DatabricksCliCredentialsProvider.class);
16+
17+
public static final String DATABRICKS_CLI = "databricks-cli";
18+
19+
@Override
20+
public String authType() {
21+
return DATABRICKS_CLI;
22+
}
23+
24+
private CliTokenSource getDatabricksCliTokenSource(DatabricksConfig config) {
25+
String cliPath = config.getDatabricksCliPath();
26+
if (cliPath == null) {
27+
cliPath = "databricks";
28+
}
29+
// If the path is unqualified, look it up in PATH.
30+
if (!cliPath.contains("/")) {
31+
cliPath = findExecutable(cliPath);
32+
}
33+
List<String> cmd =
34+
new ArrayList<>(Arrays.asList(cliPath, "auth", "token", "--host", config.getHost()));
35+
if (config.isAccountClient()) {
36+
cmd.add("--account-id");
37+
cmd.add(config.getAccountId());
38+
}
39+
return new CliTokenSource(cmd, "token_type", "access_token", "expiry", config::getAllEnv);
40+
}
41+
42+
private static String findExecutable(String name) {
43+
String pathVal = System.getenv("PATH");
44+
StringTokenizer stringTokenizer = new StringTokenizer(pathVal, File.pathSeparator);
45+
while (stringTokenizer.hasMoreTokens()) {
46+
Path path = Paths.get(stringTokenizer.nextToken(), name).toAbsolutePath().normalize();
47+
if (!Files.isRegularFile(path)) {
48+
continue;
49+
}
50+
long size;
51+
try {
52+
size = Files.size(path);
53+
} catch (IOException e) {
54+
LOG.warn("Unable to get size of databricks cli: " + e.getMessage());
55+
return null;
56+
}
57+
if (size < 1024 * 1024) {
58+
LOG.info("Databricks CLI version <0.100.0 detected");
59+
return null;
60+
}
61+
return path.toString();
62+
}
63+
LOG.warn("Most likely the databricks CLI is not installed");
64+
return null;
65+
}
66+
67+
@Override
68+
public HeaderFactory configure(DatabricksConfig config) {
69+
String host = config.getHost();
70+
if (host == null || !config.isAws()) {
71+
return null;
72+
}
73+
74+
try {
75+
CliTokenSource tokenSource = getDatabricksCliTokenSource(config);
76+
tokenSource.getToken(); // We need this for checking if databricks CLI is installed.
77+
return () -> {
78+
Token token = tokenSource.getToken();
79+
Map<String, String> headers = new HashMap<>();
80+
headers.put("Authorization", token.getTokenType() + " " + token.getAccessToken());
81+
return headers;
82+
};
83+
} catch (DatabricksException e) {
84+
String stderr = e.getMessage();
85+
if (stderr.contains("not found")) {
86+
LOG.warn("Most likely databricks CLI is not installed");
87+
return null;
88+
}
89+
if (stderr.contains("databricks OAuth is not")) {
90+
LOG.info("OAuth not configured or not available");
91+
return null;
92+
}
93+
throw e;
94+
}
95+
}
96+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public class DatabricksConfig {
106106
auth = "azure")
107107
private String azureLoginAppId;
108108

109-
@ConfigAttribute(value = "bricks_cli_path", env = "BRICKS_CLI_PATH")
110-
private String bricksCliPath;
109+
@ConfigAttribute(value = "databricks_cli_path", env = "DATABRICKS_CLI_PATH")
110+
private String databricksCliPath;
111111

112112
/**
113113
* When multiple auth attributes are available in the environment, use the auth type specified by
@@ -228,12 +228,12 @@ public DatabricksConfig setAccountId(String accountId) {
228228
return this;
229229
}
230230

231-
public String getBricksCliPath() {
232-
return this.bricksCliPath;
231+
public String getDatabricksCliPath() {
232+
return this.databricksCliPath;
233233
}
234234

235-
public DatabricksConfig setBricksCliPath(String bricksCliPath) {
236-
this.bricksCliPath = bricksCliPath;
235+
public DatabricksConfig setDatabricksCliPath(String databricksCliPath) {
236+
this.databricksCliPath = databricksCliPath;
237237
return this;
238238
}
239239

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public DefaultCredentialsProvider() {
2828
new AzureServicePrincipalCredentialsProvider(),
2929
new AzureCliCredentialsProvider(),
3030
new ExternalBrowserCredentialsProvider(),
31-
new BricksCliCredentialsProvider());
31+
new DatabricksCliCredentialsProvider());
3232
}
3333

3434
@Override

examples/cli-auth-demo/src/main/java/com/databricks/example/CliAuthAccount.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
Example for authenticating with Databricks Account through CLI.
9-
The authentication type can be set to either "bricks-cli" or "azure-cli".
9+
The authentication type can be set to either "databricks-cli" or "azure-cli".
1010
For details on authenticating via bricks cli, please see: <a href="https://docs.databricks.com/dev-tools/cli/auth-commands.html">...</a>
1111
*/
1212
public class CliAuthAccount {
@@ -17,8 +17,8 @@ public class CliAuthAccount {
1717
private static DatabricksConfig getConfig() {
1818
// Change to "azure-cli" if you want to authenticate through azure cli
1919
// Please authenticate using cli before using them in SDK.
20-
// Example: $ bricks auth login --host <host> --account-id <accountId>
21-
String authType = "bricks-cli";
20+
// Example: $ databricks auth login --host <host> --account-id <accountId>
21+
String authType = "databricks-cli";
2222
String profile = "";
2323
return new DatabricksConfig()
2424
.setAuthType(authType)

examples/cli-auth-demo/src/main/java/com/databricks/example/CliAuthWorkspace.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/**
99
Example for authenticating with Databricks Workspace through CLI.
10-
The authentication type can be set to either "bricks-cli" or "azure-cli".
10+
The authentication type can be set to either "databricks-cli" or "azure-cli".
1111
For details on authenticating via bricks cli, please see: <a href="https://docs.databricks.com/dev-tools/cli/auth-commands.html">...</a>
1212
*/
1313
public class CliAuthWorkspace {
@@ -18,8 +18,8 @@ public class CliAuthWorkspace {
1818
private static DatabricksConfig getConfig() {
1919
// Change to "azure-cli" if you want to authenticate through azure cli
2020
// Please authenticate using cli before using them in SDK.
21-
// Example: $ bricks auth login --host <host>
22-
String authType = "bricks-cli";
21+
// Example: $ databricks auth login --host <host>
22+
String authType = "databricks-cli";
2323
String profile = "";
2424
return new DatabricksConfig()
2525
.setAuthType(authType)

0 commit comments

Comments
 (0)