Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CosmosClientOptions: Adds ServerCertificateCustomValidationCallback for Http and TCP #3636

Merged
merged 31 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
405eb85
Adding ServerCertificateCustomValidationCallback in clientoptions
aavasthy Jan 6, 2023
c543e57
Adding Server callback for Http and fixing tests
aavasthy Jan 9, 2023
f713c8f
Merge branch 'master' into users/aavasthy/parameterizesslissue
aavasthy Jan 9, 2023
ee5b07e
Fixing failing E2Etests
aavasthy Jan 9, 2023
3e848ab
Merge branch 'users/aavasthy/parameterizesslissue' of https://github.…
aavasthy Jan 9, 2023
d051b35
Merge branch 'master' into users/aavasthy/parameterizesslissue
aavasthy Jan 9, 2023
a8c05ce
Merge branch 'master' into users/aavasthy/parameterizesslissue
aavasthy Jan 9, 2023
b41022a
Resolving merge conflicts
kundadebdatta Jan 11, 2023
e4ba6a5
Running update contracts script
aavasthy Jan 12, 2023
43a5e1e
Merge branch 'master' into users/aavasthy/parameterizesslissue
aavasthy Jan 12, 2023
16283ca
Running Update contracts script
aavasthy Jan 12, 2023
f9d5b5e
Running Update contracts script
aavasthy Jan 12, 2023
a41da65
Reverting the v3 version change
aavasthy Jan 12, 2023
15af702
Update based on review comments
aavasthy Jan 17, 2023
55af901
Added unit tests
aavasthy Jan 17, 2023
315a821
Merge branch 'master' into users/aavasthy/parameterizesslissue
aavasthy Jan 17, 2023
ba2e0c5
Added remarks for callback delegate
aavasthy Jan 17, 2023
ad751ca
Merge branch 'users/aavasthy/parameterizesslissue' of https://github.…
aavasthy Jan 17, 2023
d76228e
Ran update contracts script
aavasthy Jan 17, 2023
4ffbec4
Update based on review comments
aavasthy Jan 18, 2023
7e18109
Merge branch 'master' into users/aavasthy/parameterizesslissue
aavasthy Jan 18, 2023
637d843
Ran update contracts script
aavasthy Jan 18, 2023
095b822
Updated unit tests
aavasthy Jan 18, 2023
9172209
Making ssl validation function private
aavasthy Jan 20, 2023
dfb71e8
Merge branch 'master' into users/aavasthy/parameterizesslissue
aavasthy Jan 20, 2023
03a8fbb
Updating test files
aavasthy Jan 22, 2023
6d5e49d
Merge branch 'users/aavasthy/parameterizesslissue' of https://github.…
aavasthy Jan 22, 2023
e19c5b0
Merge branch 'master' into users/aavasthy/parameterizesslissue
aavasthy Jan 23, 2023
78d6410
Update remarks for sslvalidation public contract
aavasthy Jan 23, 2023
eb0ba54
Merge branch 'users/aavasthy/parameterizesslissue' of https://github.…
aavasthy Jan 23, 2023
5abc195
Added emulator tests for server validation
aavasthy Jan 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Microsoft.Azure.Cosmos/src/ConnectionPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Microsoft.Azure.Cosmos
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.Cosmos.Telemetry;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
Expand Down Expand Up @@ -46,8 +48,8 @@ public ConnectionPolicy()
this.MaxConnectionLimit = defaultMaxConcurrentConnectionLimit;
this.RetryOptions = new RetryOptions();
this.EnableReadRequestsFallback = null;

this.EnableClientTelemetry = ClientTelemetryOptions.IsClientTelemetryEnabled();
this.ServerCertificateCustomValidationCallback = null;
}

/// <summary>
Expand Down Expand Up @@ -294,6 +296,11 @@ public bool EnablePartitionLevelFailover
get;
set;
}
public Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, Boolean> ServerCertificateCustomValidationCallback
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
{
get;
set;
}

