Skip to content

Commit 95186f3

Browse files
authored
revert(http-client-wrapper): implement skipping SSL cert verification
This reverts commit 4cf2a85.
1 parent 4cf2a85 commit 95186f3

File tree

6 files changed

+6
-136
lines changed

6 files changed

+6
-136
lines changed

.editorconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,6 @@ dotnet_diagnostic.CA1310.severity = none
179179
# JSON002: Probable JSON string detected
180180
dotnet_diagnostic.JSON002.severity = none
181181

182-
# S4830: Server certificates should be verified during SSL/TLS connection
183-
dotnet_diagnostic.S4830.severity = none
184-
185182
[*.vb]
186183
###############################
187184
# VB Coding Conventions #

APIMatic.Core.Test/Http/CoreHttpClientConfigurationTest.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public void Builder_BuildWithPrameters_CoreHttpClientConfiguration()
2222
{
2323
// Arrange
2424
var timeout = 180;
25-
var skipSslCertVerification = true;
2625
var numberOfRetries = 3;
2726
var backoffFactor = 3;
2827
var retryInterval = 4.5;
@@ -33,7 +32,6 @@ public void Builder_BuildWithPrameters_CoreHttpClientConfiguration()
3332
// Act
3433
var config = _config.ToBuilder()
3534
.Timeout(TimeSpan.FromSeconds(timeout))
36-
.SkipSslCertVerification(skipSslCertVerification)
3735
.NumberOfRetries(numberOfRetries)
3836
.BackoffFactor(backoffFactor)
3937
.RetryInterval(retryInterval)
@@ -45,7 +43,6 @@ public void Builder_BuildWithPrameters_CoreHttpClientConfiguration()
4543
// Assert
4644
Assert.NotNull(config);
4745
Assert.AreEqual(config.Timeout, TimeSpan.FromSeconds(timeout));
48-
Assert.AreEqual(config.SkipSslCertVerification, skipSslCertVerification);
4946
Assert.AreEqual(config.NumberOfRetries, numberOfRetries);
5047
Assert.AreEqual(config.BackoffFactor, backoffFactor);
5148
Assert.AreEqual(config.RetryInterval, retryInterval);
@@ -63,12 +60,10 @@ public void Builder_BuildWithInvalidPrameters_CoreHttpClientConfiguration()
6360
var backoffFactor = 0;
6461
var retryInterval = -1;
6562
var maximumRetryWaitTime = -1;
66-
var skipSslCertVerification = false;
6763

6864
var config = _config.ToBuilder()
6965
.HttpClientInstance(null)
7066
.Timeout(TimeSpan.FromSeconds(timeout))
71-
.SkipSslCertVerification(skipSslCertVerification)
7267
.NumberOfRetries(numberOfRetries)
7368
.BackoffFactor(backoffFactor)
7469
.RetryInterval(retryInterval)
@@ -88,7 +83,6 @@ public void Builder_BuildWithInvalidPrameters_CoreHttpClientConfiguration()
8883
Assert.NotNull(config);
8984
Assert.NotNull(config.HttpClientInstance);
9085
Assert.AreEqual(config.Timeout, TimeSpan.FromSeconds(defaultTimeout));
91-
Assert.AreEqual(config.SkipSslCertVerification, skipSslCertVerification);
9286
Assert.AreEqual(config.NumberOfRetries, defaultNumberOfRetries);
9387
Assert.AreEqual(config.BackoffFactor, defaultBackoffFactor);
9488
Assert.AreEqual(config.RetryInterval, defaultRetryInterval);
@@ -104,7 +98,7 @@ public void ToString_Default_CoreHttpClientConfiguration()
10498
var actual = _config.ToString();
10599

106100
// Assert
107-
var expected = "HttpClientConfiguration: 00:01:40 , False , 0 , 2 , 1 , 00:02:00 , System.Collections.Immutable.ImmutableList`1[System.Int32] , System.Collections.Immutable.ImmutableList`1[System.Net.Http.HttpMethod] , System.Net.Http.HttpClient , True ";
101+
var expected = "HttpClientConfiguration: 00:01:40 , 0 , 2 , 1 , 00:02:00 , System.Collections.Immutable.ImmutableList`1[System.Int32] , System.Collections.Immutable.ImmutableList`1[System.Net.Http.HttpMethod] , System.Net.Http.HttpClient , True ";
108102
Assert.AreEqual(expected, actual);
109103
}
110104
}

APIMatic.Core.Test/Http/HttpClientWrapperSSLTest.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

