From 30b7626d80d0df25deea382ee3ede0ef501e5486 Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Thu, 12 Oct 2023 22:42:40 +0200 Subject: [PATCH] added data source support - crypto support --- .../core/grammar/datasource/DataSource.java | 2 +- .../grammar/datasource/DataSourceInvoker.java | 10 ++++++- .../grammar/datasource/GroupsDataSource.java | 6 ++-- .../grammar/datasource/LowerDataSource.java | 4 +-- .../grammar/datasource/NodesDataSource.java | 6 ++-- .../datasource/TraverseDataSource.java | 28 ++++++++----------- .../grammar/datasource/UpperDataSource.java | 4 +-- .../datasource/ValueMapDataSource.java | 4 +-- .../apm/core/grammar/ScriptRunnerTest.groovy | 11 +++++--- 9 files changed, 41 insertions(+), 34 deletions(-) diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSource.java index 6e026610..08337a86 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSource.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSource.java @@ -27,5 +27,5 @@ public interface DataSource { String getName(); - ApmType determine(ResourceResolver resolver, List parameters); + ApmType determine(ResourceResolver resolver, List parameters); } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSourceInvoker.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSourceInvoker.java index 37717057..833fae04 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSourceInvoker.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSourceInvoker.java @@ -19,11 +19,13 @@ */ package com.cognifide.apm.core.grammar.datasource; +import com.cognifide.apm.core.crypto.DecryptionService; import com.cognifide.apm.core.grammar.ApmType; import com.cognifide.apm.core.grammar.argument.ArgumentResolverException; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.apache.sling.api.resource.ResourceResolver; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @@ -33,6 +35,9 @@ @Component(service = DataSourceInvoker.class) public class DataSourceInvoker { + @Reference + private DecryptionService decryptionService; + private final Map dataSources = new HashMap<>(); @Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE, service = DataSource.class) @@ -47,7 +52,10 @@ protected final void unbindDataSource(DataSource dataSource) { public ApmType determine(String name, ResourceResolver resolver, List parameters) { DataSource dataSource = dataSources.get(name.toUpperCase()); try { - return dataSource == null ? null : dataSource.determine(resolver, parameters); + List decryptedParameters = parameters.stream() + .map(parameter -> parameter.getArgument(decryptionService)) + .collect(Collectors.toList()); + return dataSource == null ? null : dataSource.determine(resolver, decryptedParameters); } catch (Exception e) { throw new ArgumentResolverException(String.format("%s data source: %s", name.toUpperCase(), e.getMessage())); } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/GroupsDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/GroupsDataSource.java index ab026a96..0631ab15 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/GroupsDataSource.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/GroupsDataSource.java @@ -38,9 +38,9 @@ public String getName() { } @Override - public ApmType determine(ResourceResolver resolver, List parameters) { - String value = parameters.get(0).getString(); - String regex = parameters.get(1).getString(); + public ApmType determine(ResourceResolver resolver, List parameters) { + String value = (String) parameters.get(0); + String regex = (String) parameters.get(1); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(value); List list = new ArrayList<>(); diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/LowerDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/LowerDataSource.java index 4b656bde..4fa325d0 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/LowerDataSource.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/LowerDataSource.java @@ -34,8 +34,8 @@ public String getName() { } @Override - public ApmType determine(ResourceResolver resolver, List parameters) { - String value = parameters.get(0).getString(); + public ApmType determine(ResourceResolver resolver, List parameters) { + String value = (String) parameters.get(0); return new ApmString(value.toLowerCase()); } } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodesDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodesDataSource.java index d9030d3c..8ba96c3c 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodesDataSource.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodesDataSource.java @@ -41,9 +41,9 @@ public String getName() { } @Override - public ApmType determine(ResourceResolver resolver, List parameters) { - String path = parameters.get(0).getString(); - String regex = parameters.get(1).getString(); + public ApmType determine(ResourceResolver resolver, List parameters) { + String path = (String) parameters.get(0); + String regex = (String) parameters.get(1); Pattern pattern = Pattern.compile(regex); Resource root = resolver.getResource(path); List values = StreamSupport.stream(root.getChildren().spliterator(), false) diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/TraverseDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/TraverseDataSource.java index bad16893..ac503fb6 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/TraverseDataSource.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/TraverseDataSource.java @@ -27,6 +27,7 @@ import com.day.cq.wcm.api.NameConstants; import com.google.common.collect.ImmutableMap; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -51,8 +52,8 @@ public String getName() { } @Override - public ApmType determine(ResourceResolver resolver, List parameters) { - String rootPath = parameters.get(0).getString(); + public ApmType determine(ResourceResolver resolver, List parameters) { + String rootPath = (String) parameters.get(0); List configs = determineConfigs(parameters); Resource root = resolver.getResource(rootPath); return traverseTree(root, 0, configs); @@ -88,11 +89,10 @@ private ApmType traverseTree(Resource root, int depth, List configs) { return new ApmList(list); } - private List determineConfigs(List parameters) { - return parameters.get(1) - .getList() + private List determineConfigs(List parameters) { + return ((List) parameters.get(1)) .stream() - .map(ApmType::getMap) + .map(item -> (Map) item) .map(Config::new) .collect(Collectors.toList()); } @@ -109,22 +109,18 @@ private static class Config { List paramNames; - Config(Map map) { - String regex = map.getOrDefault("regex", new ApmEmpty()).getString(); + Config(Map map) { + String regex = (String) map.get("regex"); if (StringUtils.isNotEmpty(regex)) { pattern = Pattern.compile(regex); } - String excludeRegex = map.getOrDefault("excludeRegex", new ApmEmpty()).getString(); + String excludeRegex = (String) map.get("excludeRegex"); if (StringUtils.isNotEmpty(excludeRegex)) { excludePattern = Pattern.compile(excludeRegex); } - template = map.getOrDefault("template", new ApmEmpty()).getString(); - resourceType = map.getOrDefault("resourceType", new ApmEmpty()).getString(); - paramNames = map.getOrDefault("paramNames", new ApmList(new ArrayList<>())) - .getList() - .stream() - .map(ApmType::getString) - .collect(Collectors.toList()); + template = (String) map.get("template"); + resourceType = (String) map.get("resourceType"); + paramNames = (List) map.getOrDefault("paramNames", Collections.emptyList()); if (StringUtils.countMatches(regex, "(") != paramNames.size()) { throw new IllegalArgumentException("Number of paramNames must match number of regex groups"); } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/UpperDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/UpperDataSource.java index 19705fef..7bb147c5 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/UpperDataSource.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/UpperDataSource.java @@ -34,8 +34,8 @@ public String getName() { } @Override - public ApmType determine(ResourceResolver resolver, List parameters) { - String value = parameters.get(0).getString(); + public ApmType determine(ResourceResolver resolver, List parameters) { + String value = (String) parameters.get(0); return new ApmString(value.toUpperCase()); } } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ValueMapDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ValueMapDataSource.java index e303f506..ada60404 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ValueMapDataSource.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ValueMapDataSource.java @@ -44,8 +44,8 @@ public String getName() { } @Override - public ApmType determine(ResourceResolver resolver, List parameters) { - String path = parameters.get(0).getString(); + public ApmType determine(ResourceResolver resolver, List parameters) { + String path = (String) parameters.get(0); ValueMap valueMap = resolver.getResource(path).getValueMap(); Map map = new HashMap<>(); for (Map.Entry entry : valueMap.entrySet()) { diff --git a/app/aem/core/src/test/groovy/com/cognifide/apm/core/grammar/ScriptRunnerTest.groovy b/app/aem/core/src/test/groovy/com/cognifide/apm/core/grammar/ScriptRunnerTest.groovy index 4073eece..fa981be3 100644 --- a/app/aem/core/src/test/groovy/com/cognifide/apm/core/grammar/ScriptRunnerTest.groovy +++ b/app/aem/core/src/test/groovy/com/cognifide/apm/core/grammar/ScriptRunnerTest.groovy @@ -23,10 +23,12 @@ package com.cognifide.apm.core.grammar import com.cognifide.apm.api.scripts.Script import com.cognifide.apm.api.services.ScriptFinder import com.cognifide.apm.api.status.Status +import com.cognifide.apm.core.crypto.DecryptionService import com.cognifide.apm.core.grammar.datasource.DataSource import com.cognifide.apm.core.grammar.datasource.DataSourceInvoker import com.cognifide.apm.core.progress.ProgressImpl import org.apache.commons.io.IOUtils +import org.apache.commons.lang3.reflect.FieldUtils import org.apache.sling.api.resource.ResourceResolver import spock.lang.Specification @@ -242,6 +244,7 @@ class ScriptRunnerTest extends Specification { private static DataSourceInvoker createDataSourceInvoker() { def dataSourceInvoker = new DataSourceInvoker() + FieldUtils.writeField(dataSourceInvoker, "decryptionService", new DecryptionService(), true) def bindDataSource = DataSourceInvoker.class.getDeclaredMethod("bindDataSource", DataSource.class) bindDataSource.setAccessible(true) bindDataSource.invoke(dataSourceInvoker, new DataSource() { @@ -251,8 +254,8 @@ class ScriptRunnerTest extends Specification { } @Override - ApmType determine(ResourceResolver resolver, List parameters) { - return parameters.get(0) + ApmType determine(ResourceResolver resolver, List parameters) { + return new ApmString(parameters.get(0)) } }) bindDataSource.invoke(dataSourceInvoker, new DataSource() { @@ -262,8 +265,8 @@ class ScriptRunnerTest extends Specification { } @Override - ApmType determine(ResourceResolver resolver, List parameters) { - return new ApmString(parameters.get(0).getString().toUpperCase()) + ApmType determine(ResourceResolver resolver, List parameters) { + return new ApmString(parameters.get(0).toUpperCase()) } }) return dataSourceInvoker