Skip to content

Commit 77c60d5

Browse files
author
Oleksandr Poliakov
committed
Pr
1 parent 138efa7 commit 77c60d5

File tree

2 files changed

+31
-40
lines changed

2 files changed

+31
-40
lines changed

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ public OidcConfiguration(
5656
TokenResource = GetProperty<string>(authorizationProperty);
5757
break;
5858
default:
59-
throw new ArgumentException(
60-
$"Unknown OIDC property '{authorizationProperty.Key}'.",
61-
authorizationProperty.Key);
59+
throw new MongoConfigurationException($"Unknown OIDC property '{authorizationProperty.Key}'.");
6260
}
6361
}
6462
}
@@ -73,7 +71,7 @@ static T GetProperty<T>(KeyValuePair<string, object> property)
7371
return result;
7472
}
7573

76-
throw new ArgumentException($"Cannot read {property.Key} property as {typeof(T).Name}", property.Key);
74+
throw new MongoConfigurationException($"Cannot read {property.Key} property as {typeof(T).Name}");
7775
}
7876
}
7977

@@ -110,39 +108,33 @@ private void ValidateOptions()
110108
{
111109
if (Environment == null && Callback == null)
112110
{
113-
throw new ArgumentException($"{EnvironmentMechanismPropertyName} or {CallbackMechanismPropertyName} must be configured.");
111+
throw new MongoConfigurationException($"{EnvironmentMechanismPropertyName} or {CallbackMechanismPropertyName} must be configured.");
114112
}
115113

116114
if (Environment != null && Callback != null)
117115
{
118-
throw new ArgumentException($"{CallbackMechanismPropertyName} is mutually exclusive with {EnvironmentMechanismPropertyName}.");
116+
throw new MongoConfigurationException($"{CallbackMechanismPropertyName} is mutually exclusive with {EnvironmentMechanismPropertyName}.");
119117
}
120118

121119
if (Environment != null && !__supportedEnvironments.Contains(Environment))
122120
{
123-
throw new ArgumentException(
124-
$"Not supported value of {EnvironmentMechanismPropertyName} mechanism property: {Environment}.",
125-
EnvironmentMechanismPropertyName);
121+
throw new MongoConfigurationException($"Not supported value of {EnvironmentMechanismPropertyName} mechanism property: {Environment}.");
126122
}
127123

128124
var tokenResourceRequired = Environment == "azure" || Environment == "gcp";
129125
if (!tokenResourceRequired && !string.IsNullOrEmpty(TokenResource))
130126
{
131-
throw new ArgumentException(
132-
$"{TokenResourceMechanismPropertyName} mechanism property is not supported by {Environment} environment.",
133-
TokenResourceMechanismPropertyName);
127+
throw new MongoConfigurationException($"{TokenResourceMechanismPropertyName} mechanism property is not supported by {Environment} environment.");
134128
}
135129

136130
if (tokenResourceRequired && string.IsNullOrEmpty(TokenResource))
137131
{
138-
throw new ArgumentException(
139-
$"{TokenResourceMechanismPropertyName} mechanism property is required by {Environment} environment.",
140-
TokenResourceMechanismPropertyName);
132+
throw new MongoConfigurationException($"{TokenResourceMechanismPropertyName} mechanism property is required by {Environment} environment.");
141133
}
142134

143135
if (Environment == "test" && !string.IsNullOrEmpty(PrincipalName))
144136
{
145-
throw new ArgumentException($"{nameof(PrincipalName)} is not supported by {Environment} environment.", nameof(PrincipalName));
137+
throw new MongoConfigurationException($"{nameof(PrincipalName)} is not supported by {Environment} environment.");
146138
}
147139
}
148140
}

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)