|
| 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.core.utils.Environment; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 10 | +import com.google.common.base.Strings; |
| 11 | +import java.io.IOException; |
| 12 | + |
| 13 | +/** |
| 14 | + * AzureDevOpsIDTokenSource retrieves JWT Tokens from Azure DevOps Pipelines. This class implements |
| 15 | + * the IDTokenSource interface and provides a method for obtaining ID tokens specifically from Azure |
| 16 | + * DevOps Pipeline environment. |
| 17 | + * |
| 18 | + * <p>This implementation relies on the <a |
| 19 | + * href="https://learn.microsoft.com/en-us/rest/api/azure/devops/distributedtask/oidctoken/create">Azure |
| 20 | + * DevOps OIDC token API</a>. |
| 21 | + */ |
| 22 | +public class AzureDevOpsIDTokenSource implements IDTokenSource { |
| 23 | + /* Access token for authenticating with Azure DevOps API */ |
| 24 | + private final String azureDevOpsAccessToken; |
| 25 | + /* Team Foundation Collection URI (e.g., https://dev.azure.com/organization) */ |
| 26 | + private final String azureDevOpsTeamFoundationCollectionUri; |
| 27 | + /* Plan ID for the current pipeline run */ |
| 28 | + private final String azureDevOpsPlanId; |
| 29 | + /* Job ID for the current pipeline job */ |
| 30 | + private final String azureDevOpsJobId; |
| 31 | + /* Team Project ID where the pipeline is running */ |
| 32 | + private final String azureDevOpsTeamProjectId; |
| 33 | + /* Host type (e.g., "build", "release") */ |
| 34 | + private final String azureDevOpsHostType; |
| 35 | + /* HTTP client for making requests to Azure DevOps */ |
| 36 | + private final HttpClient httpClient; |
| 37 | + /* Environment for reading configuration values */ |
| 38 | + private final Environment environment; |
| 39 | + /* JSON mapper for parsing response data */ |
| 40 | + private static final ObjectMapper mapper = new ObjectMapper(); |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructs a new AzureDevOpsIDTokenSource by reading environment variables. This constructor |
| 44 | + * implements fail-early validation - if any required environment variables are missing, it will |
| 45 | + * throw a DatabricksException immediately. |
| 46 | + * |
| 47 | + * @param httpClient The HTTP client to use for making requests |
| 48 | + * @throws DatabricksException if any required environment variables are missing |
| 49 | + */ |
| 50 | + public AzureDevOpsIDTokenSource(HttpClient httpClient) { |
| 51 | + this(httpClient, createDefaultEnvironment()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Constructs a new AzureDevOpsIDTokenSource with a custom environment. This constructor is |
| 56 | + * primarily used for testing to inject mock environment variables. |
| 57 | + * |
| 58 | + * @param httpClient The HTTP client to use for making requests |
| 59 | + * @param environment The environment to read configuration from |
| 60 | + * @throws DatabricksException if httpClient is null or any required environment variables are |
| 61 | + * missing |
| 62 | + */ |
| 63 | + protected AzureDevOpsIDTokenSource(HttpClient httpClient, Environment environment) { |
| 64 | + if (httpClient == null) { |
| 65 | + throw new DatabricksException("HttpClient cannot be null"); |
| 66 | + } |
| 67 | + this.httpClient = httpClient; |
| 68 | + this.environment = environment; |
| 69 | + |
| 70 | + this.azureDevOpsAccessToken = validateEnvironmentVariable("SYSTEM_ACCESSTOKEN"); |
| 71 | + this.azureDevOpsTeamFoundationCollectionUri = |
| 72 | + validateEnvironmentVariable("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"); |
| 73 | + this.azureDevOpsPlanId = validateEnvironmentVariable("SYSTEM_PLANID"); |
| 74 | + this.azureDevOpsJobId = validateEnvironmentVariable("SYSTEM_JOBID"); |
| 75 | + this.azureDevOpsTeamProjectId = validateEnvironmentVariable("SYSTEM_TEAMPROJECTID"); |
| 76 | + this.azureDevOpsHostType = validateEnvironmentVariable("SYSTEM_HOSTTYPE"); |
| 77 | + } |
| 78 | + |
| 79 | + /** Creates a default Environment using system environment variables. */ |
| 80 | + private static Environment createDefaultEnvironment() { |
| 81 | + String pathEnv = System.getenv("PATH"); |
| 82 | + String[] pathArray = |
| 83 | + pathEnv != null ? pathEnv.split(java.io.File.pathSeparator) : new String[0]; |
| 84 | + return new Environment(System.getenv(), pathArray, System.getProperty("os.name")); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Validates that an environment variable is present and not empty. |
| 89 | + * |
| 90 | + * @param varName The environment variable name |
| 91 | + * @return The environment variable value |
| 92 | + * @throws DatabricksException if the environment variable is missing or empty |
| 93 | + */ |
| 94 | + private String validateEnvironmentVariable(String varName) { |
| 95 | + String value = environment.get(varName); |
| 96 | + if (Strings.isNullOrEmpty(value)) { |
| 97 | + if (varName.equals("SYSTEM_ACCESSTOKEN")) { |
| 98 | + throw new DatabricksException( |
| 99 | + String.format( |
| 100 | + "Missing environment variable %s, if calling from Azure DevOps Pipeline, please set this env var following https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#systemaccesstoken", |
| 101 | + varName)); |
| 102 | + } |
| 103 | + throw new DatabricksException( |
| 104 | + String.format( |
| 105 | + "Missing environment variable %s, likely not calling from Azure DevOps Pipeline", |
| 106 | + varName)); |
| 107 | + } |
| 108 | + return value; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Retrieves an ID token from Azure DevOps Pipelines. This method makes an authenticated request |
| 113 | + * to Azure DevOps to obtain a JWT token that can later be exchanged for a Databricks access |
| 114 | + * token. |
| 115 | + * |
| 116 | + * <p>Note: The audience parameter is ignored for Azure DevOps OIDC tokens as they have a |
| 117 | + * hardcoded audience for Azure AD integration. |
| 118 | + * |
| 119 | + * @param audience Ignored for Azure DevOps OIDC tokens |
| 120 | + * @return An IDToken object containing the JWT token value |
| 121 | + * @throws DatabricksException if the token request fails |
| 122 | + */ |
| 123 | + @Override |
| 124 | + public IDToken getIDToken(String audience) { |
| 125 | + |
| 126 | + // Build Azure DevOps OIDC endpoint URL. |
| 127 | + String requestUrl = |
| 128 | + String.format( |
| 129 | + "%s/%s/_apis/distributedtask/hubs/%s/plans/%s/jobs/%s/oidctoken?api-version=7.2-preview.1", |
| 130 | + azureDevOpsTeamFoundationCollectionUri, |
| 131 | + azureDevOpsTeamProjectId, |
| 132 | + azureDevOpsHostType, |
| 133 | + azureDevOpsPlanId, |
| 134 | + azureDevOpsJobId); |
| 135 | + |
| 136 | + Request req = |
| 137 | + new Request("POST", requestUrl) |
| 138 | + .withHeader("Authorization", "Bearer " + azureDevOpsAccessToken) |
| 139 | + .withHeader("Content-Type", "application/json"); |
| 140 | + |
| 141 | + Response resp; |
| 142 | + try { |
| 143 | + resp = httpClient.execute(req); |
| 144 | + } catch (IOException e) { |
| 145 | + throw new DatabricksException( |
| 146 | + "Failed to request ID token from Azure DevOps at " + requestUrl + ": " + e.getMessage(), |
| 147 | + e); |
| 148 | + } |
| 149 | + |
| 150 | + if (resp.getStatusCode() != 200) { |
| 151 | + throw new DatabricksException( |
| 152 | + "Failed to request ID token from Azure DevOps: status code " |
| 153 | + + resp.getStatusCode() |
| 154 | + + ", response body: " |
| 155 | + + resp.getBody().toString()); |
| 156 | + } |
| 157 | + |
| 158 | + ObjectNode jsonResp; |
| 159 | + try { |
| 160 | + jsonResp = mapper.readValue(resp.getBody(), ObjectNode.class); |
| 161 | + } catch (IOException e) { |
| 162 | + throw new DatabricksException( |
| 163 | + "Failed to parse Azure DevOps OIDC token response: " + e.getMessage(), e); |
| 164 | + } |
| 165 | + |
| 166 | + // Azure DevOps returns {"oidcToken":"***"} format, not {"value":"***"} like GitHub Actions. |
| 167 | + if (!jsonResp.has("oidcToken")) { |
| 168 | + throw new DatabricksException("Azure DevOps OIDC token response missing 'oidcToken' field"); |
| 169 | + } |
| 170 | + |
| 171 | + String tokenValue = jsonResp.get("oidcToken").textValue(); |
| 172 | + if (Strings.isNullOrEmpty(tokenValue)) { |
| 173 | + throw new DatabricksException("Received empty OIDC token from Azure DevOps"); |
| 174 | + } |
| 175 | + return new IDToken(tokenValue); |
| 176 | + } |
| 177 | +} |
0 commit comments