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

BBS Integration tests #696

Merged
merged 31 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b360750
added bbs integration test
dylan-smith Oct 24, 2022
c68349c
fix arg name, add secrets to CI yml
dylan-smith Oct 24, 2022
f05bf52
change SshPort to int
dylan-smith Oct 24, 2022
6fa6bf8
remove unused variable
dylan-smith Oct 24, 2022
2b2a80a
fix tests
dylan-smith Oct 24, 2022
5d943f0
add extra test logging
dylan-smith Oct 25, 2022
77c2aa4
dotnet format
dylan-smith Oct 25, 2022
4a19d22
adding logging to powershell script run too
dylan-smith Oct 25, 2022
802301d
added troubleshooting to CI
dylan-smith Oct 25, 2022
f6846ea
changed dir
dylan-smith Oct 25, 2022
29dd7e0
turn off troubleshooting in CI
dylan-smith Oct 25, 2022
a66ab21
reverted process stdout capture
dylan-smith Oct 25, 2022
b41632f
accidentally deleted some test output
dylan-smith Oct 25, 2022
132e2b5
fix ssh key path
dylan-smith Oct 25, 2022
999612c
chmod on pem file
dylan-smith Oct 25, 2022
4c6e509
fixed problem with SSH key
dylan-smith Oct 27, 2022
fd68600
dotnet format
dylan-smith Oct 27, 2022
7140946
Merge branch 'main' into dylan-smith/bbs-integration-tests
dylan-smith Oct 27, 2022
f053ceb
update repo names
dylan-smith Oct 28, 2022
ac746ca
Merge branch 'dylan-smith/bbs-integration-tests' of https://github.co…
dylan-smith Oct 28, 2022
1f134b0
Merge branch 'main' into dylan-smith/bbs-integration-tests
dylan-smith Oct 28, 2022
8049589
fix compile error after merge
dylan-smith Oct 28, 2022
f44b867
trying to fix disposable problems
dylan-smith Oct 31, 2022
ab3375c
removed unnecessary field
dylan-smith Oct 31, 2022
dc6bf40
remove commented out code
dylan-smith Oct 31, 2022
4204c8d
cleaned up CI yml
dylan-smith Oct 31, 2022
10f26c0
simplified rsa 256 signature and doesnt break other key formats
dylan-smith Nov 1, 2022
fadfb72
Merge branch 'dylan-smith/bbs-integration-tests' of https://github.co…
dylan-smith Nov 1, 2022
fa58d87
fix null reference codeql warning
dylan-smith Nov 1, 2022
879d0d3
make field readonly
dylan-smith Nov 1, 2022
50942a4
fix typo
dylan-smith Nov 1, 2022
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
Prev Previous commit
Next Next commit
simplified rsa 256 signature and doesnt break other key formats
  • Loading branch information
dylan-smith committed Nov 1, 2022
commit 10f26c09ba112b9207a5c6fcf7665705828f007f
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Reflection;
using System.Security.Cryptography;
using Renci.SshNet;
using Renci.SshNet.Common;
using Renci.SshNet.Security;
using Renci.SshNet.Security.Cryptography;
Expand Down Expand Up @@ -76,26 +74,3 @@ protected virtual void Dispose(bool disposing)
}
}
}

public static class RsaSha256Util
{
public static RsaWithSha256SignatureKey ConvertToKeyWithSha256Signature(PrivateKeyFile keyFile)
{
return keyFile?.HostKey is not KeyHostAlgorithm oldKeyHostAlgorithm
? throw new ArgumentException("HostKey must be a KeyHostAlgorithm", nameof(keyFile))
: oldKeyHostAlgorithm.Key is not RsaKey oldRsaKey
? throw new ArgumentException("HostKey.Key must be a RsaKey", nameof(keyFile))
: new RsaWithSha256SignatureKey(oldRsaKey.Modulus, oldRsaKey.Exponent, oldRsaKey.D, oldRsaKey.P, oldRsaKey.Q, oldRsaKey.InverseQ);
}

public static void UpdatePrivateKeyFile(PrivateKeyFile keyFile, RsaWithSha256SignatureKey key)
{
var keyHostAlgorithm = new KeyHostAlgorithm(key?.ToString(), key);

var hostKeyProperty = typeof(PrivateKeyFile).GetProperty(nameof(PrivateKeyFile.HostKey));
hostKeyProperty.SetValue(keyFile, keyHostAlgorithm);

var keyField = typeof(PrivateKeyFile).GetField("_key", BindingFlags.NonPublic | BindingFlags.Instance);
keyField.SetValue(keyFile, key);
}
}
49 changes: 35 additions & 14 deletions src/bbs2gh/Services/BbsSshArchiveDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Renci.SshNet;
using Renci.SshNet.Security;
Expand All @@ -11,30 +12,50 @@ public sealed class BbsSshArchiveDownloader : IBbsArchiveDownloader, IDisposable
private const int DOWNLOAD_PROGRESS_REPORT_INTERVAL_IN_SECONDS = 10;

