|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * athena-synapse |
| 4 | + * %% |
| 5 | + * Copyright (C) 2019 - 2025 Amazon Web Services |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package com.amazonaws.athena.connectors.synapse; |
| 21 | + |
| 22 | +import com.amazonaws.athena.connector.credentials.CredentialsProvider; |
| 23 | +import com.amazonaws.athena.connector.credentials.DefaultCredentials; |
| 24 | +import com.amazonaws.athena.connector.lambda.exceptions.AthenaConnectorException; |
| 25 | +import com.amazonaws.athena.connector.lambda.security.CachableSecretsManager; |
| 26 | +import com.fasterxml.jackson.databind.JsonNode; |
| 27 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 28 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 29 | +import software.amazon.awssdk.services.glue.model.ErrorDetails; |
| 30 | +import software.amazon.awssdk.services.glue.model.FederationSourceErrorCode; |
| 31 | +import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; |
| 32 | +import software.amazon.awssdk.services.secretsmanager.model.PutSecretValueRequest; |
| 33 | + |
| 34 | +import java.net.URI; |
| 35 | +import java.net.http.HttpClient; |
| 36 | +import java.net.http.HttpRequest; |
| 37 | +import java.net.http.HttpResponse; |
| 38 | +import java.time.Duration; |
| 39 | +import java.time.Instant; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.Map; |
| 42 | + |
| 43 | +public class SynapseCredentialsProvider implements CredentialsProvider |
| 44 | +{ |
| 45 | + private static final String ACCESS_TOKEN = "access_token"; |
| 46 | + private static final String FETCHED_AT = "fetched_at"; |
| 47 | + private static final String EXPIRES_IN = "expires_in"; |
| 48 | + |
| 49 | + private static final String CLIENT_ID = "client_id"; |
| 50 | + private static final String CLIENT_SECRET = "client_secret"; |
| 51 | + private static final String TENANT_ID = "tenant_id"; |
| 52 | + |
| 53 | + private static final String USER = "user"; |
| 54 | + private static final String PASSWORD = "password"; |
| 55 | + private static final String USERNAME = "username"; |
| 56 | + |
| 57 | + private static final String GRANT_TYPE = "client_credentials"; |
| 58 | + private static final String SCOPE = "https://sql.azuresynapse.net/.default"; |
| 59 | + private static final String TOKEN_ENDPOINT_TEMPLATE = "https://login.microsoftonline.com/%s/oauth2/v2.0/token"; |
| 60 | + private static final long TOKEN_REFRESH_BUFFER_SECONDS = 300; |
| 61 | + |
| 62 | + private final String secretName; |
| 63 | + private final CachableSecretsManager secretsManager; |
| 64 | + private final ObjectMapper objectMapper; |
| 65 | + |
| 66 | + public SynapseCredentialsProvider(String secretName) |
| 67 | + { |
| 68 | + this.secretName = secretName; |
| 69 | + this.secretsManager = new CachableSecretsManager(SecretsManagerClient.create()); |
| 70 | + this.objectMapper = new ObjectMapper(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public DefaultCredentials getCredential() |
| 75 | + { |
| 76 | + Map<String, String> credentialMap = getCredentialMap(); |
| 77 | + return new DefaultCredentials( |
| 78 | + credentialMap.get(USER), |
| 79 | + credentialMap.get(PASSWORD) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Map<String, String> getCredentialMap() |
| 85 | + { |
| 86 | + try { |
| 87 | + String secretString = secretsManager.getSecret(secretName); |
| 88 | + Map<String, String> oauthConfig = objectMapper.readValue(secretString, Map.class); |
| 89 | + |
| 90 | + Map<String, String> credentialMap = new HashMap<>(); |
| 91 | + |
| 92 | + if (!isOAuthConfigured(oauthConfig)) { |
| 93 | + credentialMap.put(USER, oauthConfig.get(USERNAME)); |
| 94 | + credentialMap.put(PASSWORD, oauthConfig.get(PASSWORD)); |
| 95 | + } |
| 96 | + |
| 97 | + return credentialMap; |
| 98 | + } |
| 99 | + catch (Exception e) { |
| 100 | + throw new AthenaConnectorException( |
| 101 | + "Failed to retrieve Synapse credentials", |
| 102 | + ErrorDetails.builder() |
| 103 | + .errorCode(FederationSourceErrorCode.INTERNAL_SERVICE_EXCEPTION.toString()) |
| 104 | + .build() |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public String getOAuthAccessToken() |
| 110 | + { |
| 111 | + try { |
| 112 | + String secretValue = secretsManager.getSecret(secretName); |
| 113 | + Map<String, String> oauthConfig = objectMapper.readValue(secretValue, Map.class); |
| 114 | + |
| 115 | + if (isOAuthConfigured(oauthConfig)) { |
| 116 | + return fetchAccessToken(oauthConfig); |
| 117 | + } |
| 118 | + return null; |
| 119 | + } |
| 120 | + catch (Exception e) { |
| 121 | + throw new AthenaConnectorException( |
| 122 | + "Failed to get OAuth access token", |
| 123 | + ErrorDetails.builder() |
| 124 | + .errorCode(FederationSourceErrorCode.INTERNAL_SERVICE_EXCEPTION.toString()) |
| 125 | + .build() |
| 126 | + ); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private boolean isOAuthConfigured(Map<String, String> oauthConfig) |
| 131 | + { |
| 132 | + return oauthConfig.containsKey(CLIENT_ID) && |
| 133 | + !oauthConfig.get(CLIENT_ID).isEmpty() && |
| 134 | + oauthConfig.containsKey(CLIENT_SECRET) && |
| 135 | + !oauthConfig.get(CLIENT_SECRET).isEmpty() && |
| 136 | + oauthConfig.containsKey(TENANT_ID) && |
| 137 | + !oauthConfig.get(TENANT_ID).isEmpty(); |
| 138 | + } |
| 139 | + |
| 140 | + private String fetchAccessToken(Map<String, String> oauthConfig) throws Exception |
| 141 | + { |
| 142 | + String accessToken = oauthConfig.get(ACCESS_TOKEN); |
| 143 | + |
| 144 | + if (accessToken != null && |
| 145 | + oauthConfig.containsKey(FETCHED_AT) && |
| 146 | + oauthConfig.containsKey(EXPIRES_IN)) { |
| 147 | + long fetchedAt = Long.parseLong(oauthConfig.get(FETCHED_AT)); |
| 148 | + long expiresIn = Long.parseLong(oauthConfig.get(EXPIRES_IN)); |
| 149 | + long now = Instant.now().getEpochSecond(); |
| 150 | + |
| 151 | + if (now < (fetchedAt + expiresIn - TOKEN_REFRESH_BUFFER_SECONDS)) { |
| 152 | + return accessToken; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return fetchAndStoreNewToken(oauthConfig); |
| 157 | + } |
| 158 | + |
| 159 | + private String fetchAndStoreNewToken(Map<String, String> oauthConfig) throws Exception |
| 160 | + { |
| 161 | + String clientId = oauthConfig.get(CLIENT_ID); |
| 162 | + String clientSecret = oauthConfig.get(CLIENT_SECRET); |
| 163 | + String tenantId = oauthConfig.get(TENANT_ID); |
| 164 | + |
| 165 | + String tokenUrl = String.format(TOKEN_ENDPOINT_TEMPLATE, tenantId); |
| 166 | + String requestBody = String.format( |
| 167 | + "grant_type=%s&scope=%s&client_id=%s&client_secret=%s", |
| 168 | + GRANT_TYPE, SCOPE, clientId, clientSecret |
| 169 | + ); |
| 170 | + |
| 171 | + HttpClient client = HttpClient.newHttpClient(); |
| 172 | + HttpRequest request = HttpRequest.newBuilder() |
| 173 | + .uri(URI.create(tokenUrl)) |
| 174 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 175 | + .POST(HttpRequest.BodyPublishers.ofString(requestBody)) |
| 176 | + .timeout(Duration.ofSeconds(30)) |
| 177 | + .build(); |
| 178 | + |
| 179 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 180 | + |
| 181 | + if (response.statusCode() != 200) { |
| 182 | + throw new AthenaConnectorException( |
| 183 | + "Failed to fetch access token", |
| 184 | + ErrorDetails.builder() |
| 185 | + .errorCode(FederationSourceErrorCode.INTERNAL_SERVICE_EXCEPTION.toString()) |
| 186 | + .build() |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + JsonNode tokenResponse = objectMapper.readTree(response.body()); |
| 191 | + String accessToken = tokenResponse.get(ACCESS_TOKEN).asText(); |
| 192 | + long expiresIn = tokenResponse.get(EXPIRES_IN).asLong(); |
| 193 | + long fetchedAt = Instant.now().getEpochSecond(); |
| 194 | + |
| 195 | + oauthConfig.put(ACCESS_TOKEN, accessToken); |
| 196 | + oauthConfig.put(EXPIRES_IN, String.valueOf(expiresIn)); |
| 197 | + oauthConfig.put(FETCHED_AT, String.valueOf(fetchedAt)); |
| 198 | + |
| 199 | + ObjectNode updatedSecretJson = objectMapper.createObjectNode(); |
| 200 | + for (Map.Entry<String, String> entry : oauthConfig.entrySet()) { |
| 201 | + updatedSecretJson.put(entry.getKey(), entry.getValue()); |
| 202 | + } |
| 203 | + |
| 204 | + secretsManager.getSecretsManager().putSecretValue(PutSecretValueRequest.builder() |
| 205 | + .secretId(secretName) |
| 206 | + .secretString(updatedSecretJson.toString()) |
| 207 | + .build()); |
| 208 | + |
| 209 | + return accessToken; |
| 210 | + } |
| 211 | +} |
0 commit comments