Skip to content

Commit acb210f

Browse files
committed
Resolve TokenAudience from default_oidc_audience in host metadata
Signed-off-by: Tanmay Rustagi <tanmay.rustagi@databricks.com>
1 parent dff706f commit acb210f

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,17 @@ void resolveHostMetadata() throws IOException {
931931
discoveryUrl = oidcUri.resolve(".well-known/oauth-authorization-server").toString();
932932
LOG.debug("Resolved discovery_url from host metadata: \"{}\"", discoveryUrl);
933933
}
934-
// For account hosts, use the accountId as the token audience if not already set.
934+
if (tokenAudience == null
935+
&& meta.getDefaultOidcAudience() != null
936+
&& !meta.getDefaultOidcAudience().isEmpty()) {
937+
LOG.debug(
938+
"Resolved token_audience from host metadata default_oidc_audience: \"{}\"",
939+
meta.getDefaultOidcAudience());
940+
tokenAudience = meta.getDefaultOidcAudience();
941+
}
942+
// Fallback: for account hosts, use the accountId as the token audience if not already set.
935943
if (tokenAudience == null && getClientType() == ClientType.ACCOUNT && accountId != null) {
944+
LOG.debug("Setting token_audience to account_id for account host: \"{}\"", accountId);
936945
tokenAudience = accountId;
937946
}
938947
}

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/HostMetadata.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public class HostMetadata {
2626
@JsonProperty("host_type")
2727
private String hostType;
2828

29+
@JsonProperty("default_oidc_audience")
30+
private String defaultOidcAudience;
31+
2932
public HostMetadata() {}
3033

3134
public HostMetadata(String oidcEndpoint, String accountId, String workspaceId) {
@@ -60,4 +63,8 @@ public String getCloud() {
6063
public String getHostType() {
6164
return hostType;
6265
}
66+
67+
public String getDefaultOidcAudience() {
68+
return defaultOidcAudience;
69+
}
6370
}

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,98 @@ public void testResolveHostMetadataHostTypeUnified() throws IOException {
800800
}
801801
}
802802

803+
// --- resolveHostMetadata default_oidc_audience tests ---
804+
805+
@Test
806+
public void testResolveHostMetadataSetsTokenAudienceFromDefaultOidcAudience() throws IOException {
807+
String response =
808+
"{\"oidc_endpoint\":\"https://ws.databricks.com/oidc\","
809+
+ "\"account_id\":\""
810+
+ DUMMY_ACCOUNT_ID
811+
+ "\","
812+
+ "\"default_oidc_audience\":\"https://ws.databricks.com/oidc/v1/token\"}";
813+
try (FixtureServer server =
814+
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
815+
DatabricksConfig config = new DatabricksConfig().setHost(server.getUrl());
816+
config.resolve(emptyEnv());
817+
config.resolveHostMetadata();
818+
assertEquals("https://ws.databricks.com/oidc/v1/token", config.getTokenAudience());
819+
}
820+
}
821+
822+
@Test
823+
public void testResolveHostMetadataDefaultOidcAudiencePriorityOverAccountIdFallback()
824+
throws IOException {
825+
// default_oidc_audience should take priority over the account_id fallback for account hosts
826+
String response =
827+
"{\"oidc_endpoint\":\"https://acc.databricks.com/oidc/accounts/{account_id}\","
828+
+ "\"account_id\":\""
829+
+ DUMMY_ACCOUNT_ID
830+
+ "\","
831+
+ "\"default_oidc_audience\":\"custom-audience\"}";
832+
try (FixtureServer server =
833+
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
834+
DatabricksConfig config =
835+
new DatabricksConfig()
836+
.setHost(server.getUrl())
837+
.setExperimentalIsUnifiedHost(true)
838+
.setAccountId(DUMMY_ACCOUNT_ID);
839+
// Note: need two fixtures — resolve() consumes first one (unified host triggers
840+
// tryResolveHostMetadata)
841+
// Instead, don't set unified — just test direct call
842+
config = new DatabricksConfig().setHost(server.getUrl()).setAccountId(DUMMY_ACCOUNT_ID);
843+
config.resolve(emptyEnv());
844+
config.resolveHostMetadata();
845+
// Should use default_oidc_audience, NOT account_id
846+
assertEquals("custom-audience", config.getTokenAudience());
847+
}
848+
}
849+
850+
@Test
851+
public void testResolveHostMetadataDoesNotOverrideExistingTokenAudienceWithOidcAudience()
852+
throws IOException {
853+
String response =
854+
"{\"oidc_endpoint\":\"https://ws.databricks.com/oidc\","
855+
+ "\"account_id\":\""
856+
+ DUMMY_ACCOUNT_ID
857+
+ "\","
858+
+ "\"default_oidc_audience\":\"metadata-audience\"}";
859+
try (FixtureServer server =
860+
new FixtureServer().with("GET", "/.well-known/databricks-config", response, 200)) {
861+
DatabricksConfig config =
862+
new DatabricksConfig().setHost(server.getUrl()).setTokenAudience("existing-audience");
863+
config.resolve(emptyEnv());
864+
config.resolveHostMetadata();
865+
assertEquals("existing-audience", config.getTokenAudience());
866+
}
867+
}
868+
869+
@Test
870+
public void testResolveHostMetadataFallsBackToAccountIdWhenNoDefaultOidcAudience()
871+
throws IOException {
872+
// When no default_oidc_audience, should fall back to account_id for account hosts.
873+
// Use unified host flag so getClientType() returns ACCOUNT (no workspaceId).
874+
String response =
875+
"{\"oidc_endpoint\":\"https://acc.databricks.com/oidc/accounts/{account_id}\","
876+
+ "\"account_id\":\""
877+
+ DUMMY_ACCOUNT_ID
878+
+ "\"}";
879+
try (FixtureServer server =
880+
new FixtureServer()
881+
.with("GET", "/.well-known/databricks-config", response, 200)
882+
.with("GET", "/.well-known/databricks-config", response, 200)) {
883+
DatabricksConfig config =
884+
new DatabricksConfig()
885+
.setHost(server.getUrl())
886+
.setAccountId(DUMMY_ACCOUNT_ID)
887+
.setExperimentalIsUnifiedHost(true);
888+
config.resolve(emptyEnv());
889+
// resolve() with unified flag triggers tryResolveHostMetadata() consuming first fixture.
890+
// Now call again with second fixture to verify account_id fallback.
891+
assertEquals(DUMMY_ACCOUNT_ID, config.getTokenAudience());
892+
}
893+
}
894+
803895
// --- discoveryUrl / OIDC endpoint tests ---
804896

805897
@Test

0 commit comments

Comments
 (0)