Skip to content

Commit

Permalink
[ACM-10342]update metrics collector client cert when client CA is del…
Browse files Browse the repository at this point in the history
…eted (stolostron#1379)

* update client cert when client CA is deleted

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor update cert

Signed-off-by: Coleen Iona Quadros <[email protected]>

---------

Signed-off-by: Coleen Iona Quadros <[email protected]>
  • Loading branch information
coleenquadros authored Apr 16, 2024
1 parent dce4af4 commit 4607dc0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func createUpdateResourcesForHubMetricsCollection(c client.Client, manifests []w
log.Info("MCO Operator is terminating, skip creating resources for hub metrics collection")
return nil
}

updateMtlsCert := false
hubManifestCopy = make([]workv1.Manifest, len(manifests))
for i, manifest := range manifests {
obj := manifest.RawExtension.Object.DeepCopyObject()
Expand Down Expand Up @@ -534,6 +534,9 @@ func createUpdateResourcesForHubMetricsCollection(c client.Client, manifests []w
}

if k8serrors.IsNotFound(err) {
if obj.GetName() == operatorconfig.ClientCACertificateCN {
updateMtlsCert = true
}
err = c.Create(context.TODO(), obj)
if err != nil {
log.Error(err, "Failed to create resource", "kind", obj.GetObjectKind().GroupVersionKind().Kind)
Expand Down Expand Up @@ -602,6 +605,9 @@ func createUpdateResourcesForHubMetricsCollection(c client.Client, manifests []w
}

if needsUpdate {
if obj.GetName() == operatorconfig.ClientCACertificateCN {
updateMtlsCert = true
}
err = c.Update(context.TODO(), obj)
if err != nil {
log.Error(err, "Failed to update resource", "kind", obj.GetObjectKind().GroupVersionKind().Kind)
Expand All @@ -611,7 +617,7 @@ func createUpdateResourcesForHubMetricsCollection(c client.Client, manifests []w
}
}

err := cert_controller.CreateMtlsCertSecretForHubCollector(c)
err := cert_controller.CreateUpdateMtlsCertSecretForHubCollector(c, updateMtlsCert)
if err != nil {
log.Error(err, "Failed to create client cert secret for hub metrics collection")
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ func CreateCSR() ([]byte, []byte) {
return csr, privateKey
}

func CreateMtlsCertSecretForHubCollector(c client.Client) error {
func CreateUpdateMtlsCertSecretForHubCollector(c client.Client, updateMtlsCert bool) error {
csrBytes, privateKeyBytes := CreateCSR()
csr := &certificatesv1.CertificateSigningRequest{
Spec: certificatesv1.CertificateSigningRequestSpec{
Expand All @@ -514,23 +514,41 @@ func CreateMtlsCertSecretForHubCollector(c client.Client) error {
if signedClientCert == nil {
log.Error(nil, "failed to sign CSR")
return errors.NewBadRequest("failed to sign CSR")
} else {
//Create a secret
HubMtlsSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: operatorconfig.HubMetricsCollectorMtlsCert,
Namespace: config.GetDefaultNamespace(),
},
Data: map[string][]byte{
"tls.crt": signedClientCert,
"tls.key": privateKeyBytes,
},
}
//Create a secret
HubMtlsSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: operatorconfig.HubMetricsCollectorMtlsCert,
Namespace: config.GetDefaultNamespace(),
},
Data: map[string][]byte{
"tls.crt": signedClientCert,
"tls.key": privateKeyBytes,
},
}
err := c.Create(context.TODO(), HubMtlsSecret)
if err != nil && !errors.IsAlreadyExists(err) {
log.Error(err, "Failed to create secret", "name", operatorconfig.HubMetricsCollectorMtlsCert)
return err
}
if errors.IsAlreadyExists(err) && updateMtlsCert {
err := c.Get(context.TODO(), types.NamespacedName{
Name: operatorconfig.HubMetricsCollectorMtlsCert,
Namespace: config.GetDefaultNamespace(),
}, HubMtlsSecret)
if err != nil {
log.Error(err, "Failed to get secret", "name", operatorconfig.HubMetricsCollectorMtlsCert)
return err
}
err := c.Create(context.TODO(), HubMtlsSecret)
if err != nil && !errors.IsAlreadyExists(err) {
log.Error(err, "Failed to create secret", "name", operatorconfig.HubMetricsCollectorMtlsCert)
HubMtlsSecret.Data["tls.crt"] = signedClientCert
HubMtlsSecret.Data["tls.key"] = privateKeyBytes
err = c.Update(context.TODO(), HubMtlsSecret)
if err != nil {
log.Error(err, "Failed to update secret", "name", operatorconfig.HubMetricsCollectorMtlsCert)
return err
}

}

return nil
}

0 comments on commit 4607dc0

Please sign in to comment.