From 5e7351bd834ece461d66a96d3c597340161c262e Mon Sep 17 00:00:00 2001 From: Yannic Bonenberger Date: Fri, 22 Jul 2022 03:03:39 -0700 Subject: [PATCH] Move newCredentialHelperProvider into GoogleAuthUtils Follow-up on https://github.com/bazelbuild/bazel/pull/15930 due to https://github.com/bazelbuild/bazel/pull/15906 Progress on https://github.com/bazelbuild/bazel/issues/15856 Closes #15941. PiperOrigin-RevId: 462580799 Change-Id: Ibac79ed68a0c87a4f34eb9d0729abb1552b44519 --- .../java/com/google/devtools/build/lib/BUILD | 26 ++ .../devtools/build/lib/authandtls/BUILD | 2 + .../build/lib/authandtls/GoogleAuthUtils.java | 66 +++ .../build/lib/remote/RemoteModule.java | 57 +-- .../java/com/google/devtools/build/lib/BUILD | 1 + .../devtools/build/lib/authandtls/BUILD | 8 + .../lib/authandtls/GoogleAuthUtilsTest.java | 377 ++++++++++++++++++ .../build/lib/remote/RemoteModuleTest.java | 10 - 8 files changed, 481 insertions(+), 66 deletions(-) create mode 100644 src/test/java/com/google/devtools/build/lib/authandtls/GoogleAuthUtilsTest.java diff --git a/src/main/java/com/google/devtools/build/lib/BUILD b/src/main/java/com/google/devtools/build/lib/BUILD index 4f744c2f91bf26..d5952a3cd3c182 100644 --- a/src/main/java/com/google/devtools/build/lib/BUILD +++ b/src/main/java/com/google/devtools/build/lib/BUILD @@ -205,6 +205,30 @@ java_library( ], ) +java_library( + name = "runtime/memory_pressure_event", + srcs = [ + "runtime/MemoryPressureEvent.java", + ], + deps = [ + "//third_party:auto_value", + "//third_party:guava", + ], +) + +java_library( + name = "runtime/command_line_path_factory", + srcs = [ + "runtime/CommandLinePathFactory.java", + ], + deps = [ + "//src/main/java/com/google/devtools/build/lib/analysis:blaze_directories", + "//src/main/java/com/google/devtools/build/lib/vfs", + "//src/main/java/com/google/devtools/build/lib/vfs:pathfragment", + "//third_party:guava", + ], +) + java_library( name = "runtime", srcs = glob( @@ -217,6 +241,7 @@ java_library( "buildtool/BuildRequestOptions.java", "runtime/BlazeCommandResult.java", "runtime/CommandDispatcher.java", + "runtime/CommandLinePathFactory.java", "runtime/KeepGoingOption.java", "runtime/LoadingPhaseThreadsOption.java", ], @@ -227,6 +252,7 @@ java_library( ":loading-phase-threads-option", ":runtime/blaze_command_result", ":runtime/command_dispatcher", + ":runtime/command_line_path_factory", "//src/main/java/com/google/devtools/build/lib/actions", "//src/main/java/com/google/devtools/build/lib/actions:action_lookup_data", "//src/main/java/com/google/devtools/build/lib/actions:artifacts", diff --git a/src/main/java/com/google/devtools/build/lib/authandtls/BUILD b/src/main/java/com/google/devtools/build/lib/authandtls/BUILD index 29250880640ff0..4a5d87ff6b2957 100644 --- a/src/main/java/com/google/devtools/build/lib/authandtls/BUILD +++ b/src/main/java/com/google/devtools/build/lib/authandtls/BUILD @@ -14,6 +14,8 @@ java_library( name = "authandtls", srcs = glob(["*.java"]), deps = [ + "//src/main/java/com/google/devtools/build/lib:runtime/command_line_path_factory", + "//src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper", "//src/main/java/com/google/devtools/build/lib/concurrent", "//src/main/java/com/google/devtools/common/options", "//third_party:auth", diff --git a/src/main/java/com/google/devtools/build/lib/authandtls/GoogleAuthUtils.java b/src/main/java/com/google/devtools/build/lib/authandtls/GoogleAuthUtils.java index bf640f1bfc2aef..5861d27544a257 100644 --- a/src/main/java/com/google/devtools/build/lib/authandtls/GoogleAuthUtils.java +++ b/src/main/java/com/google/devtools/build/lib/authandtls/GoogleAuthUtils.java @@ -19,6 +19,13 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Strings; +import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialHelperEnvironment; +import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialHelperProvider; +import com.google.devtools.build.lib.events.Event; +import com.google.devtools.build.lib.events.Reporter; +import com.google.devtools.build.lib.runtime.CommandLinePathFactory; +import com.google.devtools.build.lib.vfs.FileSystem; +import com.google.devtools.build.lib.vfs.Path; import io.grpc.CallCredentials; import io.grpc.ClientInterceptor; import io.grpc.ManagedChannel; @@ -258,4 +265,63 @@ public static Credentials newCredentials( throw new IOException(message, e); } } + + /** + * Create a new {@link Credentials} object by parsing the .netrc file with following order to + * search it: + * + *
    + *
  1. If environment variable $NETRC exists, use it as the path to the .netrc file + *
  2. Fallback to $HOME/.netrc + *
