Skip to content

Commit

Permalink
Merge pull request #57 from sia-digital/feature/Healthchecks
Browse files Browse the repository at this point in the history
Feature/healthchecks
  • Loading branch information
tst-sia authored Sep 27, 2024
2 parents 46e5246 + fdca68e commit 3149ba1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
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.GetAuthority()) { Path = $"{_keycloakPluginConfiguration.Realms.Prefix.TrimEnd('/')}/master" };
var uriBuilder = new UriBuilder(_keycloakPluginConfiguration.GetHealthCheck()) { Path = _keycloakPluginConfiguration.HealthCheckConfig.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,6 +15,16 @@ 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 Uri GetHealthCheck()
{
if (string.IsNullOrEmpty(HealthCheckConfig.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;
}

public Uri GetAuthority()
{
Expand All @@ -30,4 +40,12 @@ public class RealmsConfig
public string Prefix { get; set; } = "/auth/realms";
public string Default { get; set; } = "master";
}

public class HealthCheckConfig
{
public string Host { get; set; } = "example.com";
public int? Port { get; set; } = 9000;
public string Prefix { get; set; } = "/health/ready";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,79 @@ public void TestRedirectUriHttpToHttpsReplace(string uri, string expected)
KeycloakDefaults.BuildCorrectRedirectUri(uri).Should().Be(expected);
}

[Test]
public void ConfigureHealthChecks_Use9000ForHealth()
{
var config = new KeycloakPluginConfiguration
{
Enabled = true,
Host = "example.com",
Insecure = false,
Port = 8080,
HealthCheckConfig = new HealthCheckConfig
{
Host = "example.com",
Port = 9000,
Prefix = "/health/ready"
}
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheckConfig.Prefix };
uriBuilder.Uri.Should().Be("https://example.com:9000/health/ready");
}

[Test]
public void ConfigureHealthChecks_WithoutSettingHealthCheckConfig()
{
var config = new KeycloakPluginConfiguration
{
Enabled = true,
Host = "example.com",
Insecure = false,
Port = 8080,
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheckConfig.Prefix };
uriBuilder.Uri.Should().Be("https://example.com:9000/health/ready");
}

[Test]
public void ConfigureHealthChecks_DifferentPrefixAndPort()
{
var config = new KeycloakPluginConfiguration
{
Enabled = true,
Host = "newhost.com",
Insecure = false,
Port = 8080,
HealthCheckConfig = new HealthCheckConfig
{
Host = "health.com",
Port = 9999,
Prefix = "/something/notready"
}
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheckConfig.Prefix };
uriBuilder.Uri.Should().Be("https://health.com:9999/something/notready");
}

[Test]
public void ConfigureHealthChecks_DefaultHealthHost()
{
var config = new KeycloakPluginConfiguration
{
Enabled = true,
Host = "newhost.com",
Insecure = false,
Port = 8080,
HealthCheckConfig = new HealthCheckConfig
{
Port = 9999,
Prefix = "/something/notready"
}
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheckConfig.Prefix };
uriBuilder.Uri.Should().Be("https://example.com:9999/something/notready");
}

private static void AssertMiddleware<TMiddleware>(ICall call)
{
var func = (call.GetOriginalArguments()[0] as Func<RequestDelegate, RequestDelegate>)?.Target;
Expand Down

0 comments on commit 3149ba1

Please sign in to comment.