/// <summary>
/// Gets or sets the flag to enable writes on any locations (regions) for geo-replicated database accounts in the Azure Cosmos DB service.
Expand Down
20 changes: 19 additions & 1 deletion Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Microsoft.Azure.Cosmos
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
Expand Down Expand Up @@ -633,6 +635,21 @@ internal Protocol ConnectionProtocol
/// </summary>
internal Func<TransportClient, TransportClient> TransportClientHandlerFactory { get; set; }

/// <summary>
/// Certificate validation function
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> ServerCertificateCustomValidationCallback { get; set; }
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
internal RemoteCertificateValidationCallback SslCustomValidationCallBack
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
{
get
{
if (this.ServerCertificateCustomValidationCallback == null)
{
return null;
}
return (obj, cert, chain, policy) => this.ServerCertificateCustomValidationCallback((HttpRequestMessage)obj, (X509Certificate2)cert, chain, policy);
}
}
/// <summary>
/// API type for the account
/// </summary>
Expand Down Expand Up @@ -746,7 +763,8 @@ internal virtual ConnectionPolicy GetConnectionPolicy(int clientId)
EnablePartitionLevelFailover = this.EnablePartitionLevelFailover,
PortReuseMode = this.portReuseMode,
EnableTcpConnectionEndpointRediscovery = this.EnableTcpConnectionEndpointRediscovery,
HttpClientFactory = this.httpClientFactory
HttpClientFactory = this.httpClientFactory,
ServerCertificateCustomValidationCallback = this.ServerCertificateCustomValidationCallback
};

if (this.EnableClientTelemetry.HasValue)
Expand Down
18 changes: 14 additions & 4 deletions Microsoft.Azure.Cosmos/src/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.Azure.Cosmos
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -163,6 +164,9 @@ internal partial class DocumentClient : IDisposable, IAuthorizationTokenProvider
//Trace Id.
private int traceId;

//RemoteCertificateValidationCallback
internal RemoteCertificateValidationCallback remoteCertificateValidationCallback;

//SessionContainer.
internal ISessionContainer sessionContainer;

Expand Down Expand Up @@ -421,6 +425,7 @@ internal DocumentClient(Uri serviceEndpoint,
/// <param name="storeClientFactory">Factory that creates store clients sharing the same transport client to optimize network resource reuse across multiple document clients in the same process.</param>
/// <param name="isLocalQuorumConsistency">Flag to allow Quorum Read with Eventual Consistency Account</param>
/// <param name="cosmosClientId"></param>
/// <param name="remoteCertificateValidationCallback"></param>
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
/// <remarks>
/// The service endpoint can be obtained from the Azure Management Portal.
/// If you are connecting using one of the Master Keys, these can be obtained along with the endpoint from the Azure Management Portal
Expand All @@ -446,7 +451,8 @@ internal DocumentClient(Uri serviceEndpoint,
Func<TransportClient, TransportClient> transportClientHandlerFactory = null,
IStoreClientFactory storeClientFactory = null,
bool isLocalQuorumConsistency = false,
string cosmosClientId = null)
string cosmosClientId = null,
RemoteCertificateValidationCallback remoteCertificateValidationCallback = null)
{
if (sendingRequestEventArgs != null)
{
Expand Down Expand Up @@ -478,7 +484,8 @@ internal DocumentClient(Uri serviceEndpoint,
sessionContainer: sessionContainer,
enableCpuMonitor: enableCpuMonitor,
storeClientFactory: storeClientFactory,
cosmosClientId: cosmosClientId);
cosmosClientId: cosmosClientId,
remoteCertificateValidationCallback: remoteCertificateValidationCallback);
}

