sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject [sling-org-apache-sling-feature-cpconverter] branch master updated: SLING-8387 - ACL Repoinit statements after conversion miss out create path statements
Date Fri, 03 May 2019 11:08:14 GMT
This is an automated email from the ASF dual-hosted git repository.

simonetripodi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git


The following commit(s) were added to refs/heads/master by this push:
     new 636da3e  SLING-8387 - ACL Repoinit statements after conversion miss out create path
statements
636da3e is described below

commit 636da3edd4f2e7a4728693914146b2e899e47c30
Author: stripodi <stripodi@simos-mbp>
AuthorDate: Fri May 3 13:08:07 2019 +0200

    SLING-8387 - ACL Repoinit statements after conversion miss out create
    path statements
---
 .../ContentPackage2FeatureModelConverter.java      |  2 +-
 .../sling/feature/cpconverter/acl/AclManager.java  | 52 +++++++++++++++++++++-
 .../feature/cpconverter/acl/PrimaryTypeParser.java | 48 ++++++++++++++++++++
 .../handlers/RepPolicyEntryHandler.java            |  1 +
 .../handlers/SystemUsersEntryHandler.java          |  1 +
 .../handlers/XmlConfigurationEntryHandler.java     |  4 +-
 .../AbstractJcrNodeParser.java                     | 20 ++++++---
 .../feature/cpconverter/shared/package-info.java   | 21 +++++++++
 .../cpconverter/vltpkg/VaultPackageAssembler.java  | 10 ++++-
 .../handlers/RepPolicyEntryHandlerTest.java        | 17 +++++--
 .../handlers/SystemUsersEntryHandlerTest.java      |  9 +++-
 11 files changed, 171 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
