Skip to content

Commit

Permalink
fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sourabh1007 committed Jan 28, 2024
1 parent a152002 commit 5e94bc7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
20 changes: 15 additions & 5 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,11 @@ internal Protocol ConnectionProtocol
/// Flag that controls whether CPU monitoring thread is created to enrich timeout exceptions with additional diagnostic. Default value is true.
/// </summary>
internal bool? EnableCpuMonitor { get; set; }

/// <summary>
/// Flag indicates the value of DisableServerCertificateValidation flag set at connection string level.Default it is false.
/// </summary>
internal bool DisableServerCertificateValidation { get; set; }

/// <summary>
/// Gets or sets Client Telemetry Options like feature flags and corresponding options
Expand Down Expand Up @@ -758,7 +763,7 @@ internal virtual ConnectionPolicy GetConnectionPolicy(int clientId)
this.ValidateDirectTCPSettings();
this.ValidateLimitToEndpointSettings();
this.ValidatePartitionLevelFailoverSettings();
this.ValidateServerCallbackSettings();
this.ValidateAndSetServerCallbackSettings();

ConnectionPolicy connectionPolicy = new ConnectionPolicy()
{
Expand Down Expand Up @@ -867,7 +872,7 @@ internal static CosmosClientOptions GetCosmosClientOptionsWithCertificateFlag(st
clientOptions ??= new CosmosClientOptions();
if (CosmosClientOptions.IsConnectionStringDisableServerCertificateValidationFlag(connectionString))
{
clientOptions.ServerCertificateCustomValidationCallback = (_, _, _) => true;
clientOptions.DisableServerCertificateValidation = true;
}

return clientOptions;
Expand Down Expand Up @@ -932,11 +937,16 @@ private void ValidatePartitionLevelFailoverSettings()
}
}

private void ValidateServerCallbackSettings()
private void ValidateAndSetServerCallbackSettings()
{
if (this.HttpClientFactory != null && this.ServerCertificateCustomValidationCallback != null)
if (this.DisableServerCertificateValidation && this.ServerCertificateCustomValidationCallback != null)
{
throw new ArgumentException($"Cannot specify {nameof(this.HttpClientFactory)} and {nameof(this.ServerCertificateCustomValidationCallback)}. Only one can be set.");
throw new ArgumentException($"Cannot specify {nameof(this.DisableServerCertificateValidation)} flag in Connection String and {nameof(this.ServerCertificateCustomValidationCallback)}. Only one can be set.");
}

if (this.DisableServerCertificateValidation)
{
this.ServerCertificateCustomValidationCallback = (_, _, _) => true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ public void InvalidApplicationNameCatchTest()
[TestMethod]
[DataRow(ConnectionString, false)]
[DataRow(ConnectionString + "DisableServerCertificateValidation=true;", true)]
[DataRow(ConnectionString + "DisableServerCertificateValidation=false;", false)]
public void TestServerCertificatesValidationCallback(string connStr, bool expectedIgnoreCertificateFlag)
{
//Arrange
Expand All @@ -917,11 +918,11 @@ public void TestServerCertificatesValidationCallback(string connStr, bool expect
[TestMethod]
[DataRow(ConnectionString + "DisableServerCertificateValidation=true;")]
[ExpectedException(typeof(ArgumentException))]
public void TestServerCertificatesValidationWithHttpFactoryCallback(string connStr)
public void TestServerCertificatesValidationWithDisableSSLFlagTrue(string connStr)
{
CosmosClientOptions options = new CosmosClientOptions
{
HttpClientFactory = () => new HttpClient()
ServerCertificateCustomValidationCallback = (certificate, chain, sslPolicyErrors) => true
};
CosmosClient cosmosClient = new CosmosClient(connStr, options);
}
Expand Down

0 comments on commit 5e94bc7

Please sign in to comment.