|
| 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.operator.cluster.model; |
| 6 | + |
| 7 | +import io.fabric8.certmanager.api.model.v1.Certificate; |
| 8 | +import io.fabric8.certmanager.api.model.v1.CertificateBuilder; |
| 9 | +import io.fabric8.kubernetes.api.model.OwnerReference; |
| 10 | +import io.fabric8.kubernetes.api.model.Secret; |
| 11 | +import io.strimzi.operator.common.Annotations; |
| 12 | +import io.strimzi.operator.common.model.Ca; |
| 13 | +import io.strimzi.operator.common.model.ClientsCa; |
| 14 | +import io.strimzi.operator.common.model.Labels; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +/** |
| 21 | + * cert-manager utility methods |
| 22 | + */ |
| 23 | +public class CertManagerUtils { |
| 24 | + private static final String CERT_MANAGER_SECRET_SUFFIX = "-cm"; |
| 25 | + /** |
| 26 | + * Get the name of the Secret managed by cert-manager, given a Strimzi managed Secret |
| 27 | + * |
| 28 | + * @param strimziSecretName Name of the Secret managed by Strimzi |
| 29 | + * @return Secret name to use for cert-manager managed Secret |
| 30 | + */ |
| 31 | + public static String certManagerSecretName(String strimziSecretName) { |
| 32 | + return strimziSecretName + CERT_MANAGER_SECRET_SUFFIX; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the name of the Secret managed by Strimzi, given a cert-manager managed Secret |
| 37 | + * |
| 38 | + * @param certManagerSecretName Name of the Secret managed by cert-manager |
| 39 | + * @return Secret name to use for Strimzi managed Secret |
| 40 | + */ |
| 41 | + public static String strimziSecretName(String certManagerSecretName) { |
| 42 | + if (!matchesCertManagerSecretNaming(certManagerSecretName)) { |
| 43 | + throw new RuntimeException("Supplied Secret does not match expected format for cert-manager Secret name"); |
| 44 | + } |
| 45 | + return certManagerSecretName.substring(0, certManagerSecretName.length() - CERT_MANAGER_SECRET_SUFFIX.length()); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns whether the supplied Secret name has the same format as a Secret created by cert-manager |
| 50 | + * |
| 51 | + * @param secretName Secret name to check |
| 52 | + * @return Whether the Secret name matches the format of a Secret created by cert-manager |
| 53 | + */ |
| 54 | + public static boolean matchesCertManagerSecretNaming(String secretName) { |
| 55 | + return secretName.endsWith(CERT_MANAGER_SECRET_SUFFIX); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Build Certificate object to give to cert-manager to generate certificate |
| 60 | + * |
| 61 | + * @param namespace Namespace for the Certificate |
| 62 | + * @param name Name for the Certificate |
| 63 | + * @param initialCertificate Certificate to use as a base |
| 64 | + * @param labels Labels for Certificate |
| 65 | + * @param ownerReference Owner reference for Certificate |
| 66 | + * @return Certificate object |
| 67 | + */ |
| 68 | + public static Certificate buildCertManagerCertificate(String namespace, |
| 69 | + String name, Certificate initialCertificate, |
| 70 | + Labels labels, OwnerReference ownerReference) { |
| 71 | + String secretName = certManagerSecretName(name); |
| 72 | + if (ownerReference == null) { |
| 73 | + return new CertificateBuilder(initialCertificate) |
| 74 | + .withNewMetadata() |
| 75 | + .withName(name) |
| 76 | + .withNamespace(namespace) |
| 77 | + .endMetadata() |
| 78 | + .editSpec() |
| 79 | + .withSecretName(secretName) |
| 80 | + .withNewSecretTemplate() |
| 81 | + .withLabels(labels.toMap()) |
| 82 | + .endSecretTemplate() |
| 83 | + .endSpec() |
| 84 | + .build(); |
| 85 | + } else { |
| 86 | + return new CertificateBuilder(initialCertificate) |
| 87 | + .withNewMetadata() |
| 88 | + .withName(name) |
| 89 | + .withNamespace(namespace) |
| 90 | + .withOwnerReferences(ownerReference) |
| 91 | + .endMetadata() |
| 92 | + .editSpec() |
| 93 | + .withSecretName(secretName) |
| 94 | + .withNewSecretTemplate() |
| 95 | + .withLabels(labels.toMap()) |
| 96 | + .endSecretTemplate() |
| 97 | + .endSpec() |
| 98 | + .build(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Builds a clusterCa certificate Secret for the different Strimzi components (TO, UO, KE, ...). |
| 104 | + * Used when cert-manager has issued the CA Secret. |
| 105 | + * |
| 106 | + * @param clusterCa The Cluster CA |
| 107 | + * @param clientsCa The Clients CA. If this is not null the Clients CA generation is added |
| 108 | + * @param certManagerSecret cert-manager Secret containing the Cluster CA |
| 109 | + * @param namespace Namespace for the Secret |
| 110 | + * @param secretName Name of the Secret |
| 111 | + * @param keyCertName Key under which the certificate will be stored in the new Secret |
| 112 | + * @param labels Labels |
| 113 | + * @param ownerReference Owner reference |
| 114 | + * @return Newly built Secret |
| 115 | + */ |
| 116 | + public static Secret buildTrustedCertificateSecretFromCertManager(ClusterCa clusterCa, ClientsCa clientsCa, Secret certManagerSecret, String namespace, |
| 117 | + String secretName, String keyCertName, Labels labels, |
| 118 | + OwnerReference ownerReference) { |
| 119 | + String certHash = CertUtils.getCertificateThumbprint(certManagerSecret, "tls.crt"); |
| 120 | + Objects.requireNonNull(certHash); |
| 121 | + |
| 122 | + Map<String, String> annotations = new HashMap<>(); |
| 123 | + annotations.put(Annotations.ANNO_STRIMZI_SERVER_CERT_HASH, certHash); |
| 124 | + Map.Entry<String, String> clusterCaGeneration = clusterCa.caCertGenerationFullAnnotation(); |
| 125 | + annotations.put(clusterCaGeneration.getKey(), clusterCaGeneration.getValue()); |
| 126 | + |
| 127 | + if (clientsCa != null) { |
| 128 | + Map.Entry<String, String> clientsCaGeneration = clientsCa.caCertGenerationFullAnnotation(); |
| 129 | + annotations.put(clientsCaGeneration.getKey(), clientsCaGeneration.getValue()); |
| 130 | + } |
| 131 | + |
| 132 | + return ModelUtils.createSecret(secretName, namespace, labels, ownerReference, |
| 133 | + Map.of( |
| 134 | + Ca.SecretEntry.CRT.asKey(keyCertName), certManagerSecret.getData().get("tls.crt"), |
| 135 | + Ca.SecretEntry.KEY.asKey(keyCertName), certManagerSecret.getData().get("tls.key") |
| 136 | + ), |
| 137 | + annotations, |
| 138 | + Map.of()); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Checks if the hashes of certs in a cert Secret have changed |
| 143 | + * @param existingCertSecret Existing cert Secret |
| 144 | + * @param newCertSecret New cert Secret |
| 145 | + * @return Whether the cert has been updated in the new Secret |
| 146 | + */ |
| 147 | + public static boolean certManagerCertUpdated(Secret existingCertSecret, Secret newCertSecret) { |
| 148 | + String existingCertHash = Annotations.stringAnnotation(existingCertSecret, Annotations.ANNO_STRIMZI_SERVER_CERT_HASH, null); |
| 149 | + String newCertHash = Annotations.stringAnnotation(newCertSecret, Annotations.ANNO_STRIMZI_SERVER_CERT_HASH, null); |
| 150 | + if (existingCertHash == null || newCertHash == null) { |
| 151 | + throw new RuntimeException(String.format("Failed to find server-cert-hash annotation for Secret %s/%s", existingCertSecret.getMetadata().getNamespace(), existingCertSecret.getMetadata().getName())); |
| 152 | + } |
| 153 | + return !existingCertHash.equals(newCertHash); |
| 154 | + } |
| 155 | +} |
0 commit comments