Skip to content

Commit 536f100

Browse files
CSHARP-2425: Implement Unified URI Options.
1 parent c0ea843 commit 536f100

40 files changed

+2062
-329
lines changed

src/MongoDB.Driver.Core/Core/Configuration/ClusterBuilderExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,13 @@ public static ClusterBuilder ConfigureWithConnectionString(this ClusterBuilder b
9090
writeTimeout: connectionString.SocketTimeout.Value));
9191
}
9292

93-
if (connectionString.Ssl != null)
93+
if (connectionString.Tls != null)
9494
{
9595
builder = builder.ConfigureSsl(ssl =>
9696
{
97-
if (!connectionString.SslVerifyCertificate.GetValueOrDefault(true))
97+
if (connectionString.TlsInsecure.GetValueOrDefault(false))
9898
{
99-
ssl = ssl.With(
100-
serverCertificateValidationCallback: new RemoteCertificateValidationCallback(AcceptAnySslCertificate));
99+
ssl = ssl.With(serverCertificateValidationCallback: new RemoteCertificateValidationCallback(AcceptAnySslCertificate));
101100
}
102101

103102
return ssl;

src/MongoDB.Driver.Core/Core/Configuration/ConnectionString.cs

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public sealed class ConnectionString
9494
private ConnectionStringScheme _scheme;
9595
private TimeSpan? _serverSelectionTimeout;
9696
private TimeSpan? _socketTimeout;
97-
private bool? _ssl;
98-
private bool? _sslVerifyCertificate;
97+
private bool? _tls;
98+
private bool? _tlsInsecure;
9999
private string _username;
100100
private GuidRepresentation? _uuidRepresentation;
101101
private double? _waitQueueMultiple;
@@ -383,7 +383,7 @@ public bool? RetryWrites
383383
get { return _retryWrites; }
384384
}
385385

386-
/// <summary>
386+
/// <summary>
387387
/// Gets the connection string scheme.
388388
/// </summary>
389389
public ConnectionStringScheme Scheme
@@ -410,18 +410,27 @@ public TimeSpan? SocketTimeout
410410
/// <summary>
411411
/// Gets whether to use SSL.
412412
/// </summary>
413+
[Obsolete("Use Tls instead.")]
413414
public bool? Ssl
414415
{
415-
get { return _ssl; }
416+
get { return _tls; }
416417
}
417418

418419
/// <summary>
419420
/// Gets whether to verify SSL certificates.
420421
/// </summary>
421-
public bool? SslVerifyCertificate
422-
{
423-
get { return _sslVerifyCertificate; }
424-
}
422+
[Obsolete("Use TlsInsecure instead.")]
423+
public bool? SslVerifyCertificate => !_tlsInsecure;
424+
425+
/// <summary>
426+
/// Gets whether to use TLS.
427+
/// </summary>
428+
public bool? Tls => _tls;
429+
430+
/// <summary>
431+
/// Gets whether to relax TLS constraints as much as possible.
432+
/// </summary>
433+
public bool? TlsInsecure => _tlsInsecure;
425434

426435
/// <summary>
427436
/// Gets the username.
@@ -620,9 +629,9 @@ private ConnectionString BuildResolvedConnectionString(ConnectionStringScheme re
620629
}
621630

622631
// remove any option from the resolved options that was specified locally
623-
foreach(var key in _allOptions.AllKeys)
632+
foreach (var key in _allOptions.AllKeys)
624633
{
625-
if(resolvedOptions.Get(key) != null)
634+
if (resolvedOptions.Get(key) != null)
626635
{
627636
resolvedOptions.Remove(key);
628637
}
@@ -654,8 +663,11 @@ private void ExtractScheme(Match match)
654663
if (schemeGroup.Value == "mongodb+srv")
655664
{
656665
_scheme = ConnectionStringScheme.MongoDBPlusSrv;
657-
_ssl = true;
658-
_allOptions.Add("ssl", "true");
666+
if (!_tls.HasValue)
667+
{
668+
_tls = true;
669+
_allOptions.Add("tls", "true");
670+
}
659671
}
660672
}
661673
}
@@ -777,10 +789,10 @@ private void Parse()
777789
throw new MongoConfigurationException(message);
778790
}
779791

780-
ExtractScheme(match);
781792
ExtractUsernameAndPassword(match);
782793
ExtractDatabaseName(match);
783794
ExtractOptions(match);
795+
ExtractScheme(match);
784796
ExtractHosts(match);
785797

