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

Commit f0636dd

Browse files
committed
#327 Can now deploy privileges with CMA
1 parent 7b12603 commit f0636dd

File tree

8 files changed

+110
-5
lines changed

8 files changed

+110
-5
lines changed

src/main/java/com/marklogic/appdeployer/AppConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class AppConfig {
7070
private boolean deployAmpsWithCma = false;
7171
private boolean deployForestsWithCma = false;
7272
private boolean deployRolesWithCma = false;
73+
private boolean deployPrivilegesWithCma = false;
7374

7475
// Used to construct DatabaseClient instances based on inputs defined in this class
7576
private ConfiguredDatabaseClientFactory configuredDatabaseClientFactory = new DefaultConfiguredDatabaseClientFactory();
@@ -1269,4 +1270,12 @@ public boolean isDeployRolesWithCma() {
12691270
public void setDeployRolesWithCma(boolean deployRolesWithCma) {
12701271
this.deployRolesWithCma = deployRolesWithCma;
12711272
}
1273+
1274+
public boolean isDeployPrivilegesWithCma() {
1275+
return deployPrivilegesWithCma;
1276+
}
1277+
1278+
public void setDeployPrivilegesWithCma(boolean deployPrivilegesWithCma) {
1279+
this.deployPrivilegesWithCma = deployPrivilegesWithCma;
1280+
}
12721281
}

src/main/java/com/marklogic/appdeployer/DefaultAppConfigFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public void initialize() {
5858
config.setDeployForestsWithCma(Boolean.parseBoolean(prop));
5959
});
6060

