From ffbbb6d23a269d84793e413d36f9b3be9a2c4452 Mon Sep 17 00:00:00 2001 From: Neha Nigam Date: Thu, 15 Feb 2024 19:18:31 +0530 Subject: [PATCH 1/2] Custom Configuration --- .../Controllers/ThirdPartyController.cs | 18 +++++++++++++- .../YCompanyThirdPartyAPI/Program.cs | 4 ++++ .../YCompanyThirdPartyAPI.csproj | 8 +++++-- .../YCompanyThirdPartyAPI/appsettings.json | 2 +- YCompany.Configurations/Custom_Config.json | 4 ++++ .../SecretManagerConfigurationExtensions.cs | 14 +++++++++++ .../SecretManagerConfigurationProvider.cs | 24 +++++++++++++++++-- .../SecretManagerConfigurationSource.cs | 15 ++++++++++++ YCompany.Configurations/SecurityMetadata.cs | 8 +++++++ .../YCompany.Configurations.csproj | 6 +++++ 10 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 YCompany.Configurations/Custom_Config.json create mode 100644 YCompany.Configurations/SecretManagerConfigurationExtensions.cs create mode 100644 YCompany.Configurations/SecretManagerConfigurationSource.cs create mode 100644 YCompany.Configurations/SecurityMetadata.cs diff --git a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs index cc3e3fc8..7c81e537 100644 --- a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs +++ b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using YCompany.Configurations; using YCompanyPaymentsAPI.Data; using YCompanyPaymentsAPI.Models; @@ -11,10 +12,12 @@ namespace YCompanyThirdPartyAPI.Controllers public class ThirdPartyController : ControllerBase { private readonly InsuranceContext _context; + private readonly IConfiguration _configuration; - public ThirdPartyController(InsuranceContext context) + public ThirdPartyController(InsuranceContext context, IConfiguration configuration) { _context = context; + _configuration = configuration; } [HttpGet] @@ -23,5 +26,18 @@ public IEnumerable Get() List result = _context.Policies.ToList(); return result; } + + [HttpGet] + public IActionResult GetConfig() + { + var metadata = new SecurityMetadata + { + + ApiKey = _configuration["ApiKey"], + ApiSecret = _configuration["ApiSecret"] + }; + Console.WriteLine(metadata.ApiKey); + return Ok(metadata); + } } } \ No newline at end of file diff --git a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs index 8a4b1154..afc79b0b 100644 --- a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs +++ b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs @@ -1,7 +1,9 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; using System.Reflection; +using YCompany.Configurations; using YCompanyPaymentsAPI.Data; var builder = WebApplication.CreateBuilder(args); @@ -81,6 +83,8 @@ }); var app = builder.Build(); +SecurityMetadata options = app.Services.GetRequiredService>().Value; +Console.WriteLine($"apiKey={options.ApiKey}"); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) diff --git a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj index 8ab27f52..85ce0885 100644 --- a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj +++ b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj @@ -8,12 +8,12 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -21,4 +21,8 @@ + + + + diff --git a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/appsettings.json b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/appsettings.json index 7c640a45..cc4ba22c 100644 --- a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/appsettings.json +++ b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=localhost\\MSSQLSERVER02;Database=ycompany;Trusted_Connection=True;TrustServerCertificate=True;" + "DefaultConnection": "Server=IN-PG03521Q;Database=ycompany;Integrated Security=True;TrustServerCertificate=True;" }, "Logging": { "LogLevel": { diff --git a/YCompany.Configurations/Custom_Config.json b/YCompany.Configurations/Custom_Config.json new file mode 100644 index 00000000..f7679a66 --- /dev/null +++ b/YCompany.Configurations/Custom_Config.json @@ -0,0 +1,4 @@ +{ + "apiKey": "your-api-key", + "apiSecret": "your-api-secret" +} diff --git a/YCompany.Configurations/SecretManagerConfigurationExtensions.cs b/YCompany.Configurations/SecretManagerConfigurationExtensions.cs new file mode 100644 index 00000000..5aa46eb4 --- /dev/null +++ b/YCompany.Configurations/SecretManagerConfigurationExtensions.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Configuration; + + +namespace YCompany.Configurations +{ + public static class SecretManagerConfigurationExtensions + { + public static IConfigurationBuilder AddSecurityConfiguration + (this IConfigurationBuilder builder) + { + return builder.Add(new SecretManagerConfigurationSource()); + } + } +} diff --git a/YCompany.Configurations/SecretManagerConfigurationProvider.cs b/YCompany.Configurations/SecretManagerConfigurationProvider.cs index 9288b67a..f795298c 100644 --- a/YCompany.Configurations/SecretManagerConfigurationProvider.cs +++ b/YCompany.Configurations/SecretManagerConfigurationProvider.cs @@ -1,9 +1,29 @@ -using System; +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; namespace YCompany.Configurations { - public class SecretManagerConfigurationProvider + public class SecretManagerConfigurationProvider : ConfigurationProvider { + public override void Load() + { + var text = File.ReadAllText("Custom_Config.json"); + var options = new JsonSerializerOptions + { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; + var content = JsonSerializer.Deserialize + (text, options); + if (content != null) + { + Data = new Dictionary + { + {"ApiKey", content.ApiKey}, + {"ApiSecret", content.ApiSecret} + }; + } + } } } diff --git a/YCompany.Configurations/SecretManagerConfigurationSource.cs b/YCompany.Configurations/SecretManagerConfigurationSource.cs new file mode 100644 index 00000000..38f33898 --- /dev/null +++ b/YCompany.Configurations/SecretManagerConfigurationSource.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Text; + +namespace YCompany.Configurations +{ + public class SecretManagerConfigurationSource : IConfigurationSource + { + public IConfigurationProvider Build(IConfigurationBuilder builder) + { + return new SecretManagerConfigurationProvider(); + } + } +} diff --git a/YCompany.Configurations/SecurityMetadata.cs b/YCompany.Configurations/SecurityMetadata.cs new file mode 100644 index 00000000..6f349f38 --- /dev/null +++ b/YCompany.Configurations/SecurityMetadata.cs @@ -0,0 +1,8 @@ +namespace YCompany.Configurations +{ + public class SecurityMetadata + { + public string ApiKey { get; set; } + public string ApiSecret { get; set; } + } +} diff --git a/YCompany.Configurations/YCompany.Configurations.csproj b/YCompany.Configurations/YCompany.Configurations.csproj index b4b43f4c..164a0155 100644 --- a/YCompany.Configurations/YCompany.Configurations.csproj +++ b/YCompany.Configurations/YCompany.Configurations.csproj @@ -5,4 +5,10 @@ enable + + + + + + From c18235351db77beb2a558f616910f84472471bf9 Mon Sep 17 00:00:00 2001 From: Neha Nigam Date: Wed, 13 Mar 2024 11:05:08 +0530 Subject: [PATCH 2/2] Apply AWS_SecretManager in custom config --- .../Controllers/ThirdPartyController.cs | 30 ++++----- .../YCompanyThirdPartyAPI/Program.cs | 7 +- .../YCompanyThirdPartyAPI.csproj | 3 +- YCompany.Configurations/Custom_Config.json | 4 -- .../SecretManagerConfigurationExtensions.cs | 14 ++-- .../SecretManagerConfigurationProvider.cs | 65 ++++++++++++++----- .../SecretManagerConfigurationSource.cs | 13 +++- YCompany.Configurations/SecurityMetadata.cs | 5 +- .../YCompany.Configurations.csproj | 1 + 9 files changed, 97 insertions(+), 45 deletions(-) delete mode 100644 YCompany.Configurations/Custom_Config.json diff --git a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs index 7c81e537..2157711c 100644 --- a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs +++ b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; using YCompany.Configurations; using YCompanyPaymentsAPI.Data; using YCompanyPaymentsAPI.Models; @@ -12,31 +13,30 @@ namespace YCompanyThirdPartyAPI.Controllers public class ThirdPartyController : ControllerBase { private readonly InsuranceContext _context; - private readonly IConfiguration _configuration; + private readonly MyApiCredentials _myApiCredentials; - public ThirdPartyController(InsuranceContext context, IConfiguration configuration) + + public ThirdPartyController(InsuranceContext context, IOptions options) { _context = context; - _configuration = configuration; + _myApiCredentials = options.Value; } - [HttpGet] - public IEnumerable Get() - { - List result = _context.Policies.ToList(); - return result; - } + //[HttpGet] + //public IEnumerable Get() + //{ + // List result = _context.Policies.ToList(); + // return result; + //} [HttpGet] - public IActionResult GetConfig() + public IActionResult GetKey() { - var metadata = new SecurityMetadata + var metadata = new MyApiCredentials { - - ApiKey = _configuration["ApiKey"], - ApiSecret = _configuration["ApiSecret"] + ApiKey = _myApiCredentials.ApiKey, + UserId = _myApiCredentials.UserId }; - Console.WriteLine(metadata.ApiKey); return Ok(metadata); } } diff --git a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs index afc79b0b..879725c7 100644 --- a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs +++ b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs @@ -7,6 +7,10 @@ using YCompanyPaymentsAPI.Data; var builder = WebApplication.CreateBuilder(args); +builder.Host.ConfigureAppConfiguration(((_, configurationBuilder) => +{ + configurationBuilder.AddAmazonSecretsManager("", ""); +})); // Add services to the container. builder.Services.AddDbContext((serviceProvider, dbContextOptionsBuilder) => @@ -41,6 +45,7 @@ builder.Services.AddControllers(); +builder.Services.Configure(builder.Configuration); builder.Services.AddCors(corsOptions => { corsOptions.AddDefaultPolicy(corsPolicyBuilder => @@ -83,8 +88,6 @@ }); var app = builder.Build(); -SecurityMetadata options = app.Services.GetRequiredService>().Value; -Console.WriteLine($"apiKey={options.ApiKey}"); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) diff --git a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj index 85ce0885..a6b544d5 100644 --- a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj +++ b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -7,6 +7,7 @@ + diff --git a/YCompany.Configurations/Custom_Config.json b/YCompany.Configurations/Custom_Config.json deleted file mode 100644 index f7679a66..00000000 --- a/YCompany.Configurations/Custom_Config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "apiKey": "your-api-key", - "apiSecret": "your-api-secret" -} diff --git a/YCompany.Configurations/SecretManagerConfigurationExtensions.cs b/YCompany.Configurations/SecretManagerConfigurationExtensions.cs index 5aa46eb4..4cd5ace2 100644 --- a/YCompany.Configurations/SecretManagerConfigurationExtensions.cs +++ b/YCompany.Configurations/SecretManagerConfigurationExtensions.cs @@ -3,12 +3,18 @@ namespace YCompany.Configurations { - public static class SecretManagerConfigurationExtensions + public static class CustomConfigurationExtensions { - public static IConfigurationBuilder AddSecurityConfiguration - (this IConfigurationBuilder builder) + public static void AddAmazonSecretsManager(this IConfigurationBuilder configurationBuilder, + string region, + string secretName) { - return builder.Add(new SecretManagerConfigurationSource()); + var configurationSource = + new AmazonSecretsManagerConfigurationSource(region, secretName); + + configurationBuilder.Add(configurationSource); } } + } + diff --git a/YCompany.Configurations/SecretManagerConfigurationProvider.cs b/YCompany.Configurations/SecretManagerConfigurationProvider.cs index f795298c..5a0c83a1 100644 --- a/YCompany.Configurations/SecretManagerConfigurationProvider.cs +++ b/YCompany.Configurations/SecretManagerConfigurationProvider.cs @@ -1,29 +1,64 @@ -using Microsoft.Extensions.Configuration; +using Amazon; +using Amazon.Runtime; +using Amazon.SecretsManager; +using Amazon.SecretsManager.Model; +using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.IO; using System.Text.Json; +using System.Threading.Tasks; namespace YCompany.Configurations { - public class SecretManagerConfigurationProvider : ConfigurationProvider + public class AmazonSecretsManagerConfigurationProvider : ConfigurationProvider { - public override void Load() + private readonly string _region; + private readonly string _secretName; + + public AmazonSecretsManagerConfigurationProvider(string region, string secretName) { - var text = File.ReadAllText("Custom_Config.json"); - var options = new JsonSerializerOptions - { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; - var content = JsonSerializer.Deserialize - (text, options); - if (content != null) + _region = region; + _secretName = secretName; + } + + public override async void Load() + { + var secret = await GetSecret(); + + Data = JsonSerializer.Deserialize>(secret); + } + private async Task GetSecret() + { + string secretName = "my-key"; + string region = "eu-north-1"; + + AWSCredentials credentials = new BasicAWSCredentials("AKIAYS2NUQSEQSBBZPPA", "uIDN9E+ZZh7nuV0UvmoGEMxfcnCJ8zVdxeY1xdgs"); + IAmazonSecretsManager client = new AmazonSecretsManagerClient(credentials, RegionEndpoint.GetBySystemName(region)); + + + GetSecretValueRequest request = new GetSecretValueRequest { - Data = new Dictionary - { - {"ApiKey", content.ApiKey}, - {"ApiSecret", content.ApiSecret} - }; + SecretId = secretName, + VersionStage = "AWSCURRENT", // VersionStage defaults to AWSCURRENT if unspecified. + }; + + GetSecretValueResponse response; + + try + { + response = await client.GetSecretValueAsync(request); } + catch (Exception e) + { + throw e; + } + + string secret = response.SecretString; + + return secret; } + } - } } + diff --git a/YCompany.Configurations/SecretManagerConfigurationSource.cs b/YCompany.Configurations/SecretManagerConfigurationSource.cs index 38f33898..b0eed8ea 100644 --- a/YCompany.Configurations/SecretManagerConfigurationSource.cs +++ b/YCompany.Configurations/SecretManagerConfigurationSource.cs @@ -5,11 +5,20 @@ namespace YCompany.Configurations { - public class SecretManagerConfigurationSource : IConfigurationSource + public class AmazonSecretsManagerConfigurationSource : IConfigurationSource { + private readonly string _region; + private readonly string _secretName; + + public AmazonSecretsManagerConfigurationSource(string region, string secretName) + { + _region = region; + _secretName = secretName; + } + public IConfigurationProvider Build(IConfigurationBuilder builder) { - return new SecretManagerConfigurationProvider(); + return new AmazonSecretsManagerConfigurationProvider(_region, _secretName); } } } diff --git a/YCompany.Configurations/SecurityMetadata.cs b/YCompany.Configurations/SecurityMetadata.cs index 6f349f38..6597a362 100644 --- a/YCompany.Configurations/SecurityMetadata.cs +++ b/YCompany.Configurations/SecurityMetadata.cs @@ -1,8 +1,9 @@ namespace YCompany.Configurations { - public class SecurityMetadata + public class MyApiCredentials { public string ApiKey { get; set; } - public string ApiSecret { get; set; } + public string UserId { get; set; } + public string Password { get; set; } } } diff --git a/YCompany.Configurations/YCompany.Configurations.csproj b/YCompany.Configurations/YCompany.Configurations.csproj index 164a0155..0228ea2e 100644 --- a/YCompany.Configurations/YCompany.Configurations.csproj +++ b/YCompany.Configurations/YCompany.Configurations.csproj @@ -6,6 +6,7 @@ +