From 2b1cc4c7b82ae9f5dd85f06e78e2e0e6ca43bbf6 Mon Sep 17 00:00:00 2001 From: travisn Date: Tue, 15 Aug 2023 17:01:30 -0600 Subject: [PATCH] object: allow creating an object user in different namespace The object user was previously required to be created in the same namespace as the object store and the cluster. Now, the object user can be reconciled even in a different namespace from the cluster and object store. The namespace would be specified in the object user CR. Signed-off-by: travisn --- .../Object-Storage/ceph-object-store-crd.md | 8 ++- .../ceph-object-store-user-crd.md | 3 + Documentation/CRDs/specification.md | 58 ++++++++++++++++ .../charts/rook-ceph/templates/resources.yaml | 8 +++ deploy/examples/crds.yaml | 8 +++ deploy/examples/object-user.yaml | 20 +++--- deploy/examples/object.yaml | 4 ++ pkg/apis/ceph.rook.io/v1/types.go | 12 ++++ pkg/operator/ceph/object/user/controller.go | 44 ++++++++++-- .../ceph/object/user/controller_test.go | 67 +++++++++++++++++++ 10 files changed, 217 insertions(+), 15 deletions(-) diff --git a/Documentation/CRDs/Object-Storage/ceph-object-store-crd.md b/Documentation/CRDs/Object-Storage/ceph-object-store-crd.md index 69c45428b..6bb9a5e92 100644 --- a/Documentation/CRDs/Object-Storage/ceph-object-store-crd.md +++ b/Documentation/CRDs/Object-Storage/ceph-object-store-crd.md @@ -82,7 +82,13 @@ When the `zone` section is set pools with the object stores name will not be cre * `metadataPool`: The settings used to create all of the object store metadata pools. Must use replication. * `dataPool`: The settings to create the object store data pool. Can use replication or erasure coding. -* `preservePoolsOnDelete`: If it is set to 'true' the pools used to support the object store will remain when the object store will be deleted. This is a security measure to avoid accidental loss of data. It is set to 'false' by default. If not specified is also deemed as 'false'. +* `preservePoolsOnDelete`: If it is set to 'true' the pools used to support the object store will remain when the object store + will be deleted. This is a security measure to avoid accidental loss of data. It is set to 'false' by default. If not specified + is also deemed as 'false'. +* `allowUsersInNamespaces`: If a CephObjectStoreUser is created in a namespace other than the Rook cluster namespace, + the namespace must be added to this list of allowed namespaces, or specify "*" to allow all namespaces. + This is useful for applications that need object store credentials to be created in their own namespace, + where neither OBCs nor COSI is being used to create buckets. The default is empty. ## Gateway Settings diff --git a/Documentation/CRDs/Object-Storage/ceph-object-store-user-crd.md b/Documentation/CRDs/Object-Storage/ceph-object-store-user-crd.md index 039d2d948..7824746e4 100644 --- a/Documentation/CRDs/Object-Storage/ceph-object-store-user-crd.md +++ b/Documentation/CRDs/Object-Storage/ceph-object-store-user-crd.md @@ -36,6 +36,9 @@ spec: * `store`: The object store in which the user will be created. This matches the name of the objectstore CRD. * `displayName`: The display name which will be passed to the `radosgw-admin user create` command. +* `clusterNamespace`: The namespace where the parent CephCluster and CephObjectStore are found. If not specified, + the user must be in the same namespace as the cluster and object store. + To enable this feature, the CephObjectStore allowUsersInNamespaces must include the namespace of this user. * `quotas`: This represents quota limitation can be set on the user. Please refer [here](https://docs.ceph.com/en/latest/radosgw/admin/#quota-management) for details. * `maxBuckets`: The maximum bucket limit for the user. * `maxSize`: Maximum size limit of all objects across all the user's buckets. diff --git a/Documentation/CRDs/specification.md b/Documentation/CRDs/specification.md index 9c509aecf..aeafb2beb 100644 --- a/Documentation/CRDs/specification.md +++ b/Documentation/CRDs/specification.md @@ -1872,6 +1872,23 @@ ObjectStoreSecuritySpec

Security represents security settings