/// <summary>
Expand Down Expand Up @@ -660,14 +667,16 @@ internal virtual void Initialize(Uri serviceEndpoint,
bool? enableCpuMonitor = null,
IStoreClientFactory storeClientFactory = null,
TokenCredential tokenCredential = null,
string cosmosClientId = null)
string cosmosClientId = null,
RemoteCertificateValidationCallback remoteCertificateValidationCallback = null)
{
if (serviceEndpoint == null)
{
throw new ArgumentNullException("serviceEndpoint");
}

this.clientId = cosmosClientId;
this.remoteCertificateValidationCallback = remoteCertificateValidationCallback;

this.queryPartitionProvider = new AsyncLazy<QueryPartitionProvider>(async () =>
{
Expand Down Expand Up @@ -6648,7 +6657,8 @@ private void InitializeDirectConnectivity(IStoreClientFactory storeClientFactory
retryWithConfiguration: this.ConnectionPolicy.RetryOptions?.GetRetryWithConfiguration(),
enableTcpConnectionEndpointRediscovery: this.ConnectionPolicy.EnableTcpConnectionEndpointRediscovery,
addressResolver: this.AddressResolver,
rntbdMaxConcurrentOpeningConnectionCount: this.rntbdMaxConcurrentOpeningConnectionCount);
rntbdMaxConcurrentOpeningConnectionCount: this.rntbdMaxConcurrentOpeningConnectionCount,
remoteCertificateValidationCallback: this.remoteCertificateValidationCallback );

if (this.transportClientHandlerFactory != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Microsoft.Azure.Cosmos
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Resource.CosmosExceptions;
Expand Down Expand Up @@ -73,7 +75,8 @@ public static CosmosHttpClient CreateWithConnectionPolicy(
{
httpMessageHandler = CosmosHttpClientCore.CreateHttpClientHandler(
gatewayModeMaxConnectionLimit: connectionPolicy.MaxConnectionLimit,
webProxy: null);
webProxy: null,
serverCertificateCustomValidationCallback: connectionPolicy.ServerCertificateCustomValidationCallback);
}

if (sendingRequestEventArgs != null ||
Expand All @@ -96,7 +99,7 @@ public static CosmosHttpClient CreateWithConnectionPolicy(
eventSource: eventSource);
}

public static HttpMessageHandler CreateHttpClientHandler(int gatewayModeMaxConnectionLimit, IWebProxy webProxy)
public static HttpMessageHandler CreateHttpClientHandler(int gatewayModeMaxConnectionLimit, IWebProxy webProxy, Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> serverCertificateCustomValidationCallback)
{
HttpClientHandler httpClientHandler = new HttpClientHandler();

Expand All @@ -110,6 +113,7 @@ public static HttpMessageHandler CreateHttpClientHandler(int gatewayModeMaxConne
try
{
httpClientHandler.MaxConnectionsPerServer = gatewayModeMaxConnectionLimit;
httpClientHandler.ServerCertificateCustomValidationCallback = serverCertificateCustomValidationCallback;
}
// MaxConnectionsPerServer is not supported on some platforms.
catch (PlatformNotSupportedException)
Expand Down
6 changes: 4 additions & 2 deletions Microsoft.Azure.Cosmos/src/Resource/ClientContextCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ internal static CosmosClientContext Create(
clientOptions = ClientContextCore.CreateOrCloneClientOptions(clientOptions);
HttpMessageHandler httpMessageHandler = CosmosHttpClientCore.CreateHttpClientHandler(
clientOptions.GatewayModeMaxConnectionLimit,
clientOptions.WebProxy);
clientOptions.WebProxy,
clientOptions.ServerCertificateCustomValidationCallback);

DocumentClient documentClient = new DocumentClient(
cosmosClient.Endpoint,
Expand All @@ -79,7 +80,8 @@ internal static CosmosClientContext Create(
desiredConsistencyLevel: clientOptions.GetDocumentsConsistencyLevel(),
handler: httpMessageHandler,
sessionContainer: clientOptions.SessionContainer,
cosmosClientId: cosmosClient.Id);
cosmosClientId: cosmosClient.Id,
remoteCertificateValidationCallback: clientOptions.SslCustomValidationCallBack);

return ClientContextCore.Create(
cosmosClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,6 @@
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.ChangeFeedMode AllVersionsAndDeletes;CanRead:True;CanWrite:False;Microsoft.Azure.Cosmos.ChangeFeedMode get_AllVersionsAndDeletes();IsAbstract:False;IsStatic:True;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Microsoft.Azure.Cosmos.ChangeFeedMode LatestVersion": {
"Type": "Property",
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.ChangeFeedMode LatestVersion;CanRead:True;CanWrite:False;Microsoft.Azure.Cosmos.ChangeFeedMode get_LatestVersion();IsAbstract:False;IsStatic:True;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Microsoft.Azure.Cosmos.ChangeFeedMode get_AllVersionsAndDeletes()": {
"Type": "Method",
"Attributes": [],
Expand All @@ -179,6 +174,11 @@
"Type": "Method",
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.ChangeFeedMode get_LatestVersion();IsAbstract:False;IsStatic:True;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
aavasthy marked this conversation as resolved.
Show resolved Hide resolved
"Microsoft.Azure.Cosmos.ChangeFeedMode LatestVersion": {
"Type": "Property",
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.ChangeFeedMode LatestVersion;CanRead:True;CanWrite:False;Microsoft.Azure.Cosmos.ChangeFeedMode get_LatestVersion();IsAbstract:False;IsStatic:True;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
}
},
"NestedTypes": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,18 @@
],
"MethodInfo": "System.Func`1[System.Net.Http.HttpClient] HttpClientFactory;CanRead:True;CanWrite:True;System.Func`1[System.Net.Http.HttpClient] get_HttpClientFactory();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;Void set_HttpClientFactory(System.Func`1[System.Net.Http.HttpClient]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Func`5[System.Net.Http.HttpRequestMessage,System.Security.Cryptography.X509Certificates.X509Certificate2,System.Security.Cryptography.X509Certificates.X509Chain,System.Net.Security.SslPolicyErrors,System.Boolean] get_ServerCertificateCustomValidationCallback()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "System.Func`5[System.Net.Http.HttpRequestMessage,System.Security.Cryptography.X509Certificates.X509Certificate2,System.Security.Cryptography.X509Certificates.X509Chain,System.Net.Security.SslPolicyErrors,System.Boolean] get_ServerCertificateCustomValidationCallback();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Func`5[System.Net.Http.HttpRequestMessage,System.Security.Cryptography.X509Certificates.X509Certificate2,System.Security.Cryptography.X509Certificates.X509Chain,System.Net.Security.SslPolicyErrors,System.Boolean] ServerCertificateCustomValidationCallback": {
"Type": "Property",
"Attributes": [],
"MethodInfo": "System.Func`5[System.Net.Http.HttpRequestMessage,System.Security.Cryptography.X509Certificates.X509Certificate2,System.Security.Cryptography.X509Certificates.X509Chain,System.Net.Security.SslPolicyErrors,System.Boolean] ServerCertificateCustomValidationCallback;CanRead:True;CanWrite:True;System.Func`5[System.Net.Http.HttpRequestMessage,System.Security.Cryptography.X509Certificates.X509Certificate2,System.Security.Cryptography.X509Certificates.X509Chain,System.Net.Security.SslPolicyErrors,System.Boolean] get_ServerCertificateCustomValidationCallback();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;Void set_ServerCertificateCustomValidationCallback(System.Func`5[System.Net.Http.HttpRequestMessage,System.Security.Cryptography.X509Certificates.X509Certificate2,System.Security.Cryptography.X509Certificates.X509Chain,System.Net.Security.SslPolicyErrors,System.Boolean]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Net.IWebProxy get_WebProxy()": {
"Type": "Method",
"Attributes": [],
Expand Down Expand Up @@ -3050,6 +3062,13 @@
"Attributes": [],
"MethodInfo": "Void set_SerializerOptions(Microsoft.Azure.Cosmos.CosmosSerializationOptions);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Void set_ServerCertificateCustomValidationCallback(System.Func`5[System.Net.Http.HttpRequestMessage,System.Security.Cryptography.X509Certificates.X509Certificate2,System.Security.Cryptography.X509Certificates.X509Chain,System.Net.Security.SslPolicyErrors,System.Boolean])[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "Void set_ServerCertificateCustomValidationCallback(System.Func`5[System.Net.Http.HttpRequestMessage,System.Security.Cryptography.X509Certificates.X509Certificate2,System.Security.Cryptography.X509Certificates.X509Chain,System.Net.Security.SslPolicyErrors,System.Boolean]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Void set_TokenCredentialBackgroundRefreshInterval(System.Nullable`1[System.TimeSpan])[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
Expand Down