|
| 1 | +package com.databricks.sdk.core.oauth; |
| 2 | + |
| 3 | +import com.databricks.sdk.core.http.HttpClient; |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.concurrent.ConcurrentHashMap; |
| 6 | + |
| 7 | +/** |
| 8 | + * Manages and provides Databricks data plane tokens. This class is responsible for acquiring and |
| 9 | + * caching OAuth tokens that are specific to a particular Databricks data plane service endpoint and |
| 10 | + * a set of authorization details. It utilizes a {@link DatabricksOAuthTokenSource} for obtaining |
| 11 | + * control plane tokens, which may then be exchanged or used to authorize requests for data plane |
| 12 | + * tokens. Cached {@link EndpointTokenSource} instances are used to efficiently reuse tokens for |
| 13 | + * repeated requests to the same endpoint with the same authorization context. |
| 14 | + */ |
| 15 | +public class DataPlaneTokenSource { |
| 16 | + private final HttpClient httpClient; |
| 17 | + private final TokenSource cpTokenSource; |
| 18 | + private final String host; |
| 19 | + private final ConcurrentHashMap<TokenSourceKey, EndpointTokenSource> sourcesCache; |
| 20 | + /** |
| 21 | + * Caching key for {@link EndpointTokenSource}, based on endpoint and authorization details. This |
| 22 | + * is a value object that uniquely identifies a token source configuration. |
| 23 | + */ |
| 24 | + private static final class TokenSourceKey { |
| 25 | + /** The target service endpoint URL. */ |
| 26 | + private final String endpoint; |
| 27 | + |
| 28 | + /** Specific authorization details for the endpoint. */ |
| 29 | + private final String authDetails; |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructs a TokenSourceKey. |
| 33 | + * |
| 34 | + * @param endpoint The target service endpoint URL. |
| 35 | + * @param authDetails Specific authorization details. |
| 36 | + */ |
| 37 | + public TokenSourceKey(String endpoint, String authDetails) { |
| 38 | + this.endpoint = endpoint; |
| 39 | + this.authDetails = authDetails; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public boolean equals(Object o) { |
| 44 | + if (this == o) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + if (o == null || getClass() != o.getClass()) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + TokenSourceKey that = (TokenSourceKey) o; |
| 51 | + return Objects.equals(endpoint, that.endpoint) |
| 52 | + && Objects.equals(authDetails, that.authDetails); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int hashCode() { |
| 57 | + return Objects.hash(endpoint, authDetails); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Constructs a DataPlaneTokenSource. |
| 63 | + * |
| 64 | + * @param httpClient The {@link HttpClient} for token requests. |
| 65 | + * @param cpTokenSource The {@link TokenSource} for control plane tokens. |
| 66 | + * @param host The host for the token exchange request. |
| 67 | + * @throws NullPointerException if any parameter is null. |
| 68 | + * @throws IllegalArgumentException if the host is empty. |
| 69 | + */ |
| 70 | + public DataPlaneTokenSource(HttpClient httpClient, TokenSource cpTokenSource, String host) { |
| 71 | + this.httpClient = Objects.requireNonNull(httpClient, "HTTP client cannot be null"); |
| 72 | + this.cpTokenSource = |
| 73 | + Objects.requireNonNull(cpTokenSource, "Control plane token source cannot be null"); |
| 74 | + this.host = Objects.requireNonNull(host, "Host cannot be null"); |
| 75 | + |
| 76 | + if (host.isEmpty()) { |
| 77 | + throw new IllegalArgumentException("Host cannot be empty"); |
| 78 | + } |
| 79 | + this.sourcesCache = new ConcurrentHashMap<>(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Retrieves a token for the specified endpoint and authorization details. It uses a cached {@link |
| 84 | + * EndpointTokenSource} if available, otherwise creates and caches a new one. |
| 85 | + * |
| 86 | + * @param endpoint The target data plane service endpoint. |
| 87 | + * @param authDetails Authorization details for the endpoint. |
| 88 | + * @return The dataplane {@link Token}. |
| 89 | + * @throws NullPointerException if either parameter is null. |
| 90 | + * @throws IllegalArgumentException if either parameter is empty. |
| 91 | + * @throws DatabricksException if the token request fails. |
| 92 | + */ |
| 93 | + public Token getToken(String endpoint, String authDetails) { |
| 94 | + Objects.requireNonNull(endpoint, "Data plane endpoint URL cannot be null"); |
| 95 | + Objects.requireNonNull(authDetails, "Authorization details cannot be null"); |
| 96 | + |
| 97 | + if (endpoint.isEmpty()) { |
| 98 | + throw new IllegalArgumentException("Data plane endpoint URL cannot be empty"); |
| 99 | + } |
| 100 | + if (authDetails.isEmpty()) { |
| 101 | + throw new IllegalArgumentException("Authorization details cannot be empty"); |
| 102 | + } |
| 103 | + |
| 104 | + TokenSourceKey key = new TokenSourceKey(endpoint, authDetails); |
| 105 | + |
| 106 | + EndpointTokenSource specificSource = |
| 107 | + sourcesCache.computeIfAbsent( |
| 108 | + key, |
| 109 | + k -> |
| 110 | + new EndpointTokenSource( |
| 111 | + this.cpTokenSource, k.authDetails, this.httpClient, this.host)); |
| 112 | + |
| 113 | + return specificSource.getToken(); |
| 114 | + } |
| 115 | +} |
0 commit comments