Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Add cache for oidc endpoint #349

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -12,6 +12,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.HttpMessage;

public class DatabricksConfig {
Expand Down Expand Up @@ -139,6 +140,7 @@ public class DatabricksConfig {

private Environment env;

ConcurrentHashMap<String, OpenIDConnectEndpoints> oidcEndpointsCache = new ConcurrentHashMap<>();
private DatabricksEnvironment databricksEnvironment;

public Environment getEnv() {
Expand Down Expand Up @@ -374,13 +376,17 @@ public DatabricksConfig setAzureUseMsi(boolean azureUseMsi) {
return this;
}

/** @deprecated Use {@link #getAzureUseMsi()} instead. */
/**
* @deprecated Use {@link #getAzureUseMsi()} instead.
*/
@Deprecated()
public boolean getAzureUseMSI() {
return azureUseMsi;
}

/** @deprecated Use {@link #setAzureUseMsi(boolean)} instead. */
/**
* @deprecated Use {@link #setAzureUseMsi(boolean)} instead.
*/
@Deprecated
public DatabricksConfig setAzureUseMSI(boolean azureUseMsi) {
this.azureUseMsi = azureUseMsi;
Expand Down Expand Up @@ -545,20 +551,7 @@ public OpenIDConnectEndpoints getOidcEndpoints() throws IOException {
if (discoveryUrl == null) {
return fetchDefaultOidcEndpoints();
}
return fetchOidcEndpointsFromDiscovery();
}

private OpenIDConnectEndpoints fetchOidcEndpointsFromDiscovery() {
try {
Request request = new Request("GET", discoveryUrl);
Response resp = getHttpClient().execute(request);
if (resp.getStatusCode() == 200) {
return new ObjectMapper().readValue(resp.getBody(), OpenIDConnectEndpoints.class);
}
} catch (IOException e) {
throw ConfigLoader.makeNicerError(e.getMessage(), e, this);
}
return null;
return fetchOidcEndpointWithCache(discoveryUrl);
}

private OpenIDConnectEndpoints fetchDefaultOidcEndpoints() throws IOException {
Expand All @@ -582,11 +575,7 @@ private OpenIDConnectEndpoints fetchDefaultOidcEndpoints() throws IOException {
}

String oidcEndpoint = getHost() + "/oidc/.well-known/oauth-authorization-server";
Response resp = getHttpClient().execute(new Request("GET", oidcEndpoint));
if (resp.getStatusCode() != 200) {
return null;
}
return new ObjectMapper().readValue(resp.getBody(), OpenIDConnectEndpoints.class);
return fetchOidcEndpointWithCache(oidcEndpoint);
}

@Override
Expand Down Expand Up @@ -656,4 +645,20 @@ public DatabricksConfig newWithWorkspaceHost(String host) {
newConfig.setHost(host);
return newConfig;
}

private OpenIDConnectEndpoints fetchOidcEndpointWithCache(String oidcEndpoint) {
return oidcEndpointsCache.computeIfAbsent(
oidcEndpoint,
key -> {
try {
Response resp = getHttpClient().execute(new Request("GET", oidcEndpoint));
if (resp.getStatusCode() != 200) {
return null;
}
return new ObjectMapper().readValue(resp.getBody(), OpenIDConnectEndpoints.class);
} catch (IOException e) {
throw ConfigLoader.makeNicerError(e.getMessage(), e, this);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public HeaderFactory configure(DatabricksConfig config) {
|| config.getHost() == null) {
return null;
}
// TODO: Add cache in getOidcEndpoints
// TODO: Azure returns 404 for UC workspace after redirecting to
// https://login.microsoftonline.com/{cfg.azure_tenant_id}/.well-known/oauth-authorization-server
try {
Expand Down
Loading