Skip to content

Commit

Permalink
Inject the logger in the constructor of the TrustedSigningService ins…
Browse files Browse the repository at this point in the history
…tead of an IServiceProvider.
  • Loading branch information
dlemstra committed Jun 20, 2024
1 parent 3ddb680 commit c785523
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Azure.CodeSigning;
using Azure.CodeSigning.Models;
using Azure.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sign.Core;

Expand All @@ -28,23 +27,23 @@ internal sealed class TrustedSigningService : ISignatureAlgorithmProvider, ICert
private X509Certificate2? _publicKey;

public TrustedSigningService(
IServiceProvider serviceProvider,
TokenCredential tokenCredential,
Uri endpointUrl,
string accountName,
string certificateProfileName)
string certificateProfileName,
ILogger<TrustedSigningService> logger)
{
ArgumentNullException.ThrowIfNull(serviceProvider, nameof(serviceProvider));
ArgumentNullException.ThrowIfNull(tokenCredential, nameof(tokenCredential));
ArgumentNullException.ThrowIfNull(endpointUrl, nameof(endpointUrl));
ArgumentException.ThrowIfNullOrEmpty(accountName, nameof(accountName));
ArgumentException.ThrowIfNullOrEmpty(certificateProfileName, nameof(certificateProfileName));
ArgumentNullException.ThrowIfNull(logger, nameof(logger));

_client = new CertificateProfileClient(tokenCredential, endpointUrl);
_accountName = accountName;
_certificateProfileName = certificateProfileName;
_logger = logger;

_logger = serviceProvider.GetRequiredService<ILogger<TrustedSigningService>>();
_client = new CertificateProfileClient(tokenCredential, endpointUrl);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// See the LICENSE.txt file in the project root for more information.

using Azure.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sign.Core;

namespace Sign.SignatureProviders.TrustedSigning
Expand Down Expand Up @@ -61,7 +63,8 @@ private TrustedSigningService GetService(IServiceProvider serviceProvider)
return _trustedSigningService;
}

_trustedSigningService = new TrustedSigningService(serviceProvider, _tokenCredential, _endpointUrl, _accountName, _certificateProfileName);
ILogger<TrustedSigningService> logger = serviceProvider.GetRequiredService<ILogger<TrustedSigningService>>();
_trustedSigningService = new TrustedSigningService(_tokenCredential, _endpointUrl, _accountName, _certificateProfileName, logger);
}

return _trustedSigningService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
// See the LICENSE.txt file in the project root for more information.

using Azure.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using Sign.TestInfrastructure;

namespace Sign.SignatureProviders.TrustedSigning.Test
{
Expand All @@ -16,29 +14,13 @@ public class TrustedSigningServiceTests
private readonly static Uri EndpointUrl = new("https://trustedsigning.test");
private const string AccountName = "a";
private const string CertificateProfileName = "b";
private readonly IServiceProvider serviceProvider;

public TrustedSigningServiceTests()
{
ServiceCollection services = new();
services.AddSingleton<ILogger<TrustedSigningService>>(new TestLogger<TrustedSigningService>());
serviceProvider = services.BuildServiceProvider();
}

[Fact]
public void Constructor_WhenServiceProviderIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(serviceProvider: null!, TokenCredential, EndpointUrl, AccountName, CertificateProfileName));

Assert.Equal("serviceProvider", exception.ParamName);
}
private readonly static ILogger<TrustedSigningService> Logger = Mock.Of<ILogger<TrustedSigningService>>();

[Fact]
public void Constructor_WhenTokenCredentialIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(serviceProvider, tokenCredential: null!, EndpointUrl, AccountName, CertificateProfileName));
() => new TrustedSigningService(tokenCredential: null!, EndpointUrl, AccountName, CertificateProfileName, Logger));

Assert.Equal("tokenCredential", exception.ParamName);
}
Expand All @@ -47,7 +29,7 @@ public void Constructor_WhenTokenCredentialIsNull_Throws()
public void Constructor_WhenEndpointUrlIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(serviceProvider, TokenCredential, endpointUrl: null!, AccountName, CertificateProfileName));
() => new TrustedSigningService(TokenCredential, endpointUrl: null!, AccountName, CertificateProfileName, Logger));

Assert.Equal("endpointUrl", exception.ParamName);
}
Expand All @@ -56,7 +38,7 @@ public void Constructor_WhenEndpointUrlIsNull_Throws()
public void Constructor_WhenAccountNameIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(serviceProvider, TokenCredential, EndpointUrl, accountName: null!, CertificateProfileName));
() => new TrustedSigningService(TokenCredential, EndpointUrl, accountName: null!, CertificateProfileName, Logger));

Assert.Equal("accountName", exception.ParamName);
}
Expand All @@ -65,7 +47,7 @@ public void Constructor_WhenAccountNameIsNull_Throws()
public void Constructor_WhenAccountNameIsEmpty_Throws()
{
ArgumentException exception = Assert.Throws<ArgumentException>(
() => new TrustedSigningService(serviceProvider, TokenCredential, EndpointUrl, accountName: string.Empty, CertificateProfileName));
() => new TrustedSigningService(TokenCredential, EndpointUrl, accountName: string.Empty, CertificateProfileName, Logger));

Assert.Equal("accountName", exception.ParamName);
}
Expand All @@ -74,7 +56,7 @@ public void Constructor_WhenAccountNameIsEmpty_Throws()
public void Constructor_WhenCertificateProfileNameIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(serviceProvider, TokenCredential, EndpointUrl, AccountName, certificateProfileName: null!));
() => new TrustedSigningService(TokenCredential, EndpointUrl, AccountName, certificateProfileName: null!, Logger));

Assert.Equal("certificateProfileName", exception.ParamName);
}
Expand All @@ -83,9 +65,18 @@ public void Constructor_WhenCertificateProfileNameIsNull_Throws()
public void Constructor_WhenCertificateProfileNameIsEmpty_Throws()
{
ArgumentException exception = Assert.Throws<ArgumentException>(
() => new TrustedSigningService(serviceProvider, TokenCredential, EndpointUrl, AccountName, certificateProfileName: string.Empty));
() => new TrustedSigningService(TokenCredential, EndpointUrl, AccountName, certificateProfileName: string.Empty, Logger));

Assert.Equal("certificateProfileName", exception.ParamName);
}

[Fact]
public void Constructor_WhenLoggerIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(TokenCredential, EndpointUrl, AccountName, CertificateProfileName, logger: null!));

Assert.Equal("logger", exception.ParamName);
}
}
}

0 comments on commit c785523

Please sign in to comment.