Skip to content

Commit e59cc4c

Browse files
Fix WICD updated kubeconfig usage
1 parent 3880ade commit e59cc4c

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
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)

pkg/nodeconfig/nodeconfig.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ func (nc *nodeConfig) Configure(ctx context.Context) error {
184184
return fmt.Errorf("bootstrapping the Windows instance failed: %w", err)
185185
}
186186

187-
var wicdNodeKC string
187+
// Track which kubeconfig WICD is currently using for cleanup purposes
188+
wicdActiveKubeconfig := wicdKC
188189

189190
// Perform rest of the configuration with the kubelet running
190191
err = func() error {
@@ -227,6 +228,9 @@ func (nc *nodeConfig) Configure(ctx context.Context) error {
227228
if err := nc.Windows.ConfigureWICD(nc.wmcoNamespace, wicdNodeKC); err != nil {
228229
return fmt.Errorf("configuring WICD failed: %w", err)
229230
}
231+
// Update the active kubeconfig tracker since WICD was successfully reconfigured
232+
wicdActiveKubeconfig = wicdNodeKC
233+
230234
// Set the desired version annotation, communicating to WICD which Windows services configmap to use
231235
if err := metadata.ApplyDesiredVersionAnnotation(ctx, nc.client, *nc.node, wmcoVersion); err != nil {
232236
return fmt.Errorf("error updating desired version annotation on node %s: %w", nc.node.GetName(), err)
@@ -261,7 +265,7 @@ func (nc *nodeConfig) Configure(ctx context.Context) error {
261265
// Stop the kubelet so that the node is marked NotReady in case of an error in configuration. We are stopping all
262266
// the required services as they are interdependent and is safer to do so given the node is going to be NotReady.
263267
if err != nil {
264-
if cleanupErr := nc.Windows.RunWICDCleanup(nc.wmcoNamespace, wicdNodeKC); cleanupErr != nil {
268+
if cleanupErr := nc.Windows.RunWICDCleanup(nc.wmcoNamespace, wicdActiveKubeconfig); cleanupErr != nil {
265269
nc.log.Info("Unable to mark node as NotReady", "error", cleanupErr)
266270
}
267271
}

pkg/windows/windows.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,19 @@ func (vm *windows) Bootstrap(ctx context.Context, desiredVer, watchNamespace, wi
558558

559559
// ConfigureWICD starts the Windows Instance Config Daemon service
560560
func (vm *windows) ConfigureWICD(watchNamespace, wicdKubeconfigContents string) error {
561+
// Check if service is already running before updating files
562+
serviceExists, err := vm.serviceExists(WicdServiceName)
563+
if err != nil {
564+
return fmt.Errorf("error checking if %s Windows service exists: %w", WicdServiceName, err)
565+
}
566+
serviceRunning := false
567+
if serviceExists {
568+
serviceRunning, err = vm.isRunning(WicdServiceName)
569+
if err != nil {
570+
return fmt.Errorf("error checking if %s Windows service is running: %w", WicdServiceName, err)
571+
}
572+
}
573+
561574
if err := vm.ensureWICDFilesExist(wicdKubeconfigContents); err != nil {
562575
return err
563576
}
@@ -590,6 +603,16 @@ func (vm *windows) ConfigureWICD(watchNamespace, wicdKubeconfigContents string)
590603
if err != nil {
591604
return fmt.Errorf("error creating %s service object: %w", WicdServiceName, err)
592605
}
606+
607+
// If service was already running, stop it first so it picks up the new kubeconfig
608+
// This ensures WICD always uses the latest configuration when ConfigureWICD is called
609+
if serviceRunning {
610+
vm.log.Info("stopping WICD service to reload configuration", "service", WicdServiceName)
611+
if err := vm.stopService(wicdService); err != nil {
612+
return fmt.Errorf("error stopping %s Windows service for reconfiguration: %w", WicdServiceName, err)
613+
}
614+
}
615+
593616
if err := vm.ensureServiceIsRunning(wicdService); err != nil {
594617
return fmt.Errorf("error ensuring %s Windows service has started running: %w", WicdServiceName, err)
595618
}

0 commit comments

Comments
 (0)