Skip to content

Commit 26c3aa2

Browse files
fix private key change race
1 parent 3880ade commit 26c3aa2

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

controllers/controllers.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/openshift/windows-machine-config-operator/pkg/metadata"
2424
"github.com/openshift/windows-machine-config-operator/pkg/nodeconfig"
2525
"github.com/openshift/windows-machine-config-operator/pkg/secrets"
26+
"github.com/openshift/windows-machine-config-operator/pkg/signer"
2627
"github.com/openshift/windows-machine-config-operator/version"
2728
)
2829

@@ -107,12 +108,28 @@ func (r *instanceReconciler) instanceFromNode(ctx context.Context, node *core.No
107108
return nil, err
108109
}
109110

110-
// Decrypt username annotation to plain text using private key
111+
// Get private key for decryption
111112
privateKeyBytes, err := secrets.GetPrivateKey(ctx, kubeTypes.NamespacedName{Namespace: r.watchNamespace,
112113
Name: secrets.PrivateKeySecret}, r.client)
113114
if err != nil {
114115
return nil, err
115116
}
117+
118+
// Check if the username annotation is encrypted with the current private key by comparing public key hash
119+
signer, err := signer.Create(ctx, kubeTypes.NamespacedName{Namespace: r.watchNamespace,
120+
Name: secrets.PrivateKeySecret}, r.client)
121+
if err != nil {
122+
return nil, fmt.Errorf("unable to create signer for public key hash verification: %w", err)
123+
}
124+
expectedPubKeyHash := nodeconfig.CreatePubKeyHashAnnotation(signer.PublicKey())
125+
currentPubKeyHash := node.Annotations[nodeconfig.PubKeyHashAnnotation]
126+
127+
if currentPubKeyHash != expectedPubKeyHash {
128+
// The username annotation is encrypted with an old private key and hasn't been updated yet
129+
// This can happen during private key rotation. Return a specific error that can be retried.
130+
return nil, fmt.Errorf("node %s username annotation is encrypted with an outdated private key, waiting for secret controller to update it", node.Name)
131+
}
132+
116133
username, err := crypto.DecryptFromJSONString(usernameAnnotation, privateKeyBytes)
117134
if err != nil {
118135
return nil, fmt.Errorf("unable to decrypt username annotation for node %s: %w", node.Name, err)

0 commit comments

Comments
 (0)