-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSHARP-4610: OIDC: Automatic token acquisition for GCP Identity Provider
- Loading branch information
Oleksandr Poliakov
committed
May 1, 2024
1 parent
7c4216e
commit 300f7c4
Showing
13 changed files
with
345 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,34 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Don't trace since the URI contains a password that shouldn't show up in the logs | ||
set -o errexit # Exit the script with error if any of the commands fail | ||
|
||
DOTNET_SDK_PATH="$(pwd)/.dotnet" | ||
|
||
echo "Downloading .NET SDK installer into $DOTNET_SDK_PATH folder..." | ||
curl -Lfo ./dotnet-install.sh https://dot.net/v1/dotnet-install.sh | ||
echo "Installing .NET LTS SDK..." | ||
bash ./dotnet-install.sh --channel 6.0 --install-dir "$DOTNET_SDK_PATH" --no-path | ||
export PATH=$DOTNET_SDK_PATH:$PATH | ||
|
||
if [ "$OIDC_ENV" == "azure" ]; then | ||
source ./env.sh | ||
TOKEN_RESOURCE="$AZUREOIDC_RESOURCE" | ||
elif [ "$OIDC_ENV" == "gcp" ]; then | ||
source ./secrets-export.sh | ||
TOKEN_RESOURCE="$GCPOIDC_AUDIENCE" | ||
else | ||
echo "Unrecognized OIDC_ENV $OIDC_ENV" | ||
exit 1 | ||
fi | ||
|
||
if [[ "$MONGODB_URI" =~ ^mongodb:.* ]]; then | ||
MONGODB_URI="mongodb://${OIDC_ADMIN_USER}:${OIDC_ADMIN_PWD}@${MONGODB_URI:10}?authSource=admin" | ||
elif [[ "$MONGODB_URI" =~ ^mongodb\+srv:.* ]]; then | ||
MONGODB_URI="mongodb+srv://${OIDC_ADMIN_USER}:${OIDC_ADMIN_PWD}@${MONGODB_URI:14}?authSource=admin" | ||
else | ||
echo "Unexpected MONGODB_URI format: $MONGODB_URI" | ||
exit 1 | ||
fi | ||
|
||
dotnet test --no-build --framework net6.0 --filter Category=MongoDbOidc -e OIDC_ENV="$OIDC_ENV" -e TOKEN_RESOURCE="$TOKEN_RESOURCE" -e MONGODB_URI="$MONGODB_URI" --results-directory ./build/test-results --logger "console;verbosity=detailed" ./tests/**/*.Tests.dll |
This file contains 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 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 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
46 changes: 46 additions & 0 deletions
46
src/MongoDB.Driver.Core/Core/Authentication/Oidc/GcpOidcCallback.cs
This file contains 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,46 @@ | ||
/* Copyright 2010-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. | ||
*/ | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net; | ||
|
||
namespace MongoDB.Driver.Core.Authentication.Oidc | ||
{ | ||
internal sealed class GcpOidcCallback : HttpRequestOidcCallback | ||
{ | ||
private readonly string _tokenResource; | ||
|
||
public GcpOidcCallback(string tokenResource) | ||
{ | ||
_tokenResource = tokenResource; | ||
} | ||
|
||
protected override HttpWebRequest CreateMetadataRequest(OidcCallbackParameters parameters) | ||
{ | ||
var metadataUrl = $"http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience={Uri.EscapeDataString(_tokenResource)}"; | ||
var request = WebRequest.CreateHttp(new Uri(metadataUrl)); | ||
request.Method = "GET"; | ||
request.Headers.Add("Metadata-Flavor", "Google"); | ||
return request; | ||
} | ||
|
||
protected override OidcAccessToken ParseMetadataResponseContent(Stream responseStream) | ||
{ | ||
using var responseReader = new StreamReader(responseStream); | ||
return new OidcAccessToken(responseReader.ReadToEnd(), null); | ||
} | ||
} | ||
} |
Oops, something went wrong.