|
| 1 | +// Copyright 2022 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.devtools.build.lib.authandtls.credentialhelper; |
| 16 | + |
| 17 | +import com.github.benmanes.caffeine.cache.CacheLoader; |
| 18 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 19 | +import com.github.benmanes.caffeine.cache.LoadingCache; |
| 20 | +import com.google.auth.Credentials; |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | +import java.io.IOException; |
| 24 | +import java.net.URI; |
| 25 | +import java.time.Duration; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Optional; |
| 29 | +import javax.annotation.Nullable; |
| 30 | + |
| 31 | +/** |
| 32 | + * Implementation of {@link Credentials} which fetches credentials by invoking a {@code credential |
| 33 | + * helper} as subprocess, falling back to another {@link Credentials} if no suitable helper exists. |
| 34 | + */ |
| 35 | +public class CredentialHelperCredentials extends Credentials { |
| 36 | + private final Optional<Credentials> fallbackCredentials; |
| 37 | + |
| 38 | + private final LoadingCache<URI, GetCredentialsResponse> credentialCache; |
| 39 | + |
| 40 | + public CredentialHelperCredentials( |
| 41 | + CredentialHelperProvider credentialHelperProvider, |
| 42 | + CredentialHelperEnvironment credentialHelperEnvironment, |
| 43 | + Optional<Credentials> fallbackCredentials, |
| 44 | + Duration cacheTimeout) { |
| 45 | + Preconditions.checkNotNull(credentialHelperProvider); |
| 46 | + Preconditions.checkNotNull(credentialHelperEnvironment); |
| 47 | + this.fallbackCredentials = Preconditions.checkNotNull(fallbackCredentials); |
| 48 | + Preconditions.checkNotNull(cacheTimeout); |
| 49 | + Preconditions.checkArgument( |
| 50 | + !cacheTimeout.isNegative() && !cacheTimeout.isZero(), |
| 51 | + "Cache timeout must be greater than 0"); |
| 52 | + |
| 53 | + credentialCache = |
| 54 | + Caffeine.newBuilder() |
| 55 | + .expireAfterWrite(cacheTimeout) |
| 56 | + .build( |
| 57 | + new CredentialHelperCacheLoader( |
| 58 | + credentialHelperProvider, credentialHelperEnvironment)); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String getAuthenticationType() { |
| 63 | + if (fallbackCredentials.isPresent()) { |
| 64 | + return "credential-helper-with-fallback-" + fallbackCredentials.get().getAuthenticationType(); |
| 65 | + } |
| 66 | + |
| 67 | + return "credential-helper"; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException { |
| 72 | + Preconditions.checkNotNull(uri); |
| 73 | + |
| 74 | + Optional<Map<String, List<String>>> credentials = getRequestMetadataFromCredentialHelper(uri); |
| 75 | + if (credentials.isPresent()) { |
| 76 | + return credentials.get(); |
| 77 | + } |
| 78 | + |
| 79 | + if (fallbackCredentials.isPresent()) { |
| 80 | + return fallbackCredentials.get().getRequestMetadata(uri); |
| 81 | + } |
| 82 | + |
| 83 | + return ImmutableMap.of(); |
| 84 | + } |
| 85 | + |
| 86 | + @SuppressWarnings("unchecked") // Map<String, ImmutableList<String>> to Map<String<List<String>> |
| 87 | + private Optional<Map<String, List<String>>> getRequestMetadataFromCredentialHelper(URI uri) { |
| 88 | + Preconditions.checkNotNull(uri); |
| 89 | + |
| 90 | + GetCredentialsResponse response = credentialCache.get(uri); |
| 91 | + |
| 92 | + return Optional.ofNullable(response).map(value -> (Map) value.getHeaders()); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean hasRequestMetadata() { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean hasRequestMetadataOnly() { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void refresh() throws IOException { |
| 107 | + if (fallbackCredentials.isPresent()) { |
| 108 | + fallbackCredentials.get().refresh(); |
| 109 | + } |
| 110 | + |
| 111 | + credentialCache.invalidateAll(); |
| 112 | + } |
| 113 | + |
| 114 | + private static final class CredentialHelperCacheLoader |
| 115 | + implements CacheLoader<URI, GetCredentialsResponse> { |
| 116 | + private final CredentialHelperProvider credentialHelperProvider; |
| 117 | + private final CredentialHelperEnvironment credentialHelperEnvironment; |
| 118 | + |
| 119 | + public CredentialHelperCacheLoader( |
| 120 | + CredentialHelperProvider credentialHelperProvider, |
| 121 | + CredentialHelperEnvironment credentialHelperEnvironment) { |
| 122 | + this.credentialHelperProvider = Preconditions.checkNotNull(credentialHelperProvider); |
| 123 | + this.credentialHelperEnvironment = Preconditions.checkNotNull(credentialHelperEnvironment); |
| 124 | + } |
| 125 | + |
| 126 | + @Nullable |
| 127 | + @Override |
| 128 | + public GetCredentialsResponse load(URI uri) throws IOException, InterruptedException { |
| 129 | + Preconditions.checkNotNull(uri); |
| 130 | + |
| 131 | + Optional<CredentialHelper> maybeCredentialHelper = |
| 132 | + credentialHelperProvider.findCredentialHelper(uri); |
| 133 | + if (maybeCredentialHelper.isEmpty()) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + CredentialHelper credentialHelper = maybeCredentialHelper.get(); |
| 137 | + |
| 138 | + return credentialHelper.getCredentials(credentialHelperEnvironment, uri); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments