Skip to content

Commit 91149d9

Browse files
author
Oleksandr Poliakov
committed
Pr
1 parent 78b3925 commit 91149d9

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

src/MongoDB.Driver.Core/Core/Authentication/Oidc/OidcConfiguration.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public OidcConfiguration(
3939
EndPoints = Ensure.IsNotNullOrEmpty(endPoints, nameof(endPoints));
4040
Ensure.IsNotNull(authMechanismProperties, nameof(authMechanismProperties));
4141
PrincipalName = principalName;
42-
42+
4343
foreach (var authorizationProperty in authMechanismProperties)
4444
{
4545
switch (authorizationProperty.Key)
@@ -54,9 +54,7 @@ public OidcConfiguration(
5454
TokenResource = GetProperty<string>(authorizationProperty);
5555
break;
5656
default:
57-
throw new ArgumentException(
58-
$"Unknown OIDC property '{authorizationProperty.Key}'.",
59-
authorizationProperty.Key);
57+
throw new MongoConfigurationException($"Unknown OIDC property '{authorizationProperty.Key}'.");
6058
}
6159
}
6260

@@ -70,7 +68,7 @@ static T GetProperty<T>(KeyValuePair<string, object> property)
7068
return result;
7169
}
7270

73-
throw new ArgumentException($"Cannot read {property.Key} property as {typeof(T).Name}", property.Key);
71+
throw new MongoConfigurationException($"Cannot read {property.Key} property as {typeof(T).Name}");
7472
}
7573
}
7674

@@ -107,39 +105,33 @@ private void ValidateOptions()
107105
{
108106
if (Environment == null && Callback == null)
109107
{
110-
throw new ArgumentException($"{EnvironmentMechanismPropertyName} or {CallbackMechanismPropertyName} must be configured.");
108+
throw new MongoConfigurationException($"{EnvironmentMechanismPropertyName} or {CallbackMechanismPropertyName} must be configured.");
111109
}
112110

113111
if (Environment != null && Callback != null)
114112
{
115-
throw new ArgumentException($"{CallbackMechanismPropertyName} is mutually exclusive with {EnvironmentMechanismPropertyName}.");
113+
throw new MongoConfigurationException($"{CallbackMechanismPropertyName} is mutually exclusive with {EnvironmentMechanismPropertyName}.");
116114
}
117115

118116
if (Environment != null && !__supportedEnvironments.Contains(Environment))
119117
{
120-
throw new ArgumentException(
121-
$"Not supported value of {EnvironmentMechanismPropertyName} mechanism property: {Environment}.",
122-
EnvironmentMechanismPropertyName);
118+
throw new MongoConfigurationException($"Not supported value of {EnvironmentMechanismPropertyName} mechanism property: {Environment}.");
123119
}
124120

125121
var tokenResourceRequired = Environment == "azure" || Environment == "gcp";
126122
if (!tokenResourceRequired && !string.IsNullOrEmpty(TokenResource))
127123
{
128-
throw new ArgumentException(
129-
$"{TokenResourceMechanismPropertyName} mechanism property is not supported by {Environment} environment.",
130-
TokenResourceMechanismPropertyName);
124+
throw new MongoConfigurationException($"{TokenResourceMechanismPropertyName} mechanism property is not supported by {Environment} environment.");
131125
}
132126

133127
if (tokenResourceRequired && string.IsNullOrEmpty(TokenResource))
134128
{
135-
throw new ArgumentException(
136-
$"{TokenResourceMechanismPropertyName} mechanism property is required by {Environment} environment.",
137-
TokenResourceMechanismPropertyName);
129+
throw new MongoConfigurationException($"{TokenResourceMechanismPropertyName} mechanism property is required by {Environment} environment.");
138130
}
139131

140132
if (Environment == "test" && !string.IsNullOrEmpty(PrincipalName))
141133
{
142-
throw new ArgumentException($"{nameof(PrincipalName)} is not supported by {Environment} environment.", nameof(PrincipalName));
134+
throw new MongoConfigurationException($"{nameof(PrincipalName)} is not supported by {Environment} environment.");
143135
}
144136
}
145137
}

