Search in sources :

Example 11 with KafkaUserBuilder

use of io.strimzi.api.kafka.model.KafkaUserBuilder in project strimzi-kafka-operator by strimzi.

the class KafkaUserModelTest method testGenerateSecretUseDesiredPasswordWhenSpecified.

@Test
public void testGenerateSecretUseDesiredPasswordWhenSpecified() {
    KafkaUser user = new KafkaUserBuilder(scramShaUser).editSpec().withNewKafkaUserScramSha512ClientAuthentication().withNewPassword().withNewValueFrom().withNewSecretKeyRef("my-password", "my-secret", false).endValueFrom().endPassword().endKafkaUserScramSha512ClientAuthentication().endSpec().build();
    Secret desiredPasswordSecret = new SecretBuilder().withNewMetadata().withName("my-secret").endMetadata().addToData("my-password", DESIRED_BASE64_PASSWORD).build();
    KafkaUserModel model = KafkaUserModel.fromCrd(user, UserOperatorConfig.DEFAULT_SECRET_PREFIX, UserOperatorConfig.DEFAULT_STRIMZI_ACLS_ADMIN_API_SUPPORTED);
    model.maybeGeneratePassword(Reconciliation.DUMMY_RECONCILIATION, passwordGenerator, null, desiredPasswordSecret);
    Secret generatedSecret = model.generateSecret();
    assertThat(model.getScramSha512Password(), is(DESIRED_PASSWORD));
    assertThat(generatedSecret.getMetadata().getName(), is(ResourceUtils.NAME));
    assertThat(generatedSecret.getMetadata().getNamespace(), is(ResourceUtils.NAMESPACE));
    assertThat(generatedSecret.getMetadata().getLabels(), is(Labels.fromMap(ResourceUtils.LABELS).withStrimziKind(KafkaUser.RESOURCE_KIND).withKubernetesName(KafkaUserModel.KAFKA_USER_OPERATOR_NAME).withKubernetesInstance(ResourceUtils.NAME).withKubernetesPartOf(ResourceUtils.NAME).withKubernetesManagedBy(KafkaUserModel.KAFKA_USER_OPERATOR_NAME).toMap()));
    assertThat(generatedSecret.getData().keySet(), is(new HashSet<>(Arrays.asList(KafkaUserModel.KEY_PASSWORD, KafkaUserModel.KEY_SASL_JAAS_CONFIG))));
    assertThat(new String(Base64.getDecoder().decode(generatedSecret.getData().get(KafkaUserModel.KEY_PASSWORD))), is(DESIRED_PASSWORD));
    assertThat(new String(Base64.getDecoder().decode(generatedSecret.getData().get(KafkaUserModel.KEY_SASL_JAAS_CONFIG))), is("org.apache.kafka.common.security.scram.ScramLoginModule required username=\"" + ResourceUtils.NAME + "\" password=\"" + DESIRED_PASSWORD + "\";"));
    // Check owner reference
    checkOwnerReference(model.createOwnerReference(), generatedSecret);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) KafkaUserBuilder(io.strimzi.api.kafka.model.KafkaUserBuilder) KafkaUser(io.strimzi.api.kafka.model.KafkaUser) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 12 with KafkaUserBuilder

use of io.strimzi.api.kafka.model.KafkaUserBuilder in project strimzi-kafka-operator by strimzi.

the class KafkaUserModelTest method testFromCrdScramShaUserWith65CharSaslUsernameValid.

@Test
public void testFromCrdScramShaUserWith65CharSaslUsernameValid() {
    // 65 characters => should work with SCRAM-SHA-512
    KafkaUser tooLong = new KafkaUserBuilder(scramShaUser).editMetadata().withName("User-123456789012345678901234567890123456789012345678901234567890").endMetadata().build();
    KafkaUserModel.fromCrd(tooLong, UserOperatorConfig.DEFAULT_SECRET_PREFIX, UserOperatorConfig.DEFAULT_STRIMZI_ACLS_ADMIN_API_SUPPORTED);
}
Also used : KafkaUserBuilder(io.strimzi.api.kafka.model.KafkaUserBuilder) KafkaUser(io.strimzi.api.kafka.model.KafkaUser) Test(org.junit.jupiter.api.Test)

