Skip to content

Commit

Permalink
Add Bearer credentials type to the enum
Browse files Browse the repository at this point in the history
Add bearer type to the credentials type enum. Remove the constant from
AuthUtils.

Change-Id: I3300988f4bdaaf3c1a45b0f1a8c9e2e971b544ec
Reviewed-on: https://bellevue-ci.eng.vmware.com:8080/40165
Upgrade-Verified: jenkins <[email protected]>
Closures-Verified: jenkins <[email protected]>
PG-Verified: jenkins <[email protected]>
Bellevue-Verified: jenkins <[email protected]>
CS-Verified: jenkins <[email protected]>
Reviewed-by: Miroslav Shipkovenski <[email protected]>
  • Loading branch information
lazarin committed Jul 30, 2018
1 parent f00b340 commit 928ff2f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.vmware.admiral.adapter.pks.PKSConstants;
import com.vmware.admiral.common.ManagementUriParts;
import com.vmware.admiral.common.util.AssertUtil;
import com.vmware.admiral.common.util.AuthUtils;
import com.vmware.admiral.common.util.YamlMapper;
import com.vmware.admiral.compute.ContainerHostUtil;
import com.vmware.admiral.compute.content.kubernetes.KubernetesUtil;
Expand Down Expand Up @@ -87,7 +86,7 @@ private void constructKubeConfig(Operation op, ComputeState kubernetesHost,
}

