diff --git a/.vscode/launch.json b/.vscode/launch.json index 3a9238a..e56041a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/src/example-api/bin/Debug/netcoreapp2.0/example-api.dll", + "program": "${workspaceFolder}/src/example-api/bin/Debug/netcoreapp3.1/example-api.dll", "args": [], "cwd": "${workspaceFolder}/src/example-api", "stopAtEntry": false, diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 4166009..4cdf972 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -2,7 +2,7 @@ "version": "2.0.0", "tasks": [ { - "taskName": "build", + "label": "build", "command": "dotnet", "type": "process", "args": [ diff --git a/Readme.md b/Readme.md index e551a01..f273c84 100644 --- a/Readme.md +++ b/Readme.md @@ -5,13 +5,12 @@ Enrich your configuration with plain text or secure settings from AWS ParameterS ## Example -Example configuration for a asp.net core project: +Example configuration for an ASP.Net Core project: - public static IWebHost BuildWebHost(string[] args) + public static IHost BuildHost(string[] args) { - - return WebHost.CreateDefaultBuilder(args) - .ConfigureAppConfiguration((hostContext, config)=> + return Host.CreateDefaultBuilder(args) + .ConfigureAppConfiguration((hostContext, config) => { config.SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) @@ -37,17 +36,21 @@ Example configuration for a asp.net core project: parameterStoreConfig.AwsCredential = new Amazon.Runtime.StoredProfileAWSCredentials(); }); }) - .UseStartup() + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }) .Build(); } # Alternatively + You can also use parameterStoreConfig.UseDefaultCredentials = true; to let AWS handle this. # Finding your way in the solution -in /src you find two projects, one example-api and the src for the nuget package itself -in /test you find the unittests +In /src you find two projects, one example-api and the src for the nuget package itself +In /test you find the unit tests To use the provided samples you have to will have to setup 3 parameters in the ParameterStore @@ -55,9 +58,8 @@ To use the provided samples you have to will have to setup 3 parameters in the P /somenamespace/someotherkey /somenamespace/somesecurekey => This needs to be set up as a secure string, which requires a KMS-Encryption key -You will also have set up and save a local default aws profile on the computer if you want to use StoredProfileAWSCredentials as it is used -in the example above (see http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) +You will also have set up and save a local default aws profile on the computer if you want to use StoredProfileAWSCredentials as it is used in the example above (see http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) # ParameterStore on AWS -see: http://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-paramstore.html +For reference see the [Parameter Store documentation on AWS](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html) diff --git a/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationProvider.cs b/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationProvider.cs index 20bdb90..3f157ce 100644 --- a/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationProvider.cs +++ b/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationProvider.cs @@ -1,13 +1,12 @@ -using Amazon.SimpleSystemsManagement; +using System; +using System.Collections.Generic; +using System.Linq; +using Amazon.SimpleSystemsManagement; using Amazon.SimpleSystemsManagement.Model; using Microsoft.Extensions.Configuration; -using System.Linq; -using System; -using System.Collections.Generic; namespace ParameterStoreConfigurationProvider { - public class ParameterStoreConfigurationProvider : ConfigurationProvider { private readonly ParameterStoreConfigurationSource configurationSource; @@ -21,18 +20,17 @@ public override void Load() { IEnumerable responses; - if (this.configurationSource.UseDefaultCredentials) + if (configurationSource.UseDefaultCredentials) { - using (var client = new AmazonSimpleSystemsManagementClient(Amazon.RegionEndpoint.GetBySystemName(this.configurationSource.Region))) + using (var client = new AmazonSimpleSystemsManagementClient(Amazon.RegionEndpoint.GetBySystemName(configurationSource.Region))) { responses = MappingClientResponseToData(client); } } else { - using (var client = new AmazonSimpleSystemsManagementClient(this.configurationSource.AwsCredential, Amazon.RegionEndpoint.GetBySystemName(this.configurationSource.Region))) + using (var client = new AmazonSimpleSystemsManagementClient(configurationSource.AwsCredential, Amazon.RegionEndpoint.GetBySystemName(configurationSource.Region))) { - responses = MappingClientResponseToData(client); } } @@ -40,9 +38,9 @@ public override void Load() MapResults(responses); } - private IEnumerable MappingClientResponseToData(AmazonSimpleSystemsManagementClient client) + private IEnumerable MappingClientResponseToData(IAmazonSimpleSystemsManagement client) { - IEnumerable requests = PrepareRequests(); + var requests = PrepareRequests(); IList responses = new List(); foreach (var request in requests) @@ -58,8 +56,9 @@ private IEnumerable MappingClientResponseToData(AmazonSim private void CheckParametersValidity(GetParametersResponse response) { var requiredInvalidParameters = response.InvalidParameters.Where(item => - this.configurationSource.ParameterMapping.First(pm => + configurationSource.ParameterMapping.First(pm => pm.AwsName == item).Optional == false).ToList(); + if (requiredInvalidParameters.Count > 0) { var wrongParams = ""; @@ -72,9 +71,9 @@ private void CheckParametersValidity(GetParametersResponse response) } } - internal IEnumerable PrepareRequests() + private IEnumerable PrepareRequests() { - var names = this.configurationSource.ParameterMapping.Select(x => x.AwsName).ToList(); + var names = configurationSource.ParameterMapping.Select(x => x.AwsName).ToList(); const int groupSize = 10; var requests = names @@ -83,30 +82,30 @@ internal IEnumerable PrepareRequests() .Select(g => new GetParametersRequest { Names = g.ToList(), - WithDecryption = this.configurationSource.WithDecryption + WithDecryption = configurationSource.WithDecryption }); return requests; } - internal void MapResults(IEnumerable responses) + private void MapResults(IEnumerable responses) { - this.Data = new Dictionary(); + Data = new Dictionary(); foreach (var response in responses) { foreach (var parameter in response.Parameters) { var parameterMapping = - this.configurationSource.ParameterMapping.First(pm => pm.AwsName == parameter.Name); - this.Data[parameterMapping.SettingName] = parameter.Value; + configurationSource.ParameterMapping.First(pm => pm.AwsName == parameter.Name); + Data[parameterMapping.SettingName] = parameter.Value; } foreach (var parameter in response.InvalidParameters) { var parameterMapping = - this.configurationSource.ParameterMapping.First(pm => pm.AwsName == parameter); - this.Data[parameterMapping.SettingName] = parameterMapping.Default; + configurationSource.ParameterMapping.First(pm => pm.AwsName == parameter); + Data[parameterMapping.SettingName] = parameterMapping.Default; } } } diff --git a/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationProvider.csproj b/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationProvider.csproj index 53d3975..d0b0674 100644 --- a/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationProvider.csproj +++ b/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationProvider.csproj @@ -1,21 +1,20 @@  - netcoreapp2.0 + netstandard2.0 true - Use AWS ParameterStore to configure your .net core project + Use AWS ParameterStore to configure your .Net project https://github.com/schwamster/ParameterStoreConfigurationProvider - https://github.com/schwamster/ParameterStoreConfigurationProvider/blob/master/LICENSE + MIT Bastian Töpfer AWS ParameterStore Configuration - 1.1.0 + 2.0.0 - - - - + + + diff --git a/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationSource.cs b/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationSource.cs index 97c4814..eda2f52 100644 --- a/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationSource.cs +++ b/src/ParameterStoreConfigurationProvider/ParameterStoreConfigurationSource.cs @@ -1,6 +1,6 @@ -using Amazon.Runtime; +using System.Collections.Generic; +using Amazon.Runtime; using Microsoft.Extensions.Configuration; -using System.Collections.Generic; namespace ParameterStoreConfigurationProvider { diff --git a/src/ParameterStoreConfigurationProvider/ParameterStoreExtension.cs b/src/ParameterStoreConfigurationProvider/ParameterStoreExtension.cs index 3c3f1fb..69707db 100644 --- a/src/ParameterStoreConfigurationProvider/ParameterStoreExtension.cs +++ b/src/ParameterStoreConfigurationProvider/ParameterStoreExtension.cs @@ -1,5 +1,5 @@ -using Microsoft.Extensions.Configuration; -using System; +using System; +using Microsoft.Extensions.Configuration; namespace ParameterStoreConfigurationProvider { diff --git a/src/example-api/Controllers/ValuesController.cs b/src/example-api/Controllers/ConfigurationController.cs similarity index 71% rename from src/example-api/Controllers/ValuesController.cs rename to src/example-api/Controllers/ConfigurationController.cs index f2bf7a0..8c3f9b5 100644 --- a/src/example-api/Controllers/ValuesController.cs +++ b/src/example-api/Controllers/ConfigurationController.cs @@ -1,14 +1,12 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; namespace example_api.Controllers { - [Route("api/[controller]")] - public class ConfigurationController : Controller + [ApiController] + [Route("[controller]")] + public class ConfigurationController : ControllerBase { private readonly IConfiguration configuration; @@ -17,7 +15,7 @@ public ConfigurationController(IConfiguration configuration) this.configuration = configuration; } - // GET api/values/5 + // GET Configuration/key [HttpGet("{key}")] public string Get(string key) { diff --git a/src/example-api/Program.cs b/src/example-api/Program.cs index f9c77ee..9a9e3d7 100644 --- a/src/example-api/Program.cs +++ b/src/example-api/Program.cs @@ -1,12 +1,8 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Hosting; using ParameterStoreConfigurationProvider; namespace example_api @@ -15,14 +11,13 @@ public class Program { public static void Main(string[] args) { - BuildWebHost(args).Run(); + BuildHost(args).Run(); } - public static IWebHost BuildWebHost(string[] args) + public static IHost BuildHost(string[] args) { - - return WebHost.CreateDefaultBuilder(args) - .ConfigureAppConfiguration((hostContext, config)=> + return Host.CreateDefaultBuilder(args) + .ConfigureAppConfiguration((hostContext, config) => { config.SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) @@ -36,7 +31,7 @@ public static IWebHost BuildWebHost(string[] args) }; parameterStoreConfig.Region = "eu-west-1"; parameterStoreConfig.UseDefaultCredentials = true; - // parameterStoreConfig.AwsCredential = new Amazon.Runtime.StoredProfileAWSCredentials(); + // parameterStoreConfig.AwsCredential = new Amazon.Runtime.StoredProfileAWSCredentials(); }) .AddParameterStoreConfig(parameterStoreConfig => { @@ -47,10 +42,13 @@ public static IWebHost BuildWebHost(string[] args) parameterStoreConfig.WithDecryption = true; parameterStoreConfig.Region = "eu-west-1"; parameterStoreConfig.UseDefaultCredentials = true; - // parameterStoreConfig.AwsCredential = new Amazon.Runtime.StoredProfileAWSCredentials(); + // parameterStoreConfig.AwsCredential = new Amazon.Runtime.StoredProfileAWSCredentials(); }); }) - .UseStartup() + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }) .Build(); } } diff --git a/src/example-api/Startup.cs b/src/example-api/Startup.cs index 6c1ad62..dcdaa16 100644 --- a/src/example-api/Startup.cs +++ b/src/example-api/Startup.cs @@ -1,13 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; +using Microsoft.Extensions.Hosting; namespace example_api { @@ -15,7 +10,7 @@ public class Startup { public Startup(IConfiguration configuration) { - Configuration = configuration; + Configuration = configuration; } public IConfiguration Configuration { get; } @@ -23,18 +18,25 @@ public Startup(IConfiguration configuration) // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddMvc(); + services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } - app.UseMvc(); + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); } } -} +} \ No newline at end of file diff --git a/src/example-api/appsettings.Development.json b/src/example-api/appsettings.Development.json index f334029..33dc90f 100644 --- a/src/example-api/appsettings.Development.json +++ b/src/example-api/appsettings.Development.json @@ -1,10 +1,9 @@ { "Logging": { - "IncludeScopes": false, "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" } } } diff --git a/src/example-api/appsettings.json b/src/example-api/appsettings.json index e5bef9a..a456eed 100644 --- a/src/example-api/appsettings.json +++ b/src/example-api/appsettings.json @@ -1,16 +1,11 @@ { "Logging": { - "IncludeScopes": false, - "Debug": { - "LogLevel": { - "Default": "Warning" - } - }, - "Console": { - "LogLevel": { - "Default": "Warning" - } + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" } }, + "AllowedHosts": "*", "somekey": "blup" } diff --git a/src/example-api/example-api.csproj b/src/example-api/example-api.csproj index 67d164d..a7ed759 100644 --- a/src/example-api/example-api.csproj +++ b/src/example-api/example-api.csproj @@ -1,23 +1,10 @@ - netcoreapp2.0 + netcoreapp3.1 + example-api - - - - - - - - - - - - - - diff --git a/test/example-api-test/ConfigurationTest.cs b/test/example-api-test/ConfigurationTest.cs index a4c9f70..9fd16ad 100644 --- a/test/example-api-test/ConfigurationTest.cs +++ b/test/example-api-test/ConfigurationTest.cs @@ -21,7 +21,7 @@ public async System.Threading.Tasks.Task PipelineSetupTestAsync_GetRegularSettin { // Act SetupValidServer(); - var response = await _client.GetAsync("/api/Configuration/somekey"); + var response = await _client.GetAsync("/Configuration/somekey"); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); @@ -36,7 +36,7 @@ public async System.Threading.Tasks.Task PipelineSetupTestAsync_GetSecureSetting { // Act SetupValidServer(); - var response = await _client.GetAsync("/api/Configuration/somesecurekey"); + var response = await _client.GetAsync("/Configuration/somesecurekey"); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); @@ -50,7 +50,7 @@ public async System.Threading.Tasks.Task PipelineSetupTestAsync_GetDefaultSettin { // Act SetupValidServer(); - var response = await _client.GetAsync("/api/Configuration/nonexistantkey"); + var response = await _client.GetAsync("/Configuration/nonexistantkey"); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); @@ -64,7 +64,7 @@ public async System.Threading.Tasks.Task PipelineSetupTestAsync_GetNullDefaultSe { // Act SetupValidServer(); - var response = await _client.GetAsync("/api/Configuration/anothernonexistantkey"); + var response = await _client.GetAsync("/Configuration/anothernonexistantkey"); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); @@ -100,7 +100,7 @@ private void SetupValidServer() } }; parameterStoreConfig.Region = "eu-west-1"; - parameterStoreConfig.AwsCredential = new Amazon.Runtime.StoredProfileAWSCredentials(); + parameterStoreConfig.UseDefaultCredentials = true; }) .AddParameterStoreConfig(parameterStoreConfig => { @@ -127,7 +127,7 @@ private void SetupValidServer() }; parameterStoreConfig.WithDecryption = true; parameterStoreConfig.Region = "eu-west-1"; - parameterStoreConfig.AwsCredential = new Amazon.Runtime.StoredProfileAWSCredentials(); + parameterStoreConfig.UseDefaultCredentials = true; }); var config = builder.Build(); @@ -172,7 +172,7 @@ private void SetupInvalidServer() }; parameterStoreConfig.WithDecryption = true; parameterStoreConfig.Region = "eu-west-1"; - parameterStoreConfig.AwsCredential = new Amazon.Runtime.StoredProfileAWSCredentials(); + parameterStoreConfig.UseDefaultCredentials = true; }); var config = builder.Build(); diff --git a/test/example-api-test/appsettings.json b/test/example-api-test/appsettings.json index e5bef9a..a456eed 100644 --- a/test/example-api-test/appsettings.json +++ b/test/example-api-test/appsettings.json @@ -1,16 +1,11 @@ { "Logging": { - "IncludeScopes": false, - "Debug": { - "LogLevel": { - "Default": "Warning" - } - }, - "Console": { - "LogLevel": { - "Default": "Warning" - } + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" } }, + "AllowedHosts": "*", "somekey": "blup" } diff --git a/test/example-api-test/example-api-test.csproj b/test/example-api-test/example-api-test.csproj index 80842ab..4dcf693 100644 --- a/test/example-api-test/example-api-test.csproj +++ b/test/example-api-test/example-api-test.csproj @@ -1,7 +1,7 @@ - netcoreapp2.0 + netcoreapp3.1 false @@ -14,10 +14,17 @@ - - - - + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all +