Class SsmSendGridApiKeyResolver
- Namespace
- Builvero.Infrastructure.Services
- Assembly
- Builvero.Infrastructure.dll
Implementation of ISendGridApiKeyResolver that resolves SendGrid API keys from AWS Systems Manager (SSM) Parameter Store or environment variables.
public class SsmSendGridApiKeyResolver : ISendGridApiKeyResolver
- Inheritance
-
SsmSendGridApiKeyResolver
- Implements
- Inherited Members
Remarks
This resolver uses a priority-based approach:
- First checks the
SENDGRID_API_KEYenvironment variable - If not found, retrieves the API key from AWS SSM Parameter Store using the configured parameter name
The resolved API key is cached in memory for the lifetime of the service instance to avoid repeated SSM calls. The parameter name is configured via ApiKeyParameterName.
This implementation is suitable for production environments where API keys should be stored securely in SSM rather than in application configuration files.
Constructors
SsmSendGridApiKeyResolver(IOptions<SendGridOptions>, IAmazonSimpleSystemsManagement, ILogger<SsmSendGridApiKeyResolver>)
Initializes a new instance of the SsmSendGridApiKeyResolver class.
public SsmSendGridApiKeyResolver(IOptions<SendGridOptions> options, IAmazonSimpleSystemsManagement ssmClient, ILogger<SsmSendGridApiKeyResolver> logger)
Parameters
optionsIOptions<SendGridOptions>SendGrid configuration options including the SSM parameter name.
ssmClientIAmazonSimpleSystemsManagementAWS Systems Manager client for retrieving parameters.
loggerILogger<SsmSendGridApiKeyResolver>Logger for recording API key resolution operations and errors.
Methods
ResolveApiKeyAsync(CancellationToken)
Resolves the SendGrid API key from environment variable or AWS SSM Parameter Store.
public Task<string> ResolveApiKeyAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
Remarks
The resolved API key is cached in memory. Subsequent calls return the cached value without making additional SSM requests or environment variable lookups.
The SSM parameter is retrieved with decryption enabled (WithDecryption = true) to support
SecureString parameters.
Exceptions
- InvalidOperationException
Thrown when:
- The API key parameter name is not configured
- The parameter is not found in SSM (if environment variable is not set)
- The parameter value is null or empty
- An error occurs while retrieving the parameter from SSM