Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/healthchecks #57

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
tst-sia marked this conversation as resolved.
Show resolved Hide resolved
public void ConfigureHealthChecks_Use9000ForHealth()
{
var config = new KeycloakPluginConfiguration
{
Enabled = true,
Host = "example.com",
Insecure = false,
Port = 8080,
HealthCheckConfig = new HealthCheckConfig
{
Host = "example.com",
tst-sia marked this conversation as resolved.
Show resolved Hide resolved
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
Loading