From edf8583a5174094e64e74ff16a9d598a038322c5 Mon Sep 17 00:00:00 2001 From: Andrej Petras Date: Thu, 23 May 2024 18:35:24 +0200 Subject: [PATCH] feat: refactoring digest --- ...onecx-data-orchestrator-operator-docs.adoc | 4 ++ .../orchestrator}/operator/Data.java | 2 +- .../orchestrator}/operator/DataConfig.java | 7 +--- .../operator/DataController.java | 39 +++++++++++++++--- .../orchestrator}/operator/DataSpec.java | 2 +- .../orchestrator}/operator/DataStatus.java | 2 +- .../operator/client/DataClientApi.java | 2 +- .../operator/client/DataConfigClient.java | 2 +- .../operator/client/DataService.java | 6 +-- .../product/store/slot/operator/CheckSum.java | 41 ------------------- .../orchestrator/operator/AbstractTest.java | 4 ++ .../operator/DataControllerResponseTest.java | 11 ++++- .../operator/DataControllerTest.java | 2 +- .../store/slot/operator/AbstractTest.java | 4 -- .../store/slot/operator/CheckSumTest.java | 15 ------- 15 files changed, 61 insertions(+), 82 deletions(-) rename src/main/java/org/tkit/onecx/{product/store/slot => data/orchestrator}/operator/Data.java (86%) rename src/main/java/org/tkit/onecx/{product/store/slot => data/orchestrator}/operator/DataConfig.java (62%) rename src/main/java/org/tkit/onecx/{product/store/slot => data/orchestrator}/operator/DataController.java (73%) rename src/main/java/org/tkit/onecx/{product/store/slot => data/orchestrator}/operator/DataSpec.java (96%) rename src/main/java/org/tkit/onecx/{product/store/slot => data/orchestrator}/operator/DataStatus.java (95%) rename src/main/java/org/tkit/onecx/{product/store/slot => data/orchestrator}/operator/client/DataClientApi.java (81%) rename src/main/java/org/tkit/onecx/{product/store/slot => data/orchestrator}/operator/client/DataConfigClient.java (94%) rename src/main/java/org/tkit/onecx/{product/store/slot => data/orchestrator}/operator/client/DataService.java (90%) delete mode 100644 src/main/java/org/tkit/onecx/product/store/slot/operator/CheckSum.java create mode 100644 src/test/java/org/tkit/onecx/data/orchestrator/operator/AbstractTest.java rename src/test/java/org/tkit/onecx/{product/store/slot => data/orchestrator}/operator/DataControllerResponseTest.java (76%) rename src/test/java/org/tkit/onecx/{product/store/slot => data/orchestrator}/operator/DataControllerTest.java (99%) delete mode 100644 src/test/java/org/tkit/onecx/product/store/slot/operator/AbstractTest.java delete mode 100644 src/test/java/org/tkit/onecx/product/store/slot/operator/CheckSumTest.java diff --git a/docs/modules/onecx-data-orchestrator-operator/pages/onecx-data-orchestrator-operator-docs.adoc b/docs/modules/onecx-data-orchestrator-operator/pages/onecx-data-orchestrator-operator-docs.adoc index c8498fe..9f807d8 100644 --- a/docs/modules/onecx-data-orchestrator-operator/pages/onecx-data-orchestrator-operator-docs.adoc +++ b/docs/modules/onecx-data-orchestrator-operator/pages/onecx-data-orchestrator-operator-docs.adoc @@ -52,3 +52,7 @@ app: watchNamespaces: "JOSDK_WATCH_CURRENT" ---- +ACES or comma separated list of namespaces +watchNamespaces: "JOSDK_WATCH_CURRENT" +---- + diff --git a/src/main/java/org/tkit/onecx/product/store/slot/operator/Data.java b/src/main/java/org/tkit/onecx/data/orchestrator/operator/Data.java similarity index 86% rename from src/main/java/org/tkit/onecx/product/store/slot/operator/Data.java rename to src/main/java/org/tkit/onecx/data/orchestrator/operator/Data.java index 99ff28d..a092d8f 100644 --- a/src/main/java/org/tkit/onecx/product/store/slot/operator/Data.java +++ b/src/main/java/org/tkit/onecx/data/orchestrator/operator/Data.java @@ -1,4 +1,4 @@ -package org.tkit.onecx.product.store.slot.operator; +package org.tkit.onecx.data.orchestrator.operator; import io.fabric8.kubernetes.api.model.Namespaced; import io.fabric8.kubernetes.client.CustomResource; diff --git a/src/main/java/org/tkit/onecx/product/store/slot/operator/DataConfig.java b/src/main/java/org/tkit/onecx/data/orchestrator/operator/DataConfig.java similarity index 62% rename from src/main/java/org/tkit/onecx/product/store/slot/operator/DataConfig.java rename to src/main/java/org/tkit/onecx/data/orchestrator/operator/DataConfig.java index a9e47ab..016840d 100644 --- a/src/main/java/org/tkit/onecx/product/store/slot/operator/DataConfig.java +++ b/src/main/java/org/tkit/onecx/data/orchestrator/operator/DataConfig.java @@ -1,9 +1,6 @@ -package org.tkit.onecx.product.store.slot.operator; +package org.tkit.onecx.data.orchestrator.operator; -import io.quarkus.runtime.annotations.ConfigDocFilename; -import io.quarkus.runtime.annotations.ConfigPhase; -import io.quarkus.runtime.annotations.ConfigRoot; -import io.quarkus.runtime.annotations.StaticInitSafe; +import io.quarkus.runtime.annotations.*; import io.smallrye.config.ConfigMapping; import io.smallrye.config.WithDefault; import io.smallrye.config.WithName; diff --git a/src/main/java/org/tkit/onecx/product/store/slot/operator/DataController.java b/src/main/java/org/tkit/onecx/data/orchestrator/operator/DataController.java similarity index 73% rename from src/main/java/org/tkit/onecx/product/store/slot/operator/DataController.java rename to src/main/java/org/tkit/onecx/data/orchestrator/operator/DataController.java index 21b78ea..d7e93a9 100644 --- a/src/main/java/org/tkit/onecx/product/store/slot/operator/DataController.java +++ b/src/main/java/org/tkit/onecx/data/orchestrator/operator/DataController.java @@ -1,25 +1,30 @@ -package org.tkit.onecx.product.store.slot.operator; +package org.tkit.onecx.data.orchestrator.operator; import static io.javaoperatorsdk.operator.api.reconciler.Constants.WATCH_CURRENT_NAMESPACE; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + import jakarta.inject.Inject; import jakarta.ws.rs.WebApplicationException; +import jakarta.xml.bind.DatatypeConverter; +import org.eclipse.microprofile.config.ConfigProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.tkit.onecx.product.store.slot.operator.client.DataService; +import org.tkit.onecx.data.orchestrator.operator.client.DataService; import io.javaoperatorsdk.operator.api.reconciler.*; import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter; import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter; +import io.smallrye.config.SmallRyeConfig; @ControllerConfiguration(name = "data", namespaces = WATCH_CURRENT_NAMESPACE, onAddFilter = DataController.SlotAddFilter.class, onUpdateFilter = DataController.SlotUpdateFilter.class) public class DataController implements Reconciler, ErrorStatusHandler { private static final Logger log = LoggerFactory.getLogger(DataController.class); - private static final CheckSum CHECK_SUM = new CheckSum(); - @Inject DataService service; @@ -65,7 +70,7 @@ public ErrorStatusUpdateControl updateErrorStatus(Data slot, private void updateStatusPojo(Data data, int responseCode) { var result = new DataStatus(); result.setResponseCode(responseCode); - result.setChecksum(CHECK_SUM.createCheckSum(data.getSpec().getData())); + result.setChecksum(createCheckSum(data.getSpec().getData())); var status = switch (responseCode) { case 201: yield DataStatus.Status.CREATED; @@ -108,8 +113,30 @@ public boolean accept(Data newResource, Data oldResource) { if (newResource.getSpec().getData() == null || newResource.getSpec().getData().isEmpty()) { return false; } - return oldResource.getStatus().getChecksum().equals(CHECK_SUM.createCheckSum(newResource.getSpec().getData())); + return oldResource.getStatus().getChecksum() + .equals(createCheckSum(newResource.getSpec().getData())); + } + } + + static MessageDigest createDigest(String name) { + try { + return MessageDigest.getInstance(name); + } catch (NoSuchAlgorithmException e) { + throw new CheckSumException(e); } } + static String createCheckSum(String data) { + var config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class).getConfigMapping(DataConfig.class); + var digest = createDigest(config.digest()); + byte[] hash = digest.digest(data.getBytes(StandardCharsets.UTF_8)); + return DatatypeConverter.printHexBinary(hash).toUpperCase(); + } + + static class CheckSumException extends RuntimeException { + + public CheckSumException(Throwable e) { + super("Error create check-sum from the data content", e); + } + } } diff --git a/src/main/java/org/tkit/onecx/product/store/slot/operator/DataSpec.java b/src/main/java/org/tkit/onecx/data/orchestrator/operator/DataSpec.java similarity index 96% rename from src/main/java/org/tkit/onecx/product/store/slot/operator/DataSpec.java rename to src/main/java/org/tkit/onecx/data/orchestrator/operator/DataSpec.java index 0ea3a6a..8cd2b3f 100644 --- a/src/main/java/org/tkit/onecx/product/store/slot/operator/DataSpec.java +++ b/src/main/java/org/tkit/onecx/data/orchestrator/operator/DataSpec.java @@ -1,4 +1,4 @@ -package org.tkit.onecx.product.store.slot.operator; +package org.tkit.onecx.data.orchestrator.operator; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/org/tkit/onecx/product/store/slot/operator/DataStatus.java b/src/main/java/org/tkit/onecx/data/orchestrator/operator/DataStatus.java similarity index 95% rename from src/main/java/org/tkit/onecx/product/store/slot/operator/DataStatus.java rename to src/main/java/org/tkit/onecx/data/orchestrator/operator/DataStatus.java index 1c873fe..7a6fdd7 100644 --- a/src/main/java/org/tkit/onecx/product/store/slot/operator/DataStatus.java +++ b/src/main/java/org/tkit/onecx/data/orchestrator/operator/DataStatus.java @@ -1,4 +1,4 @@ -package org.tkit.onecx.product.store.slot.operator; +package org.tkit.onecx.data.orchestrator.operator; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/org/tkit/onecx/product/store/slot/operator/client/DataClientApi.java b/src/main/java/org/tkit/onecx/data/orchestrator/operator/client/DataClientApi.java similarity index 81% rename from src/main/java/org/tkit/onecx/product/store/slot/operator/client/DataClientApi.java rename to src/main/java/org/tkit/onecx/data/orchestrator/operator/client/DataClientApi.java index fb8f538..1324efb 100644 --- a/src/main/java/org/tkit/onecx/product/store/slot/operator/client/DataClientApi.java +++ b/src/main/java/org/tkit/onecx/data/orchestrator/operator/client/DataClientApi.java @@ -1,4 +1,4 @@ -package org.tkit.onecx.product.store.slot.operator.client; +package org.tkit.onecx.data.orchestrator.operator.client; import jakarta.ws.rs.POST; import jakarta.ws.rs.Produces; diff --git a/src/main/java/org/tkit/onecx/product/store/slot/operator/client/DataConfigClient.java b/src/main/java/org/tkit/onecx/data/orchestrator/operator/client/DataConfigClient.java similarity index 94% rename from src/main/java/org/tkit/onecx/product/store/slot/operator/client/DataConfigClient.java rename to src/main/java/org/tkit/onecx/data/orchestrator/operator/client/DataConfigClient.java index 90e0712..50f801f 100644 --- a/src/main/java/org/tkit/onecx/product/store/slot/operator/client/DataConfigClient.java +++ b/src/main/java/org/tkit/onecx/data/orchestrator/operator/client/DataConfigClient.java @@ -1,4 +1,4 @@ -package org.tkit.onecx.product.store.slot.operator.client; +package org.tkit.onecx.data.orchestrator.operator.client; import java.util.Map; diff --git a/src/main/java/org/tkit/onecx/product/store/slot/operator/client/DataService.java b/src/main/java/org/tkit/onecx/data/orchestrator/operator/client/DataService.java similarity index 90% rename from src/main/java/org/tkit/onecx/product/store/slot/operator/client/DataService.java rename to src/main/java/org/tkit/onecx/data/orchestrator/operator/client/DataService.java index 591a350..c11643b 100644 --- a/src/main/java/org/tkit/onecx/product/store/slot/operator/client/DataService.java +++ b/src/main/java/org/tkit/onecx/data/orchestrator/operator/client/DataService.java @@ -1,4 +1,4 @@ -package org.tkit.onecx.product.store.slot.operator.client; +package org.tkit.onecx.data.orchestrator.operator.client; import java.net.URI; @@ -9,8 +9,8 @@ import org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.tkit.onecx.product.store.slot.operator.Data; -import org.tkit.onecx.product.store.slot.operator.DataSpec; +import org.tkit.onecx.data.orchestrator.operator.Data; +import org.tkit.onecx.data.orchestrator.operator.DataSpec; @ApplicationScoped public class DataService { diff --git a/src/main/java/org/tkit/onecx/product/store/slot/operator/CheckSum.java b/src/main/java/org/tkit/onecx/product/store/slot/operator/CheckSum.java deleted file mode 100644 index 1c5d399..0000000 --- a/src/main/java/org/tkit/onecx/product/store/slot/operator/CheckSum.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.tkit.onecx.product.store.slot.operator; - -import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -import jakarta.xml.bind.DatatypeConverter; - -import org.eclipse.microprofile.config.ConfigProvider; - -import io.smallrye.config.SmallRyeConfig; - -public class CheckSum { - - private final MessageDigest digest; - - public CheckSum() { - var config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class).getConfigMapping(DataConfig.class); - digest = createDigest(config.digest()); - } - - protected static MessageDigest createDigest(String name) { - try { - return MessageDigest.getInstance(name); - } catch (NoSuchAlgorithmException e) { - throw new CheckSumException(e); - } - } - - public String createCheckSum(String data) throws RuntimeException { - byte[] hash = digest.digest(data.getBytes(StandardCharsets.UTF_8)); - return DatatypeConverter.printHexBinary(hash).toUpperCase(); - } - - public static class CheckSumException extends RuntimeException { - - public CheckSumException(Throwable e) { - super("Error create check-sum from the data content", e); - } - } -} diff --git a/src/test/java/org/tkit/onecx/data/orchestrator/operator/AbstractTest.java b/src/test/java/org/tkit/onecx/data/orchestrator/operator/AbstractTest.java new file mode 100644 index 0000000..b4dcefd --- /dev/null +++ b/src/test/java/org/tkit/onecx/data/orchestrator/operator/AbstractTest.java @@ -0,0 +1,4 @@ +package org.tkit.onecx.data.orchestrator.operator; + +public abstract class AbstractTest { +} diff --git a/src/test/java/org/tkit/onecx/product/store/slot/operator/DataControllerResponseTest.java b/src/test/java/org/tkit/onecx/data/orchestrator/operator/DataControllerResponseTest.java similarity index 76% rename from src/test/java/org/tkit/onecx/product/store/slot/operator/DataControllerResponseTest.java rename to src/test/java/org/tkit/onecx/data/orchestrator/operator/DataControllerResponseTest.java index 2bb64c2..b183ddf 100644 --- a/src/test/java/org/tkit/onecx/product/store/slot/operator/DataControllerResponseTest.java +++ b/src/test/java/org/tkit/onecx/data/orchestrator/operator/DataControllerResponseTest.java @@ -1,14 +1,16 @@ -package org.tkit.onecx.product.store.slot.operator; +package org.tkit.onecx.data.orchestrator.operator; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.tkit.onecx.data.orchestrator.operator.DataController.createDigest; import jakarta.inject.Inject; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import org.tkit.onecx.product.store.slot.operator.client.DataService; +import org.tkit.onecx.data.orchestrator.operator.client.DataService; import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; import io.quarkus.test.InjectMock; @@ -28,6 +30,11 @@ void beforeAll() { Mockito.when(dataService.updateData(any())).thenReturn(404); } + @Test + void wrongAlgoDigest() { + Assertions.assertThrows(DataController.CheckSumException.class, () -> createDigest("WRONG_NAME")); + } + @Test void testWrongResponse() throws Exception { diff --git a/src/test/java/org/tkit/onecx/product/store/slot/operator/DataControllerTest.java b/src/test/java/org/tkit/onecx/data/orchestrator/operator/DataControllerTest.java similarity index 99% rename from src/test/java/org/tkit/onecx/product/store/slot/operator/DataControllerTest.java rename to src/test/java/org/tkit/onecx/data/orchestrator/operator/DataControllerTest.java index 0b94222..42e57fc 100644 --- a/src/test/java/org/tkit/onecx/product/store/slot/operator/DataControllerTest.java +++ b/src/test/java/org/tkit/onecx/data/orchestrator/operator/DataControllerTest.java @@ -1,4 +1,4 @@ -package org.tkit.onecx.product.store.slot.operator; +package org.tkit.onecx.data.orchestrator.operator; import static java.util.concurrent.TimeUnit.SECONDS; import static org.assertj.core.api.Assertions.assertThat; diff --git a/src/test/java/org/tkit/onecx/product/store/slot/operator/AbstractTest.java b/src/test/java/org/tkit/onecx/product/store/slot/operator/AbstractTest.java deleted file mode 100644 index 1446365..0000000 --- a/src/test/java/org/tkit/onecx/product/store/slot/operator/AbstractTest.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.tkit.onecx.product.store.slot.operator; - -public abstract class AbstractTest { -} diff --git a/src/test/java/org/tkit/onecx/product/store/slot/operator/CheckSumTest.java b/src/test/java/org/tkit/onecx/product/store/slot/operator/CheckSumTest.java deleted file mode 100644 index 97799d2..0000000 --- a/src/test/java/org/tkit/onecx/product/store/slot/operator/CheckSumTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.tkit.onecx.product.store.slot.operator; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import io.quarkus.test.junit.QuarkusTest; - -@QuarkusTest -class CheckSumTest { - - @Test - void wrongAlgoDigest() { - Assertions.assertThrows(CheckSum.CheckSumException.class, () -> CheckSum.createDigest("WRONG_NAME")); - } -}