|
19 | 19 | import com.google.common.annotations.VisibleForTesting;
|
20 | 20 | import com.google.common.base.Preconditions;
|
21 | 21 | 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; |
22 | 29 | import io.grpc.CallCredentials;
|
23 | 30 | import io.grpc.ClientInterceptor;
|
24 | 31 | import io.grpc.ManagedChannel;
|
@@ -258,4 +265,63 @@ public static Credentials newCredentials(
|
258 | 265 | throw new IOException(message, e);
|
259 | 266 | }
|
260 | 267 | }
|
| 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 | + } |
261 | 327 | }
|
0 commit comments