|
| 1 | +package com.databricks.sdk.core.oauth; |
| 2 | + |
| 3 | +import com.databricks.sdk.core.DatabricksException; |
| 4 | +import com.databricks.sdk.core.http.HttpClient; |
| 5 | +import com.databricks.sdk.core.http.Request; |
| 6 | +import com.databricks.sdk.core.http.Response; |
| 7 | +import com.databricks.sdk.support.InternalApi; |
| 8 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 9 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import java.io.IOException; |
| 12 | +import java.time.Instant; |
| 13 | + |
| 14 | +/** |
| 15 | + * A {@link TokenSource} that fetches OAuth tokens from the Azure Instance Metadata Service (IMDS) |
| 16 | + * endpoint for Managed Service Identity (MSI) authentication. |
| 17 | + * |
| 18 | + * <p>This token source makes HTTP GET requests to the well-known IMDS endpoint at {@code |
| 19 | + * http://169.254.169.254/metadata/identity/oauth2/token} to obtain access tokens for the specified |
| 20 | + * Azure resource. |
| 21 | + */ |
| 22 | +@InternalApi |
| 23 | +public class AzureMsiTokenSource implements TokenSource { |
| 24 | + |
| 25 | + private static final String IMDS_ENDPOINT = |
| 26 | + "http://169.254.169.254/metadata/identity/oauth2/token"; |
| 27 | + |
| 28 | + private final HttpClient httpClient; |
| 29 | + private final String resource; |
| 30 | + private final String clientId; |
| 31 | + private final ObjectMapper mapper = new ObjectMapper(); |
| 32 | + |
| 33 | + /** Response structure from the Azure IMDS token endpoint. */ |
| 34 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 35 | + static class MsiTokenResponse { |
| 36 | + @JsonProperty("token_type") |
| 37 | + private String tokenType; |
| 38 | + |
| 39 | + @JsonProperty("access_token") |
| 40 | + private String accessToken; |
| 41 | + |
| 42 | + @JsonProperty("expires_on") |
| 43 | + private String expiresOn; |
| 44 | + |
| 45 | + Token toToken() { |
| 46 | + if (accessToken == null || accessToken.isEmpty()) { |
| 47 | + throw new DatabricksException("MSI token response missing or empty 'access_token' field"); |
| 48 | + } |
| 49 | + if (tokenType == null || tokenType.isEmpty()) { |
| 50 | + throw new DatabricksException("MSI token response missing or empty 'token_type' field"); |
| 51 | + } |
| 52 | + if (expiresOn == null || expiresOn.isEmpty()) { |
| 53 | + throw new DatabricksException("MSI token response missing 'expires_on' field"); |
| 54 | + } |
| 55 | + long epoch; |
| 56 | + try { |
| 57 | + epoch = Long.parseLong(expiresOn); |
| 58 | + } catch (NumberFormatException e) { |
| 59 | + throw new DatabricksException( |
| 60 | + "Invalid 'expires_on' value in MSI token response: " + expiresOn, e); |
| 61 | + } |
| 62 | + return new Token(accessToken, tokenType, Instant.ofEpochSecond(epoch)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Creates a new AzureMsiTokenSource. |
| 68 | + * |
| 69 | + * @param httpClient The HTTP client to use for requests to the IMDS endpoint. |
| 70 | + * @param resource The Azure resource for which to obtain an access token. |
| 71 | + * @param clientId The client ID of the managed identity to use. May be null for system-assigned |
| 72 | + * identities. |
| 73 | + */ |
| 74 | + public AzureMsiTokenSource(HttpClient httpClient, String resource, String clientId) { |
| 75 | + this.httpClient = httpClient; |
| 76 | + this.resource = resource; |
| 77 | + this.clientId = clientId; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Token getToken() { |
| 82 | + Request req = new Request("GET", IMDS_ENDPOINT); |
| 83 | + req.withQueryParam("api-version", "2018-02-01"); |
| 84 | + req.withQueryParam("resource", resource); |
| 85 | + if (clientId != null && !clientId.isEmpty()) { |
| 86 | + req.withQueryParam("client_id", clientId); |
| 87 | + } |
| 88 | + req.withHeader("Metadata", "true"); |
| 89 | + |
| 90 | + Response resp; |
| 91 | + try { |
| 92 | + resp = httpClient.execute(req); |
| 93 | + } catch (IOException e) { |
| 94 | + throw new DatabricksException( |
| 95 | + "Failed to request MSI token from IMDS endpoint: " + e.getMessage(), e); |
| 96 | + } |
| 97 | + |
| 98 | + if (resp.getStatusCode() != 200) { |
| 99 | + throw new DatabricksException( |
| 100 | + "Failed to request MSI token: status code " |
| 101 | + + resp.getStatusCode() |
| 102 | + + ", response body: " |
| 103 | + + resp.getDebugBody()); |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + MsiTokenResponse msiToken = mapper.readValue(resp.getBody(), MsiTokenResponse.class); |
| 108 | + return msiToken.toToken(); |
| 109 | + } catch (IOException e) { |
| 110 | + throw new DatabricksException("Failed to parse MSI token response: " + e.getMessage(), e); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments