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

Read secrets for client-onboarding-token-validation #2827

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mrudraia1
Copy link

This PR reads the secrets instead of reading the secrets from the volume mounts.
whenever the new onboarding secrets are created, it takes more time to read the secrets from the volume mounts,
The user clicks the rotate onboarding keys, the kubernetes still uses the old public, private keys , the new keys are mounted later, So this PR will read the secrets directly from the kubernetes secrets.

controllers/storagecluster/storageclient.go Outdated Show resolved Hide resolved
controllers/storagecluster/storageclient.go Outdated Show resolved Hide resolved
controllers/util/provider.go Show resolved Hide resolved
controllers/util/provider.go Outdated Show resolved Hide resolved
controllers/util/provider.go Outdated Show resolved Hide resolved
services/ux-backend/handlers/common.go Outdated Show resolved Hide resolved
services/ux-backend/main.go Outdated Show resolved Hide resolved
tools/csv-merger/csv-merger.go Outdated Show resolved Hide resolved
@leelavg
Copy link
Contributor

leelavg commented Oct 1, 2024

a suggestion, we are seeing this PR for third time, second time it's fine that you weren't able to recover GH (remember you can't create new a/c every-time though) but last time it's better if you can focus on rebasing properly.

yes, GH doesn't have any issue w/ closing & opening a new PR but for reviewers it's kinda hard to relook from the start.

@mrudraia1
Copy link
Author

I have tested this PR, with the latest 4.18 build. I could see the keys getting exchanged when the rotate signing key is clicked in storageclient page.

@leelavg
Copy link
Contributor

leelavg commented Oct 15, 2024

I have tested this PR, with the latest 4.18 build. I could see the keys getting exchanged when the rotate signing key is clicked in storageclient page.

  • ack, only minor comments now. @nb-ohad possible for a final review?

controllers/storagecluster/storageclient.go Outdated Show resolved Hide resolved
@@ -225,7 +225,7 @@ func (r *StorageClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&appsv1.Deployment{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Service{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.ConfigMap{}, builder.MatchEveryOwner, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Secret{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Secret{}, builder.MatchEveryOwner, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a MatchEveryOwner here? Isn't StorageCluster the controller owner of the secret we want to watch?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

controller owner reference set to secrets, ack

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was not addressed as tall! You are matching by controller ownership, it does not make sense to match every owner

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Owner reference were set during creation of secrets in on-boarding job, when the rotate signing key is clicked the re-consiliation is not happening, The onboarding-token needs to be created which is failing.

Any suggestion for this issue.

controllers/util/provider.go Show resolved Hide resolved
controllers/util/provider.go Outdated Show resolved Hide resolved
controllers/util/provider.go Outdated Show resolved Hide resolved
services/ux-backend/main.go Outdated Show resolved Hide resolved
services/ux-backend/main.go Outdated Show resolved Hide resolved
services/ux-backend/main.go Outdated Show resolved Hide resolved
services/ux-backend/main.go Outdated Show resolved Hide resolved
Copy link
Contributor

openshift-ci bot commented Oct 16, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mrudraia1
Once this PR has been reviewed and has the lgtm label, please ask for approval from nb-ohad. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepsm007
Copy link

infra issue
/test images

@@ -225,7 +225,7 @@ func (r *StorageClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&appsv1.Deployment{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Service{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.ConfigMap{}, builder.MatchEveryOwner, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Secret{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Secret{}, builder.MatchEveryOwner, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was not addressed as tall! You are matching by controller ownership, it does not make sense to match every owner

privateKey, err := x509.ParsePKCS1PrivateKey(Block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %v", err)
if privateSecret.Data != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this check: Maps in golang, even when not initialized, support the key access operator.

Suggested change
if privateSecret.Data != nil {

Comment on lines 110 to 120
if privateSecretKey, ok := privateSecret.Data["key"]; ok {
Block, _ := pem.Decode(privateSecretKey)
privateKey, err := x509.ParsePKCS1PrivateKey(Block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %v", err)
}
return privateKey, nil
}
}
return privateKey, nil

return nil, fmt.Errorf("No data found in secret")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer, from code readability and extensibility concerns, if we first handle error conditions and only at the end have the return that is not erroneous. This way you don't need nested returns and extending the function in the future with more checks become easier

Suggested change
if privateSecretKey, ok := privateSecret.Data["key"]; ok {
Block, _ := pem.Decode(privateSecretKey)
privateKey, err := x509.ParsePKCS1PrivateKey(Block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %v", err)
}
return privateKey, nil
}
}
return privateKey, nil
return nil, fmt.Errorf("No data found in secret")
privateSecretKey, ok := privateSecret.Data["key"];
if !ok {
return nil, fmt.Errorf("No data found in secret")
}
Block, _ := pem.Decode(privateSecretKey)
privateKey, err := x509.ParsePKCS1PrivateKey(Block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %v", err)
}
return privateKey, nil

return
}

if onboardingToken, err := util.GenerateClientOnboardingToken(tokenLifetimeInHours, privateKey, storageQuotaInGiB, storageCluster.UID); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is too long, please split over multi-lines

Suggested change
if onboardingToken, err := util.GenerateClientOnboardingToken(tokenLifetimeInHours, privateKey, storageQuotaInGiB, storageCluster.UID); err != nil {
if onboardingToken, err := util.GenerateClientOnboardingToken(
tokenLifetimeInHours,
privateKey,
storageQuotaInGiB,
storageCluster.UID,
); err != nil {

return
}

if onboardingToken, err := util.GeneratePeerOnboardingToken(tokenLifetimeInHours, privateKey, storageCluster.UID); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line too long, see last comment for breaking into multi lines

storageCluster, err := util.GetStorageClusterInNamespace(r.Context(), cl, namespace)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if onboardingToken, err := util.GeneratePeerOnboardingToken(tokenLifetimeInHours, onboardingPrivateKeyFilePath, storageCluster.UID); err != nil {
ctx := context.TODO()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. You should not use a TODO context but a background context
  2. If there is a need to create a context for handling a request it should be created at the topmost level of the request handling and passed down

@@ -87,10 +87,10 @@ func main() {
// Set the Deprecation header
w.Header().Set("Deprecation", "true") // Standard "Deprecation" header
w.Header().Set("Link", "/onboarding/client-tokens; rel=\"alternate\"")
clienttokens.HandleMessage(w, r, config.tokenLifetimeInHours, cl, namespace)
clienttokens.HandleMessage(r.Context(), w, r, config.tokenLifetimeInHours, cl, namespace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not use the same context for main and for specific request handling. Each request should initialize its own background context that will be used during the entire request.

})
http.HandleFunc("/onboarding/client-tokens", func(w http.ResponseWriter, r *http.Request) {
clienttokens.HandleMessage(w, r, config.tokenLifetimeInHours, cl, namespace)
clienttokens.HandleMessage(r.Context(), w, r, config.tokenLifetimeInHours, cl, namespace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as last comment

@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 12, 2024
@openshift-merge-robot openshift-merge-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants