| | | 1 | | namespace Envilder.Infrastructure.Aws; |
| | | 2 | | |
| | | 3 | | using Amazon.SimpleSystemsManagement; |
| | | 4 | | using Amazon.SimpleSystemsManagement.Model; |
| | | 5 | | using Envilder.Domain.Ports; |
| | | 6 | | using System; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// <see cref="ISecretProvider"/> backed by AWS Systems Manager Parameter Store. |
| | | 12 | | /// Parameters are retrieved with decryption enabled so that <c>SecureString</c> |
| | | 13 | | /// values are returned in plain text. |
| | | 14 | | /// </summary> |
| | | 15 | | public class AwsSsmSecretProvider : ISecretProvider |
| | | 16 | | { |
| | | 17 | | private readonly IAmazonSimpleSystemsManagement _ssmClient; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance using the supplied SSM client. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="ssmClient">A configured <see cref="IAmazonSimpleSystemsManagement"/> instance.</param> |
| | 1 | 23 | | public AwsSsmSecretProvider(IAmazonSimpleSystemsManagement ssmClient) |
| | | 24 | | { |
| | 1 | 25 | | _ssmClient = ssmClient ?? throw new ArgumentNullException(nameof(ssmClient)); |
| | 1 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public async Task<string?> GetSecretAsync(string name, CancellationToken cancellationToken = default) |
| | | 30 | | { |
| | 1 | 31 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 32 | | { |
| | 0 | 33 | | throw new ArgumentException("Secret name cannot be null or whitespace.", nameof(name)); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | try |
| | | 37 | | { |
| | 1 | 38 | | var response = await _ssmClient.GetParameterAsync(new() { Name = name, WithDecryption = true }, cancellation |
| | 1 | 39 | | return response.Parameter.Value; |
| | | 40 | | } |
| | 1 | 41 | | catch (ParameterNotFoundException) |
| | | 42 | | { |
| | 1 | 43 | | return null; |
| | | 44 | | } |
| | 1 | 45 | | } |
| | | 46 | | } |