-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Gcp.SecretManager.Provider.Tests/SecretManagerConfigurationProviderExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Moq; | ||
using System; | ||
using Xunit; | ||
|
||
namespace Gcp.SecretManager.Provider.Tests | ||
{ | ||
public class SecretManagerConfigurationProviderExtensionsTests | ||
{ | ||
private readonly Mock<IConfigurationBuilder> _mockConfigurationProvider; | ||
|
||
public SecretManagerConfigurationProviderExtensionsTests() | ||
{ | ||
_mockConfigurationProvider = new Mock<IConfigurationBuilder>(MockBehavior.Strict); | ||
} | ||
|
||
[Fact] | ||
public void Should_ThrowArgumentNullException_When_OptionsMethodNotProvided() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => _mockConfigurationProvider.Object.AddGcpSecretManager(null)); | ||
} | ||
|
||
[Fact] | ||
public void Should_ThrowArgumentNullException_When_ProjectIdIsNull() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => _mockConfigurationProvider.Object.AddGcpSecretManager((options) => | ||
{ | ||
|
||
})); | ||
} | ||
|
||
[Fact] | ||
public void Should_AddSecretManagerConfigurationSource_When_Configured() | ||
{ | ||
_mockConfigurationProvider.Setup(x => x.Add(It.IsAny<SecretManagerConfigurationSource>())).Returns<IConfigurationBuilder>(null); | ||
|
||
_mockConfigurationProvider.Object.AddGcpSecretManager((options) => | ||
{ | ||
options.ProjectId = "TestId"; | ||
}); | ||
|
||
_mockConfigurationProvider.Verify(x => x.Add(It.IsAny<SecretManagerConfigurationSource>()), Times.Once); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
Gcp.SecretManager.Provider.Tests/SecretManagerConfigurationSourceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Gcp.SecretManager.Provider.Helpers; | ||
using Google.Cloud.SecretManager.V1; | ||
using Microsoft.Extensions.Configuration; | ||
using Moq; | ||
using System; | ||
using Xunit; | ||
|
||
namespace Gcp.SecretManager.Provider.Tests | ||
{ | ||
public class SecretManagerConfigurationSourceTests | ||
{ | ||
private readonly Mock<ServiceClientHelper> _mockServiceClientHelper; | ||
private readonly Mock<IConfigurationBuilder> _mockConfigurationBuilder; | ||
|
||
public SecretManagerConfigurationSourceTests() | ||
{ | ||
_mockServiceClientHelper = new Mock<ServiceClientHelper>(MockBehavior.Strict); | ||
_mockConfigurationBuilder = new Mock<IConfigurationBuilder>(MockBehavior.Strict); | ||
} | ||
|
||
[Fact] | ||
public void Should_CreateProviderWithoutCredentialPath_When_CredentialsPathIsNull() | ||
{ | ||
_mockServiceClientHelper.Setup(x => x.Create()).Returns<SecretManagerServiceClient>(null); | ||
var options = new SecretManagerConfigurationOptions | ||
{ | ||
ProjectId = "ProjectId" | ||
}; | ||
|
||
var configurationSource = new SecretManagerConfigurationSource(options, _mockServiceClientHelper.Object); | ||
var provider = configurationSource.Build(_mockConfigurationBuilder.Object); | ||
|
||
_mockServiceClientHelper.Verify(x => x.Create(), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public void Should_CreateProviderWithCredentialPath_When_CredentialsPathIsNotNull() | ||
{ | ||
string credentialPath = "/Test/Credential/Path"; | ||
_mockServiceClientHelper.Setup(x => x.Create(credentialPath)).Returns<SecretManagerServiceClient>(null); | ||
var options = new SecretManagerConfigurationOptions | ||
{ | ||
ProjectId = "ProjectId", | ||
CredentialsPath = credentialPath | ||
}; | ||
|
||
var configurationSource = new SecretManagerConfigurationSource(options, _mockServiceClientHelper.Object); | ||
var provider = configurationSource.Build(_mockConfigurationBuilder.Object); | ||
|
||
_mockServiceClientHelper.Verify(x => x.Create(credentialPath), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public void Should_ThrowArgumentNullException_When_OptionsIsNull() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => new SecretManagerConfigurationSource(null, null)); | ||
} | ||
|
||
[Fact] | ||
public void Should_ThrowArgumentNullException_When_ProjectIdIsNull() | ||
{ | ||
var options = new SecretManagerConfigurationOptions(); | ||
|
||
var configurationSource = new SecretManagerConfigurationSource(options, _mockServiceClientHelper.Object); | ||
Assert.Throws<ArgumentNullException>(() => configurationSource.Build(_mockConfigurationBuilder.Object)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Google.Cloud.SecretManager.V1; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Gcp.SecretManager.Provider.Helpers | ||
{ | ||
public class ServiceClientHelper | ||
{ | ||
public virtual SecretManagerServiceClient Create() | ||
=> SecretManagerServiceClient.Create(); | ||
|
||
public virtual SecretManagerServiceClient Create(string credentialsPath) | ||
{ | ||
var clientBuilder = new SecretManagerServiceClientBuilder() | ||
{ | ||
CredentialsPath = credentialsPath | ||
}; | ||
|
||
return clientBuilder.Build(); | ||
} | ||
|
||
} | ||
} |
32 changes: 19 additions & 13 deletions
32
Gcp.SecretManager.Provider/SecretManagerConfigurationSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,50 @@ | ||
using Google.Api.Gax.ResourceNames; | ||
using Gcp.SecretManager.Provider.Helpers; | ||
using Google.Api.Gax.ResourceNames; | ||
using Google.Cloud.SecretManager.V1; | ||
using Microsoft.Extensions.Configuration; | ||
using System; | ||
|
||
namespace Gcp.SecretManager.Provider | ||
{ | ||
public class SecretManagerConfigurationSource : IConfigurationSource | ||
{ | ||
private readonly SecretManagerConfigurationOptions _options; | ||
private readonly ServiceClientHelper _clientHelper; | ||
|
||
public SecretManagerConfigurationSource(SecretManagerConfigurationOptions options) | ||
public SecretManagerConfigurationSource(SecretManagerConfigurationOptions options, ServiceClientHelper clientHelper = null) | ||
{ | ||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
_options = options; | ||
_clientHelper = clientHelper ?? new ServiceClientHelper(); | ||
} | ||
|
||
public IConfigurationProvider Build(IConfigurationBuilder builder) | ||
{ | ||
var client = CreateClient(); | ||
if (string.IsNullOrEmpty(_options.ProjectId)) | ||
{ | ||
throw new ArgumentNullException(nameof(_options.ProjectId)); | ||
} | ||
|
||
var projectName = new ProjectName(_options.ProjectId); | ||
var client = CreateClient(); | ||
|
||
return new SecretManagerConfigurationProvider(client, projectName); | ||
} | ||
|
||
private SecretManagerServiceClient CreateClient() | ||
{ | ||
SecretManagerServiceClient client; | ||
if (string.IsNullOrEmpty(_options.CredentialsPath)) | ||
{ | ||
client = SecretManagerServiceClient.Create(); | ||
return _clientHelper.Create(); | ||
} | ||
else | ||
{ | ||
var clientBuilder = new SecretManagerServiceClientBuilder() | ||
{ | ||
CredentialsPath = _options.CredentialsPath | ||
}; | ||
|
||
client = clientBuilder.Build(); | ||
return _clientHelper.Create(_options.CredentialsPath); | ||
} | ||
|
||
return client; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters