Skip to content

Commit 8dbbde0

Browse files
EdSchoutencopybara-github
authored andcommitted
Allow overriding the hostname and instance name in bytestream:// URIs
In cases where full network transparency doesn't exist, people may run Bazel with custom values of --remote_executor and --remote_instance_name to proxy gRPC traffic. Such proxies may do caching, tunneling and authentication. What's problematic about this is that the values of these command line flags aren't just used to establish a gRPC connection to a remote execution service. They also get logged by Bazel in build event streams in the form of bytestream:// URIs. This means that a build event stream generated on system A may contain bytestream:// URIs that are not valid on system B. Let's introduce a command line flag, --remote_bytestream_uri_prefix, that can be used to force generation of bytestream:// URIs that are canonical. Closes #13085. PiperOrigin-RevId: 362508252
1 parent 3ebf658 commit 8dbbde0

File tree

6 files changed

+64
-26
lines changed

6 files changed

+64
-26
lines changed

src/main/java/com/google/devtools/build/lib/remote/ByteStreamBuildEventArtifactUploader.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import build.bazel.remote.execution.v2.Digest;
1717
import build.bazel.remote.execution.v2.RequestMetadata;
1818
import com.google.common.base.Preconditions;
19-
import com.google.common.base.Strings;
2019
import com.google.common.collect.ImmutableList;
2120
import com.google.common.collect.ImmutableSet;
2221
import com.google.common.collect.Iterables;
@@ -45,7 +44,6 @@
4544
import java.util.Set;
4645
import java.util.concurrent.Executors;
4746
import java.util.concurrent.atomic.AtomicBoolean;
48-
import javax.annotation.Nullable;
4947

