Skip to content

Commit 2cf92df

Browse files
Alexander Tkachenkotkalex
Alexander Tkachenko
authored andcommitted
enable support of AuthenticationTokens
1 parent a29d360 commit 2cf92df

7 files changed

+137
-1
lines changed

pom.xml

+18-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
<slf4j.version>1.7.25</slf4j.version>
4444

4545
<!-- jenkins plugins versions -->
46-
<jenkins-credentials.version>2.1.7</jenkins-credentials.version>
46+
<jenkins-credentials.version>2.1.16</jenkins-credentials.version>
47+
<jenkins-structs.version>1.19</jenkins-structs.version>
4748
<jenkins-plain-credentials.version>1.3</jenkins-plain-credentials.version>
4849

4950
<!-- maven plugins versions -->
@@ -78,6 +79,22 @@
7879
<artifactId>plain-credentials</artifactId>
7980
<version>${jenkins-plain-credentials.version}</version>
8081
</dependency>
82+
<dependency>
83+
<groupId>org.jenkins-ci.plugins</groupId>
84+
<artifactId>authentication-tokens</artifactId>
85+
<version>1.3</version>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.jenkins-ci.plugins</groupId>
89+
<artifactId>google-oauth-plugin</artifactId>
90+
<version>0.8</version>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.jenkins-ci.plugins</groupId>
94+
<artifactId>docker-commons</artifactId>
95+
<version>1.14</version>
96+
</dependency>
97+
8198
</dependencies>
8299

83100
<!-- just to fix enforcer RequireUpperBoundDeps -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.jenkinsci.plugins.kubernetes.auth;
2+
3+
public interface KubernetesAuth {
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.jenkinsci.plugins.kubernetes.auth;
2+
3+
public class KubernetesAuthCertificate implements KubernetesAuth {
4+
private final String certificate;
5+
6+
private final String key;
7+
8+
private final String password;
9+
10+
11+
public KubernetesAuthCertificate(String certificate, String key, String password) {
12+
this.certificate = certificate;
13+
this.key = key;
14+
this.password = password;
15+
}
16+
17+
public KubernetesAuthCertificate(String certificate, String key) {
18+
this(certificate, key, null);
19+
}
20+
21+
public String getCertificate() {
22+
return certificate;
23+
}
24+
25+
public String getKey() {
26+
return key;
27+
}
28+
29+
public String getKeyPassword() {
30+
return password;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.jenkinsci.plugins.kubernetes.auth;
2+
3+
public class KubernetesAuthKubeconfig implements KubernetesAuth {
4+
private final String kubeconfig;
5+
6+
public KubernetesAuthKubeconfig(String kubeconfig) {
7+
this.kubeconfig = kubeconfig;
8+
}
9+
10+
public String getKubeconfig() {
11+
return kubeconfig;
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.jenkinsci.plugins.kubernetes.auth;
2+
3+
public class KubernetesAuthToken implements KubernetesAuth {
4+
private final String token;
5+
6+
public KubernetesAuthToken(String token) {
7+
this.token = token;
8+
}
9+
10+
public String getToken() {
11+
return token;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.jenkinsci.plugins.kubernetes.auth;
2+
3+
public class KubernetesAuthUsernamePassword implements KubernetesAuth {
4+
private final String username;
5+
6+
private final String password;
7+
8+
9+
public KubernetesAuthUsernamePassword(String username, String password) {
10+
11+
this.username = username;
12+
this.password = password;
13+
}
14+
15+
public String getUsername() {
16+
return username;
17+
}
18+
19+
public String getPassword() {
20+
return password;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.jenkinsci.plugins.kubernetes.credentials;
2+
3+
import edu.umd.cs.findbugs.annotations.NonNull;
4+
import hudson.Extension;
5+
import jenkins.authentication.tokens.api.AuthenticationTokenException;
6+
import jenkins.authentication.tokens.api.AuthenticationTokenSource;
7+
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
8+
import com.google.jenkins.plugins.credentials.oauth.GoogleOAuth2ScopeRequirement;
9+
import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthToken;
10+
11+
import java.util.Collection;
12+
import java.util.Collections;
13+
14+
/**
15+
* Converts {@link GoogleRobotCredentials} to {@link String} token.
16+
*/
17+
@Extension
18+
public class GoogleRobotCredentialsTokenSource extends AuthenticationTokenSource<KubernetesAuthToken, GoogleRobotCredentials> {
19+
public GoogleRobotCredentialsTokenSource() {
20+
super(KubernetesAuthToken.class, GoogleRobotCredentials.class);
21+
}
22+
23+
@NonNull
24+
@Override
25+
public KubernetesAuthToken convert(@NonNull GoogleRobotCredentials credential) throws AuthenticationTokenException {
26+
return new KubernetesAuthToken(credential.getAccessToken(new GoogleOAuth2ScopeRequirement() {
27+
@Override
28+
public Collection<String> getScopes() {
29+
return Collections.singleton("https://www.googleapis.com/auth/cloud-platform");
30+
}
31+
}).getPlainText());
32+
}
33+
}

0 commit comments

Comments
 (0)