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

SNOW-1640968 dispose disposable objects #1014

Merged
merged 7 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 6 additions & 5 deletions Snowflake.Data.Tests/UnitTests/ConcatenatedStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.
*/

using Snowflake.Data.Tests.Util;
using NUnit.Framework;
using System;
using System.IO;
using System.Text;

namespace Snowflake.Data.Tests.UnitTests
{
using NUnit.Framework;
using Snowflake.Data.Core;
using System;
using System.IO;
using System.Text;

[TestFixture]
class ConcatenatedStreamTest
Expand Down
4 changes: 2 additions & 2 deletions Snowflake.Data.Tests/UnitTests/SFRemoteStorageClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,14 +527,14 @@ private void SetUpMockEncryptedFileForDownload()

// Get encrypted stream from file
SFEncryptionMetadata encryptionMetadata = new SFEncryptionMetadata();
Stream stream = EncryptionProvider.EncryptFile(
StreamPair streamPair = EncryptionProvider.EncryptFile(
t_downloadFileName,
_fileMetadata.encryptionMaterial,
encryptionMetadata,
FileTransferConfiguration.FromFileMetadata(_fileMetadata));

// Set up the stream and metadata for decryption
MockRemoteStorageClient.SetEncryptionData(stream, encryptionMetadata.iv, encryptionMetadata.key);
MockRemoteStorageClient.SetEncryptionData(streamPair.MainStream, encryptionMetadata.iv, encryptionMetadata.key);
}

[Test]
Expand Down
39 changes: 39 additions & 0 deletions Snowflake.Data.Tests/UnitTests/StreamPairTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.IO;
using NUnit.Framework;
using Moq;
using Snowflake.Data.Core.FileTransfer;

namespace Snowflake.Data.Tests.UnitTests
{
[TestFixture]
public class StreamPairTest
{
[Test]
public void TestCloseBothStreams()
{
// arrange
var mockedMainStream = new Mock<Stream>();
var mockedHelperStream = new Mock<Stream>();

// act
using (new StreamPair { MainStream = mockedMainStream.Object, HelperStream = mockedHelperStream.Object }) {}

// assert
mockedMainStream.Verify(stream => stream.Close());
mockedHelperStream.Verify(stream => stream.Close());
}

[Test]
public void TestCloseMainStreamOnlyWhenHelperStreamNotGiven()
{
// arrange
var mockedMainStream = new Mock<Stream>();

// act
using (new StreamPair { MainStream = mockedMainStream.Object }) {}

// assert
mockedMainStream.Verify(stream => stream.Close());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2012-2019 Snowflake Computing Inc. All rights reserved.
* Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
*/

using System;
using System.Collections.Generic;
using System.IO;

namespace Snowflake.Data.Core
namespace Snowflake.Data.Tests.Util
{
/// <summary>
/// Used to concat multiple streams without copying. Since we need to preappend '[' and append ']'
Expand Down
64 changes: 35 additions & 29 deletions Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,36 +100,29 @@
{
try
{
PemReader pr = null;
if (null != pkPwd)
using (PemReader pr = CreatePemReader(tr, pkPwd))

Check warning on line 103 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L103

Added line #L103 was not covered by tests
{
IPasswordFinder ipwdf = new PasswordFinder(pkPwd);
pr = new PemReader(tr, ipwdf);
}
else
{
pr = new PemReader(tr);
}

object key = pr.ReadObject();
// Infer what the pem reader is sending back based on the object properties
if (key.GetType().GetProperty("Private") != null)
{
// PKCS1 key
keypair = (AsymmetricCipherKeyPair)key;
rsaParams = DotNetUtilities.ToRSAParameters(
keypair.Private as RsaPrivateCrtKeyParameters);
}
else
{
// PKCS8 key
RsaPrivateCrtKeyParameters pk = (RsaPrivateCrtKeyParameters)key;
rsaParams = DotNetUtilities.ToRSAParameters(pk);
keypair = DotNetUtilities.GetRsaKeyPair(rsaParams);
}
if (keypair == null)
{
throw new Exception("Unknown error.");
object key = pr.ReadObject();

Check warning on line 105 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L105

Added line #L105 was not covered by tests
// Infer what the pem reader is sending back based on the object properties
if (key.GetType().GetProperty("Private") != null)
{

Check warning on line 108 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L108

Added line #L108 was not covered by tests
// PKCS1 key
keypair = (AsymmetricCipherKeyPair)key;
rsaParams = DotNetUtilities.ToRSAParameters(
keypair.Private as RsaPrivateCrtKeyParameters);
}

Check warning on line 113 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L110-L113

Added lines #L110 - L113 were not covered by tests
else
{

Check warning on line 115 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L115

Added line #L115 was not covered by tests
// PKCS8 key
RsaPrivateCrtKeyParameters pk = (RsaPrivateCrtKeyParameters)key;
rsaParams = DotNetUtilities.ToRSAParameters(pk);
keypair = DotNetUtilities.GetRsaKeyPair(rsaParams);
}

Check warning on line 120 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L117-L120

Added lines #L117 - L120 were not covered by tests

if (keypair == null)
{
throw new Exception("Unknown error.");

Check warning on line 124 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L123-L124

Added lines #L123 - L124 were not covered by tests
}
}
}
catch (Exception e)
Expand Down Expand Up @@ -207,6 +200,19 @@
return jwtToken;
}

private PemReader CreatePemReader(TextReader textReader, string privateKeyPassword)
{

Check warning on line 204 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L204

Added line #L204 was not covered by tests
if (null != privateKeyPassword)
{
IPasswordFinder ipwdf = new PasswordFinder(privateKeyPassword);
return new PemReader(textReader, ipwdf);

Check warning on line 208 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L206-L208

Added lines #L206 - L208 were not covered by tests
}
else
{
return new PemReader(textReader);

Check warning on line 212 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L211-L212

Added lines #L211 - L212 were not covered by tests
}
}

Check warning on line 214 in Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Authenticator/KeyPairAuthenticator.cs#L214

Added line #L214 was not covered by tests

/// <summary>
/// Helper class to handle the password for the certificate if there is one.
/// </summary>
Expand Down
Loading
Loading