Skip to content

Commit

Permalink
Merge branch 'release/1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Cultti committed Aug 27, 2020
2 parents 9b70a78 + 39e1ad9 commit fa9e861
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dotnet-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: .NET Core

on:
push:
branches: [ master ]
branches: [ master, develop ]
pull_request:
branches: [ master ]
branches: [ develop ]

jobs:
build:
Expand Down
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);
}
}
}
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));
}
}
}
8 changes: 4 additions & 4 deletions Gcp.SecretManager.Provider/Gcp.SecretManager.Provider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<RepositoryUrl>https://github.com/Cultti/Gcp.SecretManager.Provider</RepositoryUrl>
<Authors>Timo Salola</Authors>
<Company />
<PackageTags>Google, Cloud, GCP, ConfigurationProvider</PackageTags>
<AssemblyVersion>1.0.1.0</AssemblyVersion>
<FileVersion>1.0.1.0</FileVersion>
<VersionPrefix>1.0.1</VersionPrefix>
<PackageTags>Google, Cloud, GCP, Secret Manager, ConfigurationProvider</PackageTags>
<AssemblyVersion>1.0.2.0</AssemblyVersion>
<FileVersion>1.0.2.0</FileVersion>
<VersionPrefix>1.0.2</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>

Expand Down
24 changes: 24 additions & 0 deletions Gcp.SecretManager.Provider/Helpers/ServiceClientHelper.cs
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 Gcp.SecretManager.Provider/SecretManagerConfigurationSource.cs
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;
}
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# [Google Secret Manager](https://cloud.google.com/secret-manager/) ConfigurationProvider
![.NET Core](https://github.com/Cultti/Gcp.SecretManager.Provider/workflows/.NET%20Core/badge.svg) [![codecov](https://codecov.io/gh/Cultti/Gcp.SecretManager.Provider/branch/master/graph/badge.svg)](https://codecov.io/gh/Cultti/Gcp.SecretManager.Provider)

Provides access to Google Secret Manager trough ConfigurationProvider

```
Expand Down

0 comments on commit fa9e861

Please sign in to comment.