private readonly ISftpClient _sftpClient;
private readonly RsaKey _rsaKey = new RsaKey();
private readonly PrivateKeyFile _pkRsa;
private RsaKey _rsaKey;
private readonly PrivateKeyFile _privateKey;
private readonly PrivateKeyAuthenticationMethod _authenticationMethodRsa;
private readonly OctoLogger _log;
private readonly FileSystemProvider _fileSystemProvider;
private readonly object _mutex = new();
private DateTime _nextProgressReport;

#pragma warning disable CA2000 // Incorrectly flagged as a not-disposing error
public BbsSshArchiveDownloader(OctoLogger log, FileSystemProvider fileSystemProvider, string host, string sshUser, string privateKeyFileFullPath, int sshPort = 22)
{
_pkRsa = new PrivateKeyFile(privateKeyFileFullPath);
var newKey = RsaSha256Util.ConvertToKeyWithSha256Signature(_pkRsa);
RsaSha256Util.UpdatePrivateKeyFile(_pkRsa, newKey);
_authenticationMethodRsa = new PrivateKeyAuthenticationMethod(sshUser, _pkRsa);
var connection = new ConnectionInfo(host, sshPort, sshUser, _authenticationMethodRsa);
connection.HostKeyAlgorithms["rsa-sha2-256"] = data => new KeyHostAlgorithm("rsa-sha2-256", _rsaKey, data);

_sftpClient = new SftpClient(connection);

_log = log;
_fileSystemProvider = fileSystemProvider;

_privateKey = new PrivateKeyFile(privateKeyFileFullPath);

if (IsRsaKey(_privateKey))
{
UpdatePrivateKeyFileToRsaSha256(_privateKey);
_authenticationMethodRsa = new PrivateKeyAuthenticationMethod(sshUser, _privateKey);
var connection = new ConnectionInfo(host, sshPort, sshUser, _authenticationMethodRsa);
connection.HostKeyAlgorithms["rsa-sha2-256"] = data => new KeyHostAlgorithm("rsa-sha2-256", _rsaKey, data);
_sftpClient = new SftpClient(connection);
}
else
{
_sftpClient = new SftpClient(host, sshPort, sshUser, _privateKey);
}
}

private bool IsRsaKey(PrivateKeyFile privateKeyFile) => privateKeyFile.HostKey is KeyHostAlgorithm keyHostAlgorithm && keyHostAlgorithm.Key is RsaKey;

private void UpdatePrivateKeyFileToRsaSha256(PrivateKeyFile privateKeyFile)
{
var oldRsaKey = (privateKeyFile.HostKey as KeyHostAlgorithm).Key as RsaKey;
_rsaKey = new RsaWithSha256SignatureKey(oldRsaKey.Modulus, oldRsaKey.Exponent, oldRsaKey.D, oldRsaKey.P, oldRsaKey.Q, oldRsaKey.InverseQ);
Fixed Show fixed Hide fixed

var keyHostAlgorithm = new KeyHostAlgorithm(_rsaKey.ToString(), _rsaKey);

var hostKeyProperty = typeof(PrivateKeyFile).GetProperty(nameof(PrivateKeyFile.HostKey));
hostKeyProperty.SetValue(privateKeyFile, keyHostAlgorithm);

var keyField = typeof(PrivateKeyFile).GetField("_key", BindingFlags.NonPublic | BindingFlags.Instance);
keyField.SetValue(privateKeyFile, _rsaKey);
}
#pragma warning restore CA2000

internal BbsSshArchiveDownloader(OctoLogger log, FileSystemProvider fileSystemProvider, ISftpClient sftpClient)
{
Expand Down Expand Up @@ -130,7 +151,7 @@ public void Dispose()
{
(_sftpClient as IDisposable)?.Dispose();
(_rsaKey as IDisposable)?.Dispose();
(_pkRsa as IDisposable)?.Dispose();
(_authenticationMethodRsa as IDisposable)?.Dispose();
(_privateKey as IDisposable)?.Dispose();
}
}