Skip to content

Commit 5e7351b

Browse files
Yannic“Gowroji
authored and
“Gowroji
committed
Move newCredentialHelperProvider into GoogleAuthUtils
Follow-up on bazelbuild#15930 due to bazelbuild#15906 Progress on bazelbuild#15856 Closes bazelbuild#15941. PiperOrigin-RevId: 462580799 Change-Id: Ibac79ed68a0c87a4f34eb9d0729abb1552b44519
1 parent 9c2c3de commit 5e7351b

File tree

8 files changed

+481
-66
lines changed

8 files changed

+481
-66
lines changed

src/main/java/com/google/devtools/build/lib/BUILD

+26
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,30 @@ java_library(
205205
],
206206
)
207207

208+
java_library(
209+
name = "runtime/memory_pressure_event",
210+
srcs = [
211+
"runtime/MemoryPressureEvent.java",
212+
],
213+
deps = [
214+
"//third_party:auto_value",
215+
"//third_party:guava",
216+
],
217+
)
218+
219+
java_library(
220+
name = "runtime/command_line_path_factory",
221+
srcs = [
222+
"runtime/CommandLinePathFactory.java",
223+
],
224+
deps = [
225+
"//src/main/java/com/google/devtools/build/lib/analysis:blaze_directories",
226+
"//src/main/java/com/google/devtools/build/lib/vfs",
227+
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
228+
"//third_party:guava",
229+
],
230+
)
231+
208232
java_library(
209233
name = "runtime",
210234
srcs = glob(
@@ -217,6 +241,7 @@ java_library(
217241
"buildtool/BuildRequestOptions.java",
218242
"runtime/BlazeCommandResult.java",
219243
"runtime/CommandDispatcher.java",
244+
"runtime/CommandLinePathFactory.java",
220245
"runtime/KeepGoingOption.java",
221246
"runtime/LoadingPhaseThreadsOption.java",
222247
],
@@ -227,6 +252,7 @@ java_library(
227252
":loading-phase-threads-option",
228253
":runtime/blaze_command_result",
229254
":runtime/command_dispatcher",
255+
":runtime/command_line_path_factory",
230256
"//src/main/java/com/google/devtools/build/lib/actions",
231257
"//src/main/java/com/google/devtools/build/lib/actions:action_lookup_data",
232258
"//src/main/java/com/google/devtools/build/lib/actions:artifacts",

src/main/java/com/google/devtools/build/lib/authandtls/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ java_library(
1414
name = "authandtls",
1515
srcs = glob(["*.java"]),
1616
deps = [
17+
"//src/main/java/com/google/devtools/build/lib:runtime/command_line_path_factory",
18+
"//src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper",
1719
"//src/main/java/com/google/devtools/build/lib/concurrent",
1820
"//src/main/java/com/google/devtools/common/options",
1921
"//third_party:auth",

src/main/java/com/google/devtools/build/lib/authandtls/GoogleAuthUtils.java

+66
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
import com.google.common.annotations.VisibleForTesting;
2020
import com.google.common.base.Preconditions;
2121
import com.google.common.base.Strings;
22+
import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialHelperEnvironment;
23+
import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialHelperProvider;
24+
import com.google.devtools.build.lib.events.Event;
25+
import com.google.devtools.build.lib.events.Reporter;
26+
import com.google.devtools.build.lib.runtime.CommandLinePathFactory;
27+
import com.google.devtools.build.lib.vfs.FileSystem;
28+
import com.google.devtools.build.lib.vfs.Path;
2229
import io.grpc.CallCredentials;
2330
import io.grpc.ClientInterceptor;
2431
import io.grpc.ManagedChannel;
@@ -258,4 +265,63 @@ public static Credentials newCredentials(
258265
throw new IOException(message, e);
259266
}
260267
}
268+
269+
/**
270+
* Create a new {@link Credentials} object by parsing the .netrc file with following order to
271+
* search it:
272+
*
273+
* <ol>
274+
* <li>If environment variable $NETRC exists, use it as the path to the .netrc file
275+
* <li>Fallback to $HOME/.netrc
276+
* </ol>
277+
*
278+
* @return the {@link Credentials} object or {@code null} if there is no .netrc file.
279+
* @throws IOException in case the credentials can't be constructed.
280+
*/
281+
@VisibleForTesting
282+
static Optional<Credentials> newCredentialsFromNetrc(
283+
Map<String, String> clientEnv, FileSystem fileSystem) throws IOException {
284+
Optional<String> netrcFileString =
285+
Optional.ofNullable(clientEnv.get("NETRC"))
286+
.or(() -> Optional.ofNullable(clientEnv.get("HOME")).map(home -> home + "/.netrc"));
287+
if (netrcFileString.isEmpty()) {
288+
return Optional.empty();
289+
}
290+
291+
Path netrcFile = fileSystem.getPath(netrcFileString.get());
292+
if (!netrcFile.exists()) {
293+
return Optional.empty();
294+
}
295+
296+
try {
297+
Netrc netrc = NetrcParser.parseAndClose(netrcFile.getInputStream());
298+
return Optional.of(new NetrcCredentials(netrc));
299+
} catch (IOException e) {
300+
throw new IOException(
301+
"Failed to parse " + netrcFile.getPathString() + ": " + e.getMessage(), e);
302+
}
303+
}
304+
305+
@VisibleForTesting
306+
public static CredentialHelperProvider newCredentialHelperProvider(
307+
CredentialHelperEnvironment environment,
308+
CommandLinePathFactory pathFactory,
309+
List<AuthAndTLSOptions.UnresolvedScopedCredentialHelper> helpers)
310+
throws IOException {
311+
Preconditions.checkNotNull(environment);
312+
Preconditions.checkNotNull(pathFactory);
313+
Preconditions.checkNotNull(helpers);
314+
315+
CredentialHelperProvider.Builder builder = CredentialHelperProvider.builder();
316+
for (AuthAndTLSOptions.UnresolvedScopedCredentialHelper helper : helpers) {
317+
Optional<String> scope = helper.getScope();
318+
Path path = pathFactory.create(environment.getClientEnvironment(), helper.getPath());
319+
if (scope.isPresent()) {
320+
builder.add(scope.get(), path);
321+
} else {
322+
builder.add(path);
323+
}
324+
}
325+
return builder.build();
326+
}
261327
}

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

+1-56
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@
4747
import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions;
4848
import com.google.devtools.build.lib.authandtls.CallCredentialsProvider;
4949
import com.google.devtools.build.lib.authandtls.GoogleAuthUtils;
50-
import com.google.devtools.build.lib.authandtls.Netrc;
51-
import com.google.devtools.build.lib.authandtls.NetrcCredentials;
52-
import com.google.devtools.build.lib.authandtls.NetrcParser;
5350
import com.google.devtools.build.lib.bazel.repository.downloader.Downloader;
5451
import com.google.devtools.build.lib.buildeventstream.BuildEventArtifactUploader;
5552
import com.google.devtools.build.lib.buildeventstream.LocalFilesArtifactUploader;
@@ -1047,58 +1044,6 @@ RemoteActionContextProvider getActionContextProvider() {
10471044
return actionContextProvider;
10481045
}
10491046

1050-
/**
1051-
* Create a new {@link Credentials} object by parsing the .netrc file with following order to
1052-
* search it:
1053-
*
1054-
* <ol>
1055-
* <li>If environment variable $NETRC exists, use it as the path to the .netrc file
1056-
* <li>Fallback to $HOME/.netrc
1057-
* </ol>
1058-
*
1059-
* @return the {@link Credentials} object or {@code null} if there is no .netrc file.
1060-
* @throws IOException in case the credentials can't be constructed.
1061-
*/
1062-
@VisibleForTesting
1063-
static Credentials newCredentialsFromNetrc(Map<String, String> clientEnv, FileSystem fileSystem)
1064-
throws IOException {
1065-
String netrcFileString =
1066-
Optional.ofNullable(clientEnv.get("NETRC"))
1067-
.orElseGet(
1068-
() ->
1069-
Optional.ofNullable(clientEnv.get("HOME"))
1070-
.map(home -> home + "/.netrc")
1071-
.orElse(null));
1072-
if (netrcFileString == null) {
1073-
return null;
1074-
}
1075-
1076-
Path netrcFile = fileSystem.getPath(netrcFileString);
1077-
if (netrcFile.exists()) {
1078-
try {
1079-
Netrc netrc = NetrcParser.parseAndClose(netrcFile.getInputStream());
1080-
return new NetrcCredentials(netrc);
1081-
} catch (IOException e) {
1082-
throw new IOException(
1083-
"Failed to parse " + netrcFile.getPathString() + ": " + e.getMessage(), e);
1084-
}
1085-
} else {
1086-
return null;
1087-
}
1088-
}
1089-
1090-
/**
1091-
* Create a new {@link Credentials} with following order:
1092-
*
1093-
* <ol>
1094-
* <li>If authentication enabled by flags, use it to create credentials
1095-
* <li>Use .netrc to provide credentials if exists
1096-
* <li>Otherwise, return {@code null}
1097-
* </ol>
1098-
*
1099-
* @throws IOException in case the credentials can't be constructed.
1100-
*/
1101-
@VisibleForTesting
11021047
static Credentials newCredentials(
11031048
Map<String, String> clientEnv,
11041049
FileSystem fileSystem,
@@ -1132,6 +1077,6 @@ static Credentials newCredentials(
11321077
}
11331078
}
11341079

1135-
return creds;
1080+
return credentials;
11361081
}
11371082
}

src/test/java/com/google/devtools/build/lib/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ java_test(
118118
"//src/main/java/com/google/devtools/build/lib:runtime",
119119
"//src/main/java/com/google/devtools/build/lib:runtime/blaze_command_result",
120120
"//src/main/java/com/google/devtools/build/lib:runtime/command_dispatcher",
121+
"//src/main/java/com/google/devtools/build/lib:runtime/command_line_path_factory",
121122
"//src/main/java/com/google/devtools/build/lib:runtime/safe_request_logging",
122123
"//src/main/java/com/google/devtools/build/lib/actions",
123124
"//src/main/java/com/google/devtools/build/lib/actions:action_lookup_data",

src/test/java/com/google/devtools/build/lib/authandtls/BUILD

+8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ java_library(
2424
],
2525
),
2626
deps = [
27+
"//src/main/java/com/google/devtools/build/lib:runtime/command_line_path_factory",
2728
"//src/main/java/com/google/devtools/build/lib/authandtls",
29+
"//src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper",
30+
"//src/main/java/com/google/devtools/build/lib/events",
31+
"//src/main/java/com/google/devtools/build/lib/vfs",
32+
"//src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs",
33+
"//src/main/java/com/google/devtools/common/options",
34+
"//src/test/java/com/google/devtools/build/lib/testutil",
35+
"//third_party:auth",
2836
"//third_party:guava",
2937
"//third_party:junit4",
3038
"//third_party:mockito",

0 commit comments

Comments
 (0)