Skip to content

Commit 3fc7c83

Browse files
committed
Merge pull request jenkinsci#18 from ndeloof/ServiceAccount
Service account
2 parents 25903d9 + e5dbb9d commit 3fc7c83

File tree

4 files changed

+92
-27
lines changed

4 files changed

+92
-27
lines changed
Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,11 @@
11
package org.csanchez.jenkins.plugins.kubernetes;
22

3-
import com.cloudbees.plugins.credentials.CredentialsScope;
4-
import com.cloudbees.plugins.credentials.impl.*;
5-
import hudson.Extension;
6-
import hudson.util.Secret;
7-
import org.kohsuke.stapler.DataBoundConstructor;
3+
import com.cloudbees.plugins.credentials.Credentials;
4+
import com.cloudbees.plugins.credentials.common.StandardCredentials;
85

96
/**
107
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
118
*/
12-
public class BearerTokenCredential extends BaseStandardCredentials {
13-
14-
private final Secret token;
15-
16-
@DataBoundConstructor
17-
public BearerTokenCredential(CredentialsScope scope, String id, String description, String token) {
18-
super(scope, id, description);
19-
this.token = Secret.fromString(token);
20-
}
21-
22-
public String getToken() {
23-
return Secret.toString(token);
24-
}
25-
26-
@Extension
27-
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {
28-
29-
@Override
30-
public String getDisplayName() {
31-
return "OAuth Bearer token";
32-
}
33-
}
9+
public interface BearerTokenCredential extends Credentials, StandardCredentials {
10+
String getToken();
3411
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.csanchez.jenkins.plugins.kubernetes;
2+
3+
import com.cloudbees.plugins.credentials.CredentialsScope;
4+
import com.cloudbees.plugins.credentials.impl.*;
5+
import hudson.Extension;
6+
import hudson.util.Secret;
7+
import org.kohsuke.stapler.DataBoundConstructor;
8+
9+
/**
10+
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
11+
*/
12+
public class BearerTokenCredentialImpl extends BaseStandardCredentials implements BearerTokenCredential {
13+
14+
private final Secret token;
15+
16+
@DataBoundConstructor
17+
public BearerTokenCredentialImpl(CredentialsScope scope, String id, String description, String token) {
18+
super(scope, id, description);
19+
this.token = Secret.fromString(token);
20+
}
21+
22+
@Override
23+
public String getToken() {
24+
return Secret.toString(token);
25+
}
26+
27+
@Extension
28+
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {
29+
30+
@Override
31+
public String getDisplayName() {
32+
return "OAuth Bearer token";
33+
}
34+
}
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.csanchez.jenkins.plugins.kubernetes;
2+
3+
import com.cloudbees.plugins.credentials.CredentialsScope;
4+
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials;
5+
import hudson.Extension;
6+
import org.apache.commons.io.FileUtils;
7+
import org.kohsuke.stapler.DataBoundConstructor;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
/**
13+
* Read the OAuth bearer token from service account file provisionned by kubernetes
14+
* <a href="http://kubernetes.io/v1.0/docs/admin/service-accounts-admin.html">Service Account Admission Controller</a>
15+
* when Jenkins itself is deployed inside a Pod.
16+
*
17+
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
18+
*/
19+
public class ServiceAccountCredential extends BaseStandardCredentials implements BearerTokenCredential {
20+
21+
@DataBoundConstructor
22+
public ServiceAccountCredential(CredentialsScope scope, String id, String description) {
23+
super(scope, id, description);
24+
}
25+
26+
@Override
27+
public String getToken() {
28+
try {
29+
return FileUtils.readFileToString(new File("/run/secrets/kubernetes.io/serviceaccount/token"));
30+
} catch (IOException e) {
31+
return null;
32+
}
33+
}
34+
35+
@Extension(optional = true)
36+
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {
37+
38+
public DescriptorImpl() {
39+
if (!new File("/run/secrets/kubernetes.io/serviceaccount/token").exists()) {
40+
throw new RuntimeException("Jenkins isn't running inside Kubernetes with Admission Controller.");
41+
}
42+
}
43+
44+
@Override
45+
public String getDisplayName() {
46+
return "Kubernetes Service Account";
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler">
3+
<st:include page="id-and-description" class="${descriptor.clazz}"/>
4+
</j:jelly>

0 commit comments

Comments
 (0)