Skip to content

Commit

Permalink
Merge pull request #348 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 13, 2022
2 parents 3c88bd8 + f4304de commit d1ef3e1
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@

import java.math.BigInteger;
import java.security.SecureRandom;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

public class RandomPasswordGenerator {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class RandomPasswordGenerator {

public static final int MAX_BITS = 130;
private static final int MAX_BITS = 130;

public static final int RADIX = 32;
private static final int RADIX = 32;

private final SecureRandom random = new SecureRandom();
private static final SecureRandom random = new SecureRandom();

public String getRandomPassword() {
public static String getRandomPassword() {
return new BigInteger(MAX_BITS, random).toString(RADIX);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,41 @@ public class CreateAuthorizable implements Action {

private final String password;

private final String externalId;

private final Boolean ignoreIfExists;

private final CreateAuthorizableStrategy createStrategy;

public CreateAuthorizable(final String id, final String password, final String path,
final Boolean ignoreIfExists, final CreateAuthorizableStrategy createStrategy) {
public CreateAuthorizable(String id, String password, String path, String externalId,
Boolean ignoreIfExists, CreateAuthorizableStrategy createStrategy) {
this.id = id;
this.password = password;
this.path = path;
this.externalId = externalId;
this.ignoreIfExists = ignoreIfExists;
this.createStrategy = createStrategy;
}

@Override
public ActionResult simulate(final Context context) {
public ActionResult simulate(Context context) {
return process(context, true);
}

@Override
public ActionResult execute(final Context context) {
public ActionResult execute(Context context) {
return process(context, false);
}

public ActionResult process(final Context context, boolean simulate) {
public ActionResult process(Context context, boolean simulate) {
ActionResult actionResult = context.createActionResult();
try {
Authorizable authorizable = context.getAuthorizableManager().getAuthorizableIfExists(id);
LOGGER.info("Creating authorizable with id = " + id);
if (authorizable != null) {
logMessage(actionResult, authorizable);
} else {
authorizable = createStrategy.create(id, password, path, context, actionResult, simulate);
authorizable = createStrategy.create(id, password, path, externalId, context, actionResult, simulate);
}
context.setCurrentAuthorizable(authorizable);
} catch (RepositoryException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.cognifide.apm.main.RandomPasswordGenerator;
import java.security.Principal;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
Expand All @@ -33,12 +34,16 @@ public enum CreateAuthorizableStrategy {

GROUP {
@Override
public Group create(final String id, final String password, final String path, final Context context,
final ActionResult actionResult, boolean simulate) throws RepositoryException {
final Principal namePrincipal = context.getAuthorizableManager().createMockPrincipal(id);
public Group create(String id, String password, String path, String externalId,
Context context, ActionResult actionResult, boolean simulate) throws RepositoryException {
Principal namePrincipal = context.getAuthorizableManager().createMockPrincipal(id);
Group group;
if (!simulate) {
group = context.getAuthorizableManager().createGroup(id, namePrincipal, path);
if (externalId != null) {
Value value = context.getValueFactory().createValue(externalId);
group.setProperty("rep:externalId", value);
}
} else {
group = context.getAuthorizableManager().createMockGroup(id);
}
Expand All @@ -50,14 +55,13 @@ public Group create(final String id, final String password, final String path, f

USER {
@Override
public User create(String id, String password, String path, Context context,
ActionResult actionResult, boolean simulate) throws RepositoryException {
final RandomPasswordGenerator randomPasswordGenerator = new RandomPasswordGenerator();
final Principal namePrincipal = context.getAuthorizableManager().createMockPrincipal(id);
public User create(String id, String password, String path, String externalId,
Context context, ActionResult actionResult, boolean simulate) throws RepositoryException {
Principal namePrincipal = context.getAuthorizableManager().createMockPrincipal(id);
User user;
if (!simulate) {
user = context.getAuthorizableManager().createUser(
id, StringUtils.isBlank(password) ? randomPasswordGenerator.getRandomPassword() : password,
id, StringUtils.isBlank(password) ? RandomPasswordGenerator.getRandomPassword() : password,
namePrincipal, path);
} else {
user = context.getAuthorizableManager().createMockUser(id);
Expand All @@ -70,8 +74,8 @@ public User create(String id, String password, String path, Context context,

SYSTEM_USER {
@Override
public User create(String id, String password, String path, Context context,
ActionResult actionResult, boolean simulate) throws RepositoryException {
public User create(String id, String password, String path, String externalId,
Context context, ActionResult actionResult, boolean simulate) throws RepositoryException {
User user;
if (!simulate) {
user = context.getAuthorizableManager().createSystemUser(id, path);
Expand All @@ -84,8 +88,7 @@ public User create(String id, String password, String path, Context context,
}
};

public abstract Authorizable create(final String id, final String password, final String path,
final Context context, final ActionResult actionResult, boolean simulate)
throws RepositoryException;
public abstract Authorizable create(String id, String password, String path, String externalId,
Context context, ActionResult actionResult, boolean simulate) throws RepositoryException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ public class CreateGroupMapper {
@Mapping(
examples = {
"CREATE-GROUP 'authors'",
"CREATE-GROUP 'authors' path= '/home/users/client/domain' --ERROR-IF-EXISTS"
"CREATE-GROUP 'authors' path='/home/groups/client/domain' --ERROR-IF-EXISTS",
"CREATE-GROUP 'authors' path='/home/groups/client/domain' externalId='authors'"
},
reference = REFERENCE
)
public Action mapAction(
@Required(value = "groupId", description = "group's id e.g.: 'authors'") String groupId,
@Named(value = "path", description = "group's home e.g.: '/home/groups/domain'") String path,
@Named(value = "path", description = "group's home e.g.: '/home/groups/client/domain'") String path,
@Named(value = "externalId", description = "group's external id e.g.: 'authors'") String externalId,
@Flag(value = ERROR_IF_EXISTS, description = "if group already exists, raise an error and stop script execution") boolean errorIfExists) {
return new CreateAuthorizable(groupId, null, path, !errorIfExists, GROUP);
return new CreateAuthorizable(groupId, null, path, externalId, !errorIfExists, GROUP);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ public class CreateSystemUserMapper {
examples = {
"CREATE-SYSTEM-USER 'apm-user'",
"CREATE-SYSTEM-USER 'apm-user' --ERROR-IF-EXISTS",
"CREATE-SYSTEM-USER 'apm-user' path= '/home/users/client/domain'",
"CREATE-SYSTEM-USER 'apm-user' path= '/home/users/client/domain' BEGIN\n" +
"CREATE-SYSTEM-USER 'apm-user' path='/home/users/system/client/domain'",
"CREATE-SYSTEM-USER 'apm-user' path='/home/users/system/client/domain' BEGIN\n" +
" SET-PROPERTY 'first-name' 'APM'\n" +
"END"
},
reference = REFERENCE
)
public Action mapAction(
@Required(value = "userId", description = "user's login e.g.: 'apm-user'") String userId,
@Named(value = "path", description = "user's home e.g.: '/home/users/domain'") String path,
@Named(value = "path", description = "user's home e.g.: '/home/users/system/client/domain'") String path,
@Flag(value = ERROR_IF_EXISTS, description = "if user already exists, raise an error and stop script execution") boolean errorIfExists) {
return new CreateAuthorizable(userId, null, path, !errorIfExists, SYSTEM_USER);
return new CreateAuthorizable(userId, null, path, null, !errorIfExists, SYSTEM_USER);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public class CreateUserMapper {
@Mapping(
examples = {
"CREATE-USER 'author'",
"CREATE-USER 'author' password= 'p@$$w0rd' --ERROR-IF-EXISTS",
"CREATE-USER 'author' path= '/home/users/client/domain'",
"CREATE-USER 'author' path= '/home/users/client/domain' BEGIN\n" +
"CREATE-USER 'author' password='p@$$w0rd' --ERROR-IF-EXISTS",
"CREATE-USER 'author' path='/home/users/client/domain'",
"CREATE-USER 'author' path='/home/users/client/domain' BEGIN\n" +
" SET-PROPERTY 'first-name' 'Author'\n" +
"END"
},
Expand All @@ -49,9 +49,9 @@ public class CreateUserMapper {
public Action mapAction(
@Required(value = "userId", description = "user's login e.g.: 'author'") String userId,
@Named(value = "password", description = "user's password e.g.: 'p@$$w0rd'") String password,
@Named(value = "path", description = "user's home e.g.: '/home/users/domain'") String path,
@Named(value = "path", description = "user's home e.g.: '/home/users/client/domain'") String path,
@Flag(value = ERROR_IF_EXISTS, description = "if user already exists, raise an error and stop script execution") boolean errorIfExists) {
return new CreateAuthorizable(userId, password, path, !errorIfExists, USER);
return new CreateAuthorizable(userId, password, path, null, !errorIfExists, USER);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ private void processScripts(Configuration config, ResourceResolver resolver) thr
boolean compositeNodeStore = RuntimeUtils.determineCompositeNodeStore(configurationAdmin);
logger.info("compositeNodeStore = {}", compositeNodeStore);
List<Script> scripts = Arrays.stream(config.scriptPaths())
.map(scriptPath -> scriptFinder.find(scriptPath, resolver))
.map(scriptPath -> {
logger.info("scriptPath = {}", scriptPath);
Script script = scriptFinder.find(scriptPath, resolver);
logger.info("scriptPath = {} script.exists = {}", scriptPath, script != null);
return script;
})
.filter(Objects::nonNull)
.filter(script -> {
List<Script> subtree = referenceFinder.findReferences(script);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.collect.Maps;
import java.util.HashMap;
import java.util.Map;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
Expand All @@ -48,6 +49,18 @@ public class ResourceResolverProvider {
@Reference
private ServiceUserMapped serviceUserMapped;

@Reference(target = "(models.adapter.implementationClass=com.cognifide.apm.core.scripts.ScriptModel)")
private AdapterFactory scriptModelAdapterFactory;

@Reference(target = "(models.adapter.implementationClass=com.cognifide.apm.core.services.version.ScriptVersionModel)")
private AdapterFactory scriptVersionModelAdapterFactory;

@Reference(target = "(models.adapter.implementationClass=com.cognifide.apm.core.history.ScriptHistoryImpl)")
private AdapterFactory scriptHistoryImplAdapterFactory;

@Reference(target = "(models.adapter.implementationClass=com.cognifide.apm.core.history.HistoryEntryImpl)")
private AdapterFactory historyEntryImplAdapterFactory;

public ResourceResolver getResourceResolver(String userId) throws LoginException {
ResourceResolver resolver;
if (userId != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ScriptRunner(
executionContext.createScriptContext(loadScript)
try {
arguments.named.forEach { (key, value) -> executionContext.setVariable(key, value) }
progress(ctx, Status.SUCCESS, "run", "Begin: path= ${loadScript.path}", arguments)
progress(ctx, Status.SUCCESS, "run", "Begin: path=${loadScript.path}", arguments)
visit(loadScript.apm)
progress(ctx, Status.SUCCESS, "run", "End")
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CREATE-GROUP "test_group_1"
CREATE-GROUP "test_group_2"

# We can specify path where group will be created.
CREATE-GROUP "test_group_3" path= "/home/groups/company/cognifide"
CREATE-GROUP "test_group_3" path="/home/groups/company/cognifide"

# This action will create user. If user already exists, action and following block of code are skipped.
CREATE-USER "test_user_1" BEGIN
Expand All @@ -23,10 +23,10 @@ CREATE-USER "test_user_2" BEGIN
END

# We can also set password while creating user.
CREATE-USER "test_user_3" password= "password3"
CREATE-USER "test_user_3" password="password3"

# We can specify path where user will be created.
CREATE-USER "test_user_4" path= "/home/users/company/cognifide"
CREATE-USER "test_user_4" path="/home/users/company/cognifide"

# For these actions we need context. To set context use "FOR-USER" or "FOR-GROUP" action.
FOR-USER "test_user_1" BEGIN
Expand Down

0 comments on commit d1ef3e1

Please sign in to comment.