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

Commit 71c2ee4

Browse files
committed
#232 Restored admin username/password to ManageConfig and ManageClient
1 parent c4be553 commit 71c2ee4

File tree

12 files changed

+222
-51
lines changed

12 files changed

+222
-51
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
package com.marklogic.mgmt;
22

33
import com.marklogic.client.ext.helper.LoggingObject;
4+
import com.marklogic.mgmt.ManageClient;
5+
import com.marklogic.mgmt.PayloadParser;
46
import org.springframework.http.ResponseEntity;
57
import org.springframework.util.ClassUtils;
68

79
public class AbstractManager extends LoggingObject {
810

911
protected PayloadParser payloadParser = new PayloadParser();
1012

13+
/**
14+
* Manager classes that need to connect to ML as a user with the admin role should override this to return true.
15+
* The main use case for this is while an application may define a user with the manage-admin role that can be used
16+
* for deploying most resources, that user must first be created. And thus, some user with at least the manage-admin
17+
* and security roles must already exist and must be used to create that user. And while only the manage-admin and
18+
* security roles are needed, in practice it's likely that this is an admin user. Finally, since that user may
19+
* depend on app-specific roles and privileges, then those resources must be created first by the admin user too.
20+
*
21+
* @return
22+
*/
23+
protected boolean useAdminUser() {
24+
return false;
25+
}
26+
1127
/**
1228
* Assumes the resource name is based on the class name - e.g. RoleManager would have a resource name of "role".
1329
*
@@ -34,16 +50,18 @@ protected String getResourceId(String payload) {
3450
}
3551

3652
protected ResponseEntity<String> putPayload(ManageClient client, String path, String payload) {
53+
boolean useAdmin = useAdminUser();
3754
if (payloadParser.isJsonPayload(payload)) {
38-
return client.putJson(path, payload);
55+
return useAdmin ? client.putJsonAsAdmin(path, payload) : client.putJson(path, payload);
3956
}
40-
return client.putXml(path, payload);
57+
return useAdmin ? client.putXmlAsAdmin(path, payload) : client.putXml(path, payload);
4158
}
4259

4360
protected ResponseEntity<String> postPayload(ManageClient client, String path, String payload) {
61+
boolean useAdmin = useAdminUser();
4462
if (payloadParser.isJsonPayload(payload)) {
45-
return client.postJson(path, payload);
63+
return useAdmin ? client.postJsonAsAdmin(path, payload) : client.postJson(path, payload);
4664
}
47-
return client.postXml(path, payload);
65+
return useAdmin ? client.postXmlAsAdmin(path, payload) : client.postXml(path, payload);
4866
}
4967
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ public ManageConfig newManageConfig() {
7272
c.setCleanJsonPayloads(Boolean.parseBoolean(prop));
7373
}
7474

75+
prop = getProperty("mlAdminUsername");
76+
if (prop != null) {
77+
logger.info("Manage admin username: " + prop);
78+
c.setAdminUsername(prop);
79+
} else if (mlUsername != null) {
80+
logger.info("Manage admin username: " + mlUsername);
81+
c.setAdminUsername(mlUsername);
82+
} else {
83+
c.setAdminUsername(c.getUsername());
84+
}
85+
86+
prop = getProperty("mlAdminPassword");
87+
if (prop != null) {
88+
c.setAdminPassword(prop);
89+
} else if (mlPassword != null) {
90+
c.setAdminPassword(mlPassword);
91+
} else {
92+
c.setAdminPassword(c.getPassword());
93+
}
94+
7595
return c;
7696
}
7797

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

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.marklogic.client.ext.helper.LoggingObject;
55
import com.marklogic.mgmt.util.ObjectMapperFactory;
66
import com.marklogic.rest.util.Fragment;
7+
import com.marklogic.rest.util.RestConfig;
78
import com.marklogic.rest.util.RestTemplateUtil;
89
import org.jdom2.Namespace;
910
import org.springframework.http.*;
@@ -23,8 +24,9 @@
2324
*/
2425
public class ManageClient extends LoggingObject {
2526

26-
private ManageConfig manageConfig;
27-
private RestTemplate restTemplate;
27+
private ManageConfig manageConfig;
28+
private RestTemplate restTemplate;
29+
private RestTemplate adminRestTemplate;
2830
private PayloadParser payloadParser;
2931

3032
/**
@@ -44,6 +46,21 @@ public void initialize(ManageConfig config) {
4446
logger.info("Initializing ManageClient with manage config of: " + config);
4547
}
4648
this.restTemplate = RestTemplateUtil.newRestTemplate(config);
49+
50+
if (!config.getUsername().equals(config.getAdminUsername())) {
51+
if (logger.isInfoEnabled()) {
52+
logger.info("Initializing ManageClient with admin config, admin user: " + config.getAdminUsername());
53+
}
54+
55+
RestConfig rc = new RestConfig(config.getHost(), config.getPort(), config.getAdminUsername(), config.getAdminPassword());
56+
rc.setScheme(config.getScheme());
57+
rc.setConfigureSimpleSsl(config.isConfigureSimpleSsl());
58+
rc.setHostnameVerifier(config.getHostnameVerifier());
59+
rc.setSslContext(config.getSslContext());
60+
this.adminRestTemplate = RestTemplateUtil.newRestTemplate(rc);
61+
} else {
62+
this.adminRestTemplate = restTemplate;
63+
}
4764
}
4865

4966
/**
@@ -53,29 +70,61 @@ public void initialize(ManageConfig config) {
5370
* @param restTemplate
5471
*/
5572
public ManageClient(RestTemplate restTemplate) {
56-
this.restTemplate = restTemplate;
73+
this(restTemplate, restTemplate);
74+
}
75+
76+
/**
77+
* Use this when you want to provide your own RestTemplate as opposed to using the one that's constructed via a
78+
* ManageConfig instance.
79+
*
80+
* @param restTemplate
81+
* @param adminRestTemplate
82+
*/
83+
public ManageClient(RestTemplate restTemplate, RestTemplate adminRestTemplate) {
84+
this.restTemplate = restTemplate;
85+
this.adminRestTemplate = adminRestTemplate;
5786
}
5887

5988
public ResponseEntity<String> putJson(String path, String json) {
6089
logRequest(path, "JSON", "PUT");
6190
return restTemplate.exchange(buildUri(path), HttpMethod.PUT, buildJsonEntity(json), String.class);
6291
}
6392

93+
public ResponseEntity<String> putJsonAsAdmin(String path, String json) {
94+
logAdminRequest(path, "JSON", "PUT");
95+
return adminRestTemplate.exchange(buildUri(path), HttpMethod.PUT, buildJsonEntity(json), String.class);
96+
}
97+
6498
public ResponseEntity<String> putXml(String path, String xml) {
6599
logRequest(path, "XML", "PUT");
66100
return restTemplate.exchange(buildUri(path), HttpMethod.PUT, buildXmlEntity(xml), String.class);
67101
}
68102

103+
public ResponseEntity<String> putXmlAsAdmin(String path, String xml) {
104+
logAdminRequest(path, "XML", "PUT");
105+
return adminRestTemplate.exchange(buildUri(path), HttpMethod.PUT, buildXmlEntity(xml), String.class);
106+
}
107+
69108
public ResponseEntity<String> postJson(String path, String json) {
70109
logRequest(path, "JSON", "POST");
71110
return restTemplate.exchange(buildUri(path), HttpMethod.POST, buildJsonEntity(json), String.class);
72111
}
73112

113+
public ResponseEntity<String> postJsonAsAdmin(String path, String json) {
114+
logAdminRequest(path, "JSON", "POST");
115+
return adminRestTemplate.exchange(buildUri(path), HttpMethod.POST, buildJsonEntity(json), String.class);
116+
}
117+
74118
public ResponseEntity<String> postXml(String path, String xml) {
75119
logRequest(path, "XML", "POST");
76120
return restTemplate.exchange(buildUri(path), HttpMethod.POST, buildXmlEntity(xml), String.class);
77121
}
78122

123+
public ResponseEntity<String> postXmlAsAdmin(String path, String xml) {
124+
logAdminRequest(path, "XML", "POST");
125+
return adminRestTemplate.exchange(buildUri(path), HttpMethod.POST, buildXmlEntity(xml), String.class);
126+
}
127+
79128
public ResponseEntity<String> postForm(String path, String... params) {
80129
logRequest(path, "form", "POST");
81130
HttpHeaders headers = new HttpHeaders();
@@ -102,6 +151,20 @@ public Fragment getXml(String path, String... namespacePrefixesAndUris) {
102151
return new Fragment(xml, list.toArray(new Namespace[] {}));
103152
}
104153

154+
public String getXmlStringAsAdmin(String path) {
155+
logAdminRequest(path, "XML", "GET");
156+
return getAdminRestTemplate().getForObject(buildUri(path), String.class);
157+
}
158+
159+
public Fragment getXmlAsAdmin(String path, String... namespacePrefixesAndUris) {
160+
String xml = getXmlStringAsAdmin(path);
161+
List<Namespace> list = new ArrayList<Namespace>();
162+
for (int i = 0; i < namespacePrefixesAndUris.length; i += 2) {
163+
list.add(Namespace.getNamespace(namespacePrefixesAndUris[i], namespacePrefixesAndUris[i + 1]));
164+
}
165+
return new Fragment(xml, list.toArray(new Namespace[] {}));
166+
}
167+
105168
public String getJson(String path) {
106169
logRequest(path, "JSON", "GET");
107170
HttpHeaders headers = new HttpHeaders();
@@ -117,11 +180,24 @@ public String getJson(URI uri) {
117180
return getRestTemplate().exchange(uri, HttpMethod.GET, new HttpEntity<>(headers), String.class).getBody();
118181
}
119182

183+
public String getJsonAsAdmin(String path) {
184+
logAdminRequest(path, "JSON", "GET");
185+
HttpHeaders headers = new HttpHeaders();
186+
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
187+
return getAdminRestTemplate().exchange(buildUri(path), HttpMethod.GET, new HttpEntity<>(headers), String.class)
188+
.getBody();
189+
}
190+
120191
public void delete(String path) {
121192
logRequest(path, "", "DELETE");
122193
restTemplate.delete(buildUri(path));
123194
}
124195

196+
public void deleteAsAdmin(String path) {
197+
logAdminRequest(path, "", "DELETE");
198+
adminRestTemplate.delete(buildUri(path));
199+
}
200+
125201
/**
126202
* Per #187 and version 3.1.0, when an HttpEntity is constructed with a JSON payload, this method will check to see
127203
* if it should "clean" the JSON via the Jackson library, which is primarily intended for removing comments from
@@ -187,11 +263,19 @@ public RestTemplate getRestTemplate() {
187263
return restTemplate;
188264
}
189265

266+
public RestTemplate getAdminRestTemplate() {
267+
return adminRestTemplate;
268+
}
269+
190270
public ManageConfig getManageConfig() {
191271
return manageConfig;
192272
}
193273

194274
public void setRestTemplate(RestTemplate restTemplate) {
195275
this.restTemplate = restTemplate;
196276
}
277+
278+
public void setAdminRestTemplate(RestTemplate adminRestTemplate) {
279+
this.adminRestTemplate = adminRestTemplate;
280+
}
197281
}

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
/**
66
* Defines the configuration data for talking to the Mgmt REST API. Also includes properties for the admin user, as this
7-
* user is typically needed for managing resources such as roles and users. If adminUsername and adminPassword are not
8-
* set, they default to the username/password attribute values.
7+
* user is typically needed for creating an app-specific user (which may depend on app-specific roles and privileges)
8+
* which is then used for deploying every other resources. If adminUsername and adminPassword are not set, they default
9+
* to the username/password attribute values.
910
*/
1011
public class ManageConfig extends RestConfig {
1112

@@ -16,6 +17,8 @@ public class ManageConfig extends RestConfig {
1617
public static final String DEFAULT_USERNAME = "admin";
1718
public static final String DEFAULT_PASSWORD = "admin";
1819

20+
private String adminUsername;
21+
private String adminPassword;
1922
private boolean cleanJsonPayloads = false;
2023

2124
public ManageConfig() {
@@ -28,12 +31,14 @@ public ManageConfig(String host, String password) {
2831

2932
public ManageConfig(String host, int port, String username, String password) {
3033
super(host, port, username, password);
34+
setAdminUsername(username);
35+
setAdminPassword(password);
3136
}
3237

3338
@Override
3439
public String toString() {
35-
return String.format("[ManageConfig host: %s, port: %d, username: %s]", getHost(),
36-
getPort(), getUsername());
40+
return String.format("[ManageConfig host: %s, port: %d, username: %s, admin username: %s]", getHost(),
41+
getPort(), getUsername(), getAdminUsername());
3742
}
3843

3944
public boolean isCleanJsonPayloads() {
@@ -43,4 +48,20 @@ public boolean isCleanJsonPayloads() {
4348
public void setCleanJsonPayloads(boolean cleanJsonPayloads) {
4449
this.cleanJsonPayloads = cleanJsonPayloads;
4550
}
51+
52+
public String getAdminUsername() {
53+
return adminUsername;
54+
}
55+
56+
public void setAdminUsername(String adminUsername) {
57+
this.adminUsername = adminUsername;
58+
}
59+
60+
public String getAdminPassword() {
61+
return adminPassword;
62+
}
63+
64+
public void setAdminPassword(String adminPassword) {
65+
this.adminPassword = adminPassword;
66+
}
4667
}

src/main/java/com/marklogic/mgmt/api/API.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ public void connect(String host, ManageConfig mc) {
8787
logger.info("Connecting to host: " + host);
8888
}
8989
SimplePropertySource sps = new SimplePropertySource("mlHost", host, "mlManageUsername", mc.getUsername(),
90-
"mlManagePassword", mc.getPassword(),
91-
"mlManageSimpleSsl", mc.isConfigureSimpleSsl() + "",
92-
"mlManageScheme", mc.getScheme(),
93-
"mlManagePort", mc.getPort() + "");
90+
"mlManagePassword", mc.getPassword(), "mlManageSimpleSsl", mc.isConfigureSimpleSsl() + "",
91+
"mlManageScheme", mc.getScheme(), "mlManagePort", mc.getPort() + "",
92+
"mlAdminUsername", mc.getAdminUsername(), "mlAdminPassword", mc.getAdminPassword());
9493
this.manageClient = new ManageClient(new DefaultManageConfigFactory(sps).newManageConfig());
9594
if (logger.isInfoEnabled()) {
9695
logger.info("Connected to host: " + host);
@@ -105,19 +104,34 @@ public void connect(String host, ManageConfig mc) {
105104
* @param password
106105
*/
107106
public void connect(String host, String username, String password) {
108-
ManageConfig mc = new ManageConfig();
109-
mc.setHost(host);
110-
mc.setUsername(username);
111-
mc.setPassword(password);
112-
connect(host, mc);
107+
connect(host, username, password, username, password);
113108
}
114109

115-
/**
116-
* Constructs a new ClientHelper, using newClient().
117-
*
118-
* @return
119-
*/
120-
public ClientHelper clientHelper() {
110+
/**
111+
* Connect to a (presumably) different MarkLogic Management API.
112+
*
113+
* @param host
114+
* @param username
115+
* @param password
116+
* @param adminUsername
117+
* @param adminPassword
118+
*/
119+
public void connect(String host, String username, String password, String adminUsername, String adminPassword) {
120+
ManageConfig mc = new ManageConfig();
121+
mc.setHost(host);
122+
mc.setUsername(username);
123+
mc.setPassword(password);
124+
mc.setAdminUsername(adminUsername);
125+
mc.setAdminPassword(adminPassword);
126+
connect(host, mc);
127+
}
128+
129+
/**
130+
* Constructs a new ClientHelper, using newClient().
131+
*
132+
* @return
133+
*/
134+
public ClientHelper clientHelper() {
121135
return new ClientHelper(newClient());
122136
}
123137

0 commit comments

Comments
 (0)