Skip to content

Commit

Permalink
Add support for Azure authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Feb 24, 2018
1 parent 77b93f1 commit d775b08
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions util/src/main/java/io/kubernetes/client/util/KubeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
package io.kubernetes.client.util;

import io.kubernetes.client.util.authenticators.Authenticator;
import io.kubernetes.client.util.authenticators.AzureActiveDirectoryAuthenticator;
import io.kubernetes.client.util.authenticators.GCPAuthenticator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
Expand Down Expand Up @@ -58,6 +60,11 @@ public static void registerAuthenticator(Authenticator auth) {
}
}

static {
registerAuthenticator(new GCPAuthenticator());
registerAuthenticator(new AzureActiveDirectoryAuthenticator());
}

/** Load a Kubernetes config from the default location */
public static KubeConfig loadDefaultKubeConfig() throws FileNotFoundException {
File config = new File(new File(System.getenv(ENV_HOME), KUBEDIR), KUBECONFIG);
Expand Down Expand Up @@ -182,6 +189,8 @@ public String getAccessToken() {
// TODO persist things here.
}
return auth.getToken(authConfig);
} else {
log.error("Unknown auth provider: " + name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client.util.authenticators;

import io.kubernetes.client.util.KubeConfig;
import java.util.Date;
import java.util.Map;

/**
* The Authenticator interface represents a plugin that can handle a specific type of authentication
* information (e.g. 'azure')
*/
public class AzureActiveDirectoryAuthenticator implements Authenticator {
static {
KubeConfig.registerAuthenticator(new AzureActiveDirectoryAuthenticator());
}

private static final String ACCESS_TOKEN = "access-token";
private static final String EXPIRES_ON = "expires-on";

@Override
public String getName() {
return "azure";
}

@Override
public String getToken(Map<String, Object> config) {
return (String) config.get(ACCESS_TOKEN);
}

@Override
public boolean isExpired(Map<String, Object> config) {
String expiresOn = (String) config.get(EXPIRES_ON);
Date expiry = new Date(Long.parseLong(expiresOn) * 1000);
if (expiry != null && expiry.compareTo(new Date()) <= 0) {
return true;
}
return false;
}

@Override
public Map<String, Object> refresh(Map<String, Object> config) {
throw new RuntimeException("Unimplemented");
}
}

0 comments on commit d775b08

Please sign in to comment.