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

Commit c0a66f6

Browse files
committed
#173 Exported users now have a default password
1 parent 335682c commit c0a66f6

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

src/main/java/com/marklogic/appdeployer/export/AbstractNamedResourceExporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public abstract class AbstractNamedResourceExporter extends AbstractResourceExporter {
1919

2020
private String[] resourceNames;
21-
private ObjectMapper objectMapper;
21+
protected ObjectMapper objectMapper;
2222

2323
protected AbstractNamedResourceExporter(ManageClient manageClient, String... resourceNames) {
2424
super(manageClient);

src/main/java/com/marklogic/appdeployer/export/security/UserExporter.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
package com.marklogic.appdeployer.export.security;
22

3+
import com.fasterxml.jackson.databind.node.ObjectNode;
34
import com.marklogic.appdeployer.ConfigDir;
45
import com.marklogic.appdeployer.export.AbstractNamedResourceExporter;
56
import com.marklogic.mgmt.ManageClient;
7+
import com.marklogic.mgmt.PayloadParser;
68
import com.marklogic.mgmt.ResourceManager;
79
import com.marklogic.mgmt.security.UserManager;
10+
import com.marklogic.rest.util.Fragment;
11+
import org.jdom2.Document;
12+
import org.jdom2.Element;
13+
import org.jdom2.output.Format;
14+
import org.jdom2.output.XMLOutputter;
815

916
import java.io.File;
17+
import java.io.IOException;
1018

1119
public class UserExporter extends AbstractNamedResourceExporter {
1220

21+
private PayloadParser payloadParser = new PayloadParser();
22+
private String defaultPassword = "CHANGEME";
23+
1324
public UserExporter(ManageClient manageClient, String... usernames) {
1425
super(manageClient, usernames);
1526
}
@@ -26,6 +37,42 @@ protected File getResourceDirectory(File baseDir) {
2637

2738
@Override
2839
protected String[] getExportMessages() {
29-
return new String[]{"The exported user files do not have a password in them, as passwords cannot be retrieved. Each user file needs a password added before it can be deployed."};
40+
return new String[]{"The exported user files each have a default password in them, as the real password cannot be exported for security reasons."};
41+
}
42+
43+
/**
44+
* As of version 2.9.0 of ml-app-deployer, an attempt is made to add a default password to each exported user. This
45+
* allows the user to be immediately deployed, and the developer can then change the password to a real one at a
46+
* later date.
47+
*
48+
* @param resourceName
49+
* @param payload
50+
* @return
51+
*/
52+
@Override
53+
protected String beforeResourceWrittenToFile(String resourceName, String payload) {
54+
try {
55+
if (payloadParser.isJsonPayload(payload)) {
56+
ObjectNode json = (ObjectNode) payloadParser.parseJson(payload);
57+
if (!json.has("password")) {
58+
json.put("password", defaultPassword);
59+
return objectMapper.writeValueAsString(json);
60+
}
61+
} else {
62+
Fragment xml = new Fragment(payload);
63+
if (!xml.elementExists("/node()/m:password")) {
64+
Document doc = xml.getInternalDoc();
65+
doc.getRootElement().addContent(new Element("password", "http://marklogic.com/manage").setText(defaultPassword));
66+
return new XMLOutputter(Format.getPrettyFormat()).outputString(doc);
67+
}
68+
}
69+
} catch (Exception ex) {
70+
logger.warn("Unable to add a default password to exported user: " + resourceName + "; still exporting user but without a password; exception message: " + ex.getMessage());
71+
}
72+
return payload;
73+
}
74+
75+
public void setDefaultPassword(String defaultPassword) {
76+
this.defaultPassword = defaultPassword;
3077
}
3178
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.marklogic.appdeployer.export;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.marklogic.rest.util.Fragment;
5+
import org.junit.Test;
6+
import org.springframework.util.FileCopyUtils;
7+
8+
public class ExportUsersTest extends AbstractExportTest {
9+
10+
@Test
11+
public void exportAsJson() throws Exception {
12+
ExportedResources resources = new Exporter(manageClient).users("admin", "healthcheck").export(exportDir);
13+
14+
assertEquals("The exported user files each have a default password in them, as the real password cannot be exported for security reasons.",
15+
resources.getMessages().get(0));
16+
17+
JsonNode json = objectMapper.readTree(resources.getFiles().get(0));
18+
assertEquals("admin", json.get("user-name").asText());
19+
assertEquals("CHANGEME", json.get("password").asText());
20+
21+
json = objectMapper.readTree(resources.getFiles().get(1));
22+
assertEquals("healthcheck", json.get("user-name").asText());
23+
assertEquals("CHANGEME", json.get("password").asText());
24+
}
25+
26+
@Test
27+
public void exportAsXml() throws Exception {
28+
ExportedResources resources = new Exporter(manageClient).format("xml").users("admin", "healthcheck").export(exportDir);
29+
30+
assertEquals("The exported user files each have a default password in them, as the real password cannot be exported for security reasons.",
31+
resources.getMessages().get(0));
32+
33+
Fragment xml = new Fragment(new String(FileCopyUtils.copyToByteArray(resources.getFiles().get(0))));
34+
assertEquals("admin", xml.getElementValue("/node()/m:user-name"));
35+
assertEquals("CHANGEME", xml.getElementValue("/node()/m:password"));
36+
37+
xml = new Fragment(new String(FileCopyUtils.copyToByteArray(resources.getFiles().get(1))));
38+
assertEquals("healthcheck", xml.getElementValue("/node()/m:user-name"));
39+
assertEquals("CHANGEME", xml.getElementValue("/node()/m:password"));
40+
}
41+
}

0 commit comments

Comments
 (0)