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

Commit 6d9bf9f

Browse files
committed
#435 Ensuring that 'admin' role cannot be deleted on undeploy
1 parent 6a42371 commit 6d9bf9f

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.marklogic.appdeployer.command.CommandContext;
77
import com.marklogic.appdeployer.command.SortOrderConstants;
88
import com.marklogic.appdeployer.command.SupportsCmaCommand;
9+
import com.marklogic.mgmt.PayloadParser;
910
import com.marklogic.mgmt.SaveReceipt;
1011
import com.marklogic.mgmt.api.configuration.Configuration;
1112
import com.marklogic.mgmt.api.configuration.Configurations;
@@ -19,8 +20,7 @@
1920

2021
import java.io.File;
2122
import java.io.IOException;
22-
import java.util.ArrayList;
23-
import java.util.List;
23+
import java.util.*;
2424

2525
/**
2626
* As of 3.15.0, this no longer deploys roles in two phases. This is due to the new sorting class, which uses a
@@ -34,6 +34,7 @@
3434
public class DeployRolesCommand extends AbstractResourceCommand implements SupportsCmaCommand {
3535

3636
private ObjectNodesSorter objectNodesSorter = new RoleObjectNodesSorter();
37+
private Set<String> defaultRolesToNotUndeploy;
3738

3839
public DeployRolesCommand() {
3940
setExecuteSortOrder(SortOrderConstants.DEPLOY_ROLES);
@@ -42,6 +43,10 @@ public DeployRolesCommand() {
4243
setSupportsResourceMerging(true);
4344
setResourceIdPropertyName("role-name");
4445
setResourceClassType(Role.class);
46+
47+
defaultRolesToNotUndeploy = new HashSet<>();
48+
// "admin" is the main one to never delete, throwing in a couple other sensible ones too
49+
defaultRolesToNotUndeploy.addAll(Arrays.asList("admin", "manage-admin", "security"));
4550
}
4651

4752
/**
@@ -179,8 +184,28 @@ protected ResourceManager getResourceManager(CommandContext context) {
179184
return new RoleManager(context.getManageClient());
180185
}
181186

187+
@Override
188+
protected String adjustPayloadBeforeDeletingResource(ResourceManager mgr, CommandContext context, File f, String payload) {
189+
String roleName = new PayloadParser().getPayloadFieldValue(payload, "role-name", false);
190+
191+
if (roleName != null && defaultRolesToNotUndeploy != null && defaultRolesToNotUndeploy.contains(roleName)) {
192+
logger.info(format("Not undeploying role '%s' because it's in the list of role names to not undeploy", roleName));
193+
return null;
194+
}
195+
196+
return super.adjustPayloadBeforeDeletingResource(mgr, context, f, payload);
197+
}
198+
182199
public void setObjectNodesSorter(ObjectNodesSorter objectNodesSorter) {
183200
this.objectNodesSorter = objectNodesSorter;
184201
}
202+
203+
public Set<String> getDefaultRolesToNotUndeploy() {
204+
return defaultRolesToNotUndeploy;
205+
}
206+
207+
public void setDefaultRolesToNotUndeploy(Set<String> defaultRolesToNotUndeploy) {
208+
this.defaultRolesToNotUndeploy = defaultRolesToNotUndeploy;
209+
}
185210
}
186211

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.marklogic.appdeployer.command.security;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.mgmt.resource.security.RoleManager;
5+
import com.marklogic.mgmt.resource.security.UserManager;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.File;
9+
import java.util.Set;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
public class DontUndeployCertainRolesTest extends AbstractAppDeployerTest {
14+
15+
@Test
16+
public void test() {
17+
final String testRole = "ml-app-deployer-test-role";
18+
final String adminRole = "admin";
19+
20+
appConfig.getFirstConfigDir().setBaseDir(new File("src/test/resources/sample-app/users-to-not-undeploy"));
21+
initializeAppDeployer(new DeployRolesCommand());
22+
23+
RoleManager mgr = new RoleManager(manageClient);
24+
assertFalse(mgr.exists(testRole));
25+
assertTrue(mgr.exists(adminRole));
26+
27+
deploySampleApp();
28+
29+
try {
30+
assertTrue(mgr.exists(testRole));
31+
assertTrue(mgr.exists(adminRole));
32+
} finally {
33+
undeploySampleApp();
34+
35+
assertFalse(mgr.exists(testRole));
36+
assertTrue(mgr.exists(adminRole), "The 'admin' role should not have been deleted since it's in the list of " +
37+
"roles to not undeploy");
38+
}
39+
}
40+
41+
@Test
42+
public void verifySetOfDefaultRoles() {
43+
Set<String> roles = new DeployRolesCommand().getDefaultRolesToNotUndeploy();
44+
assertEquals(3, roles.size(), "The main role we don't want to delete is admin, but manage-admin and " +
45+
"security are included as well just to be safe, as those two roles together can allow for any other " +
46+
"role to be recreated");
47+
assertTrue(roles.contains("admin"));
48+
assertTrue(roles.contains("manage-admin"));
49+
assertTrue(roles.contains("security"));
50+
}
51+
52+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"role-name": "admin",
3+
"description": "Not clear why someone would try to modify this role, but just in case, we don't want to delete it on undeploy"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"role-name": "ml-app-deployer-test-role",
3+
"description": "This is here to make sure we can delete it"
4+
}

0 commit comments

Comments
 (0)