+ + +allowUsersInNamespaces
+ +[]string + + + +(Optional) +

The list of allowed namespaces in addition to the object store namespace +where ceph object store users may be created. Specify “*” to allow all +namespaces, otherwise list individual namespaces that are to be allowed. +This is useful for applications that need object store credentials +to be created in their own namespace, where neither OBCs nor COSI +is being used to create buckets. The default is empty.

+ + @@ -1996,6 +2013,18 @@ ObjectUserQuotaSpec (Optional) + + +clusterNamespace
+ +string + + + +(Optional) +

The namespace where the parent CephCluster and CephObjectStore are found

+ + @@ -8616,6 +8645,23 @@ ObjectStoreSecuritySpec

Security represents security settings

+ + +allowUsersInNamespaces
+ +[]string + + + +(Optional) +

The list of allowed namespaces in addition to the object store namespace +where ceph object store users may be created. Specify “*” to allow all +namespaces, otherwise list individual namespaces that are to be allowed. +This is useful for applications that need object store credentials +to be created in their own namespace, where neither OBCs nor COSI +is being used to create buckets. The default is empty.

+ +

ObjectStoreStatus @@ -8774,6 +8820,18 @@ ObjectUserQuotaSpec (Optional) + + +clusterNamespace
+ +string + + + +(Optional) +

The namespace where the parent CephCluster and CephObjectStore are found

+ +