APIMatic.Core/Http/Configuration/CoreHttpClientConfiguration.cs

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class CoreHttpClientConfiguration : ICoreHttpClientConfiguration
2020
/// </summary>
2121
private CoreHttpClientConfiguration(
2222
TimeSpan timeout,
23-
bool skipSslCertVerification,
2423
int numberOfRetries,
2524
int backoffFactor,
2625
double retryInterval,
@@ -31,7 +30,6 @@ private CoreHttpClientConfiguration(
3130
bool overrideHttpClientConfiguration)
3231
{
3332
Timeout = timeout;
34-
SkipSslCertVerification = skipSslCertVerification;
3533
NumberOfRetries = numberOfRetries;
3634
BackoffFactor = backoffFactor;
3735
RetryInterval = retryInterval;
@@ -47,11 +45,6 @@ private CoreHttpClientConfiguration(
4745
/// </summary>
4846
public TimeSpan Timeout { get; }
4947

50-
/// <summary>
51-
/// Gets Whether to skip verification of SSL certificates.
52-
/// </summary>
53-
public bool SkipSslCertVerification { get; }
54-
5548
/// <summary>
5649
/// Gets Number of times the request is retried.
5750
/// </summary>
@@ -97,7 +90,6 @@ public override string ToString()
9790
{
9891
return "HttpClientConfiguration: " +
9992
$"{Timeout} , " +
100-
$"{SkipSslCertVerification} , " +
10193
$"{NumberOfRetries} , " +
10294
$"{BackoffFactor} , " +
10395
$"{RetryInterval} , " +
@@ -116,7 +108,6 @@ public Builder ToBuilder()
116108
{
117109
var builder = new Builder()
118110
.Timeout(Timeout)
119-
.SkipSslCertVerification(SkipSslCertVerification)
120111
.NumberOfRetries(NumberOfRetries)
121112
.BackoffFactor(BackoffFactor)
122113
.RetryInterval(RetryInterval)
@@ -134,7 +125,6 @@ public Builder ToBuilder()
134125
public class Builder
135126
{
136127
private TimeSpan timeout = TimeSpan.FromSeconds(100);
137-
private bool skipSslCertVerification = false;
138128
private int numberOfRetries = 0;
139129
private int backoffFactor = 2;
140130
private double retryInterval = 1;
@@ -147,7 +137,7 @@ public class Builder
147137
{
148138
"GET", "PUT"
149139
}.Select(val => new HttpMethod(val)).ToImmutableList();
150-
private HttpClient httpClientInstance = null;
140+
private HttpClient httpClientInstance = new HttpClient();
151141
private bool overrideHttpClientConfiguration = true;
152142

153143
/// <summary>
@@ -161,17 +151,6 @@ public Builder Timeout(TimeSpan timeout)
161151
return this;
162152
}
163153

164-
/// <summary>
165-
/// Sets the SkipSslCertVerification.
166-
/// </summary>
167-
/// <param name="skipSslCertVerification">Bool for skipping (or not) SSL certificate verification</param>
168-
/// <returns>Builder.</returns>
169-
public Builder SkipSslCertVerification(bool skipSslCertVerification)
170-
{
171-
this.skipSslCertVerification = skipSslCertVerification;
172-
return this;
173-
}
174-
175154
/// <summary>
176155
/// Sets the NumberOfRetries.
177156
/// </summary>
@@ -246,7 +225,7 @@ public Builder RequestMethodsToRetry(IList<HttpMethod> requestMethodsToRetry)
246225
/// <returns>Builder.</returns>
247226
public Builder HttpClientInstance(HttpClient httpClientInstance, bool overrideHttpClientConfiguration = true)
248227
{
249-
this.httpClientInstance = httpClientInstance;
228+
this.httpClientInstance = httpClientInstance ?? new HttpClient();
250229
this.overrideHttpClientConfiguration = overrideHttpClientConfiguration;
251230
return this;
252231
}
@@ -259,41 +238,15 @@ public CoreHttpClientConfiguration Build()
259238
{
260239
return new CoreHttpClientConfiguration(
261240
timeout,
262-
skipSslCertVerification,
263241
numberOfRetries,
264242
backoffFactor,
265243
retryInterval,
266244
maximumRetryWaitTime,
267245
statusCodesToRetry,
268246
requestMethodsToRetry,
269-
GetInitializedHttpClientInstance(),
247+
httpClientInstance,
270248
overrideHttpClientConfiguration);
271249
}
272-
273-
private HttpClient GetInitializedHttpClientInstance()
274-
{
275-
if (overrideHttpClientConfiguration)
276-
{
277-
if (skipSslCertVerification)
278-
{
279-
var httpClientHandler = new HttpClientHandler
280-
{
281-
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }
282-
};
283-
return new HttpClient(httpClientHandler, disposeHandler: true)
284-
{
285-
Timeout = timeout,
286-
};
287-
}
288-
289-
var httpClient = httpClientInstance ?? new HttpClient();
290-
httpClient.Timeout = timeout;
291-
292-
return httpClient;
293-
}
294-
295-
return httpClientInstance ?? new HttpClient();
296-
}
297250
}
298251
}
299252
}

APIMatic.Core/Http/Configuration/ICoreHttpClientConfiguration.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ public interface ICoreHttpClientConfiguration
1717
/// </summary>
1818
TimeSpan Timeout { get; }
1919

20-
/// <summary>
21-
/// Gets Whether to skip verification of SSL certificates.
22-
/// </summary>
23-
bool SkipSslCertVerification { get; }
24-
2520
/// <summary>
2621
/// Number of times the request is retried.
2722
/// </summary>

APIMatic.Core/Http/HttpClientWrapper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public HttpClientWrapper(ICoreHttpClientConfiguration httpClientConfig)
5858
_backoffFactor = httpClientConfig.BackoffFactor;
5959
_retryInterval = httpClientConfig.RetryInterval;
6060
_maximumRetryWaitTime = httpClientConfig.MaximumRetryWaitTime;
61+
_client.Timeout = httpClientConfig.Timeout;
6162
}
6263
}
6364

@@ -291,6 +292,7 @@ private double GetExponentialWaitTime(int retryAttempt)
291292
{
292293
double noise = new Random().NextDouble() * 100;
293294
return (1000 * _retryInterval * Math.Pow(_backoffFactor, retryAttempt - 1)) + noise;
295+
294296
}
295297

296298
private static Dictionary<string, string> GetCombinedResponseHeaders(HttpResponseMessage responseMessage)

0 commit comments

Comments
 (0)