This content originally appeared on DEV Community and was authored by Antonio Falcão Jr.
Always knowing the state of the application is a sign that the system is under control, but sometimes this is not trivial.
Depending on the environment, it is not a simple task to access the application settings, and making this information available in the wrong location can expose vulnerabilities.
With this simple approach is possible to define a complete and safely dump for configuration settings.
Route approache
Create an IEndpointRouteBuilder
extensions method to encapsulate details and guarantee the right environment:
public static class EndpointRouteBuilderExtensions
{
public static void MapDumpConfig(this IEndpointRouteBuilder endpoints, string pattern,
IConfigurationRoot configurationRoot, bool isProduction)
{
if (isProduction) return;
endpoints.MapGet(
pattern: pattern,
requestDelegate: context
=> context.Response.WriteAsync(
text: configurationRoot.GetDebugView(),
cancellationToken: context.RequestAborted));
}
}
As we can see, if the environment is the Production one, the configuration will be skipped.
Now, specify the middleware configuration details in Startup.cs
, Adding a RouteEndpoint
to the IEndpointRouteBuilder
with /dump-config
pattern:
public void Configure(IApplicationBuilder app)
{
app.UseEndpoints(
endpoints =>
{
endpoints.MapDumpConfig(
pattern: "/dump-config",
configurationRoot: _configuration as IConfigurationRoot,
isProduction: _env.IsProduction());
}
}
The resource will respond in http://localhost/dump-config
.
Log approache
If prefer, is it possible to deliver the dump for a log service. In this case, is not necessary the skip statement:
public static void MapDumpConfig(this IEndpointRouteBuilder endpoints, string pattern,
IConfigurationRoot configurationRoot, ILogger logger)
{
endpoints.MapGet(
pattern: pattern,
requestDelegate: context =>
{
logger.LogInformation("{Settings}", configurationRoot.GetDebugView());
return context.Response.WriteAsync(
text: "Configuration dumped successfully.",
cancellationToken: context.RequestAborted);
});
}
And then, propagate the log resource:
public void Configure(IApplicationBuilder app , ILoggerFactory loggerFactory)
{
app.UseEndpoints(
endpoints =>
{
endpoints.MapDumpConfig(
pattern: "/dump-config",
configurationRoot: _configuration as IConfigurationRoot,
logger: loggerFactory.CreateLogger<Startup>());
}
}
Conclusion
In this way, we can improve the developer experience given a simple way to check the actual state of the system.
This content originally appeared on DEV Community and was authored by Antonio Falcão Jr.
Antonio Falcão Jr. | Sciencx (2021-05-30T16:13:18+00:00) [Quick Tip] Dump the configuration easily and improve the developer experience.. Retrieved from https://www.scien.cx/2021/05/30/quick-tip-dump-the-configuration-easily-and-improve-the-developer-experience/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.