use of io.strimzi.certs.CertAndKey in project strimzi-kafka-operator by strimzi.
the class CaRenewalTest method renewalOfStatefulSetCertificatesWithCaRenewal.
@ParallelTest
public void renewalOfStatefulSetCertificatesWithCaRenewal() throws IOException {
MockedCa mockedCa = new MockedCa(Reconciliation.DUMMY_RECONCILIATION, null, null, null, null, null, null, null, 2, 1, true, null);
mockedCa.setCertRenewed(true);
Secret initialSecret = new SecretBuilder().withNewMetadata().withName("test-secret").endMetadata().addToData("pod0.crt", Base64.getEncoder().encodeToString("old-cert".getBytes())).addToData("pod0.key", Base64.getEncoder().encodeToString("old-key".getBytes())).addToData("pod0.p12", Base64.getEncoder().encodeToString("old-keystore".getBytes())).addToData("pod0.password", Base64.getEncoder().encodeToString("old-password".getBytes())).addToData("pod1.crt", Base64.getEncoder().encodeToString("old-cert".getBytes())).addToData("pod1.key", Base64.getEncoder().encodeToString("old-key".getBytes())).addToData("pod1.p12", Base64.getEncoder().encodeToString("old-keystore".getBytes())).addToData("pod1.password", Base64.getEncoder().encodeToString("old-password".getBytes())).addToData("pod2.crt", Base64.getEncoder().encodeToString("old-cert".getBytes())).addToData("pod2.key", Base64.getEncoder().encodeToString("old-key".getBytes())).addToData("pod2.p12", Base64.getEncoder().encodeToString("old-keystore".getBytes())).addToData("pod2.password", Base64.getEncoder().encodeToString("old-password".getBytes())).build();
int replicas = 3;
Function<Integer, Subject> subjectFn = i -> new Subject.Builder().build();
Function<Integer, String> podNameFn = i -> "pod" + i;
boolean isMaintenanceTimeWindowsSatisfied = true;
Map<String, CertAndKey> newCerts = mockedCa.maybeCopyOrGenerateCerts(Reconciliation.DUMMY_RECONCILIATION, replicas, subjectFn, initialSecret, podNameFn, isMaintenanceTimeWindowsSatisfied);
assertThat(new String(newCerts.get("pod0").cert()), is("new-cert0"));
assertThat(new String(newCerts.get("pod0").key()), is("new-key0"));
assertThat(new String(newCerts.get("pod0").keyStore()), is("new-keystore0"));
assertThat(newCerts.get("pod0").storePassword(), is("new-password0"));
assertThat(new String(newCerts.get("pod1").cert()), is("new-cert1"));
assertThat(new String(newCerts.get("pod1").key()), is("new-key1"));
assertThat(new String(newCerts.get("pod1").keyStore()), is("new-keystore1"));
assertThat(newCerts.get("pod1").storePassword(), is("new-password1"));
assertThat(new String(newCerts.get("pod2").cert()), is("new-cert2"));
assertThat(new String(newCerts.get("pod2").key()), is("new-key2"));
assertThat(new String(newCerts.get("pod2").keyStore()), is("new-keystore2"));
assertThat(newCerts.get("pod2").storePassword(), is("new-password2"));
}
use of io.strimzi.certs.CertAndKey in project strimzi by strimzi.
the class KafkaCluster method generateCertificatesSecret.
/**
* Generates the private keys for the Kafka brokers (if needed) and the secret with them which contains both the
* public and private keys.
*
* @param clusterCa The CA for cluster certificates
* @param clientsCa The CA for clients certificates
* @param externalBootstrapDnsName Map with bootstrap DNS names which should be added to the certificate
* @param externalDnsNames Map with broker DNS names which should be added to the certificate
* @param isMaintenanceTimeWindowsSatisfied Indicates whether we are in a maintenance window or not
*
* @return The generated Secret with broker certificates
*/
public Secret generateCertificatesSecret(ClusterCa clusterCa, ClientsCa clientsCa, Set<String> externalBootstrapDnsName, Map<Integer, Set<String>> externalDnsNames, boolean isMaintenanceTimeWindowsSatisfied) {
Map<String, CertAndKey> brokerCerts;
Map<String, String> data = new HashMap<>(replicas * 4);
try {
brokerCerts = clusterCa.generateBrokerCerts(namespace, cluster, replicas, externalBootstrapDnsName, externalDnsNames, isMaintenanceTimeWindowsSatisfied);
} catch (IOException e) {
LOGGER.warnCr(reconciliation, "Error while generating certificates", e);
throw new RuntimeException("Failed to prepare Kafka certificates", e);
}
for (int i = 0; i < replicas; i++) {
CertAndKey cert = brokerCerts.get(KafkaResources.kafkaPodName(cluster, i));
data.put(KafkaResources.kafkaPodName(cluster, i) + ".key", cert.keyAsBase64String());
data.put(KafkaResources.kafkaPodName(cluster, i) + ".crt", cert.certAsBase64String());
data.put(KafkaResources.kafkaPodName(cluster, i) + ".p12", cert.keyStoreAsBase64String());
data.put(KafkaResources.kafkaPodName(cluster, i) + ".password", cert.storePasswordAsBase64String());
}
return createSecret(KafkaResources.kafkaSecretName(cluster), data, Map.of(clusterCa.caCertGenerationAnnotation(), String.valueOf(clusterCa.certGeneration()), clientsCa.caCertGenerationAnnotation(), String.valueOf(clientsCa.certGeneration())));
}
use of io.strimzi.certs.CertAndKey in project strimzi by strimzi.
the class ZookeeperCluster method generateCertificatesSecret.
/**
* Generate the Secret containing the Zookeeper nodes certificates signed by the cluster CA certificate used for TLS
* based internal communication with Kafka. It contains both the public and private keys.
*
* @param clusterCa The CA for cluster certificates
* @param isMaintenanceTimeWindowsSatisfied Indicates whether we are in the maintenance window or not.
*
* @return The generated Secret with the ZooKeeper node certificates
*/
public Secret generateCertificatesSecret(ClusterCa clusterCa, boolean isMaintenanceTimeWindowsSatisfied) {
Map<String, String> secretData = new HashMap<>(replicas * 4);
Map<String, CertAndKey> certs;
try {
certs = clusterCa.generateZkCerts(namespace, cluster, replicas, isMaintenanceTimeWindowsSatisfied);
} catch (IOException e) {
LOGGER.warnCr(reconciliation, "Error while generating certificates", e);
throw new RuntimeException("Failed to prepare ZooKeeper certificates", e);
}
for (int i = 0; i < replicas; i++) {
CertAndKey cert = certs.get(KafkaResources.zookeeperPodName(cluster, i));
secretData.put(KafkaResources.zookeeperPodName(cluster, i) + ".key", cert.keyAsBase64String());
secretData.put(KafkaResources.zookeeperPodName(cluster, i) + ".crt", cert.certAsBase64String());
secretData.put(KafkaResources.zookeeperPodName(cluster, i) + ".p12", cert.keyStoreAsBase64String());
secretData.put(KafkaResources.zookeeperPodName(cluster, i) + ".password", cert.storePasswordAsBase64String());
}
return createSecret(KafkaResources.zookeeperSecretName(cluster), secretData, Collections.singletonMap(clusterCa.caCertGenerationAnnotation(), String.valueOf(clusterCa.certGeneration())));
}
use of io.strimzi.certs.CertAndKey in project strimzi by strimzi.
the class Ca method maybeCopyOrGenerateCerts.
/**
* Copy already existing certificates from provided Secret based on number of effective replicas
* and maybe generate new ones for new replicas (i.e. scale-up).
*/
protected Map<String, CertAndKey> maybeCopyOrGenerateCerts(Reconciliation reconciliation, int replicas, Function<Integer, Subject> subjectFn, Secret secret, Function<Integer, String> podNameFn, boolean isMaintenanceTimeWindowsSatisfied) throws IOException {
int replicasInSecret;
if (secret == null || secret.getData() == null || this.certRenewed()) {
replicasInSecret = 0;
} else {
replicasInSecret = (int) secret.getData().keySet().stream().filter(k -> k.contains(".crt")).count();
}
File brokerCsrFile = File.createTempFile("tls", "broker-csr");
File brokerKeyFile = File.createTempFile("tls", "broker-key");
File brokerCertFile = File.createTempFile("tls", "broker-cert");
File brokerKeyStoreFile = File.createTempFile("tls", "broker-p12");
int replicasInNewSecret = Math.min(replicasInSecret, replicas);
Map<String, CertAndKey> certs = new HashMap<>(replicasInNewSecret);
// scale down -> it will copy just the requested number of replicas
for (int i = 0; i < replicasInNewSecret; i++) {
String podName = podNameFn.apply(i);
LOGGER.debugCr(reconciliation, "Certificate for {} already exists", podName);
Subject subject = subjectFn.apply(i);
CertAndKey certAndKey;
if (secret.getData().get(podName + ".p12") != null && !secret.getData().get(podName + ".p12").isEmpty() && secret.getData().get(podName + ".password") != null && !secret.getData().get(podName + ".password").isEmpty()) {
certAndKey = asCertAndKey(secret, podName + ".key", podName + ".crt", podName + ".p12", podName + ".password");
} else {
// coming from an older operator version, the secret exists but without keystore and password
certAndKey = addKeyAndCertToKeyStore(subject.commonName(), Base64.getDecoder().decode(secret.getData().get(podName + ".key")), Base64.getDecoder().decode(secret.getData().get(podName + ".crt")));
}
List<String> reasons = new ArrayList<>(2);
if (certSubjectChanged(certAndKey, subject, podName)) {
reasons.add("DNS names changed");
}
if (isExpiring(secret, podName + ".crt") && isMaintenanceTimeWindowsSatisfied) {
reasons.add("certificate is expiring");
}
if (renewalType.equals(RenewalType.CREATE)) {
reasons.add("certificate added");
}
if (!reasons.isEmpty()) {
LOGGER.debugCr(reconciliation, "Certificate for pod {} need to be regenerated because: {}", podName, String.join(", ", reasons));
CertAndKey newCertAndKey = generateSignedCert(subject, brokerCsrFile, brokerKeyFile, brokerCertFile, brokerKeyStoreFile);
certs.put(podName, newCertAndKey);
} else {
certs.put(podName, certAndKey);
}
}
// scale down -> does nothing
for (int i = replicasInSecret; i < replicas; i++) {
String podName = podNameFn.apply(i);
LOGGER.debugCr(reconciliation, "Certificate for pod {} to generate", podName);
CertAndKey k = generateSignedCert(subjectFn.apply(i), brokerCsrFile, brokerKeyFile, brokerCertFile, brokerKeyStoreFile);
certs.put(podName, k);
}
delete(reconciliation, brokerCsrFile);
delete(reconciliation, brokerKeyFile);
delete(reconciliation, brokerCertFile);
delete(reconciliation, brokerKeyStoreFile);
return certs;
}
use of io.strimzi.certs.CertAndKey in project strimzi by strimzi.
the class Ca method addKeyAndCertToKeyStore.
public CertAndKey addKeyAndCertToKeyStore(String alias, byte[] key, byte[] cert) throws IOException {
File keyFile = File.createTempFile("tls", "key");
File certFile = File.createTempFile("tls", "cert");
File keyStoreFile = File.createTempFile("tls", "p12");
Files.write(keyFile.toPath(), key);
Files.write(certFile.toPath(), cert);
String keyStorePassword = passwordGenerator.generate();
certManager.addKeyAndCertToKeyStore(keyFile, certFile, alias, keyStoreFile, keyStorePassword);
CertAndKey result = new CertAndKey(Files.readAllBytes(keyFile.toPath()), Files.readAllBytes(certFile.toPath()), null, Files.readAllBytes(keyStoreFile.toPath()), keyStorePassword);
delete(reconciliation, keyFile);
delete(reconciliation, certFile);
delete(reconciliation, keyStoreFile);
return result;
}
Aggregations