786798
if (_journal.HasValue && _journal.Value && _w != null && _w.Equals(0))
@@ -940,11 +952,22 @@ private void ParseOption(string name, string value)
940952
case "sockettimeoutms":
941953
_socketTimeout = ParseTimeSpan(name, value);
942954
break;
943-
case "ssl":
944-
_ssl = ParseBoolean(name, value);
955+
case "ssl": // Obsolete
956+
case "tls":
957+
var tlsValue = ParseBoolean(name, value);
958+
if (_tls.HasValue && _tls.Value != tlsValue)
959+
{
960+
throw new MongoConfigurationException("tls has already been configured with a different value.");
961+
}
962+
_tls = tlsValue;
963+
break;
964+
case "sslverifycertificate": // Obsolete
965+
var sslVerifyCertificateValue = ParseBoolean(name, value);
966+
_tlsInsecure = EnsureTlsInsecureIsValid(!sslVerifyCertificateValue);
945967
break;
946-
case "sslverifycertificate":
947-
_sslVerifyCertificate = ParseBoolean(name, value);
968+
case "tlsinsecure":
969+
var tlsInsecureValue = ParseBoolean(name, value);
970+
_tlsInsecure = EnsureTlsInsecureIsValid(tlsInsecureValue);
948971
break;
949972
case "guids":
950973
case "uuidrepresentation":
@@ -1126,6 +1149,16 @@ private static TimeSpan ParseTimeSpan(string name, string value)
11261149
}
11271150
}
11281151

1152+
private bool EnsureTlsInsecureIsValid(bool value)
1153+
{
1154+
if (_tlsInsecure.HasValue && _tlsInsecure.Value != value)
1155+
{
1156+
throw new MongoConfigurationException("tlsInsecure has already been configured with a different value.");
1157+
}
1158+
1159+
return value;
1160+
}
1161+
11291162
private List<string> GetHostsFromResponse(IDnsQueryResponse response)
11301163
{
11311164
var hosts = new List<string>();
@@ -1176,7 +1209,7 @@ private void ValidateResolvedHosts(string original, List<string> resolved)
11761209
}
11771210

