Skip to content

Commit

Permalink
Merge pull request #59 from sia-digital/feature/keycloak-healthchecks
Browse files Browse the repository at this point in the history
add Insecure option for HealthCheck and update tests accordingly
  • Loading branch information
tst-sia authored Oct 14, 2024
2 parents 5c21666 + 17d80a5 commit 122e198
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class KeycloakPluginConfiguration
public Uri GetHealthCheck()
{
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();
var httpScheme = (HealthCheck.Insecure ? HttpScheme.Http : HttpScheme.Https).ToString();
return Port.HasValue
? new UriBuilder(httpScheme, HealthCheck.Host, HealthCheck.Port.Value).Uri
: new UriBuilder(httpScheme, HealthCheck.Host).Uri;
Expand All @@ -43,6 +43,7 @@ public class RealmsConfig

public class HealthCheckConfig
{
public bool Insecure { get; set; } = true;
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 @@ -127,6 +127,47 @@ public void ConfigureHealthChecks_Use9000ForHealth()
}
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheck.Prefix };
uriBuilder.Uri.Should().Be("http://example.com:9000/health/ready");
}

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

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

Expand All @@ -146,7 +187,7 @@ public void ConfigureHealthChecks_WithSettingHealthCheckHost()
};

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

[Test]
Expand All @@ -166,7 +207,7 @@ public void ConfigureHealthChecks_DifferentPrefixAndPort()
}
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheck.Prefix };
uriBuilder.Uri.Should().Be("https://health.com:9999/something/notready");
uriBuilder.Uri.Should().Be("http://health.com:9999/something/notready");
}

[Test]
Expand All @@ -186,7 +227,7 @@ public void ConfigureHealthChecks_DefaultHealthHost()
}
};
var uriBuilder = new UriBuilder(config.GetHealthCheck()) { Path = config.HealthCheck.Prefix };
uriBuilder.Uri.Should().Be("https://example.com:9999/something/notready");
uriBuilder.Uri.Should().Be("http://example.com:9999/something/notready");
}

private static void AssertMiddleware<TMiddleware>(ICall call)
Expand Down

0 comments on commit 122e198

Please sign in to comment.