Skip to content

Commit e98d42d

Browse files
authored
Merge pull request #31 from brendandburns/auth
Fix up auth a little bit.
2 parents 2d418a8 + 4b6b093 commit e98d42d

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

examples/src/main/java/io/kubernetes/client/examples/Example.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,13 @@
2828
* Requires kubectl proxy running
2929
*
3030
* Easiest way to run this:
31-
* mvn exec:java -Dex.mainClass="io.kubernetes.client.examples.Example"
31+
* mvn exec:java -Dexec.mainClass="io.kubernetes.client.examples.Example"
3232
*
3333
* From inside $REPO_DIR/kubernetes
3434
*/
3535
public class Example {
3636
public static void main(String[] args) throws IOException, ApiException{
37-
ApiClient client;
38-
if (args.length == 0) {
39-
client = new ApiClient();
40-
client.setBasePath("http://localhost:8001");
41-
} else {
42-
client = Config.fromConfig(args[0]);
43-
}
37+
ApiClient client = Config.defaultClient();
4438
Configuration.setDefaultApiClient(client);
4539

4640
CoreV1Api api = new CoreV1Api();

kubernetes/src/main/java/io/kubernetes/client/ApiClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ public ApiClient() {
162162
// Setup authentications (key: authentication name, value: authentication).
163163
authentications = new HashMap<String, Authentication>();
164164
authentications.put("BearerToken", new ApiKeyAuth("header", "authorization"));
165+
authentications.put("BasicAuth", new HttpBasicAuth());
166+
165167
// Prevent the authentications from being modified.
166168
authentications = Collections.unmodifiableMap(authentications);
167169
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ public static ApiClient fromConfig(Reader input) {
128128

129129
String token = config.getAccessToken();
130130
if (token != null) {
131-
client.setAccessToken(token);
131+
// This is kind of a hack, except not, because I don't think we actually
132+
// want to use oauth here.
133+
client.setApiKey("Bearer " + token);
132134
}
133135

134136
String username = config.getUsername();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.FileReader;
1818
import java.io.Reader;
1919
import java.util.ArrayList;
20+
import java.util.Date;
2021
import java.util.Map;
2122

2223
import org.yaml.snakeyaml.Yaml;
@@ -154,9 +155,19 @@ public String getAccessToken() {
154155
Map<String, Object> authProviderMap = (Map<String, Object>) authProvider;
155156
Map<String, Object> authConfig = (Map<String, Object>) authProviderMap.get("config");
156157
if (authConfig != null) {
158+
Date expiry = (Date) authConfig.get("expiry");
159+
if (expiry != null) {
160+
if (expiry.compareTo(new Date()) <= 0) {
161+
// TODO: Generate new token here...
162+
throw new IllegalStateException("Token is expired!");
163+
}
164+
}
157165
return (String) authConfig.get("access-token");
158166
}
159167
}
168+
if (currentUser.containsKey("token")) {
169+
return (String) currentUser.get("token");
170+
}
160171
return null;
161172
}
162173

0 commit comments

Comments
 (0)