-
Notifications
You must be signed in to change notification settings - Fork 184
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 onboarding-token validation #2715
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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{})). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change is needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will trigger reconcilation to all owners whenever their is change in secrets. |
||
Owns(&routev1.Route{}). | ||
Owns(&templatev1.Template{}). | ||
Watches(&storagev1.StorageClass{}, enqueueStorageClusterRequest). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func ReadPrivateKey(cl client.Client) (*rsa.PrivateKey, error) { | ||
klog.Info("Getting the Pem key") | ||
ctx := context.Background() | ||
|
||
operatorNamespace, err := GetOperatorNamespace() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to get operator namespace: %v", err) | ||
} | ||
|
||
privateSecret := &corev1.Secret{} | ||
privateSecret.Name = onboardingValidationPrivateKeySecretName | ||
privateSecret.Namespace = operatorNamespace | ||
|
||
err = cl.Get(ctx, client.ObjectKeyFromObject(privateSecret), privateSecret) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get private secret: %v", err) | ||
} | ||
|
||
Block, _ := pem.Decode(privateSecret.Data["key"]) | ||
privateKey, err := x509.ParsePKCS1PrivateKey(Block.Bytes) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse private key: %v", err) | ||
} | ||
|
||
return privateKey, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,22 +5,19 @@ import ( | |
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/sha256" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"encoding/json" | ||
"encoding/pem" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/red-hat-storage/ocs-operator/v4/services" | ||
) | ||
|
||
// GenerateOnboardingToken generates a token valid for a duration of "tokenLifetimeInHours". | ||
// The token content is predefined and signed by the private key which'll be read from supplied "privateKeyPath". | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are updating the parameter type so I would suggest this comment should be update accordingly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
// The token content is predefined and signed by the private key which'll be read from supplied "privateKey" | ||
// The storageQuotaInGiB is optional, and it is used to limit the storage of PVC in the application cluster. | ||
func GenerateOnboardingToken(tokenLifetimeInHours int, privateKeyPath string, storageQuotaInGiB *uint) (string, error) { | ||
func GenerateOnboardingToken(tokenLifetimeInHours int, privateKey *rsa.PrivateKey, storageQuotaInGiB *uint) (string, error) { | ||
tokenExpirationDate := time.Now(). | ||
Add(time.Duration(tokenLifetimeInHours) * time.Hour). | ||
Unix() | ||
|
@@ -46,11 +43,6 @@ func GenerateOnboardingToken(tokenLifetimeInHours int, privateKeyPath string, st | |
return "", fmt.Errorf("failed to hash onboarding token payload: %v", err) | ||
} | ||
|
||
privateKey, err := readAndDecodePrivateKey(privateKeyPath) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read and decode private key: %v", err) | ||
} | ||
|
||
msgHashSum := msgHash.Sum(nil) | ||
// In order to generate the signature, we provide a random number generator, | ||
// our private key, the hashing algorithm that we used, and the hash sum | ||
|
@@ -63,17 +55,3 @@ func GenerateOnboardingToken(tokenLifetimeInHours int, privateKeyPath string, st | |
encodedSignature := base64.StdEncoding.EncodeToString(signature) | ||
return fmt.Sprintf("%s.%s", encodedPayload, encodedSignature), nil | ||
} | ||
|
||
func readAndDecodePrivateKey(privateKeyPath string) (*rsa.PrivateKey, error) { | ||
pemString, err := os.ReadFile(privateKeyPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read private key: %v", err) | ||
} | ||
|
||
Block, _ := pem.Decode(pemString) | ||
privateKey, err := x509.ParsePKCS1PrivateKey(Block.Bytes) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse private key: %v", err) | ||
} | ||
return privateKey, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ import ( | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const onboardingValidationPrivateKeySecretName = "onboarding-private-key" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its also defined in storageclient.go There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed unused code |
||
|
||
func RemoveDuplicatesFromStringSlice(slice []string) []string { | ||
keys := make(map[string]bool) | ||
list := []string{} | ||
|
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.
If there is an err while reading the private key we should return the err