Repository navigation
certs: migrate generated key material to ECDSA P-384 with a zero-downtime CA trust transition #3986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ed6dc94
d8983b0
0606aad
9966551
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,13 @@ const ( | |
| DefaultCertValidFor = time.Hour * 24 * 730 | ||
| // OLMCAPEMKey is the CAPEM | ||
| OLMCAPEMKey = "olmCAKey" | ||
| // OLMPreviousCAPEMKey is the key under which the PEM of the previous CA is | ||
| // stored in a cert Secret while a CA trust transition is in flight. Its | ||
| // presence indicates that the transition has not been finalized yet: the | ||
| // previous CA is kept in the CABundles of webhook configurations, CRD | ||
| // conversions and APIServices until the rollout onto the current CA has | ||
| // completed. | ||
| OLMPreviousCAPEMKey = "olmPreviousCAKey" | ||
| // OLMCAHashAnnotationKey is the label key used to store the hash of the CA cert | ||
| OLMCAHashAnnotationKey = "olmcahash" | ||
| // Organization is the organization name used in the generation of x509 certs | ||
|
|
@@ -228,6 +235,30 @@ func (i *StrategyDeploymentInstaller) CertsRotateAt() time.Time { | |
| return CalculateCertRotatesAt(i.certificateExpirationTime) | ||
| } | ||
|
|
||
| // caPEMWithPreviousCA returns the given current CA PEM extended with the | ||
| // previous CA stored in the given Secret when a CA trust transition is in | ||
| // flight, marking the transition on the installer so that cert resources are | ||
| // updated before any deployment is rolled out. | ||
| func (i *StrategyDeploymentInstaller) caPEMWithPreviousCA(currentCAPEM []byte, secret *corev1.Secret) []byte { | ||
| previousCAPEM, ok := secret.Data[OLMPreviousCAPEMKey] | ||
| if !ok || len(previousCAPEM) == 0 { | ||
| return currentCAPEM | ||
| } | ||
| i.caTransition = true | ||
| return caPEMBundle(currentCAPEM, previousCAPEM) | ||
| } | ||
|
|
||
| // caPEMBundle returns a PEM bundle with the current CA followed by the | ||
| // previous CA. The current CA is always written first so that hashing the | ||
| // first certificate of a bundle (see certs.CurrentCAPEM) yields the hash of | ||
| // the current CA, and removing the previous CA from the bundle does not | ||
| // change it. | ||
| func caPEMBundle(currentCAPEM, previousCAPEM []byte) []byte { | ||
| bundle := make([]byte, 0, len(currentCAPEM)+len(previousCAPEM)) | ||
| bundle = append(bundle, currentCAPEM...) | ||
| return append(bundle, previousCAPEM...) | ||
| } | ||
|
|
||
| func (i *StrategyDeploymentInstaller) CertsRotated() bool { | ||
| return i.certificatesRotated | ||
| } | ||
|
|
@@ -258,6 +289,12 @@ func shouldRotateCerts(certSecret *corev1.Secret, hosts []string) bool { | |
| return true | ||
| } | ||
|
|
||
| // Rotate CA and serving certs that do not use ECDSA P-384, e.g. the P-256 | ||
| // material generated by older versions of OLM. | ||
| if !certs.IsP384(ca) || !certs.IsP384(cert) { | ||
| return true | ||
| } | ||
|
|
||
| // check for cert freshness | ||
| if !certs.Active(ca) || now.Time.After(CalculateCertRotatesAt(ca.NotAfter)) || | ||
| !certs.Active(cert) || now.Time.After(CalculateCertRotatesAt(cert.NotAfter)) { | ||
|
|
@@ -300,6 +337,88 @@ func (i *StrategyDeploymentInstaller) ShouldRotateCerts(s Strategy) (bool, error | |
| return false, nil | ||
| } | ||
|
|
||
| // FinalizeCATransition completes an in-flight CA trust transition once every | ||
| // deployment has rolled out onto the current CA: the CABundles of the | ||
| // generated cert resources (webhook configurations, CRD conversions and | ||
| // APIServices) are rewritten with only the current CA and the transition | ||
| // state (the olmPreviousCAKey entry) is cleared from the cert Secrets. | ||
| // | ||
| // It returns true when a transition was in flight and has been finalized. The | ||
| // previous CA is only dropped once each deployment that owns cert resources | ||
| // is up-to-date and available and carries an olmcahash annotation matching | ||
| // the current CA of its cert Secret, so that no pod still serves a | ||
| // certificate trusted only via the previous CA. Removing the previous CA does | ||
| // not change the olmcahash (which hashes the current CA only) and therefore | ||
| // does not trigger an additional rollout. | ||
| func (i *StrategyDeploymentInstaller) FinalizeCATransition(s Strategy) (bool, error) { | ||
| strategy, ok := s.(*v1alpha1.StrategyDetailsDeployment) | ||
| if !ok { | ||
| return false, fmt.Errorf("failed to finalize CA transition with deployment installer: unsupported deployment install strategy: %s", s.GetStrategyName()) | ||
| } | ||
|
|
||
| // Collect the cert Secrets with an in-flight transition and point every | ||
| // cert resource at the current CA of its deployment's Secret, so that the | ||
| // CABundles are rewritten without the previous CA. | ||
| inFlight := map[string]*corev1.Secret{} | ||
| for _, sddSpec := range strategy.DeploymentSpecs { | ||
| if len(i.certResourcesForDeployment(sddSpec.Name)) == 0 { | ||
| continue | ||
| } | ||
| secret, err := i.strategyClient.GetOpLister().CoreV1().SecretLister().Secrets(i.owner.GetNamespace()).Get(SecretName(ServiceName(sddSpec.Name))) | ||
| if err != nil { | ||
| if apierrors.IsNotFound(err) { | ||
| // The cert Secret is managed by the install path. If it is | ||
| // missing, the CSV is reinstalled and the transition restarts. | ||
| return false, nil | ||
| } | ||
| return false, err | ||
| } | ||
| if len(secret.Data[OLMPreviousCAPEMKey]) > 0 { | ||
| inFlight[sddSpec.Name] = secret | ||
| } | ||
| i.updateCertResourcesForDeployment(sddSpec.Name, secret.Data[OLMCAPEMKey]) | ||
| } | ||
| if len(inFlight) == 0 { | ||
| return false, nil | ||
| } | ||
|
|
||
| // Wait for the rollout onto the current CA to complete before dropping | ||
| // trust in the previous CA. | ||
| for deploymentName, secret := range inFlight { | ||
| deployment, err := i.strategyClient.GetOpLister().AppsV1().DeploymentLister().Deployments(i.owner.GetNamespace()).Get(deploymentName) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| reason, ready, err := DeploymentStatus(deployment) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| if !ready { | ||
| log.Infof("waiting for deployment %s to roll out onto the current CA before finalizing the CA transition: %s", deploymentName, reason) | ||
| return false, nil | ||
| } | ||
| if hash, ok := deployment.Spec.Template.GetAnnotations()[OLMCAHashAnnotationKey]; !ok || hash != secret.GetAnnotations()[OLMCAHashAnnotationKey] { | ||
|
Comment on lines
+392
to
+400
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Finalization can remove trust in the previous CA while a replacement webhook Pod still serves a certificate from that CA. The finalizer checks only that each cert-owning Deployment is ready and that its template's The trigger requires the Secret update to reach the node after the replacement Pod starts. Finalization must not drop the previous CA until each cert-owning Deployment serves a certificate signed by the current CA. Check the served certificate. Do not rely on the hash annotation or on the projected Secret file alone.
|
||
| log.Infof("waiting for deployment %s to pick up the current CA hash before finalizing the CA transition", deploymentName) | ||
| return false, nil | ||
| } | ||
| } | ||
|
|
||
| // Rewrite the CABundles with only the current CA, then clear the | ||
| // transition state. Neither step changes the olmcahash, so no additional | ||
| // rollout is triggered. | ||
| if err := i.createOrUpdateCertResourcesForDeployment(); err != nil { | ||
| return false, err | ||
| } | ||
| for _, secret := range inFlight { | ||
| updated := secret.DeepCopy() | ||
| delete(updated.Data, OLMPreviousCAPEMKey) | ||
| if _, err := i.strategyClient.GetOpClient().UpdateSecret(updated); err != nil { | ||
| return false, err | ||
| } | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| func CalculateCertExpiration(startingFrom time.Time) time.Time { | ||
| return startingFrom.Add(DefaultCertValidFor) | ||
| } | ||
|
|
@@ -384,12 +503,32 @@ func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deplo | |
| secret = existingSecret | ||
| caPEM = existingSecret.Data[OLMCAPEMKey] | ||
| caHash = certs.PEMSHA256(caPEM) | ||
| caPEM = i.caPEMWithPreviousCA(caPEM, existingSecret) | ||
| } else { | ||
| // Start a CA trust transition when the existing CA is still | ||
| // active: remember it in the secret so that it remains trusted in | ||
| // the CABundles until the rollout onto the current CA has | ||
| // completed and the transition is finalized. | ||
| previousCAPEM := existingSecret.Data[OLMCAPEMKey] | ||
| previousCA, prevErr := certs.PEMToCert(previousCAPEM) | ||
| switch { | ||
| case prevErr != nil: | ||
| logger.Warnf("existing CA for Service %s is malformed, rotating without a trust transition: %v", serviceName, prevErr) | ||
| case !certs.Active(previousCA): | ||
| logger.Warnf("existing CA for Service %s is no longer active, rotating without a trust transition", serviceName) | ||
| default: | ||
| secret.Data[OLMPreviousCAPEMKey] = previousCAPEM | ||
| } | ||
|
|
||
| if _, err := i.strategyClient.GetOpClient().UpdateSecret(secret); err != nil { | ||
| logger.Warnf("could not update secret %s", secret.GetName()) | ||
| return nil, nil, err | ||
| } | ||
| i.certificatesRotated = true | ||
| if _, ok := secret.Data[OLMPreviousCAPEMKey]; ok { | ||
| i.caTransition = true | ||
| caPEM = caPEMBundle(caPEM, previousCAPEM) | ||
| } | ||
| } | ||
| } else if apierrors.IsNotFound(err) { | ||
| // Create the secret | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return false for a typed nil ECDSA key.
If a caller constructs an
x509.CertificatewithPublicKeyAlgorithm: x509.ECDSAandPublicKey: (*ecdsa.PublicKey)(nil), the type assertion succeeds.IsP384then panics when it readspublicKey.Curve. CheckpublicKey == nilalongside!ok. A non-nil P-384 key will still pass.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.