Skip to content

Commit 44b40ce

Browse files
authored
Fix(config): Env vars should result in settings as locked in UI/API (#1690)
1 parent f0a51fc commit 44b40ce

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

Rnwood.Smtp4dev/Controllers/ServerController.cs

+40-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
using Microsoft.EntityFrameworkCore.Metadata.Internal;
1818
using DeepEqual.Syntax;
1919
using System.Text.Json.Serialization.Metadata;
20+
using Microsoft.Extensions.Configuration.EnvironmentVariables;
21+
using Microsoft.Extensions.Configuration;
22+
using CommandLiners;
2023

2124
namespace Rnwood.Smtp4dev.Controllers
2225
{
@@ -25,7 +28,7 @@ namespace Rnwood.Smtp4dev.Controllers
2528
public class ServerController : Controller
2629
{
2730
public ServerController(ISmtp4devServer server, ImapServer imapServer, IOptionsMonitor<ServerOptions> serverOptions,
28-
IOptionsMonitor<RelayOptions> relayOptions, IOptionsMonitor<DesktopOptions> desktopOptions, CommandLineOptions cmdLineOptions, IHostingEnvironmentHelper hostingEnvironmentHelper)
31+
IOptionsMonitor<RelayOptions> relayOptions, IOptionsMonitor<DesktopOptions> desktopOptions, MapOptions<CommandLineOptions> commandLineOptions, CommandLineOptions cmdLineOptions, IHostingEnvironmentHelper hostingEnvironmentHelper)
2932
{
3033
this.server = server;
3134
this.imapServer = imapServer;
@@ -34,6 +37,7 @@ public ServerController(ISmtp4devServer server, ImapServer imapServer, IOptionsM
3437
this.desktopOptions = desktopOptions;
3538
this.hostingEnvironmentHelper = hostingEnvironmentHelper;
3639
this.cmdLineOptions = cmdLineOptions;
40+
this.commandLineOptions = commandLineOptions;
3741
}
3842

3943
private readonly ISmtp4devServer server;
@@ -43,6 +47,7 @@ public ServerController(ISmtp4devServer server, ImapServer imapServer, IOptionsM
4347
private readonly IOptionsMonitor<DesktopOptions> desktopOptions;
4448
private readonly IHostingEnvironmentHelper hostingEnvironmentHelper;
4549
private readonly CommandLineOptions cmdLineOptions;
50+
private readonly MapOptions<CommandLineOptions> commandLineOptions;
4651

4752
/// <summary>
4853
/// Gets the current state and settings for the smtp4dev server.
@@ -124,34 +129,56 @@ private IDictionary<string, string> GetLockedSettings(bool toJsonCasing)
124129
}
125130
else
126131
{
127-
if (this.hostingEnvironmentHelper.IsRunningInContainer())
132+
var fixedConfigSources = new ConfigurationBuilder().AddEnvironmentVariables()
133+
.AddCommandLineOptions(commandLineOptions).Build();
134+
135+
var fixedConfig = new SettingsFile
128136
{
129-
lockedSettings.Add(nameof(ApiModel.Server.Port), "Running in a container. Change this port mapping in the container host.");
130-
lockedSettings.Add(nameof(ApiModel.Server.ImapPort), "Running in a container. Change this port mapping in the container host.");
131-
lockedSettings.Add(nameof(ApiModel.Server.AllowRemoteConnections), "Running in a container. Change this port mapping in the container host.");
137+
ServerOptions = fixedConfigSources.GetSection("ServerOptions").Get<ServerOptionsSource>(),
138+
DesktopOptions = fixedConfigSources.GetSection("DesktopOptions").Get<DesktopOptionsSource>(),
139+
RelayOptions = fixedConfigSources.GetSection("RelayOptions").Get<RelayOptionsSource>()
140+
};
141+
142+
if (fixedConfig.ServerOptions != null)
143+
{
144+
foreach (var p in fixedConfig.ServerOptions.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
145+
{
146+
if (p.GetValue(fixedConfig.ServerOptions) != null)
147+
{
148+
lockedSettings[p.Name] = "Specified using command line option or environment variable";
149+
}
150+
}
132151
}
133152

134-
if (cmdLineOptions.ServerOptions != null)
153+
if (fixedConfig.RelayOptions != null)
135154
{
136-
foreach (var p in cmdLineOptions.ServerOptions.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
155+
foreach (var p in fixedConfig.RelayOptions.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
137156
{
138-
if (p.GetValue(cmdLineOptions.ServerOptions) != null)
157+
if (p.GetValue(fixedConfig.RelayOptions) != null)
139158
{
140-
lockedSettings[p.Name] = "Specified using command line option";
159+
lockedSettings["Relay" + p.Name] = "Specified using command line option or environment variable";
141160
}
142161
}
143162
}
144163

145-
if (cmdLineOptions.RelayOptions != null)
164+
if (fixedConfig.DesktopOptions != null)
146165
{
147-
foreach (var p in cmdLineOptions.RelayOptions.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
166+
foreach (var p in fixedConfig.DesktopOptions.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
148167
{
149-
if (p.GetValue(cmdLineOptions.RelayOptions) != null)
168+
if (p.GetValue(fixedConfig.DesktopOptions) != null)
150169
{
151-
lockedSettings["Relay" + p.Name] = "Specified using command line option";
170+
lockedSettings["Desktop" + p.Name] = "Specified using command line option or environment variable";
152171
}
153172
}
154173
}
174+
175+
176+
if (this.hostingEnvironmentHelper.IsRunningInContainer())
177+
{
178+
lockedSettings.Add(nameof(ApiModel.Server.Port), "Running in a container. Change this port mapping in the container host.");
179+
lockedSettings.Add(nameof(ApiModel.Server.ImapPort), "Running in a container. Change this port mapping in the container host.");
180+
lockedSettings.Add(nameof(ApiModel.Server.AllowRemoteConnections), "Running in a container. Change this port mapping in the container host.");
181+
}
155182
}
156183

157184
if (toJsonCasing)

Rnwood.Smtp4dev/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ private static IHost BuildWebHost(string[] args, CommandLineOptions cmdLineOptio
230230
}
231231

232232
services.AddSingleton(cmdLineOptions);
233+
services.AddSingleton(commandLineOptions);
233234
services.AddHostedService(sp => (Smtp4devServer)sp.GetRequiredService<ISmtp4devServer>());
234235
services.AddHostedService(sp => sp.GetRequiredService<ImapServer>());
235236
});

logo-dark.svg

+1-1
Loading

0 commit comments

Comments
 (0)