Example 13 with KafkaUserBuilder

use of io.strimzi.api.kafka.model.KafkaUserBuilder in project strimzi-kafka-operator by strimzi.

the class KafkaUserModelTest method testGenerateSecretUseDesiredPasswordMissingKey.

@Test
public void testGenerateSecretUseDesiredPasswordMissingKey() {
    KafkaUser user = new KafkaUserBuilder(scramShaUser).editSpec().withNewKafkaUserScramSha512ClientAuthentication().withNewPassword().withNewValueFrom().withNewSecretKeyRef("my-password", "my-secret", false).endValueFrom().endPassword().endKafkaUserScramSha512ClientAuthentication().endSpec().build();
    Secret desiredPasswordSecret = new SecretBuilder().withNewMetadata().withName("my-secret").endMetadata().addToData("my-other-password", DESIRED_BASE64_PASSWORD).build();
    KafkaUserModel model = KafkaUserModel.fromCrd(user, UserOperatorConfig.DEFAULT_SECRET_PREFIX, UserOperatorConfig.DEFAULT_STRIMZI_ACLS_ADMIN_API_SUPPORTED);
    InvalidResourceException e = assertThrows(InvalidResourceException.class, () -> {
        model.maybeGeneratePassword(Reconciliation.DUMMY_RECONCILIATION, passwordGenerator, null, desiredPasswordSecret);
    });
    assertThat(e.getMessage(), is("Secret my-secret does not contain the key my-password with requested user password."));
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) InvalidResourceException(io.strimzi.operator.cluster.model.InvalidResourceException) KafkaUserBuilder(io.strimzi.api.kafka.model.KafkaUserBuilder) KafkaUser(io.strimzi.api.kafka.model.KafkaUser) Test(org.junit.jupiter.api.Test)

Example 14 with KafkaUserBuilder

use of io.strimzi.api.kafka.model.KafkaUserBuilder in project strimzi-kafka-operator by strimzi.

the class KafkaUserModelTest method testFromCrdScramShaUserWithEmptyPasswordThrows.

@Test
public void testFromCrdScramShaUserWithEmptyPasswordThrows() {
    KafkaUser emptyPassword = new KafkaUserBuilder(scramShaUser).editSpec().withNewKafkaUserScramSha512ClientAuthentication().withNewPassword().endPassword().endKafkaUserScramSha512ClientAuthentication().endSpec().build();
    InvalidResourceException e = assertThrows(InvalidResourceException.class, () -> {
        KafkaUserModel.fromCrd(emptyPassword, UserOperatorConfig.DEFAULT_SECRET_PREFIX, UserOperatorConfig.DEFAULT_STRIMZI_ACLS_ADMIN_API_SUPPORTED);
    });
    assertThat(e.getMessage(), is("Resource requests custom SCRAM-SHA-512 password but doesn't specify the secret name and/or key"));
}
Also used : InvalidResourceException(io.strimzi.operator.cluster.model.InvalidResourceException) KafkaUserBuilder(io.strimzi.api.kafka.model.KafkaUserBuilder) KafkaUser(io.strimzi.api.kafka.model.KafkaUser) Test(org.junit.jupiter.api.Test)

Example 15 with KafkaUserBuilder

use of io.strimzi.api.kafka.model.KafkaUserBuilder in project strimzi-kafka-operator by strimzi.

the class KafkaUserModelTest method testGenerateSecretUpdatesPasswordWhenRequestedByTheUser.

