Skip to content

Commit

Permalink
Implement secret, no cache, Temporary and Encrypted annotations. WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
credmond-git committed Oct 29, 2024
1 parent 74e248e commit cedbd4b
Show file tree
Hide file tree
Showing 29 changed files with 1,036 additions and 365 deletions.
1 change: 1 addition & 0 deletions gestalt-core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
org.github.gestalt.config.node.factory.UrlConfigNodeFactory;

provides org.github.gestalt.config.processor.config.annotation.AnnotationMetadataTransform with
org.github.gestalt.config.processor.config.annotation.NoCacheAnnotationMetadataTransform,
org.github.gestalt.config.processor.config.annotation.SecretAnnotationMetadataTransform;
}

Expand Down
24 changes: 24 additions & 0 deletions gestalt-core/src/main/java/org/github/gestalt/config/Gestalt.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.reload.CoreReloadListener;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.GResultOf;

import java.util.Optional;

Expand Down Expand Up @@ -72,6 +73,18 @@ public interface Gestalt {
*/
<T> T getConfig(String path, TypeCapture<T> klass, Tags tags) throws GestaltException;

/**
* Get a config for a path and a given TypeCapture.
*
* @param path path to get the config for. The path is not case sensitive.
* @param klass TypeCapture to get the class for.
* @param tags the tags to match while searching for configs
* @param <T> type of class to get.
* @return the configuration.
* @throws GestaltException any errors such as if there are no configs.
*/
<T> GResultOf<T> getConfigResult(String path, TypeCapture<T> klass, Tags tags) throws GestaltException;

/**
* Get a config for a path and a given class.
* If the config is missing or invalid it will return the default value.
Expand Down Expand Up @@ -164,6 +177,17 @@ public interface Gestalt {
*/
<T> Optional<T> getConfigOptional(String path, TypeCapture<T> klass, Tags tags);

/**
* Get a config Optional wrapped in the GResultOf for a path and a given TypeCapture. If there are any exceptions or errors it will return an Optional.empty()
*
* @param path path to get the config for. The path is not case sensitive.
* @param klass TypeCapture to get the class for.
* @param tags the tags to match while searching for configs
* @param <T> type of class to get.
* @return the configuration or Optional.empty() if it failed.
*/
<T> Optional<GResultOf<T>> getConfigOptionalResult(String path, TypeCapture<T> klass, Tags tags);

/**
* register a core event listener.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.github.gestalt.config.reload.CoreReloadListener;
import org.github.gestalt.config.secret.rules.SecretChecker;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.GResultOf;
import org.github.gestalt.config.utils.Triple;

import java.util.*;
Expand All @@ -23,7 +24,7 @@
@SuppressWarnings("OverloadMethodsDeclarationOrder")
public class GestaltCache implements Gestalt, CoreReloadListener {
private final Gestalt delegate;
private final Map<Triple<String, TypeCapture<?>, Tags>, Object> cache = Collections.synchronizedMap(new HashMap<>());
private final Map<Triple<String, TypeCapture<?>, Tags>, GResultOf<Object>> cache = Collections.synchronizedMap(new HashMap<>());
private final Tags defaultTags;
private final ObservationService observationService;
private final GestaltConfig gestaltConfig;
Expand Down Expand Up @@ -63,7 +64,7 @@ public <T> T getConfig(String path, Class<T> klass) throws GestaltException {
Objects.requireNonNull(klass);

TypeCapture<T> typeCapture = TypeCapture.of(klass);
return getConfigInternal(path, typeCapture, null);
return getConfigInternal(path, typeCapture, null).results();
}

@Override
Expand All @@ -73,15 +74,15 @@ public <T> T getConfig(String path, Class<T> klass, Tags tags) throws GestaltExc
Objects.requireNonNull(tags);

TypeCapture<T> typeCapture = TypeCapture.of(klass);
return getConfigInternal(path, typeCapture, tags);
return getConfigInternal(path, typeCapture, tags).results();
}

@Override
public <T> T getConfig(String path, TypeCapture<T> klass) throws GestaltException {
Objects.requireNonNull(path);
Objects.requireNonNull(klass);

return getConfigInternal(path, klass, null);
return getConfigInternal(path, klass, null).results();
}

@Override
Expand All @@ -90,29 +91,39 @@ public <T> T getConfig(String path, TypeCapture<T> klass, Tags tags) throws Gest
Objects.requireNonNull(klass);
Objects.requireNonNull(tags);

return getConfigInternal(path, klass, tags).results();
}

@Override
public <T> GResultOf<T> getConfigResult(String path, TypeCapture<T> klass, Tags tags) throws GestaltException {
Objects.requireNonNull(path);
Objects.requireNonNull(klass);
Objects.requireNonNull(tags);

return getConfigInternal(path, klass, tags);
}

@SuppressWarnings("unchecked")
private <T> T getConfigInternal(String path, TypeCapture<T> klass, Tags tags) throws GestaltException {
private <T> GResultOf<T> getConfigInternal(String path, TypeCapture<T> klass, Tags tags) throws GestaltException {

Tags resolvedTags = tagMergingStrategy.mergeTags(tags, defaultTags);
Triple<String, TypeCapture<?>, Tags> key = new Triple<>(path, klass, resolvedTags);
if (cache.get(key) != null) {
if (gestaltConfig.isObservationsEnabled() && observationService != null) {
observationService.recordObservation("cache.hit", 1, Tags.of());
}
return (T) cache.get(key);
return (GResultOf<T>) cache.get(key);
} else {
T result = delegate.getConfig(path, klass, resolvedTags);
GResultOf<T> result = delegate.getConfigResult(path, klass, resolvedTags);
updateCache(path, key, result);
return result;
}
}

private <T> void updateCache(String path, Triple<String, TypeCapture<?>, Tags> key, T result) {
@SuppressWarnings("unchecked")
private <T> void updateCache(String path, Triple<String, TypeCapture<?>, Tags> key, GResultOf<T> result) {
if (shouldCacheValue(path)) {
cache.put(key, result);
cache.put(key, (GResultOf<Object>) result);
}
}

Expand All @@ -122,7 +133,7 @@ public <T> T getConfig(String path, T defaultVal, Class<T> klass) {
Objects.requireNonNull(klass);

TypeCapture<T> typeCapture = TypeCapture.of(klass);
return getConfigInternal(path, defaultVal, typeCapture, null);
return getConfigInternal(path, defaultVal, typeCapture, null).results();
}

@Override
Expand All @@ -132,7 +143,7 @@ public <T> T getConfig(String path, T defaultVal, Class<T> klass, Tags tags) {
Objects.requireNonNull(tags);

TypeCapture<T> typeCapture = TypeCapture.of(klass);
return getConfigInternal(path, defaultVal, typeCapture, tags);
return getConfigInternal(path, defaultVal, typeCapture, tags).results();
}


Expand All @@ -142,7 +153,7 @@ public <T> T getConfig(String path, T defaultVal, TypeCapture<T> klass) {
Objects.requireNonNull(path);
Objects.requireNonNull(klass);

return getConfigInternal(path, defaultVal, klass, null);
return getConfigInternal(path, defaultVal, klass, null).results();
}

@Override
Expand All @@ -151,17 +162,17 @@ public <T> T getConfig(String path, T defaultVal, TypeCapture<T> klass, Tags tag
Objects.requireNonNull(klass);
Objects.requireNonNull(tags);

return getConfigInternal(path, defaultVal, klass, tags);
return getConfigInternal(path, defaultVal, klass, tags).results();
}

@SuppressWarnings("unchecked")
private <T> T getConfigInternal(String path, T defaultVal, TypeCapture<T> klass, Tags tags) {
private <T> GResultOf<T> getConfigInternal(String path, T defaultVal, TypeCapture<T> klass, Tags tags) {
Tags resolvedTags = tagMergingStrategy.mergeTags(tags, defaultTags);
Triple<String, TypeCapture<?>, Tags> key = new Triple<>(path, klass, resolvedTags);
if (cache.containsKey(key)) {
T result = (T) cache.get(key);
GResultOf<T> result = (GResultOf<T>) cache.get(key);
if (result == null) {
result = defaultVal;
result = GResultOf.result(defaultVal);
}

if (gestaltConfig.isObservationsEnabled() && observationService != null) {
Expand All @@ -171,13 +182,13 @@ private <T> T getConfigInternal(String path, T defaultVal, TypeCapture<T> klass,
return result;

} else {
Optional<T> resultOptional = delegate.getConfigOptional(path, klass, resolvedTags);
T result = resultOptional.orElse(null);
Optional<GResultOf<T>> resultOptional = delegate.getConfigOptionalResult(path, klass, resolvedTags);
GResultOf<T> result = resultOptional.orElse(null);
updateCache(path, key, result);
if (result != null) {
return result;
} else {
return defaultVal;
return GResultOf.result(defaultVal);
}
}
}
Expand All @@ -188,7 +199,7 @@ public <T> Optional<T> getConfigOptional(String path, Class<T> klass) {
Objects.requireNonNull(klass);

TypeCapture<T> typeCapture = TypeCapture.of(klass);
return getConfigOptionalInternal(path, typeCapture, null);
return getConfigOptionalInternal(path, typeCapture, null).map(GResultOf::results);
}

@Override
Expand All @@ -198,15 +209,15 @@ public <T> Optional<T> getConfigOptional(String path, Class<T> klass, Tags tags)
Objects.requireNonNull(tags);

TypeCapture<T> typeCapture = TypeCapture.of(klass);
return getConfigOptionalInternal(path, typeCapture, tags);
return getConfigOptionalInternal(path, typeCapture, tags).map(GResultOf::results);
}

@Override
public <T> Optional<T> getConfigOptional(String path, TypeCapture<T> klass) {
Objects.requireNonNull(path);
Objects.requireNonNull(klass);

return getConfigOptionalInternal(path, klass, null);
return getConfigOptionalInternal(path, klass, null).map(GResultOf::results);
}

@Override
Expand All @@ -215,11 +226,20 @@ public <T> Optional<T> getConfigOptional(String path, TypeCapture<T> klass, Tags
Objects.requireNonNull(klass);
Objects.requireNonNull(tags);

return getConfigOptionalInternal(path, klass, tags).map(GResultOf::results);
}

@Override
public <T> Optional<GResultOf<T>> getConfigOptionalResult(String path, TypeCapture<T> klass, Tags tags) {
Objects.requireNonNull(path);
Objects.requireNonNull(klass);
Objects.requireNonNull(tags);

return getConfigOptionalInternal(path, klass, tags);
}

@SuppressWarnings("unchecked")
public <T> Optional<T> getConfigOptionalInternal(String path, TypeCapture<T> klass, Tags tags) {
public <T> Optional<GResultOf<T>> getConfigOptionalInternal(String path, TypeCapture<T> klass, Tags tags) {

Tags resolvedTags = tagMergingStrategy.mergeTags(tags, defaultTags);
Triple<String, TypeCapture<?>, Tags> key = new Triple<>(path, klass, resolvedTags);
Expand All @@ -228,11 +248,11 @@ public <T> Optional<T> getConfigOptionalInternal(String path, TypeCapture<T> kla
observationService.recordObservation("cache.hit", 1, Tags.of());
}

T result = (T) cache.get(key);
GResultOf<T> result = (GResultOf<T>) cache.get(key);
return Optional.ofNullable(result);
} else {
Optional<T> resultOptional = delegate.getConfigOptional(path, klass, resolvedTags);
T result = resultOptional.orElse(null);
Optional<GResultOf<T>> resultOptional = delegate.getConfigOptionalResult(path, klass, resolvedTags);
GResultOf<T> result = resultOptional.orElse(null);
updateCache(path, key, result);
return Optional.ofNullable(result);
}
Expand Down
Loading

0 comments on commit cedbd4b

Please sign in to comment.