diff --git a/CustomConfigProvider/CustomConfigProvider.csproj b/CustomConfigProvider/CustomConfigProvider.csproj new file mode 100644 index 00000000..132c02c5 --- /dev/null +++ b/CustomConfigProvider/CustomConfigProvider.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/CustomConfigProvider/CustomConfigurationExtensions.cs b/CustomConfigProvider/CustomConfigurationExtensions.cs new file mode 100644 index 00000000..7727326c --- /dev/null +++ b/CustomConfigProvider/CustomConfigurationExtensions.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Text; + +namespace YCompany.Configurations +{ + public static class CustomConfigurationExtensions + { + public static IConfigurationBuilder AddSecurityConfiguration + (this IConfigurationBuilder builder) + { + return builder.Add(new CustomConfigurationSource()); + } + } +} diff --git a/CustomConfigProvider/CustomConfigurationProvider.cs b/CustomConfigProvider/CustomConfigurationProvider.cs new file mode 100644 index 00000000..f20db3e0 --- /dev/null +++ b/CustomConfigProvider/CustomConfigurationProvider.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Text.Json; +using CustomConfigProvider; +using Microsoft.Extensions.Configuration; + +namespace YCompany.Configurations +{ + public class CustomConfigurationProvider :ConfigurationProvider + { + public async Task GetSecrets() + { + var secretsService = new SecretsManagerService("AKIATODEJPR4HSRAM5UQ", "7u0tKwy46NWtjdzT3cu0v8J0Ym0EOuvDdRzMjgoj", "ap-south-1"); + string secretValue = await secretsService.GetSecretValueByNameAsync("ApiAccessKeys"); + return secretValue; + } + public async override void Load() + { + + var text = File.ReadAllText(@"D:\SecurityMetadata.json"); + var options = new JsonSerializerOptions + { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; + var content = JsonSerializer.Deserialize + (text, options); + if (content != null) + { + var Data = new Dictionary + { + {"ApiKey", content.ApiKey}, + {"ApiSecret", content.ApiSecret} + }; + } + } + + } +} diff --git a/CustomConfigProvider/CustomConfigurationSource.cs b/CustomConfigProvider/CustomConfigurationSource.cs new file mode 100644 index 00000000..52a0c0b1 --- /dev/null +++ b/CustomConfigProvider/CustomConfigurationSource.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Text; + +namespace YCompany.Configurations +{ + public class CustomConfigurationSource:IConfigurationSource + + { + public IConfigurationProvider Build(IConfigurationBuilder builder) + { + return new CustomConfigurationProvider(); + } + } +} diff --git a/CustomConfigProvider/SecretsManagerService.cs b/CustomConfigProvider/SecretsManagerService.cs new file mode 100644 index 00000000..766f7f8d --- /dev/null +++ b/CustomConfigProvider/SecretsManagerService.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Amazon.Runtime; +using Amazon.SecretsManager; +using Amazon.SecretsManager.Model; +using Amazon.SecretsManager.Model; + +namespace CustomConfigProvider +{ + public class SecretsManagerService + { + private readonly AmazonSecretsManagerClient _secretsManagerClient; + public SecretsManagerService(string accessKey, string secretKey, string region) + { + var credentials = new BasicAWSCredentials(accessKey, secretKey); + var config = new AmazonSecretsManagerConfig + { + RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(region) + }; + _secretsManagerClient = new AmazonSecretsManagerClient(credentials, config); + } // Add methods to retrieve secrets here + + public async Task GetSecretValueByNameAsync(string secretName) + { + var request = new GetSecretValueRequest { SecretId = secretName }; + var response = await _secretsManagerClient.GetSecretValueAsync(request); + return response.SecretString; + } + + } +} diff --git a/CustomConfigProvider/SecurityMetaData.cs b/CustomConfigProvider/SecurityMetaData.cs new file mode 100644 index 00000000..7dc37b2c --- /dev/null +++ b/CustomConfigProvider/SecurityMetaData.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YCompany.Configurations +{ + public class SecurityMetaData + { + public string ApiKey { get; set; } + public string ApiSecret { get; set; } + } +} diff --git a/CustomConfigurationProvider/Controllers/CustomConfigurationController.cs b/CustomConfigurationProvider/Controllers/CustomConfigurationController.cs new file mode 100644 index 00000000..d88b1b6d --- /dev/null +++ b/CustomConfigurationProvider/Controllers/CustomConfigurationController.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using YCompany.Configurations; + +namespace CustomConfigurationProvider.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class CustomConfigurationController : ControllerBase + { + private readonly IConfiguration _configuration; + public CustomConfigurationController(IConfiguration configuration) + { + _configuration = configuration; + } + [HttpGet] + public IActionResult Get() + { + var metadata = new SecurityMetaData + { + ApiKey = _configuration["ApiKey"], + ApiSecret = _configuration["ApiSecret"] + }; + return Ok(metadata); + } + } +} diff --git a/CustomConfigurationProvider/CustomConfigurationExtensions.cs b/CustomConfigurationProvider/CustomConfigurationExtensions.cs new file mode 100644 index 00000000..7727326c --- /dev/null +++ b/CustomConfigurationProvider/CustomConfigurationExtensions.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Text; + +namespace YCompany.Configurations +{ + public static class CustomConfigurationExtensions + { + public static IConfigurationBuilder AddSecurityConfiguration + (this IConfigurationBuilder builder) + { + return builder.Add(new CustomConfigurationSource()); + } + } +} diff --git a/CustomConfigurationProvider/CustomConfigurationProvider.cs b/CustomConfigurationProvider/CustomConfigurationProvider.cs new file mode 100644 index 00000000..ecabfdf3 --- /dev/null +++ b/CustomConfigurationProvider/CustomConfigurationProvider.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Text.Json; +using Microsoft.Extensions.Configuration; + +namespace YCompany.Configurations +{ + public class CustomConfigurationProvider :ConfigurationProvider + { + public override void Load() + { + var text = File.ReadAllText(@"D:\SecurityMetadata.json"); + var options = new JsonSerializerOptions + { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; + var content = JsonSerializer.Deserialize + (text, options); + if (content != null) + { + var Data = new Dictionary + { + {"ApiKey", content.ApiKey}, + {"ApiSecret", content.ApiSecret} + }; + } + } + + } +} diff --git a/CustomConfigurationProvider/CustomConfigurationProvider1.csproj b/CustomConfigurationProvider/CustomConfigurationProvider1.csproj new file mode 100644 index 00000000..60bf9ead --- /dev/null +++ b/CustomConfigurationProvider/CustomConfigurationProvider1.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/CustomConfigurationProvider/CustomConfigurationProvider1.csproj.user b/CustomConfigurationProvider/CustomConfigurationProvider1.csproj.user new file mode 100644 index 00000000..efbdd2d3 --- /dev/null +++ b/CustomConfigurationProvider/CustomConfigurationProvider1.csproj.user @@ -0,0 +1,7 @@ + + + + ApiControllerEmptyScaffolder + root/Common/Api + + \ No newline at end of file diff --git a/CustomConfigurationProvider/CustomConfigurationSource.cs b/CustomConfigurationProvider/CustomConfigurationSource.cs new file mode 100644 index 00000000..52a0c0b1 --- /dev/null +++ b/CustomConfigurationProvider/CustomConfigurationSource.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Text; + +namespace YCompany.Configurations +{ + public class CustomConfigurationSource:IConfigurationSource + + { + public IConfigurationProvider Build(IConfigurationBuilder builder) + { + return new CustomConfigurationProvider(); + } + } +} diff --git a/CustomConfigurationProvider/Program.cs b/CustomConfigurationProvider/Program.cs new file mode 100644 index 00000000..60686773 --- /dev/null +++ b/CustomConfigurationProvider/Program.cs @@ -0,0 +1,29 @@ +using YCompany.Configurations; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Configuration.AddSecurityConfiguration(); + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/CustomConfigurationProvider/Properties/launchSettings.json b/CustomConfigurationProvider/Properties/launchSettings.json new file mode 100644 index 00000000..72782c3d --- /dev/null +++ b/CustomConfigurationProvider/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:57907", + "sslPort": 44305 + } + }, + "profiles": { + "CustomConfigurationProvider": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7007;http://localhost:5261", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/CustomConfigurationProvider/SecurityMetaData.cs b/CustomConfigurationProvider/SecurityMetaData.cs new file mode 100644 index 00000000..7dc37b2c --- /dev/null +++ b/CustomConfigurationProvider/SecurityMetaData.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YCompany.Configurations +{ + public class SecurityMetaData + { + public string ApiKey { get; set; } + public string ApiSecret { get; set; } + } +} diff --git a/CustomConfigurationProvider/appsettings.Development.json b/CustomConfigurationProvider/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CustomConfigurationProvider/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CustomConfigurationProvider/appsettings.json b/CustomConfigurationProvider/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/CustomConfigurationProvider/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} 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 + + + + + + diff --git a/YCompany.sln b/YCompany.sln index feb1b68b..93460ee5 100644 --- a/YCompany.sln +++ b/YCompany.sln @@ -153,7 +153,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YCompany.Identity.DataAcces EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YCompany.Identity.Infrastructure", "Services\YCompanyIdentityServer\YCompany.Identity.Infrastructure\YCompany.Identity.Infrastructure.csproj", "{351E9F40-37F7-475D-ACE2-9EF60A690E58}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YCompany.Configurations", "YCompany.Configurations\YCompany.Configurations.csproj", "{C1B484F6-DC3C-4E21-9CCA-BFC3DA0B5652}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YCompany.Configurations", "YCompany.Configurations\YCompany.Configurations.csproj", "{C1B484F6-DC3C-4E21-9CCA-BFC3DA0B5652}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomConfigurationProvider", "CustomConfigurationProvider\CustomConfigurationProvider.csproj", "{B9824F05-2B60-4332-83EB-B1A0F0B5A624}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -333,6 +335,10 @@ Global {C1B484F6-DC3C-4E21-9CCA-BFC3DA0B5652}.Debug|Any CPU.Build.0 = Debug|Any CPU {C1B484F6-DC3C-4E21-9CCA-BFC3DA0B5652}.Release|Any CPU.ActiveCfg = Release|Any CPU {C1B484F6-DC3C-4E21-9CCA-BFC3DA0B5652}.Release|Any CPU.Build.0 = Release|Any CPU + {B9824F05-2B60-4332-83EB-B1A0F0B5A624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B9824F05-2B60-4332-83EB-B1A0F0B5A624}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B9824F05-2B60-4332-83EB-B1A0F0B5A624}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B9824F05-2B60-4332-83EB-B1A0F0B5A624}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -411,6 +417,7 @@ Global {54EF6F3D-45C3-4F0C-8013-57D95C039806} = {11C5EC21-A895-494F-A1EF-C80C23DE2F3D} {351E9F40-37F7-475D-ACE2-9EF60A690E58} = {11C5EC21-A895-494F-A1EF-C80C23DE2F3D} {C1B484F6-DC3C-4E21-9CCA-BFC3DA0B5652} = {347BC5A5-E6B2-4FB9-89CF-5C5060779E19} + {B9824F05-2B60-4332-83EB-B1A0F0B5A624} = {347BC5A5-E6B2-4FB9-89CF-5C5060779E19} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A7F43644-7FC7-4EDB-9631-E5784A786800}