|
17 | 17 | package com.mongodb.internal.authentication;
|
18 | 18 |
|
19 | 19 | import com.mongodb.MongoClientException;
|
| 20 | +import com.mongodb.internal.ExpirableValue; |
20 | 21 | import org.bson.BsonDocument;
|
| 22 | +import org.bson.BsonString; |
21 | 23 | import org.bson.json.JsonParseException;
|
22 | 24 |
|
| 25 | +import java.time.Duration; |
23 | 26 | import java.util.HashMap;
|
24 | 27 | import java.util.Map;
|
| 28 | +import java.util.Optional; |
25 | 29 |
|
26 | 30 | import static com.mongodb.internal.authentication.HttpHelper.getHttpContents;
|
27 | 31 |
|
|
31 | 35 | * <p>This class should not be considered a part of the public API.</p>
|
32 | 36 | */
|
33 | 37 | public final class AzureCredentialHelper {
|
34 |
| - public static BsonDocument obtainFromEnvironment() { |
35 |
| - String endpoint = "http://" + "169.254.169.254:80" |
36 |
| - + "/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net"; |
37 |
| - |
38 |
| - Map<String, String> headers = new HashMap<>(); |
39 |
| - headers.put("Metadata", "true"); |
40 |
| - headers.put("Accept", "application/json"); |
41 |
| - |
42 |
| - String response = getHttpContents("GET", endpoint, headers); |
43 |
| - try { |
44 |
| - BsonDocument responseDocument = BsonDocument.parse(response); |
45 |
| - if (responseDocument.containsKey("access_token")) { |
46 |
| - return new BsonDocument("accessToken", responseDocument.get("access_token")); |
47 |
| - } else { |
48 |
| - throw new MongoClientException("The access_token is missing from Azure IMDS metadata response."); |
| 38 | + private static final String ACCESS_TOKEN_FIELD = "access_token"; |
| 39 | + private static final String EXPIRES_IN_FIELD = "expires_in"; |
| 40 | + |
| 41 | + private static ExpirableValue<String> cachedAccessToken = ExpirableValue.expired(); |
| 42 | + |
| 43 | + public static synchronized BsonDocument obtainFromEnvironment() { |
| 44 | + String accessToken; |
| 45 | + Optional<String> cachedValue = cachedAccessToken.getValue(); |
| 46 | + if (cachedValue.isPresent()) { |
| 47 | + accessToken = cachedValue.get(); |
| 48 | + } else { |
| 49 | + String endpoint = "http://" + "169.254.169.254:80" |
| 50 | + + "/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net"; |
| 51 | + |
| 52 | + Map<String, String> headers = new HashMap<>(); |
| 53 | + headers.put("Metadata", "true"); |
| 54 | + headers.put("Accept", "application/json"); |
| 55 | + |
| 56 | + long startNanoTime = System.nanoTime(); |
| 57 | + BsonDocument responseDocument; |
| 58 | + try { |
| 59 | + responseDocument = BsonDocument.parse(getHttpContents("GET", endpoint, headers)); |
| 60 | + } catch (JsonParseException e) { |
| 61 | + throw new MongoClientException("Exception parsing JSON from Azure IMDS metadata response.", e); |
| 62 | + } |
| 63 | + |
| 64 | + if (!responseDocument.isString(ACCESS_TOKEN_FIELD)) { |
| 65 | + throw new MongoClientException(String.format( |
| 66 | + "The %s field from Azure IMDS metadata response is missing or is not a string", ACCESS_TOKEN_FIELD)); |
| 67 | + } |
| 68 | + if (!responseDocument.isString(EXPIRES_IN_FIELD)) { |
| 69 | + throw new MongoClientException(String.format( |
| 70 | + "The %s field from Azure IMDS metadata response is missing or is not a string", EXPIRES_IN_FIELD)); |
49 | 71 | }
|
50 |
| - } catch (JsonParseException e) { |
51 |
| - throw new MongoClientException("Exception parsing JSON from Azure IMDS metadata response.", e); |
52 |
| - } |
| 72 | + accessToken = responseDocument.getString(ACCESS_TOKEN_FIELD).getValue(); |
| 73 | + int expiresInSeconds = Integer.parseInt(responseDocument.getString(EXPIRES_IN_FIELD).getValue()); |
| 74 | + cachedAccessToken = ExpirableValue.expirable(accessToken, Duration.ofSeconds(expiresInSeconds).minus(Duration.ofMinutes(1)), |
| 75 | + startNanoTime); |
| 76 | + } |
| 77 | + return new BsonDocument("accessToken", new BsonString(accessToken)); |
53 | 78 | }
|
54 | 79 |
|
55 | 80 | private AzureCredentialHelper() {
|
|
0 commit comments