Skip to content

Commit b496c45

Browse files
authored
Allow to provide SslClientAuthenticationOptions when leveraging SslStream (#213)
1 parent 75baff2 commit b496c45

9 files changed

+38
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
**/bin/*
22
**/obj/*
3+
.DS_Store*
34
TestResults/*
45
*.suo
56
*.user

src/Enyim.Caching/Configuration/IMemcachedClientConfiguration.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Net;
4+
using System.Net.Security;
45
using Enyim.Caching.Memcached;
56

67
namespace Enyim.Caching.Configuration
@@ -47,6 +48,8 @@ public interface IMemcachedClientConfiguration
4748
bool UseIPv6 { get; }
4849

4950
bool SuppressException { get; }
51+
52+
SslClientAuthenticationOptions SslClientAuth { get; }
5053
}
5154
}
5255

src/Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.Options;
99
using Microsoft.Extensions.Configuration;
1010
using System.Linq;
11+
using System.Net.Security;
1112
using System.Net.Sockets;
1213
using Enyim.Caching.Memcached.Transcoders;
1314

@@ -123,6 +124,7 @@ public MemcachedClientConfiguration(
123124
UseSslStream = options.UseSslStream;
124125
UseIPv6 = options.UseIPv6;
125126
SuppressException = options.SuppressException;
127+
SslClientAuth = options.SslClientAuth;
126128

127129
if (!string.IsNullOrEmpty(options.KeyTransformer))
128130
{
@@ -351,6 +353,7 @@ IServerPool IMemcachedClientConfiguration.CreatePool()
351353
public bool UseSslStream { get; private set; }
352354
public bool UseIPv6 { get; private set; }
353355
public bool SuppressException { get; private set; }
356+
public SslClientAuthenticationOptions SslClientAuth { get; private set; }
354357

355358
#endregion
356359
}

src/Enyim.Caching/Configuration/MemcachedClientOptions.cs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using System.Net.Security;
67
using System.Threading.Tasks;
78

89
namespace Enyim.Caching.Configuration
@@ -27,6 +28,8 @@ public class MemcachedClientOptions : IOptions<MemcachedClientOptions>
2728

2829
public bool SuppressException { get; set; } = true;
2930

31+
public SslClientAuthenticationOptions SslClientAuth { get; set; }
32+
3033
public IProviderFactory<IMemcachedNodeLocator> NodeLocatorFactory { get; set; }
3134

3235
public MemcachedClientOptions Value => this;

src/Enyim.Caching/Memcached/DefaultServerPool.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public DefaultServerPool(
5050

5151
protected virtual IMemcachedNode CreateNode(EndPoint endpoint)
5252
{
53-
return new MemcachedNode(endpoint, _configuration.SocketPool, _logger, _configuration.UseSslStream, _configuration.UseIPv6);
53+
return new MemcachedNode(endpoint, _configuration.SocketPool, _logger, _configuration.UseSslStream, _configuration.UseIPv6, _configuration.SslClientAuth);
5454
}
5555

5656
private void rezCallback(object state)

src/Enyim.Caching/Memcached/MemcachedNode.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Diagnostics;
1313
using System.IO;
1414
using System.Net;
15+
using System.Net.Security;
1516
using System.Net.Sockets;
1617
using System.Runtime.Serialization;
1718
using System.Security;
@@ -37,16 +38,19 @@ public class MemcachedNode : IMemcachedNode
3738
private readonly TimeSpan _initPoolTimeout;
3839
private bool _useSslStream;
3940
private bool _useIPv6;
41+
private readonly SslClientAuthenticationOptions _sslClientAuthOptions;
4042

4143
public MemcachedNode(
4244
EndPoint endpoint,
4345
ISocketPoolConfiguration socketPoolConfig,
4446
ILogger logger,
4547
bool useSslStream,
46-
bool useIPv6)
48+
bool useIPv6,
49+
SslClientAuthenticationOptions sslClientAuthOptions)
4750
{
4851
_endPoint = endpoint;
4952
_useSslStream = useSslStream;
53+
_sslClientAuthOptions = sslClientAuthOptions;
5054
EndPointString = endpoint?.ToString().Replace("Unspecified/", string.Empty);
5155
_config = socketPoolConfig;
5256

@@ -859,7 +863,7 @@ protected internal virtual PooledSocket CreateSocket()
859863
{
860864
try
861865
{
862-
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream, _useIPv6);
866+
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream, _useIPv6, _sslClientAuthOptions);
863867
ps.Connect();
864868
return ps;
865869
}
@@ -875,7 +879,7 @@ protected internal virtual async Task<PooledSocket> CreateSocketAsync()
875879
{
876880
try
877881
{
878-
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream, _useIPv6);
882+
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream, _useIPv6, _sslClientAuthOptions);
879883
await ps.ConnectAsync();
880884
return ps;
881885
}

src/Enyim.Caching/Memcached/PooledSocket.cs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.Logging;
12
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics;
@@ -6,10 +7,8 @@
67
using System.Net;
78
using System.Net.Security;
89
using System.Net.Sockets;
9-
using System.Text;
1010
using System.Threading;
1111
using System.Threading.Tasks;
12-
using Microsoft.Extensions.Logging;
1312

1413
namespace Enyim.Caching.Memcached
1514
{
@@ -27,13 +26,24 @@ public partial class PooledSocket : IDisposable
2726

2827
private NetworkStream _inputStream;
2928
private SslStream _sslStream;
29+
private readonly SslClientAuthenticationOptions _sslClientAuthOptions;
3030

31-
public PooledSocket(EndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout, ILogger logger, bool useSslStream, bool useIPv6)
31+
public PooledSocket(EndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout, ILogger logger, bool useSslStream, bool useIPv6, SslClientAuthenticationOptions sslClientAuthOptions)
3232
{
3333
_logger = logger;
3434
_isAlive = true;
3535
_useSslStream = useSslStream;
3636
_useIPv6 = useIPv6;
37+
_sslClientAuthOptions = sslClientAuthOptions;
38+
39+
if (_useSslStream && _sslClientAuthOptions == null)
40+
{
41+
// When not provided, create a default instance with target host set to the endpoint's host
42+
_sslClientAuthOptions = new SslClientAuthenticationOptions
43+
{
44+
TargetHost = ((DnsEndPoint)_endpoint).Host,
45+
};
46+
}
3747

3848
var socket = new Socket(useIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3949
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
@@ -99,7 +109,7 @@ void Cancel()
99109
if (_useSslStream)
100110
{
101111
_sslStream = new SslStream(new NetworkStream(_socket));
102-
_sslStream.AuthenticateAsClient(((DnsEndPoint)_endpoint).Host);
112+
_sslStream.AuthenticateAsClient(_sslClientAuthOptions);
103113
}
104114
else
105115
{
@@ -158,7 +168,7 @@ public async Task ConnectAsync()
158168
if (_useSslStream)
159169
{
160170
_sslStream = new SslStream(new NetworkStream(_socket));
161-
await _sslStream.AuthenticateAsClientAsync(((DnsEndPoint)_endpoint).Host);
171+
await _sslStream.AuthenticateAsClientAsync(_sslClientAuthOptions);
162172
}
163173
else
164174
{

src/Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Net;
5+
using System.Net.Security;
56
using System.Threading;
67
using Enyim.Caching.Configuration;
78
using Enyim.Collections;
@@ -25,8 +26,9 @@ public BinaryNode(
2526
ISaslAuthenticationProvider authenticationProvider,
2627
ILogger logger,
2728
bool useSslStream,
28-
bool useIPv6)
29-
: base(endpoint, config, logger, useSslStream, useIPv6)
29+
bool useIPv6,
30+
SslClientAuthenticationOptions sslClientAuthOptions)
31+
: base(endpoint, config, logger, useSslStream, useIPv6, sslClientAuthOptions)
3032
{
3133
_authenticationProvider = authenticationProvider;
3234
_logger = logger;

src/Enyim.Caching/Memcached/Protocol/Binary/BinaryPool.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public BinaryPool(IMemcachedClientConfiguration configuration, ILogger logger)
2929

3030
protected override IMemcachedNode CreateNode(EndPoint endpoint)
3131
{
32-
return new BinaryNode(endpoint, _configuration.SocketPool, _authenticationProvider, _logger, _configuration.UseSslStream, _configuration.UseIPv6);
32+
return new BinaryNode(endpoint, _configuration.SocketPool, _authenticationProvider, _logger, _configuration.UseSslStream, _configuration.UseIPv6, _configuration.SslClientAuth);
3333
}
3434

3535
private static ISaslAuthenticationProvider GetProvider(IMemcachedClientConfiguration configuration)

0 commit comments

Comments
 (0)