+ * + * @return the {@link Credentials} object or {@code null} if there is no .netrc file. + * @throws IOException in case the credentials can't be constructed. + */ + @VisibleForTesting + static Optional newCredentialsFromNetrc( + Map clientEnv, FileSystem fileSystem) throws IOException { + Optional netrcFileString = + Optional.ofNullable(clientEnv.get("NETRC")) + .or(() -> Optional.ofNullable(clientEnv.get("HOME")).map(home -> home + "/.netrc")); + if (netrcFileString.isEmpty()) { + return Optional.empty(); + } + + Path netrcFile = fileSystem.getPath(netrcFileString.get()); + if (!netrcFile.exists()) { + return Optional.empty(); + } + + try { + Netrc netrc = NetrcParser.parseAndClose(netrcFile.getInputStream()); + return Optional.of(new NetrcCredentials(netrc)); + } catch (IOException e) { + throw new IOException( + "Failed to parse " + netrcFile.getPathString() + ": " + e.getMessage(), e); + } + } + + @VisibleForTesting + public static CredentialHelperProvider newCredentialHelperProvider( + CredentialHelperEnvironment environment, + CommandLinePathFactory pathFactory, + List helpers) + throws IOException { + Preconditions.checkNotNull(environment); + Preconditions.checkNotNull(pathFactory); + Preconditions.checkNotNull(helpers); + + CredentialHelperProvider.Builder builder = CredentialHelperProvider.builder(); + for (AuthAndTLSOptions.UnresolvedScopedCredentialHelper helper : helpers) { + Optional scope = helper.getScope(); + Path path = pathFactory.create(environment.getClientEnvironment(), helper.getPath()); + if (scope.isPresent()) { + builder.add(scope.get(), path); + } else { + builder.add(path); + } + } + return builder.build(); + } } diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java index 871a9d42e6a4d7..5f6b5334636781 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java +++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java @@ -47,9 +47,6 @@ import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions; import com.google.devtools.build.lib.authandtls.CallCredentialsProvider; import com.google.devtools.build.lib.authandtls.GoogleAuthUtils; -import com.google.devtools.build.lib.authandtls.Netrc; -import com.google.devtools.build.lib.authandtls.NetrcCredentials; -import com.google.devtools.build.lib.authandtls.NetrcParser; import com.google.devtools.build.lib.bazel.repository.downloader.Downloader; import com.google.devtools.build.lib.buildeventstream.BuildEventArtifactUploader; import com.google.devtools.build.lib.buildeventstream.LocalFilesArtifactUploader; @@ -1047,58 +1044,6 @@ RemoteActionContextProvider getActionContextProvider() { return actionContextProvider; } - /** - * Create a new {@link Credentials} object by parsing the .netrc file with following order to - * search it: - * - *
    - *
  1. If environment variable $NETRC exists, use it as the path to the .netrc file - *
  2. Fallback to $HOME/.netrc - *
