Skip to content

Commit cae4a1d

Browse files
authoredOct 1, 2024
Implement single entry point for all driver's extensions. (#1466)
1 parent 543e391 commit cae4a1d

12 files changed

+199
-96
lines changed
 

‎src/MongoDB.Driver.Authentication.AWS/AuthRegistryExtensions.cs

-45
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
namespace MongoDB.Driver.Authentication.AWS
17+
{
18+
/// <summary>
19+
/// Auth Registry Extensions.
20+
/// </summary>
21+
public static class ExtensionManagerExtensions
22+
{
23+
/// <summary>
24+
/// Registers both AWS SASL mechanism and AWS Kms Provider.
25+
/// </summary>
26+
/// <param name="extensionManager"></param>
27+
public static IExtensionManager AddAWSAuthentication(this IExtensionManager extensionManager)
28+
{
29+
extensionManager.SaslMechanisms.Register(AWSSaslMechanism.MechanismName, AWSSaslMechanism.Create);
30+
extensionManager.KmsProviders.Register(AWSKmsProvider.ProviderName, () => AWSKmsProvider.Instance);
31+
return extensionManager;
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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;
17+
18+
namespace MongoDB.Driver.Authentication
19+
{
20+
/// <summary>
21+
/// SASL Mechanism Registry.
22+
/// </summary>
23+
public interface ISaslMechanismRegistry
24+
{
25+
/// <summary>
26+
/// Registers new SASL mechanism factory.
27+
/// </summary>
28+
/// <param name="mechanismName">Mechanism name.</param>
29+
/// <param name="factory">Factory method.</param>
30+
void Register(string mechanismName, Func<SaslContext, ISaslMechanism> factory);
31+
32+
/// <summary>
33+
/// Creates SASL mechanism if possible.
34+
/// </summary>
35+
/// <param name="context">Sasl context.</param>
36+
/// <param name="mechanism">When this method succeeds contains the created mechanism, otherwise <value>null</value>.</param>
37+
/// <returns><value>true</value> if the requested provider was created, otherwise <value>false</value>.</returns>
38+
bool TryCreate(SaslContext context, out ISaslMechanism mechanism);
39+
}
40+
}
41+

‎src/MongoDB.Driver/Authentication/SaslAuthenticator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static bool TryCreate(
4646
MechanismProperties = mechanismProperties
4747
};
4848

49-
if (SaslMechanismRegistry.Instance.TryCreate(context, out var saslMechanism))
49+
if (MongoClientSettings.Extensions.SaslMechanisms.TryCreate(context, out var saslMechanism))
5050
{
5151
authenticator = new SaslAuthenticator(saslMechanism, serverApi);
5252
return true;

‎src/MongoDB.Driver/Authentication/SaslMechanismRegistry.cs

+3-20
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,9 @@
2323

2424
namespace MongoDB.Driver.Authentication
2525
{
26-
/// <summary>
27-
/// SASL Mechanism Registry.
28-
/// </summary>
29-
public sealed class SaslMechanismRegistry
26+
internal sealed class SaslMechanismRegistry : ISaslMechanismRegistry
3027
{
31-
/// <summary>
32-
/// SASL Mechanism Registry Instance.
33-
/// </summary>
34-
public static readonly SaslMechanismRegistry Instance = CreateDefaultInstance();
35-
36-
private static SaslMechanismRegistry CreateDefaultInstance()
28+
internal static SaslMechanismRegistry CreateDefaultInstance()
3729
{
3830
var registry = new SaslMechanismRegistry();
3931
registry.Register(PlainSaslMechanism.MechanismName, PlainSaslMechanism.Create);
@@ -47,15 +39,6 @@ private static SaslMechanismRegistry CreateDefaultInstance()
4739

4840
private readonly ConcurrentDictionary<string, Func<SaslContext, ISaslMechanism>> _registry = new();
4941

50-
internal SaslMechanismRegistry()
51-
{
52-
}
53-
54-
/// <summary>
55-
/// Registers new SASL mechanism factory.
56-
/// </summary>
57-
/// <param name="mechanismName">Mechanism name.</param>
58-
/// <param name="factory">Factory method.</param>
5942
public void Register(string mechanismName, Func<SaslContext, ISaslMechanism> factory)
6043
{
6144
Ensure.IsNotNullOrEmpty(mechanismName, nameof(mechanismName));
@@ -67,7 +50,7 @@ public void Register(string mechanismName, Func<SaslContext, ISaslMechanism> fac
6750
}
6851
}
6952

70-
internal bool TryCreate(SaslContext context, out ISaslMechanism mechanism)
53+
public bool TryCreate(SaslContext context, out ISaslMechanism mechanism)
7154
{
7255
Ensure.IsNotNull(context, nameof(context));
7356

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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;
17+
18+
namespace MongoDB.Driver.Encryption
19+
{
20+
/// <summary>
21+
/// Kms Provider Registry.
22+
/// </summary>
23+
public interface IKmsProviderRegistry
24+
{
25+
/// <summary>
26+
/// Registers new Kms Provider.
27+
/// </summary>
28+
/// <param name="kmsProviderName">Kms Provider Name.</param>
29+
/// <param name="factory">Factory method.</param>
30+
void Register(string kmsProviderName, Func<IKmsProvider> factory);
31+
32+
/// <summary>
33+
/// Creates KMS provider if possible.
34+
/// </summary>
35+
/// <param name="providerName">The requested provider name.</param>
36+
/// <param name="provider">When this method succeeds contains the created provider, otherwise <value>null</value>.</param>
37+
/// <returns><value>true</value> if the requested provider was created, otherwise <value>false</value>.</returns>
38+
bool TryCreate(string providerName, out IKmsProvider provider);
39+
}
40+
}
41+

‎src/MongoDB.Driver/Encryption/KmsProviderRegistry.cs

+2-25
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,9 @@
1919

2020
namespace MongoDB.Driver.Encryption
2121
{
22-
/// <summary>
23-
/// Kms Provider Registry.
24-
/// </summary>
25-
public sealed class KmsProviderRegistry
22+
internal sealed class KmsProviderRegistry : IKmsProviderRegistry
2623
{
27-
/// <summary>
28-
/// Kms Provider Registry Instance.
29-
/// </summary>
30-
public static readonly KmsProviderRegistry Instance = CreateDefaultInstance();
31-
32-
private static KmsProviderRegistry CreateDefaultInstance()
24+
internal static KmsProviderRegistry CreateDefaultInstance()
3325
{
3426
var registry = new KmsProviderRegistry();
3527
registry.Register(GcpKmsProvider.ProviderName, () => GcpKmsProvider.Instance);
@@ -39,15 +31,6 @@ private static KmsProviderRegistry CreateDefaultInstance()
3931

4032
private readonly ConcurrentDictionary<string, Func<IKmsProvider>> _registry = new();
4133

42-
internal KmsProviderRegistry()
43-
{
44-
}
45-
46-
/// <summary>
47-
/// Registers new Kms Provider.
48-
/// </summary>
49-
/// <param name="kmsProviderName">Kms Provider Name.</param>
50-
/// <param name="factory">Factory method.</param>
5134
public void Register(string kmsProviderName, Func<IKmsProvider> factory)
5235
{
5336
Ensure.IsNotNullOrEmpty(kmsProviderName, nameof(kmsProviderName));
@@ -59,12 +42,6 @@ public void Register(string kmsProviderName, Func<IKmsProvider> factory)
5942
}
6043
}
6144

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>
6845
public bool TryCreate(string providerName, out IKmsProvider provider)
6946
{
7047
Ensure.IsNotNullOrEmpty(providerName, nameof(providerName));

‎src/MongoDB.Driver/Encryption/LibMongoCryptControllerBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ private async Task ProcessNeedKmsCredentialsAsync(CryptContext context, Cancella
245245
var newCredentialsList = new List<BsonElement>();
246246
foreach (var kmsProvider in _kmsProviders.Where(k => k.Value.Count == 0))
247247
{
248-
if (KmsProviderRegistry.Instance.TryCreate(kmsProvider.Key, out var provider))
248+
if (MongoClientSettings.Extensions.KmsProviders.TryCreate(kmsProvider.Key, out var provider))
249249
{
250250
var credentials = await provider.GetKmsCredentialsAsync(cancellationToken).ConfigureAwait(false);
251251
newCredentialsList.Add(new BsonElement(kmsProvider.Key, credentials));
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 MongoDB.Driver.Authentication;
17+
using MongoDB.Driver.Encryption;
18+
19+
namespace MongoDB.Driver
20+
{
21+
internal sealed class ExtensionManager : IExtensionManager
22+
{
23+
public ExtensionManager()
24+
{
25+
SaslMechanisms = SaslMechanismRegistry.CreateDefaultInstance();
26+
KmsProviders = KmsProviderRegistry.CreateDefaultInstance();
27+
}
28+
29+
public ISaslMechanismRegistry SaslMechanisms { get; }
30+
31+
public IKmsProviderRegistry KmsProviders { get; }
32+
}
33+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 MongoDB.Driver.Authentication;
17+
using MongoDB.Driver.Encryption;
18+
19+
namespace MongoDB.Driver
20+
{
21+
/// <summary>
22+
/// Extension Manager provides a way to configure extensions for the driver.
23+
/// </summary>
24+
public interface IExtensionManager
25+
{
26+
/// <summary>
27+
/// Sasl Mechanisms Registry.
28+
/// </summary>
29+
ISaslMechanismRegistry SaslMechanisms { get; }
30+
31+
/// <summary>
32+
/// Kms Providers Registry.
33+
/// </summary>
34+
IKmsProviderRegistry KmsProviders { get; }
35+
}
36+
}
37+

‎src/MongoDB.Driver/MongoClientSettings.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
using System.Collections.ObjectModel;
1919
using System.Linq;
2020
using System.Text;
21-
using MongoDB.Bson;
2221
using MongoDB.Driver.Core.Clusters;
2322
using MongoDB.Driver.Core.Compression;
2423
using MongoDB.Driver.Core.Configuration;
2524
using MongoDB.Driver.Core.Misc;
2625
using MongoDB.Driver.Core.Servers;
2726
using MongoDB.Driver.Encryption;
28-
using MongoDB.Driver.Linq;
2927
using MongoDB.Shared;
3028

3129
namespace MongoDB.Driver
@@ -35,6 +33,11 @@ namespace MongoDB.Driver
3533
/// </summary>
3634
public class MongoClientSettings : IEquatable<MongoClientSettings>, IInheritableMongoClientSettings
3735
{
36+
/// <summary>
37+
/// Extension Manager provides a way to configure extensions for the driver.
38+
/// </summary>
39+
public static readonly IExtensionManager Extensions = new ExtensionManager();
40+
3841
// private fields
3942
private bool _allowInsecureTls;
4043
private string _applicationName;

‎tests/MongoDB.Driver.TestHelpers/DriverTestConfiguration.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ static DriverTestConfiguration()
5555
isThreadSafe: true);
5656
__collectionNamespace = new CollectionNamespace(__databaseNamespace, "testcollection");
5757

58-
SaslMechanismRegistry.Instance.RegisterAWSMechanism();
59-
KmsProviderRegistry.Instance.RegisterAWSKmsProvider();
58+
MongoClientSettings.Extensions.AddAWSAuthentication();
6059
}
6160

6261
// public static properties

0 commit comments

Comments
 (0)
Please sign in to comment.