forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wire up credential helper to command-line flag(s)
RELNOTES: Add support for fetching RPC credentials from a credential helper. This release note clearly needs improvement! Progress on bazelbuild#15856
- Loading branch information
Showing
10 changed files
with
242 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...om/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelperCredentials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package com.google.devtools.build.lib.authandtls.credentialhelper; | ||
|
||
import com.github.benmanes.caffeine.cache.CacheLoader; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.github.benmanes.caffeine.cache.LoadingCache; | ||
import com.google.auth.Credentials; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableMap; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Implementation of {@link Credentials} which fetches credentials by invoking a {@code credential | ||
* helper} as subprocess. | ||
*/ | ||
public class CredentialHelperCredentials extends Credentials { | ||
private final Optional<Credentials> fallbackCredentials; | ||
|
||
private final LoadingCache<URI, GetCredentialsResponse> credentialCache; | ||
|
||
public CredentialHelperCredentials( | ||
CredentialHelperProvider credentialHelperProvider, | ||
CredentialHelperEnvironment credentialHelperEnvironment, | ||
Optional<Credentials> fallbackCredentials, | ||
Duration cacheTimeout) { | ||
Preconditions.checkNotNull(credentialHelperProvider); | ||
Preconditions.checkNotNull(credentialHelperEnvironment); | ||
this.fallbackCredentials = Preconditions.checkNotNull(fallbackCredentials); | ||
Preconditions.checkNotNull(cacheTimeout); | ||
Preconditions.checkArgument(!cacheTimeout.isNegative() && !cacheTimeout.isZero(), "Cache timeout must be greater than 0"); | ||
|
||
credentialCache = | ||
Caffeine.newBuilder() | ||
.expireAfterWrite(cacheTimeout) | ||
.build( | ||
new CredentialHelperCacheLoader( | ||
credentialHelperProvider, credentialHelperEnvironment)); | ||
} | ||
|
||
@Override | ||
public String getAuthenticationType() { | ||
if (fallbackCredentials.isPresent()) { | ||
return "credential-helper-with-fallback-" + fallbackCredentials.get().getAuthenticationType(); | ||
} | ||
|
||
return "credential-helper"; | ||
} | ||
|
||
@Override | ||
public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException { | ||
Preconditions.checkNotNull(uri); | ||
|
||
Optional<Map<String, List<String>>> credentials = | ||
getRequestMetadataFromCredentialHelper(uri); | ||
if (credentials.isPresent()) { | ||
return credentials.get(); | ||
} | ||
|
||
if (fallbackCredentials.isPresent()) { | ||
return fallbackCredentials.get().getRequestMetadata(uri); | ||
} | ||
|
||
return ImmutableMap.of(); | ||
} | ||
|
||
private Optional<Map<String, List<String>>> getRequestMetadataFromCredentialHelper( | ||
URI uri) throws IOException { | ||
Preconditions.checkNotNull(uri); | ||
|
||
GetCredentialsResponse response = credentialCache.get(uri); | ||
if (response == null) { | ||
return Optional.empty(); | ||
} | ||
|
||
// The cast is needed to convert value type of map from `ImmutableList` to `List`. | ||
return Optional.of((Map)response.getHeaders()); | ||
} | ||
|
||
@Override | ||
public boolean hasRequestMetadata() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean hasRequestMetadataOnly() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void refresh() throws IOException { | ||
if (fallbackCredentials.isPresent()) { | ||
fallbackCredentials.get().refresh(); | ||
} | ||
|
||
credentialCache.invalidateAll(); | ||
} | ||
|
||
private static final class CredentialHelperCacheLoader implements CacheLoader<URI, GetCredentialsResponse> { | ||
private final CredentialHelperProvider credentialHelperProvider; | ||
private final CredentialHelperEnvironment credentialHelperEnvironment; | ||
|
||
public CredentialHelperCacheLoader( | ||
CredentialHelperProvider credentialHelperProvider, | ||
CredentialHelperEnvironment credentialHelperEnvironment) { | ||
this.credentialHelperProvider = Preconditions.checkNotNull(credentialHelperProvider); | ||
this.credentialHelperEnvironment = Preconditions.checkNotNull(credentialHelperEnvironment); | ||
} | ||
|
||
@Override | ||
public GetCredentialsResponse load(URI uri) throws IOException, InterruptedException { | ||
Preconditions.checkNotNull(uri); | ||
|
||
Optional<GetCredentialsResponse> response = loadInternal(uri); | ||
if (response.isPresent()) { | ||
return response.get(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private Optional<GetCredentialsResponse> loadInternal(URI uri) throws IOException, InterruptedException { | ||
Preconditions.checkNotNull(uri); | ||
|
||
Optional<CredentialHelper> maybeCredentialHelper = credentialHelperProvider.findCredentialHelper(uri); | ||
if (!maybeCredentialHelper.isPresent()) { | ||
return Optional.empty(); | ||
} | ||
CredentialHelper credentialHelper = maybeCredentialHelper.get(); | ||
|
||
return Optional.of(credentialHelper.getCredentials(credentialHelperEnvironment, uri)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.