KafkaUserModel { public static KafkaUserModel fromCrd(CertManager certManager, PasswordGenerator passwordGenerator, KafkaUser kafkaUser, Secret clientsCaCert, Secret clientsCaKey, Secret userSecret) { KafkaUserModel result = new KafkaUserModel(kafkaUser.getMetadata().getNamespace(), kafkaUser.getMetadata().getName(), Labels.fromResource(kafkaUser).withStrimziKind(kafkaUser.getKind())); result.setOwnerReference(kafkaUser); result.setAuthentication(kafkaUser.getSpec().getAuthentication()); if (kafkaUser.getSpec().getAuthentication() instanceof KafkaUserTlsClientAuthentication) { if (kafkaUser.getMetadata().getName().length() > OpenSslCertManager.MAXIMUM_CN_LENGTH) { throw new InvalidResourceException("Users with TLS client authentication can have a username (name of the KafkaUser custom resource) only up to 64 characters long."); } result.maybeGenerateCertificates(certManager, passwordGenerator, clientsCaCert, clientsCaKey, userSecret, UserOperatorConfig.getClientsCaValidityDays(), UserOperatorConfig.getClientsCaRenewalDays()); } else if (kafkaUser.getSpec().getAuthentication() instanceof KafkaUserScramSha512ClientAuthentication) { result.maybeGeneratePassword(passwordGenerator, userSecret); } if (kafkaUser.getSpec().getAuthorization() != null && kafkaUser.getSpec().getAuthorization().getType().equals(KafkaUserAuthorizationSimple.TYPE_SIMPLE)) { KafkaUserAuthorizationSimple simple = (KafkaUserAuthorizationSimple) kafkaUser.getSpec().getAuthorization(); result.setSimpleAclRules(simple.getAcls()); } result.setQuotas(kafkaUser.getSpec().getQuotas()); if (kafkaUser.getSpec().getTemplate() != null && kafkaUser.getSpec().getTemplate().getSecret() != null && kafkaUser.getSpec().getTemplate().getSecret().getMetadata() != null) { result.templateSecretLabels = kafkaUser.getSpec().getTemplate().getSecret().getMetadata().getLabels(); result.templateSecretAnnotations = kafkaUser.getSpec().getTemplate().getSecret().getMetadata().getAnnotations(); } return result; } protected  KafkaUserModel(String namespace, String name, Labels labels); static KafkaUserModel fromCrd(CertManager certManager,
                                         PasswordGenerator passwordGenerator,
                                         KafkaUser kafkaUser,
                                         Secret clientsCaCert,
                                         Secret clientsCaKey,
                                         Secret userSecret); Secret generateSecret(); @SuppressWarnings("checkstyle:BooleanExpressionComplexity") void maybeGenerateCertificates(CertManager certManager, PasswordGenerator passwordGenerator,
                                          Secret clientsCaCertSecret, Secret clientsCaKeySecret,
                                          Secret userSecret, int validityDays, int renewalDays); void maybeGeneratePassword(PasswordGenerator generator, Secret userSecret); static String decodeUsername(String username); static String getTlsUserName(String username); static String getScramUserName(String username); String getUserName(); String getName(); static String getSecretName(String username); String getSecretName(); void setAuthentication(KafkaUserAuthentication authentication); void setQuotas(KafkaUserQuotas quotas); KafkaUserQuotas getQuotas(); Set<SimpleAclRule> getSimpleAclRules(); void setSimpleAclRules(List<AclRule> rules); boolean isTlsUser(); boolean isScramUser(); boolean isNoneUser(); static final String KEY_PASSWORD; static final String ENV_VAR_CLIENTS_CA_VALIDITY; static final String ENV_VAR_CLIENTS_CA_RENEWAL; static final String KAFKA_USER_OPERATOR_NAME; }
@Test public void testFromCrdTlsUserWith65CharTlsUsernameThrows() { KafkaUser tooLong = new KafkaUserBuilder(tlsUser) .editMetadata() .withName("User-123456789012345678901234567890123456789012345678901234567890") .endMetadata() .build(); assertThrows(InvalidResourceException.class, () -> { KafkaUserModel.fromCrd(mockCertManager, passwordGenerator, tooLong, clientsCaCert, clientsCaKey, null); }); }
@Test public void testFromCrdTlsUserWith64CharTlsUsernameValid() { KafkaUser notTooLong = new KafkaUserBuilder(tlsUser) .editMetadata() .withName("User123456789012345678901234567890123456789012345678901234567890") .endMetadata() .build(); KafkaUserModel.fromCrd(mockCertManager, passwordGenerator, notTooLong, clientsCaCert, clientsCaKey, null); }
@Test public void testFromCrdScramShaUserWith65CharSaslUsernameValid() { KafkaUser tooLong = new KafkaUserBuilder(scramShaUser) .editMetadata() .withName("User-123456789012345678901234567890123456789012345678901234567890") .endMetadata() .build(); KafkaUserModel.fromCrd(mockCertManager, passwordGenerator, tooLong, clientsCaCert, clientsCaKey, null); }