Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtered S3 task sandbox file search #1971

Merged
merged 5 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ private static String getDayOrMonth(int value) {
return String.format("%02d", value);
}

public static Collection<String> getS3KeyPrefixes(String s3KeyFormat, String requestId, String deployId, Optional<String> tag, long start, long end, String group) {
public static Collection<String> getS3KeyPrefixes(String s3KeyFormat, String requestId, String deployId, Optional<String> tag, long start, long end, String group, List<String> prefixWhitelist) {
String keyFormat = getS3KeyFormat(s3KeyFormat, requestId, deployId, tag, group);

keyFormat = trimTaskId(keyFormat, requestId + "-" + deployId);

return getS3KeyPrefixes(keyFormat, DISALLOWED_FOR_DEPLOY, start, end);
return getS3KeyPrefixes(keyFormat, DISALLOWED_FOR_DEPLOY, start, end, prefixWhitelist);
}

private static String trimTaskId(String s3KeyFormat, String replaceWith) {
Expand All @@ -143,10 +143,10 @@ public static Collection<String> getS3KeyPrefixes(String s3KeyFormat, String req

s3KeyFormat = trimTaskId(s3KeyFormat, requestId);

return getS3KeyPrefixes(s3KeyFormat, DISALLOWED_FOR_REQUEST, start, end);
return getS3KeyPrefixes(s3KeyFormat, DISALLOWED_FOR_REQUEST, start, end, Collections.emptyList());
}

private static Collection<String> getS3KeyPrefixes(String s3KeyFormat, List<String> disallowedKeys, long start, long end) {
private static Collection<String> getS3KeyPrefixes(String s3KeyFormat, List<String> disallowedKeys, long start, long end, List<String> prefixWhitelist) {
String trimKeyFormat = trimKeyFormat(s3KeyFormat, disallowedKeys);

int indexOfY = trimKeyFormat.indexOf("%Y");
Expand Down Expand Up @@ -192,7 +192,9 @@ private static Collection<String> getS3KeyPrefixes(String s3KeyFormat, List<Stri
keyBuilder.replace(indexOfD, indexOfD + 2, getDayOrMonth(calendar.get(Calendar.DAY_OF_MONTH)));
}

keyPrefixes.add(keyBuilder.toString());
if (prefixWhitelist.isEmpty() || prefixWhitelist.stream().anyMatch(allowedPrefix -> keyBuilder.toString().startsWith(allowedPrefix))) {
keyPrefixes.add(keyBuilder.toString());
}

if (indexOfD > -1) {
calendar.add(Calendar.DAY_OF_YEAR, 1);
Expand All @@ -208,10 +210,10 @@ private static Collection<String> getS3KeyPrefixes(String s3KeyFormat, List<Stri
return keyPrefixes;
}

public static Collection<String> getS3KeyPrefixes(String s3KeyFormat, SingularityTaskId taskId, Optional<String> tag, long start, long end, String group) {
public static Collection<String> getS3KeyPrefixes(String s3KeyFormat, SingularityTaskId taskId, Optional<String> tag, long start, long end, String group, List<String> prefixWhitelist) {
String keyFormat = getS3KeyFormat(s3KeyFormat, taskId, tag, group);

return getS3KeyPrefixes(keyFormat, DISALLOWED_FOR_TASK, start, end);
return getS3KeyPrefixes(keyFormat, DISALLOWED_FOR_TASK, start, end, prefixWhitelist);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@Schema(description = "Describes a request to search for task logs in s3")
public class SingularityS3SearchRequest {
private final Map<String, List<String>> requestsAndDeploys;
private final List<String> fileNamePrefixWhitelist;
private final List<String> taskIds;
private final Optional<Long> start;
private final Optional<Long> end;
Expand All @@ -21,9 +22,19 @@ public class SingularityS3SearchRequest {
private final Optional<Integer> maxPerPage;
private final Map<String, ContinuationToken> continuationTokens;

@JsonCreator
public SingularityS3SearchRequest(Map<String, List<String>> requestsAndDeploys,
List<String> taskIds,
Optional<Long> start,
Optional<Long> end,
boolean excludeMetadata,
boolean listOnly,
Optional<Integer> maxPerPage, Map<String, ContinuationToken> continuationTokens) {
this(requestsAndDeploys, null, taskIds, start, end, excludeMetadata, listOnly, maxPerPage, continuationTokens);
}

@JsonCreator
public SingularityS3SearchRequest(@JsonProperty("requestsAndDeploys") Map<String, List<String>> requestsAndDeploys,
@JsonProperty("fileNamePrefixWhitelist") List<String> fileNamePrefixWhitelist,
@JsonProperty("taskIds") List<String> taskIds,
@JsonProperty("start") Optional<Long> start,
@JsonProperty("end") Optional<Long> end,
Expand All @@ -32,6 +43,7 @@ public SingularityS3SearchRequest(@JsonProperty("requestsAndDeploys") Map<String
@JsonProperty("maxPerPage") Optional<Integer> maxPerPage,
@JsonProperty("continuationTokens") Map<String, ContinuationToken> continuationTokens) {
this.requestsAndDeploys = requestsAndDeploys != null ? requestsAndDeploys : Collections.<String, List<String>>emptyMap();
this.fileNamePrefixWhitelist = fileNamePrefixWhitelist != null ? fileNamePrefixWhitelist : Collections.emptyList();
this.taskIds = taskIds != null ? taskIds : Collections.<String>emptyList();
this.start = start;
this.end = end;
Expand All @@ -46,6 +58,11 @@ public Map<String, List<String>> getRequestsAndDeploys() {
return requestsAndDeploys;
}

@Schema(description = "A whitelist of file name prefixes which should be returned")
public List<String> getFileNamePrefixWhitelist() {
return fileNamePrefixWhitelist;
}

@Schema(description = "A list of task IDs to search for")
public List<String> getTaskIds() {
return taskIds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public S3LogResource(AsyncHttpClient httpClient, LeaderLatch leaderLatch, Object
}

// Generation of prefixes
private Collection<String> getS3PrefixesForTask(S3Configuration s3Configuration, SingularityTaskId taskId, Optional<Long> startArg, Optional<Long> endArg, String group, SingularityUser user) {
private Collection<String> getS3PrefixesForTask(S3Configuration s3Configuration, SingularityTaskId taskId, Optional<Long> startArg, Optional<Long> endArg, String group, SingularityUser user, List<String> prefixWhitelist) {
Optional<SingularityTaskHistory> history = getTaskHistory(taskId, user);

long start = taskId.getStartedAt();
Expand Down Expand Up @@ -165,10 +165,10 @@ private Collection<String> getS3PrefixesForTask(S3Configuration s3Configuration,
tag = history.get().getTask().getTaskRequest().getDeploy().getExecutorData().get().getLoggingTag();
}

Collection<String> prefixes = SingularityS3FormatHelper.getS3KeyPrefixes(s3Configuration.getS3KeyFormat(), taskId, tag, start, end, group);
Collection<String> prefixes = SingularityS3FormatHelper.getS3KeyPrefixes(s3Configuration.getS3KeyFormat(), taskId, tag, start, end, group, prefixWhitelist);
for (SingularityS3UploaderFile additionalFile : s3Configuration.getS3UploaderAdditionalFiles()) {
if (additionalFile.getS3UploaderKeyPattern().isPresent() && !additionalFile.getS3UploaderKeyPattern().get().equals(s3Configuration.getS3KeyFormat())) {
prefixes.addAll(SingularityS3FormatHelper.getS3KeyPrefixes(additionalFile.getS3UploaderKeyPattern().get(), taskId, tag, start, end, group));
prefixes.addAll(SingularityS3FormatHelper.getS3KeyPrefixes(additionalFile.getS3UploaderKeyPattern().get(), taskId, tag, start, end, group, prefixWhitelist));
}
}

Expand Down Expand Up @@ -238,10 +238,10 @@ private Collection<String> getS3PrefixesForDeploy(S3Configuration s3Configuratio
tag = deployHistory.getDeploy().get().getExecutorData().get().getLoggingTag();
}

Collection<String> prefixes = SingularityS3FormatHelper.getS3KeyPrefixes(s3Configuration.getS3KeyFormat(), requestId, deployId, tag, start, end, group);
Collection<String> prefixes = SingularityS3FormatHelper.getS3KeyPrefixes(s3Configuration.getS3KeyFormat(), requestId, deployId, tag, start, end, group, Collections.emptyList());
for (SingularityS3UploaderFile additionalFile : s3Configuration.getS3UploaderAdditionalFiles()) {
if (additionalFile.getS3UploaderKeyPattern().isPresent() && !additionalFile.getS3UploaderKeyPattern().get().equals(s3Configuration.getS3KeyFormat())) {
prefixes.addAll(SingularityS3FormatHelper.getS3KeyPrefixes(additionalFile.getS3UploaderKeyPattern().get(), requestId, deployId, tag, start, end, group));
prefixes.addAll(SingularityS3FormatHelper.getS3KeyPrefixes(additionalFile.getS3UploaderKeyPattern().get(), requestId, deployId, tag, start, end, group, Collections.emptyList()));
}
}

Expand All @@ -258,7 +258,7 @@ private Map<SingularityS3Service, Set<String>> getServiceToPrefixes(SingularityS
SingularityTaskId taskIdObject = getTaskIdObject(taskId);
String group = getRequestGroupForTask(taskIdObject, user).orElse(SingularityS3FormatHelper.DEFAULT_GROUP_NAME);
Set<String> s3Buckets = getBuckets(group);
Collection<String> prefixes = getS3PrefixesForTask(configuration.get(), taskIdObject, search.getStart(), search.getEnd(), group, user);
Collection<String> prefixes = getS3PrefixesForTask(configuration.get(), taskIdObject, search.getStart(), search.getEnd(), group, user, search.getFileNamePrefixWhitelist());
for (String s3Bucket : s3Buckets) {
SingularityS3Service s3Service = s3Services.getServiceByGroupAndBucketOrDefault(group, s3Bucket);
if (!servicesToPrefixes.containsKey(s3Service)) {
Expand Down Expand Up @@ -370,7 +370,9 @@ public List<S3ObjectSummaryHolder> call() throws Exception {
continuationTokens.putIfAbsent(key, new ContinuationToken(result.getNextContinuationToken(), !result.isTruncated()));
List<S3ObjectSummaryHolder> objectSummaryHolders = new ArrayList<>();
for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
objectSummaryHolders.add(new S3ObjectSummaryHolder(group, objectSummary));
if (search.getFileNamePrefixWhitelist().isEmpty() || search.getFileNamePrefixWhitelist().stream().anyMatch(whitelistedPrefix -> objectSummary.getKey().startsWith(whitelistedPrefix))) {
objectSummaryHolders.add(new S3ObjectSummaryHolder(group, objectSummary));
}
}
return objectSummaryHolders;
} else {
Expand All @@ -386,7 +388,9 @@ public List<S3ObjectSummaryHolder> call() throws Exception {
ListObjectsV2Result result = s3Client.listObjectsV2(request);
List<S3ObjectSummaryHolder> objectSummaryHolders = new ArrayList<>();
for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
objectSummaryHolders.add(new S3ObjectSummaryHolder(group, objectSummary));
if (search.getFileNamePrefixWhitelist().isEmpty() || search.getFileNamePrefixWhitelist().stream().anyMatch(whitelistedPrefix -> objectSummary.getKey().startsWith(whitelistedPrefix))) {
objectSummaryHolders.add(new S3ObjectSummaryHolder(group, objectSummary));
}
}
while (result.isTruncated() && result.getContinuationToken() != null) {
result = s3Client.listObjectsV2(new ListObjectsV2Request().withBucketName(s3Bucket).withPrefix(s3Prefix).withContinuationToken(result.getContinuationToken()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hubspot.singularity;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.TimeZone;

Expand All @@ -18,24 +19,24 @@ public void testS3FormatHelper() throws Exception {
long start = 1414610537117L; // Wed, 29 Oct 2014 19:22:17 GMT
long end = 1415724215000L; // Tue, 11 Nov 2014 16:43:35 GMT

Collection<String> prefixes = SingularityS3FormatHelper.getS3KeyPrefixes("%Y/%m/%taskId", taskId, Optional.<String>empty(), start, end, "default");
Collection<String> prefixes = SingularityS3FormatHelper.getS3KeyPrefixes("%Y/%m/%taskId", taskId, Optional.<String>empty(), start, end, "default", Collections.emptyList());

Assertions.assertTrue(prefixes.size() == 2);

end = 1447265861000L; // Tue, 11 Nov 2015 16:43:35 GMT

prefixes = SingularityS3FormatHelper.getS3KeyPrefixes("%Y/%taskId", taskId, Optional.<String>empty(), start, end, "default");
prefixes = SingularityS3FormatHelper.getS3KeyPrefixes("%Y/%taskId", taskId, Optional.<String>empty(), start, end, "default", Collections.emptyList());

Assertions.assertTrue(prefixes.size() == 2);

start = 1415750399999L;
end = 1415771999000L;

prefixes = SingularityS3FormatHelper.getS3KeyPrefixes("%Y/%m/%d/%taskId", taskId, Optional.<String>empty(), start, end, "default");
prefixes = SingularityS3FormatHelper.getS3KeyPrefixes("%Y/%m/%d/%taskId", taskId, Optional.<String>empty(), start, end, "default", Collections.emptyList());

Assertions.assertTrue(prefixes.size() == 2);

prefixes = SingularityS3FormatHelper.getS3KeyPrefixes("%requestId/%group/%Y/%m", taskId, Optional.<String>empty(), start, end, "groupName");
prefixes = SingularityS3FormatHelper.getS3KeyPrefixes("%requestId/%group/%Y/%m", taskId, Optional.<String>empty(), start, end, "groupName", Collections.emptyList());

Assertions.assertEquals("rid/groupName/2014/11", prefixes.iterator().next());

Expand Down