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

Renamed SignatureProvider to DataFormatSigner so SignatureProvider can be used by the new SignatureProvider libraries. #700

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
14 changes: 7 additions & 7 deletions src/Sign.Core/ServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ internal static ServiceProvider CreateDefault(
services.AddSingleton<IContainerProvider, ContainerProvider>();
services.AddSingleton<IFileMetadataService, FileMetadataService>();
services.AddSingleton<IDirectoryService, DirectoryService>();
services.AddSingleton<ISignatureProvider, AzureSignToolSignatureProvider>();
services.AddSingleton<ISignatureProvider, ClickOnceSignatureProvider>();
services.AddSingleton<ISignatureProvider, VsixSignatureProvider>();
services.AddSingleton<ISignatureProvider, NuGetSignatureProvider>();
services.AddSingleton<ISignatureProvider, AppInstallerServiceSignatureProvider>();
services.AddSingleton<IDefaultSignatureProvider, DefaultSignatureProvider>();
services.AddSingleton<IAggregatingSignatureProvider, AggregatingSignatureProvider>();
services.AddSingleton<IDataFormatSigner, AzureSignToolSigner>();
services.AddSingleton<IDataFormatSigner, ClickOnceSigner>();
services.AddSingleton<IDataFormatSigner, VsixSigner>();
services.AddSingleton<IDataFormatSigner, NuGetSigner>();
services.AddSingleton<IDataFormatSigner, AppInstallerServiceSigner>();
services.AddSingleton<IDefaultDataFormatSigner, DefaultSigner>();
services.AddSingleton<IAggregatingDataFormatSigner, AggregatingSigner>();
services.AddSingleton<IManifestSigner, ManifestSigner>();
services.AddSingleton<IMageCli, MageCli>();
services.AddSingleton<IMakeAppxCli, MakeAppxCli>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@

namespace Sign.Core
{
internal sealed class AggregatingSignatureProvider : IAggregatingSignatureProvider
internal sealed class AggregatingSigner : IAggregatingDataFormatSigner
{
private readonly IContainerProvider _containerProvider;
private readonly IDefaultSignatureProvider _defaultSignatureProvider;
private readonly IDefaultDataFormatSigner _defaultSigner;
private readonly IFileMetadataService _fileMetadataService;
private readonly IMatcherFactory _matcherFactory;
private readonly IEnumerable<ISignatureProvider> _signatureProviders;
private readonly IEnumerable<IDataFormatSigner> _signers;

// Dependency injection requires a public constructor.
public AggregatingSignatureProvider(
IEnumerable<ISignatureProvider> signatureProviders,
IDefaultSignatureProvider defaultSignatureProvider,
public AggregatingSigner(
IEnumerable<IDataFormatSigner> signers,
IDefaultDataFormatSigner defaultSigner,
IContainerProvider containerProvider,
IFileMetadataService fileMetadataService,
IMatcherFactory matcherFactory)
{
ArgumentNullException.ThrowIfNull(signatureProviders, nameof(signatureProviders));
ArgumentNullException.ThrowIfNull(defaultSignatureProvider, nameof(defaultSignatureProvider));
ArgumentNullException.ThrowIfNull(signers, nameof(signers));
ArgumentNullException.ThrowIfNull(defaultSigner, nameof(defaultSigner));
ArgumentNullException.ThrowIfNull(containerProvider, nameof(containerProvider));
ArgumentNullException.ThrowIfNull(fileMetadataService, nameof(fileMetadataService));
ArgumentNullException.ThrowIfNull(matcherFactory, nameof(matcherFactory));

_signatureProviders = signatureProviders;
_defaultSignatureProvider = defaultSignatureProvider;
_signers = signers;
_defaultSigner = defaultSigner;
_containerProvider = containerProvider;
_fileMetadataService = fileMetadataService;
_matcherFactory = matcherFactory;
Expand All @@ -39,9 +39,9 @@ public bool CanSign(FileInfo file)
{
ArgumentNullException.ThrowIfNull(file, nameof(file));

foreach (ISignatureProvider signatureProvider in _signatureProviders)
foreach (IDataFormatSigner signer in _signers)
{
if (signatureProvider.CanSign(file))
if (signer.CanSign(file))
{
return true;
}
Expand Down Expand Up @@ -181,19 +181,19 @@ where _containerProvider.IsAppxBundleContainer(file)

// split by code sign service and fallback to default

var grouped = (from signatureProvider in _signatureProviders
var grouped = (from signer in _signers
from file in files
where signatureProvider.CanSign(file)
group file by signatureProvider into groups
where signer.CanSign(file)
group file by signer into groups
select groups).ToList();

// get all files and exclude existing;

// This is to catch PE files that don't have the correct extension set
var defaultFiles = files.Except(grouped.SelectMany(g => g))
.Where(_fileMetadataService.IsPortableExecutable)
.Select(f => new { _defaultSignatureProvider.SignatureProvider, f })
.GroupBy(a => a.SignatureProvider, k => k.f)
.Select(f => new { _defaultSigner.Signer, f })
.GroupBy(a => a.Signer, k => k.f)
.SingleOrDefault(); // one group here

if (defaultFiles != null)
Expand All @@ -207,11 +207,11 @@ group file by signatureProvider into groups
public void CopySigningDependencies(FileInfo file, DirectoryInfo destination, SignOptions options)
{
// pass the handling for this down to the actual implementations
foreach (ISignatureProvider signatureProvider in _signatureProviders)
foreach (IDataFormatSigner signer in _signers)
{
if (signatureProvider.CanSign(file))
if (signer.CanSign(file))
{
signatureProvider.CopySigningDependencies(file, destination, options);
signer.CopySigningDependencies(file, destination, options);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace Sign.Core
{
// Not really signing anything, but updates the manifest file with the
// correct publisher information
internal sealed class AppInstallerServiceSignatureProvider : ISignatureProvider
internal sealed class AppInstallerServiceSigner : IDataFormatSigner
{
private readonly ICertificateProvider _certificateProvider;
private readonly ILogger _logger;

// Dependency injection requires a public constructor.
public AppInstallerServiceSignatureProvider(
public AppInstallerServiceSigner(
ICertificateProvider certificateProvider,
ILogger<ISignatureProvider> logger)
ILogger<IDataFormatSigner> logger)
{
ArgumentNullException.ThrowIfNull(certificateProvider, nameof(certificateProvider));
ArgumentNullException.ThrowIfNull(logger, nameof(logger));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Sign.Core
{
internal sealed class AzureSignToolSignatureProvider : IAzureSignToolSignatureProvider
internal sealed class AzureSignToolSigner : IAzureSignToolDataFormatSigner
{
private readonly ICertificateProvider _certificateProvider;
private readonly ISignatureAlgorithmProvider _signatureAlgorithmProvider;
Expand All @@ -19,11 +19,11 @@ internal sealed class AzureSignToolSignatureProvider : IAzureSignToolSignaturePr
private readonly IToolConfigurationProvider _toolConfigurationProvider;

// Dependency injection requires a public constructor.
public AzureSignToolSignatureProvider(
public AzureSignToolSigner(
IToolConfigurationProvider toolConfigurationProvider,
ISignatureAlgorithmProvider signatureAlgorithmProvider,
ICertificateProvider certificateProvider,
ILogger<ISignatureProvider> logger)
ILogger<IDataFormatSigner> logger)
{
ArgumentNullException.ThrowIfNull(toolConfigurationProvider, nameof(toolConfigurationProvider));
ArgumentNullException.ThrowIfNull(signatureAlgorithmProvider, nameof(signatureAlgorithmProvider));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace Sign.Core
{
internal sealed class ClickOnceSignatureProvider : RetryingSignatureProvider, ISignatureProvider
internal sealed class ClickOnceSigner : RetryingSigner, IDataFormatSigner
{
private readonly Lazy<IAggregatingSignatureProvider> _aggregatingSignatureProvider;
private readonly Lazy<IAggregatingDataFormatSigner> _aggregatingSigner;
private readonly ICertificateProvider _certificateProvider;
private readonly ISignatureAlgorithmProvider _signatureAlgorithmProvider;
private readonly IMageCli _mageCli;
Expand All @@ -22,13 +22,13 @@ internal sealed class ClickOnceSignatureProvider : RetryingSignatureProvider, IS
private readonly IFileMatcher _fileMatcher;

// Dependency injection requires a public constructor.
public ClickOnceSignatureProvider(
public ClickOnceSigner(
ISignatureAlgorithmProvider signatureAlgorithmProvider,
ICertificateProvider certificateProvider,
IServiceProvider serviceProvider,
IMageCli mageCli,
IManifestSigner manifestSigner,
ILogger<ISignatureProvider> logger,
ILogger<IDataFormatSigner> logger,
IFileMatcher fileMatcher)
: base(logger)
{
Expand All @@ -46,7 +46,7 @@ public ClickOnceSignatureProvider(
_fileMatcher = fileMatcher;

// Need to delay this as it'd create a dependency loop if directly in the ctor
_aggregatingSignatureProvider = new Lazy<IAggregatingSignatureProvider>(() => serviceProvider.GetService<IAggregatingSignatureProvider>()!);
_aggregatingSigner = new Lazy<IAggregatingDataFormatSigner>(() => serviceProvider.GetService<IAggregatingDataFormatSigner>()!);
}

public bool CanSign(FileInfo file)
Expand Down Expand Up @@ -110,7 +110,7 @@ await Parallel.ForEachAsync(files, _parallelOptions, async (file, state) =>
filesToSign.AddRange(setupExe);

// sign the inner files
await _aggregatingSignatureProvider.Value.SignAsync(filesToSign!, options);
await _aggregatingSigner.Value.SignAsync(filesToSign!, options);

// rename the rest of the deploy files since signing the manifest will need them.
// this uses the overload of GetFiles() that ignores file matching options because we
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@

namespace Sign.Core
{
internal sealed class DefaultSignatureProvider : IDefaultSignatureProvider
internal sealed class DefaultSigner : IDefaultDataFormatSigner
{
public ISignatureProvider SignatureProvider { get; }
public IDataFormatSigner Signer { get; }

// Dependency injection requires a public constructor.
public DefaultSignatureProvider(IServiceProvider serviceProvider)
public DefaultSigner(IServiceProvider serviceProvider)
{
ArgumentNullException.ThrowIfNull(serviceProvider, nameof(serviceProvider));

foreach (ISignatureProvider signatureProvider in serviceProvider.GetServices<ISignatureProvider>())
foreach (IDataFormatSigner signer in serviceProvider.GetServices<IDataFormatSigner>())
{
if (signatureProvider is IAzureSignToolSignatureProvider)
if (signer is IAzureSignToolDataFormatSigner)
{
SignatureProvider = signatureProvider;
Signer = signer;

return;
}
}

SignatureProvider = new DoNothingDefaultSignatureProvider();
Signer = new DoNothingDefaultDataFormatSigner();
}

public bool CanSign(FileInfo file)
{
return SignatureProvider.CanSign(file);
return Signer.CanSign(file);
}

public Task SignAsync(IEnumerable<FileInfo> files, SignOptions options)
{
return SignatureProvider.SignAsync(files, options);
return Signer.SignAsync(files, options);
}

private sealed class DoNothingDefaultSignatureProvider : ISignatureProvider
private sealed class DoNothingDefaultDataFormatSigner : IDataFormatSigner
{
public bool CanSign(FileInfo file)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sign.Core
{
internal interface IAggregatingSignatureProvider : ISignatureProvider
internal interface IAggregatingDataFormatSigner : IDataFormatSigner
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sign.Core
{
internal interface IAzureSignToolSignatureProvider : ISignatureProvider
internal interface IAzureSignToolDataFormatSigner : IDataFormatSigner
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sign.Core
{
internal interface ISignatureProvider
internal interface IDataFormatSigner
{
bool CanSign(FileInfo file);
Task SignAsync(IEnumerable<FileInfo> files, SignOptions options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Sign.Core
{
internal interface IDefaultSignatureProvider
internal interface IDefaultDataFormatSigner
{
ISignatureProvider SignatureProvider { get; }
IDataFormatSigner Signer { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

namespace Sign.Core
{
internal sealed class NuGetSignatureProvider : RetryingSignatureProvider, ISignatureProvider
internal sealed class NuGetSigner : RetryingSigner, IDataFormatSigner
{
private readonly ICertificateProvider _certificateProvider;
private readonly ISignatureAlgorithmProvider _signatureAlgorithmProvider;
private readonly INuGetSignTool _nuGetSignTool;

// Dependency injection requires a public constructor.
public NuGetSignatureProvider(
public NuGetSigner(
ISignatureAlgorithmProvider signatureAlgorithmProvider,
ICertificateProvider certificateProvider,
INuGetSignTool nuGetSignTool,
ILogger<ISignatureProvider> logger)
ILogger<IDataFormatSigner> logger)
: base(logger)
{
ArgumentNullException.ThrowIfNull(signatureAlgorithmProvider, nameof(signatureAlgorithmProvider));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

namespace Sign.Core
{
internal abstract class RetryingSignatureProvider
internal abstract class RetryingSigner
{
protected ILogger Logger { get; }

protected RetryingSignatureProvider(ILogger logger)
protected RetryingSigner(ILogger logger)
{
ArgumentNullException.ThrowIfNull(logger, nameof(logger));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

namespace Sign.Core
{
internal sealed class VsixSignatureProvider : RetryingSignatureProvider, ISignatureProvider
internal sealed class VsixSigner : RetryingSigner, IDataFormatSigner
{
private readonly ICertificateProvider _certificateProvider;
private readonly ISignatureAlgorithmProvider _signatureAlgorithmProvider;
private readonly IVsixSignTool _vsixSignTool;

// Dependency injection requires a public constructor.
public VsixSignatureProvider(
public VsixSigner(
ISignatureAlgorithmProvider signatureAlgorithmProvider,
ICertificateProvider certificateProvider,
IVsixSignTool vsixSignTool,
ILogger<ISignatureProvider> logger)
ILogger<IDataFormatSigner> logger)
: base(logger)
{
ArgumentNullException.ThrowIfNull(signatureAlgorithmProvider, nameof(signatureAlgorithmProvider));
Expand Down
11 changes: 5 additions & 6 deletions src/Sign.Core/Signer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Azure.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -42,7 +41,7 @@ public async Task<int> SignAsync(
HashAlgorithmName fileHashAlgorithm,
HashAlgorithmName timestampHashAlgorithm)
{
IAggregatingSignatureProvider signatureProvider = _serviceProvider.GetRequiredService<IAggregatingSignatureProvider>();
IAggregatingDataFormatSigner signer = _serviceProvider.GetRequiredService<IAggregatingDataFormatSigner>();
IDirectoryService directoryService = _serviceProvider.GetRequiredService<IDirectoryService>();
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = maxConcurrency };

Expand Down Expand Up @@ -135,7 +134,7 @@ await Parallel.ForEachAsync(inputFiles, parallelOptions, async (input, token) =>
{
string inputFileName = Path.Combine(temporaryDirectory.Directory.FullName, Path.GetRandomFileName());
// However check its extension as it might be important (e.g. zip, bundle, etc)
if (signatureProvider.CanSign(input))
if (signer.CanSign(input))
{
// Keep the input extenstion as it has significance.
inputFileName = Path.ChangeExtension(inputFileName, input.Extension);
Expand All @@ -148,16 +147,16 @@ await Parallel.ForEachAsync(inputFiles, parallelOptions, async (input, token) =>
input.CopyTo(inputFileName, overwrite: true);
// for things like clickonce we will need additional files from the source location
// in order to fully sign everything, so ask the signature provider to do it for us.
signatureProvider.CopySigningDependencies(input, temporaryDirectory.Directory, signOptions);
signer.CopySigningDependencies(input, temporaryDirectory.Directory, signOptions);
}

FileInfo fi = new(inputFileName);

await signatureProvider.SignAsync(new[] { fi }, signOptions);
await signer.SignAsync(new[] { fi }, signOptions);

// copy everything back
fi.CopyTo(output.FullName, overwrite: true);
signatureProvider.CopySigningDependencies(fi, output.Directory!, signOptions);
signer.CopySigningDependencies(fi, output.Directory!, signOptions);
}

_logger.LogInformation(Resources.SigningSucceededWithTimeElapsed, sw.ElapsedMilliseconds);
Expand Down
Loading