Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
#276 Can now deploy users via CMA
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudin committed Aug 28, 2018
1 parent ae2e675 commit b96764b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import com.marklogic.appdeployer.command.AbstractResourceCommand;
import com.marklogic.appdeployer.command.CommandContext;
import com.marklogic.appdeployer.command.SortOrderConstants;
import com.marklogic.mgmt.api.API;
import com.marklogic.mgmt.api.security.User;
import com.marklogic.mgmt.mapper.DefaultResourceMapper;
import com.marklogic.mgmt.mapper.ResourceMapper;
import com.marklogic.mgmt.resource.ResourceManager;
import com.marklogic.mgmt.resource.security.UserManager;

import java.io.File;

/**
* As of version 3.9.0, this will now use CMA to deploy users if isOptimizeWithCma on the AppConfig object passed in
* via the CommandContext returns true.
*/
public class DeployUsersCommand extends AbstractResourceCommand {

public DeployUsersCommand() {
Expand All @@ -24,4 +32,56 @@ protected ResourceManager getResourceManager(CommandContext context) {
return new UserManager(context.getManageClient());
}

@Override
protected void processExecuteOnResourceDir(CommandContext context, File resourceDir) {
if (context.getAppConfig().isOptimizeWithCma()) {
deployUsersViaCma(context, resourceDir);
} else {
super.processExecuteOnResourceDir(context, resourceDir);
}
}

/**
* If deploying via CMA, then each user file is read in, unmarshalled into a User object, and then written out as
* JSON. This allows for both JSON and XML files to be supported.
*
* @param context
* @param resourceDir
*/
protected void deployUsersViaCma(CommandContext context, File resourceDir) {
if (resourceDir.exists()) {
ResourceMapper resourceMapper = new DefaultResourceMapper(new API(context.getManageClient()));

StringBuilder sb = new StringBuilder("{\"config\":[{\"user\":[");

boolean foundUser = false;
for (File f : listFilesInDirectory(resourceDir, context)) {
if (logger.isInfoEnabled()) {
logger.info("Processing file: " + f.getAbsolutePath());
}
String payload = copyFileToString(f, context);
User user = resourceMapper.readResource(payload, User.class);
if (foundUser) {
sb.append(",");
}
sb.append(user.getJson());
foundUser = true;
}

if (foundUser) {
sb.append("]}]}");

// Not logging the payload because it can contain passwords
if (logger.isInfoEnabled()) {
logger.info("Submitting configuration containing users");
}
context.getManageClient().postJson("/manage/v3", sb.toString());
if (logger.isInfoEnabled()) {
logger.info("Successfully submitted configuration containing users");
}
}
} else {
logResourceDirectoryNotFound(resourceDir);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.marklogic.appdeployer.command.security;

import com.marklogic.appdeployer.AbstractAppDeployerTest;
import com.marklogic.mgmt.resource.security.UserManager;
import org.junit.Test;

public class DeployUsersViaCmaTest extends AbstractAppDeployerTest {

/**
* Can't really verify that CMA was used other than looking at the logging. Can still verify that the users were
* created.
*/
@Test
public void test() {
UserManager userManager = new UserManager(manageClient);

appConfig.setOptimizeWithCma(true);

initializeAppDeployer(new DeployUsersCommand());

try {
deploySampleApp();

assertTrue(userManager.exists("sample-app-jane"));
assertTrue(userManager.exists("sample-app-john"));
} finally {
undeploySampleApp();

assertFalse(userManager.exists("sample-app-jane"));
assertFalse(userManager.exists("sample-app-john"));
}
}
}

0 comments on commit b96764b

Please sign in to comment.