Skip to content

Commit

Permalink
Merge pull request #1255 from github/remove-rsa-sha2-256-workaround-f…
Browse files Browse the repository at this point in the history
…rom-bbs2gh

Remove rsa-sha2-256 workaround
  • Loading branch information
ArinGhazarian authored Dec 18, 2024
2 parents 49ecd20 + b98ae1a commit 2b6567d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 122 deletions.
76 changes: 0 additions & 76 deletions src/bbs2gh/RsaWithSha256SignatureKey.cs

This file was deleted.

50 changes: 5 additions & 45 deletions src/bbs2gh/Services/BbsSshArchiveDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using OctoshiftCLI.Extensions;
using OctoshiftCLI.Services;
using Renci.SshNet;
using Renci.SshNet.Security;

namespace OctoshiftCLI.BbsToGithub.Services;

Expand All @@ -14,9 +12,7 @@ public sealed class BbsSshArchiveDownloader : IBbsArchiveDownloader, IDisposable
private const int DOWNLOAD_PROGRESS_REPORT_INTERVAL_IN_SECONDS = 10;

private readonly ISftpClient _sftpClient;
private readonly RsaKey _rsaKey;
private readonly PrivateKeyFile _privateKey;
private readonly PrivateKeyAuthenticationMethod _authenticationMethodRsa;
private readonly OctoLogger _log;
private readonly FileSystemProvider _fileSystemProvider;
private readonly object _mutex = new();
Expand All @@ -26,43 +22,8 @@ public BbsSshArchiveDownloader(OctoLogger log, FileSystemProvider fileSystemProv
{
_log = log;
_fileSystemProvider = fileSystemProvider;

_privateKey = new PrivateKeyFile(privateKeyFileFullPath);

if (IsRsaKey(_privateKey))
{
_rsaKey = 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 RsaWithSha256SignatureKey UpdatePrivateKeyFileToRsaSha256(PrivateKeyFile privateKeyFile)
{
if ((privateKeyFile.HostKey as KeyHostAlgorithm).Key is not RsaKey oldRsaKey)
{
throw new ArgumentException("The private key file is not an RSA key.", nameof(privateKeyFile));
}

var rsaKey = new RsaWithSha256SignatureKey(oldRsaKey.Modulus, oldRsaKey.Exponent, oldRsaKey.D, oldRsaKey.P, oldRsaKey.Q, oldRsaKey.InverseQ);

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);

return rsaKey;
_sftpClient = new SftpClient(host, sshPort, sshUser, _privateKey);
}

internal BbsSshArchiveDownloader(OctoLogger log, FileSystemProvider fileSystemProvider, ISftpClient sftpClient)
Expand Down Expand Up @@ -125,7 +86,8 @@ private void LogProgress(ulong downloadedBytes, ulong totalBytes)
return;
}

_log.LogInformation($"Archive download in progress, {GetLogFriendlySize(downloadedBytes)} out of {GetLogFriendlySize(totalBytes)} ({GetPercentage(downloadedBytes, totalBytes)}) completed...");
_log.LogInformation(
$"Archive download in progress, {GetLogFriendlySize(downloadedBytes)} out of {GetLogFriendlySize(totalBytes)} ({GetPercentage(downloadedBytes, totalBytes)}) completed...");

_nextProgressReport = _nextProgressReport.AddSeconds(DOWNLOAD_PROGRESS_REPORT_INTERVAL_IN_SECONDS);
}
Expand Down Expand Up @@ -159,9 +121,7 @@ private string GetLogFriendlySize(ulong size)

public void Dispose()
{
(_sftpClient as IDisposable)?.Dispose();
(_rsaKey as IDisposable)?.Dispose();
(_authenticationMethodRsa as IDisposable)?.Dispose();
(_privateKey as IDisposable)?.Dispose();
_sftpClient?.Dispose();
_privateKey?.Dispose();
}
}
2 changes: 1 addition & 1 deletion src/bbs2gh/bbs2gh.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="SMBLibrary" Version="1.5.0.1" />
<PackageReference Include="SSH.NET" Version="2020.0.2" />
<PackageReference Include="SSH.NET" Version="2024.2.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
Expand Down

0 comments on commit 2b6567d

Please sign in to comment.