Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
import java.time.Duration;
import java.util.*;
import org.apache.http.HttpMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DatabricksConfig {
private static final Logger LOG = LoggerFactory.getLogger(DatabricksConfig.class);

private CredentialsProvider credentialsProvider = new DefaultCredentialsProvider();

@ConfigAttribute(env = "DATABRICKS_HOST")
Expand Down Expand Up @@ -219,12 +223,28 @@ private synchronized DatabricksConfig innerResolve() {
sortScopes();
ConfigLoader.fixHostIfNeeded(this);
initHttp();
tryResolveHostMetadata();
return this;
} catch (DatabricksException e) {
throw ConfigLoader.makeNicerError(e.getMessage(), e, this);
}
}

/**
* Attempts to resolve host metadata from the well-known endpoint. Logs a warning and continues if
* metadata resolution fails, since not all hosts support the discovery endpoint.
*/
private void tryResolveHostMetadata() {
if (host == null) {
return;
}
try {
resolveHostMetadata();
} catch (Exception e) {
LOG.debug("Failed to resolve host metadata: {}", e.getMessage());
}
}

// Sort scopes in-place for better de-duplication in the refresh token cache.
private void sortScopes() {
if (scopes != null && !scopes.isEmpty()) {
Expand Down Expand Up @@ -962,6 +982,9 @@ private DatabricksConfig clone(Set<String> fieldsToSkip) {
if (fieldsToSkip.contains(f.getName())) {
continue;
}
if (java.lang.reflect.Modifier.isStatic(f.getModifiers())) {
continue;
}
try {
f.set(newConfig, f.get(this));
} catch (IllegalAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void testToStringWithEnv() {
public void testWorkspaceLevelOidcEndpointsWithAccountId() throws IOException {
try (FixtureServer server =
new FixtureServer()
.with("GET", "/.well-known/databricks-config", "{}", 404)
.with(
"GET",
"/oidc/.well-known/oauth-authorization-server",
Expand All @@ -118,6 +119,7 @@ public void testWorkspaceLevelOidcEndpointsWithAccountId() throws IOException {
public void testWorkspaceLevelOidcEndpointsRetries() throws IOException {
try (FixtureServer server =
new FixtureServer()
.with("GET", "/.well-known/databricks-config", "{}", 404)
.with("GET", "/oidc/.well-known/oauth-authorization-server", "", 429)
.with(
"GET",
Expand Down Expand Up @@ -443,7 +445,9 @@ public void testGetHostMetadataWorkspaceStaticOidcEndpoint() throws IOException
+ DUMMY_WORKSPACE_ID
+ "\"}";
try (FixtureServer server =
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
new FixtureServer()
.with("GET", "/.well-known/databricks-config", response, 200)
.with("GET", "/.well-known/databricks-config", response, 200)) {
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
config.resolve(emptyEnv());
HostMetadata meta = config.getHostMetadata();
Expand All @@ -458,7 +462,9 @@ public void testGetHostMetadataAccountRawOidcTemplate() throws IOException {
String response =
"{\"oidc_endpoint\":\"https://acc.databricks.com/oidc/accounts/{account_id}\"}";
try (FixtureServer server =
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
new FixtureServer()
.with("GET", "/.well-known/databricks-config", response, 200)
.with("GET", "/.well-known/databricks-config", response, 200)) {
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
config.resolve(emptyEnv());
HostMetadata meta = config.getHostMetadata();
Expand All @@ -471,7 +477,9 @@ public void testGetHostMetadataAccountRawOidcTemplate() throws IOException {
@Test
public void testGetHostMetadataRaisesOnHttpError() throws IOException {
try (FixtureServer server =
new FixtureServer().with("GET", "/.well-known/databricks-config", "{}", 404)) {
new FixtureServer()
.with("GET", "/.well-known/databricks-config", "{}", 404)
.with("GET", "/.well-known/databricks-config", "{}", 404)) {
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
config.resolve(emptyEnv());
DatabricksException ex =
Expand All @@ -493,7 +501,9 @@ public void testResolveHostMetadataWorkspacePopulatesAllFields() throws IOExcept
+ DUMMY_WORKSPACE_ID
+ "\"}";
try (FixtureServer server =
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
new FixtureServer()
.with("GET", "/.well-known/databricks-config", response, 200)
.with("GET", "/.well-known/databricks-config", response, 200)) {
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
config.resolve(emptyEnv());
config.resolveHostMetadata();
Expand All @@ -508,7 +518,9 @@ public void testResolveHostMetadataAccountSubstitutesAccountId() throws IOExcept
String response =
"{\"oidc_endpoint\":\"https://acc.databricks.com/oidc/accounts/{account_id}\"}";
try (FixtureServer server =
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
new FixtureServer()
.with("GET", "/.well-known/databricks-config", response, 200)
.with("GET", "/.well-known/databricks-config", response, 200)) {
DatabricksConfig config =
new DatabricksConfig().setHost(server.getUrl()).setAccountId(DUMMY_ACCOUNT_ID);
config.resolve(emptyEnv());
Expand All @@ -527,7 +539,9 @@ public void testResolveHostMetadataDoesNotOverwriteExistingFields() throws IOExc
+ "\"account_id\":\"other-account\","
+ "\"workspace_id\":\"other-ws\"}";
try (FixtureServer server =
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
new FixtureServer()
.with("GET", "/.well-known/databricks-config", response, 200)
.with("GET", "/.well-known/databricks-config", response, 200)) {
DatabricksConfig config =
new DatabricksConfig()
.setHost(server.getUrl())
Expand All @@ -545,7 +559,9 @@ public void testResolveHostMetadataRaisesWhenAccountIdUnresolvable() throws IOEx
String response =
"{\"oidc_endpoint\":\"https://acc.databricks.com/oidc/accounts/{account_id}\"}";
try (FixtureServer server =
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
new FixtureServer()
.with("GET", "/.well-known/databricks-config", response, 200)
.with("GET", "/.well-known/databricks-config", response, 200)) {
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
config.resolve(emptyEnv());
DatabricksException ex =
Expand All @@ -558,7 +574,9 @@ public void testResolveHostMetadataRaisesWhenAccountIdUnresolvable() throws IOEx
public void testResolveHostMetadataRaisesWhenOidcEndpointMissing() throws IOException {
String response = "{\"account_id\":\"" + DUMMY_ACCOUNT_ID + "\"}";
try (FixtureServer server =
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
new FixtureServer()
.with("GET", "/.well-known/databricks-config", response, 200)
.with("GET", "/.well-known/databricks-config", response, 200)) {
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
config.resolve(emptyEnv());
DatabricksException ex =
Expand All @@ -570,7 +588,9 @@ public void testResolveHostMetadataRaisesWhenOidcEndpointMissing() throws IOExce
@Test
public void testResolveHostMetadataRaisesOnHttpError() throws IOException {
try (FixtureServer server =
new FixtureServer().with("GET", "/.well-known/databricks-config", "{}", 500)) {
new FixtureServer()
.with("GET", "/.well-known/databricks-config", "{}", 500)
.with("GET", "/.well-known/databricks-config", "{}", 500)) {
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
config.resolve(emptyEnv());
DatabricksException ex =
Expand Down
Loading