Skip to content

Commit

Permalink
SNOW-961434 Fix usage of insecure mode in connection parameters (#832)
Browse files Browse the repository at this point in the history
### Description
SNOW-961434 Fix usage of insecure mode in connection parameters

### Checklist
- [x] Code compiles correctly
- [x] Code is formatted according to [Coding
Conventions](../CodingConventions.md)
- [x] Created tests which fail without the change (if possible)
- [x] All tests passing (`dotnet test`)
- [x] Extended the README / documentation, if necessary
- [x] Provide JIRA issue id (if possible) or GitHub issue id in PR name
  • Loading branch information
sfc-gh-knozderko authored Dec 15, 2023
1 parent 3ccdab3 commit 49cb77d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ namespace Snowflake.Data.Tests.UnitTests.Session
public class SFHttpClientPropertiesTest
{
[Test]
public void ShouldConvertToMapOnly2Properties(
public void TestConvertToMapOnly2Properties(
[Values(true, false)] bool validateDefaultParameters,
[Values(true, false)] bool clientSessionKeepAlive)
{
// given
// arrange
var proxyProperties = new SFSessionHttpClientProxyProperties()
{
proxyHost = "localhost",
Expand All @@ -41,19 +41,52 @@ public void ShouldConvertToMapOnly2Properties(
proxyProperties = proxyProperties
};

// when
// act
var parameterMap = properties.ToParameterMap();

// then
// assert
Assert.AreEqual(2, parameterMap.Count);
Assert.AreEqual(validateDefaultParameters, parameterMap[SFSessionParameter.CLIENT_VALIDATE_DEFAULT_PARAMETERS]);
Assert.AreEqual(clientSessionKeepAlive, parameterMap[SFSessionParameter.CLIENT_SESSION_KEEP_ALIVE]);
}

[Test]
public void ShouldBuildHttpClientConfig()
public void TestBuildHttpClientConfig()
{
// arrange
var properties = RandomSFSessionHttpClientProperties();

// act
var config = properties.BuildHttpClientConfig();

// assert
Assert.AreEqual(!properties.insecureMode, config.CrlCheckEnabled);
Assert.AreEqual(properties.proxyProperties.proxyHost, config.ProxyHost);
Assert.AreEqual(properties.proxyProperties.proxyPort, config.ProxyPort);
Assert.AreEqual(properties.proxyProperties.proxyUser, config.ProxyUser);
Assert.AreEqual(properties.proxyProperties.proxyPassword, config.ProxyPassword);
Assert.AreEqual(properties.proxyProperties.nonProxyHosts, config.NoProxyList);
Assert.AreEqual(properties.disableRetry, config.DisableRetry);
Assert.AreEqual(properties.forceRetryOn404, config.ForceRetryOn404);
Assert.AreEqual(properties.maxHttpRetries, config.MaxHttpRetries);
}

[Test]
public void TestCrlCheckEnabledToBeOppositeInsecureMode([Values] bool insecureMode)
{
// arrange
var properties = RandomSFSessionHttpClientProperties();
properties.insecureMode = insecureMode;

// act
var config = properties.BuildHttpClientConfig();

// assert
Assert.AreEqual(!insecureMode, config.CrlCheckEnabled);
}

private SFSessionHttpClientProperties RandomSFSessionHttpClientProperties()
{
// given
var proxyProperties = new SFSessionHttpClientProxyProperties()
{
proxyHost = TestDataGenarator.NextAlphaNumeric(),
Expand All @@ -62,7 +95,7 @@ public void ShouldBuildHttpClientConfig()
proxyPassword = TestDataGenarator.NextAlphaNumeric(),
proxyUser = TestDataGenarator.NextAlphaNumeric()
};
var properties = new SFSessionHttpClientProperties()
return new SFSessionHttpClientProperties()
{
validateDefaultParameters = TestDataGenarator.NextBool(),
clientSessionKeepAlive = TestDataGenarator.NextBool(),
Expand All @@ -74,26 +107,12 @@ public void ShouldBuildHttpClientConfig()
maxHttpRetries = TestDataGenarator.NextInt(0, 15),
proxyProperties = proxyProperties
};

// when
var config = properties.BuildHttpClientConfig();

// then
Assert.AreEqual(properties.insecureMode, config.CrlCheckEnabled);
Assert.AreEqual(properties.proxyProperties.proxyHost, config.ProxyHost);
Assert.AreEqual(properties.proxyProperties.proxyPort, config.ProxyPort);
Assert.AreEqual(properties.proxyProperties.proxyUser, config.ProxyUser);
Assert.AreEqual(properties.proxyProperties.proxyPassword, config.ProxyPassword);
Assert.AreEqual(properties.proxyProperties.nonProxyHosts, config.NoProxyList);
Assert.AreEqual(properties.disableRetry, config.DisableRetry);
Assert.AreEqual(properties.forceRetryOn404, config.ForceRetryOn404);
Assert.AreEqual(properties.maxHttpRetries, config.MaxHttpRetries);
}

[Test, TestCaseSource(nameof(PropertiesProvider))]
public void ShouldExtractProperties(PropertiesTestCase testCase)
public void TestExtractProperties(PropertiesTestCase testCase)
{
// given
// arrange
var proxyExtractorMock = new Moq.Mock<SFSessionHttpClientProxyProperties.IExtractor>();
var extractor = new SFSessionHttpClientProperties.Extractor(proxyExtractorMock.Object);
var properties = SFSessionProperties.parseConnectionString(testCase.conectionString, null);
Expand All @@ -102,11 +121,11 @@ public void ShouldExtractProperties(PropertiesTestCase testCase)
.Setup(e => e.ExtractProperties(properties))
.Returns(proxyProperties);

// when
// act
var extractedProperties = extractor.ExtractProperties(properties);
extractedProperties.CheckPropertiesAreValid();

// then
// assert
Assert.AreEqual(testCase.expectedProperties.validateDefaultParameters, extractedProperties.validateDefaultParameters);
Assert.AreEqual(testCase.expectedProperties.clientSessionKeepAlive, extractedProperties.clientSessionKeepAlive);
Assert.AreEqual(testCase.expectedProperties.timeoutInSec, extractedProperties.timeoutInSec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal TimeSpan TimeoutDuration()
internal HttpClientConfig BuildHttpClientConfig()
{
return new HttpClientConfig(
insecureMode,
!insecureMode,
proxyProperties.proxyHost,
proxyProperties.proxyPort,
proxyProperties.proxyUser,
Expand Down

0 comments on commit 49cb77d

Please sign in to comment.