Skip to content

Commit d775b08

Browse files
committed
Add support for Azure authentication.
1 parent 77b93f1 commit d775b08

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

util/src/main/java/io/kubernetes/client/util/KubeConfig.java

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
package io.kubernetes.client.util;
1414

1515
import io.kubernetes.client.util.authenticators.Authenticator;
16+
import io.kubernetes.client.util.authenticators.AzureActiveDirectoryAuthenticator;
17+
import io.kubernetes.client.util.authenticators.GCPAuthenticator;
1618
import java.io.File;
1719
import java.io.FileNotFoundException;
1820
import java.io.FileReader;
@@ -58,6 +60,11 @@ public static void registerAuthenticator(Authenticator auth) {
5860
}
5961
}
6062

63+
static {
64+
registerAuthenticator(new GCPAuthenticator());
65+
registerAuthenticator(new AzureActiveDirectoryAuthenticator());
66+
}
67+
6168
/** Load a Kubernetes config from the default location */
6269
public static KubeConfig loadDefaultKubeConfig() throws FileNotFoundException {
6370
File config = new File(new File(System.getenv(ENV_HOME), KUBEDIR), KUBECONFIG);
@@ -182,6 +189,8 @@ public String getAccessToken() {
182189
// TODO persist things here.
183190
}
184191
return auth.getToken(authConfig);
192+
} else {
193+
log.error("Unknown auth provider: " + name);
185194
}
186195
}
187196
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
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+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.util.authenticators;
14+
15+
import io.kubernetes.client.util.KubeConfig;
16+
import java.util.Date;
17+
import java.util.Map;
18+
19+
/**
20+
* The Authenticator interface represents a plugin that can handle a specific type of authentication
21+
* information (e.g. 'azure')
22+
*/
23+
public class AzureActiveDirectoryAuthenticator implements Authenticator {
24+
static {
25+
KubeConfig.registerAuthenticator(new AzureActiveDirectoryAuthenticator());
26+
}
27+
28+
private static final String ACCESS_TOKEN = "access-token";
29+
private static final String EXPIRES_ON = "expires-on";
30+
31+
@Override
32+
public String getName() {
33+
return "azure";
34+
}
35+
36+
@Override
37+
public String getToken(Map<String, Object> config) {
38+
return (String) config.get(ACCESS_TOKEN);
39+
}
40+
41+
@Override
42+
public boolean isExpired(Map<String, Object> config) {
43+
String expiresOn = (String) config.get(EXPIRES_ON);
44+
Date expiry = new Date(Long.parseLong(expiresOn) * 1000);
45+
if (expiry != null && expiry.compareTo(new Date()) <= 0) {
46+
return true;
47+
}
48+
return false;
49+
}
50+
51+
@Override
52+
public Map<String, Object> refresh(Map<String, Object> config) {
53+
throw new RuntimeException("Unimplemented");
54+
}
55+
}

0 commit comments

Comments
 (0)