tests/MongoDB.Driver.Core.Tests/Core/Authentication/Oidc/OidcConfigurationTests.cs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,38 +61,37 @@ public void Constructor_throws_on_invalid_arguments(
6161
IReadOnlyList<EndPoint> endPoints,
6262
string principalName,
6363
IReadOnlyDictionary<string, object> mechanismProperties,
64-
string paramName)
64+
Type exceptionType)
6565
{
6666
var exception = Record.Exception(() =>
6767
new OidcConfiguration(endPoints, principalName, mechanismProperties));
6868

69-
exception.Should().BeAssignableTo<ArgumentException>()
70-
.Subject.ParamName.Should().Be(paramName);
69+
exception.Should().BeOfType(exceptionType);
7170
}
7271

7372
public static IEnumerable<object[]> InvalidConfigurationTestCases = new[]
7473
{
75-
new object[] { null, null, new Dictionary<string, object> { ["ENVIRONMENT"] = "test" }, "endPoints" },
76-
new object[] { Array.Empty<EndPoint>(), null, new Dictionary<string, object> { ["ENVIRONMENT"] = "test" }, "endPoints" },
77-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, null, null, "authMechanismProperties" },
78-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", null, "authMechanismProperties" },
79-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, null, new Dictionary<string, object>(), null },
80-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object>(), null },
81-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["unknown_property"] = 42 }, "unknown_property" },
82-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = null }, "ENVIRONMENT" },
83-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "" }, "ENVIRONMENT" },
84-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = 1 }, "ENVIRONMENT" },
85-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "unknown provider" }, "ENVIRONMENT" },
86-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["OIDC_CALLBACK"] = null }, "OIDC_CALLBACK" },
87-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["OIDC_CALLBACK"] = "invalid type" }, "OIDC_CALLBACK" },
88-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["OIDC_CALLBACK"] = __callbackMock, ["ENVIRONMENT"] = "test" }, null },
89-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "test", ["TOKEN_RESOURCE"] = "tr" }, "TOKEN_RESOURCE" },
90-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "azure" }, "TOKEN_RESOURCE" },
91-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "azure", ["TOKEN_RESOURCE"] = null }, "TOKEN_RESOURCE" },
92-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "azure", ["TOKEN_RESOURCE"] = "" }, "TOKEN_RESOURCE" },
93-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "gcp" }, "TOKEN_RESOURCE" },
94-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "gcp", ["TOKEN_RESOURCE"] = null }, "TOKEN_RESOURCE" },
95-
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "gcp", ["TOKEN_RESOURCE"] = "" }, "TOKEN_RESOURCE" },
74+
new object[] { null, null, new Dictionary<string, object> { ["ENVIRONMENT"] = "test" }, typeof(ArgumentNullException) },
75+
new object[] { Array.Empty<EndPoint>(), null, new Dictionary<string, object> { ["ENVIRONMENT"] = "test" }, typeof(ArgumentException) },
76+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, null, null, typeof(ArgumentNullException) },
77+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", null, typeof(ArgumentNullException) },
78+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, null, new Dictionary<string, object>(), typeof(MongoConfigurationException) },
79+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object>(), typeof(MongoConfigurationException) },
80+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["unknown_property"] = 42 }, typeof(MongoConfigurationException) },
81+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = null }, typeof(MongoConfigurationException) },
82+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "" }, typeof(MongoConfigurationException) },
83+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = 1 }, typeof(MongoConfigurationException) },
84+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "unknown provider" }, typeof(MongoConfigurationException) },
85+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["OIDC_CALLBACK"] = null }, typeof(MongoConfigurationException) },
86+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["OIDC_CALLBACK"] = "invalid type" }, typeof(MongoConfigurationException) },
87+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["OIDC_CALLBACK"] = __callbackMock, ["ENVIRONMENT"] = "test" }, typeof(MongoConfigurationException) },
88+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "test", ["TOKEN_RESOURCE"] = "tr" }, typeof(MongoConfigurationException) },
89+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "azure" }, typeof(MongoConfigurationException) },
90+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "azure", ["TOKEN_RESOURCE"] = null }, typeof(MongoConfigurationException) },
91+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "azure", ["TOKEN_RESOURCE"] = "" }, typeof(MongoConfigurationException) },
92+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "gcp" }, typeof(MongoConfigurationException) },
93+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "gcp", ["TOKEN_RESOURCE"] = null }, typeof(MongoConfigurationException) },
94+
new object[] { new[] { new DnsEndPoint("localhost", 27017) }, "name", new Dictionary<string, object> { ["ENVIRONMENT"] = "gcp", ["TOKEN_RESOURCE"] = "" }, typeof(MongoConfigurationException) },
9695
};
9796

9897
[Theory]

0 commit comments

Comments
 (0)