Skip to content

Commit

Permalink
Improve startup, allow disabling security
Browse files Browse the repository at this point in the history
  • Loading branch information
macsux committed Oct 22, 2021
1 parent 03c41aa commit 4370527
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
2 changes: 2 additions & 0 deletions DotnetAccelerator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ Global
{160A1C52-AF25-4593-B8C1-7A6B91E1AC84} = {DAAA1A53-C9A3-472A-9BF9-293FFB211440}
{05AF40AC-8C37-4C5D-8C93-E6B5DB0A3658} = {5E351270-3697-4CA3-9330-84D290EBE0E1}
{7C7A4D96-06EB-4767-94BD-515CDCA43E9A} = {5E351270-3697-4CA3-9330-84D290EBE0E1}
{4A39293F-B876-4AC8-9865-11F376FF36D8} = {D5B7E79B-4D03-4B94-B927-73CAEF204422}
{45E90901-4A67-4041-AABB-9A79D614D133} = {D5B7E79B-4D03-4B94-B927-73CAEF204422}
EndGlobalSection
EndGlobal
5 changes: 5 additions & 0 deletions config/application-Development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
Logging:
LogLevel:
Default: Debug
Microsoft: Debug
Steeltoe.Extensions.Configuration.ConfigServer: Warning

Spring:
Cloud:
Config:
Expand All @@ -15,6 +17,9 @@ Spring:
"user.name": admin
"user.password": admin
Management:
Endpoints:
Actuator:
EnableSecurity: false
Tracing:
Exporter:
Zipkin:
Expand Down
1 change: 1 addition & 0 deletions config/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Logging:
Microsoft.Hosting.Lifetime: Information
idunno: Error
Steeltoe: Information
Steeltoe.Management.Endpoint.Metrics.Observer.AspNetCoreHostingObserver: None
AllowedHosts: '*'
Management:
Endpoints:
Expand Down
11 changes: 11 additions & 0 deletions services/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ services:
image: steeltoeoss/spring-boot-admin
ports:
- 8088:8080

live-view:
image: registry.tanzu.vmware.com/app-live-view/application-live-view-sidecar:0.2.0
environment:
- app.live.view.sidecar.application-name=DotnetAccelerator
- app.live.view.sidecar.application-host=host.docker.internal
- app.live.view.sidecar.application-protocol=http
- app.live.view.sidecar.application-port=5000
- app.live.view.sidecar.actuator-path=/actuator
- app.live.view.sidecar.app-flavours=steeltoe
- app.live.view.client.host=host.docker.internal

volumes:
postgresql:
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class ActuatorSecurityOptions
private static string GenerateSecret() => Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Trim('=');
public string UserName { get; set; } = _defaultUsername.Value;
public string Password { get; set; } = _defaultPassword.Value;
public bool Enabled { get; set; } = true;
}
}
32 changes: 23 additions & 9 deletions src/MyProjectGroup.Common/Security/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,24 @@ public SpringBootAdminSafeStartupHostedService(IServiceProvider serviceProvider,
_springBootAdminHostedService = (IHostedService)ActivatorUtilities.CreateInstance(serviceProvider, type);
}

public async Task StartAsync(CancellationToken cancellationToken)
public Task StartAsync(CancellationToken cancellationToken)
{
if (_springBootAdminHostedService == null)
return;
try
if (_springBootAdminHostedService != null)
{
await _springBootAdminHostedService.StartAsync(cancellationToken);
}
catch (Exception)
{
_logger.LogWarning($"Can't connect to Spring Boot Admin at {_options.Url}");
Task.Run(async () =>
{
try
{
await _springBootAdminHostedService.StartAsync(cancellationToken);
_logger.LogInformation("Successfully registered with Spring Boot Admin at {Url}", _options.Url);
}
catch (Exception)
{
_logger.LogWarning("Can't connect to Spring Boot Admin at {Url}", _options.Url);
}
}, cancellationToken);
}
return Task.CompletedTask;
}

public async Task StopAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -109,9 +115,12 @@ public static IServiceCollection AddSecureActuators(this IServiceCollection serv
{
options.Password = password;
}

options.Enabled = config.GetValue<bool?>("Management:Endpoints:Actuator:EnableSecurity") ?? true;
});
services.AddAuthentication().AddBasic(BasicAuthenticationDefaults.AuthenticationScheme, options =>
{
options.AllowInsecureProtocol = true;
options.Events = new BasicAuthenticationEvents
{
OnValidateCredentials = context =>
Expand All @@ -135,6 +144,11 @@ public static IServiceCollection AddSecureActuators(this IServiceCollection serv
.RequireAssertion(context =>
{
var httpContext = (HttpContext) context.Resource!;
var options = httpContext.RequestServices.GetRequiredService<IOptionsSnapshot<ActuatorSecurityOptions>>().Value;
if (!options.Enabled)
{
return true;
}
var actuatorEndpoints = httpContext.RequestServices.GetServices<IEndpointOptions>()
.Where(x => x.Id is not "health" and not "info")
.Select(x => ((PathString) $"/actuator").Add($"/{x.Id}"))
Expand Down

0 comments on commit 4370527

Please sign in to comment.