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

Update GrpcRemoteDownloader to only include relevant headers. #16439

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import build.bazel.remote.execution.v2.RequestMetadata;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.bazel.repository.downloader.Checksum;
import com.google.devtools.build.lib.bazel.repository.downloader.Downloader;
Expand Down Expand Up @@ -72,7 +73,8 @@ public class GrpcRemoteDownloader implements AutoCloseable, Downloader {
private final RemoteCacheClient cacheClient;
private final RemoteOptions options;
private final boolean verboseFailures;
@Nullable private final Downloader fallbackDownloader;
@Nullable
private final Downloader fallbackDownloader;

private final AtomicBoolean closed = new AtomicBoolean();

Expand Down Expand Up @@ -198,7 +200,7 @@ static FetchBlobRequest newFetchBlobRequest(
requestBuilder.addQualifiers(
Qualifier.newBuilder()
.setName(QUALIFIER_AUTH_HEADERS)
.setValue(authHeadersJson(authHeaders, includeAllHeaders))
.setValue(authHeadersJson(urls, authHeaders, includeAllHeaders))
.build());
}

Expand All @@ -224,10 +226,18 @@ private OutputStream newOutputStream(
return out;
}

private static String authHeadersJson(
private static String authHeadersJson(List<URL> urls,
Map<URI, Map<String, List<String>>> authHeaders, boolean includeAllHeaders) {
ImmutableSet<String> hostSet = urls.stream().map(URL::getHost)
.collect(ImmutableSet.toImmutableSet());
Map<String, JsonObject> subObjects = new TreeMap<>();
for (Map.Entry<URI, Map<String, List<String>>> entry : authHeaders.entrySet()) {
URI uri = entry.getKey();
// Only add headers that are relevant to the hosts.
if (!hostSet.contains(uri.getHost())) {
continue;
}

JsonObject subObject = new JsonObject();
Map<String, List<String>> orderedHeaders = new TreeMap<>(entry.getValue());
for (Map.Entry<String, List<String>> subEntry : orderedHeaders.entrySet()) {
Expand All @@ -244,7 +254,7 @@ private static String authHeadersJson(
}
}
}
subObjects.put(entry.getKey().toString(), subObject);
subObjects.put(uri.toString(), subObject);
}

JsonObject authHeadersJson = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,6 @@ public void testFetchBlobRequest() throws Exception {
+ "\"http://example.com\":{"
+ "\"Another-Header\":\"another header content\","
+ "\"Some-Header\":\"some header content\""
+ "},"
+ "\"http://example.org\":{"
+ "\"Org-Header\":\"org header content\""
+ "}"
+ "}";

Expand Down Expand Up @@ -427,9 +424,6 @@ public void testFetchBlobRequestWithAllHeaders() throws Exception {
+ "\"http://example.com\":{"
+ "\"Another-Header\":[\"another header content\",\"even more header content\"],"
+ "\"Some-Header\":[\"some header content\"]"
+ "},"
+ "\"http://example.org\":{"
+ "\"Org-Header\":[\"org header content\",\"and a second one\",\"and a third one\"]"
+ "}"
+ "}";

Expand Down