You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/07/04 11:32:49 UTC
[sling-whiteboard] branch master updated: Testing for the Whitelist
Enforcer
This is an automated email from the ASF dual-hosted git repository.
davidb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
The following commit(s) were added to refs/heads/master by this push:
new 9e8706b Testing for the Whitelist Enforcer
9e8706b is described below
commit 9e8706b6caaabcb60512756675d5e742c9eebfa1
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Wed Jul 4 12:32:16 2018 +0100
Testing for the Whitelist Enforcer
---
.../feature/service/impl/FeatureServiceImpl.java | 2 +-
.../sling/feature/whitelist/WhitelistService.java | 12 +++--
.../feature/whitelist/impl/ResolverHookImpl.java | 6 +--
.../feature/whitelist/impl/WhitelistEnforcer.java | 15 ++++---
.../whitelist/impl/WhitelistServiceImpl.java | 6 +--
.../whitelist/impl/WhitelistEnforcerTest.java | 52 ++++++++++++++++++++++
.../whitelist/impl/WhitelistServiceImplTest.java | 8 ++--
7 files changed, 80 insertions(+), 21 deletions(-)
diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeatureServiceImpl.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeatureServiceImpl.java
index a551343..3af0068 100644
--- a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeatureServiceImpl.java
+++ b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeatureServiceImpl.java
@@ -28,7 +28,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-public class FeatureServiceImpl implements FeatureService {
+class FeatureServiceImpl implements FeatureService {
private final Set<Feature> features;
private final Map<Long, Feature> bundleFeatureMap;
diff --git a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/WhitelistService.java b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/WhitelistService.java
index 0136b9c..1a2712a 100644
--- a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/WhitelistService.java
+++ b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/WhitelistService.java
@@ -26,10 +26,16 @@ public interface WhitelistService {
/**
* List the regions that a given feature is part of
* @param feature The feature ID in mvn syntax.
- * @return A set of regions.
+ * @return A set of regions or {@code null} if the feature is no known.
*/
Set<String> listRegions(String feature);
- boolean regionContainsPackage(String region, String packageName);
-
+ /**
+ * Returns whether a package is whitelisted for the given region.
+ * @param region The region
+ * @param packageName The package
+ * @return Returns whether the package is whitelisted for the region.
+ * If the region is not known {@code null} is returned.
+ */
+ Boolean regionWhitelistsPackage(String region, String packageName);
}
diff --git a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/ResolverHookImpl.java b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/ResolverHookImpl.java
index 6327951..4926593 100644
--- a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/ResolverHookImpl.java
+++ b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/ResolverHookImpl.java
@@ -32,7 +32,7 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
-public class ResolverHookImpl implements ResolverHook {
+class ResolverHookImpl implements ResolverHook {
private static final long SERVICE_WAIT_TIMEOUT = 60000;
private final ServiceTracker<FeatureService, FeatureService> featureServiceTracker;
@@ -96,7 +96,7 @@ public class ResolverHookImpl implements ResolverHook {
if (pkg instanceof String) {
String packageName = (String) pkg;
// If the export is in the global region.
- if (whitelistService.regionContainsPackage(WhitelistService.GLOBAL_REGION, packageName))
+ if (whitelistService.regionWhitelistsPackage(WhitelistService.GLOBAL_REGION, packageName))
continue nextCapability;
// If the export is in a region that the feature is also in, then allow
@@ -105,7 +105,7 @@ public class ResolverHookImpl implements ResolverHook {
if (WhitelistService.GLOBAL_REGION.equals(region))
continue;
- if (whitelistService.regionContainsPackage(region, packageName))
+ if (whitelistService.regionWhitelistsPackage(region, packageName))
continue nextCapability;
}
diff --git a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcer.java b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcer.java
index 609c34f..95e25d2 100644
--- a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcer.java
+++ b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcer.java
@@ -40,7 +40,7 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
-public class WhitelistEnforcer implements ResolverHookFactory, ManagedService {
+class WhitelistEnforcer implements ResolverHookFactory, ManagedService {
private static final String CONFIG_REGION_MAPPING_PREFIX = "whitelist.region.";
private static final String CONFIG_FEATURE_MAPPING_PREFIX = "whitelist.feature.";
static final Logger LOG = LoggerFactory.getLogger(WhitelistEnforcer.class);
@@ -48,7 +48,7 @@ public class WhitelistEnforcer implements ResolverHookFactory, ManagedService {
final ServiceTracker<FeatureService, FeatureService> featureServiceTracker;
volatile WhitelistService whitelistService = new NullWhitelistService();
- public WhitelistEnforcer(ServiceTracker<FeatureService, FeatureService> tracker) {
+ WhitelistEnforcer(ServiceTracker<FeatureService, FeatureService> tracker) {
featureServiceTracker = tracker;
}
@@ -84,27 +84,28 @@ public class WhitelistEnforcer implements ResolverHookFactory, ManagedService {
whitelistService = new WhitelistServiceImpl(rpm, frm);
}
- private Set<String> getStringPlusValue(Object val) {
+ Set<String> getStringPlusValue(Object val) {
if (val == null)
return null;
if (val instanceof Collection) {
- return ((Collection<?>) val).stream().map(Object::toString).collect(Collectors.toSet());
+ return ((Collection<?>) val).stream().map(Object::toString)
+ .collect(Collectors.toSet());
} else if (val instanceof String[]) {
return new HashSet<>(Arrays.asList((String[]) val));
}
return Collections.singleton(val.toString());
}
- private static class NullWhitelistService implements WhitelistService {
+ static class NullWhitelistService implements WhitelistService {
@Override
public Set<String> listRegions(String feature) {
return null;
}
@Override
- public boolean regionContainsPackage(String region, String packageName) {
- return true;
+ public Boolean regionWhitelistsPackage(String region, String packageName) {
+ return null;
}
}
diff --git a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceImpl.java b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceImpl.java
index e3a4c8a..2243f7e 100644
--- a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceImpl.java
+++ b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceImpl.java
@@ -26,7 +26,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-public class WhitelistServiceImpl implements WhitelistService {
+class WhitelistServiceImpl implements WhitelistService {
final Map<String, Set<String>> featureRegionMapping;
final Map<String, Set<String>> regionPackageMapping;
@@ -53,11 +53,11 @@ public class WhitelistServiceImpl implements WhitelistService {
}
@Override
- public boolean regionContainsPackage(String region, String packageName) {
+ public Boolean regionWhitelistsPackage(String region, String packageName) {
Set<String> packages = regionPackageMapping.get(region);
if (packages == null)
- return false;
+ return null;
return packages.contains(packageName);
}
diff --git a/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcerTest.java b/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcerTest.java
index 145b0af..6173511 100644
--- a/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcerTest.java
+++ b/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcerTest.java
@@ -18,6 +18,58 @@
*/
package org.apache.sling.feature.whitelist.impl;
+import org.apache.sling.feature.whitelist.WhitelistService;
+import org.junit.Test;
+import org.osgi.service.cm.ConfigurationException;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
public class WhitelistEnforcerTest {
+ @Test
+ public void testWhitelistEnforcerConfigUpdate() throws ConfigurationException {
+ WhitelistEnforcer enf = new WhitelistEnforcer(null);
+
+ assertTrue("Precondition",
+ enf.whitelistService instanceof WhitelistEnforcer.NullWhitelistService);
+
+ Dictionary<String, Object> props = new Hashtable<>();
+ props.put("ignored", "ignored-too");
+ props.put("whitelist.region.region1", "org.foo.pkg1");
+ props.put("whitelist.region.region2", new String[] {"pkga", "pkgb"});
+ props.put("whitelist.region.region.3", Arrays.asList("a.b.c", "d.e.f", "g.h.i"));
+ props.put("whitelist.feature.gid:aid:slingfeature:testfeature:1.0.0", WhitelistService.GLOBAL_REGION);
+ props.put("whitelist.feature.gid:myfeature:1.0.0", new String [] {"region1", "region2"});
+ enf.updated(props);
+
+ assertFalse(enf.whitelistService instanceof WhitelistEnforcer.NullWhitelistService);
+
+ // check that the configuration parsing worked
+ assertTrue(enf.whitelistService.regionWhitelistsPackage("region1", "org.foo.pkg1"));
+ assertTrue(enf.whitelistService.regionWhitelistsPackage("region2", "pkga"));
+ assertTrue(enf.whitelistService.regionWhitelistsPackage("region2", "pkgb"));
+ assertFalse(enf.whitelistService.regionWhitelistsPackage("region1", "pkg1"));
+ assertTrue(enf.whitelistService.regionWhitelistsPackage("region.3", "d.e.f"));
+ assertFalse(enf.whitelistService.regionWhitelistsPackage("region.3", "d.e.f.g"));
+ assertNull(enf.whitelistService.regionWhitelistsPackage("unknown", "pkga"));
+
+ Set<String> regions = enf.whitelistService.listRegions("gid:myfeature:1.0.0");
+ assertEquals(new HashSet<String>(Arrays.asList("region1", "region2")), regions);
+ assertEquals(Collections.singleton("global"), enf.whitelistService
+ .listRegions("gid:aid:slingfeature:testfeature:1.0.0"));
+ assertNull(enf.whitelistService.listRegions("unknown"));
+ enf.updated(null);
+ assertTrue("A null configuration should put back the null whitelist service",
+ enf.whitelistService instanceof WhitelistEnforcer.NullWhitelistService);
+ }
}
diff --git a/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceImplTest.java b/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceImplTest.java
index 9168958..57a3d0b 100644
--- a/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceImplTest.java
+++ b/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceImplTest.java
@@ -53,9 +53,9 @@ public class WhitelistServiceImplTest {
rpm.put("region1", new HashSet<>(Arrays.asList("org.foo", "org.bar", "org.foo.bar")));
WhitelistService wls = new WhitelistServiceImpl(rpm, Collections.emptyMap());
- assertTrue(wls.regionContainsPackage("region1", "org.foo"));
- assertTrue(wls.regionContainsPackage("region1", "org.foo.bar"));
- assertFalse(wls.regionContainsPackage("region1", "org.bar.foo"));
- assertFalse(wls.regionContainsPackage("nonexitent", "org.foo"));
+ assertTrue(wls.regionWhitelistsPackage("region1", "org.foo"));
+ assertTrue(wls.regionWhitelistsPackage("region1", "org.foo.bar"));
+ assertFalse(wls.regionWhitelistsPackage("region1", "org.bar.foo"));
+ assertNull(wls.regionWhitelistsPackage("nonexitent", "org.foo"));
}
}