61+
propertyConsumerMap.put("mlDeployPrivilegesWithCma", (config, prop) -> {
62+
logger.info("Deploy privileges" + cmaMessage + prop);
63+
config.setDeployPrivilegesWithCma(Boolean.parseBoolean(prop));
64+
});
65+
6166
propertyConsumerMap.put("mlDeployRolesWithCma", (config, prop) -> {
6267
logger.info("Deploy roles" + cmaMessage + prop);
6368
config.setDeployRolesWithCma(Boolean.parseBoolean(prop));

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
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.appdeployer.command.SupportsCmaCommand;
7+
import com.marklogic.mgmt.api.configuration.Configuration;
8+
import com.marklogic.mgmt.api.security.Privilege;
9+
import com.marklogic.mgmt.mapper.ResourceMapper;
610
import com.marklogic.mgmt.resource.ResourceManager;
711
import com.marklogic.mgmt.resource.security.PrivilegeManager;
812

@@ -12,7 +16,7 @@
1216

1317
import java.io.File;
1418

15-
public class DeployPrivilegesCommand extends AbstractResourceCommand {
19+
public class DeployPrivilegesCommand extends AbstractResourceCommand implements SupportsCmaCommand {
1620

1721
public DeployPrivilegesCommand() {
1822
setExecuteSortOrder(SortOrderConstants.DEPLOY_PRIVILEGES);
@@ -29,4 +33,13 @@ protected ResourceManager getResourceManager(CommandContext context) {
2933
return new PrivilegeManager(context.getManageClient());
3034
}
3135

36+
@Override
37+
public boolean cmaShouldBeUsed(CommandContext context) {
38+
return context.getAppConfig().isDeployPrivilegesWithCma();
39+
}
40+
41+
@Override
42+
public void addResourceToConfiguration(String payload, ResourceMapper resourceMapper, Configuration configuration) {
43+
configuration.addPrivilege(resourceMapper.readResource(payload, Privilege.class));
44+
}
3245
}

src/main/java/com/marklogic/mgmt/api/configuration/Configuration.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.marklogic.mgmt.api.forest.Forest;
66
import com.marklogic.mgmt.api.group.Group;
77
import com.marklogic.mgmt.api.security.Amp;
8+
import com.marklogic.mgmt.api.security.Privilege;
89
import com.marklogic.mgmt.api.security.Role;
910
import com.marklogic.mgmt.api.security.User;
1011
import com.marklogic.mgmt.api.server.Server;
@@ -29,6 +30,9 @@ public class Configuration {
2930
@JsonProperty("group")
3031
private List<Group> groups;
3132

33+
@JsonProperty("privilege")
34+
private List<Privilege> privileges;
35+
3236
@JsonProperty("role")
3337
private List<Role> roles;
3438

@@ -72,7 +76,12 @@ public void addUser(User u) {
7276
if (users == null) users = new ArrayList<>();
7377
users.add(u);
7478
}
75-
79+
80+
public void addPrivilege(Privilege p) {
81+
if (privileges == null) privileges = new ArrayList<>();
82+
privileges.add(p);
83+
}
84+
7685
public List<Amp> getAmps() {
7786
return amps;
7887
}
@@ -128,4 +137,12 @@ public List<User> getUsers() {
128137
public void setUsers(List<User> users) {
129138
this.users = users;
130139
}
140+
141+
public List<Privilege> getPrivileges() {
142+
return privileges;
143+
}
144+
145+
public void setPrivileges(List<Privilege> privileges) {
146+
this.privileges = privileges;
147+
}
131148
}

src/main/java/com/marklogic/mgmt/api/configuration/Configurations.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.marklogic.mgmt.ManageClient;
55
import com.marklogic.mgmt.api.ApiObject;
6+
import com.marklogic.mgmt.cma.ConfigurationManager;
67
import com.marklogic.mgmt.util.ObjectMapperFactory;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
@@ -51,7 +52,7 @@ public void submit(ManageClient manageClient) {
5152
logger.info("Submitting configuration: " + json);
5253
}
5354
}
54-
manageClient.postJson("/manage/v3", json);
55+
new ConfigurationManager(manageClient).submit(json);
5556
if (logger.isInfoEnabled()) {
5657
logger.info("Successfully submitted configuration");
5758
}

src/main/java/com/marklogic/mgmt/cma/ConfigurationManager.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public ConfigurationManager(ManageClient manageClient) {
2525
this.manageClient = manageClient;
2626
}
2727

28+
@Override
29+
protected boolean useSecurityUser() {
30+
return true;
31+
}
32+
2833
/**
2934
* Returns true if the CMA endpoint exists. This temporarily disables logging in MgmtResponseErrorHandler so that
3035
* a client doesn't see the 404 error being logged, which could be mistakenly perceived as a real error.
@@ -36,7 +41,8 @@ public boolean endpointExists() {
3641
if (logger.isInfoEnabled()) {
3742
logger.info("Checking to see if Configuration Management API is available at: " + PATH);
3843
}
39-
manageClient.postJson(PATH, "{}");
44+
final String emptyPayload = "{}";
45+
submit(emptyPayload);
4046
return true;
4147
} catch (HttpClientErrorException ex) {
4248
return false;
@@ -45,6 +51,12 @@ public boolean endpointExists() {
4551
}
4652
}
4753

54+
/**
55+
* Submits the configuration, with some logging before and after.
56+
*
57+
* @param payload
58+
* @return
59+
*/
4860
public SaveReceipt save(String payload) {
4961
String configurationName = payloadParser.getPayloadFieldValue(payload, "name", false);
5062
if (configurationName == null) {
@@ -55,12 +67,17 @@ public SaveReceipt save(String payload) {
5567
logger.info("Applying configuration " + configurationName);
5668
}
5769

58-
ResponseEntity<String> response = postPayload(manageClient, PATH, payload);
70+
SaveReceipt receipt = submit(payload);
5971

6072
if (logger.isInfoEnabled()) {
6173
logger.info("Applied configuration " + configurationName);
6274
}
6375

76+
return receipt;
77+
}
78+
79+
public SaveReceipt submit(String payload) {
80+
ResponseEntity<String> response = postPayload(manageClient, PATH, payload);
6481
return new SaveReceipt(null, payload, PATH, response);
6582
}
6683

src/test/java/com/marklogic/appdeployer/DefaultAppConfigFactoryTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public void allProperties() {
110110

111111
p.setProperty("mlDeployAmpsWithCma", "true");
112112
p.setProperty("mlDeployForestsWithCma", "true");
113+
p.setProperty("mlDeployPrivilegesWithCma", "true");
113114
p.setProperty("mlDeployRolesWithCma", "true");
114115

115116
p.setProperty("mlHost", "prophost");
@@ -211,6 +212,7 @@ public void allProperties() {
211212

212213
assertTrue(config.isDeployAmpsWithCma());
213214
assertTrue(config.isDeployForestsWithCma());
215+
assertTrue(config.isDeployPrivilegesWithCma());
214216
assertTrue(config.isDeployRolesWithCma());
215217

216218
assertEquals("prophost", config.getHost());
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.marklogic.appdeployer.command.security;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.appdeployer.command.CommandContext;
5+
import com.marklogic.mgmt.resource.ResourceManager;
6+
import com.marklogic.mgmt.resource.security.PrivilegeManager;
7+
import org.junit.Test;
8+
9+
public class DeployPrivilegesWithCmaTest extends AbstractAppDeployerTest {
10+
11+
@Test
12+
public void test() {
13+
initializeAppDeployer(new TestDeployPrivilegesCommand());
14+
15+
PrivilegeManager mgr = new PrivilegeManager(manageClient);
16+
appConfig.setDeployPrivilegesWithCma(true);
17+
18+
try {
19+
deploySampleApp();
20+
assertTrue(mgr.exists("sample-app-execute-1"));
21+
assertTrue(mgr.exists("sample-app-execute-1"));
22+
23+
deploySampleApp();
24+
assertTrue(mgr.exists("sample-app-execute-1"));
25+
assertTrue(mgr.exists("sample-app-execute-1"));
26+
} finally {
27+
initializeAppDeployer(new DeployPrivilegesCommand());
28+
29+
undeploySampleApp();
30+
assertFalse(mgr.exists("sample-app-execute-1"));
31+
assertFalse(mgr.exists("sample-app-execute-1"));
32+
}
33+
}
34+
}
35+
36+
class TestDeployPrivilegesCommand extends DeployPrivilegesCommand {
37+
@Override
38+
protected ResourceManager getResourceManager(CommandContext context) {
39+
throw new RuntimeException("This should not be called when deploying with CMA");
40+
}
41+
}

0 commit comments

Comments
 (0)