Skip to content

Commit c37f21f

Browse files
committed
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
1 parent 74b5010 commit c37f21f

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
import java.time.Duration;
1919
import java.util.*;
2020
import org.apache.http.HttpMessage;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
2123

2224
public class DatabricksConfig {
25+
private static final Logger LOG = LoggerFactory.getLogger(DatabricksConfig.class);
26+
2327
private CredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
2428

2529
@ConfigAttribute(env = "DATABRICKS_HOST")
@@ -219,12 +223,28 @@ private synchronized DatabricksConfig innerResolve() {
219223
sortScopes();
220224
ConfigLoader.fixHostIfNeeded(this);
221225
initHttp();
226+
tryResolveHostMetadata();
222227
return this;
223228
} catch (DatabricksException e) {
224229
throw ConfigLoader.makeNicerError(e.getMessage(), e, this);
225230
}
226231
}
227232

233+
/**
234+
* Attempts to resolve host metadata from the well-known endpoint. Logs a warning and continues if
235+
* metadata resolution fails, since not all hosts support the discovery endpoint.
236+
*/
237+
private void tryResolveHostMetadata() {
238+
if (host == null) {
239+
return;
240+
}
241+
try {
242+
resolveHostMetadata();
243+
} catch (Exception e) {
244+
LOG.debug("Failed to resolve host metadata: {}", e.getMessage());
245+
}
246+
}
247+
228248
// Sort scopes in-place for better de-duplication in the refresh token cache.
229249
private void sortScopes() {
230250
if (scopes != null && !scopes.isEmpty()) {
@@ -962,6 +982,9 @@ private DatabricksConfig clone(Set<String> fieldsToSkip) {
962982
if (fieldsToSkip.contains(f.getName())) {
963983
continue;
964984
}
985+
if (java.lang.reflect.Modifier.isStatic(f.getModifiers())) {
986+
continue;
987+
}
965988
try {
966989
f.set(newConfig, f.get(this));
967990
} catch (IllegalAccessException e) {

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public void testToStringWithEnv() {
9999
public void testWorkspaceLevelOidcEndpointsWithAccountId() throws IOException {
100100
try (FixtureServer server =
101101
new FixtureServer()
102+
.with("GET", "/.well-known/databricks-config", "{}", 404)
102103
.with(
103104
"GET",
104105
"/oidc/.well-known/oauth-authorization-server",
@@ -118,6 +119,7 @@ public void testWorkspaceLevelOidcEndpointsWithAccountId() throws IOException {
118119
public void testWorkspaceLevelOidcEndpointsRetries() throws IOException {
119120
try (FixtureServer server =
120121
new FixtureServer()
122+
.with("GET", "/.well-known/databricks-config", "{}", 404)
121123
.with("GET", "/oidc/.well-known/oauth-authorization-server", "", 429)
122124
.with(
123125
"GET",
@@ -443,7 +445,9 @@ public void testGetHostMetadataWorkspaceStaticOidcEndpoint() throws IOException
443445
+ DUMMY_WORKSPACE_ID
444446
+ "\"}";
445447
try (FixtureServer server =
446-
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
448+
new FixtureServer()
449+
.with("GET", "/.well-known/databricks-config", response, 200)
450+
.with("GET", "/.well-known/databricks-config", response, 200)) {
447451
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
448452
config.resolve(emptyEnv());
449453
HostMetadata meta = config.getHostMetadata();
@@ -458,7 +462,9 @@ public void testGetHostMetadataAccountRawOidcTemplate() throws IOException {
458462
String response =
459463
"{\"oidc_endpoint\":\"https://acc.databricks.com/oidc/accounts/{account_id}\"}";
460464
try (FixtureServer server =
461-
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
465+
new FixtureServer()
466+
.with("GET", "/.well-known/databricks-config", response, 200)
467+
.with("GET", "/.well-known/databricks-config", response, 200)) {
462468
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
463469
config.resolve(emptyEnv());
464470
HostMetadata meta = config.getHostMetadata();
@@ -471,7 +477,9 @@ public void testGetHostMetadataAccountRawOidcTemplate() throws IOException {
471477
@Test
472478
public void testGetHostMetadataRaisesOnHttpError() throws IOException {
473479
try (FixtureServer server =
474-
new FixtureServer().with("GET", "/.well-known/databricks-config", "{}", 404)) {
480+
new FixtureServer()
481+
.with("GET", "/.well-known/databricks-config", "{}", 404)
482+
.with("GET", "/.well-known/databricks-config", "{}", 404)) {
475483
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
476484
config.resolve(emptyEnv());
477485
DatabricksException ex =
@@ -493,7 +501,9 @@ public void testResolveHostMetadataWorkspacePopulatesAllFields() throws IOExcept
493501
+ DUMMY_WORKSPACE_ID
494502
+ "\"}";
495503
try (FixtureServer server =
496-
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
504+
new FixtureServer()
505+
.with("GET", "/.well-known/databricks-config", response, 200)
506+
.with("GET", "/.well-known/databricks-config", response, 200)) {
497507
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
498508
config.resolve(emptyEnv());
499509
config.resolveHostMetadata();
@@ -508,7 +518,9 @@ public void testResolveHostMetadataAccountSubstitutesAccountId() throws IOExcept
508518
String response =
509519
"{\"oidc_endpoint\":\"https://acc.databricks.com/oidc/accounts/{account_id}\"}";
510520
try (FixtureServer server =
511-
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
521+
new FixtureServer()
522+
.with("GET", "/.well-known/databricks-config", response, 200)
523+
.with("GET", "/.well-known/databricks-config", response, 200)) {
512524
DatabricksConfig config =
513525
new DatabricksConfig().setHost(server.getUrl()).setAccountId(DUMMY_ACCOUNT_ID);
514526
config.resolve(emptyEnv());
@@ -527,7 +539,9 @@ public void testResolveHostMetadataDoesNotOverwriteExistingFields() throws IOExc
527539
+ "\"account_id\":\"other-account\","
528540
+ "\"workspace_id\":\"other-ws\"}";
529541
try (FixtureServer server =
530-
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
542+
new FixtureServer()
543+
.with("GET", "/.well-known/databricks-config", response, 200)
544+
.with("GET", "/.well-known/databricks-config", response, 200)) {
531545
DatabricksConfig config =
532546
new DatabricksConfig()
533547
.setHost(server.getUrl())
@@ -545,7 +559,9 @@ public void testResolveHostMetadataRaisesWhenAccountIdUnresolvable() throws IOEx
545559
String response =
546560
"{\"oidc_endpoint\":\"https://acc.databricks.com/oidc/accounts/{account_id}\"}";
547561
try (FixtureServer server =
548-
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
562+
new FixtureServer()
563+
.with("GET", "/.well-known/databricks-config", response, 200)
564+
.with("GET", "/.well-known/databricks-config", response, 200)) {
549565
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
550566
config.resolve(emptyEnv());
551567
DatabricksException ex =
@@ -558,7 +574,9 @@ public void testResolveHostMetadataRaisesWhenAccountIdUnresolvable() throws IOEx
558574
public void testResolveHostMetadataRaisesWhenOidcEndpointMissing() throws IOException {
559575
String response = "{\"account_id\":\"" + DUMMY_ACCOUNT_ID + "\"}";
560576
try (FixtureServer server =
561-
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
577+
new FixtureServer()
578+
.with("GET", "/.well-known/databricks-config", response, 200)
579+
.with("GET", "/.well-known/databricks-config", response, 200)) {
562580
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
563581
config.resolve(emptyEnv());
564582
DatabricksException ex =
@@ -570,7 +588,9 @@ public void testResolveHostMetadataRaisesWhenOidcEndpointMissing() throws IOExce
570588
@Test
571589
public void testResolveHostMetadataRaisesOnHttpError() throws IOException {
572590
try (FixtureServer server =
573-
new FixtureServer().with("GET", "/.well-known/databricks-config", "{}", 500)) {
591+
new FixtureServer()
592+
.with("GET", "/.well-known/databricks-config", "{}", 500)
593+
.with("GET", "/.well-known/databricks-config", "{}", 500)) {
574594
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
575595
config.resolve(emptyEnv());
576596
DatabricksException ex =

0 commit comments

Comments
 (0)