From 3407ce025c2a3e2a0615f638413f4c8626e4deed Mon Sep 17 00:00:00 2001 From: lucio Date: Thu, 26 Jan 2023 18:24:18 +0100 Subject: [PATCH] health objects added --- pom.xml | 8 +-- .../extensions/resource/HealthResource.java | 44 +++++++++++++ .../extensions/resource/RemoteResource.java | 4 +- .../gcube/smartgears/health/HealthCheck.java | 9 +++ .../health/KeyCloakHealthCheck.java | 25 +++++++ .../smartgears/health/ReadinessChecker.java | 12 ++++ .../org/gcube/smartgears/health/Status.java | 6 ++ .../health/response/HealthCheckResponse.java | 30 +++++++++ .../response/HealthCheckResponseBuilder.java | 65 +++++++++++++++++++ .../health/response/HealthResponse.java | 18 +++++ .../response/HealthResponseBuilder.java | 13 ++++ 11 files changed, 228 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/gcube/smartgears/extensions/resource/HealthResource.java create mode 100644 src/main/java/org/gcube/smartgears/health/HealthCheck.java create mode 100644 src/main/java/org/gcube/smartgears/health/KeyCloakHealthCheck.java create mode 100644 src/main/java/org/gcube/smartgears/health/ReadinessChecker.java create mode 100644 src/main/java/org/gcube/smartgears/health/Status.java create mode 100644 src/main/java/org/gcube/smartgears/health/response/HealthCheckResponse.java create mode 100644 src/main/java/org/gcube/smartgears/health/response/HealthCheckResponseBuilder.java create mode 100644 src/main/java/org/gcube/smartgears/health/response/HealthResponse.java create mode 100644 src/main/java/org/gcube/smartgears/health/response/HealthResponseBuilder.java diff --git a/pom.xml b/pom.xml index ca29b5e..37a5489 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ distro 8.0.42 2.25.1 - UTF-8 + UTF-8 1.8 1.8 @@ -117,11 +117,11 @@ 4.0.1 provided - + - javax.xml.bind - jaxb-api + javax.xml.bind + jaxb-api diff --git a/src/main/java/org/gcube/smartgears/extensions/resource/HealthResource.java b/src/main/java/org/gcube/smartgears/extensions/resource/HealthResource.java new file mode 100644 index 0000000..d3d5394 --- /dev/null +++ b/src/main/java/org/gcube/smartgears/extensions/resource/HealthResource.java @@ -0,0 +1,44 @@ +package org.gcube.smartgears.extensions.resource; + +import static org.gcube.smartgears.Constants.application_json; +import static org.gcube.smartgears.extensions.HttpExtension.Method.GET; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.gcube.smartgears.extensions.ApiResource; +import org.gcube.smartgears.extensions.ApiSignature; +import org.gcube.smartgears.health.response.HealthResponse; + +public class HealthResource extends ApiResource { + + private static final long serialVersionUID = 1L; + + public static final String mapping = "/health"; + + private static final ApiSignature signature = handles(mapping).with(method(GET).produces(application_json)); + + + HealthResource() { + super(signature); + } + + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + } + + + public HealthResponse liveness() { + return null; + } + + public HealthResponse readiness() { + return null; + } + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/smartgears/extensions/resource/RemoteResource.java b/src/main/java/org/gcube/smartgears/extensions/resource/RemoteResource.java index 475c1d8..f160cae 100644 --- a/src/main/java/org/gcube/smartgears/extensions/resource/RemoteResource.java +++ b/src/main/java/org/gcube/smartgears/extensions/resource/RemoteResource.java @@ -1,6 +1,6 @@ package org.gcube.smartgears.extensions.resource; -import static org.gcube.smartgears.Constants.*; +import static org.gcube.smartgears.Constants.remote_management; import javax.xml.bind.annotation.XmlRootElement; @@ -27,7 +27,7 @@ public class RemoteResource extends HttpController { public RemoteResource() { super(remote_management, default_mapping); addResources(new FrontPageResource(), new ConfigurationResource(), new ProfileResource(), - new LifecycleResource(), new MetricsResource()); + new LifecycleResource(), new MetricsResource(), new HealthResource()); } @Override diff --git a/src/main/java/org/gcube/smartgears/health/HealthCheck.java b/src/main/java/org/gcube/smartgears/health/HealthCheck.java new file mode 100644 index 0000000..b3f992b --- /dev/null +++ b/src/main/java/org/gcube/smartgears/health/HealthCheck.java @@ -0,0 +1,9 @@ +package org.gcube.smartgears.health; + +import org.gcube.smartgears.health.response.HealthCheckResponse; + +public interface HealthCheck { + + HealthCheckResponse check(); + +} diff --git a/src/main/java/org/gcube/smartgears/health/KeyCloakHealthCheck.java b/src/main/java/org/gcube/smartgears/health/KeyCloakHealthCheck.java new file mode 100644 index 0000000..15fd0aa --- /dev/null +++ b/src/main/java/org/gcube/smartgears/health/KeyCloakHealthCheck.java @@ -0,0 +1,25 @@ +package org.gcube.smartgears.health; + +import java.util.Set; + +import org.gcube.smartgears.health.response.HealthCheckResponse; +import org.gcube.smartgears.provider.ProviderFactory; + +@ReadinessChecker +public class KeyCloakHealthCheck implements HealthCheck{ + + private static final String CHECK_NAME = "authorizationCheck" ; + + @Override + public HealthCheckResponse check() { + try { + Set contexts = ProviderFactory.provider().containerContext().authorizationProvider().getContexts(); + if (contexts.isEmpty()) + return HealthCheckResponse.builder(CHECK_NAME).down().withMessage("no contexts are defined for this server").build(); + return HealthCheckResponse.builder(CHECK_NAME).up().build(); + }catch (Exception e) { + return HealthCheckResponse.builder(CHECK_NAME).down().withMessage(e.getMessage()).build(); + } + } + +} diff --git a/src/main/java/org/gcube/smartgears/health/ReadinessChecker.java b/src/main/java/org/gcube/smartgears/health/ReadinessChecker.java new file mode 100644 index 0000000..6483255 --- /dev/null +++ b/src/main/java/org/gcube/smartgears/health/ReadinessChecker.java @@ -0,0 +1,12 @@ +package org.gcube.smartgears.health; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface ReadinessChecker { + +} diff --git a/src/main/java/org/gcube/smartgears/health/Status.java b/src/main/java/org/gcube/smartgears/health/Status.java new file mode 100644 index 0000000..1ecaef1 --- /dev/null +++ b/src/main/java/org/gcube/smartgears/health/Status.java @@ -0,0 +1,6 @@ +package org.gcube.smartgears.health; + +public enum Status { + + UP, DOWN, NOT_CALCULATED +} diff --git a/src/main/java/org/gcube/smartgears/health/response/HealthCheckResponse.java b/src/main/java/org/gcube/smartgears/health/response/HealthCheckResponse.java new file mode 100644 index 0000000..33bcdf7 --- /dev/null +++ b/src/main/java/org/gcube/smartgears/health/response/HealthCheckResponse.java @@ -0,0 +1,30 @@ +package org.gcube.smartgears.health.response; + +import org.gcube.com.fasterxml.jackson.annotation.JsonInclude; +import org.gcube.com.fasterxml.jackson.annotation.JsonInclude.Include; +import org.gcube.common.validator.annotations.NotEmpty; +import org.gcube.common.validator.annotations.NotNull; +import org.gcube.smartgears.health.Status; + +@JsonInclude(Include.NON_NULL) +public class HealthCheckResponse { + + public static HealthCheckResponseBuilder builder(String checkName) { + return new HealthCheckResponseBuilder(checkName); + } + + protected HealthCheckResponse() {} + + //enum ErrorType {UNRECOVERABLE, RECOVERABLE} + + @NotNull + protected Status status; + + @NotNull @NotEmpty + protected String name; + + //protected ErrorType errorType; + + protected String errorMessage; + +} diff --git a/src/main/java/org/gcube/smartgears/health/response/HealthCheckResponseBuilder.java b/src/main/java/org/gcube/smartgears/health/response/HealthCheckResponseBuilder.java new file mode 100644 index 0000000..266e0a2 --- /dev/null +++ b/src/main/java/org/gcube/smartgears/health/response/HealthCheckResponseBuilder.java @@ -0,0 +1,65 @@ +package org.gcube.smartgears.health.response; + +import java.util.ArrayList; +import java.util.List; + +import org.gcube.common.validator.ValidationError; +import org.gcube.common.validator.Validator; +import org.gcube.common.validator.ValidatorFactory; +import org.gcube.smartgears.health.Status; + +public class HealthCheckResponseBuilder { + + private HealthCheckResponse healthCheckResponse = new HealthCheckResponse(); + + protected HealthCheckResponseBuilder(String name) { + healthCheckResponse.name = name; + } + + public BuildPart up() { + healthCheckResponse.status = Status.UP; + return new BuildPart(); + } + + public ErrorPart down() { + healthCheckResponse.status = Status.DOWN; + return new ErrorPart(); + } + + private void validateResponse() { + + List msgs = new ArrayList(); + + Validator validator = ValidatorFactory.validator(); + + for (ValidationError error : validator.validate(healthCheckResponse)) + msgs.add(error.toString()); + + if (!msgs.isEmpty()) + throw new IllegalStateException("invalid configuration: "+msgs); + + } + + public class ErrorPart{ + + private ErrorPart() {} + + public BuildPart withMessage(String message) { + healthCheckResponse.errorMessage = message; + return new BuildPart(); + } + + } + + public class BuildPart{ + + private BuildPart() {}; + + public HealthCheckResponse build() { + validateResponse(); + return healthCheckResponse; + } + + } + +} diff --git a/src/main/java/org/gcube/smartgears/health/response/HealthResponse.java b/src/main/java/org/gcube/smartgears/health/response/HealthResponse.java new file mode 100644 index 0000000..8321d1f --- /dev/null +++ b/src/main/java/org/gcube/smartgears/health/response/HealthResponse.java @@ -0,0 +1,18 @@ +package org.gcube.smartgears.health.response; + +import java.util.List; + +import org.gcube.smartgears.health.HealthCheck; +import org.gcube.smartgears.health.Status; + +public class HealthResponse { + + Status status; + + List checks; + + void register(HealthCheck check){ + checks.add(check); + } + +} diff --git a/src/main/java/org/gcube/smartgears/health/response/HealthResponseBuilder.java b/src/main/java/org/gcube/smartgears/health/response/HealthResponseBuilder.java new file mode 100644 index 0000000..4077c85 --- /dev/null +++ b/src/main/java/org/gcube/smartgears/health/response/HealthResponseBuilder.java @@ -0,0 +1,13 @@ +package org.gcube.smartgears.health.response; + +public class HealthResponseBuilder { + + private HealthResponse healthResponse; + + public HealthResponse build() { + return healthResponse; + } + + + +}