Skip to content

Commit

Permalink
Merge pull request #346 from wttech/feature/install-cloud-mode
Browse files Browse the repository at this point in the history
Feature/install cloud mode
  • Loading branch information
dprzybyl authored May 4, 2022
2 parents 0fd4386 + 41bb375 commit c826f63
Show file tree
Hide file tree
Showing 34 changed files with 391 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import com.cognifide.apm.api.actions.ActionResult;
import com.cognifide.apm.api.actions.Context;
import com.cognifide.apm.api.exceptions.ActionExecutionException;
import com.cognifide.apm.api.status.Status;
import com.cognifide.apm.main.permissions.PermissionActionHelper;
import com.cognifide.apm.main.permissions.Restrictions;
import com.cognifide.apm.main.permissions.exceptions.PermissionException;
import com.cognifide.apm.main.utils.MessagingUtils;
import com.cognifide.apm.main.utils.PathUtils;
import java.util.Collections;
import java.util.List;
import javax.jcr.PathNotFoundException;
Expand Down Expand Up @@ -72,23 +74,27 @@ private ActionResult process(final Context context, boolean simulate) {
try {
Authorizable authorizable = context.getCurrentAuthorizable();
actionResult.setAuthorizable(authorizable.getID());
context.getSession().getNode(path);
final PermissionActionHelper permissionActionHelper = new PermissionActionHelper(
context.getValueFactory(), path, permissions, restrictions);
LOGGER.info(String.format("Adding permissions %s for authorizable with id = %s for path = %s %s",
permissions.toString(), context.getCurrentAuthorizable().getID(), path, restrictions));
if (simulate) {
permissionActionHelper.checkPermissions(context.getAccessControlManager());
if (context.isCompositeNodeStore() && PathUtils.isAppsOrLibsPath(path)) {
actionResult.changeStatus(Status.SKIPPED, "Skipped adding allow privilege for " + authorizable.getID() + " on " + path);
} else {
permissionActionHelper.applyPermissions(context.getAccessControlManager(), authorizable.getPrincipal(), true);
}
actionResult.logMessage("Added allow privilege for " + authorizable.getID() + " on " + path);
if (permissions.contains("MODIFY")) {
String preparedGlob = recalculateGlob(restrictions.getGlob());
new Allow(path, Collections.singletonList("MODIFY_PAGE"),
preparedGlob + "*/jcr:content*", restrictions.getNtNames(), restrictions.getItemNames(),
ignoreNonExistingPaths
).process(context, simulate);
context.getSession().getNode(path);
final PermissionActionHelper permissionActionHelper = new PermissionActionHelper(
context.getValueFactory(), path, permissions, restrictions);
LOGGER.info(String.format("Adding permissions %s for authorizable with id = %s for path = %s %s",
permissions.toString(), context.getCurrentAuthorizable().getID(), path, restrictions));
if (simulate) {
permissionActionHelper.checkPermissions(context.getAccessControlManager());
} else {
permissionActionHelper.applyPermissions(context.getAccessControlManager(), authorizable.getPrincipal(), true);
}
actionResult.logMessage("Added allow privilege for " + authorizable.getID() + " on " + path);
if (permissions.contains("MODIFY")) {
String preparedGlob = recalculateGlob(restrictions.getGlob());
new Allow(path, Collections.singletonList("MODIFY_PAGE"),
preparedGlob + "*/jcr:content*", restrictions.getNtNames(), restrictions.getItemNames(),
ignoreNonExistingPaths
).process(context, simulate);
}
}
} catch (final PathNotFoundException e) {
if (ignoreNonExistingPaths) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.cognifide.apm.api.exceptions.ActionExecutionException;
import com.cognifide.apm.api.status.Status;
import com.cognifide.apm.main.utils.MessagingUtils;
import com.cognifide.apm.main.utils.PathUtils;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
Expand Down Expand Up @@ -63,12 +64,16 @@ private ActionResult process(final Context context, boolean execute) {
try {
Authorizable authorizable = context.getCurrentAuthorizable();
actionResult.setAuthorizable(authorizable.getID());
LOGGER.info(String.format("Purging privileges for authorizable with id = %s under path = %s",
authorizable.getID(), path));
if (execute) {
purge(context, actionResult);
if (context.isCompositeNodeStore() && PathUtils.isAppsOrLibsPath(path)) {
actionResult.changeStatus(Status.SKIPPED, "Skipped purging privileges for " + authorizable.getID() + " on " + path);
} else {
LOGGER.info(String.format("Purging privileges for authorizable with id = %s under path = %s",
authorizable.getID(), path));
if (execute) {
purge(context, actionResult);
}
actionResult.logMessage("Purged privileges for " + authorizable.getID() + " on " + path);
}
actionResult.logMessage("Purged privileges for " + authorizable.getID() + " on " + path);
} catch (RepositoryException | ActionExecutionException e) {
actionResult.logError(MessagingUtils.createMessage(e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import com.cognifide.apm.api.actions.ActionResult;
import com.cognifide.apm.api.actions.Context;
import com.cognifide.apm.api.exceptions.ActionExecutionException;
import com.cognifide.apm.api.status.Status;
import com.cognifide.apm.main.permissions.utils.JackrabbitAccessControlListUtil;
import com.cognifide.apm.main.utils.MessagingUtils;
import com.cognifide.apm.main.utils.PathUtils;
import java.security.Principal;
import javax.jcr.RepositoryException;
import javax.jcr.security.AccessControlEntry;
Expand Down Expand Up @@ -59,12 +61,16 @@ private ActionResult process(final Context context, boolean execute) {
try {
Authorizable authorizable = context.getCurrentAuthorizable();
actionResult.setAuthorizable(authorizable.getID());
LOGGER.info(String.format("Removing all priveleges for authorizable with id = %s on path = %s",
authorizable.getID(), path));
if (execute) {
removeAll(context, authorizable);
if (context.isCompositeNodeStore() && PathUtils.isAppsOrLibsPath(path)) {
actionResult.changeStatus(Status.SKIPPED, "Skipped removing all privileges for " + authorizable.getID() + " on " + path);
} else {
LOGGER.info(String.format("Removing all priveleges for authorizable with id = %s on path = %s",
authorizable.getID(), path));
if (execute) {
removeAll(context, authorizable);
}
actionResult.logMessage("Removed all privileges for " + authorizable.getID() + " on " + path);
}
actionResult.logMessage("Removed all privileges for " + authorizable.getID() + " on " + path);
} catch (RepositoryException | ActionExecutionException e) {
actionResult.logError(MessagingUtils.createMessage(e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import com.cognifide.apm.api.actions.ActionResult;
import com.cognifide.apm.api.actions.Context;
import com.cognifide.apm.api.exceptions.ActionExecutionException;
import com.cognifide.apm.api.status.Status;
import com.cognifide.apm.main.permissions.PermissionActionHelper;
import com.cognifide.apm.main.permissions.Restrictions;
import com.cognifide.apm.main.permissions.exceptions.PermissionException;
import com.cognifide.apm.main.utils.MessagingUtils;
import com.cognifide.apm.main.utils.PathUtils;
import java.util.ArrayList;
import java.util.List;
import javax.jcr.PathNotFoundException;
Expand Down Expand Up @@ -72,25 +74,29 @@ private ActionResult process(final Context context, boolean simulate) {
try {
Authorizable authorizable = context.getCurrentAuthorizable();
actionResult.setAuthorizable(authorizable.getID());
context.getSession().getNode(path);
final PermissionActionHelper permissionActionHelper = new PermissionActionHelper(
context.getValueFactory(), path, permissions, restrictions);
LOGGER.info(String.format("Denying permissions %s for authorizable with id = %s for path = %s %s",
permissions.toString(), context.getCurrentAuthorizable().getID(), path, restrictions));
if (simulate) {
permissionActionHelper.checkPermissions(context.getAccessControlManager());
if (context.isCompositeNodeStore() && PathUtils.isAppsOrLibsPath(path)) {
actionResult.changeStatus(Status.SKIPPED, "Skipped adding deny privilege for " + authorizable.getID() + " on " + path);
} else {
permissionActionHelper.applyPermissions(context.getAccessControlManager(), authorizable.getPrincipal(), false);
}
actionResult.logMessage("Added deny privilege for " + authorizable.getID() + " on " + path);
if (permissions.contains("MODIFY")) {
List<String> globModifyPermission = new ArrayList<>();
globModifyPermission.add("MODIFY_PAGE");
String preparedGlob = recalculateGlob(restrictions.getGlob());
new Deny(path, globModifyPermission,
preparedGlob + "*/jcr:content*", restrictions.getNtNames(), restrictions.getItemNames(),
ignoreNonExistingPaths)
.process(context, simulate);
context.getSession().getNode(path);
final PermissionActionHelper permissionActionHelper = new PermissionActionHelper(
context.getValueFactory(), path, permissions, restrictions);
LOGGER.info(String.format("Denying permissions %s for authorizable with id = %s for path = %s %s",
permissions.toString(), context.getCurrentAuthorizable().getID(), path, restrictions));
if (simulate) {
permissionActionHelper.checkPermissions(context.getAccessControlManager());
} else {
permissionActionHelper.applyPermissions(context.getAccessControlManager(), authorizable.getPrincipal(), false);
}
actionResult.logMessage("Added deny privilege for " + authorizable.getID() + " on " + path);
if (permissions.contains("MODIFY")) {
List<String> globModifyPermission = new ArrayList<>();
globModifyPermission.add("MODIFY_PAGE");
String preparedGlob = recalculateGlob(restrictions.getGlob());
new Deny(path, globModifyPermission,
preparedGlob + "*/jcr:content*", restrictions.getNtNames(), restrictions.getItemNames(),
ignoreNonExistingPaths)
.process(context, simulate);
}
}
} catch (final PathNotFoundException e) {
if (ignoreNonExistingPaths) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
* limitations under the License.
* =========================LICENSE_END==================================
*/
package com.cognifide.apm.core.launchers;
package com.cognifide.apm.main.utils;

import org.apache.commons.lang.WordUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

enum LauncherType {
SCHEDULED, REPLICATED, STARTUP, STARTUP_MODIFIED;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class PathUtils {

@Override
public String toString() {
return WordUtils.capitalizeFully(name());
public static boolean isAppsOrLibsPath(String path) {
return path.startsWith("/apps") || path.startsWith("/libs");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ public interface Context {
ActionResult createActionResult();

Context newContext();

boolean isCompositeNodeStore();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@
*/
package com.cognifide.apm.api.services;

import com.cognifide.apm.api.scripts.Script;
import java.util.Map;

import javax.jcr.RepositoryException;

import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.ResourceResolver;

import com.cognifide.apm.api.scripts.Script;

public interface ScriptManager {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ private Property() {

public static final String CHANGE_TYPE = ResourceChangeListener.CHANGES + "=";

public static final String VENDOR = Constants.SERVICE_VENDOR + "=Cognifide";
public static final String VENDOR = Constants.SERVICE_VENDOR + "=Wunderman Thompson Technology";

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
immediate = true,
service = MapperContext.class,
property = {
Property.DESCRIPTION + "Mapper Context service",
Property.DESCRIPTION + "APM Mapper Context service",
Property.VENDOR
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import com.cognifide.apm.api.actions.annotations.Flag;
import com.cognifide.apm.api.actions.annotations.Named;
import com.cognifide.apm.api.actions.annotations.Required;
import com.cognifide.apm.core.crypto.DecryptionService;
import com.cognifide.apm.core.grammar.ApmType;
import com.cognifide.apm.core.grammar.argument.Arguments;
import com.cognifide.apm.core.crypto.DecryptionService;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.cognifide.apm.core.actions.ActionResultImpl;
import com.cognifide.apm.core.sessions.SessionSavingPolicyImpl;
import com.cognifide.apm.core.utils.AuthorizableManagerImpl;
import com.cognifide.apm.core.utils.RuntimeUtils;
import javax.jcr.RepositoryException;
import javax.jcr.ValueFactory;
import javax.jcr.security.AccessControlManager;
Expand Down Expand Up @@ -54,11 +55,15 @@ public final class ContextImpl implements Context {
@Setter
private Authorizable currentAuthorizable;

@Getter
private boolean compositeNodeStore;

public ContextImpl(final JackrabbitSession session) throws RepositoryException {
this.session = session;
this.accessControlManager = session.getAccessControlManager();
this.authorizableManager = new AuthorizableManagerImpl(session.getUserManager());
this.savingPolicy = new SessionSavingPolicyImpl();
this.compositeNodeStore = RuntimeUtils.determineCompositeNodeStore(session);
}

private ContextImpl(AccessControlManager accessControlManager,
Expand All @@ -68,6 +73,7 @@ private ContextImpl(AccessControlManager accessControlManager,
this.authorizableManager = authorizableManager;
this.savingPolicy = savingPolicy;
this.session = session;
this.compositeNodeStore = RuntimeUtils.determineCompositeNodeStore(session);
}

@Override
Expand Down Expand Up @@ -118,4 +124,5 @@ public ActionResult createActionResult() {
public Context newContext() {
return new ContextImpl(accessControlManager, authorizableManager, savingPolicy, session);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@

package com.cognifide.apm.core.history;

import com.cognifide.apm.core.services.ResourceResolverProvider;
import com.cognifide.apm.core.utils.sling.SlingHelper;
import java.util.Calendar;
import lombok.extern.slf4j.Slf4j;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.serviceusermapping.ServiceUserMapped;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
Expand All @@ -48,10 +47,7 @@ public class HistoryAutocleanService implements Runnable {
private Config config;

@Reference
private ResourceResolverFactory resolverFactory;

@Reference
private ServiceUserMapped serviceUserMapped;
private ResourceResolverProvider resolverProvider;

@Reference
private History history;
Expand All @@ -64,8 +60,8 @@ public void activate(Config config) {

@Override
public void run() {
SlingHelper.operateTraced(resolverFactory, this::deleteHistoryByEntries);
SlingHelper.operateTraced(resolverFactory, this::deleteHistoryByDays);
SlingHelper.operateTraced(resolverProvider, this::deleteHistoryByEntries);
SlingHelper.operateTraced(resolverProvider, this::deleteHistoryByDays);
}

private void deleteHistoryByEntries(ResourceResolver resolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ public interface HistoryEntry {
Calendar getExecutionTimeCalendar();

String getScriptContentPath();

boolean isCompositeNodeStore();

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class HistoryEntryImpl implements HistoryEntry {
public static final String PROGRESS_LOG = "summaryJSON";
public static final String UPLOAD_TIME = "uploadTime";
public static final String SCRIPT_CONTENT_PATH = "scriptContentPath";
public static final String COMPOSITE_NODE_STORE = "compositeNodeStore";

@Inject
@Named(AUTHOR)
Expand Down Expand Up @@ -98,6 +99,10 @@ public class HistoryEntryImpl implements HistoryEntry {
@Named(SCRIPT_CONTENT_PATH)
private String scriptContentPath;

@Inject
@Named(COMPOSITE_NODE_STORE)
private boolean compositeNodeStore;

private final String path;

private Calendar executionTimeCalendar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class HistoryEntryWriter {
private final Boolean isRunSuccessful;
private final String mode;
private final String progressLog;
private final boolean compositeNodeStore;

public void writeTo(Resource historyLogResource) {
ModifiableValueMap valueMap = historyLogResource.adaptTo(ModifiableValueMap.class);
Expand All @@ -47,5 +48,6 @@ public void writeTo(Resource historyLogResource) {
valueMap.put(HistoryEntryImpl.IS_RUN_SUCCESSFUL, isRunSuccessful);
valueMap.put(HistoryEntryImpl.EXECUTION_TIME, executionTime);
valueMap.put(HistoryEntryImpl.EXECUTOR, executor);
valueMap.put(HistoryEntryImpl.COMPOSITE_NODE_STORE, compositeNodeStore);
}
}
Loading

0 comments on commit c826f63

Please sign in to comment.