Skip to content

Commit

Permalink
Update to add checks for the name of secrets and certificates (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ross-mcdermott authored Jul 24, 2022
1 parent d01b7e3 commit 7d77d6e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/AzAcme.Cli/Commands/OrderCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AzAcme.Cli.Commands.Options;
using AzAcme.Cli.Util;
using AzAcme.Core;
using AzAcme.Core.Exceptions;
using AzAcme.Core.Extensions;
using AzAcme.Core.Providers.Models;
using Microsoft.Extensions.Logging;
Expand All @@ -27,6 +28,10 @@ public OrderCommand(ILogger logger,

protected override async Task<int> OnExecute(OrderOptions opts)
{
// check the name is valid first.
await certificateStore.ValidateCertificateName(opts.Certificate);

Console.WriteLine(opts.Certificate);
var certificateRequest = new CertificateRequest(opts.Certificate, opts.Subject, opts.SubjectAlternativeNames);

this.logger.LogInformation("Loading metadata from certificate store for certificate '{0}'.", opts.Certificate);
Expand Down
2 changes: 2 additions & 0 deletions src/AzAcme.Core/ICertificateStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace AzAcme.Core
{
public interface ICertificateStore
{
Task ValidateCertificateName(string name);

Task<CertificateMetadata> GetMetadata(CertificateRequest request);

Task<CertificateCsr> Prepare(CertificateRequest request);
Expand Down
2 changes: 2 additions & 0 deletions src/AzAcme.Core/ISecretStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
public interface ISecretStore
{
Task ValidateSecretName(string name);

Task<IScopedSecret> CreateScopedSecret(string name);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using AzAcme.Core.Providers.Models;
using System.Text.RegularExpressions;
using AzAcme.Core.Exceptions;
using AzAcme.Core.Providers.Models;
using Azure;
using Azure.Security.KeyVault.Certificates;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -30,6 +32,17 @@ public Task<CertificateMetadata> GetMetadata(CertificateRequest request)
return Task.FromResult(info);
}

public Task ValidateCertificateName(string name)
{
const string regex = "^[A-Za-z0-9-]+$";

if (!Regex.IsMatch(name, regex))
{
throw new ConfigurationException($"Key Vault Certificate '{name}' invalid. Should be alphanumeric and dashes only.");
}

return Task.CompletedTask;
}
public async Task<CertificateCsr> Prepare(CertificateRequest request)
{
const string IssuerName = "Unknown"; // always same for externally managed lifecycles in azure.
Expand Down
23 changes: 20 additions & 3 deletions src/AzAcme.Core/Providers/KeyVault/AzureKeyVaultSecretStore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Azure.Security.KeyVault.Secrets;
using System.Text.RegularExpressions;
using AzAcme.Core.Exceptions;
using Azure.Security.KeyVault.Secrets;
using Microsoft.Extensions.Logging;

namespace AzAcme.Core.Providers.KeyVault
Expand All @@ -14,11 +16,26 @@ public AzureKeyVaultSecretStore(ILogger logger, SecretClient client)
this.client = client ?? throw new ArgumentNullException(nameof(client));
}

public Task<IScopedSecret> CreateScopedSecret(string name)
public Task ValidateSecretName(string name)
{
const string regex = "^[A-Za-z0-9-]+$";

if (!Regex.IsMatch(name, regex))
{
throw new ConfigurationException($"Key Vault Certificate '{name}' invalid. Should be alphanumeric and dashes only.");
}

return Task.CompletedTask;
}

public async Task<IScopedSecret> CreateScopedSecret(string name)
{
// ensure validation has occured.
await ValidateSecretName(name);

IScopedSecret ss = new AzureKeyVaultScopedSecret(this.client, name);

return Task.FromResult(ss);
return ss;
}

internal class AzureKeyVaultScopedSecret : IScopedSecret
Expand Down

0 comments on commit 7d77d6e

Please sign in to comment.