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

[7.1.0] Passthrough HTTP headers to remote downloader service #21503

Merged
merged 1 commit into from
Mar 4, 2024
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 @@ -77,6 +77,11 @@ public class GrpcRemoteDownloader implements AutoCloseable, Downloader {
private static final String QUALIFIER_CHECKSUM_SRI = "checksum.sri";
private static final String QUALIFIER_CANONICAL_ID = "bazel.canonical_id";

// The `:` character is not permitted in an HTTP header name. So, we use it to
// delimit the qualifier prefix which denotes an HTTP header qualifer from the
// header name itself.
private static final String QUALIFIER_HTTP_HEADER_PREFIX = "http_header:";

public GrpcRemoteDownloader(
String buildRequestId,
String commandId,
Expand Down Expand Up @@ -125,7 +130,7 @@ public void download(
RemoteActionExecutionContext.create(metadata);

final FetchBlobRequest request =
newFetchBlobRequest(options.remoteInstanceName, urls, checksum, canonicalId);
newFetchBlobRequest(options.remoteInstanceName, urls, checksum, canonicalId, headers);
try {
FetchBlobResponse response =
retrier.execute(
Expand Down Expand Up @@ -169,24 +174,40 @@ public void download(

@VisibleForTesting
static FetchBlobRequest newFetchBlobRequest(
String instanceName, List<URL> urls, Optional<Checksum> checksum, String canonicalId) {
String instanceName,
List<URL> urls,
Optional<Checksum> checksum,
String canonicalId,
Map<String, List<String>> headers) {
FetchBlobRequest.Builder requestBuilder =
FetchBlobRequest.newBuilder().setInstanceName(instanceName);
for (URL url : urls) {
requestBuilder.addUris(url.toString());
}

if (checksum.isPresent()) {
requestBuilder.addQualifiers(
Qualifier.newBuilder()
.setName(QUALIFIER_CHECKSUM_SRI)
.setValue(checksum.get().toSubresourceIntegrity())
.build());
}

if (!Strings.isNullOrEmpty(canonicalId)) {
requestBuilder.addQualifiers(
Qualifier.newBuilder().setName(QUALIFIER_CANONICAL_ID).setValue(canonicalId).build());
}

for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
// https://www.rfc-editor.org/rfc/rfc9110.html#name-field-order permits
// merging the field-values with a comma.
requestBuilder.addQualifiers(
Qualifier.newBuilder()
.setName(QUALIFIER_HTTP_HEADER_PREFIX + entry.getKey())
.setValue(String.join(",", entry.getValue()))
.build());
}

return requestBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,10 @@ public void testFetchBlobRequest() throws Exception {
Optional.<Checksum>of(
Checksum.fromSubresourceIntegrity(
"sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")),
"canonical ID");
"canonical ID",
ImmutableMap.of(
"Authorization", ImmutableList.of("Basic Zm9vOmJhcg=="),
"X-Custom-Token", ImmutableList.of("foo", "bar")));

assertThat(request)
.isEqualTo(
Expand All @@ -366,6 +369,14 @@ public void testFetchBlobRequest() throws Exception {
.setValue("sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="))
.addQualifiers(
Qualifier.newBuilder().setName("bazel.canonical_id").setValue("canonical ID"))
.addQualifiers(
Qualifier.newBuilder()
.setName("http_header:Authorization")
.setValue("Basic Zm9vOmJhcg=="))
.addQualifiers(
Qualifier.newBuilder()
.setName("http_header:X-Custom-Token")
.setValue("foo,bar"))
.build());
}
}
Loading