forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSHARP-4911: Refactor AWS auth library to a separate package (mongodb…
- Loading branch information
1 parent
8447ef1
commit 1e97f44
Showing
166 changed files
with
3,658 additions
and
3,111 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
items: | ||
- name: MongoDB.Bson | ||
href: MongoDB.Bson/toc.yml | ||
- name: MongoDB.Driver | ||
href: MongoDB.Driver/toc.yml | ||
- href: MongoDB.Bson/toc.yml | ||
- href: MongoDB.Driver/toc.yml | ||
- href: MongoDB.Driver.Authentication.Aws/toc.yml |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* 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 MongoDB.Driver.Core.Misc; | ||
|
||
namespace MongoDB.Driver.Authentication.AWS | ||
{ | ||
internal sealed class AWSCredentials | ||
{ | ||
public AWSCredentials(string accessKeyId, string secretAccessKey, string sessionToken) | ||
{ | ||
AccessKeyId = Ensure.IsNotNull(accessKeyId, nameof(accessKeyId)); | ||
SecretAccessKey = Ensure.IsNotNull(secretAccessKey, nameof(secretAccessKey)); | ||
SessionToken = sessionToken; // can be null | ||
} | ||
|
||
public string AccessKeyId { get; } | ||
public string SecretAccessKey { get; } | ||
public string SessionToken { get; } | ||
} | ||
} |
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,42 @@ | ||
/* 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.Threading; | ||
using System.Threading.Tasks; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver.Authentication.AWS.CredentialsSources; | ||
using MongoDB.Driver.Encryption; | ||
|
||
namespace MongoDB.Driver.Authentication.AWS | ||
{ | ||
internal sealed class AWSKmsProvider : IKmsProvider | ||
{ | ||
public const string ProviderName = "aws"; | ||
|
||
public static readonly AWSKmsProvider Instance = new AWSKmsProvider(); | ||
|
||
public async Task<BsonDocument> GetKmsCredentialsAsync(CancellationToken cancellationToken) | ||
{ | ||
var credentials = await AWSFallbackCredentialsSource.Instance.GetCredentialsAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
return new() | ||
{ | ||
{ "accessKeyId", credentials.AccessKeyId }, | ||
{ "secretAccessKey", credentials.SecretAccessKey }, | ||
{ "sessionToken", credentials.SessionToken, credentials.SessionToken != null } | ||
}; | ||
} | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
src/MongoDB.Driver.Authentication.AWS/AWSSaslMechanism.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,149 @@ | ||
/* 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.Collections.Generic; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver.Authentication.AWS.CredentialsSources; | ||
using MongoDB.Driver.Authentication.AWS.SaslSteps; | ||
using MongoDB.Driver.Core.Connections; | ||
using MongoDB.Driver.Core.Misc; | ||
|
||
namespace MongoDB.Driver.Authentication.AWS | ||
{ | ||
internal sealed class AWSSaslMechanism : ISaslMechanism | ||
{ | ||
public static AWSSaslMechanism Create(SaslContext context) | ||
=> Create(context, RandomByteGenerator.Instance, SystemClock.Instance); | ||
|
||
public static AWSSaslMechanism Create(SaslContext context, IRandomByteGenerator randomByteGenerator, IClock clock) | ||
{ | ||
Ensure.IsNotNull(context, nameof(context)); | ||
Ensure.IsNotNull(randomByteGenerator, nameof(randomByteGenerator)); | ||
Ensure.IsNotNull(clock, nameof(clock)); | ||
if (context.Mechanism != MechanismName) | ||
{ | ||
throw new InvalidOperationException($"Unexpected authentication mechanism: {context.Mechanism}"); | ||
} | ||
|
||
if (context.Identity.Source != "$external") | ||
{ | ||
throw new ArgumentException("MONGODB-AWS authentication may only use the $external source.", nameof(context.Identity.Source)); | ||
} | ||
|
||
string password = null; | ||
if (context.IdentityEvidence is PasswordEvidence passwordEvidence) | ||
{ | ||
password = passwordEvidence.ToInsecureString(); | ||
} | ||
|
||
var awsCredentials = CreateAwsCredentialsFromMongoCredentials(context.Identity.Username, password, context.MechanismProperties); | ||
IAWSCredentialsSource credentialsSource = awsCredentials != null ? new AWSInstanceCredentialsSource(awsCredentials) : AWSFallbackCredentialsSource.Instance; | ||
|
||
return new AWSSaslMechanism(credentialsSource, randomByteGenerator, clock); | ||
} | ||
|
||
private static AWSCredentials CreateAwsCredentialsFromMongoCredentials(string username, string password, IEnumerable<KeyValuePair<string, object>> properties) | ||
{ | ||
ValidateMechanismProperties(properties); | ||
var sessionToken = ExtractSessionTokenFromMechanismProperties(properties); | ||
|
||
if (username == null && password == null && sessionToken == null) | ||
{ | ||
return null; | ||
} | ||
if (password != null && username == null) | ||
{ | ||
throw new InvalidOperationException("When using MONGODB-AWS authentication if a password is provided via settings then a username must be provided also."); | ||
} | ||
if (username != null && password == null) | ||
{ | ||
throw new InvalidOperationException("When using MONGODB-AWS authentication if a username is provided via settings then a password must be provided also."); | ||
} | ||
if (sessionToken != null && (username == null || password == null)) | ||
{ | ||
throw new InvalidOperationException("When using MONGODB-AWS authentication if a session token is provided via settings then a username and password must be provided also."); | ||
} | ||
|
||
return new AWSCredentials(accessKeyId: username, secretAccessKey: password, sessionToken); | ||
} | ||
|
||
private static string ExtractSessionTokenFromMechanismProperties(IEnumerable<KeyValuePair<string, object>> properties) | ||
{ | ||
if (properties != null) | ||
{ | ||
foreach (var pair in properties) | ||
{ | ||
if (pair.Key.ToUpperInvariant() == "AWS_SESSION_TOKEN") | ||
{ | ||
return (string)pair.Value; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static void ValidateMechanismProperties(IEnumerable<KeyValuePair<string, object>> properties) | ||
{ | ||
if (properties != null) | ||
{ | ||
foreach (var pair in properties) | ||
{ | ||
if (pair.Key.ToUpperInvariant() != "AWS_SESSION_TOKEN") | ||
{ | ||
throw new ArgumentException($"Unknown AWS property '{pair.Key}'.", nameof(properties)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public const int ClientNonceLength = 32; | ||
public const string MechanismName = "MONGODB-AWS"; | ||
|
||
private readonly IClock _clock; | ||
private readonly IAWSCredentialsSource _credentialsSource; | ||
private readonly IRandomByteGenerator _randomByteGenerator; | ||
|
||
private AWSSaslMechanism(IAWSCredentialsSource credentialsSource, IRandomByteGenerator randomByteGenerator, IClock clock) | ||
{ | ||
_credentialsSource = credentialsSource; | ||
_randomByteGenerator = randomByteGenerator; | ||
_clock = clock; | ||
} | ||
|
||
public string DatabaseName => "$external"; | ||
|
||
public string Name => MechanismName; | ||
|
||
public ISaslStep CreateSpeculativeAuthenticationStep() => null; | ||
|
||
public BsonDocument CustomizeSaslStartCommand(BsonDocument startCommand) => startCommand; | ||
|
||
public ISaslStep Initialize(SaslConversation conversation, ConnectionDescription description) | ||
=> new AWSFirstSaslStep(_credentialsSource, _randomByteGenerator, _clock); | ||
|
||
public void OnReAuthenticationRequired() | ||
{ | ||
} | ||
|
||
public bool TryHandleAuthenticationException(MongoException exception, ISaslStep step, SaslConversation conversation, ConnectionDescription description, out ISaslStep nextStep) | ||
{ | ||
_credentialsSource.ResetCache(); | ||
nextStep = null; | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.