From a0a6e7deb4b27f9208f5ef4c6820ad19051cc67d Mon Sep 17 00:00:00 2001 From: Dominic Villemure Date: Sun, 21 Apr 2024 11:07:34 -0400 Subject: [PATCH] # added userSecret to store .. secrets --- README.md | 21 ++++++++++++++++--- src/Infrastructure/DependencyInjection.cs | 6 +++--- ...Settings.json.dist => launchSettings.json} | 5 +---- src/Web/Web.csproj | 1 + 4 files changed, 23 insertions(+), 10 deletions(-) rename src/Web/Properties/{launchSettings.json.dist => launchSettings.json} (83%) diff --git a/README.md b/README.md index e923dc0..fb5f274 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ - Clean Architecture ( with Infrastructure, Domain, Application and Web layers ) - Minimal API endpoints. - Guards ( Fail fast ) : https://github.com/ardalis/GuardClauses -- ## Tools - Install Docker : https://www.docker.com/get-started/ @@ -20,8 +19,6 @@ Or with a mounted volume to persist data on the computer instead ( persist data docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=' -p 1433:1433 -v C:\dev\DockerVolumes\SqlServer-Utopy-1\data:/var/opt/mssql/data -v C:\dev\DockerVolumes\SqlServer-Utopy-1\log:/var/opt/mssql/log -v C:\dev\DockerVolumes\SqlServer-Utopy-1\secrets:/var/opt/mssql/secrets -d mcr.microsoft.com/mssql/server:2022-latest ``` -Set your password in an env var for the connection string. Windows : $Env:DB_PASSWORD = "YourPassword" - ## Entity Framework Create a new migration : @@ -34,6 +31,24 @@ Update database : ./Ef.ps1 database update ``` +## Secret Manager tool +Go to Web project: cd src/Web + +Add a user secret for local development : +``` +dotnet user-secrets set "DB_PASSWORD" "12345" +``` + +list your stored secrets : +``` +dotnet user-secrets list +``` + +Delete a secret : +``` +dotnet user-secrets remove "DB_PASSWORD" +``` + ## Build Run `dotnet build -tl` to build the solution. diff --git a/src/Infrastructure/DependencyInjection.cs b/src/Infrastructure/DependencyInjection.cs index 715bce6..e0bf63d 100644 --- a/src/Infrastructure/DependencyInjection.cs +++ b/src/Infrastructure/DependencyInjection.cs @@ -22,9 +22,9 @@ public static class DependencyInjection // Replace password in the connection string with env var in local environment. // Prod will use the connectionString stored in the vault with password in it directly. var connectionString = configuration.GetConnectionString("DefaultConnection") ?? ""; - - var dbPassword = Environment.GetEnvironmentVariable("DB_PASSWORD") ?? ""; - var dbHost = Environment.GetEnvironmentVariable("DB_HOST") ?? "localhost"; + + var dbPassword = configuration["DB_PASSWORD"] ?? ""; + var dbHost = configuration["DB_HOST"] ?? "localhost"; if (dbHost == "localhost" && dbPassword != string.Empty) { diff --git a/src/Web/Properties/launchSettings.json.dist b/src/Web/Properties/launchSettings.json similarity index 83% rename from src/Web/Properties/launchSettings.json.dist rename to src/Web/Properties/launchSettings.json index 85845fa..6ef4f83 100644 --- a/src/Web/Properties/launchSettings.json.dist +++ b/src/Web/Properties/launchSettings.json @@ -13,10 +13,7 @@ "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development", - "VaultUri": "", - "DB_PASSWORD": "", - "DB_HOST": "" + "ASPNETCORE_ENVIRONMENT": "Development" } }, "IIS Express": { diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index 14b1052..a5444e7 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -3,6 +3,7 @@ Hutopy.Web Hutopy.Web + de6d03c4-8b1c-49e2-a8ca-c38cd4dc7d85