Skip to content

Commit

Permalink
Merge pull request #58 from sia-digital/feature/Healthchecks
Browse files Browse the repository at this point in the history
Default Host removed
  • Loading branch information
apurkovic authored Oct 1, 2024
2 parents 3149ba1 + 9316198 commit 5c21666
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void ConfigureApplication(IApplicationBuilder applicationBuilder)

public void ConfigureHealthChecks(IHealthChecksBuilder healthChecksBuilder)
{
var uriBuilder = new UriBuilder(_keycloakPluginConfiguration.GetHealthCheck()) { Path = _keycloakPluginConfiguration.HealthCheckConfig.Prefix };
var uriBuilder = new UriBuilder(_keycloakPluginConfiguration.GetHealthCheck()) { Path = _keycloakPluginConfiguration.HealthCheck.Prefix };
var uri = uriBuilder.Uri;
healthChecksBuilder.AddUrlGroup(uri, "keycloak", HealthStatus.Unhealthy, new[] { HealthCheckTag.Readiness.Value });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public class KeycloakPluginConfiguration
public string ClientSecret { get; set; }
public RealmsConfig Realms { get; set; } = new RealmsConfig();
public IList<AuthPolicy> Policies { get; set; } = new List<AuthPolicy>();
public HealthCheckConfig HealthCheckConfig { get; set; } = new HealthCheckConfig();
public HealthCheckConfig HealthCheck { get; set; } = new HealthCheckConfig();

public Uri GetHealthCheck()
{
if (string.IsNullOrEmpty(HealthCheckConfig.Host)) throw new ArgumentException("Keycloak.Uri was not specified but health check is enabled!");
if (string.IsNullOrEmpty(HealthCheck.Host)) throw new ArgumentException("Keycloak.Uri was not specified but health check is enabled!");
var httpScheme = (Insecure ? HttpScheme.Http : HttpScheme.Https).ToString();
return Port.HasValue
? new UriBuilder(httpScheme, HealthCheckConfig.Host, HealthCheckConfig.Port.Value).Uri
: new UriBuilder(httpScheme, HealthCheckConfig.Host).Uri;
? new UriBuilder(httpScheme, HealthCheck.Host, HealthCheck.Port.Value).Uri
: new UriBuilder(httpScheme, HealthCheck.Host).Uri;
}

public Uri GetAuthority()
Expand All @@ -43,7 +43,7 @@ public class RealmsConfig

public class HealthCheckConfig
{
public string Host { get; set; } = "example.com";
public string Host { get; set; }
public int? Port { get; set; } = 9000;
public string Prefix { get; set; } = "/health/ready";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void ConfigureHealthChecksWorks()
{
var hcBuilder = Substitute.For<IHealthChecksBuilder>();
hcBuilder.Services.Returns(new ServiceCollection());
var config = new KeycloakPluginConfiguration { Enabled = true, Host = "example.com", Insecure = false };
var config = new KeycloakPluginConfiguration { Enabled = true, Host = "example.com", Insecure = false, HealthCheck = new HealthCheckConfig { Host = "example.com" } };
GetPlugin(config).ConfigureHealthChecks(hcBuilder);
hcBuilder.Received()
.Add(Arg.Is<HealthCheckRegistration>(h =>
Expand All @@ -119,28 +119,33 @@ public void ConfigureHealthChecks_Use9000ForHealth()
Host = "example.com",
Insecure = false,
Port = 8080,
HealthCheckConfig = new HealthCheckConfig
HealthCheck = new HealthCheckConfig
{
Host = "example.com",
Port = 9000,
Prefix = "/health/ready"
}
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheckConfig.Prefix };
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheck.Prefix };
uriBuilder.Uri.Should().Be("https://example.com:9000/health/ready");
}

[Test]
public void ConfigureHealthChecks_WithoutSettingHealthCheckConfig()
public void ConfigureHealthChecks_WithSettingHealthCheckHost()
{
var config = new KeycloakPluginConfiguration
{
Enabled = true,
Host = "example.com",
Insecure = false,
Port = 8080,
HealthCheck = new HealthCheckConfig
{
Host = "example.com"
}
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheckConfig.Prefix };

var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheck.Prefix };
uriBuilder.Uri.Should().Be("https://example.com:9000/health/ready");
}

Expand All @@ -153,14 +158,14 @@ public void ConfigureHealthChecks_DifferentPrefixAndPort()
Host = "newhost.com",
Insecure = false,
Port = 8080,
HealthCheckConfig = new HealthCheckConfig
HealthCheck = new HealthCheckConfig
{
Host = "health.com",
Port = 9999,
Prefix = "/something/notready"
}
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheckConfig.Prefix };
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheck.Prefix };
uriBuilder.Uri.Should().Be("https://health.com:9999/something/notready");
}

Expand All @@ -173,13 +178,14 @@ public void ConfigureHealthChecks_DefaultHealthHost()
Host = "newhost.com",
Insecure = false,
Port = 8080,
HealthCheckConfig = new HealthCheckConfig
HealthCheck = new HealthCheckConfig
{
Port = 9999,
Prefix = "/something/notready"
Prefix = "/something/notready",
Host = "example.com"
}
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheckConfig.Prefix };
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheck.Prefix };
uriBuilder.Uri.Should().Be("https://example.com:9999/something/notready");
}

Expand Down

0 comments on commit 5c21666

Please sign in to comment.