Skip to content

Latest commit

 

History

History
86 lines (70 loc) · 3.56 KB

README.md

File metadata and controls

86 lines (70 loc) · 3.56 KB

Libraries for dotnet based applications

Project Description
1 Configuration extension for Amazon Secrets Manager
projectnuget-packageusage
Amazon Secrets Manager service provides secure store for key/value pairs. Unlike parameter store, the data stored in Secrets Manager is encrypted by default.
You can use an extension to load config values in application's Configuration object. You must have saved config values in JSON format against keys in Secrets Manager.


Amazon Secrets Manager configuration extension

Usage:


1. Add secret in Amazon Secrets Manager, "PubSubInfra" as secret id and configuration key/value pairs as below.
key value
Kafka { "BootstrapServers": "localhost", "Producer": { "ClientId": "19", "StatisticsIntervalMs": 5000, "MessageTimeoutMs": 10000, "SocketTimeoutMs": 10000, "ApiVersionRequestTimeoutMs": 10000, "MetadataRequestTimeoutMs": 5000, "RequestTimeoutMs": 5000 }, "Consumer": { "GroupId": "49", "EnableAutoCommit": true, "StatisticsIntervalMs": 5000, "SessionTimeoutMs": 10000 } }
RabbitMQ { "Uri": "localhost", "UserName": "root@3", "Password": "SWAMI", "DispatchConsumersAsync": true }

2. Install Aksd.Extensions.Configuration.AmazonSecretsManager package. Add extesion in using following code in Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.AddAmazonSecretsManagerConfiguration("PubSubInfra");
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

3. Access configuration in code.

Startup.cs - Options pattern

services.Configure<KafkaConfig>(Configuration.GetSection("Kafka"));
services.Configure<RabbitMQConfig>(Configuration.GetSection("RabbitMQ"));
services.AddSingleton<IKafkaConnection>(sp =>
    {
        var kafkaConfigOption = sp.GetService<IOptions<KafkaConfig>>();
        var kafkaConfig = kafkaConfigOption.Value;
        return new DefaultKafkaConnection(
            bootstrapServers: kafkaConfig.BootstrapServers,
            clientId: kafkaConfig.Producer.ClientId,
            statisticsIntervalMs: 5000,
            messageTimeoutMs: 1000,
            socketTimeoutMs: 10000,
            apiVersionRequestTimeoutMs: 10000,
            metadataRequestTimeoutMs: 5000,
            requestTimeoutMs: 5000,
            groupId: "",
            enableAutoCommit: true,
            sessionTimeoutMs: 10000);
    });

services.AddSingleton<IRabbitMQConnection>(sp =>
    {
        var rabbitMQConfigOptions = sp.GetService<IOptions<RabbitMQConfig>>();
        var rabbitMQConfig = rabbitMQConfigOptions.Value;

        var connectionFactory = new ConnectionFactory()
        {
            Uri = new Uri(rabbitMQConfig.Uri),
            UserName = rabbitMQConfig.UserName,
            Password = rabbitMQConfig.Password,
            DispatchConsumersAsync = rabbitMQConfig.DispatchConsumersAsync,
        };

        var rabbitMQConnection = new DefaultRabbitMQConnection(connectionFactory);
        return rabbitMQConnection;
    });