-
Notifications
You must be signed in to change notification settings - Fork 526
AGENT-1449: Add single-phase IRI registry credential rotation #5810
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
Open
rwsu
wants to merge
8
commits into
openshift:main
Choose a base branch
from
rwsu:AGENT-1449-auth-rotation-simple
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a5fc45d
AGENT-1449: Add single-phase IRI registry credential rotation
rwsu 40e458f
AGENT-1449: Move auth token resolution into iriRegistry constructor
rwsu f7e390f
AGENT-1449: Filter secret events by namespace and use authSecret.Name…
rwsu 71ad4ed
AGENT-1449: Use feature gate check instead of nil-informer check in t…
rwsu 6760a23
AGENT-1449: Add pod-based pull verification to rotation e2e test
rwsu 8391705
AGENT-1449: Fix race condition in IRISecretMerger between gate check …
rwsu 17aa324
AGENT-1449: Wait for full master pool rollout before asserting old cr…
rwsu 6c65328
AGENT-1449: Fix post-rotation kubelet pull check in e2e test
rwsu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
pkg/controller/internalreleaseimage/internalreleaseimage_registry_auth.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package internalreleaseimage | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" | ||
| "golang.org/x/crypto/bcrypt" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| clientset "k8s.io/client-go/kubernetes" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| // generateHtpasswdEntry generates an htpasswd-formatted line for the given username | ||
| // and password using bcrypt hashing. | ||
| func generateHtpasswdEntry(username, password string) (string, error) { | ||
| hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to generate bcrypt hash: %w", err) | ||
| } | ||
| return fmt.Sprintf("%s:%s", username, string(hash)), nil | ||
| } | ||
|
|
||
| // HtpasswdMatchesPassword reports whether the given htpasswd line matches | ||
| // the provided username and password. | ||
| func HtpasswdMatchesPassword(htpasswd, username, password string) bool { | ||
| prefix := username + ":" | ||
| if !strings.HasPrefix(htpasswd, prefix) { | ||
| return false | ||
| } | ||
| hash := []byte(strings.TrimPrefix(htpasswd, prefix)) | ||
| return bcrypt.CompareHashAndPassword(hash, []byte(password)) == nil | ||
| } | ||
|
|
||
| // reconcileHtpasswd ensures the htpasswd field in the IRI auth secret is in | ||
| // sync with the password field. If the password has changed (or htpasswd is | ||
| // missing), it generates a new bcrypt hash and updates the secret. This is the | ||
| // trigger for single-phase credential rotation: the updated htpasswd causes the | ||
| // MachineConfig to be re-rendered, which MCDs roll out to nodes. Brief registry | ||
| // downtime during the rollout is accepted. | ||
| func reconcileHtpasswd(kubeClient clientset.Interface, authSecret *corev1.Secret) (*corev1.Secret, error) { | ||
| password := string(authSecret.Data["password"]) | ||
| if password == "" { | ||
| return nil, fmt.Errorf("IRI auth secret %s/%s missing or empty \"password\" field", authSecret.Namespace, authSecret.Name) | ||
| } | ||
| htpasswd := string(authSecret.Data["htpasswd"]) | ||
|
|
||
| if HtpasswdMatchesPassword(htpasswd, ctrlcommon.IRIRegistryUsername, password) { | ||
| return authSecret, nil | ||
| } | ||
|
|
||
| klog.V(4).Infof("IRI auth secret htpasswd is out of sync with password, regenerating") | ||
|
|
||
| newHtpasswd, err := generateHtpasswdEntry(ctrlcommon.IRIRegistryUsername, password) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to generate htpasswd: %w", err) | ||
| } | ||
|
|
||
| updated := authSecret.DeepCopy() | ||
| updated.Data["htpasswd"] = []byte(newHtpasswd) | ||
|
|
||
| result, err := kubeClient.CoreV1().Secrets(authSecret.Namespace).Update( | ||
| context.TODO(), updated, metav1.UpdateOptions{}) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to update IRI auth secret: %w", err) | ||
| } | ||
|
|
||
| klog.Infof("Regenerated IRI auth secret htpasswd for credential rotation (secret %s/%s)", authSecret.Namespace, authSecret.Name) | ||
| return result, nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
(very minor nit): random removal of newline