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);
}
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);
}
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."));
}
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"));
}
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);
}
Aggregations