14
14
15
15
package com .google .devtools .build .lib .authandtls .credentialhelper ;
16
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 ;
17
+ import com .github .benmanes .caffeine .cache .Cache ;
20
18
import com .google .auth .Credentials ;
21
19
import com .google .common .base .Preconditions ;
20
+ import com .google .common .collect .ImmutableList ;
22
21
import com .google .common .collect .ImmutableMap ;
23
22
import java .io .IOException ;
24
23
import java .net .URI ;
25
- import java .time .Duration ;
26
24
import java .util .List ;
27
25
import java .util .Map ;
28
26
import java .util .Optional ;
33
31
* helper} as subprocess, falling back to another {@link Credentials} if no suitable helper exists.
34
32
*/
35
33
public class CredentialHelperCredentials extends Credentials {
34
+ private final CredentialHelperProvider credentialHelperProvider ;
35
+ private final CredentialHelperEnvironment credentialHelperEnvironment ;
36
+ private final Cache <URI , ImmutableMap <String , ImmutableList <String >>> credentialCache ;
36
37
private final Optional <Credentials > fallbackCredentials ;
37
38
38
- private final LoadingCache <URI , GetCredentialsResponse > credentialCache ;
39
+ /** Wraps around an {@link IOException} so we can smuggle it through {@link Cache#get}. */
40
+ public static final class WrappedIOException extends RuntimeException {
41
+ private final IOException wrapped ;
42
+
43
+ WrappedIOException (IOException e ) {
44
+ super (e );
45
+ this .wrapped = e ;
46
+ }
47
+
48
+ IOException getWrapped () {
49
+ return wrapped ;
50
+ }
51
+ }
39
52
40
53
public CredentialHelperCredentials (
41
54
CredentialHelperProvider credentialHelperProvider ,
42
55
CredentialHelperEnvironment credentialHelperEnvironment ,
43
- Optional <Credentials > fallbackCredentials ,
44
- Duration cacheTimeout ) {
45
- Preconditions .checkNotNull (credentialHelperProvider );
46
- Preconditions .checkNotNull (credentialHelperEnvironment );
56
+ Cache <URI , ImmutableMap <String , ImmutableList <String >>> credentialCache ,
57
+ Optional <Credentials > fallbackCredentials ) {
58
+ this .credentialHelperProvider = Preconditions .checkNotNull (credentialHelperProvider );
59
+ this .credentialHelperEnvironment = Preconditions .checkNotNull (credentialHelperEnvironment );
60
+ this .credentialCache = Preconditions .checkNotNull (credentialCache );
47
61
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
62
}
60
63
61
64
@ Override
@@ -68,12 +71,18 @@ public String getAuthenticationType() {
68
71
}
69
72
70
73
@ Override
74
+ @ SuppressWarnings ("unchecked" ) // Map<String, ImmutableList<String>> to Map<String<List<String>>
71
75
public Map <String , List <String >> getRequestMetadata (URI uri ) throws IOException {
72
76
Preconditions .checkNotNull (uri );
73
77
74
- Optional <Map <String , List <String >>> credentials = getRequestMetadataFromCredentialHelper (uri );
75
- if (credentials .isPresent ()) {
76
- return credentials .get ();
78
+ ImmutableMap <String , ImmutableList <String >> credentials ;
79
+ try {
80
+ credentials = credentialCache .get (uri , this ::getCredentialsFromHelper );
81
+ } catch (WrappedIOException e ) {
82
+ throw e .getWrapped ();
83
+ }
84
+ if (credentials != null ) {
85
+ return (Map ) credentials ;
77
86
}
78
87
79
88
if (fallbackCredentials .isPresent ()) {
@@ -83,13 +92,28 @@ public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException
83
92
return ImmutableMap .of ();
84
93
}
85
94
86
- @ SuppressWarnings ( "unchecked" ) // Map<String, ImmutableList<String>> to Map<String<List<String>>
87
- private Optional < Map < String , List <String >>> getRequestMetadataFromCredentialHelper (URI uri ) {
95
+ @ Nullable
96
+ private ImmutableMap < String , ImmutableList <String >> getCredentialsFromHelper (URI uri ) {
88
97
Preconditions .checkNotNull (uri );
89
98
90
- GetCredentialsResponse response = credentialCache .get (uri );
99
+ Optional <CredentialHelper > maybeCredentialHelper =
100
+ credentialHelperProvider .findCredentialHelper (uri );
101
+ if (maybeCredentialHelper .isEmpty ()) {
102
+ return null ;
103
+ }
104
+ CredentialHelper credentialHelper = maybeCredentialHelper .get ();
105
+
106
+ GetCredentialsResponse response ;
107
+ try {
108
+ response = credentialHelper .getCredentials (credentialHelperEnvironment , uri );
109
+ } catch (IOException e ) {
110
+ throw new WrappedIOException (e );
111
+ }
112
+ if (response == null ) {
113
+ return null ;
114
+ }
91
115
92
- return Optional . ofNullable ( response ). map ( value -> ( Map ) value . getHeaders () );
116
+ return response . getHeaders ();
93
117
}
94
118
95
119
@ Override
@@ -110,32 +134,4 @@ public void refresh() throws IOException {
110
134
111
135
credentialCache .invalidateAll ();
112
136
}
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
137
}
0 commit comments