-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support the Azure VM-assigned managed identity for automatic KMS credentials #1035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e3c750f
Support the Azure VM-assigned managed identity for automatic KMS cred…
jyemin 415effc
Remove unit test
jyemin 037435e
Only run slow failure tests if property is set
jyemin 7e6177f
Checkstyle
jyemin 5c39e65
Pull out key name and endpoint into project properties
jyemin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/bin/bash | ||
|
||
set -o xtrace | ||
set -o errexit # Exit the script with error if any of the commands fail | ||
|
||
# Supported/used environment variables: | ||
# MONGODB_URI Set the URI, including an optional username/password to use to connect to the server | ||
# PROVIDER Which KMS provider to test (either "gcp" or "azure") | ||
############################################ | ||
# Main Program # | ||
############################################ | ||
|
||
echo "Running ${PROVIDER}} Credential Acquisition Test" | ||
|
||
if ! which java ; then | ||
echo "Installing java..." | ||
sudo apt install openjdk-17-jdk -y | ||
fi | ||
|
||
./gradlew -Dorg.mongodb.test.uri="${MONGODB_URI}" \ | ||
-Dorg.mongodb.test.fle.on.demand.credential.test.success.enabled="true" \ | ||
-Dorg.mongodb.test.fle.on.demand.credential.provider="${PROVIDER}" \ | ||
--stacktrace --debug --info driver-sync:test --tests ClientSideEncryptionOnDemandCredentialsTest | ||
first=$? | ||
echo $first | ||
|
||
./gradlew -Dorg.mongodb.test.uri="${MONGODB_URI}" \ | ||
-Dorg.mongodb.test.fle.on.demand.credential.test.success.enabled="true" \ | ||
-Dorg.mongodb.test.fle.on.demand.credential.provider="${PROVIDER}" \ | ||
--stacktrace --debug --info driver-reactive-streams:test --tests ClientSideEncryptionOnDemandCredentialsTest | ||
second=$? | ||
echo $second | ||
|
||
if [ $first -ne 0 ]; then | ||
exit $first | ||
elif [ $second -ne 0 ]; then | ||
exit $second | ||
else | ||
exit 0 | ||
fi |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
driver-core/src/main/com/mongodb/internal/authentication/AzureCredentialHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.internal.authentication; | ||
|
||
import com.mongodb.MongoClientException; | ||
import org.bson.BsonDocument; | ||
import org.bson.json.JsonParseException; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.mongodb.internal.authentication.HttpHelper.getHttpContents; | ||
|
||
/** | ||
* Utility class for working with Azure authentication. | ||
* | ||
* <p>This class should not be considered a part of the public API.</p> | ||
*/ | ||
public final class AzureCredentialHelper { | ||
public static BsonDocument obtainFromEnvironment() { | ||
String endpoint = "http://" + "169.254.169.254:80" | ||
+ "/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net"; | ||
|
||
Map<String, String> headers = new HashMap<>(); | ||
headers.put("Metadata", "true"); | ||
headers.put("Accept", "application/json"); | ||
|
||
String response = getHttpContents("GET", endpoint, headers); | ||
try { | ||
BsonDocument responseDocument = BsonDocument.parse(response); | ||
if (responseDocument.containsKey("access_token")) { | ||
return new BsonDocument("accessToken", responseDocument.get("access_token")); | ||
} else { | ||
throw new MongoClientException("The access_token is missing from Azure IMDS metadata response."); | ||
} | ||
} catch (JsonParseException e) { | ||
throw new MongoClientException("Exception parsing JSON from Azure IMDS metadata response.", e); | ||
} | ||
} | ||
|
||
private AzureCredentialHelper() { | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why did you miss
KEY_NAME='${testazurekms_keyname}' KEY_VAULT_ENDPOINT='${testazurekms_keyvaultendpoint}'
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like those values are hardcoded in the integration test. Is that not ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to use env variables instead hardcoded values. That values are going to be changed in this ticket https://jira.mongodb.org/browse/BUILD-16154. Then, applying it in the driver will be easier with env variables instead of changing it in the code. NOTE: we will need to change the code in c# driver, my fault :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done