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

Commit d4f4a57

Browse files
committed
#284 Security user is used when server has external security
1 parent fa6d673 commit d4f4a57

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ protected boolean useSecurityUser() {
2222
return false;
2323
}
2424

25+
/**
26+
* Some payloads - such as a server payload that uses external security - require a condition to determine if the
27+
* security user should be used or not.
28+
*
29+
* @param payload
30+
* @return
31+
*/
32+
protected boolean useSecurityUser(String payload) {
33+
return useSecurityUser();
34+
}
35+
2536
/**
2637
* Assumes the resource name is based on the class name - e.g. RoleManager would have a resource name of "role".
2738
*
@@ -48,15 +59,15 @@ protected String getResourceId(String payload) {
4859
}
4960

5061
protected ResponseEntity<String> putPayload(ManageClient client, String path, String payload) {
51-
boolean requiresSecurityUser = useSecurityUser();
62+
boolean requiresSecurityUser = useSecurityUser(payload);
5263
if (payloadParser.isJsonPayload(payload)) {
5364
return requiresSecurityUser ? client.putJsonAsSecurityUser(path, payload) : client.putJson(path, payload);
5465
}
5566
return requiresSecurityUser ? client.putXmlAsSecurityUser(path, payload) : client.putXml(path, payload);
5667
}
5768

5869
protected ResponseEntity<String> postPayload(ManageClient client, String path, String payload) {
59-
boolean requiresSecurityUser = useSecurityUser();
70+
boolean requiresSecurityUser = useSecurityUser(payload);
6071
if (payloadParser.isJsonPayload(payload)) {
6172
return requiresSecurityUser ? client.postJsonAsSecurityUser(path, payload) : client.postJson(path, payload);
6273
}

src/main/java/com/marklogic/mgmt/resource/appservers/ServerManager.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.marklogic.mgmt.resource.appservers;
22

3+
import com.marklogic.mgmt.SaveReceipt;
34
import com.marklogic.mgmt.resource.AbstractResourceManager;
45
import com.marklogic.mgmt.ManageClient;
56
import com.marklogic.rest.util.Fragment;
@@ -20,6 +21,24 @@ public ServerManager(ManageClient manageClient, String groupName) {
2021
this.groupName = groupName != null ? groupName : DEFAULT_GROUP;
2122
}
2223

24+
/**
25+
* This is hacky, but it should be close to 100% reliable. Worst case is that the payload has the string
26+
* "external-security" in some other field and we unnecessarily use the security user.
27+
*
28+
* Public so that it can be unit-tested easily.
29+
*
30+
* @param payload
31+
* @return
32+
*/
33+
@Override
34+
public boolean useSecurityUser(String payload) {
35+
boolean b = payload != null && payload.contains("external-security");
36+
if (b && logger.isInfoEnabled()) {
37+
logger.info("Server payload contains external-security, so using the security user");
38+
}
39+
return b;
40+
}
41+
2342
/**
2443
* When doing an existence check, have to take the group name into account.
2544
*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.marklogic.appdeployer.command.servers;
2+
3+
import com.marklogic.mgmt.resource.appservers.ServerManager;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
public class DeployServerWithExternalSecurityTest extends Assert {
8+
9+
@Test
10+
public void test() {
11+
ServerManager mgr = new ServerManager(null);
12+
13+
assertTrue(mgr.useSecurityUser("{\"server-name\": \"my-server\", \"external-security\": [\"my-external-security\"]}"));
14+
assertFalse(mgr.useSecurityUser("{\"server-name\": \"my-server\"}"));
15+
16+
assertTrue(
17+
"This is an expected false positive, but it's considered fine because it just means that the security user " +
18+
"will be used in the rare event that some other field in the payload has the string 'external-security' in it",
19+
mgr.useSecurityUser("{\"server-name\": \"my-external-security-test\"}")
20+
);
21+
}
22+
}

0 commit comments

Comments
 (0)