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

Commit 5cdfd6e

Browse files
committed
#393 Can now deploy privileges with roles that haven't been created yet
1 parent 59cd068 commit 5cdfd6e

File tree

10 files changed

+252
-84
lines changed

10 files changed

+252
-84
lines changed

src/main/java/com/marklogic/appdeployer/command/CommandMapBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public Map<String, List<Command>> buildCommandMap() {
6262
securityCommands.add(new InsertCertificateHostsTemplateCommand());
6363
securityCommands.add(new DeployExternalSecurityCommand());
6464
securityCommands.add(new DeployPrivilegesCommand());
65+
securityCommands.add(new DeployPrivilegeRolesCommand());
6566
securityCommands.add(new DeployProtectedCollectionsCommand());
6667
securityCommands.add(new DeployProtectedPathsCommand());
6768
securityCommands.add(new DeployQueryRolesetsCommand());
@@ -158,7 +159,7 @@ public Map<String, List<Command>> buildCommandMap() {
158159
List<Command> pluginCommands = new ArrayList<>();
159160
pluginCommands.add(new InstallPluginsCommand());
160161
map.put("mlPluginCommands", pluginCommands);
161-
162+
162163
// Tasks
163164
List<Command> taskCommands = new ArrayList<Command>();
164165
taskCommands.add(new DeployScheduledTasksCommand());

src/main/java/com/marklogic/appdeployer/command/SortOrderConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ public abstract class SortOrderConstants {
88
public static Integer DEPLOY_QUERY_ROLESETS = 13; // depends on roles
99
public static Integer DEPLOY_USERS = 15; // depends on roles
1010

11+
// After users are deployed so they're not included in combined CMA requests
12+
public static Integer DEPLOY_PRIVILEGE_ROLES = 18;
13+
1114
public static Integer DEPLOY_CERTIFICATE_AUTHORITIES = 20;
1215
public static Integer DEPLOY_CERTIFICATE_TEMPLATES = 24;
1316
public static Integer GENERATE_TEMPORARY_CERTIFICATE = 25;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.marklogic.appdeployer.command.security;
2+
3+
import com.marklogic.appdeployer.command.AbstractResourceCommand;
4+
import com.marklogic.appdeployer.command.CommandContext;
5+
import com.marklogic.appdeployer.command.ResourceReference;
6+
import com.marklogic.appdeployer.command.SortOrderConstants;
7+
import com.marklogic.mgmt.api.API;
8+
import com.marklogic.mgmt.api.security.Privilege;
9+
import com.marklogic.mgmt.mapper.DefaultResourceMapper;
10+
import com.marklogic.mgmt.mapper.ResourceMapper;
11+
import com.marklogic.mgmt.resource.ResourceManager;
12+
import com.marklogic.mgmt.resource.security.PrivilegeManager;
13+
14+
import java.io.File;
15+
import java.util.function.BiPredicate;
16+
17+
/**
18+
* Intended to run after roles and privileges have been deployed so that any roles associated with privileges can be
19+
* safely deployed.
20+
*/
21+
public class DeployPrivilegeRolesCommand extends AbstractResourceCommand {
22+
23+
private ResourceMapper resourceMapper;
24+
25+
public DeployPrivilegeRolesCommand() {
26+
setExecuteSortOrder(SortOrderConstants.DEPLOY_PRIVILEGE_ROLES);
27+
setUndoSortOrder(SortOrderConstants.DELETE_PRIVILEGES);
28+
29+
setSupportsResourceMerging(true);
30+
setResourceClassType(Privilege.class);
31+
}
32+
33+
@Override
34+
public void undo(CommandContext context) {
35+
logger.info("Nothing to do, as DeployPrivilegesCommand is expected to delete privileges");
36+
}
37+
38+
@Override
39+
protected File[] getResourceDirs(CommandContext context) {
40+
return findResourceDirs(context, configDir -> configDir.getPrivilegesDir());
41+
}
42+
43+
@Override
44+
protected ResourceManager getResourceManager(CommandContext context) {
45+
return new PrivilegeManager(context.getManageClient());
46+
}
47+
48+
@Override
49+
protected String adjustPayloadBeforeSavingResource(CommandContext context, File f, String payload) {
50+
payload = super.adjustPayloadBeforeSavingResource(context, f, payload);
51+
if (payload != null) {
52+
if (resourceMapper == null) {
53+
resourceMapper = new DefaultResourceMapper(new API(context.getManageClient()));
54+
}
55+
Privilege p = resourceMapper.readResource(payload, Privilege.class);
56+
if (p.getRole() == null || p.getRole().isEmpty()) {
57+
return null;
58+
}
59+
}
60+
return payload;
61+
}
62+
63+
@Override
64+
protected BiPredicate<ResourceReference, ResourceReference> getBiPredicateForMergingResources() {
65+
return new PrivilegeBiPredicate();
66+
}
67+
}

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.node.ObjectNode;
44
import com.marklogic.appdeployer.command.*;
5+
import com.marklogic.mgmt.PayloadParser;
56
import com.marklogic.mgmt.api.configuration.Configuration;
67
import com.marklogic.mgmt.api.security.Privilege;
78
import com.marklogic.mgmt.resource.ResourceManager;
@@ -13,6 +14,8 @@
1314

1415
public class DeployPrivilegesCommand extends AbstractResourceCommand implements SupportsCmaCommand {
1516

17+
private boolean removeRolesBeforeSaving = true;
18+
1619
public DeployPrivilegesCommand() {
1720
setExecuteSortOrder(SortOrderConstants.DEPLOY_PRIVILEGES);
1821
setUndoSortOrder(SortOrderConstants.DELETE_PRIVILEGES);
@@ -31,13 +34,23 @@ protected ResourceManager getResourceManager(CommandContext context) {
3134
return new PrivilegeManager(context.getManageClient());
3235
}
3336

37+
@Override
38+
protected String adjustPayloadBeforeSavingResource(CommandContext context, File f, String payload) {
39+
payload = super.adjustPayloadBeforeSavingResource(context, f, payload);
40+
return removeRolesBeforeSaving ? new PayloadParser().excludeProperties(payload, "role") : payload;
41+
}
42+
3443
@Override
3544
public boolean cmaShouldBeUsed(CommandContext context) {
3645
return context.getAppConfig().getCmaConfig().isDeployPrivileges();
3746
}
3847

3948
@Override
4049
public void addResourceToConfiguration(ObjectNode resource, Configuration configuration) {
50+
if (removeRolesBeforeSaving && resource != null && resource.has("role")) {
51+
resource.remove("role");
52+
}
53+
4154
configuration.addPrivilege(resource);
4255
}
4356

@@ -53,16 +66,29 @@ protected void deployConfiguration(CommandContext context, Configuration config)
5366

5467
@Override
5568
protected BiPredicate<ResourceReference, ResourceReference> getBiPredicateForMergingResources() {
56-
return (reference1, reference2) -> {
57-
EqualsBuilder b = new EqualsBuilder();
69+
return new PrivilegeBiPredicate();
70+
}
71+
72+
public boolean isRemoveRolesBeforeSaving() {
73+
return removeRolesBeforeSaving;
74+
}
75+
76+
public void setRemoveRolesBeforeSaving(boolean removeRolesBeforeSaving) {
77+
this.removeRolesBeforeSaving = removeRolesBeforeSaving;
78+
}
79+
}
80+
81+
class PrivilegeBiPredicate implements BiPredicate<ResourceReference, ResourceReference> {
82+
@Override
83+
public boolean test(ResourceReference reference1, ResourceReference reference2) {
84+
EqualsBuilder b = new EqualsBuilder();
5885

59-
final ObjectNode node1 = reference1.getObjectNode();
60-
final ObjectNode node2 = reference2.getObjectNode();
86+
final ObjectNode node1 = reference1.getObjectNode();
87+
final ObjectNode node2 = reference2.getObjectNode();
6188

62-
b.append(node1.get("privilege-name").asText(), node2.get("privilege-name").asText());
63-
b.append(node1.has("kind") ? node1.get("kind").asText() : null, node2.has("kind") ? node2.get("kind").asText() : null);
89+
b.append(node1.get("privilege-name").asText(), node2.get("privilege-name").asText());
90+
b.append(node1.has("kind") ? node1.get("kind").asText() : null, node2.has("kind") ? node2.get("kind").asText() : null);
6491

65-
return b.isEquals();
66-
};
92+
return b.isEquals();
6793
}
6894
}
Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,82 @@
11
package com.marklogic.mgmt.api.security;
22

3-
import com.marklogic.mgmt.resource.ResourceManager;
43
import com.marklogic.mgmt.api.API;
54
import com.marklogic.mgmt.api.Resource;
5+
import com.marklogic.mgmt.resource.ResourceManager;
66
import com.marklogic.mgmt.resource.security.PrivilegeManager;
77

8+
import javax.xml.bind.annotation.*;
89
import java.util.ArrayList;
910
import java.util.List;
1011

12+
@XmlRootElement(name = "privilege-properties")
13+
@XmlAccessorType(XmlAccessType.FIELD)
1114
public class Privilege extends Resource {
1215

13-
private String privilegeName;
14-
private String action;
15-
private String kind;
16-
private List<String> role;
17-
18-
public Privilege() {
19-
super();
20-
}
21-
22-
public Privilege(API api, String privilegeName) {
23-
super(api);
24-
this.privilegeName = privilegeName;
25-
}
26-
27-
public void addRole(String r) {
28-
if (role == null) {
29-
role = new ArrayList<String>();
30-
}
31-
role.add(r);
32-
}
33-
34-
@Override
35-
protected ResourceManager getResourceManager() {
36-
return new PrivilegeManager(getClient());
37-
}
38-
39-
@Override
40-
protected String getResourceId() {
41-
return privilegeName;
42-
}
43-
44-
public String getPrivilegeName() {
45-
return privilegeName;
46-
}
47-
48-
public void setPrivilegeName(String privilegeName) {
49-
this.privilegeName = privilegeName;
50-
}
51-
52-
public String getAction() {
53-
return action;
54-
}
55-
56-
public void setAction(String action) {
57-
this.action = action;
58-
}
59-
60-
public String getKind() {
61-
return kind;
62-
}
63-
64-
public void setKind(String kind) {
65-
this.kind = kind;
66-
}
67-
68-
public List<String> getRole() {
69-
return role;
70-
}
71-
72-
public void setRole(List<String> role) {
73-
this.role = role;
74-
}
16+
@XmlElement(name = "privilege-name")
17+
private String privilegeName;
18+
private String action;
19+
private String kind;
20+
21+
@XmlElementWrapper(name = "roles")
22+
private List<String> role;
23+
24+
public Privilege() {
25+
super();
26+
}
27+
28+
public Privilege(API api, String privilegeName) {
29+
super(api);
30+
this.privilegeName = privilegeName;
31+
}
32+
33+
public void addRole(String r) {
34+
if (role == null) {
35+
role = new ArrayList<>();
36+
}
37+
role.add(r);
38+
}
39+
40+
@Override
41+
protected ResourceManager getResourceManager() {
42+
return new PrivilegeManager(getClient());
43+
}
44+
45+
@Override
46+
protected String getResourceId() {
47+
return privilegeName;
48+
}
49+
50+
public String getPrivilegeName() {
51+
return privilegeName;
52+
}
53+
54+
public void setPrivilegeName(String privilegeName) {
55+
this.privilegeName = privilegeName;
56+
}
57+
58+
public String getAction() {
59+
return action;
60+
}
61+
62+
public void setAction(String action) {
63+
this.action = action;
64+
}
65+
66+
public String getKind() {
67+
return kind;
68+
}
69+
70+
public void setKind(String kind) {
71+
this.kind = kind;
72+
}
73+
74+
public List<String> getRole() {
75+
return role;
76+
}
77+
78+
public void setRole(List<String> role) {
79+
this.role = role;
80+
}
7581

7682
}
Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,60 @@
11
package com.marklogic.appdeployer.command.security;
22

3+
import com.marklogic.appdeployer.ConfigDir;
34
import com.marklogic.appdeployer.command.AbstractManageResourceTest;
45
import com.marklogic.appdeployer.command.Command;
6+
import com.marklogic.mgmt.api.API;
7+
import com.marklogic.mgmt.api.security.Privilege;
8+
import com.marklogic.mgmt.mapper.DefaultResourceMapper;
59
import com.marklogic.mgmt.resource.ResourceManager;
610
import com.marklogic.mgmt.resource.security.PrivilegeManager;
11+
import org.junit.Test;
12+
13+
import java.io.File;
714

815
/**
916
* TODO Unable to update a privilege of kind "uri".
1017
*/
1118
public class ManagePrivilegesTest extends AbstractManageResourceTest {
1219

13-
@Override
14-
protected ResourceManager newResourceManager() {
15-
return new PrivilegeManager(manageClient);
16-
}
20+
@Override
21+
protected ResourceManager newResourceManager() {
22+
return new PrivilegeManager(manageClient);
23+
}
24+
25+
@Override
26+
protected Command newCommand() {
27+
return new DeployPrivilegesCommand();
28+
}
29+
30+
@Override
31+
protected String[] getResourceNames() {
32+
return new String[]{"sample-app-execute-1", "sample-app-execute-2"};
33+
}
34+
35+
@Test
36+
public void privilegeWithRole() {
37+
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/privileges-with-roles")));
38+
39+
initializeAppDeployer(new DeployPrivilegesCommand(), new DeployRolesCommand(), new DeployPrivilegeRolesCommand());
40+
try {
41+
deploySampleApp();
1742

18-
@Override
19-
protected Command newCommand() {
20-
return new DeployPrivilegesCommand();
21-
}
43+
String json = new PrivilegeManager(manageClient).getPropertiesAsJson("sample-app-execute-1", "kind", "execute");
44+
Privilege p = new DefaultResourceMapper(new API(manageClient)).readResource(json, Privilege.class);
45+
assertEquals("sample-app-role1", p.getRole().get(0));
46+
assertEquals("manage-user", p.getRole().get(1));
2247

23-
@Override
24-
protected String[] getResourceNames() {
25-
return new String[] { "sample-app-execute-1", "sample-app-execute-2" };
26-
}
48+
json = new PrivilegeManager(manageClient).getPropertiesAsJson("sample-app-xml-privilege", "kind", "execute");
49+
p = new DefaultResourceMapper(new API(manageClient)).readResource(json, Privilege.class);
50+
assertEquals("sample-app-role1", p.getRole().get(0));
51+
assertEquals("rest-admin", p.getRole().get(1));
2752

53+
json = new PrivilegeManager(manageClient).getPropertiesAsJson("sample-app-execute-3", "kind", "execute");
54+
p = new DefaultResourceMapper(new API(manageClient)).readResource(json, Privilege.class);
55+
assertNull(p.getRole());
56+
} finally {
57+
undeploySampleApp();
58+
}
59+
}
2860
}

0 commit comments

Comments
 (0)