- * - * @return the {@link Credentials} object or {@code null} if there is no .netrc file. - * @throws IOException in case the credentials can't be constructed. - */ - @VisibleForTesting - static Credentials newCredentialsFromNetrc(Map clientEnv, FileSystem fileSystem) - throws IOException { - String netrcFileString = - Optional.ofNullable(clientEnv.get("NETRC")) - .orElseGet( - () -> - Optional.ofNullable(clientEnv.get("HOME")) - .map(home -> home + "/.netrc") - .orElse(null)); - if (netrcFileString == null) { - return null; - } - - Path netrcFile = fileSystem.getPath(netrcFileString); - if (netrcFile.exists()) { - try { - Netrc netrc = NetrcParser.parseAndClose(netrcFile.getInputStream()); - return new NetrcCredentials(netrc); - } catch (IOException e) { - throw new IOException( - "Failed to parse " + netrcFile.getPathString() + ": " + e.getMessage(), e); - } - } else { - return null; - } - } - - /** - * Create a new {@link Credentials} with following order: - * - *
    - *
  1. If authentication enabled by flags, use it to create credentials - *
  2. Use .netrc to provide credentials if exists - *
  3. Otherwise, return {@code null} - *
- * - * @throws IOException in case the credentials can't be constructed. - */ - @VisibleForTesting static Credentials newCredentials( Map clientEnv, FileSystem fileSystem, @@ -1132,6 +1077,6 @@ static Credentials newCredentials( } } - return creds; + return credentials; } } diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD index aa68080cb92b18..c0a45c40213eb0 100644 --- a/src/test/java/com/google/devtools/build/lib/BUILD +++ b/src/test/java/com/google/devtools/build/lib/BUILD @@ -118,6 +118,7 @@ java_test( "//src/main/java/com/google/devtools/build/lib:runtime", "//src/main/java/com/google/devtools/build/lib:runtime/blaze_command_result", "//src/main/java/com/google/devtools/build/lib:runtime/command_dispatcher", + "//src/main/java/com/google/devtools/build/lib:runtime/command_line_path_factory", "//src/main/java/com/google/devtools/build/lib:runtime/safe_request_logging", "//src/main/java/com/google/devtools/build/lib/actions", "//src/main/java/com/google/devtools/build/lib/actions:action_lookup_data", diff --git a/src/test/java/com/google/devtools/build/lib/authandtls/BUILD b/src/test/java/com/google/devtools/build/lib/authandtls/BUILD index 724726bd61e8e4..23619242cf4eae 100644 --- a/src/test/java/com/google/devtools/build/lib/authandtls/BUILD +++ b/src/test/java/com/google/devtools/build/lib/authandtls/BUILD @@ -24,7 +24,15 @@ java_library( ], ), deps = [ + "//src/main/java/com/google/devtools/build/lib:runtime/command_line_path_factory", "//src/main/java/com/google/devtools/build/lib/authandtls", + "//src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper", + "//src/main/java/com/google/devtools/build/lib/events", + "//src/main/java/com/google/devtools/build/lib/vfs", + "//src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs", + "//src/main/java/com/google/devtools/common/options", + "//src/test/java/com/google/devtools/build/lib/testutil", + "//third_party:auth", "//third_party:guava", "//third_party:junit4", "//third_party:mockito", diff --git a/src/test/java/com/google/devtools/build/lib/authandtls/GoogleAuthUtilsTest.java b/src/test/java/com/google/devtools/build/lib/authandtls/GoogleAuthUtilsTest.java new file mode 100644 index 00000000000000..0fb1d69e2cebcd --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/authandtls/GoogleAuthUtilsTest.java @@ -0,0 +1,377 @@ +// Copyright 2022 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.authandtls; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth8.assertThat; +import static java.nio.charset.StandardCharsets.UTF_8; + +import com.google.auth.Credentials; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import com.google.common.eventbus.EventBus; +import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialHelperEnvironment; +import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialHelperProvider; +import com.google.devtools.build.lib.events.Reporter; +import com.google.devtools.build.lib.runtime.CommandLinePathFactory; +import com.google.devtools.build.lib.testutil.Scratch; +import com.google.devtools.build.lib.vfs.DigestHashFunction; +import com.google.devtools.build.lib.vfs.FileSystem; +import com.google.devtools.build.lib.vfs.Path; +import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; +import com.google.devtools.common.options.OptionsParsingException; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URI; +import java.time.Duration; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GoogleAuthUtilsTest { + @Test + public void testNetrc_emptyEnv_shouldIgnore() throws Exception { + ImmutableMap clientEnv = ImmutableMap.of(); + FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256); + + assertThat(GoogleAuthUtils.newCredentialsFromNetrc(clientEnv, fileSystem)).isEmpty(); + } + + @Test + public void testNetrc_netrcNotExist_shouldIgnore() throws Exception { + String home = "/home/foo"; + ImmutableMap clientEnv = ImmutableMap.of("HOME", home); + FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256); + + assertThat(GoogleAuthUtils.newCredentialsFromNetrc(clientEnv, fileSystem)).isEmpty(); + } + + @Test + public void testNetrc_netrcExist_shouldUse() throws Exception { + String home = "/home/foo"; + ImmutableMap clientEnv = ImmutableMap.of("HOME", home); + FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256); + Scratch scratch = new Scratch(fileSystem); + scratch.file(home + "/.netrc", "machine foo.example.org login foouser password foopass"); + + Optional credentials = + GoogleAuthUtils.newCredentialsFromNetrc(clientEnv, fileSystem); + + assertThat(credentials).isPresent(); + assertRequestMetadata( + credentials.get().getRequestMetadata(URI.create("https://foo.example.org")), + "foouser", + "foopass"); + } + + @Test + public void testNetrc_netrcFromNetrcEnvExist_shouldUse() throws Exception { + String home = "/home/foo"; + String netrc = "/.netrc"; + ImmutableMap clientEnv = ImmutableMap.of("HOME", home, "NETRC", netrc); + FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256); + Scratch scratch = new Scratch(fileSystem); + scratch.file(home + "/.netrc", "machine foo.example.org login foouser password foopass"); + scratch.file(netrc, "machine foo.example.org login baruser password barpass"); + + Optional credentials = + GoogleAuthUtils.newCredentialsFromNetrc(clientEnv, fileSystem); + + assertThat(credentials).isPresent(); + assertRequestMetadata( + credentials.get().getRequestMetadata(URI.create("https://foo.example.org")), + "baruser", + "barpass"); + } + + @Test + public void testNetrc_netrcFromNetrcEnvNotExist_shouldIgnore() throws Exception { + String home = "/home/foo"; + String netrc = "/.netrc"; + ImmutableMap clientEnv = ImmutableMap.of("HOME", home, "NETRC", netrc); + FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256); + Scratch scratch = new Scratch(fileSystem); + scratch.file(home + "/.netrc", "machine foo.example.org login foouser password foopass"); + + assertThat(GoogleAuthUtils.newCredentialsFromNetrc(clientEnv, fileSystem)).isEmpty(); + } + + @Test + public void testCredentialHelperProvider() throws Exception { + FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256); + + Path workspace = fileSystem.getPath("/workspace"); + Path pathValue = fileSystem.getPath("/usr/local/bin"); + pathValue.createDirectoryAndParents(); + + CredentialHelperEnvironment credentialHelperEnvironment = + CredentialHelperEnvironment.newBuilder() + .setEventReporter(new Reporter(new EventBus())) + .setWorkspacePath(workspace) + .setClientEnvironment(ImmutableMap.of("PATH", pathValue.getPathString())) + .setHelperExecutionTimeout(Duration.ZERO) + .build(); + CommandLinePathFactory commandLinePathFactory = + new CommandLinePathFactory(fileSystem, ImmutableMap.of("workspace", workspace)); + + Path unusedHelper = createExecutable(fileSystem, "/unused/helper"); + + Path defaultHelper = createExecutable(fileSystem, "/default/helper"); + Path exampleComHelper = createExecutable(fileSystem, "/example/com/helper"); + Path fooExampleComHelper = createExecutable(fileSystem, "/foo/example/com/helper"); + Path exampleComWildcardHelper = createExecutable(fileSystem, "/example/com/wildcard/helper"); + + Path exampleOrgHelper = createExecutable(workspace.getRelative("helpers/example-org")); + + // No helpers. + CredentialHelperProvider credentialHelperProvider1 = + newCredentialHelperProvider( + credentialHelperEnvironment, commandLinePathFactory, ImmutableList.of()); + assertThat(credentialHelperProvider1.findCredentialHelper(URI.create("https://example.com"))) + .isEmpty(); + assertThat( + credentialHelperProvider1.findCredentialHelper(URI.create("https://foo.example.com"))) + .isEmpty(); + + // Default helper only. + CredentialHelperProvider credentialHelperProvider2 = + newCredentialHelperProvider( + credentialHelperEnvironment, + commandLinePathFactory, + ImmutableList.of(defaultHelper.getPathString())); + assertThat( + credentialHelperProvider2 + .findCredentialHelper(URI.create("https://example.com")) + .get() + .getPath()) + .isEqualTo(defaultHelper); + assertThat( + credentialHelperProvider2 + .findCredentialHelper(URI.create("https://foo.example.com")) + .get() + .getPath()) + .isEqualTo(defaultHelper); + + // Default and exact match. + CredentialHelperProvider credentialHelperProvider3 = + newCredentialHelperProvider( + credentialHelperEnvironment, + commandLinePathFactory, + ImmutableList.of( + defaultHelper.getPathString(), "example.com=" + exampleComHelper.getPathString())); + assertThat( + credentialHelperProvider3 + .findCredentialHelper(URI.create("https://example.com")) + .get() + .getPath()) + .isEqualTo(exampleComHelper); + assertThat( + credentialHelperProvider3 + .findCredentialHelper(URI.create("https://foo.example.com")) + .get() + .getPath()) + .isEqualTo(defaultHelper); + + // Exact match without default. + CredentialHelperProvider credentialHelperProvider4 = + newCredentialHelperProvider( + credentialHelperEnvironment, + commandLinePathFactory, + ImmutableList.of("example.com=" + exampleComHelper.getPathString())); + assertThat( + credentialHelperProvider4 + .findCredentialHelper(URI.create("https://example.com")) + .get() + .getPath()) + .isEqualTo(exampleComHelper); + assertThat( + credentialHelperProvider4.findCredentialHelper(URI.create("https://foo.example.com"))) + .isEmpty(); + + // Multiple scoped helpers with default. + CredentialHelperProvider credentialHelperProvider5 = + newCredentialHelperProvider( + credentialHelperEnvironment, + commandLinePathFactory, + ImmutableList.of( + defaultHelper.getPathString(), + "example.com=" + exampleComHelper.getPathString(), + "*.foo.example.com=" + fooExampleComHelper.getPathString(), + "*.example.com=" + exampleComWildcardHelper.getPathString(), + "example.org=%workspace%/helpers/example-org")); + assertThat( + credentialHelperProvider5 + .findCredentialHelper(URI.create("https://anotherdomain.com")) + .get() + .getPath()) + .isEqualTo(defaultHelper); + assertThat( + credentialHelperProvider5 + .findCredentialHelper(URI.create("https://example.com")) + .get() + .getPath()) + .isEqualTo(exampleComHelper); + assertThat( + credentialHelperProvider5 + .findCredentialHelper(URI.create("https://foo.example.com")) + .get() + .getPath()) + .isEqualTo(fooExampleComHelper); + assertThat( + credentialHelperProvider5 + .findCredentialHelper(URI.create("https://abc.foo.example.com")) + .get() + .getPath()) + .isEqualTo(fooExampleComHelper); + assertThat( + credentialHelperProvider5 + .findCredentialHelper(URI.create("https://bar.example.com")) + .get() + .getPath()) + .isEqualTo(exampleComWildcardHelper); + assertThat( + credentialHelperProvider5 + .findCredentialHelper(URI.create("https://abc.bar.example.com")) + .get() + .getPath()) + .isEqualTo(exampleComWildcardHelper); + assertThat( + credentialHelperProvider5 + .findCredentialHelper(URI.create("https://example.org")) + .get() + .getPath()) + .isEqualTo(exampleOrgHelper); + + // Helpers override. + CredentialHelperProvider credentialHelperProvider6 = + newCredentialHelperProvider( + credentialHelperEnvironment, + commandLinePathFactory, + ImmutableList.of( + // + unusedHelper.getPathString(), + + // + defaultHelper.getPathString(), + "example.com=" + unusedHelper.getPathString(), + "*.example.com=" + unusedHelper.getPathString(), + "example.org=" + unusedHelper.getPathString(), + "*.example.org=" + exampleOrgHelper.getPathString(), + + // + "*.example.com=" + exampleComWildcardHelper.getPathString(), + "example.org=" + exampleOrgHelper.getPathString(), + "*.foo.example.com=" + unusedHelper.getPathString(), + + // + "example.com=" + exampleComHelper.getPathString(), + "*.foo.example.com=" + fooExampleComHelper.getPathString())); + assertThat( + credentialHelperProvider6 + .findCredentialHelper(URI.create("https://anotherdomain.com")) + .get() + .getPath()) + .isEqualTo(defaultHelper); + assertThat( + credentialHelperProvider6 + .findCredentialHelper(URI.create("https://example.com")) + .get() + .getPath()) + .isEqualTo(exampleComHelper); + assertThat( + credentialHelperProvider6 + .findCredentialHelper(URI.create("https://foo.example.com")) + .get() + .getPath()) + .isEqualTo(fooExampleComHelper); + assertThat( + credentialHelperProvider6 + .findCredentialHelper(URI.create("https://bar.example.com")) + .get() + .getPath()) + .isEqualTo(exampleComWildcardHelper); + assertThat( + credentialHelperProvider6 + .findCredentialHelper(URI.create("https://example.org")) + .get() + .getPath()) + .isEqualTo(exampleOrgHelper); + assertThat( + credentialHelperProvider6 + .findCredentialHelper(URI.create("https://foo.example.org")) + .get() + .getPath()) + .isEqualTo(exampleOrgHelper); + } + + private static Path createExecutable(FileSystem fileSystem, String path) throws IOException { + Preconditions.checkNotNull(fileSystem); + Preconditions.checkNotNull(path); + + return createExecutable(fileSystem.getPath(path)); + } + + private static Path createExecutable(Path path) throws IOException { + Preconditions.checkNotNull(path); + + path.getParentDirectory().createDirectoryAndParents(); + try (OutputStream unused = path.getOutputStream()) { + // Nothing to do. + } + path.setExecutable(true); + + return path; + } + + private static void assertRequestMetadata( + Map> requestMetadata, String username, String password) { + assertThat(requestMetadata.keySet()).containsExactly("Authorization"); + assertThat(Iterables.getOnlyElement(requestMetadata.values())) + .containsExactly(BasicHttpAuthenticationEncoder.encode(username, password, UTF_8)); + } + + private static CredentialHelperProvider newCredentialHelperProvider( + CredentialHelperEnvironment credentialHelperEnvironment, + CommandLinePathFactory commandLinePathFactory, + ImmutableList inputs) + throws Exception { + Preconditions.checkNotNull(credentialHelperEnvironment); + Preconditions.checkNotNull(commandLinePathFactory); + Preconditions.checkNotNull(inputs); + + return GoogleAuthUtils.newCredentialHelperProvider( + credentialHelperEnvironment, + commandLinePathFactory, + ImmutableList.copyOf( + Iterables.transform(inputs, s -> createUnresolvedScopedCredentialHelper(s)))); + } + + private static AuthAndTLSOptions.UnresolvedScopedCredentialHelper + createUnresolvedScopedCredentialHelper(String input) { + Preconditions.checkNotNull(input); + + try { + return AuthAndTLSOptions.UnresolvedScopedCredentialHelperConverter.INSTANCE.convert(input); + } catch (OptionsParsingException e) { + throw new IllegalStateException(e); + } + } +} diff --git a/src/test/java/com/google/devtools/build/lib/remote/RemoteModuleTest.java b/src/test/java/com/google/devtools/build/lib/remote/RemoteModuleTest.java index bfc0171b6b84f6..c186e441b98418 100644 --- a/src/test/java/com/google/devtools/build/lib/remote/RemoteModuleTest.java +++ b/src/test/java/com/google/devtools/build/lib/remote/RemoteModuleTest.java @@ -14,7 +14,6 @@ package com.google.devtools.build.lib.remote; import static com.google.common.truth.Truth.assertThat; -import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -29,13 +28,11 @@ import com.google.auth.Credentials; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; import com.google.common.eventbus.EventBus; import com.google.devtools.build.lib.analysis.BlazeDirectories; import com.google.devtools.build.lib.analysis.ServerDirectories; import com.google.devtools.build.lib.analysis.config.CoreOptions; import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions; -import com.google.devtools.build.lib.authandtls.BasicHttpAuthenticationEncoder; import com.google.devtools.build.lib.events.Reporter; import com.google.devtools.build.lib.exec.BinTools; import com.google.devtools.build.lib.exec.ExecutionOptions; @@ -569,11 +566,4 @@ public void testNetrc_netrcWithoutRemoteCache() throws Exception { assertThat(credentials.getRequestMetadata(URI.create("https://foo.example.org"))).isNotEmpty(); assertThat(credentials.getRequestMetadata(URI.create("https://bar.example.org"))).isEmpty(); } - - private static void assertRequestMetadata( - Map> requestMetadata, String username, String password) { - assertThat(requestMetadata.keySet()).containsExactly("Authorization"); - assertThat(Iterables.getOnlyElement(requestMetadata.values())) - .containsExactly(BasicHttpAuthenticationEncoder.encode(username, password, UTF_8)); - } }