Skip to content

Commit

Permalink
Move newCredentialHelperProvider into GoogleAuthUtils
Browse files Browse the repository at this point in the history
Follow-up on bazelbuild#15930 due to
bazelbuild#15906

Progress on bazelbuild#15856

Closes bazelbuild#15941.

PiperOrigin-RevId: 462580799
Change-Id: Ibac79ed68a0c87a4f34eb9d0729abb1552b44519
  • Loading branch information
Yannic authored and “Gowroji committed Jul 22, 2022
1 parent 9c2c3de commit 5e7351b
Show file tree
Hide file tree
Showing 8 changed files with 481 additions and 66 deletions.
26 changes: 26 additions & 0 deletions src/main/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -217,6 +241,7 @@ java_library(
"buildtool/BuildRequestOptions.java",
"runtime/BlazeCommandResult.java",
"runtime/CommandDispatcher.java",
"runtime/CommandLinePathFactory.java",
"runtime/KeepGoingOption.java",
"runtime/LoadingPhaseThreadsOption.java",
],
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/google/devtools/build/lib/authandtls/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
*
* <ol>
* <li>If environment variable $NETRC exists, use it as the path to the .netrc file
* <li>Fallback to $HOME/.netrc
* </ol>
*
* @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<Credentials> newCredentialsFromNetrc(
Map<String, String> clientEnv, FileSystem fileSystem) throws IOException {
Optional<String> 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<AuthAndTLSOptions.UnresolvedScopedCredentialHelper> helpers)
throws IOException {
Preconditions.checkNotNull(environment);
Preconditions.checkNotNull(pathFactory);
Preconditions.checkNotNull(helpers);

CredentialHelperProvider.Builder builder = CredentialHelperProvider.builder();
for (AuthAndTLSOptions.UnresolvedScopedCredentialHelper helper : helpers) {
Optional<String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
*
* <ol>
* <li>If environment variable $NETRC exists, use it as the path to the .netrc file
* <li>Fallback to $HOME/.netrc
* </ol>
*
* @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<String, String> 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:
*
* <ol>
* <li>If authentication enabled by flags, use it to create credentials
* <li>Use .netrc to provide credentials if exists
* <li>Otherwise, return {@code null}
* </ol>
*
* @throws IOException in case the credentials can't be constructed.
*/
@VisibleForTesting
static Credentials newCredentials(
Map<String, String> clientEnv,
FileSystem fileSystem,
Expand Down Expand Up @@ -1132,6 +1077,6 @@ static Credentials newCredentials(
}
}

return creds;
return credentials;
}
}
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/google/devtools/build/lib/authandtls/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5e7351b

Please sign in to comment.