Skip to content

Commit d92bfbb

Browse files
author
ssambasu
committed
DHFPROD-3874:Setting document permissions correctly during ingestion
1 parent 6d28020 commit d92bfbb

File tree

3 files changed

+13
-30
lines changed

3 files changed

+13
-30
lines changed

marklogic-data-hub/src/main/java/com/marklogic/hub/step/impl/WriteStepRunner.java

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.marklogic.client.DatabaseClient;
2424
import com.marklogic.client.datamovement.*;
2525
import com.marklogic.client.document.ServerTransform;
26+
import com.marklogic.client.ext.util.DefaultDocumentPermissionsParser;
27+
import com.marklogic.client.ext.util.DocumentPermissionsParser;
2628
import com.marklogic.client.io.DocumentMetadataHandle;
2729
import com.marklogic.client.io.Format;
2830
import com.marklogic.client.io.InputStreamHandle;
@@ -94,6 +96,7 @@ public class WriteStepRunner implements StepRunner {
9496
private AtomicBoolean isStopped = new AtomicBoolean(false);
9597
private IngestionStepDefinitionImpl stepDef;
9698
private Map<String, Object> stepConfig = new HashMap<>();
99+
private DocumentPermissionsParser documentPermissionsParser = new DefaultDocumentPermissionsParser();
97100

98101
public WriteStepRunner(HubConfig hubConfig) {
99102
this.hubConfig = hubConfig;
@@ -513,7 +516,7 @@ private RunStepResponse runIngester(RunStepResponse runStepResponse, Collection<
513516
//Apply permissions
514517
if(StringUtils.isNotEmpty(outputPermissions)) {
515518
try{
516-
applyPermissions(outputPermissions, metadataHandle);
519+
documentPermissionsParser.parsePermissions(outputPermissions, metadataHandle.getPermissions());
517520
}
518521
catch (Exception e){
519522
throw e;
@@ -661,7 +664,7 @@ private void processCsv(JacksonHandle jacksonHandle, File file) {
661664
private void addToBatcher(File file, Format fileFormat) throws IOException {
662665
// Coverity is saying that the docStream is a resource leak, but the comment below this indicates that it must
663666
// not be closed. Because this is for DHFPROD-3695 and we're close to releasing 5.1.0, leaving this as-is for now.
664-
667+
665668
// This docStream must not be closed, or use try-resource due to WriteBatcher needing the stream open
666669
FileInputStream docStream = new FileInputStream(file);
667670
//note these ORs are for forward compatibility if we swap out the filecollector for another lib
@@ -707,34 +710,6 @@ private String generateAndEncodeURI(String path) throws URISyntaxException {
707710
return uri.toString();
708711
}
709712

710-
private void applyPermissions(String permissions, DocumentMetadataHandle metadataHandle) {
711-
String[] perms = permissions.split(",");
712-
if (perms != null && perms.length > 0) {
713-
if (perms.length % 2 != 0) {
714-
throw new IllegalArgumentException(
715-
"Permissions are expected to be in <role, capability> pairs.");
716-
}
717-
int i = 0;
718-
while (i + 1 < perms.length) {
719-
String roleName = perms[i++];
720-
if (roleName == null || roleName.isEmpty()) {
721-
throw new IllegalArgumentException(
722-
"Illegal role name: " + roleName);
723-
}
724-
String perm = perms[i].trim();
725-
if (!DocumentMetadataHandle.Capability.READ.toString().equalsIgnoreCase(perm) &&
726-
!DocumentMetadataHandle.Capability.EXECUTE.toString().equalsIgnoreCase(perm) &&
727-
!DocumentMetadataHandle.Capability.INSERT.toString().equalsIgnoreCase(perm) &&
728-
!DocumentMetadataHandle.Capability.UPDATE.toString().equalsIgnoreCase(perm) &&
729-
!DocumentMetadataHandle.Capability.NODE_UPDATE.toString().equalsIgnoreCase(perm)) {
730-
throw new IllegalArgumentException("Illegal capability: " + perm);
731-
}
732-
metadataHandle.withPermission(roleName, DocumentMetadataHandle.Capability.getValueOf(perm));
733-
i++;
734-
}
735-
}
736-
}
737-
738713
private String outputURIReplace(String uri) {
739714
if (StringUtils.isNotEmpty(outputURIReplacement)) {
740715
String[] replace = outputURIReplacement.split(",");

marklogic-data-hub/src/test/java/com/marklogic/hub/flow/FlowRunnerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import com.marklogic.bootstrap.Installer;
2020
import com.marklogic.client.DatabaseClient;
21+
import com.marklogic.client.document.XMLDocumentManager;
2122
import com.marklogic.client.eval.EvalResult;
2223
import com.marklogic.client.eval.EvalResultIterator;
24+
import com.marklogic.client.io.DocumentMetadataHandle;
2325
import com.marklogic.client.io.StringHandle;
2426
import com.marklogic.hub.ApplicationConfig;
2527
import com.marklogic.hub.HubConfig;
@@ -99,6 +101,11 @@ public void testRunFlow(){
99101
Assertions.assertTrue(getDocCount(HubConfig.DEFAULT_FINAL_NAME, "json-map") == 1);
100102
Assertions.assertTrue(getDocCount(HubConfig.DEFAULT_FINAL_NAME, "xml-map") == 1);
101103
Assertions.assertTrue(JobStatus.FINISHED.toString().equalsIgnoreCase(resp.getJobStatus()));
104+
XMLDocumentManager docMgr = stagingClient.newXMLDocumentManager();
105+
DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
106+
docMgr.readMetadata("/ingest-xml.xml", metadataHandle);
107+
DocumentMetadataHandle.DocumentPermissions perms = metadataHandle.getPermissions();
108+
Assertions.assertEquals(2, perms.get("flow-developer-role").size());
102109
Assertions.assertNotNull(resp.getUser());
103110
Assertions.assertNotNull(resp.getStartTime());
104111
Assertions.assertNotNull(resp.getEndTime());

marklogic-data-hub/src/test/resources/flow-runner-test/flows/testFlow.flow.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"threadCount" : null,
1919
"options": {
2020
"outputFormat": "xml",
21+
"permissions": "flow-developer-role,read,flow-developer-role,update",
2122
"collections": ["xml-coll"]
2223
}
2324
},

0 commit comments

Comments
 (0)