Skip to content

Commit

Permalink
Use dotnet user-secrets instead of appsettings to store secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
haritha99ch committed May 8, 2023
1 parent 65959cb commit 0a0582c
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 61 deletions.
25 changes: 15 additions & 10 deletions .github/workflows/dotnet-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: .NET Core Desktop

on:
pull_request:
branches: ["main"]
branches: ["main", "production-*"]

jobs:
build:
Expand All @@ -15,19 +15,24 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Setting Environment Variables
env:
APPSETTINGS_PRODUCTION: ${{ secrets.APPSETTINGS_PRODUCTION }}
run: |
Set-Content -Path "Src/FoodDesire.AppSettings/appsettings.Production.json" -Value $env:APPSETTINGS_PRODUCTION
Set-Content -Path "Src/FoodDesire.AppSettings/appsettings.Development.json" -Value $env:APPSETTINGS_PRODUCTION
shell: pwsh

- name: Setup .NET
- name: Setup .NET 7
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.0.x

- name: Setting User Secrets
env:
CONNECTIONSTRINGS_DEFAULTCONNECTION: ${{ secrets.USERSECRETS_CONNECTIONSTRINGS_DEFAULTCONNECTION }}
CONNECTIONSTRINGS_STORAGECONNECTION: ${{ secrets.USERSECRETS_CONNECTIONSTRINGS_STORAGECONNECTION }}
CLIENTID: ${{ secrets.USERSECRETS_CLIENTID }}
JWT_SIGNINKEY: ${{ secrets.USERSECRETS_JWT_SIGNINKEY }}
run: |
dotnet user-secrets set "ConnectionStrings:DefaultConnection" $env:CONNECTIONSTRINGS_DEFAULTCONNECTION --project .\Src\FoodDesire.AppSettings\
dotnet user-secrets set "ConnectionStrings:StorageConnection" $env:CONNECTIONSTRINGS_STORAGECONNECTION --project .\Src\FoodDesire.AppSettings\
dotnet user-secrets set "ClientID" $env:CLIENTID --project .\Src\FoodDesire.AppSettings\
dotnet user-secrets set "Jwt:SignInKey" $env:JWT_SIGNINKEY --project .\Src\FoodDesire.AppSettings\
shell: pwsh