@Test
public void testGenerateSecretUpdatesPasswordWhenRequestedByTheUser() {
    Secret scramShaSecret = ResourceUtils.createUserSecretScramSha();
    KafkaUser user = new KafkaUserBuilder(scramShaUser).editSpec().withNewKafkaUserScramSha512ClientAuthentication().withNewPassword().withNewValueFrom().withNewSecretKeyRef("my-password", "my-secret", false).endValueFrom().endPassword().endKafkaUserScramSha512ClientAuthentication().endSpec().build();
    Secret desiredPasswordSecret = new SecretBuilder().withNewMetadata().withName("my-secret").endMetadata().addToData("my-password", DESIRED_BASE64_PASSWORD).build();
    KafkaUserModel model = KafkaUserModel.fromCrd(user, UserOperatorConfig.DEFAULT_SECRET_PREFIX, UserOperatorConfig.DEFAULT_STRIMZI_ACLS_ADMIN_API_SUPPORTED);
    model.maybeGeneratePassword(Reconciliation.DUMMY_RECONCILIATION, passwordGenerator, scramShaSecret, desiredPasswordSecret);
    Secret generated = model.generateSecret();
    assertThat(model.getScramSha512Password(), is(DESIRED_PASSWORD));
    assertThat(generated.getMetadata().getName(), is(ResourceUtils.NAME));
    assertThat(generated.getMetadata().getNamespace(), is(ResourceUtils.NAMESPACE));
    assertThat(generated.getMetadata().getLabels(), is(Labels.fromMap(ResourceUtils.LABELS).withKubernetesName(KafkaUserModel.KAFKA_USER_OPERATOR_NAME).withKubernetesInstance(ResourceUtils.NAME).withKubernetesPartOf(ResourceUtils.NAME).withKubernetesManagedBy(KafkaUserModel.KAFKA_USER_OPERATOR_NAME).withStrimziKind(KafkaUser.RESOURCE_KIND).toMap()));
    assertThat(generated.getData().keySet(), is(new HashSet<>(Arrays.asList(KafkaUserModel.KEY_PASSWORD, KafkaUserModel.KEY_SASL_JAAS_CONFIG))));
    assertThat(new String(Base64.getDecoder().decode(generated.getData().get(KafkaUserModel.KEY_PASSWORD))), is(DESIRED_PASSWORD));
    assertThat(new String(Base64.getDecoder().decode(generated.getData().get(KafkaUserModel.KEY_SASL_JAAS_CONFIG))), is("org.apache.kafka.common.security.scram.ScramLoginModule required username=\"" + ResourceUtils.NAME + "\" password=\"" + DESIRED_PASSWORD + "\";"));
    // Check owner reference
    checkOwnerReference(model.createOwnerReference(), generated);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) KafkaUserBuilder(io.strimzi.api.kafka.model.KafkaUserBuilder) KafkaUser(io.strimzi.api.kafka.model.KafkaUser) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaUser (io.strimzi.api.kafka.model.KafkaUser)30 KafkaUserBuilder (io.strimzi.api.kafka.model.KafkaUserBuilder)30 Test (org.junit.jupiter.api.Test)30 Secret (io.fabric8.kubernetes.api.model.Secret)16 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)14 InvalidResourceException (io.strimzi.operator.cluster.model.InvalidResourceException)10 HashSet (java.util.HashSet)10 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)6 KafkaUserQuotas (io.strimzi.api.kafka.model.KafkaUserQuotas)6 KafkaUserStatus (io.strimzi.api.kafka.model.status.KafkaUserStatus)6 CertManager (io.strimzi.certs.CertManager)6 Reconciliation (io.strimzi.operator.common.Reconciliation)6 Labels (io.strimzi.operator.common.model.Labels)6 MockCertManager (io.strimzi.operator.common.operator.MockCertManager)6 CrdOperator (io.strimzi.operator.common.operator.resource.CrdOperator)6 SecretOperator (io.strimzi.operator.common.operator.resource.SecretOperator)6 ResourceUtils (io.strimzi.operator.user.ResourceUtils)6 KafkaUserModel (io.strimzi.operator.user.model.KafkaUserModel)6 SimpleAclRule (io.strimzi.operator.user.model.acl.SimpleAclRule)6 Future (io.vertx.core.Future)6