|
| 1 | +/* |
| 2 | + * Copyright Strimzi authors. |
| 3 | + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). |
| 4 | + */ |
| 5 | +package io.strimzi.systemtest.kafkaclients; |
| 6 | + |
| 7 | +import io.fabric8.kubernetes.api.model.EnvVar; |
| 8 | +import io.fabric8.kubernetes.api.model.EnvVarBuilder; |
| 9 | +import io.skodjob.kubetest4j.resources.KubeResourceManager; |
| 10 | +import io.strimzi.api.kafka.model.kafka.KafkaResources; |
| 11 | +import io.strimzi.operator.common.Util; |
| 12 | +import io.strimzi.systemtest.keycloak.KeycloakInstance; |
| 13 | +import io.strimzi.testclients.configuration.Authentication; |
| 14 | +import io.strimzi.testclients.configuration.AuthenticationBuilder; |
| 15 | +import org.apache.kafka.common.security.auth.SecurityProtocol; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +public class ClientsAuthentication { |
| 21 | + public static Authentication configureTlsScramSha(String namespaceName, String userName, String clusterName) { |
| 22 | + return configureScramSha(namespaceName, userName, SecurityProtocol.SASL_SSL) |
| 23 | + .withNewSsl() |
| 24 | + .withSslTruststoreCertificate(KafkaResources.clusterCaCertificateSecretName(clusterName)) |
| 25 | + .endSsl() |
| 26 | + .build(); |
| 27 | + } |
| 28 | + |
| 29 | + public static Authentication configurePlainScramSha(String namespaceName, String userName) { |
| 30 | + return configureScramSha(namespaceName, userName, SecurityProtocol.SASL_PLAINTEXT).build(); |
| 31 | + } |
| 32 | + |
| 33 | + public static AuthenticationBuilder configureScramSha(String namespaceName, String userName, SecurityProtocol securityProtocol) { |
| 34 | + final String saslJaasConfigEncrypted = KubeResourceManager.get().kubeClient().getClient().secrets().inNamespace(namespaceName).withName(userName).get().getData().get("sasl.jaas.config"); |
| 35 | + final String saslJaasConfigDecrypted = Util.decodeFromBase64(saslJaasConfigEncrypted); |
| 36 | + |
| 37 | + return new AuthenticationBuilder() |
| 38 | + .withNewSasl() |
| 39 | + .withSaslJaasConfig(saslJaasConfigDecrypted) |
| 40 | + .withSaslMechanism("SCRAM-SHA-512") |
| 41 | + .endSasl() |
| 42 | + .withSecurityProtocol(securityProtocol.toString()); |
| 43 | + } |
| 44 | + |
| 45 | + public static Authentication configureTls(String clusterName, String userName) { |
| 46 | + return configureTls(KafkaResources.clusterCaCertificateSecretName(clusterName), userName, userName); |
| 47 | + } |
| 48 | + |
| 49 | + public static Authentication configureTlsCustomCerts(String caCertificateSecretName, String keystoreSecretName) { |
| 50 | + return configureTls(caCertificateSecretName, keystoreSecretName, keystoreSecretName); |
| 51 | + } |
| 52 | + |
| 53 | + private static Authentication configureTls(String caCertificateSecretName, String keystoreKeySecretName, String keystoreCertificateChainSecretName) { |
| 54 | + return new AuthenticationBuilder() |
| 55 | + .withNewSsl() |
| 56 | + .withSslTruststoreCertificate(caCertificateSecretName) |
| 57 | + .withSslKeystoreKey(keystoreKeySecretName) |
| 58 | + .withSslKeystoreCertificateChain(keystoreCertificateChainSecretName) |
| 59 | + .endSsl() |
| 60 | + .withNewSasl() |
| 61 | + .withSaslMechanism("GSSAPI") |
| 62 | + .endSasl() |
| 63 | + .withSecurityProtocol(SecurityProtocol.SSL.toString()) |
| 64 | + .build(); |
| 65 | + } |
| 66 | + |
| 67 | + public static Authentication configureTlsOAuth(String clusterName, String oauthClientId, String oauthClientSecret, String oauthTokenEndpointUri) { |
| 68 | + EnvVar oauthSslEndpointEnvVar = new EnvVarBuilder() |
| 69 | + .withName("OAUTH_SSL_ENDPOINT_IDENTIFICATION_ALGORITHM") |
| 70 | + .withValue("") |
| 71 | + .build(); |
| 72 | + |
| 73 | + return configureOAuth(oauthClientId, oauthClientSecret, oauthTokenEndpointUri, List.of(oauthSslEndpointEnvVar)) |
| 74 | + .withNewSsl() |
| 75 | + .withSslTruststoreCertificate(KafkaResources.clusterCaCertificateSecretName(clusterName)) |
| 76 | + .withSslKeystoreKey(oauthClientId) |
| 77 | + .withSslKeystoreCertificateChain(oauthClientId) |
| 78 | + .endSsl() |
| 79 | + .build(); |
| 80 | + } |
| 81 | + |
| 82 | + public static Authentication configureOAuthPlain(String oauthClientId, String oauthClientSecret, String oauthTokenEndpointUri) { |
| 83 | + return configureOAuth(oauthClientId, oauthClientSecret, oauthTokenEndpointUri, null).build(); |
| 84 | + } |
| 85 | + |
| 86 | + private static AuthenticationBuilder configureOAuth(String oauthClientId, String oauthClientSecret, String oauthTokenEndpointUri, List<EnvVar> additionalEnvVars) { |
| 87 | + List<EnvVar> envVars = new ArrayList<>(List.of( |
| 88 | + new EnvVarBuilder() |
| 89 | + .withName("OAUTH_SSL_TRUSTSTORE_CERTIFICATES") |
| 90 | + .withNewValueFrom() |
| 91 | + .withNewSecretKeyRef() |
| 92 | + .withName(KeycloakInstance.KEYCLOAK_SECRET_NAME) |
| 93 | + .withKey(KeycloakInstance.KEYCLOAK_SECRET_CERT) |
| 94 | + .endSecretKeyRef() |
| 95 | + .endValueFrom() |
| 96 | + .build(), |
| 97 | + new EnvVarBuilder() |
| 98 | + .withName("OAUTH_SSL_TRUSTSTORE_TYPE") |
| 99 | + .withValue("PEM") |
| 100 | + .build(), |
| 101 | + new EnvVarBuilder() |
| 102 | + .withName("OAUTH_CLIENT_SECRET") |
| 103 | + .editOrNewValueFrom() |
| 104 | + .withNewSecretKeyRef() |
| 105 | + .withName(oauthClientSecret) |
| 106 | + .withKey("clientSecret") |
| 107 | + .endSecretKeyRef() |
| 108 | + .endValueFrom() |
| 109 | + .build() |
| 110 | + )); |
| 111 | + |
| 112 | + if (additionalEnvVars != null) { |
| 113 | + envVars.addAll(additionalEnvVars); |
| 114 | + } |
| 115 | + |
| 116 | + return new AuthenticationBuilder() |
| 117 | + .withNewOauth() |
| 118 | + .withOauthClientId(oauthClientId) |
| 119 | + .withOauthTokenEndpointUri(oauthTokenEndpointUri) |
| 120 | + .withAdditionalOAuthEnvVars(envVars) |
| 121 | + .endOauth(); |
| 122 | + } |
| 123 | +} |
0 commit comments