- name: Build
run: dotnet build Src/FoodDesire.sln --configuration ${{ matrix.configuration }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Set up .NET Core
- name: Set up .NET 7
uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.x'
include-prerelease: true

- name: Setting Environment Variables
- name: Setting User Secrets
env:
APPSETTINGS_PRODUCTION: ${{ secrets.APPSETTINGS_PRODUCTION }}
CONNECTIONSTRINGS_DEFAULTCONNECTION: ${{ secrets.USERSECRETS_CONNECTIONSTRINGS_DEFAULTCONNECTION }}
CONNECTIONSTRINGS_STORAGECONNECTION: ${{ secrets.USERSECRETS_CONNECTIONSTRINGS_STORAGECONNECTION }}
CLIENTID: ${{ secrets.USERSECRETS_CLIENTID }}
JWT_SIGNINKEY: ${{ secrets.USERSECRETS_JWT_SIGNINKEY }}
run: |
Set-Content -Path "Src/FoodDesire.AppSettings/appsettings.Production.json" -Value $env:APPSETTINGS_PRODUCTION
Set-Content -Path "Src/FoodDesire.AppSettings/appsettings.Development.json" -Value $env:APPSETTINGS_PRODUCTION
dotnet user-secrets set "ConnectionStrings:DefaultConnection" $env:CONNECTIONSTRINGS_DEFAULTCONNECTION --project .\Src\FoodDesire.AppSettings\
dotnet user-secrets set "ConnectionStrings:StorageConnection" $env:CONNECTIONSTRINGS_STORAGECONNECTION --project .\Src\FoodDesire.AppSettings\
dotnet user-secrets set "ClientID" $env:CLIENTID --project .\Src\FoodDesire.AppSettings\
dotnet user-secrets set "Jwt:SignInKey" $env:JWT_SIGNINKEY --project .\Src\FoodDesire.AppSettings\
shell: pwsh

- name: Build with dotnet
Expand Down
29 changes: 18 additions & 11 deletions .github/workflows/production-winui3-fooddesire-dotnet-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
name: WinUI 3 MSIX app

on:
pull_request:
branches: [production-ims]
push:
branches:
- production-ims
workflow_dispatch:

jobs:
build:
Expand All @@ -25,19 +27,24 @@ jobs:
with:
fetch-depth: 0

- name: Setting Environment Variables
env:
APPSETTINGS_PRODUCTION: ${{ secrets.APPSETTINGS_PRODUCTION }}
run: |
Set-Content -Path "Src/FoodDesire.AppSettings/appsettings.Production.json" -Value $env:APPSETTINGS_PRODUCTION
Set-Content -Path "Src/FoodDesire.AppSettings/appsettings.Development.json" -Value $env:APPSETTINGS_PRODUCTION
shell: pwsh

- name: Install .NET Core
- name: Install .NET 7
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.0.x

- name: Setting User Secrets
env:
CONNECTIONSTRINGS_DEFAULTCONNECTION: ${{ secrets.USERSECRETS_CONNECTIONSTRINGS_DEFAULTCONNECTION }}
CONNECTIONSTRINGS_STORAGECONNECTION: ${{ secrets.USERSECRETS_CONNECTIONSTRINGS_STORAGECONNECTION }}
CLIENTID: ${{ secrets.USERSECRETS_CLIENTID }}
JWT_SIGNINKEY: ${{ secrets.USERSECRETS_JWT_SIGNINKEY }}
run: |
dotnet user-secrets set "ConnectionStrings:DefaultConnection" $env:CONNECTIONSTRINGS_DEFAULTCONNECTION --project .\Src\FoodDesire.AppSettings\
dotnet user-secrets set "ConnectionStrings:StorageConnection" $env:CONNECTIONSTRINGS_STORAGECONNECTION --project .\Src\FoodDesire.AppSettings\
dotnet user-secrets set "ClientID" $env:CLIENTID --project .\Src\FoodDesire.AppSettings\
dotnet user-secrets set "Jwt:SignInKey" $env:JWT_SIGNINKEY --project .\Src\FoodDesire.AppSettings\
shell: pwsh

- name: Setup MSBuild.exe
uses: microsoft/setup-msbuild@v1.0.2

Expand Down
14 changes: 3 additions & 11 deletions Src/FoodDesire.AppSettings/Configure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@

namespace FoodDesire.AppSettings;
public static class Configure {
public static void ConfigureEnvironment(IConfigurationBuilder config, string environmentName) {
try {
config.AddJsonFile($"appsettings.{environmentName}.json", optional: false, reloadOnChange: true);
} catch {
throw new Exception($"Configure FoodDesire.AppSettings/appsettings.{environmentName}.json.");
}
config.AddEnvironmentVariables();

IConfigurationRoot? _config = config.Build();
AppSettingsValidator.Validate(_config);
_config = null;
public static void ConfigureEnvironment(IConfigurationBuilder config) {
config.AddUserSecrets<Program>();
AppSettingsValidator.Validate(config);
}
}
12 changes: 3 additions & 9 deletions Src/FoodDesire.AppSettings/FoodDesire.AppSettings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
</PropertyGroup>
<UserSecretsId>e61839ca-880d-4230-8bb8-d7955de81dd2</UserSecretsId>
<OutputType>Library</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.Development.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="appsettings.Production.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
16 changes: 8 additions & 8 deletions Src/FoodDesire.AppSettings/Helpers/AppSettingsValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

namespace FoodDesire.AppSettings.Helpers;
public static class AppSettingsValidator {
public static void Validate(IConfiguration config) {
public static void Validate(IConfigurationBuilder config) {
IConfigurationRoot? _config = config.Build();
bool invalidAppSettings = false;
List<string> errors = new();
if (config.GetConnectionString("DefaultConnection") == null) {
if (_config.GetConnectionString("DefaultConnection") == null) {
invalidAppSettings = true;
errors.Add("Missing ConnectionStrings:DefaultConnection");
}

if (config.GetConnectionString("StorageConnection") == null) {
if (_config.GetConnectionString("StorageConnection") == null) {
invalidAppSettings = true;
errors.Add("Missing ConnectionStrings:StorageConnection");
}

if (config["ClientId"] == null) {
if (_config["ClientId"] == null) {
invalidAppSettings = true;
errors.Add("Missing ConnectionStrings:ClientId");
}

if (config.GetSection("JWT")["SignInKey"] == null) {
if (_config.GetSection("JWT")["SignInKey"] == null) {
invalidAppSettings = true;
errors.Add("Missing JWT:SignInKey");
}

_config = null;
if (!invalidAppSettings) return;
string environmentName = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
string message = $"Missing elements in FoodDesire.AppSettings/appsettings.{environmentName}.json.\n";
string message = $"Missing User Secrets. Use 'dotnet user-secrets set' to configure user secrets.\n";
message = $"{message}{string.Join("\n", errors)}";
throw new Exception(message);
}
Expand Down
2 changes: 2 additions & 0 deletions Src/FoodDesire.AppSettings/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
namespace FoodDesire.AppSettings;
internal class Program { }
2 changes: 1 addition & 1 deletion Src/FoodDesire.DAL/Context/ApplicationDbContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ApplicationDbContext CreateDbContext(string[] args) {
environmentName = context.HostingEnvironment.EnvironmentName).Build();

ConfigurationBuilder? builder = new ConfigurationBuilder();
AppSettings.Configure.ConfigureEnvironment(builder, environmentName);
AppSettings.Configure.ConfigureEnvironment(builder);
IConfigurationRoot? config = builder.Build();

string connectionString = config.GetConnectionString("DefaultConnection")!;
Expand Down
2 changes: 1 addition & 1 deletion Src/FoodDesire.IMS/Helpers/AppConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
internal static class AppConfigurator {
internal static void Configure(HostBuilderContext context, IConfigurationBuilder config) {
string environmentName = context.HostingEnvironment.EnvironmentName;
AppSettings.Configure.ConfigureEnvironment(config, environmentName);
AppSettings.Configure.ConfigureEnvironment(config);
}

internal static void Configure(HostBuilderContext context, IServiceCollection services) {
Expand Down
2 changes: 1 addition & 1 deletion Src/FoodDesire.ML.Model/Helpers/AppConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace FoodDesire.ML.Model.Helpers;
internal static class AppConfigurator {
internal static void Configure(HostBuilderContext context, IConfigurationBuilder config) {
string environmentName = context.HostingEnvironment.EnvironmentName;
AppSettings.Configure.ConfigureEnvironment(config, environmentName);
AppSettings.Configure.ConfigureEnvironment(config);
}

internal static void Configure(HostBuilderContext context, IServiceCollection services) {
Expand Down
1 change: 0 additions & 1 deletion Src/FoodDesire.Web.API/FoodDesire.Web.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@
<ProjectReference Include="..\FoodDesire.DAL\FoodDesire.DAL.csproj" />
<ProjectReference Include="..\FoodDesire.ML.Model\FoodDesire.ML.Model.csproj" />
<ProjectReference Include="..\FoodDesire.Web.Client\FoodDesire.Web.Client.csproj" />
<ProjectReference Include="..\FoodDesire.Web.Shared\FoodDesire.Web.Shared.csproj" />
</ItemGroup>
</Project>
3 changes: 1 addition & 2 deletions Src/FoodDesire.Web.API/Helpers/AppConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace FoodDesire.Web.API.Helpers;
internal static class AppConfigurator {
internal static void Configure(WebApplicationBuilder builder) {
string environmentName = builder.Environment.EnvironmentName;
AppSettings.Configure.ConfigureEnvironment(builder.Configuration, environmentName);
AppSettings.Configure.ConfigureEnvironment(builder.Configuration);
ConfigureServices(builder);
}

Expand Down
2 changes: 1 addition & 1 deletion Src/FoodDesire.Web.API/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Production"
"DOTNET_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7271;http://localhost:5048"
Expand Down

0 comments on commit 0a0582c

Please sign in to comment.