Skip to content

Commit 91f1ce8

Browse files
committed
up
1 parent 1e908c5 commit 91f1ce8

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

server/impl/src/main/java/com/walmartlabs/concord/server/process/ProcessResource.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,15 +747,17 @@ public void appendLog(@PathParam("id") UUID instanceId, InputStream data) {
747747
content = @Content(mediaType = "application/zip",
748748
schema = @Schema(type = "string", format = "binary"))
749749
)
750-
public Response downloadState(@PathParam("id") UUID instanceId) {
750+
public Response downloadState(@PathParam("id") UUID instanceId, @Context HttpServletRequest request) {
751751
ProcessEntry entry = assertProcess(PartialProcessKey.from(instanceId));
752752
ProcessKey processKey = new ProcessKey(entry.instanceId(), entry.createdAt());
753753

754754
assertProcessAccess(entry, "state");
755755

756+
boolean applyFilter = assertApplyFilter(request);
757+
756758
StreamingOutput out = output -> {
757759
try (ZipArchiveOutputStream dst = new ZipArchiveOutputStream(output)) {
758-
stateManager.export(processKey, new ProcessStateManager.FilteringConsumer(zipTo(dst), s -> {
760+
stateManager.export(processKey, new ProcessStateManager.FilteringConsumer(zipTo(dst, applyFilter), s -> {
759761
if (!isSessionResource(s)) {
760762
return true;
761763
}
@@ -769,6 +771,21 @@ public Response downloadState(@PathParam("id") UUID instanceId) {
769771
.build();
770772
}
771773

774+
private boolean assertApplyFilter(HttpServletRequest request) {
775+
// do not filter request from admins
776+
if (Roles.isAdmin()) {
777+
return false;
778+
}
779+
780+
// do not filter request from agent
781+
String userAgent = request.getHeader("User-Agent");
782+
if (userAgent.startsWith("Concord-Agent")) {
783+
return false;
784+
}
785+
786+
return true;
787+
}
788+
772789
/**
773790
* Downloads a single file from the current state snapshot of a process.
774791
*/

server/impl/src/main/java/com/walmartlabs/concord/server/process/StateManagerUtils.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
5-
import com.walmartlabs.concord.server.security.Roles;
65

76
import java.io.ByteArrayInputStream;
87
import java.io.IOException;
@@ -19,15 +18,6 @@ public final class StateManagerUtils {
1918
private static final List<String> ALLOWED_EXTENSIONS = List.of("json", "yaml", "yml");
2019

2120
public static InputStream stateFilter(String file, InputStream in) {
22-
// do not apply filter for admins
23-
if (Roles.isAdmin()) {
24-
return in;
25-
}
26-
27-
return filter(file, in);
28-
}
29-
30-
public static InputStream filter(String file, InputStream in) {
3121
try {
3222
String extension = getFileExtension(file);
3323
if (!ALLOWED_EXTENSIONS.contains(extension) || STATE_FILTER.get(file) == null) {

server/impl/src/main/java/com/walmartlabs/concord/server/process/state/ProcessStateManager.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ public static ItemConsumer copyTo(Path dst, String[] ignored, OpenOption... opti
511511
*
512512
* @param dst archive stream.
513513
*/
514-
public static ItemConsumer zipTo(ZipArchiveOutputStream dst) {
515-
return new ZipConsumer(dst);
514+
public static ItemConsumer zipTo(ZipArchiveOutputStream dst, boolean filterContents) {
515+
return new ZipConsumer(dst, filterContents);
516516
}
517517

518518
public static ItemConsumer exclude(ItemConsumer delegate, String... patterns) {
@@ -793,20 +793,22 @@ public void accept(String name, int unixMode, InputStream src) {
793793
public static final class ZipConsumer implements ItemConsumer {
794794

795795
private final ZipArchiveOutputStream dst;
796+
private final boolean filterContents;
796797

797-
private ZipConsumer(ZipArchiveOutputStream dst) {
798+
private ZipConsumer(ZipArchiveOutputStream dst, boolean filterContents) {
798799
this.dst = dst;
800+
this.filterContents = filterContents;
799801
}
800802

801803
@Override
802804
public void accept(String name, int unixMode, InputStream src) {
803805
ZipArchiveEntry entry = new ZipArchiveEntry(name);
804806
entry.setUnixMode(unixMode);
805807
// filter before zip to download
806-
InputStream filtered = StateManagerUtils.stateFilter(name, src);
808+
InputStream processed = filterContents ? StateManagerUtils.stateFilter(name, src) : src;
807809
try {
808810
dst.putArchiveEntry(entry);
809-
IOUtils.copy(filtered, dst);
811+
IOUtils.copy(processed, dst);
810812
dst.closeArchiveEntry();
811813
} catch (IOException e) {
812814
throw new RuntimeException(e);

server/impl/src/test/java/com/walmartlabs/concord/server/process/StateManagerUtilsTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void testFilterWithJsonFile() throws Exception {
1818
String jsonInput = "{\"arguments\": \"value\", \"otherKey\": \"otherValue\"}";
1919
InputStream inputStream = new ByteArrayInputStream(jsonInput.getBytes(StandardCharsets.UTF_8));
2020

21-
InputStream result = StateManagerUtils.filter("_main.json", inputStream);
21+
InputStream result = StateManagerUtils.stateFilter("_main.json", inputStream);
2222
String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
2323

2424
ObjectMapper mapper = new ObjectMapper();
@@ -33,12 +33,12 @@ void testFilterWithYamlFile() throws Exception {
3333
String yamlInput = "arguments: value\notherKey: otherValue";
3434
InputStream inputStream = new ByteArrayInputStream(yamlInput.getBytes(StandardCharsets.UTF_8));
3535

36-
InputStream result = StateManagerUtils.filter("test.yaml", inputStream);
36+
InputStream result = StateManagerUtils.stateFilter("test.yaml", inputStream);
3737

3838
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
3939
Map<String, Object> resultMap = mapper.readValue(result, Map.class);
4040

41-
// return the same if to filter items not present
41+
// return the same if to stateFilter items not present
4242
assertEquals(2, resultMap.size());
4343
assertEquals("otherValue", resultMap.get("otherKey"));
4444
}
@@ -48,7 +48,7 @@ void testFilterWithUnsupportedExtension() throws Exception {
4848
String input = "key: value";
4949
InputStream inputStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
5050

51-
InputStream result = StateManagerUtils.filter("file.txt", inputStream);
51+
InputStream result = StateManagerUtils.stateFilter("file.txt", inputStream);
5252
String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
5353

5454
assertEquals(input, text);
@@ -60,7 +60,7 @@ void testFilterWithEmptyInput() throws Exception {
6060
String input = "";
6161
InputStream inputStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
6262

63-
InputStream result = StateManagerUtils.filter("_main.json", inputStream);
63+
InputStream result = StateManagerUtils.stateFilter("_main.json", inputStream);
6464
String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
6565

6666
assertEquals(input, text);
@@ -72,7 +72,7 @@ void testFilterWithNullRules() throws Exception {
7272
String jsonInput = "{\"key\": \"value\"}";
7373
InputStream inputStream = new ByteArrayInputStream(jsonInput.getBytes(StandardCharsets.UTF_8));
7474

75-
InputStream result = StateManagerUtils.filter("unknown.json", inputStream);
75+
InputStream result = StateManagerUtils.stateFilter("unknown.json", inputStream);
7676
String text = new String(result.readAllBytes(), StandardCharsets.UTF_8);
7777

7878
assertEquals(jsonInput, text);
@@ -83,7 +83,7 @@ void testFilterWithNullRules() throws Exception {
8383
void testFilterWithNullInput() throws Exception {
8484
String jsonInput = "{\"key\": \"value\"}";
8585
InputStream inputStream = null;
86-
InputStream result = StateManagerUtils.filter("unknown.json", inputStream);
86+
InputStream result = StateManagerUtils.stateFilter("unknown.json", inputStream);
8787

8888
assertNull(result);
8989
}

0 commit comments

Comments
 (0)