diff --git a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs index cc3e3fc8..2157711c 100644 --- a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs +++ b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Controllers/ThirdPartyController.cs @@ -1,5 +1,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using YCompany.Configurations; using YCompanyPaymentsAPI.Data; using YCompanyPaymentsAPI.Models; @@ -11,17 +13,31 @@ namespace YCompanyThirdPartyAPI.Controllers public class ThirdPartyController : ControllerBase { private readonly InsuranceContext _context; + private readonly MyApiCredentials _myApiCredentials; - public ThirdPartyController(InsuranceContext context) + + public ThirdPartyController(InsuranceContext context, IOptions options) { _context = context; + _myApiCredentials = options.Value; } + //[HttpGet] + //public IEnumerable Get() + //{ + // List result = _context.Policies.ToList(); + // return result; + //} + [HttpGet] - public IEnumerable Get() + public IActionResult GetKey() { - List result = _context.Policies.ToList(); - return result; + var metadata = new MyApiCredentials + { + ApiKey = _myApiCredentials.ApiKey, + UserId = _myApiCredentials.UserId + }; + return Ok(metadata); } } } \ No newline at end of file diff --git a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs index 8a4b1154..879725c7 100644 --- a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs +++ b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/Program.cs @@ -1,10 +1,16 @@ 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); +builder.Host.ConfigureAppConfiguration(((_, configurationBuilder) => +{ + configurationBuilder.AddAmazonSecretsManager("", ""); +})); // Add services to the container. builder.Services.AddDbContext((serviceProvider, dbContextOptionsBuilder) => @@ -39,6 +45,7 @@ builder.Services.AddControllers(); +builder.Services.Configure(builder.Configuration); builder.Services.AddCors(corsOptions => { corsOptions.AddDefaultPolicy(corsPolicyBuilder => diff --git a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj index 8ab27f52..a6b544d5 100644 --- a/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj +++ b/ThirdPartyAndSeed/YCompanyThirdPartyAPI/YCompanyThirdPartyAPI.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -7,13 +7,14 @@ + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -21,4 +22,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/SecretManagerConfigurationExtensions.cs b/YCompany.Configurations/SecretManagerConfigurationExtensions.cs new file mode 100644 index 00000000..4cd5ace2 --- /dev/null +++ b/YCompany.Configurations/SecretManagerConfigurationExtensions.cs @@ -0,0 +1,20 @@ +using Microsoft.Extensions.Configuration; + + +namespace YCompany.Configurations +{ + public static class CustomConfigurationExtensions + { + public static void AddAmazonSecretsManager(this IConfigurationBuilder configurationBuilder, + string region, + string secretName) + { + var configurationSource = + new AmazonSecretsManagerConfigurationSource(region, secretName); + + configurationBuilder.Add(configurationSource); + } + } + +} + diff --git a/YCompany.Configurations/SecretManagerConfigurationProvider.cs b/YCompany.Configurations/SecretManagerConfigurationProvider.cs index 9288b67a..5a0c83a1 100644 --- a/YCompany.Configurations/SecretManagerConfigurationProvider.cs +++ b/YCompany.Configurations/SecretManagerConfigurationProvider.cs @@ -1,9 +1,64 @@ -using System; +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 + public class AmazonSecretsManagerConfigurationProvider : ConfigurationProvider { + private readonly string _region; + private readonly string _secretName; + + public AmazonSecretsManagerConfigurationProvider(string region, string secretName) + { + _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 + { + 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 new file mode 100644 index 00000000..b0eed8ea --- /dev/null +++ b/YCompany.Configurations/SecretManagerConfigurationSource.cs @@ -0,0 +1,24 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Text; + +namespace YCompany.Configurations +{ + 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 AmazonSecretsManagerConfigurationProvider(_region, _secretName); + } + } +} diff --git a/YCompany.Configurations/SecurityMetadata.cs b/YCompany.Configurations/SecurityMetadata.cs new file mode 100644 index 00000000..6597a362 --- /dev/null +++ b/YCompany.Configurations/SecurityMetadata.cs @@ -0,0 +1,9 @@ +namespace YCompany.Configurations +{ + public class MyApiCredentials + { + public string ApiKey { 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 b4b43f4c..0228ea2e 100644 --- a/YCompany.Configurations/YCompany.Configurations.csproj +++ b/YCompany.Configurations/YCompany.Configurations.csproj @@ -5,4 +5,11 @@ enable + + + + + + +