Skip to content

Commit f2536e7

Browse files
authored
Add IKmsProvider wrappers for Azure and GSC providers (#1460)
1 parent a12bf39 commit f2536e7

File tree

7 files changed

+87
-22
lines changed

7 files changed

+87
-22
lines changed

src/MongoDB.Driver/Authentication/External/AzureAuthenticationCredentialsProvider.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public AzureCredentials(string accessToken, DateTime? expiration)
3939
public string AccessToken => _accessToken;
4040
public DateTime? Expiration => _expiration;
4141
public bool ShouldBeRefreshed => _expiration.HasValue ? (_expiration.Value - DateTime.UtcNow) < __overlapWhereExpired : true;
42-
43-
public BsonDocument GetKmsCredentials() => new BsonDocument("accessToken", _accessToken);
4442
}
4543

4644
internal sealed class AzureAuthenticationCredentialsProvider : IExternalAuthenticationCredentialsProvider<AzureCredentials>

src/MongoDB.Driver/Authentication/External/GcpAuthenticationCredentialsProvider.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ internal sealed class GcpCredentials : IExternalCredentials
3333
public DateTime? Expiration => null;
3434

3535
public bool ShouldBeRefreshed => true;
36-
37-
public BsonDocument GetKmsCredentials() => new BsonDocument("accessToken", _accessToken);
3836
}
3937

4038
internal sealed class GcpAuthenticationCredentialsProvider : IExternalAuthenticationCredentialsProvider<GcpCredentials>

src/MongoDB.Driver/Authentication/External/IExternalCredentials.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
*/
1515

1616
using System;
17-
using MongoDB.Bson;
1817

1918
namespace MongoDB.Driver.Authentication.External
2019
{
2120
internal interface IExternalCredentials
2221
{
2322
DateTime? Expiration { get; }
2423
bool ShouldBeRefreshed { get; }
25-
BsonDocument GetKmsCredentials();
2624
}
2725
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Threading;
17+
using System.Threading.Tasks;
18+
using MongoDB.Bson;
19+
using MongoDB.Driver.Authentication.External;
20+
21+
namespace MongoDB.Driver.Encryption
22+
{
23+
internal sealed class AzureKmsProvider : IKmsProvider
24+
{
25+
public const string ProviderName = "azure";
26+
27+
public static readonly IKmsProvider Instance = new AzureKmsProvider();
28+
29+
public async Task<BsonDocument> GetKmsCredentialsAsync(CancellationToken cancellationToken)
30+
{
31+
var credentials = await ExternalCredentialsAuthenticators.Instance.Azure.CreateCredentialsFromExternalSourceAsync(cancellationToken).ConfigureAwait(false);
32+
return new BsonDocument("accessToken", credentials.AccessToken);
33+
}
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Threading;
17+
using System.Threading.Tasks;
18+
using MongoDB.Bson;
19+
using MongoDB.Driver.Authentication.External;
20+
21+
namespace MongoDB.Driver.Encryption
22+
{
23+
internal sealed class GcpKmsProvider : IKmsProvider
24+
{
25+
public const string ProviderName = "gcp";
26+
27+
public static readonly IKmsProvider Instance = new GcpKmsProvider();
28+
29+
public async Task<BsonDocument> GetKmsCredentialsAsync(CancellationToken cancellationToken)
30+
{
31+
var credentials = await ExternalCredentialsAuthenticators.Instance.Gcp.CreateCredentialsFromExternalSourceAsync(cancellationToken).ConfigureAwait(false);
32+
return new BsonDocument("accessToken", credentials.AccessToken);
33+
}
34+
}
35+
}

src/MongoDB.Driver/Encryption/KmsProviderRegistry.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ public sealed class KmsProviderRegistry
2727
/// <summary>
2828
/// Kms Provider Registry Instance.
2929
/// </summary>
30-
public static readonly KmsProviderRegistry Instance = new KmsProviderRegistry();
30+
public static readonly KmsProviderRegistry Instance = CreateDefaultInstance();
31+
32+
private static KmsProviderRegistry CreateDefaultInstance()
33+
{
34+
var registry = new KmsProviderRegistry();
35+
registry.Register(GcpKmsProvider.ProviderName, () => GcpKmsProvider.Instance);
36+
registry.Register(AzureKmsProvider.ProviderName, () => AzureKmsProvider.Instance);
37+
return registry;
38+
}
3139

3240
private readonly ConcurrentDictionary<string, Func<IKmsProvider>> _registry = new();
3341

@@ -51,7 +59,13 @@ public void Register(string kmsProviderName, Func<IKmsProvider> factory)
5159
}
5260
}
5361

54-
internal bool TryCreate(string providerName, out IKmsProvider provider)
62+
/// <summary>
63+
/// Creates KMS provider if possible.
64+
/// </summary>
65+
/// <param name="providerName">The requested provider name.</param>
66+
/// <param name="provider">When this method succeeds contains the created provider, otherwise <value>null</value>.</param>
67+
/// <returns><value>true</value> if the requested provider was created, otherwise <value>false</value>.</returns>
68+
public bool TryCreate(string providerName, out IKmsProvider provider)
5569
{
5670
Ensure.IsNotNullOrEmpty(providerName, nameof(providerName));
5771

src/MongoDB.Driver/Encryption/LibMongoCryptControllerBase.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
using System.Threading.Tasks;
2525
using MongoDB.Bson;
2626
using MongoDB.Bson.IO;
27-
using MongoDB.Driver.Authentication.External;
2827
using MongoDB.Driver.Core.Configuration;
2928
using MongoDB.Driver.Core.Connections;
3029
using MongoDB.Driver.Core.Misc;
@@ -246,19 +245,7 @@ private async Task ProcessNeedKmsCredentialsAsync(CryptContext context, Cancella
246245
var newCredentialsList = new List<BsonElement>();
247246
foreach (var kmsProvider in _kmsProviders.Where(k => k.Value.Count == 0))
248247
{
249-
// TODO: Refactor all KMS providers to use Registry instead of the hardcoded switch.
250-
IExternalCredentials credentialsBody = kmsProvider.Key switch
251-
{
252-
"azure" => await ExternalCredentialsAuthenticators.Instance.Azure.CreateCredentialsFromExternalSourceAsync(cancellationToken).ConfigureAwait(false),
253-
"gcp" => await ExternalCredentialsAuthenticators.Instance.Gcp.CreateCredentialsFromExternalSourceAsync(cancellationToken).ConfigureAwait(false),
254-
_ => null,
255-
};
256-
257-
if (credentialsBody != null)
258-
{
259-
newCredentialsList.Add(new BsonElement(kmsProvider.Key, credentialsBody.GetKmsCredentials()));
260-
}
261-
else if (KmsProviderRegistry.Instance.TryCreate(kmsProvider.Key, out var provider))
248+
if (KmsProviderRegistry.Instance.TryCreate(kmsProvider.Key, out var provider))
262249
{
263250
var credentials = await provider.GetKmsCredentialsAsync(cancellationToken).ConfigureAwait(false);
264251
newCredentialsList.Add(new BsonElement(kmsProvider.Key, credentials));

0 commit comments

Comments
 (0)