5048
/** A {@link BuildEventArtifactUploader} backed by {@link ByteStreamUploader}. */
5149
class ByteStreamBuildEventArtifactUploader extends AbstractReferenceCounted
@@ -63,16 +61,11 @@ class ByteStreamBuildEventArtifactUploader extends AbstractReferenceCounted
6361
ByteStreamBuildEventArtifactUploader(
6462
ByteStreamUploader uploader,
6563
MissingDigestsFinder missingDigestsFinder,
66-
String remoteServerName,
64+
String remoteServerInstanceName,
6765
String buildRequestId,
6866
String commandId,
69-
@Nullable String remoteInstanceName,
7067
int maxUploadThreads) {
7168
this.uploader = Preconditions.checkNotNull(uploader);
72-
String remoteServerInstanceName = Preconditions.checkNotNull(remoteServerName);
73-
if (!Strings.isNullOrEmpty(remoteInstanceName)) {
74-
remoteServerInstanceName += "/" + remoteInstanceName;
75-
}
7669
this.buildRequestId = buildRequestId;
7770
this.commandId = commandId;
7871
this.remoteServerInstanceName = remoteServerInstanceName;

src/main/java/com/google/devtools/build/lib/remote/ByteStreamBuildEventArtifactUploaderFactory.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.devtools.build.lib.remote.options.RemoteOptions;
1919
import com.google.devtools.build.lib.runtime.BuildEventArtifactUploaderFactory;
2020
import com.google.devtools.build.lib.runtime.CommandEnvironment;
21-
import javax.annotation.Nullable;
2221

2322
/**
2423
* A factory for {@link ByteStreamBuildEventArtifactUploader}.
@@ -27,36 +26,32 @@ class ByteStreamBuildEventArtifactUploaderFactory implements
2726
BuildEventArtifactUploaderFactory {
2827

2928
private final ByteStreamUploader uploader;
30-
private final String remoteServerName;
29+
private final String remoteServerInstanceName;
3130
private final String buildRequestId;
3231
private final String commandId;
3332
private final MissingDigestsFinder missingDigestsFinder;
34-
@Nullable private final String remoteInstanceName;
3533

3634
ByteStreamBuildEventArtifactUploaderFactory(
3735
ByteStreamUploader uploader,
3836
MissingDigestsFinder missingDigestsFinder,
39-
String remoteServerName,
37+
String remoteServerInstanceName,
4038
String buildRequestId,
41-
String commandId,
42-
@Nullable String remoteInstanceName) {
39+
String commandId) {
4340
this.uploader = uploader;
4441
this.missingDigestsFinder = missingDigestsFinder;
45-
this.remoteServerName = remoteServerName;
42+
this.remoteServerInstanceName = remoteServerInstanceName;
4643
this.buildRequestId = buildRequestId;
4744
this.commandId = commandId;
48-
this.remoteInstanceName = remoteInstanceName;
4945
}
5046

5147
@Override
5248
public BuildEventArtifactUploader create(CommandEnvironment env) {
5349
return new ByteStreamBuildEventArtifactUploader(
5450
uploader.retain(),
5551
missingDigestsFinder,
56-
remoteServerName,
52+
remoteServerInstanceName,
5753
buildRequestId,
5854
commandId,
59-
remoteInstanceName,
6055
env.getOptions().getOptions(RemoteOptions.class).buildEventUploadMaxThreads);
6156
}
6257
}

src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,14 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
469469
}
470470
}
471471

472+
String remoteBytestreamUriPrefix = remoteOptions.remoteBytestreamUriPrefix;
473+
if (Strings.isNullOrEmpty(remoteBytestreamUriPrefix)) {
474+
remoteBytestreamUriPrefix = cacheChannel.authority();
475+
if (!Strings.isNullOrEmpty(remoteOptions.remoteInstanceName)) {
476+
remoteBytestreamUriPrefix += "/" + remoteOptions.remoteInstanceName;
477+
}
478+
}
479+
472480
ByteStreamUploader uploader =
473481
new ByteStreamUploader(
474482
remoteOptions.remoteInstanceName,
@@ -489,12 +497,7 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
489497
uploader.release();
490498
buildEventArtifactUploaderFactoryDelegate.init(
491499
new ByteStreamBuildEventArtifactUploaderFactory(
492-
uploader,
493-
cacheClient,
494-
cacheChannel.authority(),
495-
buildRequestId,
496-
invocationId,
497-
remoteOptions.remoteInstanceName));
500+
uploader, cacheClient, remoteBytestreamUriPrefix, buildRequestId, invocationId));
498501

499502
if (enableRemoteExecution) {
500503
RemoteExecutionClient remoteExecutor;

src/main/java/com/google/devtools/build/lib/remote/options/RemoteOptions.java

+13
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ public final class RemoteOptions extends OptionsBase {
181181
+ " the unit is omitted, the value is interpreted as seconds.")
182182
public Duration remoteTimeout;
183183

184+
@Option(
185+
name = "remote_bytestream_uri_prefix",
186+
defaultValue = "null",
187+
documentationCategory = OptionDocumentationCategory.REMOTE,
188+
effectTags = {OptionEffectTag.UNKNOWN},
189+
help =
190+
"The hostname and instance name to be used in bytestream:// URIs that are written into "
191+
+ "build event streams. This option can be set when builds are performed using a "
192+
+ "proxy, which causes the values of --remote_executor and --remote_instance_name "
193+
+ "to no longer correspond to the canonical name of the remote execution service. "
194+
+ "When not set, it will default to \"${hostname}/${instance_name}\".")
195+
public String remoteBytestreamUriPrefix;
196+
184197
/** Returns the specified duration. Assumes seconds if unitless. */
185198
public static class RemoteTimeoutConverter implements Converter<Duration> {
186199
private static final Pattern UNITLESS_REGEX = Pattern.compile("^[0-9]+$");

src/test/java/com/google/devtools/build/lib/remote/ByteStreamBuildEventArtifactUploaderTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,9 @@ private ByteStreamBuildEventArtifactUploader newArtifactUploader(
374374
return new ByteStreamBuildEventArtifactUploader(
375375
uploader,
376376
missingDigestsFinder,
377-
"localhost",
377+
"localhost/instance",
378378
"none",
379379
"none",
380-
"instance",
381380
/* maxUploadThreads= */ 100);
382381
}
383382

src/test/shell/bazel/remote/remote_execution_test.sh

+35
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,41 @@ EOF
14401440
expect_log "uri:.*bytestream://localhost"
14411441
}
14421442

1443+
function test_bytestream_uri_prefix() {
1444+
# Test that when --remote_bytestream_uri_prefix is set, bytestream://
1445+
# URIs do not contain the hostname that's part of --remote_executor.
1446+
# They should use a fixed value instead.
1447+
mkdir -p a
1448+
cat > a/success.sh <<'EOF'
1449+
#!/bin/sh
1450+
exit 0
1451+
EOF
1452+
chmod 755 a/success.sh
1453+
cat > a/BUILD <<'EOF'
1454+
sh_test(
1455+
name = "success_test",
1456+
srcs = ["success.sh"],
1457+
)
1458+
1459+
genrule(
1460+
name = "foo",
1461+
srcs = [],
1462+
outs = ["foo.txt"],
1463+
cmd = "echo \"foo\" > \"$@\"",
1464+
)
1465+
EOF
1466+
1467+
bazel test \
1468+
--remote_executor=grpc://localhost:${worker_port} \
1469+
--remote_download_minimal \
1470+
--remote_bytestream_uri_prefix=example.com/my-instance-name \
1471+
--build_event_text_file=$TEST_log \
1472+
//a:foo //a:success_test || fail "Failed to test //a:foo //a:success_test"
1473+
1474+
expect_not_log 'uri:.*file://'
1475+
expect_log "uri:.*bytestream://example.com/my-instance-name/blobs"
1476+
}
1477+
14431478
# This test is derivative of test_bep_output_groups in
14441479
# build_event_stream_test.sh, which verifies that successful output groups'
14451480
# artifacts appear in BEP when a top-level target fails to build.

0 commit comments

Comments
 (0)