Skip to content

Commit

Permalink
feat(arquillian#963, arquillian#982): Uses Auth paramters from config…
Browse files Browse the repository at this point in the history
…uration
  • Loading branch information
dipak-pawar committed Feb 16, 2018
1 parent 89578e9 commit 4b616df
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 78 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
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 = "name";
String PASSWORD = "password";
String AUTH_TOKEN = "authToken";
String API_VERSION = "apiVersion";
String TRUST_CERTS = "trustCerts";

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,34 @@ public class ClientCreator {
private InstanceProducer<KubernetesClient> producer;

public void createClient(@Observes Configuration config) {
if (config.getMasterUrl() != null) {

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()
final ConfigBuilder configBuilder = new ConfigBuilder()
.withMasterUrl(config.getMasterUrl().toString())
.withNamespace(config.getNamespace())
.build()));
.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.getToken())) {
configBuilder.withOauthToken(config.getToken());
}

if (Strings.isNotNullOrEmpty(config.getUsername()) && Strings.isNotNullOrEmpty(config.getPassword())) {
configBuilder.withUsername(config.getUsername());
configBuilder.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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static org.arquillian.cube.impl.util.ConfigUtil.getIntProperty;
import static org.arquillian.cube.impl.util.ConfigUtil.getLongProperty;
import static org.arquillian.cube.impl.util.ConfigUtil.getStringProperty;
import static org.arquillian.cube.openshift.impl.utils.Strings.isNotNullOrEmpty;
import static org.arquillian.cube.openshift.impl.utils.Strings.isNullOrEmpty;

@Buildable(builderPackage = "io.fabric8.kubernetes.api.builder.v3_1", generateBuilderPackage = false, editableEnabled = false, refs = {
Expand All @@ -46,16 +45,11 @@ public class CubeOpenShiftConfiguration extends DefaultConfiguration implements
private static final String ROUTER_HOST = "routerHost";
private static final String OPENSHIFT_ROUTER_HTTP_PORT = "openshiftRouterHttpPort";
private static final String OPENSHIFT_ROUTER_HTTPS_PORT = "openshiftRouterHttpsPort";
private static final String AUTH_TOKEN = "authToken";
private static final String ROUTER_SNI_PORT = "routerSniPort";
private static final String TEMPLATE_URL = "templateUrl";
private static final String TEMPLATE_LABELS = "templateLabels";
private static final String TEMPLATE_PARAMETERS = "templateParameters";
private static final String TEMPLATE_PROCESS = "templateProcess";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String API_VERSION = "apiVersion";
private static final String TRUST_CERTS = "trustCerts";
private static final String STARTUP_TIMEOUT = "stratupTimeout";
private static final String HTTP_CLIENT_TIMEOUT = "httpClientTimeout";

Expand All @@ -70,16 +64,11 @@ public class CubeOpenShiftConfiguration extends DefaultConfiguration implements
private final int openshiftRouterHttpPort;
private final int openshiftRouterHttpsPort;
private final boolean enableImageStreamDetection;
private String token;
private final int routerSniPort;
private final String templateURL;
private final String templateLabels;
private final String templateParameters;
private final boolean templateProcess;
private final String username;
private final String password;
private final String apiVersion;
private final boolean trustCerts;
private final long startupTimeout;
private final long httpClientTimeout;

Expand All @@ -101,7 +90,7 @@ public CubeOpenShiftConfiguration(String sessionId, URL masterUrl, String namesp
environmentConfigUrl, environmentDependencies, namespaceLazyCreateEnabled, namespaceCleanupEnabled,
namespaceCleanupTimeout, namespaceCleanupConfirmationEnabled, namespaceDestroyEnabled,
namespaceDestroyConfirmationEnabled, namespaceDestroyTimeout, waitEnabled, waitTimeout, waitPollInterval,
waitForServiceList, ansiLoggerEnabled, environmentInitEnabled, logCopyEnabled, logPath, kubernetesDomain, dockerRegistry);
waitForServiceList, ansiLoggerEnabled, environmentInitEnabled, logCopyEnabled, logPath, kubernetesDomain, dockerRegistry, token, username, password, apiVersion, trustCerts);
this.keepAliveGitServer = keepAliveGitServer;
this.definitions = definitions;
this.definitionsFile = definitionsFile;
Expand All @@ -112,16 +101,11 @@ public CubeOpenShiftConfiguration(String sessionId, URL masterUrl, String namesp
this.openshiftRouterHttpPort = openshiftRouterHttpPort;
this.openshiftRouterHttpsPort = openshiftRouterHttpsPort;
this.enableImageStreamDetection = enableImageStreamDetection;
this.token = token;
this.routerSniPort = routerSniPort;
this.templateLabels = templateLabels;
this.templateParameters = templateParameters;
this.templateURL = templateURL;
this.templateProcess = templateProcess;
this.username = username;
this.password = password;
this.apiVersion = apiVersion;
this.trustCerts = trustCerts;
this.startupTimeout = startupTimeout;
this.httpClientTimeout = httpClientTimeout;
}
Expand Down Expand Up @@ -204,8 +188,8 @@ public static CubeOpenShiftConfiguration fromMap(Map<String, String> map) {
.withTemplateLabels(getStringProperty(TEMPLATE_LABELS, "openshift.template.labels", map, null))
.withTemplateParameters(getStringProperty(TEMPLATE_PARAMETERS, "openshift.template.parameters", map, null))
.withTemplateProcess(getBooleanProperty(TEMPLATE_PROCESS, "openshift.template.process", map, true))
.withUsername(getStringProperty(USERNAME, "openshift.username", map, "guest"))
.withPassword(getStringProperty(PASSWORD, "openshift.password", map, "guest"))
.withUsername(getStringProperty(USERNAME, "openshift.username", map, null))
.withPassword(getStringProperty(PASSWORD, "openshift.password", map, null))
.withApiVersion(getStringProperty(API_VERSION, "kubernetes.api.version", map, "v1"))
.withTrustCerts(getBooleanProperty(TRUST_CERTS, "kubernetes.trust.certs", map, true))
.withStartupTimeout(getLongProperty(STARTUP_TIMEOUT, "arquillian.startup.timeout", map, 600L))
Expand Down Expand Up @@ -285,11 +269,12 @@ public int getRouterSniPort() {
@Override
public String getToken() {

if ((token == null || token.isEmpty()) && (client != null)) {
token = client.getClientExt().getConfiguration().getOauthToken();
if ((super.getToken() == null || super.getToken().isEmpty()) && (client != null)) {
String token = client.getClientExt().getConfiguration().getOauthToken();
setToken(token);
}

return token;
return super.getToken();
}

public Properties getProperties() {
Expand All @@ -298,7 +283,7 @@ public Properties getProperties() {
return properties;
}

protected void apply(Properties properties) {
private void apply(Properties properties) {
// namespace
properties.put("kubernetes.namespace", this.getNamespace());
properties.put("namespace", this.getNamespace());
Expand All @@ -307,16 +292,6 @@ protected void apply(Properties properties) {
properties.put("kubernetes.api.version", getApiVersion());
}

public void validate() {

if (isNullOrEmpty(this.getMasterUrl().toString()))
throw new IllegalArgumentException("NULL master URL");

if ((isNullOrEmpty(username) || isNullOrEmpty(password)) && isNullOrEmpty(token)) {
throw new IllegalArgumentException("Missing OpenShift authentification -- username/password or token!");
}
}

public String getTemplateURL() {
return templateURL;
}
Expand Down Expand Up @@ -349,22 +324,6 @@ public String getKubernetesMaster() {
return this.getMasterUrl().toString();
}

public boolean hasOpenshiftBasicAuth() {
return isNotNullOrEmpty(username) && isNotNullOrEmpty(password);
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public boolean isTrustCerts() {
return trustCerts;
}

public long getStartupTimeout() {
return startupTimeout;
}
Expand All @@ -376,8 +335,4 @@ public long getHttpClientTimeout() {
public OpenShiftClient getClient() {
return client;
}

public String getApiVersion() {
return apiVersion;
}
}
Loading

0 comments on commit 4b616df

Please sign in to comment.