|
| 1 | +using Altinn.App.Core.Configuration; |
1 | 2 | using Altinn.App.Core.Extensions; |
2 | 3 | using Altinn.App.Core.Features.Maskinporten.Extensions; |
| 4 | +using Microsoft.Extensions.Configuration.Json; |
| 5 | +using Microsoft.Extensions.FileProviders; |
3 | 6 |
|
4 | 7 | namespace Altinn.App.Api.Extensions; |
5 | 8 |
|
@@ -29,19 +32,111 @@ public static void ConfigureAppWebHost(this IWebHostBuilder builder, string[] ar |
29 | 32 |
|
30 | 33 | configBuilder.AddInMemoryCollection(config); |
31 | 34 |
|
| 35 | + var runtimeSecretsDirectory = context.Configuration["AppSettings:RuntimeSecretsDirectory"]; |
| 36 | + if (string.IsNullOrWhiteSpace(runtimeSecretsDirectory)) |
| 37 | + { |
| 38 | + runtimeSecretsDirectory = AppSettings.DefaultRuntimeSecretsDirectory; |
| 39 | + } |
| 40 | + |
32 | 41 | configBuilder.AddMaskinportenSettingsFile( |
33 | 42 | context, |
34 | 43 | "MaskinportenSettingsFilepath", |
35 | | - "/mnt/app-secrets/maskinporten-settings.json" |
| 44 | + Path.Join(runtimeSecretsDirectory, "maskinporten-settings.json") |
36 | 45 | ); |
37 | 46 | configBuilder.AddMaskinportenSettingsFile( |
38 | 47 | context, |
39 | 48 | "MaskinportenSettingsInternalFilepath", |
40 | | - "/mnt/app-secrets/maskinporten-settings-internal.json" |
| 49 | + Path.Join(runtimeSecretsDirectory, "maskinporten-settings-internal.json") |
41 | 50 | ); |
42 | 51 |
|
| 52 | + configBuilder.AddRuntimeConfigFiles(context.HostingEnvironment, runtimeSecretsDirectory); |
43 | 53 | configBuilder.LoadAppConfig(args); |
44 | 54 | } |
45 | 55 | ); |
46 | 56 | } |
| 57 | + |
| 58 | + private static void AddRuntimeConfigFiles( |
| 59 | + this IConfigurationBuilder configBuilder, |
| 60 | + IHostEnvironment hostEnvironment, |
| 61 | + string secretsDirectory |
| 62 | + ) |
| 63 | + { |
| 64 | + if (hostEnvironment.IsDevelopment()) |
| 65 | + { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + ArgumentException.ThrowIfNullOrWhiteSpace(secretsDirectory); |
| 70 | + const string overrideFileNameFragment = "override"; |
| 71 | + if (!Directory.Exists(secretsDirectory)) |
| 72 | + { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + string[] jsonFiles = Directory.GetFiles(secretsDirectory, "*.json", SearchOption.TopDirectoryOnly); |
| 77 | + Array.Sort(jsonFiles, StringComparer.OrdinalIgnoreCase); |
| 78 | + |
| 79 | + PhysicalFileProvider? secretsFileProvider = null; |
| 80 | + HashSet<string> existingJsonFilePaths = []; |
| 81 | + |
| 82 | + foreach (JsonConfigurationSource source in configBuilder.Sources.OfType<JsonConfigurationSource>()) |
| 83 | + { |
| 84 | + if (source.FileProvider is null || string.IsNullOrWhiteSpace(source.Path)) |
| 85 | + { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + string? existingJsonFilePath = source.FileProvider.GetFileInfo(source.Path).PhysicalPath; |
| 90 | + if (string.IsNullOrWhiteSpace(existingJsonFilePath)) |
| 91 | + { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + existingJsonFilePaths.Add(Path.GetFullPath(existingJsonFilePath)); |
| 96 | + } |
| 97 | + |
| 98 | + foreach (string jsonFile in jsonFiles) |
| 99 | + { |
| 100 | + string jsonFilePath = Path.GetFullPath(jsonFile); |
| 101 | + if (existingJsonFilePaths.Contains(jsonFilePath)) |
| 102 | + { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + string jsonFileName = Path.GetFileName(jsonFile); |
| 107 | + if (jsonFileName.Contains(overrideFileNameFragment, StringComparison.OrdinalIgnoreCase)) |
| 108 | + { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + configBuilder.AddJsonFile( |
| 113 | + provider: secretsFileProvider ??= new PhysicalFileProvider(secretsDirectory), |
| 114 | + path: jsonFileName, |
| 115 | + optional: true, |
| 116 | + reloadOnChange: true |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + foreach (string jsonFile in jsonFiles) |
| 121 | + { |
| 122 | + string jsonFilePath = Path.GetFullPath(jsonFile); |
| 123 | + if (existingJsonFilePaths.Contains(jsonFilePath)) |
| 124 | + { |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + string jsonFileName = Path.GetFileName(jsonFile); |
| 129 | + if (!jsonFileName.Contains(overrideFileNameFragment, StringComparison.OrdinalIgnoreCase)) |
| 130 | + { |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + configBuilder.AddJsonFile( |
| 135 | + provider: secretsFileProvider ??= new PhysicalFileProvider(secretsDirectory), |
| 136 | + path: jsonFileName, |
| 137 | + optional: true, |
| 138 | + reloadOnChange: true |
| 139 | + ); |
| 140 | + } |
| 141 | + } |
47 | 142 | } |
0 commit comments