From c37f21f109e395694b6102a24de2fc9427bb1473 Mon Sep 17 00:00:00 2001 From: Hector Castejon Diaz Date: Thu, 19 Mar 2026 09:14:57 +0000 Subject: [PATCH] Call resolveHostMetadata on Config init Port of Go SDK #1542. Calls resolveHostMetadata() during config resolve() to populate accountId, workspaceId, and discoveryUrl from the host's well-known endpoint. Failures are logged at debug level and do not block initialization. Also fixes clone() to skip static fields (needed for the new Logger field). Co-authored-by: Isaac --- .../databricks/sdk/core/DatabricksConfig.java | 23 +++++++++++ .../sdk/core/DatabricksConfigTest.java | 38 ++++++++++++++----- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java index 627b0ab2f..574bacef4 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java @@ -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") @@ -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()) { @@ -962,6 +982,9 @@ private DatabricksConfig clone(Set 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) { diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java index 378581195..b7f8dab02 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java @@ -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", @@ -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", @@ -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(); @@ -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(); @@ -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 = @@ -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(); @@ -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()); @@ -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()) @@ -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 = @@ -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 = @@ -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 =