kubeConfig = credentials.customProperties.get(PKSConstants.KUBE_CONFIG_PROP_NAME);
} else if (AuthUtils.BEARER_TOKEN_AUTH_TYPE.equals(credentials.type)) {
} else if (AuthCredentialsType.Bearer.toString().equals(credentials.type)) {
KubeConfig config = KubernetesUtil.constructKubeConfig(kubernetesHost.address,
EncryptionUtils.decrypt(credentials.privateKey));
kubeConfig = Utils.toJson(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.vmware.admiral.service.common.MultiTenantDocument;
import com.vmware.admiral.service.common.ServiceTaskCallback;
import com.vmware.photon.controller.model.resources.ComputeService.ComputeState;
import com.vmware.photon.controller.model.security.util.AuthCredentialsType;
import com.vmware.xenon.common.Operation;
import com.vmware.xenon.common.StatelessService;
import com.vmware.xenon.common.UriUtils;
Expand Down Expand Up @@ -241,7 +242,7 @@ private void createCredentials(Operation op, String link, String token, KubeConf
AuthCredentialsServiceState credentials = new AuthCredentialsServiceState();
credentials.documentSelfLink = link;
credentials.privateKey = token;
credentials.type = AuthUtils.BEARER_TOKEN_AUTH_TYPE;
credentials.type = AuthCredentialsType.Bearer.toString();
credentials.tenantLinks = tenantLinks;
credentials.customProperties = new HashMap<>(4);
credentials.customProperties.put(KUBE_CONFIG_PROP_NAME, Utils.toJson(kubeConfig));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.junit.Before;
import org.junit.Test;

import com.vmware.admiral.common.util.AuthUtils;
import com.vmware.admiral.compute.ContainerHostService.ContainerHostType;
import com.vmware.admiral.compute.container.ComputeBaseTest;
import com.vmware.photon.controller.model.resources.ComputeService;
Expand Down Expand Up @@ -55,8 +54,8 @@ public void setUp() throws Throwable {

@Test
public void testGetKubeConfigWithBearerToken() throws Throwable {
String authCredentialsLink = createCredentials(AuthUtils.BEARER_TOKEN_AUTH_TYPE,
true).documentSelfLink;
String authCredentialsLink = createCredentials(AuthCredentialsType.Bearer, true)
.documentSelfLink;
String hostLink = createCompute(authCredentialsLink, true, true).documentSelfLink;

URI serviceUri = UriUtils.buildUri(host, KubeConfigContentService.SELF_LINK,
Expand All @@ -72,8 +71,8 @@ public void testGetKubeConfigWithBearerToken() throws Throwable {
@Test
public void testGetKubeConfigWithCertificateAndKey() throws Throwable {
assertTrue(UUID.randomUUID().toString().matches("[-a-z0-9]+"));
String authCredentialsLink = createCredentials(AuthCredentialsType.PublicKey.toString(),
false).documentSelfLink;
String authCredentialsLink = createCredentials(AuthCredentialsType.PublicKey, false)
.documentSelfLink;
String hostLink = createCompute(authCredentialsLink, true, false).documentSelfLink;

URI serviceUri = UriUtils.buildUri(host, KubeConfigContentService.SELF_LINK,
Expand All @@ -87,8 +86,8 @@ public void testGetKubeConfigWithCertificateAndKey() throws Throwable {

@Test
public void testShouldFailWhenHostTypeNotKubernetes() throws Throwable {
String authCredentialsLink = createCredentials(AuthUtils.BEARER_TOKEN_AUTH_TYPE,
true).documentSelfLink;
String authCredentialsLink = createCredentials(AuthCredentialsType.Bearer, true)
.documentSelfLink;
String hostLink = createCompute(authCredentialsLink, false, true).documentSelfLink;
URI serviceUri = UriUtils.buildUri(host, KubeConfigContentService.SELF_LINK,
UriUtils.buildUriQuery("hostLink", hostLink));
Expand All @@ -102,8 +101,8 @@ public void testShouldFailWhenHostTypeNotKubernetes() throws Throwable {

@Test
public void testShouldFailWhenKubeConfigContentIsMissing() throws Throwable {
String authCredentialsLink = createCredentials(AuthUtils.BEARER_TOKEN_AUTH_TYPE,
false).documentSelfLink;
String authCredentialsLink = createCredentials(AuthCredentialsType.Bearer, false)
.documentSelfLink;
String hostLink = createCompute(authCredentialsLink, true, true).documentSelfLink;
URI serviceUri = UriUtils.buildUri(host, KubeConfigContentService.SELF_LINK,
UriUtils.buildUriQuery("hostLink", hostLink));
Expand All @@ -128,8 +127,8 @@ public void testShouldFailWhenHostLinkParamIsMissing() throws Throwable {

@Test
public void testShouldFailWhenHostAuthTypeNotSupported() throws Throwable {
String authCredentialsLink = createCredentials(AuthCredentialsType.Password.toString(),
false).documentSelfLink;
String authCredentialsLink = createCredentials(AuthCredentialsType.Password, false)
.documentSelfLink;
String hostLink = createCompute(authCredentialsLink, true, false).documentSelfLink;
URI serviceUri = UriUtils.buildUri(host, KubeConfigContentService.SELF_LINK,
UriUtils.buildUriQuery("hostLink", hostLink));
Expand All @@ -141,14 +140,14 @@ public void testShouldFailWhenHostAuthTypeNotSupported() throws Throwable {
}
}

private AuthCredentialsServiceState createCredentials(String type, boolean setKubeConfig)
throws Throwable {
private AuthCredentialsServiceState createCredentials(AuthCredentialsType type,
boolean setKubeConfig) throws Throwable {

AuthCredentialsServiceState credentials = new AuthCredentialsServiceState();
if (AuthUtils.BEARER_TOKEN_AUTH_TYPE.equals(type)) {
credentials.type = AuthUtils.BEARER_TOKEN_AUTH_TYPE;
if (AuthCredentialsType.Bearer == type) {
credentials.type = AuthCredentialsType.Bearer.toString();
credentials.publicKey = "token";
} else if (AuthCredentialsType.PublicKey.toString().equals(type)) {
} else if (AuthCredentialsType.PublicKey == type) {
credentials.type = AuthCredentialsType.PublicKey.toString();
credentials.publicKey = "certificate";
credentials.privateKey = "privateKey";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

public class AuthUtils {

public static final String BEARER_TOKEN_AUTH_TYPE = "Bearer";
public static final String AUTH_CREDENTIALS_NAME_PROP_NAME = "__authCredentialsName";

/*
Expand All @@ -59,7 +58,7 @@ public static String createAuthorizationHeader(AuthCredentialsServiceState authS
return null;
}

if (BEARER_TOKEN_AUTH_TYPE.equals(authState.type)) {
if (AuthCredentialsType.Bearer.toString().equals(authState.type)) {
String token = EncryptionUtils.decrypt(authState.privateKey);
return "Bearer " + token;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public enum AuthCredentialsType {
Public,
PublicKey,
Password,
PublicKeyCA
PublicKeyCA,
Bearer
}

0 comments on commit 928ff2f

Please sign in to comment.