Yannic Bonenberger | c8d445a | 2022-11-03 04:23:46 -0700 | [diff] [blame] | 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; |
| 16 | |
| 17 | import com.google.auth.Credentials; |
| 18 | import com.google.common.base.Preconditions; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import java.net.URI; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | /** Implementation of {@link Credentials} which provides a static set of credentials. */ |
| 25 | public final class StaticCredentials extends Credentials { |
| 26 | public static final StaticCredentials EMPTY = new StaticCredentials(ImmutableMap.of()); |
| 27 | |
| 28 | private final ImmutableMap<URI, Map<String, List<String>>> credentials; |
| 29 | |
| 30 | public StaticCredentials(Map<URI, Map<String, List<String>>> credentials) { |
| 31 | Preconditions.checkNotNull(credentials); |
| 32 | |
| 33 | this.credentials = ImmutableMap.copyOf(credentials); |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public String getAuthenticationType() { |
| 38 | return "static"; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public Map<String, List<String>> getRequestMetadata(URI uri) { |
| 43 | Preconditions.checkNotNull(uri); |
| 44 | |
| 45 | return credentials.getOrDefault(uri, ImmutableMap.of()); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public boolean hasRequestMetadata() { |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean hasRequestMetadataOnly() { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void refresh() { |
| 60 | // Can't refresh static credentials. |
| 61 | } |
| 62 | } |