Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 5e590fd

Browse files
Added SslProtocols to connection string.
1 parent a4cdab7 commit 5e590fd

File tree

6 files changed

+37
-12
lines changed

6 files changed

+37
-12
lines changed

src/ServiceStack.Redis/RedisEndpoint.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public RedisEndpoint(string host, int port, string password = null, long db = Re
3535
public string Host { get; set; }
3636
public int Port { get; set; }
3737
public bool Ssl { get; set; }
38-
public SslProtocols? SslProtocol {get; set;}
38+
public SslProtocols? SslProtocols {get; set;}
3939
public int ConnectTimeout { get; set; }
4040
public int SendTimeout { get; set; }
4141
public int ReceiveTimeout { get; set; }
@@ -61,8 +61,8 @@ public override string ToString()
6161
args.Add("Db=" + Db);
6262
if (Ssl)
6363
args.Add("Ssl=true");
64-
if (SslProtocol != null)
65-
args.Add("SslProtocols=" + SslProtocol.ToString());
64+
if (SslProtocols != null)
65+
args.Add("SslProtocols=" + SslProtocols.ToString());
6666
if (ConnectTimeout != RedisConfig.DefaultConnectTimeout)
6767
args.Add("ConnectTimeout=" + ConnectTimeout);
6868
if (SendTimeout != RedisConfig.DefaultSendTimeout)
@@ -87,7 +87,7 @@ protected bool Equals(RedisEndpoint other)
8787
return string.Equals(Host, other.Host)
8888
&& Port == other.Port
8989
&& Ssl.Equals(other.Ssl)
90-
&& SslProtocol.Equals(other.SslProtocol)
90+
&& SslProtocols.Equals(other.SslProtocols)
9191
&& ConnectTimeout == other.ConnectTimeout
9292
&& SendTimeout == other.SendTimeout
9393
&& ReceiveTimeout == other.ReceiveTimeout
@@ -114,7 +114,7 @@ public override int GetHashCode()
114114
var hashCode = (Host != null ? Host.GetHashCode() : 0);
115115
hashCode = (hashCode * 397) ^ Port;
116116
hashCode = (hashCode * 397) ^ Ssl.GetHashCode();
117-
hashCode = (hashCode * 397) ^ SslProtocol.GetHashCode();
117+
hashCode = (hashCode * 397) ^ SslProtocols.GetHashCode();
118118
hashCode = (hashCode * 397) ^ ConnectTimeout;
119119
hashCode = (hashCode * 397) ^ SendTimeout;
120120
hashCode = (hashCode * 397) ^ ReceiveTimeout;

src/ServiceStack.Redis/RedisExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ public static RedisEndpoint ToRedisEndpoint(this string connectionString, int? d
7777
if (useDefaultPort)
7878
endpoint.Port = RedisConfig.DefaultPortSsl;
7979
break;
80-
case "sslProtocols":
80+
case "sslprotocols":
8181
SslProtocols protocols;
82+
value = value?.Replace("|", ",");
8283
if (!Enum.TryParse(value, true, out protocols)) throw new ArgumentOutOfRangeException("Keyword '" + name + "' requires an SslProtocol value (multiple values separated by '|').");
83-
endpoint.SslProtocol = protocols;
84+
endpoint.SslProtocols = protocols;
8485
break;
8586
case "client":
8687
endpoint.Client = value;

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ internal bool Active
9292
public string Host { get; private set; }
9393
public int Port { get; private set; }
9494
public bool Ssl { get; private set; }
95-
public SslProtocols? SslProtocol { get; private set; }
95+
public SslProtocols? SslProtocols { get; private set; }
9696

9797
/// <summary>
9898
/// Gets or sets object key prefix.
@@ -179,7 +179,7 @@ private void Init(RedisEndpoint config)
179179
Client = config.Client;
180180
Db = config.Db;
181181
Ssl = config.Ssl;
182-
SslProtocol = config.SslProtocol;
182+
SslProtocols = config.SslProtocols;
183183
IdleTimeOutSecs = config.IdleTimeOutSecs;
184184
ServerVersionNumber = RedisConfig.AssumeServerVersion.GetValueOrDefault();
185185
LogPrefix = "#" + ClientId + " ";

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,14 @@ private void Connect()
169169
#if NETSTANDARD2_0
170170
sslStream.AuthenticateAsClientAsync(Host).Wait();
171171
#else
172-
sslStream.AuthenticateAsClient(Host, new X509CertificateCollection(), SslProtocol ?? SslProtocols.Default, checkCertificateRevocation: true);
172+
if (SslProtocols != null)
173+
{
174+
sslStream.AuthenticateAsClient(Host, new X509CertificateCollection(), SslProtocols ?? System.Security.Authentication.SslProtocols.Default, checkCertificateRevocation: true);
175+
} else
176+
{
177+
sslStream.AuthenticateAsClient(Host);
178+
}
179+
173180
#endif
174181

175182
if (!sslStream.IsEncrypted)

tests/ServiceStack.Redis.Tests/ConfigTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ public void OneTimeTearDown()
2727
[TestCase("host:1?password=pass&client=nunit", "{Host:host,Port:1,Client:nunit,Password:pass}")]
2828
[TestCase("host:1?db=2", "{Host:host,Port:1,Db:2}")]
2929
[TestCase("host?ssl=true", "{Host:host,Port:6380,Ssl:True}")]
30+
[TestCase("host:6380?ssl=true&password=pass&sslprotocols=Tls12", "{Host:host,Port:6380,Ssl:True,Password:pass,SslProtocols:Tls12}")]
3031
[TestCase("host:1?ssl=true", "{Host:host,Port:1,Ssl:True}")]
3132
[TestCase("host:1?connectTimeout=1&sendtimeout=2&receiveTimeout=3&idletimeoutsecs=4",
3233
"{Host:host,Port:1,ConnectTimeout:1,SendTimeout:2,ReceiveTimeout:3,IdleTimeOutSecs:4}")]
3334
[TestCase("redis://nunit:pass@host:1?ssl=true&db=1&connectTimeout=2&sendtimeout=3&receiveTimeout=4&retryTimeout=5&idletimeoutsecs=5&NamespacePrefix=prefix.",
3435
"{Host:host,Port:1,Ssl:True,Client:nunit,Password:pass,Db:1,ConnectTimeout:2,SendTimeout:3,ReceiveTimeout:4,RetryTimeout:5,IdleTimeOutSecs:5,NamespacePrefix:prefix.}")]
36+
[TestCase("redis://nunit:pass@host:1?ssl=true&sslprotocols=Tls12&db=1&connectTimeout=2&sendtimeout=3&receiveTimeout=4&retryTimeout=5&idletimeoutsecs=5&NamespacePrefix=prefix.",
37+
"{Host:host,Port:1,Ssl:True,Client:nunit,Password:pass,SslProtocols:Tls12,Db:1,ConnectTimeout:2,SendTimeout:3,ReceiveTimeout:4,RetryTimeout:5,IdleTimeOutSecs:5,NamespacePrefix:prefix.}")]
3538
public void Does_handle_different_connection_strings_settings(string connString, string expectedJsv)
3639
{
3740
var actual = connString.ToRedisEndpoint();
@@ -55,6 +58,7 @@ public void Does_handle_different_connection_strings_settings(string connString,
5558
"host:1?ConnectTimeout=1&SendTimeout=2&ReceiveTimeout=3&IdleTimeOutSecs=4")]
5659
[TestCase("redis://nunit:pass@host:1?ssl=true&db=1&connectTimeout=2&sendtimeout=3&receiveTimeout=4&idletimeoutsecs=5&NamespacePrefix=prefix.",
5760
"host:1?Client=nunit&Password=pass&Db=1&Ssl=true&ConnectTimeout=2&SendTimeout=3&ReceiveTimeout=4&IdleTimeOutSecs=5&NamespacePrefix=prefix.")]
61+
[TestCase("password@host:6380?ssl=true&sslprotocols=Tls12", "host:6380?Password=password&Ssl=true&SslProtocols=Tls12")]
5862
public void Does_Serialize_RedisEndpoint(string connString, string expectedString)
5963
{
6064
var actual = connString.ToRedisEndpoint();
@@ -64,8 +68,8 @@ public void Does_Serialize_RedisEndpoint(string connString, string expectedStrin
6468
[Test]
6569
public void Does_set_all_properties_on_Client_using_ClientsManagers()
6670
{
67-
var connStr = "redis://nunit:pass@host:1?ssl=true&db=0&connectTimeout=2&sendtimeout=3&receiveTimeout=4&idletimeoutsecs=5&NamespacePrefix=prefix.";
68-
var expected = "{Host:host,Port:1,Ssl:True,Client:nunit,Password:pass,Db:0,ConnectTimeout:2,SendTimeout:3,ReceiveTimeout:4,IdleTimeOutSecs:5,NamespacePrefix:prefix.}"
71+
var connStr = "redis://nunit:pass@host:1?ssl=true&sslprotocols=Tls12&db=0&connectTimeout=2&sendtimeout=3&receiveTimeout=4&idletimeoutsecs=5&NamespacePrefix=prefix.";
72+
var expected = "{Host:host,Port:1,Ssl:True,SslProtocols:Tls12,Client:nunit,Password:pass,Db:0,ConnectTimeout:2,SendTimeout:3,ReceiveTimeout:4,IdleTimeOutSecs:5,NamespacePrefix:prefix.}"
6973
.FromJsv<RedisEndpoint>();
7074

7175
using (var pooledManager = new RedisManagerPool(connStr))
@@ -122,6 +126,7 @@ private static void AssertClient(RedisClient redis, RedisEndpoint expected)
122126
Assert.That(redis.Host, Is.EqualTo(expected.Host));
123127
Assert.That(redis.Port, Is.EqualTo(expected.Port));
124128
Assert.That(redis.Ssl, Is.EqualTo(expected.Ssl));
129+
Assert.That(redis.SslProtocols, Is.EqualTo(expected.SslProtocols));
125130
Assert.That(redis.Client, Is.EqualTo(expected.Client));
126131
Assert.That(redis.Password, Is.EqualTo(expected.Password));
127132
Assert.That(redis.Db, Is.EqualTo(expected.Db));

tests/ServiceStack.Redis.Tests/SslTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ public void Can_connect_to_ssl_azure_redis_with_UrlFormat()
6767
}
6868
}
6969

70+
[Test]
71+
public void Can_connect_to_ssl_azure_redis_with_UrlFormat_Custom_SSL_Protocol ()
72+
{
73+
var url = "redis://{0}?ssl=true&sslprotocols=Tls12&password={1}".Fmt(Host, Password.UrlEncode());
74+
using (var client = new RedisClient(url))
75+
{
76+
client.Set("foo", "bar");
77+
var foo = client.GetValue("foo");
78+
foo.Print();
79+
}
80+
}
81+
7082
[Test]
7183
public void Can_connect_to_ssl_azure_redis_with_PooledClientsManager()
7284
{

0 commit comments

Comments
 (0)