1
1
package org .jenkinsci .plugins .kubernetes .credentials ;
2
2
3
+ import java .io .File ;
4
+ import java .io .IOException ;
5
+
6
+ import org .apache .commons .io .FileUtils ;
7
+ import org .jenkinsci .plugins .plaincredentials .StringCredentials ;
8
+ import org .kohsuke .stapler .DataBoundConstructor ;
9
+
3
10
import com .cloudbees .plugins .credentials .CredentialsScope ;
4
11
import com .cloudbees .plugins .credentials .impl .BaseStandardCredentials ;
5
12
6
13
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
7
14
import hudson .Extension ;
8
- import org .apache .commons .io .FileUtils ;
9
- import org .kohsuke .stapler .DataBoundConstructor ;
10
-
11
- import java .io .File ;
12
- import java .io .IOException ;
15
+ import hudson .util .Secret ;
13
16
14
17
/**
15
18
* Read the OAuth bearer token from service account file provisioned by kubernetes
18
21
*
19
22
* @author <a href="mailto:[email protected] ">Nicolas De Loof</a>
20
23
*/
21
- public class ServiceAccountCredential extends BaseStandardCredentials implements TokenProducer {
24
+ public class FileSystemServiceAccountCredential extends BaseStandardCredentials
25
+ implements TokenProducer , StringCredentials {
26
+
27
+ private static final long serialVersionUID = -2222994164073756041L ;
22
28
23
29
private static final String SERVICEACCOUNT_TOKEN_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/token" ;
24
30
25
31
@ DataBoundConstructor
26
- public ServiceAccountCredential (CredentialsScope scope , String id , String description ) {
32
+ public FileSystemServiceAccountCredential (CredentialsScope scope , String id , String description ) {
27
33
super (scope , id , description );
28
34
}
29
35
30
36
@ Override
31
37
@ SuppressFBWarnings ("DMI_HARDCODED_ABSOLUTE_FILENAME" )
32
- public String getToken ( String serviceAddress , String caCertData , boolean skipTlsVerify ) {
38
+ public Secret getSecret ( ) {
33
39
try {
34
- return FileUtils .readFileToString (new File (SERVICEACCOUNT_TOKEN_PATH ));
40
+ return Secret . fromString ( FileUtils .readFileToString (new File (SERVICEACCOUNT_TOKEN_PATH ) ));
35
41
} catch (IOException e ) {
36
- return null ;
42
+ return Secret . fromString ( null ) ;
37
43
}
38
44
}
39
45
46
+ @ Override
47
+ @ SuppressFBWarnings ("DMI_HARDCODED_ABSOLUTE_FILENAME" )
48
+ @ Deprecated
49
+ public String getToken (String serviceAddress , String caCertData , boolean skipTlsVerify ) {
50
+ return getSecret ().getPlainText ();
51
+ }
52
+
40
53
@ Extension (optional = true )
41
54
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {
42
55
@@ -52,4 +65,5 @@ public String getDisplayName() {
52
65
return "Kubernetes Service Account" ;
53
66
}
54
67
}
68
+
55
69
}
0 commit comments