Skip to content

Commit

Permalink
[k8s-support] - Review cert&key generation, free it from OCP specific…
Browse files Browse the repository at this point in the history
… dependencies/logic
  • Loading branch information
fabiobrz committed Jul 5, 2023
1 parent 7d22324 commit a660e86
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
*/
package org.jboss.intersmash.testsuite.provision.k8s;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

import io.hyperfoil.v1alpha2.Hyperfoil;
import io.hyperfoil.v1alpha2.HyperfoilBuilder;
import io.hyperfoil.v1alpha2.HyperfoilSpec;
import io.hyperfoil.v1alpha2.hyperfoilspec.Route;
import org.jboss.intersmash.testsuite.k8s.KubernetesTest;
import org.jboss.intersmash.testsuite.k8s.NamespaceCreationCapable;
import org.jboss.intersmash.tools.application.operator.HyperfoilOperatorApplication;
Expand All @@ -46,10 +44,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.hyperfoil.v1alpha2.Hyperfoil;
import io.hyperfoil.v1alpha2.HyperfoilBuilder;
import io.hyperfoil.v1alpha2.HyperfoilSpec;
import io.hyperfoil.v1alpha2.hyperfoilspec.Route;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@KubernetesTest
Expand Down Expand Up @@ -180,6 +179,7 @@ public void benchmark() throws ApiException, InterruptedException, IOException {
logger.debug("JSON: {}", JSON);
RunStatisticsWrapper runStatisticsWrapper = new RunStatisticsWrapper(JSON);
Assertions.assertTrue(runStatisticsWrapper.getPhaseStats().size() > 0);
// }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
*/
package org.jboss.intersmash.testsuite.provision.openshift;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import cz.xtf.core.config.OpenShiftConfig;
import cz.xtf.core.openshift.OpenShiftWaiters;
import cz.xtf.core.openshift.OpenShifts;
import cz.xtf.core.waiting.SimpleWaiter;
import cz.xtf.junit5.annotations.CleanBeforeAll;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import lombok.extern.slf4j.Slf4j;
import org.jboss.intersmash.testsuite.openshift.OpenShiftTest;
import org.jboss.intersmash.testsuite.openshift.ProjectCreationCapable;
import org.jboss.intersmash.tools.application.openshift.PostgreSQLImageOpenShiftApplication;
import org.jboss.intersmash.tools.application.operator.KeycloakRealmImportOperatorApplication;
import org.jboss.intersmash.tools.junit5.IntersmashExtension;
import org.jboss.intersmash.tools.provision.openshift.KeycloakRealmImportOpenShiftOperatorProvisioner;
import org.jboss.intersmash.tools.provision.openshift.OpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.PostgreSQLImageOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.operator.resources.OperatorGroup;
import org.jboss.intersmash.tools.util.tls.CertificatesUtils;
Expand All @@ -51,16 +55,12 @@
import org.keycloak.k8s.v2alpha1.keycloakspec.db.UsernameSecret;
import org.slf4j.event.Level;

import cz.xtf.core.config.OpenShiftConfig;
import cz.xtf.core.openshift.OpenShiftWaiters;
import cz.xtf.core.openshift.OpenShifts;
import cz.xtf.core.waiting.SimpleWaiter;
import cz.xtf.junit5.annotations.CleanBeforeAll;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import lombok.extern.slf4j.Slf4j;
import git add .java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Test the Keycloak Operator provisioning model and APIs
Expand Down Expand Up @@ -171,7 +171,7 @@ public void customResourcesCleanup() {
* <br> - https://github.com/keycloak/keycloak-operator/tree/master/deploy/examples/keycloak
*/
@Test
public void exampleSso() {
public void exampleSso() throws IOException {
name = "example-sso";

final Keycloak keycloak = new Keycloak();
Expand All @@ -184,10 +184,14 @@ public void exampleSso() {
spec.setIngress(ingress);
Hostname hostname = new Hostname();
hostname.setHostname(OpenShifts.master().generateHostname(name));

// create key, certificate and tls secret: Keycloak expects the secret to be created beforehand
String tlsSecretName = name + "-tls-secret";
CertificatesUtils.CertificateAndKey certificateAndKey = CertificatesUtils
.generateSelfSignedCertificateAndKey(hostname.getHostname().replaceFirst("[.].*$", ""), tlsSecretName);
OpenShiftProvisioner.createTlsSecret(OpenShifts.master().getNamespace(), tlsSecretName, certificateAndKey.key,
certificateAndKey.certificate);

// add TLS config to keycloak using the secret we just created
Http http = new Http();
http.setTlsSecret(certificateAndKey.tlsSecret.getMetadata().getName());
Expand Down Expand Up @@ -220,7 +224,7 @@ public void exampleSso() {
* <br> - https://github.com/keycloak/keycloak-operator/tree/master/deploy/examples/keycloak
*/
@Test
public void exampleSsoWithDatabase() {
public void exampleSsoWithDatabase() throws IOException {
POSTGRESQL_IMAGE_PROVISIONER.configure();
try {
POSTGRESQL_IMAGE_PROVISIONER.preDeploy();
Expand All @@ -239,11 +243,15 @@ public void exampleSsoWithDatabase() {
spec.setIngress(ingress);
Hostname hostname = new Hostname();
hostname.setHostname(OpenShifts.master().generateHostname(name));

// create key, certificate and tls secret: Keycloak expects the secret to be created beforehand
String tlsSecretName = name + "-tls-secret";
CertificatesUtils.CertificateAndKey certificateAndKey = CertificatesUtils
.generateSelfSignedCertificateAndKey(hostname.getHostname().replaceFirst("[.].*$", ""),
tlsSecretName);
OpenShiftProvisioner.createTlsSecret(OpenShifts.master().getNamespace(), tlsSecretName,
certificateAndKey.key, certificateAndKey.certificate);

// add TLS config to keycloak using the secret we just created
Http http = new Http();
http.setTlsSecret(certificateAndKey.tlsSecret.getMetadata().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Slf4j
public final class KubernetesConfig {
public static final String KUBERNETES_URL = "intersmash.kubernetes.url";
public static final String KUBERNETES_HOSTNAME = "intersmash.kubernetes.hostname";
public static final String KUBERNETES_TOKEN = "intersmash.kubernetes.token";
public static final String KUBERNETES_VERSION = "intersmash.kubernetes.version";
public static final String KUBERNETES_NAMESPACE = "intersmash.kubernetes.namespace";
Expand Down Expand Up @@ -44,6 +45,10 @@ public static String url() {
return XTFConfig.get(KUBERNETES_URL);
}

public static String getKubernetesHostname() {
return XTFConfig.get(KUBERNETES_HOSTNAME, "localhost");
}

private static final String CLEAN_KUBERNETES = "intersmash.junit.clean_namespace";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.jboss.intersmash.tools.k8s.KubernetesConfig;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
Expand Down Expand Up @@ -397,4 +398,9 @@ private boolean isNamespaceClean() {
return isClean;
}

public String generateHostname() {
log.info("Kubernetes generateHostname returns: " + KubernetesConfig.getKubernetesHostname());
return KubernetesConfig.getKubernetesHostname();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
*/
package org.jboss.intersmash.tools.provision.k8s;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import org.jboss.intersmash.tools.application.Application;
import org.jboss.intersmash.tools.application.openshift.HasConfigMaps;
Expand All @@ -27,13 +33,32 @@
import org.jboss.intersmash.tools.provision.openshift.HasPods;
import org.jboss.intersmash.tools.provision.openshift.Scalable;

import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder;

/**
* Provisioner that is supposed to deploy an application on Kubernetes.
*/
public interface KubernetesProvisioner<T extends Application> extends Provisioner<T>, Scalable, HasPods {

Kubernetes kubernetes = Kuberneteses.master();

static Secret createTlsSecret(final String namespace, final String secretName, final Path key, final Path certificate)
throws IOException {
Map<String, String> data = new HashMap<>();
String keyDerData = Files.readString(key);
String crtDerData = Files.readString(certificate);
data.put("tls.key", Base64.getEncoder().encodeToString(keyDerData.getBytes()));
data.put("tls.crt", Base64.getEncoder().encodeToString(crtDerData.getBytes()));
final Secret secret = new SecretBuilder()
.withNewMetadata().withName(secretName).endMetadata()
.withType("kubernetes.io/tls")
.withImmutable(false)
.addToData(data)
.build();
return kubernetes.secrets().inNamespace(namespace).createOrReplace(secret);
}

@Override
default void preDeploy() {
// create secrets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
*/
package org.jboss.intersmash.tools.provision.openshift;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import org.jboss.intersmash.tools.application.Application;
import org.jboss.intersmash.tools.application.openshift.HasConfigMaps;
Expand All @@ -25,6 +31,8 @@

import cz.xtf.core.openshift.OpenShift;
import cz.xtf.core.openshift.OpenShifts;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder;

/**
* Provisioner that is supposed to deploy an application on OpenShift.
Expand All @@ -35,6 +43,22 @@ public interface OpenShiftProvisioner<T extends Application> extends Provisioner

OpenShift openShift = OpenShifts.master();

static Secret createTlsSecret(final String namespace, final String secretName, final Path key, final Path certificate)
throws IOException {
Map<String, String> data = new HashMap<>();
String keyDerData = Files.readString(key);
String crtDerData = Files.readString(certificate);
data.put("tls.key", Base64.getEncoder().encodeToString(keyDerData.getBytes()));
data.put("tls.crt", Base64.getEncoder().encodeToString(crtDerData.getBytes()));
final Secret secret = new SecretBuilder()
.withNewMetadata().withName(secretName).endMetadata()
.withType("kubernetes.io/tls")
.withImmutable(false)
.addToData(data)
.build();
return openShift.secrets().inNamespace(namespace).createOrReplace(secret);
}

@Override
default void preDeploy() {
// create secrets
Expand Down
Loading

0 comments on commit a660e86

Please sign in to comment.