You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by si...@apache.org on 2019/05/09 12:48:19 UTC

[sling-org-apache-sling-feature-cpconverter] 03/03: SLING-8390 - Converter not handling serviceusers and acls spread across multiple packages

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

commit 43f883fafd699e40e18d2780075480329e148cb8
Author: stripodi <st...@simos-mbp>
AuthorDate: Thu May 9 14:48:07 2019 +0200

    SLING-8390 - Converter not handling serviceusers and acls spread across
    multiple packages
    
    improved paths management
---
 .../apache/sling/feature/cpconverter/acl/Acl.java  |  20 ++--
 .../feature/cpconverter/acl/DefaultAclManager.java | 112 +++++++++++++--------
 2 files changed, 84 insertions(+), 48 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/cpconverter/acl/Acl.java b/src/main/java/org/apache/sling/feature/cpconverter/acl/Acl.java
index 0f483ff..0e21ead 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/acl/Acl.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/acl/Acl.java
@@ -16,10 +16,8 @@
  */
 package org.apache.sling.feature.cpconverter.acl;
 
-import java.util.Formatter;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.stream.Collectors;
 
 /**
  * Simple single ACL statement representation.
@@ -46,14 +44,20 @@ public final class Acl {
         }
     }
 
-    protected void addAclStatement(Formatter formatter) {
-        formatter.format("%s %s on %s", operation, privileges, path);
+    public String getOperation() {
+        return operation;
+    }
 
-        if (!restrictions.isEmpty()) {
-            formatter.format(" restriction(%s)", restrictions.stream().collect(Collectors.joining(",")));
-        }
+    public String getPrivileges() {
+        return privileges;
+    }
+
+    public String getPath() {
+        return path;
+    }
 
-        formatter.format("%n");
+    public List<String> getRestrictions() {
+        return restrictions;
     }
 
 }
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/acl/DefaultAclManager.java b/src/main/java/org/apache/sling/feature/cpconverter/acl/DefaultAclManager.java
index d6f50eb..f05164c 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/acl/DefaultAclManager.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/acl/DefaultAclManager.java
@@ -20,13 +20,15 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.util.Formatter;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeSet;
-import java.util.Map.Entry;
+import java.util.stream.Collectors;
 
 import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.ExtensionType;
@@ -41,9 +43,9 @@ public final class DefaultAclManager implements AclManager {
 
     private final Set<String> preProvidedSystemUsers = new LinkedHashSet<>();
 
-    private final Set<String> systemUsers = new LinkedHashSet<>();
+    private final Set<String> preProvidedPaths = new HashSet<String>();
 
-    private final Set<String> paths = new TreeSet<String>();
+    private final Set<String> systemUsers = new LinkedHashSet<>();
 
     private final Map<String, List<Acl>> acls = new HashMap<>();
 
@@ -55,19 +57,19 @@ public final class DefaultAclManager implements 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;
     }
 
-    private void addPath(String path) {
-        paths.add(path);
+    private void addPath(String path, Set<String> paths) {
+        if (preProvidedPaths.add(path)) {
+            paths.add(path);
+        }
 
         int endIndex = path.lastIndexOf('/');
         if (endIndex > 0) {
-            addPath(path.substring(0, endIndex));
+            addPath(path.substring(0, endIndex), paths);
         }
     }
 
@@ -82,35 +84,18 @@ public final class DefaultAclManager implements AclManager {
 
             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);
-                        }
-                    }
-                }
+            for (String systemUser : systemUsers) {
+                List<Acl> authorizations = acls.remove(systemUser);
 
-                formatter.format("create path (%s) %s%n", type, path);
-            }
+                // make sure all paths are created first
 
-            // create then the users
+                addPaths(authorizations, packageAssembler, formatter);
+
+                // create then the users
 
-            for (String systemUser : systemUsers) {
                 formatter.format("create service user %s%n", systemUser);
 
-                List<Acl> authorizations = acls.remove(systemUser);
+                // finally add ACLs
 
                 addAclStatement(formatter, systemUser, authorizations);
             }
@@ -122,7 +107,7 @@ public final class DefaultAclManager implements AclManager {
 
                 if (preProvidedSystemUsers.contains(systemUser)) {
                     List<Acl> authorizations = currentAcls.getValue();
-
+                    addPaths(authorizations, packageAssembler, formatter);
                     addAclStatement(formatter, systemUser, authorizations);
                 }
             }
@@ -140,20 +125,67 @@ public final class DefaultAclManager implements AclManager {
 
     public void reset() {
         systemUsers.clear();
-        paths.clear();
         acls.clear();
     }
 
-    private void addAclStatement(Formatter formatter, String systemUser, List<Acl> authorizations) {
-        if (authorizations != null && !authorizations.isEmpty()) {
-            formatter.format("set ACL for %s%n", systemUser);
+    private void addPaths(List<Acl> authorizations, VaultPackageAssembler packageAssembler, Formatter formatter) {
+        if (areEmpty(authorizations)) {
+            return;
+        }
+
+        Set<String> paths = new TreeSet<String>();
+        for (Acl authorization : authorizations) {
+            addPath(authorization.getPath(), paths);
+        }
+
+        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);
+                    }
+                }
+            }
+
+            formatter.format("create path (%s) %s%n", type, path);
+        }
+    }
+
+    private static void addAclStatement(Formatter formatter, String systemUser, List<Acl> authorizations) {
+        if (areEmpty(authorizations)) {
+            return;
+        }
+
+        formatter.format("set ACL for %s%n", systemUser);
 
-            for (Acl authorization : authorizations) {
-                authorization.addAclStatement(formatter);
+        for (Acl authorization : authorizations) {
+            formatter.format("%s %s on %s",
+                             authorization.getOperation(),
+                             authorization.getPrivileges(),
+                             authorization.getPath());
+
+            if (!authorization.getRestrictions().isEmpty()) {
+                formatter.format(" restriction(%s)",
+                                 authorization.getRestrictions().stream().collect(Collectors.joining(",")));
             }
 
-            formatter.format("end%n");
+            formatter.format("%n");
         }
+
+        formatter.format("end%n");
+    }
+
+    private static boolean areEmpty(List<Acl> authorizations) {
+        return authorizations == null || authorizations.isEmpty();
     }
 
 }