Skip to content
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

operator/certrotation: introduce an optional lock to CABundleConfigMap and RotatedSigningCASecret #1753

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/operator/certrotation/cabundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"reflect"
"sort"
"sync"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
Expand Down Expand Up @@ -34,6 +35,9 @@ type CABundleConfigMap struct {
Owner *metav1.OwnerReference
// AdditionalAnnotations is a collection of annotations set for the secret
AdditionalAnnotations AdditionalAnnotations
// Lock is an optional mutex that protects the EnsureConfigMapCABundle method.
// Use it only when this instance is shared across multiple controllers.
Lock *sync.Mutex
// Plumbing:
Informer corev1informers.ConfigMapInformer
Lister corev1listers.ConfigMapLister
Expand All @@ -42,6 +46,10 @@ type CABundleConfigMap struct {
}

func (c CABundleConfigMap) EnsureConfigMapCABundle(ctx context.Context, signingCertKeyPair *crypto.CA) ([]*x509.Certificate, error) {
if c.Lock != nil {
c.Lock.Lock()
defer c.Lock.Unlock()
}
// by this point we have current signing cert/key pair. We now need to make sure that the ca-bundle configmap has this cert and
// doesn't have any expired certs
modified := false
Expand Down
9 changes: 9 additions & 0 deletions pkg/operator/certrotation/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"sync"
"time"

"github.com/openshift/library-go/pkg/crypto"
Expand Down Expand Up @@ -47,6 +48,10 @@ type RotatedSigningCASecret struct {
// AdditionalAnnotations is a collection of annotations set for the secret
AdditionalAnnotations AdditionalAnnotations

// Lock is an optional mutex that protects the EnsureSigningCertKeyPair method.
// Use it only when this instance is shared across multiple controllers.
Lock *sync.Mutex

// Plumbing:
Informer corev1informers.SecretInformer
Lister corev1listers.SecretLister
Expand All @@ -63,6 +68,10 @@ type RotatedSigningCASecret struct {
// EnsureSigningCertKeyPair manages the entire lifecycle of a signer cert as a secret, from creation to continued rotation.
// It always returns the currently used CA pair, a bool indicating whether it was created/updated within this function call and an error.
func (c RotatedSigningCASecret) EnsureSigningCertKeyPair(ctx context.Context) (*crypto.CA, bool, error) {
if c.Lock != nil {
c.Lock.Lock()
defer c.Lock.Unlock()
}
modified := false
originalSigningCertKeyPairSecret, err := c.Lister.Secrets(c.Namespace).Get(c.Name)
if err != nil && !apierrors.IsNotFound(err) {
Expand Down