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

Commit b96764b

Browse files
committed
#276 Can now deploy users via CMA
1 parent ae2e675 commit b96764b

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/main/java/com/marklogic/appdeployer/command/security/DeployUsersCommand.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
import com.marklogic.appdeployer.command.AbstractResourceCommand;
44
import com.marklogic.appdeployer.command.CommandContext;
55
import com.marklogic.appdeployer.command.SortOrderConstants;
6+
import com.marklogic.mgmt.api.API;
7+
import com.marklogic.mgmt.api.security.User;
8+
import com.marklogic.mgmt.mapper.DefaultResourceMapper;
9+
import com.marklogic.mgmt.mapper.ResourceMapper;
610
import com.marklogic.mgmt.resource.ResourceManager;
711
import com.marklogic.mgmt.resource.security.UserManager;
812

913
import java.io.File;
1014

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

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

35+
@Override
36+
protected void processExecuteOnResourceDir(CommandContext context, File resourceDir) {
37+
if (context.getAppConfig().isOptimizeWithCma()) {
38+
deployUsersViaCma(context, resourceDir);
39+
} else {
40+
super.processExecuteOnResourceDir(context, resourceDir);
41+
}
42+
}
43+
44+
/**
45+
* If deploying via CMA, then each user file is read in, unmarshalled into a User object, and then written out as
46+
* JSON. This allows for both JSON and XML files to be supported.
47+
*
48+
* @param context
49+
* @param resourceDir
50+
*/
51+
protected void deployUsersViaCma(CommandContext context, File resourceDir) {
52+
if (resourceDir.exists()) {
53+
ResourceMapper resourceMapper = new DefaultResourceMapper(new API(context.getManageClient()));
54+
55+
StringBuilder sb = new StringBuilder("{\"config\":[{\"user\":[");
56+
57+
boolean foundUser = false;
58+
for (File f : listFilesInDirectory(resourceDir, context)) {
59+
if (logger.isInfoEnabled()) {
60+
logger.info("Processing file: " + f.getAbsolutePath());
61+
}
62+
String payload = copyFileToString(f, context);
63+
User user = resourceMapper.readResource(payload, User.class);
64+
if (foundUser) {
65+
sb.append(",");
66+
}
67+
sb.append(user.getJson());
68+
foundUser = true;
69+
}
70+
71+
if (foundUser) {
72+
sb.append("]}]}");
73+
74+
// Not logging the payload because it can contain passwords
75+
if (logger.isInfoEnabled()) {
76+
logger.info("Submitting configuration containing users");
77+
}
78+
context.getManageClient().postJson("/manage/v3", sb.toString());
79+
if (logger.isInfoEnabled()) {
80+
logger.info("Successfully submitted configuration containing users");
81+
}
82+
}
83+
} else {
84+
logResourceDirectoryNotFound(resourceDir);
85+
}
86+
}
2787
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.marklogic.appdeployer.command.security;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.mgmt.resource.security.UserManager;
5+
import org.junit.Test;
6+
7+
public class DeployUsersViaCmaTest extends AbstractAppDeployerTest {
8+
9+
/**
10+
* Can't really verify that CMA was used other than looking at the logging. Can still verify that the users were
11+
* created.
12+
*/
13+
@Test
14+
public void test() {
15+
UserManager userManager = new UserManager(manageClient);
16+
17+
appConfig.setOptimizeWithCma(true);
18+
19+
initializeAppDeployer(new DeployUsersCommand());
20+
21+
try {
22+
deploySampleApp();
23+
24+
assertTrue(userManager.exists("sample-app-jane"));
25+
assertTrue(userManager.exists("sample-app-john"));
26+
} finally {
27+
undeploySampleApp();
28+
29+
assertFalse(userManager.exists("sample-app-jane"));
30+
assertFalse(userManager.exists("sample-app-john"));
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)