-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support attached service accounts for GCP KMS #987
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,37 @@ | ||
#!/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 via MONGODB-AWS | ||
# authentication mechanism | ||
# SUCCESS Whether the authentication is expected to succeed or fail. One of "true" or "false" | ||
############################################ | ||
# Main Program # | ||
############################################ | ||
|
||
echo "Running GCP 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.gcp.success="${SUCCESS}" --stacktrace --debug --info driver-sync:test \ | ||
--tests ClientSideEncryptionOnDemandGcpCredentialsTest | ||
first=$? | ||
echo $first | ||
|
||
./gradlew -Dorg.mongodb.test.uri="${MONGODB_URI}" -Dorg.mongodb.test.gcp.success="${SUCCESS}" --stacktrace --debug --info driver-reactive-streams:test \ | ||
--tests ClientSideEncryptionOnDemandGcpCredentialsTest | ||
second=$? | ||
echo $second | ||
|
||
if [ $first -ne 0 ]; then | ||
exit $first | ||
elif [ $second -ne 0 ]; then | ||
exit $second | ||
else | ||
exit 0 | ||
fi |
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
52 changes: 52 additions & 0 deletions
52
driver-core/src/main/com/mongodb/internal/authentication/GcpCredentialHelper.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,52 @@ | ||
/* | ||
* 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 java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.mongodb.internal.authentication.HttpHelper.getHttpContents; | ||
|
||
/** | ||
* Utility class for working with GCP authentication. | ||
* | ||
* <p>This class should not be considered a part of the public API.</p> | ||
*/ | ||
public final class GcpCredentialHelper { | ||
public static BsonDocument obtainFromEnvironment() { | ||
String alternateHost = System.getenv("GCE_METADATA_HOST"); | ||
jyemin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
String host = alternateHost == null ? "metadata.google.internal" : alternateHost; | ||
String endpoint = "http://" + host; | ||
String path = "/computeMetadata/v1/instance/service-accounts/default/token"; | ||
|
||
Map<String, String> header = new HashMap<>(); | ||
header.put("Metadata-Flavor", "Google"); | ||
String response = getHttpContents("GET", endpoint + path, header); | ||
BsonDocument responseDocument = BsonDocument.parse(response); | ||
if (responseDocument.containsKey("access_token")) { | ||
return new BsonDocument("accessToken", responseDocument.get("access_token")); | ||
} else { | ||
throw new MongoClientException("access_token is missing from GCE metadata response. Full response is ''" + response); | ||
} | ||
} | ||
|
||
private GcpCredentialHelper() { | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
driver-core/src/main/com/mongodb/internal/authentication/HttpHelper.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,74 @@ | ||
/* | ||
* 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.MongoInternalException; | ||
import com.mongodb.lang.NonNull; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
|
||
/** | ||
* Utility class for working with HTTP servers. | ||
* | ||
* <p>This class should not be considered a part of the public API.</p> | ||
*/ | ||
public final class HttpHelper { | ||
|
||
private HttpHelper() { | ||
} | ||
|
||
@NonNull | ||
public static String getHttpContents(final String method, final String endpoint, final Map<String, String> headers) { | ||
StringBuilder content = new StringBuilder(); | ||
HttpURLConnection conn = null; | ||
try { | ||
conn = (HttpURLConnection) new URL(endpoint).openConnection(); | ||
conn.setRequestMethod(method); | ||
conn.setReadTimeout(10000); | ||
if (headers != null) { | ||
for (Map.Entry<String, String> kvp : headers.entrySet()) { | ||
conn.setRequestProperty(kvp.getKey(), kvp.getValue()); | ||
} | ||
} | ||
|
||
int status = conn.getResponseCode(); | ||
if (status != HttpURLConnection.HTTP_OK) { | ||
throw new IOException(String.format("%d %s", status, conn.getResponseMessage())); | ||
} | ||
|
||
try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { | ||
String inputLine; | ||
while ((inputLine = in.readLine()) != null) { | ||
content.append(inputLine); | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new MongoInternalException("Unexpected IOException", e); | ||
} finally { | ||
if (conn != null) { | ||
conn.disconnect(); | ||
} | ||
} | ||
return content.toString(); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.