11781211
// for each resolved host, make sure that it ends with domain of the parent.
1179-
foreach(var resolvedHost in resolved)
1212+
foreach (var resolvedHost in resolved)
11801213
{
11811214
EndPoint endPoint;
11821215
if (!EndPointHelper.TryParse(resolvedHost, 0, out endPoint) || !(endPoint is DnsEndPoint))
@@ -1206,8 +1239,8 @@ internal static bool HasValidParentDomain(string original, DnsEndPoint resolvedE
12061239
return false;
12071240
}
12081241

1209-
// loop from back to front making sure that all of b is at the back of a, in order.
1210-
for (int ai = a.Length - 1, bi = b.Length - 1; bi >= 0; ai--, bi--)
1242+
// loop from back to front making sure that all of b is at the back of a, in order.
1243+
for (int ai = a.Length - 1, bi = b.Length - 1; bi >= 0; ai--, bi--)
12111244
{
12121245
if (a[ai] != b[bi])
12131246
{

src/MongoDB.Driver.Legacy/MongoServerSettings.cs

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace MongoDB.Driver
3232
public class MongoServerSettings : IEquatable<MongoServerSettings>, IInheritableMongoClientSettings
3333
{
3434
// private fields
35+
private bool _allowInsecureTls;
3536
private string _applicationName;
3637
private Action<ClusterBuilder> _clusterConfigurator;
3738
private IReadOnlyList<CompressorConfiguration> _compressors;
@@ -60,8 +61,7 @@ public class MongoServerSettings : IEquatable<MongoServerSettings>, IInheritable
6061
private TimeSpan _serverSelectionTimeout;
6162
private TimeSpan _socketTimeout;
6263
private SslSettings _sslSettings;
63-
private bool _useSsl;
64-
private bool _verifySslCertificate;
64+
private bool _useTls;
6565
private int _waitQueueSize;
6666
private TimeSpan _waitQueueTimeout;
6767
private WriteConcern _writeConcern;
@@ -78,6 +78,7 @@ public class MongoServerSettings : IEquatable<MongoServerSettings>, IInheritable
7878
/// </summary>
7979
public MongoServerSettings()
8080
{
81+
_allowInsecureTls = false;
8182
_applicationName = null;
8283
_compressors = new CompressorConfiguration[0];
8384
_connectionMode = ConnectionMode.Automatic;
@@ -105,8 +106,7 @@ public MongoServerSettings()
105106
_serverSelectionTimeout = MongoDefaults.ServerSelectionTimeout;
106107
_socketTimeout = MongoDefaults.SocketTimeout;
107108
_sslSettings = null;
108-
_useSsl = false;
109-
_verifySslCertificate = true;
109+
_useTls = false;
110110
_waitQueueSize = MongoDefaults.ComputedWaitQueueSize;
111111
_waitQueueTimeout = MongoDefaults.WaitQueueTimeout;
112112
_writeConcern = WriteConcern.Unacknowledged;
@@ -123,6 +123,19 @@ public AddressFamily AddressFamily
123123
get { return _ipv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; }
124124
}
125125

126+
/// <summary>
127+
/// Gets or sets whether to relax TLS constraints as much as possible.
128+
/// </summary>
129+
public bool AllowInsecureTls
130+
{
131+
get { return _allowInsecureTls; }
132+
set
133+
{
134+
if (_isFrozen) { throw new InvalidOperationException("MongoServerSettings is frozen."); }
135+
_allowInsecureTls = value;
136+
}
137+
}
138+
126139
/// <summary>
127140
/// Gets or sets the application name.
128141
/// </summary>
@@ -552,26 +565,41 @@ public SslSettings SslSettings
552565
/// <summary>
553566
/// Gets or sets a value indicating whether to use SSL.
554567
/// </summary>
568+
[Obsolete("Use UseTls instead.")]
555569
public bool UseSsl
556570
{
557-
get { return _useSsl; }
571+
get { return _useTls; }
572+
set
573+
{
574+
if (_isFrozen) { throw new InvalidOperationException("MongoServerSettings is frozen."); }
575+
_useTls = value;
576+
}
577+
}
578+
579+
/// <summary>
580+
/// Gets or sets a value indicating whether to use TLS.
581+
/// </summary>
582+
public bool UseTls
583+
{
584+
get { return _useTls; }
558585
set
559586
{
560587
if (_isFrozen) { throw new InvalidOperationException("MongoServerSettings is frozen."); }
561-
_useSsl = value;
588+
_useTls = value;
562589
}
563590
}
564591

565592
/// <summary>
566593
/// Gets or sets a value indicating whether to verify an SSL certificate.
567594
/// </summary>
595+
[Obsolete("Use AllowInsecureTls instead.")]
568596
public bool VerifySslCertificate
569597
{
570-
get { return _verifySslCertificate; }
598+
get { return !_allowInsecureTls; }
571599
set
572600
{
573601
if (_isFrozen) { throw new InvalidOperationException("MongoServerSettings is frozen."); }
574-
_verifySslCertificate = value;
602+
_allowInsecureTls = !value;
575603
}
576604
}
577605

@@ -667,6 +695,7 @@ public UTF8Encoding WriteEncoding
667695
public static MongoServerSettings FromClientSettings(MongoClientSettings clientSettings)
668696
{
669697
var serverSettings = new MongoServerSettings();
698+
serverSettings.AllowInsecureTls = clientSettings.AllowInsecureTls;
670699
serverSettings.ApplicationName = clientSettings.ApplicationName;
671700
serverSettings.ClusterConfigurator = clientSettings.ClusterConfigurator;
672701
serverSettings.Compressors = clientSettings.Compressors;
@@ -696,8 +725,7 @@ public static MongoServerSettings FromClientSettings(MongoClientSettings clientS
696725
serverSettings.ServerSelectionTimeout = clientSettings.ServerSelectionTimeout;
697726
serverSettings.SocketTimeout = clientSettings.SocketTimeout;
698727
serverSettings.SslSettings = (clientSettings.SslSettings == null) ? null : clientSettings.SslSettings.Clone();
699-
serverSettings.UseSsl = clientSettings.UseSsl;
700-
serverSettings.VerifySslCertificate = clientSettings.VerifySslCertificate;
728+
serverSettings.UseTls = clientSettings.UseTls;
701729
serverSettings.WaitQueueSize = clientSettings.WaitQueueSize;
702730
serverSettings.WaitQueueTimeout = clientSettings.WaitQueueTimeout;
703731
serverSettings.WriteConcern = clientSettings.WriteConcern;
@@ -715,6 +743,7 @@ public static MongoServerSettings FromUrl(MongoUrl url)
715743
var credential = url.GetCredential();
716744

717745
var serverSettings = new MongoServerSettings();
746+
serverSettings.AllowInsecureTls = url.AllowInsecureTls;
718747
serverSettings.ApplicationName = url.ApplicationName;
719748
serverSettings.Compressors = url.Compressors;
720749
serverSettings.ConnectionMode = url.ConnectionMode;
@@ -755,8 +784,7 @@ public static MongoServerSettings FromUrl(MongoUrl url)
755784
serverSettings.ServerSelectionTimeout = url.ServerSelectionTimeout;
756785
serverSettings.SocketTimeout = url.SocketTimeout;
757786
serverSettings.SslSettings = null; // SSL settings must be provided in code
758-
serverSettings.UseSsl = url.UseSsl;
759-
serverSettings.VerifySslCertificate = url.VerifySslCertificate;
787+
serverSettings.UseTls = url.UseTls;
760788
serverSettings.WaitQueueSize = url.ComputedWaitQueueSize;
761789
serverSettings.WaitQueueTimeout = url.WaitQueueTimeout;
762790
serverSettings.WriteConcern = url.GetWriteConcern(false);
@@ -772,6 +800,7 @@ public static MongoServerSettings FromUrl(MongoUrl url)
772800
public MongoServerSettings Clone()
773801
{
774802
var clone = new MongoServerSettings();
803+
clone._allowInsecureTls = _allowInsecureTls;
775804
clone._applicationName = _applicationName;
776805
clone._clusterConfigurator = _clusterConfigurator;
777806
clone._compressors = _compressors;
@@ -800,8 +829,7 @@ public MongoServerSettings Clone()
800829
clone._serverSelectionTimeout = _serverSelectionTimeout;
801830
clone._socketTimeout = _socketTimeout;
802831
clone._sslSettings = (_sslSettings == null) ? null : _sslSettings.Clone();
803-
clone._useSsl = _useSsl;
804-
clone._verifySslCertificate = _verifySslCertificate;
832+
clone._useTls = _useTls;
805833
clone._waitQueueSize = _waitQueueSize;
806834
clone._waitQueueTimeout = _waitQueueTimeout;
807835
clone._writeConcern = _writeConcern;
@@ -833,6 +861,7 @@ public override bool Equals(object obj)
833861
if (object.ReferenceEquals(obj, null) || GetType() != obj.GetType()) { return false; }
834862
var rhs = (MongoServerSettings)obj;
835863
return
864+
_allowInsecureTls == rhs._allowInsecureTls &&
836865
_applicationName == rhs._applicationName &&
837866
object.ReferenceEquals(_clusterConfigurator, rhs._clusterConfigurator) &&
838867
_compressors.SequenceEqual(rhs._compressors) &&
@@ -861,8 +890,7 @@ public override bool Equals(object obj)
861890
_serverSelectionTimeout == rhs._serverSelectionTimeout &&
862891
_socketTimeout == rhs._socketTimeout &&
863892
_sslSettings == rhs._sslSettings &&
864-
_useSsl == rhs._useSsl &&
865-
_verifySslCertificate == rhs._verifySslCertificate &&
893+
_useTls == rhs._useTls &&
866894
_waitQueueSize == rhs._waitQueueSize &&
867895
_waitQueueTimeout == rhs._waitQueueTimeout &&
868896
_writeConcern.Equals(rhs._writeConcern) &&
@@ -912,6 +940,7 @@ public override int GetHashCode()
912940
}
913941

914942
return new Hasher()
943+
.Hash(_allowInsecureTls)
915944
.Hash(_applicationName)
916945
.Hash(_clusterConfigurator)
917946
.HashElements(_compressors)
@@ -940,8 +969,7 @@ public override int GetHashCode()
940969
.Hash(_serverSelectionTimeout)
941970
.Hash(_socketTimeout)
942971
.Hash(_sslSettings)
943-
.Hash(_useSsl)
944-
.Hash(_verifySslCertificate)
972+
.Hash(_useTls)
945973
.Hash(_waitQueueSize)
946974
.Hash(_waitQueueTimeout)
947975
.Hash(_writeConcern)
@@ -1008,8 +1036,8 @@ public override string ToString()
10081036
{
10091037
parts.Add(string.Format("SslSettings={0}", _sslSettings));
10101038
}
1011-
parts.Add(string.Format("Ssl={0}", _useSsl));
1012-
parts.Add(string.Format("SslVerifyCertificate={0}", _verifySslCertificate));
1039+
parts.Add(string.Format("Tls={0}", _useTls));
1040+
parts.Add(string.Format("TlsInsecure={0}", _allowInsecureTls));
10131041
parts.Add(string.Format("WaitQueueSize={0}", _waitQueueSize));
10141042
parts.Add(string.Format("WaitQueueTimeout={0}", _waitQueueTimeout));
10151043
parts.Add(string.Format("WriteConcern={0}", _writeConcern));
@@ -1024,6 +1052,7 @@ public override string ToString()
10241052
internal ClusterKey ToClusterKey()
10251053
{
10261054
return new ClusterKey(
1055+
_allowInsecureTls,
10271056
_applicationName,
10281057
_clusterConfigurator,
10291058
_compressors,
@@ -1047,8 +1076,7 @@ internal ClusterKey ToClusterKey()
10471076
_serverSelectionTimeout,
10481077
_socketTimeout,
10491078
_sslSettings,
1050-
_useSsl,
1051-
_verifySslCertificate,
1079+
_useTls,
10521080
_waitQueueSize,
10531081
_waitQueueTimeout);
10541082
}

0 commit comments

Comments
 (0)