From d7518f10f2d75e5a5d19756dfd7eac6396852dd3 Mon Sep 17 00:00:00 2001
From: Mostafa Kazemi <32548292+mkay1375@users.noreply.github.com>
Date: Tue, 20 Aug 2024 23:55:17 +0330
Subject: [PATCH] Add HissPropertiesBuilder (#18)
* Add HissPropertiesBuilder
* Fix javadoc errors
---
pom.xml | 3 +
.../io/github/tap30/hiss/HissFactory.java | 31 +++---
.../tap30/hiss/properties/HissProperties.java | 103 +++++++++---------
...ava => HissPropertiesFromEnvProvider.java} | 17 +--
.../properties/HissPropertiesProvider.java | 14 +++
.../properties/HissPropertiesValidator.java | 6 +-
.../io/github/tap30/hiss/BaseHissTest.java | 5 +-
.../io/github/tap30/hiss/HissFactoryTest.java | 80 +++-----------
...=> HissPropertiesFromEnvProviderTest.java} | 4 +-
.../HissPropertiesValidatorTest.java | 41 ++-----
10 files changed, 122 insertions(+), 182 deletions(-)
rename src/main/java/io/github/tap30/hiss/properties/{HissPropertiesFromEnv.java => HissPropertiesFromEnvProvider.java} (82%)
create mode 100644 src/main/java/io/github/tap30/hiss/properties/HissPropertiesProvider.java
rename src/test/java/io/github/tap30/hiss/properties/{HissPropertiesFromEnvTest.java => HissPropertiesFromEnvProviderTest.java} (95%)
diff --git a/pom.xml b/pom.xml
index f268f98..80d3458 100644
--- a/pom.xml
+++ b/pom.xml
@@ -122,6 +122,9 @@
+
+ none
+
org.apache.maven.plugins
diff --git a/src/main/java/io/github/tap30/hiss/HissFactory.java b/src/main/java/io/github/tap30/hiss/HissFactory.java
index d6ca577..26b6108 100644
--- a/src/main/java/io/github/tap30/hiss/HissFactory.java
+++ b/src/main/java/io/github/tap30/hiss/HissFactory.java
@@ -11,6 +11,7 @@
import io.github.tap30.hiss.hasher.impl.TapsiHmacSha256Hasher;
import io.github.tap30.hiss.key.KeyHashGenerator;
import io.github.tap30.hiss.properties.HissProperties;
+import io.github.tap30.hiss.properties.HissPropertiesProvider;
import io.github.tap30.hiss.properties.HissPropertiesValidator;
import org.jetbrains.annotations.NotNull;
@@ -27,14 +28,13 @@ public class HissFactory {
* Creates a Hiss instance with provided HissProperties
and default encryptors and hashers.
*
* @param hissProperties the properties by which hiss will be instantiated;
- * {@link io.github.tap30.hiss.properties.HissPropertiesFromEnv}
- * or any custom implementation of
- * {@link io.github.tap30.hiss.properties.HissProperties}
- * can be used.
+ * use {@link HissProperties#fromEnv()}
+ * or {@link HissProperties#builder()}
+ * or {@link HissProperties#withProvider(HissPropertiesProvider)}
* @return {@link Hiss} instance.
* @throws IllegalArgumentException if the properties are not valid.
*/
- public static Hiss createHiss(HissProperties hissProperties) {
+ public static Hiss createHiss(@NotNull HissProperties hissProperties) {
return createHiss(hissProperties, Set.of(), Set.of());
}
@@ -44,18 +44,17 @@ public static Hiss createHiss(HissProperties hissProperties) {
* Provided encryptors and hashers will be added alongside default ones.
*
* @param hissProperties the properties by which hiss will be instantiated;
- * {@link io.github.tap30.hiss.properties.HissPropertiesFromEnv}
- * or any custom implementation of
- * {@link io.github.tap30.hiss.properties.HissProperties}
- * can be used.
- * @param encryptors custom {@link Encryptor} implementations. Can be empty but not null.
- * @param hashers custom {@link Hasher} implementations. Can be empty but not null.
+ * use {@link HissProperties#fromEnv()}
+ * or {@link HissProperties#builder()}
+ * or {@link HissProperties#withProvider(HissPropertiesProvider)}
+ * @param encryptors custom {@link Encryptor} implementations. Can be empty but not null.
+ * @param hashers custom {@link Hasher} implementations. Can be empty but not null.
* @return {@link Hiss} instance.
* @throws IllegalArgumentException if the properties are not valid.
*/
- public static Hiss createHiss(HissProperties hissProperties,
- Set encryptors,
- Set hashers) {
+ public static Hiss createHiss(@NotNull HissProperties hissProperties,
+ @NotNull Set encryptors,
+ @NotNull Set hashers) {
Objects.requireNonNull(hissProperties);
Objects.requireNonNull(encryptors);
Objects.requireNonNull(hashers);
@@ -109,8 +108,8 @@ public static Hiss createHiss(HissProperties hissProperties,
}
private static void logInitializingHiss(HissProperties hissProperties,
- Map encryptors,
- Map hashers) {
+ Map encryptors,
+ Map hashers) {
logger.log(Level.INFO, "Hiss initialized:\n" +
" Loaded Keys: {0}\n" +
" Default Encryption Key ID: {1}\n" +
diff --git a/src/main/java/io/github/tap30/hiss/properties/HissProperties.java b/src/main/java/io/github/tap30/hiss/properties/HissProperties.java
index f1678d9..1ee46ce 100644
--- a/src/main/java/io/github/tap30/hiss/properties/HissProperties.java
+++ b/src/main/java/io/github/tap30/hiss/properties/HissProperties.java
@@ -2,74 +2,73 @@
import io.github.tap30.hiss.key.Key;
import io.github.tap30.hiss.utils.StringUtils;
+import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Value;
-import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
-import java.util.function.Supplier;
import java.util.stream.Collectors;
-public abstract class HissProperties {
-
- private final Map properties = new HashMap<>();
-
- public Map getKeys() {
- return this.getProperty("Keys", this::loadKeys, HissProperties::keysAsMap);
- }
-
- public String getDefaultEncryptionKeyId() {
- return this.getProperty("DefaultEncryptionKeyId",
- this::loadDefaultEncryptionKeyId, StringUtils::toLowerCase);
- }
-
- public String getDefaultEncryptionAlgorithm() {
- return this.getProperty("DefaultEncryptionAlgorithm",
- this::loadDefaultEncryptionAlgorithm, StringUtils::toLowerCase);
- }
-
- public String getDefaultHashingKeyId() {
- return this.getProperty("DefaultHashingKeyId",
- this::loadDefaultHashingKeyId, StringUtils::toLowerCase);
- }
-
- public String getDefaultHashingAlgorithm() {
- return this.getProperty("DefaultHashingAlgorithm",
- this::loadDefaultHashingAlgorithm, StringUtils::toLowerCase);
+@Builder
+@Value
+@AllArgsConstructor(access = AccessLevel.PACKAGE)
+public class HissProperties {
+
+ Map keys;
+ String defaultEncryptionKeyId;
+ String defaultEncryptionAlgorithm;
+ String defaultHashingKeyId;
+ String defaultHashingAlgorithm;
+ boolean keyHashGenerationEnabled;
+
+ /**
+ * See {@link HissPropertiesFromEnvProvider}.
+ */
+ public static HissProperties fromEnv() {
+ return withProvider(new HissPropertiesFromEnvProvider());
}
- public boolean isKeyHashGenerationEnabled() {
- return this.getProperty("KeyHashGenerationEnabled", this::loadKeyHashGenerationEnabled);
+ public static HissProperties withProvider(HissPropertiesProvider provider) {
+ return builder()
+ .keys(provider.getKeys())
+ .defaultEncryptionKeyId(provider.getDefaultEncryptionKeyId())
+ .defaultEncryptionAlgorithm(provider.getDefaultEncryptionAlgorithm())
+ .defaultHashingKeyId(provider.getDefaultHashingKeyId())
+ .defaultHashingAlgorithm(provider.getDefaultHashingAlgorithm())
+ .keyHashGenerationEnabled(provider.isKeyHashGenerationEnabled())
+ .build();
}
- protected abstract Set loadKeys();
-
- protected abstract String loadDefaultEncryptionKeyId();
-
- protected abstract String loadDefaultEncryptionAlgorithm();
-
- protected abstract String loadDefaultHashingKeyId();
+ public static class HissPropertiesBuilder {
+ public HissPropertiesBuilder keys(Set keys) {
+ this.keys = keys.stream()
+ .collect(Collectors.toMap(k -> StringUtils.toLowerCase(k.getId()), Function.identity()));
+ return this;
+ }
- protected abstract String loadDefaultHashingAlgorithm();
+ public HissPropertiesBuilder defaultEncryptionKeyId(String defaultEncryptionKeyId) {
+ this.defaultEncryptionKeyId = StringUtils.toLowerCase(defaultEncryptionKeyId);
+ return this;
+ }
- protected abstract boolean loadKeyHashGenerationEnabled();
+ public HissPropertiesBuilder defaultEncryptionAlgorithm(String defaultEncryptionAlgorithm) {
+ this.defaultEncryptionAlgorithm = StringUtils.toLowerCase(defaultEncryptionAlgorithm);
+ return this;
+ }
- private O getProperty(String key, Supplier valueSupplier) {
- return getProperty(key, valueSupplier, v -> v);
- }
+ public HissPropertiesBuilder defaultHashingKeyId(String defaultHashingKeyId) {
+ this.defaultHashingKeyId = StringUtils.toLowerCase(defaultHashingKeyId);
+ return this;
+ }
- @SuppressWarnings("unchecked")
- private O getProperty(String key, Supplier valueSupplier, Function mapper) {
- if (this.properties.containsKey(key)) {
- return (O) this.properties.get(key);
+ public HissPropertiesBuilder defaultHashingAlgorithm(String defaultHashingAlgorithm) {
+ this.defaultHashingAlgorithm = StringUtils.toLowerCase(defaultHashingAlgorithm);
+ return this;
}
- var value = mapper.apply(valueSupplier.get());
- this.properties.put(key, value);
- return value;
}
- private static Map keysAsMap(Set keys) {
- return keys.stream().collect(Collectors.toMap(k -> StringUtils.toLowerCase(k.getId()), k -> k));
- }
}
diff --git a/src/main/java/io/github/tap30/hiss/properties/HissPropertiesFromEnv.java b/src/main/java/io/github/tap30/hiss/properties/HissPropertiesFromEnvProvider.java
similarity index 82%
rename from src/main/java/io/github/tap30/hiss/properties/HissPropertiesFromEnv.java
rename to src/main/java/io/github/tap30/hiss/properties/HissPropertiesFromEnvProvider.java
index bc4706b..81c7407 100644
--- a/src/main/java/io/github/tap30/hiss/properties/HissPropertiesFromEnv.java
+++ b/src/main/java/io/github/tap30/hiss/properties/HissPropertiesFromEnvProvider.java
@@ -8,7 +8,7 @@
import java.util.Set;
import java.util.function.Supplier;
-// Todo: import doc
+// Todo: improve doc
/**
* Sample Envs:
*
@@ -26,7 +26,7 @@
* HISS_DEFAULT_HASHING_ALGORITHM: hmac-sha256
*
*/
-public class HissPropertiesFromEnv extends HissProperties {
+public class HissPropertiesFromEnvProvider implements HissPropertiesProvider {
private static final String KEY_ENV_PREFIX = "HISS_KEYS_";
private static final String KEY_HASH_ENV_POSTFIX = "___HASH";
@@ -34,7 +34,7 @@ public class HissPropertiesFromEnv extends HissProperties {
private static final Supplier