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

Commit 987dd49

Browse files
author
Miguel Rodriguez
committed
Adding query-roleset support
1 parent 69d5ef0 commit 987dd49

File tree

8 files changed

+162
-11
lines changed

8 files changed

+162
-11
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public abstract class SortOrderConstants {
1414
public static Integer DEPLOY_PROTECTED_COLLECTIONS = 80;
1515
public static Integer DEPLOY_MIMETYPES = 90;
1616
public static Integer DEPLOY_PROTECTED_PATHS = 95;
17+
public static Integer DEPLOY_QUERY_ROLE_SETS = 97;
1718

1819
public static Integer DEPLOY_TRIGGERS_DATABASE = 100;
1920
public static Integer DEPLOY_SCHEMAS_DATABASE = 100;
@@ -72,6 +73,7 @@ public abstract class SortOrderConstants {
7273
public static Integer DELETE_CERTIFICATE_AUTHORITIES = 9020;
7374
public static Integer DELETE_EXTERNAL_SECURITY = 9030;
7475
public static Integer DELETE_PROTECTED_COLLECTIONS = 9040;
76+
public static Integer DELETE_QUERY_ROLE_SETS = 9050;
7577

7678
// Roles can reference privileges, so must delete roles first
7779
public static Integer DELETE_ROLES = 9060;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.QueryRoleSetsManager;
8+
9+
import java.io.File;
10+
11+
public class DeployQueryRoleSetsCommand extends AbstractResourceCommand {
12+
13+
public DeployQueryRoleSetsCommand() {
14+
setExecuteSortOrder(SortOrderConstants.DEPLOY_QUERY_ROLE_SETS);
15+
setUndoSortOrder(SortOrderConstants.DELETE_QUERY_ROLE_SETS);
16+
}
17+
@Override
18+
protected File[] getResourceDirs(CommandContext context) {
19+
return new File[] { context.getAppConfig().getConfigDir().getQueryRoleSetsDir() };
20+
}
21+
22+
@Override
23+
protected ResourceManager getResourceManager(CommandContext context) {
24+
return new QueryRoleSetsManager(context.getManageClient());
25+
}
26+
}

src/main/java/com/marklogic/mgmt/PayloadParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public String getPayloadFieldValue(String payload, String fieldName) {
3030
throw new RuntimeException("Cannot get field value from JSON; field name: " + fieldName + "; JSON: "
3131
+ payload);
3232
}
33-
return node.get(fieldName).asText();
33+
return node.get(fieldName).isTextual() ? node.get(fieldName).asText() : node.get(fieldName).toString();
34+
3435
} else {
3536
Fragment f = new Fragment(payload);
3637
String xpath = String.format("/node()/*[local-name(.) = '%s']", fieldName);

src/main/java/com/marklogic/mgmt/resource/security/ProtectedPathManager.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.marklogic.mgmt.ManageClient;
44
import com.marklogic.mgmt.resource.AbstractResourceManager;
55
import com.marklogic.rest.util.Fragment;
6+
import com.marklogic.rest.util.ResourcesFragment;
7+
import org.springframework.web.client.ResourceAccessException;
68

79
import java.util.List;
810

@@ -65,4 +67,25 @@ public String getIdForPathExpression(String pathExpression) {
6567

6668
@Override
6769
protected boolean useAdminUser() { return true; }
70+
71+
72+
/**
73+
* Testing the deployment/undeployment of protected paths intermittently fails when performing a GET on the
74+
* /manage/v2/protected-paths endpoint. A single retry seems to address the issue, though the cause is still
75+
* unknown.
76+
*
77+
* @return ResourcesFragment
78+
*/
79+
@Override
80+
public ResourcesFragment getAsXml() {
81+
try {
82+
return new ResourcesFragment(getManageClient().getXmlAsAdmin(getResourcesPath()));
83+
} catch (ResourceAccessException ex) {
84+
if (logger.isWarnEnabled()) {
85+
logger.warn("Unable to get list of protected paths, retrying; cause: " + ex.getMessage());
86+
}
87+
return new ResourcesFragment(getManageClient().getXmlAsAdmin(getResourcesPath()));
88+
}
89+
}
90+
6891
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.marklogic.mgmt.resource.security;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.marklogic.mgmt.ManageClient;
5+
import com.marklogic.mgmt.PayloadParser;
6+
import com.marklogic.mgmt.resource.AbstractResourceManager;
7+
import com.marklogic.rest.util.Fragment;
8+
9+
import java.util.List;
10+
11+
public class QueryRoleSetsManager extends AbstractResourceManager {
12+
13+
private boolean updateAllowed = false;
14+
15+
public QueryRoleSetsManager(ManageClient client) {
16+
super(client);
17+
}
18+
19+
@Override
20+
public String getResourcesPath() {
21+
return "/manage/v2/query-rolesets";
22+
}
23+
24+
@Override
25+
protected String getResourceName() {
26+
return "query-rolesets";
27+
}
28+
29+
@Override
30+
protected String getIdFieldName() {
31+
return "role-name";
32+
}
33+
34+
@Override
35+
public String getPropertiesPath(String resourceNameOrId, String... resourceUrlParams) {
36+
String id = getIdForRoleNames(resourceNameOrId);
37+
if (id == null) {
38+
throw new RuntimeException("Could not find a query-roleset with roles: " + resourceNameOrId);
39+
} else return getResourcesPath() + "/" + id + "/properties";
40+
}
41+
42+
@Override
43+
public String getResourcePath(String resourceNameOrId, String... resourceUrlParams) {
44+
String id = getIdForRoleNames(resourceNameOrId);
45+
if (id == null) {
46+
throw new RuntimeException("Could not find a query-roleset with roles: " + resourceNameOrId);
47+
}else return getResourcesPath() + "/" + id;
48+
}
49+
50+
@Override
51+
public boolean exists(String resourceNameOrId, String... resourceUrlParams) {
52+
Fragment f = getAsXml();
53+
return f.elementExists(format(
54+
"/node()/*[local-name(.) = 'list-items']/node()[*[local-name(.) = 'idref'] = '%s']",
55+
getIdForRoleNames(resourceNameOrId)));
56+
}
57+
58+
public String getIdForRoleNames(String roles) {
59+
Fragment f = getAsXml();
60+
String xpath = "/node()/*[local-name(.) = 'list-items']/node()/*[local-name(.) = 'idref']";
61+
String roleSetId = null;
62+
63+
//Transform roles into role JSON array
64+
JsonNode roleArray = payloadParser.parseJson(roles);
65+
66+
//Get list of existing rolesets
67+
for(String id : f.getElementValues(xpath)) {
68+
String response =
69+
payloadParser.getPayloadFieldValue(
70+
getManageClient().getJson(getResourcesPath() + "/" + id + "/properties"),
71+
getIdFieldName()
72+
);
73+
74+
//does this roleset contain the same list of roles?
75+
if (roleArray.equals(payloadParser.parseJson(response))) {
76+
roleSetId = id;
77+
break;
78+
}
79+
}
80+
return roleSetId;
81+
}
82+
83+
}

src/test/java/com/marklogic/appdeployer/command/security/ManageProtectedPathsTest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,4 @@ protected String[] getResourceNames() {
2121
return new String[] { "/test:element" };
2222
}
2323

24-
/*
25-
@Override
26-
protected void afterResourcesCreatedAgain() {
27-
try {
28-
Thread.sleep(5000);
29-
} catch (InterruptedException e) {
30-
e.printStackTrace();
31-
}
32-
}
33-
*/
3424
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.QueryRoleSetsManager;
7+
8+
public class ManageQueryRoleSetsTest extends AbstractManageResourceTest {
9+
@Override
10+
protected ResourceManager newResourceManager() {
11+
return new QueryRoleSetsManager(manageClient);
12+
}
13+
14+
@Override
15+
protected Command newCommand() {
16+
return new DeployQueryRoleSetsCommand();
17+
}
18+
19+
@Override
20+
protected String[] getResourceNames() {
21+
return new String[] { "[\"view-admin\"]" };
22+
}
23+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"role-name": ["view-admin" ]
3+
}

0 commit comments

Comments
 (0)