Skip to content

Commit

Permalink
Include the target in the original and canonical command line events,…
Browse files Browse the repository at this point in the history
… taking care to redact the remaining residue.

PiperOrigin-RevId: 648862218
Change-Id: Ib0c32ebbad64f4711aa68698c0ea28f44b706494
  • Loading branch information
Googler authored and copybara-github committed Jul 2, 2024
1 parent 392deb4 commit 0b280ac
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,14 @@ CommandLineSection getResidual() {
CommandLineSection.Builder builder =
CommandLineSection.newBuilder().setSectionLabel("residual");
if (commandName.equals("run")
&& !commandOptions.getOptions(BuildEventProtocolOptions.class)
.includeResidueInRunBepEvent) {
builder.setChunkList(ChunkList.newBuilder().addChunk("REDACTED"));
&& !commandOptions.getOptions(BuildEventProtocolOptions.class).includeResidueInRunBepEvent
&& !commandOptions.getResidue().isEmpty()) {
String target = commandOptions.getResidue().get(0);
ChunkList.Builder residual = ChunkList.newBuilder().addChunk(target);
if (commandOptions.getResidue().size() > 1) {
residual.addChunk("REDACTED");
}
builder.setChunkList(residual);
} else {
builder.setChunkList(ChunkList.newBuilder().addAllChunk(commandOptions.getResidue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.io.BaseEncoding;
import com.google.devtools.build.lib.bazel.BazelStartupOptionsModule.Options;
import com.google.devtools.build.lib.buildeventstream.BuildEventProtocolOptions;
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId.StructuredCommandLineId;
import com.google.devtools.build.lib.runtime.CommandLineEvent.CanonicalCommandLineEvent;
import com.google.devtools.build.lib.runtime.CommandLineEvent.OriginalCommandLineEvent;
Expand Down Expand Up @@ -511,4 +512,64 @@ public void testSimpleStringToolCommandLine() throws OptionsParsingException {
assertThat(line.getSections(0).getChunkList().getChunk(0))
.isEqualTo("The quick brown fox jumps over the lazy dog");
}

@Test
public void redactedResidual_includesTarget_originalCommandLine() throws OptionsParsingException {
OptionsParser fakeStartupOptions =
OptionsParser.builder().optionsClasses(BlazeServerStartupOptions.class).build();
OptionsParser fakeCommandOptions =
OptionsParser.builder().optionsClasses(BuildEventProtocolOptions.class).build();
fakeCommandOptions.parse("--experimental_run_bep_event_include_residue=false");
fakeCommandOptions.setResidue(
ImmutableList.of("//some:target", "--sensitive_arg"), ImmutableList.of());

CommandLine line =
new OriginalCommandLineEvent(
"testblaze",
fakeStartupOptions,
"run",
fakeCommandOptions,
Optional.of(ImmutableList.of()))
.asStreamProto(null)
.getStructuredCommandLine();

assertThat(line.getCommandLineLabel()).isEqualTo("original");
checkCommandLineSectionLabels(line);
assertThat(line.getSections(0).getChunkList().getChunk(0)).isEqualTo("testblaze");
assertThat(line.getSections(1).getOptionList().getOptionCount()).isEqualTo(0);
assertThat(line.getSections(2).getChunkList().getChunk(0)).isEqualTo("run");
assertThat(line.getSections(3).getOptionList().getOptionCount()).isEqualTo(1);
assertThat(line.getSections(4).getChunkList().getChunkCount()).isEqualTo(2);
assertThat(line.getSections(4).getChunkList().getChunk(0)).isEqualTo("//some:target");
assertThat(line.getSections(4).getChunkList().getChunk(1)).isEqualTo("REDACTED");
}

@Test
public void redactedResidual_includesTarget_canonicalCommandLine()
throws OptionsParsingException {
OptionsParser fakeStartupOptions =
OptionsParser.builder().optionsClasses(BlazeServerStartupOptions.class).build();
OptionsParser fakeCommandOptions =
OptionsParser.builder().optionsClasses(BuildEventProtocolOptions.class).build();
fakeCommandOptions.parse("--experimental_run_bep_event_include_residue=false");
fakeCommandOptions.setResidue(
ImmutableList.of("//some:target", "--sensitive_arg"), ImmutableList.of());

CommandLine line =
new CanonicalCommandLineEvent("testblaze", fakeStartupOptions, "run", fakeCommandOptions)
.asStreamProto(null)
.getStructuredCommandLine();

assertThat(line.getCommandLineLabel()).isEqualTo("canonical");
checkCommandLineSectionLabels(line);
assertThat(line.getSections(0).getChunkList().getChunk(0)).isEqualTo("testblaze");
assertThat(line.getSections(1).getOptionList().getOptionCount()).isEqualTo(1);
assertThat(line.getSections(1).getOptionList().getOption(0).getCombinedForm())
.isEqualTo("--ignore_all_rc_files");
assertThat(line.getSections(2).getChunkList().getChunk(0)).isEqualTo("run");
assertThat(line.getSections(3).getOptionList().getOptionCount()).isEqualTo(1);
assertThat(line.getSections(4).getChunkList().getChunkCount()).isEqualTo(2);
assertThat(line.getSections(4).getChunkList().getChunk(0)).isEqualTo("//some:target");
assertThat(line.getSections(4).getChunkList().getChunk(1)).isEqualTo("REDACTED");
}
}
1 change: 1 addition & 0 deletions src/test/shell/bazel/bazel_build_event_stream_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ EOF
ls >& "$TEST_log"
cat bep.json >> "$TEST_log"

expect_log "//a:arg"
expect_log "execRequest"
expect_log "argv"
expect_log "REDACTED"
Expand Down

0 comments on commit 0b280ac

Please sign in to comment.