Skip to content

Commit

Permalink
Health check for OCI vaults (#3299)
Browse files Browse the repository at this point in the history
* Initial version of health check for single OCI vault.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Some improvement to vault healthcheck. Updated CDI sample to use it.

* Updated reactive vault example. Some other changes to support the reactive case.

* Updated pom project name.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Fixed checkstyle.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
  • Loading branch information
spericas authored Aug 26, 2021
1 parent 47c48c7 commit 2381396
Show file tree
Hide file tree
Showing 12 changed files with 362 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,11 @@
<artifactId>helidon-integrations-oci-vault</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.integrations.oci</groupId>
<artifactId>helidon-integrations-oci-vault-health</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.integrations.oci</groupId>
<artifactId>helidon-integrations-oci-cdi</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions examples/integrations/oci/vault-cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<groupId>io.helidon.integrations.oci</groupId>
<artifactId>helidon-integrations-oci-vault</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.integrations.oci</groupId>
<artifactId>helidon-integrations-oci-vault-health</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml-mp</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ oci:
vault:
# named OCI configuration
cryptographic-endpoint: "${oci.properties.cryptographic-endpoint}"
vault:
vault-ocid: "${oci.properties.vault-ocid}" # Vault healthcheck property

4 changes: 4 additions & 0 deletions examples/integrations/oci/vault-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.integrations.oci</groupId>
<artifactId>helidon-integrations-oci-vault-health</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import io.helidon.common.LogConfig;
import io.helidon.config.Config;
import io.helidon.health.HealthSupport;
import io.helidon.integrations.oci.vault.OciVaultRx;
import io.helidon.integrations.oci.vault.health.OciVaultHealthCheck;
import io.helidon.webserver.Routing;
import io.helidon.webserver.WebServer;

Expand Down Expand Up @@ -57,9 +59,18 @@ public static void main(String[] args) {
// ~/.oci/config
OciVaultRx ociVault = OciVaultRx.create(config.get("oci"));

// setup vault health check
HealthSupport health = HealthSupport.builder()
.addLiveness(OciVaultHealthCheck.builder()
.vaultId(vaultOcid)
.ociVault(ociVault)
.build())
.build();

WebServer.builder()
.config(config.get("server"))
.routing(Routing.builder()
.register(health)
.register("/vault", new VaultService(ociVault,
vaultOcid,
compartmentOcid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
*/
module io.helidon.examples.integrations.oci.vault.reactive {
requires io.helidon.integrations.oci.vault;
requires io.helidon.integrations.oci.vault.health;
requires io.helidon.webserver;
requires io.helidon.health;

exports io.helidon.examples.integrations.oci.vault.reactive;
}
1 change: 1 addition & 0 deletions integrations/oci/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<module>objectstorage</module>
<module>objectstorage-health</module>
<module>vault</module>
<module>vault-health</module>
<module>cdi</module>
</modules>
</project>
84 changes: 84 additions & 0 deletions integrations/oci/vault-health/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2021 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.integrations.oci</groupId>
<artifactId>helidon-integrations-oci-project</artifactId>
<version>2.4.0-SNAPSHOT</version>
</parent>

<artifactId>helidon-integrations-oci-vault-health</artifactId>
<name>Helidon Integrations OCI Vault Health Check</name>

<description>OCI Vault Health Support</description>

<dependencies>
<dependency>
<groupId>io.helidon.integrations.oci</groupId>
<artifactId>helidon-integrations-oci-vault</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.health</groupId>
<artifactId>helidon-health</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.health</groupId>
<artifactId>helidon-health-common</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!-- only needed for compilation, not required in runtime -->
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<!-- only needed for compilation, not required in runtime -->
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.oci.vault.health;

import java.util.Objects;
import java.util.logging.Logger;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import io.helidon.config.Config;
import io.helidon.health.common.BuiltInHealthCheck;
import io.helidon.integrations.common.rest.ApiOptionalResponse;
import io.helidon.integrations.oci.vault.GetVault;
import io.helidon.integrations.oci.vault.OciVault;
import io.helidon.integrations.oci.vault.OciVaultRx;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
import org.eclipse.microprofile.health.Liveness;

import static io.helidon.common.http.Http.Status.OK_200;

/**
* Liveness check for an OCI's Vault. Reads OCI properties from '~/.oci/config'.
*/
@Liveness
@ApplicationScoped // this will be ignored if not within CDI
@BuiltInHealthCheck
public final class OciVaultHealthCheck implements HealthCheck {
private static final Logger LOGGER = Logger.getLogger(OciVaultHealthCheck.class.getName());

private final String vaultId;
private final OciVault ociVault;

@Inject
OciVaultHealthCheck(@ConfigProperty(name = "oci.vault.vault-ocid") String vaultId,
OciVault ociVault) {
this.vaultId = vaultId;
this.ociVault = ociVault;
}

private OciVaultHealthCheck(Builder builder) {
this.vaultId = builder.vaultId;
this.ociVault = builder.vault;
}

/**
* Create a new fluent API builder to configure a new health check.
*
* @return builder instance
*/
public static Builder builder() {
return new Builder();
}

/**
* Create an instance.
*
* @param config the config.
* @return an instance.
*/
public static OciVaultHealthCheck create(Config config) {
return builder().config(config).build();
}

/**
* Checks that the OCI vault is accessible, if defined. Will report error only if not
* defined or not accessible. Can block since all health checks are called asynchronously.
*
* @return a response
*/
@Override
public HealthCheckResponse call() {
HealthCheckResponseBuilder builder = HealthCheckResponse.named("vault");
builder.withData("vaultId", vaultId);
try {
ApiOptionalResponse<GetVault.Response> r = ociVault.getVault(GetVault.Request.builder().vaultId(vaultId));
builder.state(r.status().equals(OK_200));
r.entity().ifPresent(e -> {
builder.withData("compartmentId", e.compartmentId());
builder.withData("displayName", e.displayName());
});
LOGGER.fine(() -> "OCI vault health check " + vaultId + " returned status code " + r.status().code());
} catch (Throwable t) {
builder.down()
.withData("Error", t.getClass().getName())
.withData("Message", t.getMessage());
LOGGER.fine(() -> "OCI vault health check " + vaultId + " exception " + t.getMessage());
}
return builder.build();
}

/**
* Fluent API builder for {@link OciVaultHealthCheck}.
*/
public static final class Builder implements io.helidon.common.Builder<OciVaultHealthCheck> {

private String vaultId;
private OciVaultRx vaultRx;
private OciVault vault;

private Builder() {
}

@Override
public OciVaultHealthCheck build() {
Objects.requireNonNull(vaultId);
if (vaultRx == null) {
vaultRx = OciVaultRx.create();
}
this.vault = OciVault.create(vaultRx);
return new OciVaultHealthCheck(this);
}

/**
* Set up this builder using config.
*
* @param config the config.
* @return the builder.
*/
public Builder config(Config config) {
config.get("oci.vault.vault-ocid").asString().ifPresent(this::vaultId);
return this;
}

/**
* Set the vault's OCID.
*
* @param vaultId vault ID.
* @return the builder.
*/
public Builder vaultId(String vaultId) {
this.vaultId = vaultId;
return this;
}

/**
* Set the vault client.
*
* @param vaultRx vault client.
* @return the builder.
*/
public Builder ociVault(OciVaultRx vaultRx) {
this.vaultRx = vaultRx;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* OCI Vault Health.
*/
package io.helidon.integrations.oci.vault.health;
37 changes: 37 additions & 0 deletions integrations/oci/vault-health/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* OCI Vault Health.
*/
module io.helidon.integrations.oci.vault.health {
requires java.logging;
requires io.helidon.integrations.oci.connect;
requires io.helidon.common;

requires transitive io.helidon.health;
requires transitive io.helidon.health.common;
requires transitive io.helidon.config;
requires transitive io.helidon.integrations.oci.vault;

requires static microprofile.config.api;
requires static jakarta.enterprise.cdi.api;
requires static jakarta.inject.api;

exports io.helidon.integrations.oci.vault.health;

opens io.helidon.integrations.oci.vault.health to weld.core.impl, io.helidon.microprofile.cdi;
}
Loading

0 comments on commit 2381396

Please sign in to comment.