ObjectStoreUserStatus diff --git a/deploy/charts/rook-ceph/templates/resources.yaml b/deploy/charts/rook-ceph/templates/resources.yaml index 3ad01b467..75c8ad415 100644 --- a/deploy/charts/rook-ceph/templates/resources.yaml +++ b/deploy/charts/rook-ceph/templates/resources.yaml @@ -9907,6 +9907,11 @@ spec: spec: description: ObjectStoreSpec represent the spec of a pool properties: + allowUsersInNamespaces: + description: The list of allowed namespaces in addition to the object store namespace where ceph object store users may be created. Specify "*" to allow all namespaces, otherwise list individual namespaces that are to be allowed. This is useful for applications that need object store credentials to be created in their own namespace, where neither OBCs nor COSI is being used to create buckets. The default is empty. + items: + type: string + type: array dataPool: description: The data pool settings nullable: true @@ -11442,6 +11447,9 @@ spec: - read, write type: string type: object + clusterNamespace: + description: The namespace where the parent CephCluster and CephObjectStore are found + type: string displayName: description: The display name for the ceph users type: string diff --git a/deploy/examples/crds.yaml b/deploy/examples/crds.yaml index dc0466d37..213f53db4 100644 --- a/deploy/examples/crds.yaml +++ b/deploy/examples/crds.yaml @@ -9898,6 +9898,11 @@ spec: spec: description: ObjectStoreSpec represent the spec of a pool properties: + allowUsersInNamespaces: + description: The list of allowed namespaces in addition to the object store namespace where ceph object store users may be created. Specify "*" to allow all namespaces, otherwise list individual namespaces that are to be allowed. This is useful for applications that need object store credentials to be created in their own namespace, where neither OBCs nor COSI is being used to create buckets. The default is empty. + items: + type: string + type: array dataPool: description: The data pool settings nullable: true @@ -11432,6 +11437,9 @@ spec: - read, write type: string type: object + clusterNamespace: + description: The namespace where the parent CephCluster and CephObjectStore are found + type: string displayName: description: The display name for the ceph users type: string diff --git a/deploy/examples/object-user.yaml b/deploy/examples/object-user.yaml index 8ae3d2413..b8c8c1cd5 100644 --- a/deploy/examples/object-user.yaml +++ b/deploy/examples/object-user.yaml @@ -13,13 +13,17 @@ spec: displayName: "my display name" # Quotas set on the user # quotas: - # maxBuckets: 100 - # maxSize: 10G - # maxObjects: 10000 + # maxBuckets: 100 + # maxSize: 10G + # maxObjects: 10000 # Additional permissions given to the user # capabilities: - # user: "*" - # bucket: "*" - # metadata: "*" - # usage: "*" - # zone: "*" + # user: "*" + # bucket: "*" + # metadata: "*" + # usage: "*" + # zone: "*" + # If the CephObjectStoreUser is created in a namespace other than the Rook cluster namespace, + # specify the namespace where the cluster and object store are found. + # "allowUsersInNamespaces" must include this namespace to enable this feature. + # clusterNamespace: rook-ceph diff --git a/deploy/examples/object.yaml b/deploy/examples/object.yaml index dcaf5e1f7..8ab1a801f 100644 --- a/deploy/examples/object.yaml +++ b/deploy/examples/object.yaml @@ -109,6 +109,10 @@ spec: disabled: false readinessProbe: disabled: false + # If a CephObjectStoreUser is created in a namespace other than the Rook cluster namespace, + # the namespace must be added to the list of allowed namespaces, or specify "*" to allow all namespaces. + # allowUsersInNamespaces: + # - other-namespace # security oriented settings # security: # To enable the Server Side Encryption configuration properly don't forget to uncomment the Secret at the end of the file diff --git a/pkg/apis/ceph.rook.io/v1/types.go b/pkg/apis/ceph.rook.io/v1/types.go index 40136d080..132eb89a2 100755 --- a/pkg/apis/ceph.rook.io/v1/types.go +++ b/pkg/apis/ceph.rook.io/v1/types.go @@ -1387,6 +1387,15 @@ type ObjectStoreSpec struct { // +optional // +nullable Security *ObjectStoreSecuritySpec `json:"security,omitempty"` + + // The list of allowed namespaces in addition to the object store namespace + // where ceph object store users may be created. Specify "*" to allow all + // namespaces, otherwise list individual namespaces that are to be allowed. + // This is useful for applications that need object store credentials + // to be created in their own namespace, where neither OBCs nor COSI + // is being used to create buckets. The default is empty. + // +optional + AllowUsersInNamespaces []string `json:"allowUsersInNamespaces,omitempty"` } // ObjectHealthCheckSpec represents the health check of an object store @@ -1597,6 +1606,9 @@ type ObjectStoreUserSpec struct { // +optional // +nullable Quotas *ObjectUserQuotaSpec `json:"quotas,omitempty"` + // The namespace where the parent CephCluster and CephObjectStore are found + // +optional + ClusterNamespace string `json:"clusterNamespace,omitempty"` } // Additional admin-level capabilities for the Ceph object store user diff --git a/pkg/operator/ceph/object/user/controller.go b/pkg/operator/ceph/object/user/controller.go index a957a77c4..a3fd7d66f 100644 --- a/pkg/operator/ceph/object/user/controller.go +++ b/pkg/operator/ceph/object/user/controller.go @@ -162,13 +162,16 @@ func (r *ReconcileObjectStoreUser) reconcile(request reconcile.Request) (reconci return reconcile.Result{}, *cephObjectStoreUser, errors.Wrap(err, "failed to add finalizer") } + clusterNamespace := request.NamespacedName + clusterNamespace.Namespace = clusterStoreNamespace(cephObjectStoreUser) + // The CR was just created, initializing status fields if cephObjectStoreUser.Status == nil { r.updateStatus(k8sutil.ObservedGenerationNotAvailable, request.NamespacedName, k8sutil.EmptyStatus) } // Make sure a CephCluster is present otherwise do nothing - cephCluster, isReadyToReconcile, cephClusterExists, reconcileResponse := opcontroller.IsReadyToReconcile(r.opManagerContext, r.client, request.NamespacedName, controllerName) + cephCluster, isReadyToReconcile, cephClusterExists, reconcileResponse := opcontroller.IsReadyToReconcile(r.opManagerContext, r.client, clusterNamespace, controllerName) if !isReadyToReconcile { // This handles the case where the Ceph Cluster is gone and we want to delete that CR // We skip the deleteUser() function since everything is gone already @@ -191,7 +194,7 @@ func (r *ReconcileObjectStoreUser) reconcile(request reconcile.Request) (reconci r.cephClusterSpec = &cephCluster.Spec // Populate clusterInfo during each reconcile - r.clusterInfo, _, _, err = opcontroller.LoadClusterInfo(r.context, r.opManagerContext, request.NamespacedName.Namespace, r.cephClusterSpec) + r.clusterInfo, _, _, err = opcontroller.LoadClusterInfo(r.context, r.opManagerContext, clusterNamespace.Namespace, r.cephClusterSpec) if err != nil { return reconcile.Result{}, *cephObjectStoreUser, errors.Wrap(err, "failed to populate cluster info") } @@ -211,7 +214,7 @@ func (r *ReconcileObjectStoreUser) reconcile(request reconcile.Request) (reconci return reconcile.Result{}, *cephObjectStoreUser, nil } logger.Debugf("ObjectStore resource not ready in namespace %q, retrying in %q. %v", - request.NamespacedName.Namespace, opcontroller.WaitForRequeueIfCephClusterNotReady.RequeueAfter.String(), err) + clusterNamespace.Namespace, opcontroller.WaitForRequeueIfCephClusterNotReady.RequeueAfter.String(), err) r.updateStatus(k8sutil.ObservedGenerationNotAvailable, request.NamespacedName, k8sutil.ReconcileFailedStatus) return opcontroller.WaitForRequeueIfCephClusterNotReady, *cephObjectStoreUser, err } @@ -222,7 +225,7 @@ func (r *ReconcileObjectStoreUser) reconcile(request reconcile.Request) (reconci // DELETE: the CR was deleted if !cephObjectStoreUser.GetDeletionTimestamp().IsZero() { - logger.Debugf("deleting pool %q", cephObjectStoreUser.Name) + logger.Debugf("deleting object store user %q", request.NamespacedName) r.recorder.Eventf(cephObjectStoreUser, v1.EventTypeNormal, string(cephv1.ReconcileStarted), "deleting CephObjectStoreUser %q", cephObjectStoreUser.Name) err := r.deleteUser(cephObjectStoreUser) @@ -383,6 +386,16 @@ func (r *ReconcileObjectStoreUser) initializeObjectStoreContext(u *cephv1.CephOb return errors.Wrapf(err, "failed to get object store %q", u.Spec.Store) } + // Check if the object store allows users to be created in any namespace + if u.Spec.ClusterNamespace != "" && u.Spec.ClusterNamespace != u.Namespace { + if !userInNamespaceAllowed(u.Namespace, store.Spec.AllowUsersInNamespaces) { + return fmt.Errorf( + "object store %q does not allow creating users namespace %q, the namespace must first be added to allowUsersInNamespaces", + u.Spec.Store, + u.Namespace) + } + } + objContext, err := object.NewMultisiteContext(r.context, r.clusterInfo, store) if err != nil { return errors.Wrapf(err, "Multisite failed to set on object context for object store user") @@ -397,6 +410,17 @@ func (r *ReconcileObjectStoreUser) initializeObjectStoreContext(u *cephv1.CephOb return nil } +func userInNamespaceAllowed(requestedNamespace string, allowedNamespaces []string) bool { + // Check if there is access to create the user in another namespace + for _, allowedNamespace := range allowedNamespaces { + if allowedNamespace == "*" || allowedNamespace == requestedNamespace { + logger.Debugf("allow creating object user in namespace %q", requestedNamespace) + return true + } + } + return false +} + func generateUserCaps(user admin.User) string { var caps string for _, c := range user.Caps { @@ -556,7 +580,7 @@ func (r *ReconcileObjectStoreUser) objectStoreInitialized(cephObjectStoreUser *c // check if at least one pod is running if len(pods.Items) > 0 { - logger.Debugf("CephObjectStore %q is running with %d pods", cephObjectStoreUser.Name, len(pods.Items)) + logger.Debugf("%d RGW pods found where object store user %q is created", len(pods.Items), cephObjectStoreUser.Name) return nil } @@ -590,7 +614,7 @@ func (r *ReconcileObjectStoreUser) getRgwPodList(cephObjectStoreUser *cephv1.Cep // check if ObjectStore is initialized // rook does this by starting the RGW pod(s) listOpts := []client.ListOption{ - client.InNamespace(cephObjectStoreUser.Namespace), + client.InNamespace(clusterStoreNamespace(cephObjectStoreUser)), client.MatchingLabels(labelsForRgw(cephObjectStoreUser.Spec.Store)), } @@ -605,6 +629,14 @@ func (r *ReconcileObjectStoreUser) getRgwPodList(cephObjectStoreUser *cephv1.Cep return pods, nil } +// Namespace where the object store and cluster are expected to be found +func clusterStoreNamespace(user *cephv1.CephObjectStoreUser) string { + if user.Spec.ClusterNamespace != "" { + return user.Spec.ClusterNamespace + } + return user.Namespace +} + // Delete the user func (r *ReconcileObjectStoreUser) deleteUser(u *cephv1.CephObjectStoreUser) error { err := r.objContext.AdminOpsClient.RemoveUser(r.opManagerContext, admin.User{ID: u.Name}) diff --git a/pkg/operator/ceph/object/user/controller_test.go b/pkg/operator/ceph/object/user/controller_test.go index 0cfd4df6f..4094b1a71 100644 --- a/pkg/operator/ceph/object/user/controller_test.go +++ b/pkg/operator/ceph/object/user/controller_test.go @@ -329,6 +329,73 @@ func TestCephObjectStoreUserController(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "Ready", objectUser.Status.Phase, objectUser) }) + + t.Run("cluster and object store in different namespace", func(t *testing.T) { + cephCluster = &cephv1.CephCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespace, + Namespace: namespace, + }, + Status: cephv1.ClusterStatus{ + Phase: k8sutil.ReadyStatus, + CephStatus: &cephv1.CephStatus{ + Health: "HEALTH_OK", + }, + }, + } + cephObjectStore.Spec.AllowUsersInNamespaces = []string{"*"} + + // Create a fake client to mock API calls. + objectUser.Spec.ClusterNamespace = namespace + objectUser.Namespace = "foo" + req.Namespace = "foo" + object = []runtime.Object{cephObjectStore, objectUser, cephCluster} + cl = fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build() + + rgwPod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{ + Name: "rook-ceph-rgw-my-store-a-5fd6fb4489-xv65v", + Namespace: namespace, + Labels: map[string]string{k8sutil.AppAttr: appName, "rgw": "my-store"}}} + + // Create a user in a different namespace, and where the cephcluster does exist + r = &ReconcileObjectStoreUser{client: cl, scheme: s, context: c, opManagerContext: ctx, recorder: record.NewFakeRecorder(5)} + err := r.client.Create(context.TODO(), rgwPod) + assert.NoError(t, err) + res, err := r.Reconcile(ctx, req) + assert.NoError(t, err) + assert.False(t, res.Requeue) + + // Disallow creating a user in a different namespace + cephObjectStore.Spec.AllowUsersInNamespaces = []string{} + cl = fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build() + r = &ReconcileObjectStoreUser{client: cl, scheme: s, context: c, opManagerContext: ctx, recorder: record.NewFakeRecorder(5)} + res, err = r.Reconcile(ctx, req) + assert.NoError(t, err) + assert.True(t, res.Requeue) + + objectUser.Spec.ClusterNamespace = "" + object = []runtime.Object{objectUser, cephCluster} + cl = fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build() + + // Create a user in a different namespace, but where the cephcluster doesn't exist + r = &ReconcileObjectStoreUser{client: cl, scheme: s, context: c, opManagerContext: ctx, recorder: record.NewFakeRecorder(5)} + res, err = r.Reconcile(ctx, req) + assert.NoError(t, err) + assert.True(t, res.Requeue) + + req.Namespace = namespace + objectUser.Namespace = namespace + }) + + t.Run("users allowed in other namespace", func(t *testing.T) { + assert.False(t, userInNamespaceAllowed("foo", []string{})) + assert.False(t, userInNamespaceAllowed("foo", []string{"bar"})) + assert.False(t, userInNamespaceAllowed("foo", []string{"bar", "baz"})) + assert.True(t, userInNamespaceAllowed("foo", []string{"*"})) + assert.True(t, userInNamespaceAllowed("foo", []string{"bar", "*"})) + assert.True(t, userInNamespaceAllowed("foo", []string{"foo"})) + assert.True(t, userInNamespaceAllowed("foo", []string{"bar", "foo"})) + }) } func TestBuildUpdateStatusInfo(t *testing.T) {