Skip to content

Commit

Permalink
Merge pull request #2844 from rewantsoni/tokens
Browse files Browse the repository at this point in the history
onboardingTicket: add uid to the ticket
  • Loading branch information
openshift-merge-bot[bot] authored Nov 6, 2024
2 parents 9295290 + ab01470 commit 502f1dd
Show file tree
Hide file tree
Showing 19 changed files with 405 additions and 96 deletions.
2 changes: 1 addition & 1 deletion controllers/storagecluster/storageclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *storageClient) ensureCreated(r *StorageClusterReconciler, storagecluste
storageClient.Name = storagecluster.Name
_, err := controllerutil.CreateOrUpdate(r.ctx, r.Client, storageClient, func() error {
if storageClient.Status.ConsumerID == "" {
token, err := util.GenerateClientOnboardingToken(tokenLifetimeInHours, onboardingPrivateKeyFilePath, nil)
token, err := util.GenerateClientOnboardingToken(tokenLifetimeInHours, onboardingPrivateKeyFilePath, nil, storagecluster.UID)
if err != nil {
return fmt.Errorf("unable to generate onboarding token: %v", err)
}
Expand Down
36 changes: 36 additions & 0 deletions controllers/util/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import (
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

const (
Expand Down Expand Up @@ -166,3 +169,36 @@ func GenerateNameForNonResilientCephBlockPoolSC(initData *ocsv1.StorageCluster)
}
return fmt.Sprintf("%s-ceph-non-resilient-rbd", initData.Name)
}

func GetStorageClusterInNamespace(ctx context.Context, cl client.Client, namespace string) (*ocsv1.StorageCluster, error) {
storageClusterList := &ocsv1.StorageClusterList{}
err := cl.List(ctx, storageClusterList, client.InNamespace(namespace), client.Limit(1))
if err != nil {
return nil, fmt.Errorf("unable to list storageCluster(s) in namespace %s: %v", namespace, err)
}

if len(storageClusterList.Items) == 0 {
return nil, fmt.Errorf("no storageCluster found in namespace %s", namespace)
}
if storageClusterList.Items[0].Status.Phase == PhaseIgnored {
return nil, fmt.Errorf("storageCluster with Phase 'Ignored' found. Please delete the storageCluster to proceed")
}

return &storageClusterList.Items[0], nil
}

func NewK8sClient(scheme *runtime.Scheme) (client.Client, error) {
klog.Info("Setting up k8s client")

config, err := config.GetConfig()
if err != nil {
return nil, err
}

k8sClient, err := client.New(config, client.Options{Scheme: scheme})
if err != nil {
return nil, err
}

return k8sClient, nil
}
10 changes: 7 additions & 3 deletions controllers/util/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import (
"os"
"time"

"github.com/google/uuid"
"github.com/red-hat-storage/ocs-operator/v4/services"

"github.com/google/uuid"
"k8s.io/apimachinery/pkg/types"
)

// GenerateClientOnboardingToken generates a ocs-client 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".
// The storageQuotaInGiB is optional, and it is used to limit the storage of PVC in the application cluster.
func GenerateClientOnboardingToken(tokenLifetimeInHours int, privateKeyPath string, storageQuotainGib *uint) (string, error) {
func GenerateClientOnboardingToken(tokenLifetimeInHours int, privateKeyPath string, storageQuotainGib *uint, storageClusterUID types.UID) (string, error) {
tokenExpirationDate := time.Now().
Add(time.Duration(tokenLifetimeInHours) * time.Hour).
Unix()
Expand All @@ -30,6 +32,7 @@ func GenerateClientOnboardingToken(tokenLifetimeInHours int, privateKeyPath stri
ExpirationDate: tokenExpirationDate,
SubjectRole: services.ClientRole,
StorageQuotaInGiB: storageQuotainGib,
StorageCluster: storageClusterUID,
}

token, err := encodeAndSignOnboardingToken(privateKeyPath, ticket)
Expand All @@ -41,7 +44,7 @@ func GenerateClientOnboardingToken(tokenLifetimeInHours int, privateKeyPath stri

// GeneratePeerOnboardingToken generates a ocs-peer 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".
func GeneratePeerOnboardingToken(tokenLifetimeInHours int, privateKeyPath string) (string, error) {
func GeneratePeerOnboardingToken(tokenLifetimeInHours int, privateKeyPath string, storageClusterUID types.UID) (string, error) {
tokenExpirationDate := time.Now().
Add(time.Duration(tokenLifetimeInHours) * time.Hour).
Unix()
Expand All @@ -50,6 +53,7 @@ func GeneratePeerOnboardingToken(tokenLifetimeInHours int, privateKeyPath string
ID: uuid.New().String(),
ExpirationDate: tokenExpirationDate,
SubjectRole: services.PeerRole,
StorageCluster: storageClusterUID,
}
token, err := encodeAndSignOnboardingToken(privateKeyPath, ticket)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ spec:
- name: ONBOARDING_TOKEN_LIFETIME
- name: UX_BACKEND_PORT
- name: TLS_ENABLED
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
image: quay.io/ocs-dev/ocs-operator:latest
imagePullPolicy: IfNotPresent
name: ux-backend-server
Expand Down
7 changes: 7 additions & 0 deletions deploy/ocs-operator/manifests/ux_backend_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ rules:
verbs:
- get
- list
- apiGroups:
- ocs.openshift.io
resources:
- storageclusters
verbs:
- get
- list

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions metrics/vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ sigs.k8s.io/container-object-storage-interface-api/apis/objectstorage/v1alpha1
## explicit; go 1.22.0
sigs.k8s.io/controller-runtime/pkg/client
sigs.k8s.io/controller-runtime/pkg/client/apiutil
sigs.k8s.io/controller-runtime/pkg/client/config
sigs.k8s.io/controller-runtime/pkg/event
sigs.k8s.io/controller-runtime/pkg/internal/log
sigs.k8s.io/controller-runtime/pkg/log
Expand Down
Loading

0 comments on commit 502f1dd

Please sign in to comment.