Skip to content

Commit

Permalink
Update AzureSignToolUtils to ignore if file is already signed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Dec 17, 2024
1 parent 5ca5e2b commit 9cccfbd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Update `GetToolInstallationPath` to use user temp folder.
- Update `TestRunUtil` icons to circle with color.
- Update `AzureSignToolUtils` sign to ignore exception.
- Update `AzureSignToolUtils` to ignore if file is already signed.
### Example
- Add `Resource` and `Resource.pt-BR` to test sign files.
### Tests
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>1.9.0-beta.2</Version>
<Version>1.9.0-beta.3</Version>
</PropertyGroup>
</Project>
15 changes: 15 additions & 0 deletions ricaun.Nuke/Extensions/NuGetExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ public static NugetVersionInfo Parse(string packageFileName)
/// </summary>
public static class NuGetExtension
{
/// <summary>
/// NuGetFileExtension (.nupkg)
/// </summary>
public const string NuGetFileExtension = ".nupkg";

/// <summary>
/// Check if file has NuGet extension (.nupkg)
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static bool IsNuGetFile(string filePath)
{
return Path.GetExtension(filePath).Equals(NuGetFileExtension, StringComparison.InvariantCultureIgnoreCase);
}

/// <summary>
/// TryGetPackageNameAndVersion
/// </summary>
Expand Down
14 changes: 7 additions & 7 deletions ricaun.Nuke/Extensions/SignExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static bool CreateCerFile(string fileNamePfx, string passwordPfx, string
/// <remarks>NuGet files use <see cref="NuGetExtension.NugetSign"/>.</remarks>
public static void Sign(string certPath, string certPassword, string filePath)
{
if (Path.GetExtension(filePath) == ".nupkg")
if (NuGetExtension.IsNuGetFile(filePath))
{
SignNuGet(certPath, certPassword, filePath);
return;
Expand Down Expand Up @@ -167,20 +167,20 @@ public static void SignNuGet(string certPath, string certPassword, string binary
}

/// <summary>
/// Has Signature
/// Has Signature in the file or NuGet
/// </summary>
/// <param name="fileInfo"></param>
/// <param name="filePath"></param>
/// <returns></returns>
static bool HasSignature(string fileInfo)
public static bool HasSignature(string filePath)
{
if (fileInfo.EndsWith(".nupkg"))
if (NuGetExtension.IsNuGetFile(filePath))
{
return NuGetExtension.NuGetVerifySignatures(fileInfo);
return NuGetExtension.NuGetVerifySignatures(filePath);
}

try
{
System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromSignedFile(fileInfo);
System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromSignedFile(filePath);
return true;
}
catch
Expand Down
17 changes: 10 additions & 7 deletions ricaun.Nuke/Tools/AzureSignToolUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Nuke.Common.Tools.DotNet;
using Nuke.Common.IO;
using Nuke.Common.Tooling;
using ricaun.Nuke.Extensions;

namespace ricaun.Nuke.Tools
{
Expand All @@ -19,7 +20,6 @@ public class AzureSignToolUtils
{
private const string TimestampUrlDefault = "http://timestamp.digicert.com";
private const string TimestampDigestDefault = "sha256";
private const string NugetPackageExtension = ".nupkg";

/// <summary>
/// Ensures that Azure Sign Tool and NuGet Key Vault Sign Tool are installed.
Expand Down Expand Up @@ -112,23 +112,26 @@ public static void DownloadNuGetKeyVaultSignTool()
/// <summary>
/// Signs the specified file using Azure Sign Tool or NuGet Key Vault Sign Tool.
/// </summary>
/// <param name="fileName">The name of the file to sign.</param>
/// <param name="filePath">The name of the file to sign.</param>
/// <param name="azureKeyVaultConfig">The Azure Key Vault configuration.</param>
/// <param name="azureKeyVaultClientSecret">The Azure Key Vault client secret.</param>
/// <param name="timestampUrlDefault">The default timestamp URL.</param>
/// <param name="timestampDigestDefault">The default timestamp digest.</param>
public static void Sign(string fileName,
public static void Sign(string filePath,
AzureKeyVaultConfig azureKeyVaultConfig, string azureKeyVaultClientSecret,
string timestampUrlDefault = TimestampUrlDefault,
string timestampDigestDefault = TimestampDigestDefault)
{
try
{
if (Path.GetExtension(fileName) == NugetPackageExtension)
if (SignExtension.HasSignature(filePath))
return;

if (NuGetExtension.IsNuGetFile(filePath))
{
DownloadNuGetKeyVaultSignTool();
NuGetKeyVaultSignToolTasks.NuGetKeyVaultSignTool(x => x
.SetFile(fileName)
.SetFile(filePath)
.SetKeyVaultCertificateName(azureKeyVaultConfig.AzureKeyVaultCertificate)
.SetKeyVaultUrl(azureKeyVaultConfig.AzureKeyVaultUrl)
.SetKeyVaultClientId(azureKeyVaultConfig.AzureKeyVaultClientId)
Expand All @@ -142,7 +145,7 @@ public static void Sign(string fileName,

DownloadAzureSignTool();
AzureSignToolTasks.AzureSignTool(x => x
.SetFiles(fileName)
.SetFiles(filePath)
.SetKeyVaultCertificateName(azureKeyVaultConfig.AzureKeyVaultCertificate)
.SetKeyVaultUrl(azureKeyVaultConfig.AzureKeyVaultUrl)
.SetKeyVaultClientId(azureKeyVaultConfig.AzureKeyVaultClientId)
Expand All @@ -154,7 +157,7 @@ public static void Sign(string fileName,
}
catch (Exception ex)
{
Serilog.Log.Error($"Azure Sign Error: {Path.GetFileName(fileName)} - {ex.Message}");
Serilog.Log.Error($"Azure Sign Error: {Path.GetFileName(filePath)} - {ex.Message}");
Serilog.Log.Information(ex.ToString());
}
}
Expand Down

0 comments on commit 9cccfbd

Please sign in to comment.