blob: ada6dc040bd750fe93d9e21181afac755b5e601a [file] [log] [blame]
Yannic Bonenbergerc8d445a2022-11-03 04:23:46 -07001// 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
15package com.google.devtools.build.lib.authandtls;
16
17import com.google.auth.Credentials;
18import com.google.common.base.Preconditions;
19import com.google.common.collect.ImmutableMap;
20import java.net.URI;
21import java.util.List;
22import java.util.Map;
23
24/** Implementation of {@link Credentials} which provides a static set of credentials. */
25public 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}