Skip to content

Commit

Permalink
feat(#982): Uses OuthToken or username and password to authenticate r…
Browse files Browse the repository at this point in the history
…emote cluster (#985)

* adds username and password

* feat(#964, #982): Uses Auth paramters from configuration

* changed builder to use fluent pattern

* reverted changes for OpenshiftClient

* Removes unused import

* renames all newly introduced properties to prefix with cube.
  • Loading branch information
dipak-pawar authored Feb 16, 2018
1 parent bec086d commit c110875
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ public static String getPropertyOrEnvironmentVariable(String property) {
}

public static boolean getPropertyOrEnvironmentVariable(String property, boolean defaultValue) {
String result = getPropertyOrEnvironmentVariable(property, new Boolean(defaultValue).toString());
String result = getPropertyOrEnvironmentVariable(property, Boolean.toString(defaultValue));
return Boolean.parseBoolean(result);
}

public static int getPropertyOrEnvironmentVariable(String property, int defaultValue) {
String result = getPropertyOrEnvironmentVariable(property, new Integer(defaultValue).toString());
String result = getPropertyOrEnvironmentVariable(property, Integer.toString(defaultValue));
return Integer.parseInt(result);
}

public static long getPropertyOrEnvironmentVariable(String property, long defaultValue) {
String result = getPropertyOrEnvironmentVariable(property, new Long(defaultValue).toString());
String result = getPropertyOrEnvironmentVariable(property, Long.toString(defaultValue));
return Long.parseLong(result);
}

Expand Down
7 changes: 6 additions & 1 deletion docs/kubernetes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ You can configure Kubernetes by using any of the next configuration properties i
|===
| Options | Type | Env | Description
| kubernetes.master | URL | Any | The URL to the Kubernetes master
| kubernetes.domain | String | OSE | Domain to use for creating routes for services
| cube.username | String | Any | Username to log in server
| cube.password | String | Any | Password to log in server
| cube.auth.token | String | Any | Bearer token for authentication to the API server
| cube.api.version | String(v1) | Any | Version for API server
| cube.trust.certs | Boolean(true) | Any | Boolean to trust Certificate
| kubernetes.domain | String | OSE | Domain to use for creating routes for services
| docker.registry | String | Any | The docker registry
| namespace.use.current | Boolean (false)| Any | Don't generate a testing namespace use the current instead
| namespace.use.existing | String | Any | Don't generate a testing namespace use the specified one instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public interface Configuration {
String LOGS_COPY = "logs.copy";
String LOGS_PATH = "logs.path";

String USERNAME = "cube.username";
String PASSWORD = "cube.password";
String AUTH_TOKEN = "cube.auth.token";
String API_VERSION = "cube.api.version";
String TRUST_CERTS = "cube.trust.certs";

Long DEFAULT_WAIT_TIMEOUT = 5 * 60 * 1000L;
Long DEFAULT_WAIT_POLL_INTERVAL = 5 * 1000L;

Expand All @@ -80,6 +86,16 @@ public interface Configuration {

String getNamespace();

String getUsername();

String getPassword();

String getApiVersion();

String getToken();

boolean isTrustCerts();

boolean isNamespaceLazyCreateEnabled();

boolean isNamespaceCleanupEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
package org.arquillian.cube.kubernetes.impl;

import io.fabric8.kubernetes.api.builder.v3_1.TypedVisitor;
import io.fabric8.kubernetes.clnt.v3_1.ConfigBuilder;
import io.fabric8.kubernetes.clnt.v3_1.DefaultKubernetesClient;
import io.fabric8.kubernetes.clnt.v3_1.KubernetesClient;
import org.arquillian.cube.impl.util.Strings;
import org.arquillian.cube.kubernetes.api.Configuration;
import org.jboss.arquillian.core.api.InstanceProducer;
import org.jboss.arquillian.core.api.annotation.ApplicationScoped;
Expand All @@ -34,15 +36,31 @@ public class ClientCreator {
private InstanceProducer<KubernetesClient> producer;

public void createClient(@Observes Configuration config) {
if (config.getMasterUrl() != null) {
producer.set(new DefaultKubernetesClient(new ConfigBuilder()
.withMasterUrl(config.getMasterUrl().toString())
.withNamespace(config.getNamespace())
.build()));
} else {
producer.set(new DefaultKubernetesClient(new ConfigBuilder()
.withNamespace(config.getNamespace())
.build()));

final ConfigBuilder configBuilder = new ConfigBuilder()
.withNamespace(config.getNamespace())
.withApiVersion(config.getApiVersion())
.withTrustCerts(config.isTrustCerts())
.accept(new TypedVisitor<ConfigBuilder>() {
@Override
public void visit(ConfigBuilder b) {
b.withNoProxy(b.getNoProxy() == null ? new String[0] : b.getNoProxy());
}
});

if (Strings.isNotNullOrEmpty(config.getMasterUrl().toString())) {
configBuilder.withMasterUrl(config.getMasterUrl().toString());
}

if (Strings.isNotNullOrEmpty(config.getToken())) {
configBuilder.withOauthToken(config.getToken());
}

if (Strings.isNotNullOrEmpty(config.getUsername()) && Strings.isNotNullOrEmpty(config.getPassword())) {
configBuilder.withUsername(config.getUsername())
.withPassword(config.getPassword());
}

producer.set(new DefaultKubernetesClient(configBuilder.build()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.fabric8.kubernetes.clnt.v3_1.ConfigBuilder;
import io.fabric8.kubernetes.clnt.v3_1.utils.Utils;
import io.sundr.builder.annotations.Buildable;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -16,9 +15,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.arquillian.cube.impl.util.Strings;
import org.arquillian.cube.impl.util.SystemEnvironmentVariables;
import org.arquillian.cube.kubernetes.api.Configuration;
Expand All @@ -35,7 +32,7 @@ public class DefaultConfiguration implements Configuration {
private static final String ENV_VAR_REGEX = "env.([a-zA-Z0-9_]+)";
private static final Pattern ENV_VAR_PATTERN = Pattern.compile(ENV_VAR_REGEX);

public static final String ROOT = "/";
private static final String ROOT = "/";

private final String sessionId;
private final String namespace;
Expand Down Expand Up @@ -68,14 +65,21 @@ public class DefaultConfiguration implements Configuration {
private final String logPath;
private final String kubernetesDomain;
private final String dockerRegistry;
private final String username;
private final String password;
private final String apiVersion;
private final boolean trustCerts;

private String token;

public DefaultConfiguration(String sessionId, URL masterUrl, String namespace, Map<String, String> scriptEnvironmentVariables, URL environmentSetupScriptUrl,
URL environmentTeardownScriptUrl, URL environmentConfigUrl, List<URL> environmentDependencies,
boolean namespaceLazyCreateEnabled, boolean namespaceCleanupEnabled, long namespaceCleanupTimeout,
boolean namespaceCleanupConfirmationEnabled, boolean namespaceDestroyEnabled,
boolean namespaceDestroyConfirmationEnabled, long namespaceDestroyTimeout, boolean waitEnabled, long waitTimeout,
long waitPollInterval, List<String> waitForServiceList, boolean ansiLoggerEnabled, boolean environmentInitEnabled, boolean logCopyEnabled,
String logPath, String kubernetesDomain, String dockerRegistry) {
String logPath, String kubernetesDomain, String dockerRegistry, String token, String username, String password,
String apiVersion, boolean trustCerts) {
this.masterUrl = masterUrl;
this.scriptEnvironmentVariables = scriptEnvironmentVariables;
this.environmentSetupScriptUrl = environmentSetupScriptUrl;
Expand All @@ -101,6 +105,11 @@ public DefaultConfiguration(String sessionId, URL masterUrl, String namespace, M
this.logPath = logPath;
this.kubernetesDomain = kubernetesDomain;
this.dockerRegistry = dockerRegistry;
this.token = token;
this.username = username;
this.password = password;
this.apiVersion = apiVersion;
this.trustCerts = trustCerts;
}

public static DefaultConfiguration fromMap(Map<String, String> map) {
Expand Down Expand Up @@ -155,6 +164,11 @@ public static DefaultConfiguration fromMap(Map<String, String> map) {
.withAnsiLoggerEnabled(getBooleanProperty(ANSI_LOGGER_ENABLED, map, true))
.withKubernetesDomain(getStringProperty(DOMAIN, KUBERNETES_DOMAIN, map, null))
.withDockerRegistry(getDockerRegistry(map))
.withToken(getStringProperty(AUTH_TOKEN, map, null))
.withUsername(getStringProperty(USERNAME, map, null))
.withPassword(getStringProperty(PASSWORD, map, null))
.withApiVersion(getStringProperty(API_VERSION, map, "v1"))
.withTrustCerts(getBooleanProperty(TRUST_CERTS, map, true))
.build();
} catch (Throwable t) {
if (t instanceof RuntimeException) {
Expand Down Expand Up @@ -402,4 +416,35 @@ public String getKubernetesDomain() {
public String getDockerRegistry() {
return dockerRegistry;
}

public boolean hasBasicAuth() {
return Strings.isNotNullOrEmpty(username) && Strings.isNotNullOrEmpty(password);
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

@Override
public String getApiVersion() {
return apiVersion;
}

@Override
public String getToken() {
return token;
}

@Override
public boolean isTrustCerts() {
return trustCerts;
}

protected void setToken(String token) {
this.token = token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected static void configureProtocolHandlers(Map<String, String> conf) {
Set<String> handlers = new LinkedHashSet<>();
handlers.addAll(Strings.splitAndTrimAsList(System.getProperty(JAVA_PROTOCOL_HANDLER, ""), " "));
handlers.addAll(Strings.splitAndTrimAsList(
conf.containsKey(PROTOCOL_HANDLERS) ? conf.get(PROTOCOL_HANDLERS) : DEFAULT_MAVEN_PROTOCOL_HANDLER, " "));
conf.getOrDefault(PROTOCOL_HANDLERS, DEFAULT_MAVEN_PROTOCOL_HANDLER), " "));
System.setProperty(JAVA_PROTOCOL_HANDLER, Strings.join(handlers, " "));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public class HelloWorldIT {
@Named("hello-openshift-service")
@PortForward
@ArquillianResource
Service service;
private Service service;

@Named("hello-openshift-service")
@PortForward
@ArquillianResource
URL url;
private URL url;

@ArquillianResource
OpenShiftClient client;
private OpenShiftClient client;

@Test
public void client_should_not_be_null() throws IOException {
Expand Down
Loading

0 comments on commit c110875

Please sign in to comment.