b/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
index 3a97fba..1c25660 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
@@ -300,7 +300,7 @@ public class ContentPackage2FeatureModelConverter {
 
             // finally serialize the Feature Model(s) file(s)
 
-            aclManager.addRepoinitExtension(getTargetFeature());
+            aclManager.addRepoinitExtension(mainPackageAssembler, getTargetFeature());
 
             logger.info("Conversion complete!");
 
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/acl/AclManager.java b/src/main/java/org/apache/sling/feature/cpconverter/acl/AclManager.java
index 54564c9..b929520 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/acl/AclManager.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/acl/AclManager.java
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.feature.cpconverter.acl;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.util.Formatter;
 import java.util.HashMap;
 import java.util.LinkedHashSet;
@@ -23,18 +25,26 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.TreeSet;
 
 import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.cpconverter.vltpkg.VaultPackageAssembler;
 
 /**
  * The Manager able to collect and build System Users and related ACL policies.
  */
 public final class AclManager {
 
+    private static final String CONTENT_XML_FILE_NAME = ".content.xml";
+
+    private static final String DEFAULT_TYPE = "sling:Folder";
+
     private final Set<String> systemUsers = new LinkedHashSet<>();
 
+    private final Set<String> paths = new TreeSet<String>();
+
     private final Map<String, List<Acl>> acls = new HashMap<>();
 
     public boolean addSystemUser(String systemUser) {
@@ -45,12 +55,23 @@ public final class AclManager {
     }
 
     public Acl addAcl(String systemUser, String operation, String privileges, String path)
{
+        addPath(path);
+
         Acl acl = new Acl(operation, privileges, path);
         acls.computeIfAbsent(systemUser, k -> new LinkedList<>()).add(acl);
         return acl;
     }
 
-    public void addRepoinitExtension(Feature feature) {
+    private void addPath(String path) {
+        paths.add(path);
+
+        int endIndex = path.lastIndexOf('/');
+        if (endIndex > 0) {
+            addPath(path.substring(0, endIndex));
+        }
+    }
+
+    public void addRepoinitExtension(VaultPackageAssembler packageAssembler, Feature feature)
{
         if (systemUsers.isEmpty()) {
             return;
         }
@@ -59,9 +80,38 @@ public final class AclManager {
 
         Formatter formatter = new Formatter();
 
+        // make sure all paths are created first
+
+        for (String path : paths) {
+            File currentDir = packageAssembler.getEntry(path);
+            String type = DEFAULT_TYPE;
+
+            if (currentDir.exists()) {
+                File currentContent = new File(currentDir, CONTENT_XML_FILE_NAME);
+                if (currentContent.exists()) {
+                    try (FileInputStream input = new FileInputStream(currentContent)) {
+                        type = new PrimaryTypeParser(DEFAULT_TYPE).parse(input);
+                    } catch (Exception e) {
+                        throw new RuntimeException("A fatal error occurred while parsing
the '"
+                                                   + currentContent
+                                                   + "' file, see nested exceptions: "
+                                                   + e);
+                    } finally {
+                        formatter.close();
+                    }
+                }
+            }
+
+            formatter.format("create path (%s) %s%n", type, path);
+        }
+
         for (String systemUser : systemUsers) {
+            // create then the users
+
             formatter.format("create service user %s%n", systemUser);
 
+            // ACL can now be set
+
             List<Acl> authorizations = acls.get(systemUser);
             if (authorizations != null && !authorizations.isEmpty()) {
                 formatter.format("set ACL for %s%n", systemUser);
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/acl/PrimaryTypeParser.java
b/src/main/java/org/apache/sling/feature/cpconverter/acl/PrimaryTypeParser.java
new file mode 100644
index 0000000..cf7a8fc
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/cpconverter/acl/PrimaryTypeParser.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.feature.cpconverter.acl;
+
+import org.apache.sling.feature.cpconverter.shared.AbstractJcrNodeParser;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+final class PrimaryTypeParser extends AbstractJcrNodeParser<String> {
+
+    private String detectedPrimaryType;
+
+    public PrimaryTypeParser(String primaryType) {
+        super(primaryType);
+    }
+
+    @Override
+    protected void onJcrRootNode(String uri, String localName, String qName, Attributes attributes,
String primaryType)
+            throws SAXException {
+        detectedPrimaryType = primaryType;
+    }
+
+    @Override
+    protected void onJcrRootElement(String uri, String localName, String qName, Attributes
attributes)
+            throws SAXException {
+        // not needed
+    }
+
+    @Override
+    protected String getParsingResult() {
+        return detectedPrimaryType != null ? detectedPrimaryType : getPrimaryType();
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/handlers/RepPolicyEntryHandler.java
b/src/main/java/org/apache/sling/feature/cpconverter/handlers/RepPolicyEntryHandler.java
index 6dad716..7e0109a 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/handlers/RepPolicyEntryHandler.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/handlers/RepPolicyEntryHandler.java
@@ -29,6 +29,7 @@ import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
 import org.apache.sling.feature.cpconverter.acl.Acl;
 import org.apache.sling.feature.cpconverter.acl.AclManager;
+import org.apache.sling.feature.cpconverter.shared.AbstractJcrNodeParser;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/handlers/SystemUsersEntryHandler.java
b/src/main/java/org/apache/sling/feature/cpconverter/handlers/SystemUsersEntryHandler.java
index 7fe64aa..17e22c0 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/handlers/SystemUsersEntryHandler.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/handlers/SystemUsersEntryHandler.java
@@ -21,6 +21,7 @@ import java.io.InputStream;
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
+import org.apache.sling.feature.cpconverter.shared.AbstractJcrNodeParser;
 import org.xml.sax.Attributes;
 
 public final class SystemUsersEntryHandler extends AbstractRegexEntryHandler {
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/handlers/XmlConfigurationEntryHandler.java
b/src/main/java/org/apache/sling/feature/cpconverter/handlers/XmlConfigurationEntryHandler.java
index 18ce031..9e4c1b6 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/handlers/XmlConfigurationEntryHandler.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/handlers/XmlConfigurationEntryHandler.java
@@ -21,6 +21,7 @@ import java.util.Dictionary;
 import java.util.Hashtable;
 
 import org.apache.jackrabbit.vault.util.DocViewProperty;
+import org.apache.sling.feature.cpconverter.shared.AbstractJcrNodeParser;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 
@@ -34,8 +35,7 @@ public final class XmlConfigurationEntryHandler extends AbstractConfigurationEnt
     protected Dictionary<String, Object> parseConfiguration(String name, InputStream
input) throws Exception {
         JcrConfigurationHandler configurationHandler = new JcrConfigurationHandler();
         try {
-            configurationHandler.parse(input);
-            return configurationHandler.getParsingResult();
+            return configurationHandler.parse(input);
         } catch (Exception e) {
             logger.warn("Current OSGi configuration does not represent a valid XML document,
see nested exceptions", e);
             return null;
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/handlers/AbstractJcrNodeParser.java
b/src/main/java/org/apache/sling/feature/cpconverter/shared/AbstractJcrNodeParser.java
similarity index 73%
rename from src/main/java/org/apache/sling/feature/cpconverter/handlers/AbstractJcrNodeParser.java
rename to src/main/java/org/apache/sling/feature/cpconverter/shared/AbstractJcrNodeParser.java
index e346ff3..5fc42ec 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/handlers/AbstractJcrNodeParser.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/shared/AbstractJcrNodeParser.java
@@ -14,7 +14,7 @@
  * License for the specific language governing permissions and limitations under
  * the License.
  */
-package org.apache.sling.feature.cpconverter.handlers;
+package org.apache.sling.feature.cpconverter.shared;
 
 import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
 
@@ -27,7 +27,7 @@ import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
 
-abstract class AbstractJcrNodeParser<O> extends DefaultHandler {
+public abstract class AbstractJcrNodeParser<O> extends DefaultHandler {
 
     private static final String JCR_ROOT = "jcr:root";
 
@@ -39,16 +39,26 @@ abstract class AbstractJcrNodeParser<O> extends DefaultHandler {
         this.primaryType = primaryType;
     }
 
-    public void parse(InputStream input) throws Exception {
+    public O parse(InputStream input) throws Exception {
         SAXParser saxParser = saxParserFactory.newSAXParser();
         saxParser.parse(input, this);
+        return getParsingResult();
     }
 
     @Override
     public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
-        String primaryType = attributes.getValue(JCR_PRIMARYTYPE);
+        if (JCR_ROOT.equals(qName)) {
+            String primaryType = attributes.getValue(JCR_PRIMARYTYPE);
+            onJcrRootNode(uri, localName, qName, attributes, primaryType);
+        }
+    }
+
+    protected final String getPrimaryType() {
+        return primaryType;
+    }
 
-        if (JCR_ROOT.equals(qName) && this.primaryType.equals(primaryType)) {
+    protected void onJcrRootNode(String uri, String localName, String qName, Attributes attributes,
String primaryType) throws SAXException {
+        if (this.primaryType.equals(primaryType)) {
             onJcrRootElement(uri, localName, qName, attributes);
         }
     }
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/shared/package-info.java b/src/main/java/org/apache/sling/feature/cpconverter/shared/package-info.java
new file mode 100644
index 0000000..64ee331
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/cpconverter/shared/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+/**
+ * Shared common utilities across converter packages.
+ */
+package org.apache.sling.feature.cpconverter.shared;
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
b/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
index 941bf27..48e77d1 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
@@ -42,7 +42,7 @@ import org.codehaus.plexus.archiver.Archiver;
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 import org.codehaus.plexus.archiver.util.DefaultFileSet;
 
-public final class VaultPackageAssembler implements EntryHandler {
+public class VaultPackageAssembler implements EntryHandler {
 
     private static final String JCR_ROOT_DIR_NAME = "jcr_root";
 
@@ -153,6 +153,14 @@ public final class VaultPackageAssembler implements EntryHandler {
         }
     }
 
+    public File getEntry(String path) {
+        if (!path.startsWith(JCR_ROOT_DIR_NAME)) {
+            path = JCR_ROOT_DIR_NAME + path;
+        }
+
+        return new File(storingDirectory, path);
+    }
+
     public File createPackage() throws IOException {
         return createPackage(TMP_DIR);
     }
diff --git a/src/test/java/org/apache/sling/feature/cpconverter/handlers/RepPolicyEntryHandlerTest.java
b/src/test/java/org/apache/sling/feature/cpconverter/handlers/RepPolicyEntryHandlerTest.java
index ecf5419..acb57dd 100644
--- a/src/test/java/org/apache/sling/feature/cpconverter/handlers/RepPolicyEntryHandlerTest.java
+++ b/src/test/java/org/apache/sling/feature/cpconverter/handlers/RepPolicyEntryHandlerTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.feature.cpconverter.handlers;
 
+import static org.mockito.ArgumentMatchers.anyString;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -25,6 +26,8 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
+import java.io.File;
+
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.sling.feature.ArtifactId;
@@ -32,6 +35,7 @@ import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
+import org.apache.sling.feature.cpconverter.vltpkg.VaultPackageAssembler;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -72,7 +76,9 @@ public final class RepPolicyEntryHandlerTest {
         assertNotNull(repoinitExtension);
         assertEquals(ExtensionType.TEXT, repoinitExtension.getType());
 
-        String expected = "create service user acs-commons-ensure-oak-index-service\n" +

+        String expected = "create path (sling:Folder) /asd\n" + 
+                "create path (sling:Folder) /asd/public\n" + 
+                "create service user acs-commons-ensure-oak-index-service\n" + 
                 "set ACL for acs-commons-ensure-oak-index-service\n" + 
                 "allow jcr:read,rep:write,rep:indexDefinitionManagement on /asd/public restriction(*/oak:index/*)\n"
+ 
                 "end\n" + 
@@ -109,7 +115,9 @@ public final class RepPolicyEntryHandlerTest {
         assertNotNull(repoinitExtension);
         assertEquals(ExtensionType.TEXT, repoinitExtension.getType());
 
-        String expected = "create service user acs-commons-package-replication-status-event-service\n"
+ 
+        String expected = "create path (sling:Folder) /asd\n" + 
+                "create path (sling:Folder) /asd/public\n" + 
+                "create service user acs-commons-package-replication-status-event-service\n"
+ 
                 "set ACL for acs-commons-package-replication-status-event-service\n" + 
                 "allow jcr:read,rep:write,jcr:readAccessControl,jcr:modifyAccessControl on
/asd/public\n" + 
                 "end\n" + 
@@ -139,6 +147,7 @@ public final class RepPolicyEntryHandlerTest {
         String path = "jcr_root/asd/public/_rep_policy.xml";
         Archive archive = mock(Archive.class);
         Entry entry = mock(Entry.class);
+        VaultPackageAssembler packageAssembler = mock(VaultPackageAssembler.class);
 
         when(archive.openInputStream(entry)).thenReturn(getClass().getResourceAsStream(path));
 
@@ -154,7 +163,9 @@ public final class RepPolicyEntryHandlerTest {
             }
         }
 
-        converter.getAclManager().addRepoinitExtension(feature);
+        when(packageAssembler.getEntry(anyString())).thenReturn(new File("itdoesnotexist"));
+
+        converter.getAclManager().addRepoinitExtension(packageAssembler, feature);
         return feature.getExtensions().getByName(Extension.EXTENSION_NAME_REPOINIT);
     }
 
diff --git a/src/test/java/org/apache/sling/feature/cpconverter/handlers/SystemUsersEntryHandlerTest.java
b/src/test/java/org/apache/sling/feature/cpconverter/handlers/SystemUsersEntryHandlerTest.java
index 70270d0..dde6411 100644
--- a/src/test/java/org/apache/sling/feature/cpconverter/handlers/SystemUsersEntryHandlerTest.java
+++ b/src/test/java/org/apache/sling/feature/cpconverter/handlers/SystemUsersEntryHandlerTest.java
@@ -21,8 +21,11 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.*;
 
+import java.io.File;
+
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.sling.feature.ArtifactId;
@@ -30,6 +33,7 @@ import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
+import org.apache.sling.feature.cpconverter.vltpkg.VaultPackageAssembler;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -80,6 +84,7 @@ public class SystemUsersEntryHandlerTest {
     private Extension parseAndSetRepoinit(String path) throws Exception {
         Archive archive = mock(Archive.class);
         Entry entry = mock(Entry.class);
+        VaultPackageAssembler packageAssembler = mock(VaultPackageAssembler.class);
 
         when(archive.openInputStream(entry)).thenReturn(getClass().getResourceAsStream(path));
 
@@ -89,7 +94,9 @@ public class SystemUsersEntryHandlerTest {
 
         systemUsersEntryHandler.handle(path, archive, entry, converter);
 
-        converter.getAclManager().addRepoinitExtension(feature);
+        when(packageAssembler.getEntry(anyString())).thenReturn(new File("itdoesnotexist"));
+
+        converter.getAclManager().addRepoinitExtension(packageAssembler, feature);
         return feature.getExtensions().getByName(Extension.EXTENSION_NAME_REPOINIT);
     }
 


Mime
View raw message