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

Commit 69d5ef0

Browse files
author
Miguel Rodriguez
committed
Adding protected-path support
1 parent b15b751 commit 69d5ef0

File tree

6 files changed

+147
-1
lines changed

6 files changed

+147
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Defines all of the directories where configuration files can be found.
1111
*
12-
* TODO Eventually turn this into an interface.
12+
* TODO Eventually turn this into an interface.
1313
*/
1414
public class ConfigDir {
1515

@@ -92,6 +92,10 @@ public File getUsersDir() {
9292
return new File(getSecurityDir(), "users");
9393
}
9494

95+
public File getProtectedPathsDir() { return new File(getSecurityDir(), "protected-paths"); }
96+
97+
public File getQueryRoleSetsDir() { return new File(getSecurityDir(), "query-rolesets"); }
98+
9599
public File getServersDir() {
96100
return new File(baseDir, "servers");
97101
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public abstract class SortOrderConstants {
1313
public static Integer DEPLOY_EXTERNAL_SECURITY = 70;
1414
public static Integer DEPLOY_PROTECTED_COLLECTIONS = 80;
1515
public static Integer DEPLOY_MIMETYPES = 90;
16+
public static Integer DEPLOY_PROTECTED_PATHS = 95;
1617

1718
public static Integer DEPLOY_TRIGGERS_DATABASE = 100;
1819
public static Integer DEPLOY_SCHEMAS_DATABASE = 100;
@@ -75,6 +76,8 @@ public abstract class SortOrderConstants {
7576
// Roles can reference privileges, so must delete roles first
7677
public static Integer DELETE_ROLES = 9060;
7778
public static Integer DELETE_PRIVILEGES = 9070;
79+
// Protected paths reference roles
80+
public static Integer DELETE_PROTECTED_PATHS = 9080;
7881

7982
/*
8083
* This executes before databases are deleted, as deleting databases normally deletes the primary forests, so we
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.SortOrderConstants;
6+
import com.marklogic.mgmt.resource.ResourceManager;
7+
import com.marklogic.mgmt.resource.security.ProtectedPathManager;
8+
import com.marklogic.mgmt.resource.security.UserManager;
9+
10+
import java.io.File;
11+
12+
public class DeployProtectedPathCommand extends AbstractResourceCommand{
13+
14+
public DeployProtectedPathCommand() {
15+
setExecuteSortOrder(SortOrderConstants.DEPLOY_PROTECTED_PATHS);
16+
setUndoSortOrder(SortOrderConstants.DELETE_PROTECTED_PATHS);
17+
}
18+
19+
@Override
20+
protected File[] getResourceDirs(CommandContext context) {
21+
return new File[] { context.getAppConfig().getConfigDir().getProtectedPathsDir() };
22+
}
23+
24+
@Override
25+
protected ResourceManager getResourceManager(CommandContext context) {
26+
return new ProtectedPathManager(context.getManageClient());
27+
}
28+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.marklogic.mgmt.resource.security;
2+
3+
import com.marklogic.mgmt.ManageClient;
4+
import com.marklogic.mgmt.resource.AbstractResourceManager;
5+
import com.marklogic.rest.util.Fragment;
6+
7+
import java.util.List;
8+
9+
public class ProtectedPathManager extends AbstractResourceManager {
10+
public ProtectedPathManager(ManageClient client) {
11+
super(client);
12+
}
13+
14+
@Override
15+
public String getResourcesPath() {
16+
return "/manage/v2/protected-paths";
17+
}
18+
19+
@Override
20+
protected String getResourceName() {
21+
return "protected-path";
22+
}
23+
24+
@Override
25+
protected String getIdFieldName() {
26+
return "path-expression";
27+
}
28+
29+
@Override
30+
public String getPropertiesPath(String resourceNameOrId, String... resourceUrlParams) {
31+
return getResourcesPath() + "/" + getIdForPathExpression(resourceNameOrId) + "/properties";
32+
}
33+
34+
@Override
35+
public String getResourcePath(String resourceNameOrId, String... resourceUrlParams) {
36+
return getResourcesPath() + "/" + getIdForPathExpression(resourceNameOrId);
37+
}
38+
39+
@Override
40+
protected String[] getDeleteResourceParams(String payload) {
41+
// We need to unprotect the path before deleting it
42+
// Otherwise we'll get a SEC-MUSTUNPROTECTPATH error
43+
return new String[]{"force", "true"};
44+
}
45+
46+
@Override
47+
public boolean exists(String resourceNameOrId, String... resourceUrlParams) {
48+
Fragment f = getAsXml();
49+
return f.elementExists(format(
50+
"/node()/*[local-name(.) = 'list-items']/node()[*[local-name(.) = 'nameref'] = '%s']",
51+
resourceNameOrId));
52+
}
53+
54+
public String getIdForPathExpression(String pathExpression) {
55+
Fragment f = getAsXml();
56+
String xpath = "/node()/*[local-name(.) = 'list-items']/node()"
57+
+ "[*[local-name(.) = 'nameref'] = '%s']/*[local-name(.) = 'idref']";
58+
xpath = String.format(xpath, pathExpression);
59+
String id = f.getElementValue(xpath);
60+
if (id == null) {
61+
throw new RuntimeException("Could not find a protected path with a path-expression of: " + pathExpression);
62+
}
63+
return id;
64+
}
65+
66+
@Override
67+
protected boolean useAdminUser() { return true; }
68+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.marklogic.appdeployer.command.security;
2+
3+
import com.marklogic.appdeployer.command.AbstractManageResourceTest;
4+
import com.marklogic.appdeployer.command.Command;
5+
import com.marklogic.mgmt.resource.ResourceManager;
6+
import com.marklogic.mgmt.resource.security.ProtectedPathManager;
7+
8+
public class ManageProtectedPathsTest extends AbstractManageResourceTest {
9+
@Override
10+
protected ResourceManager newResourceManager() {
11+
return new ProtectedPathManager(manageClient);
12+
}
13+
14+
@Override
15+
protected Command newCommand() {
16+
return new DeployProtectedPathCommand();
17+
}
18+
19+
@Override
20+
protected String[] getResourceNames() {
21+
return new String[] { "/test:element" };
22+
}
23+
24+
/*
25+
@Override
26+
protected void afterResourcesCreatedAgain() {
27+
try {
28+
Thread.sleep(5000);
29+
} catch (InterruptedException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
*/
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"path-expression":"/test:element",
3+
"path-namespace":[
4+
{"prefix":"test", "namespace-uri":"http://marklogic.com"}
5+
],
6+
"permission": [
7+
{"role-name":"view-admin", "capability":"read"}
8+
]
9+
}

0 commit comments

Comments
 (0)