rgw: fix dashboard admin creation for multiple object stores

Check the radosgw-admin realm user list per object store instead of relying
on the ceph dashboard get-rgw-api- command.

Resolves #9099

Signed-off-by: Alexander Trost <galexrt@googlemail.com>
(cherry picked from commit 5d03061d47)
Signed-off-by: Alexander Trost <galexrt@googlemail.com>
This commit is contained in:
Alexander Trost committed 2022-04-27 20:08:26 +02:00
1 parent 4fe80d39b2
commit cf139d2fc8
12 files changed
+274 -58

No files matched your search

+1 -1
View File
@@ -441,7 +441,7 @@ func GetAdminOPSUserCredentials(objContext *Context, spec *cephv1.ObjectStoreSpe
DisplayName: &rgwAdminOpsUserDisplayName,
AdminOpsUser: true,
}
logger.Debugf("creating s3 user object %q for object store %q", userConfig.UserID, ns)
logger.Debugf("creating s3 user object %q for object store %q", userConfig.UserID, objContext.Name)
forceUserCreation := false
// If the cluster where we are running the rgw user create for the admin ops user is configured
+4 -4
View File
@@ -369,12 +369,12 @@ func (r *ReconcileCephObjectStore) reconcileCreateObjectStore(cephObjectStore *c
client: r.client,
ownerInfo: ownerInfo,
}
objContext := NewContext(r.context, r.clusterInfo, cephObjectStore.Name)
objContext.UID = string(cephObjectStore.UID)
objContext, err := NewMultisiteContext(r.context, r.clusterInfo, cephObjectStore)
if err != nil {
return r.setFailedStatus(k8sutil.ObservedGenerationNotAvailable, namespacedName, "failed to setup object store context", err)
}
objContext.CephClusterSpec = cluster
var err error
if cephObjectStore.Spec.IsExternal() {
logger.Info("reconciling external object store")
+10 -4
View File
@@ -19,13 +19,14 @@ package object
import (
"context"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"net/http"
"os"
"reflect"
"testing"
"time"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/coreos/pkg/capnslog"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
@@ -676,9 +677,14 @@ func TestCephObjectStoreControllerMultisite(t *testing.T) {
clientset := test.New(t, 3)
c := &clusterd.Context{
Executor: executor,
RookClientset: rookclient.NewSimpleClientset(),
Clientset: clientset,
Executor: executor,
RookClientset: rookclient.NewSimpleClientset(
objectRealm,
objectZoneGroup,
objectZone,
objectStore,
),
Clientset: clientset,
}
// Register operator types with the runtime scheme.
+75 -16
View File
@@ -817,22 +817,84 @@ func GetObjectBucketProvisioner(data map[string]string, namespace string) string
return provName
}
// CheckDashboardUser returns true if the user is configure else return false
func checkDashboardUser(context *Context) (bool, error) {
args := []string{"dashboard", "get-rgw-api-access-key"}
cephCmd := cephclient.NewCephCommand(context.Context, context.clusterInfo, args)
out, err := cephCmd.Run()
// CheckDashboardUser returns true if the dashboard user exists and has the same credentials as the given user, else return false
func checkDashboardUser(context *Context, user ObjectUser) (bool, error) {
dUser, errId, err := GetUser(context, DashboardUser)
if string(out) != "" {
return true, err
// If not found or "none" error, all is good to not return the error
if errId == RGWErrorNone {
// If the access key or secret key is not the same as the given user, return false
if user.AccessKey != nil && *user.AccessKey != *dUser.AccessKey {
return false, nil
}
if user.SecretKey != nil && *user.SecretKey != *dUser.SecretKey {
return false, nil
}
return true, nil
} else if errId == RGWErrorNotFound {
return false, nil
}
return false, err
}
// retrieveDashboardAPICredentials Retrieves the dashboard's access and secret key and set it on the given ObjectUser
func retrieveDashboardAPICredentials(context *Context, user *ObjectUser) error {
args := []string{"dashboard", "get-rgw-api-access-key"}
cephCmd := cephclient.NewCephCommand(context.Context, context.clusterInfo, args)
out, err := cephCmd.Run()
if err != nil {
return err
}
if string(out) != "" {
accessKey := string(out)
user.AccessKey = &accessKey
}
args = []string{"dashboard", "get-rgw-api-secret-key"}
cephCmd = cephclient.NewCephCommand(context.Context, context.clusterInfo, args)
out, err = cephCmd.Run()
if err != nil {
return err
}
if string(out) != "" {
secretKey := string(out)
user.SecretKey = &secretKey
}
return nil
}
func getDashboardUser(context *Context) (ObjectUser, error) {
user := ObjectUser{
UserID: DashboardUser,
DisplayName: &DashboardUser,
SystemUser: true,
}
if !context.CephClusterSpec.External.Enable {
// Retrieve RGW Dashboard credentials if some are already set
if err := retrieveDashboardAPICredentials(context, &user); err != nil {
return user, errors.Wrapf(err, "failed to retrieve RGW Dashboard credentials for %q user", DashboardUser)
}
}
return user, nil
}
func enableRGWDashboard(context *Context) error {
logger.Info("enabling rgw dashboard")
checkDashboard, err := checkDashboardUser(context)
user, err := getDashboardUser(context)
if err != nil {
logger.Debug("failed to get current dashboard user")
return err
}
checkDashboard, err := checkDashboardUser(context, user)
if err != nil {
logger.Debug("Unable to fetch dashboard user key for RGW, hence skipping")
return nil
@@ -841,19 +903,16 @@ func enableRGWDashboard(context *Context) error {
logger.Debug("RGW Dashboard is already enabled")
return nil
}
user := ObjectUser{
UserID: DashboardUser,
DisplayName: &DashboardUser,
SystemUser: true,
}
// TODO:
// Use admin ops user instead!
// It's safe to create the user with the force flag regardless if the cluster's dashboard is
// configured as a secondary rgw site. The creation will return the user already exists and we
// will just fetch it (it has been created by the primary cluster)
u, errCode, err := CreateUser(context, user, true)
if err != nil || errCode != 0 {
return errors.Wrapf(err, "failed to create user %q", DashboardUser)
u, errCode, err := CreateOrRecreateUserIfExists(context, user, true)
if err != nil || errCode != RGWErrorNone {
// Handle already exists ErrorCodeFileExists
return errors.Wrapf(err, "failed to create/ re-create user %q", DashboardUser)
}
var accessArgs, secretArgs []string
+124 -18
View File
@@ -68,7 +68,10 @@ const (
"max_objects": -1
}
}`
//#nosec G101 -- The credentials are just for the unit tests
access_key = "VFKF8SSU9L3L2UR03Z8C"
//#nosec G101 -- The credentials are just for the unit tests
secret_key = "5U4e2MkXHgXstfWkxGZOI6AXDfVUkDDHM7Dwc3mY"
)
func TestReconcileRealm(t *testing.T) {
@@ -235,7 +238,7 @@ func TestGetObjectBucketProvisioner(t *testing.T) {
})
}
func TestDashboard(t *testing.T) {
func TestCheckDashboardUser(t *testing.T) {
storeName := "myobject"
executor := &exectest.MockExecutor{
MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) {
@@ -243,7 +246,9 @@ func TestDashboard(t *testing.T) {
},
MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, args ...string) (string, error) {
if args[0] == "user" {
return dashboardAdminCreateJSON, nil
if args[1] == "info" {
return "no user info saved", nil
}
}
return "", nil
},
@@ -255,21 +260,131 @@ func TestDashboard(t *testing.T) {
},
storeName)
checkdashboard, err := checkDashboardUser(objContext)
// Scenario 1: No user exists yet
user, err := getDashboardUser(objContext)
assert.NoError(t, err)
assert.NotNil(t, user)
assert.Nil(t, user.AccessKey)
assert.Nil(t, user.SecretKey)
checkdashboard, err := checkDashboardUser(objContext, user)
assert.NoError(t, err)
assert.False(t, checkdashboard)
// Scenario 2: User exists and the current dashboard credentials are the same
objContext.Context.Executor = &exectest.MockExecutor{
MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) {
if args[0] == "dashboard" {
if args[1] == "get-rgw-api-access-key" {
return access_key, nil
} else if args[1] == "get-rgw-api-secret-key" {
return secret_key, nil
}
}
return "", nil
},
MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, args ...string) (string, error) {
if args[0] == "user" {
if args[1] == "info" {
return dashboardAdminCreateJSON, nil
}
}
return "", nil
},
}
user, err = getDashboardUser(objContext)
assert.NoError(t, err)
assert.NotNil(t, user)
assert.NotNil(t, user.AccessKey)
assert.NotNil(t, user.SecretKey)
checkdashboard, err = checkDashboardUser(objContext, user)
assert.NoError(t, err)
assert.True(t, checkdashboard)
// Scenario 3: User exists but dashboard credentials differ from radosgw-admin user info credentials
objContext.Context.Executor = &exectest.MockExecutor{
MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) {
if args[0] == "dashboard" {
if args[1] == "get-rgw-api-access-key" {
return "incorrect", nil
} else if args[1] == "get-rgw-api-secret-key" {
return "incorrect", nil
}
}
return "", nil
},
MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, args ...string) (string, error) {
if args[0] == "user" {
if args[1] == "info" {
return dashboardAdminCreateJSON, nil
}
}
return "", nil
},
}
user, err = getDashboardUser(objContext)
assert.NoError(t, err)
assert.NotNil(t, user)
assert.NotNil(t, user.AccessKey)
assert.NotNil(t, user.SecretKey)
checkdashboard, err = checkDashboardUser(objContext, user)
assert.NoError(t, err)
assert.False(t, checkdashboard)
}
func TestDashboard(t *testing.T) {
storeName := "myobject"
executor := &exectest.MockExecutor{
MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) {
return "", nil
},
MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, args ...string) (string, error) {
if args[0] == "user" {
if args[1] == "info" {
return "no user info saved", nil
} else if args[1] == "create" {
return dashboardAdminCreateJSON, nil
}
}
return "", nil
},
}
objContext := NewContext(&clusterd.Context{Executor: executor}, &client.ClusterInfo{
Namespace: "mycluster",
CephVersion: cephver.CephVersion{Major: 15, Minor: 2, Extra: 9},
Context: context.TODO(),
},
storeName)
user, err := getDashboardUser(objContext)
assert.NoError(t, err)
assert.NotNil(t, user)
checkdashboard, err := checkDashboardUser(objContext, user)
assert.NoError(t, err)
assert.False(t, checkdashboard)
err = enableRGWDashboard(objContext)
assert.Nil(t, err)
assert.NoError(t, err)
executor = &exectest.MockExecutor{
MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) {
if args[0] == "dashboard" && args[1] == "get-rgw-api-access-key" {
return access_key, nil
return "", nil
},
MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, args ...string) (string, error) {
if args[0] == "user" && args[1] == "info" {
return dashboardAdminCreateJSON, nil
}
return "", nil
},
}
objContext.Context.Executor = executor
checkdashboard, err = checkDashboardUser(objContext)
user, err = getDashboardUser(objContext)
assert.NoError(t, err)
assert.NotNil(t, user)
checkdashboard, err = checkDashboardUser(objContext, user)
assert.NoError(t, err)
assert.True(t, checkdashboard)
disableRGWDashboard(objContext)
@@ -281,17 +396,8 @@ func TestDashboard(t *testing.T) {
},
storeName)
err = enableRGWDashboard(objContext)
assert.Nil(t, err)
executor = &exectest.MockExecutor{
MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) {
if args[0] == "dashboard" && args[1] == "get-rgw-api-access-key" {
return access_key, nil
}
return "", nil
},
}
objContext.Context.Executor = executor
checkdashboard, err = checkDashboardUser(objContext)
assert.NoError(t, err)
checkdashboard, err = checkDashboardUser(objContext, user)
assert.NoError(t, err)
assert.True(t, checkdashboard)
disableRGWDashboard(objContext)
+6 -2
View File
@@ -75,9 +75,13 @@ func (c *clusterConfig) createOrUpdateStore(realmName, zoneGroupName, zoneName s
return errors.Wrap(err, "failed to start rgw pods")
}
objContext := NewContext(c.context, c.clusterInfo, c.store.Namespace)
err := enableRGWDashboard(objContext)
objContext, err := NewMultisiteContext(c.context, c.clusterInfo, c.store)
if err != nil {
logger.Warningf("failed to get object context for rgw %q. %v", c.store.Name, err)
return nil
}
if err = enableRGWDashboard(objContext); err != nil {
logger.Warningf("failed to enable dashboard for rgw. %v", err)
}
+29
View File
@@ -134,6 +134,14 @@ func CreateUser(c *Context, user ObjectUser, force bool) (*ObjectUser, int, erro
args = append(args, "--caps", rgwAdminOpsUserCaps)
}
if user.AccessKey != nil {
args = append(args, "--access-key", *user.AccessKey)
}
if user.SecretKey != nil {
args = append(args, "--secret", *user.SecretKey)
}
if force {
args = append(args, "--yes-i-really-mean-it")
}
@@ -162,6 +170,27 @@ func CreateUser(c *Context, user ObjectUser, force bool) (*ObjectUser, int, erro
return decodeUser(result)
}
// CreateOrRecreateUserIfExists if the user doesn't exist, it is created, should it already exist it is deleted and re-created
// It is called from the rgw dashboard setup logic.
func CreateOrRecreateUserIfExists(c *Context, user ObjectUser, force bool) (*ObjectUser, int, error) {
objUser, errCode, err := CreateUser(c, user, force)
if err != nil || (errCode != ErrorCodeFileExists && errCode != RGWErrorNone) {
return nil, errCode, err
}
if errCode == RGWErrorNone {
return objUser, errCode, err
} else if errCode == ErrorCodeFileExists {
// If the user already exists, delete and re-create it
_, err := DeleteUser(c, user.UserID)
if err != nil {
return nil, RGWErrorUnknown, err
}
}
return CreateUser(c, user, force)
}
func ListUserBuckets(c *Context, id string, opts ...string) (string, error) {
args := []string{"bucket", "list", "--uid", id}
+15 -3
View File
@@ -29,6 +29,7 @@ import (
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/operator/k8sutil"
"github.com/rook/rook/tests/framework/clients"
"github.com/rook/rook/tests/framework/installer"
"github.com/rook/rook/tests/framework/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -62,14 +63,14 @@ var (
)
// Test Object StoreCreation on Rook that was installed via helm
func runObjectE2ETestLite(t *testing.T, helper *clients.TestClient, k8sh *utils.K8sHelper, namespace, storeName string, replicaSize int, deleteStore bool, enableTLS bool) {
func runObjectE2ETestLite(t *testing.T, helper *clients.TestClient, k8sh *utils.K8sHelper, installer *installer.CephInstaller, namespace, storeName string, replicaSize int, deleteStore bool, enableTLS bool) {
andDeleting := ""
if deleteStore {
andDeleting = "and deleting"
}
logger.Infof("test creating %s object store %q in namespace %q", andDeleting, storeName, namespace)
createCephObjectStore(t, helper, k8sh, namespace, storeName, replicaSize, enableTLS)
createCephObjectStore(t, helper, k8sh, installer, namespace, storeName, replicaSize, enableTLS)
if deleteStore {
t.Run("delete object store", func(t *testing.T) {
@@ -80,7 +81,7 @@ func runObjectE2ETestLite(t *testing.T, helper *clients.TestClient, k8sh *utils.
}
// create a CephObjectStore and wait for it to report ready status
func createCephObjectStore(t *testing.T, helper *clients.TestClient, k8sh *utils.K8sHelper, namespace, storeName string, replicaSize int, tlsEnable bool) {
func createCephObjectStore(t *testing.T, helper *clients.TestClient, k8sh *utils.K8sHelper, installer *installer.CephInstaller, namespace, storeName string, replicaSize int, tlsEnable bool) {
logger.Infof("Create Object Store %q with replica count %d", storeName, replicaSize)
rgwServiceName := "rook-ceph-rgw-" + storeName
if tlsEnable {
@@ -140,6 +141,17 @@ func createCephObjectStore(t *testing.T, helper *clients.TestClient, k8sh *utils
t.Run("verify RGW service is up", func(t *testing.T) {
assert.True(t, k8sh.IsServiceUp("rook-ceph-rgw-"+storeName, namespace))
})
t.Run("check if the dashboard-admin user exists in all existing object stores", func(t *testing.T) {
objectStores, err := k8sh.RookClientset.CephV1().CephObjectStores(namespace).List(ctx, metav1.ListOptions{})
assert.Nil(t, err)
for _, objectStore := range objectStores.Items {
err, output := installer.Execute("radosgw-admin", []string{"user", "info", "--uid=dashboard-admin", fmt.Sprintf("--rgw-realm=%s", objectStore.GetName())}, namespace)
logger.Infof("output: %s", output)
assert.NoError(t, err)
}
})
}
func deleteObjectStore(t *testing.T, k8sh *utils.K8sHelper, namespace, storeName string) {
+1 -1
View File
@@ -106,5 +106,5 @@ func (h *HelmSuite) TestFileStoreOnRookInstalledViaHelm() {
func (h *HelmSuite) TestObjectStoreOnRookInstalledViaHelm() {
deleteStore := true
tls := false
runObjectE2ETestLite(h.T(), h.helper, h.k8shelper, h.settings.Namespace, "default", 3, deleteStore, tls)
runObjectE2ETestLite(h.T(), h.helper, h.k8shelper, h.installer, h.settings.Namespace, "default", 3, deleteStore, tls)
}
+7 -7
View File
@@ -97,7 +97,7 @@ func (s *ObjectSuite) TestWithTLS() {
tls := true
objectStoreServicePrefix = objectStoreServicePrefixUniq
runObjectE2ETest(s.helper, s.k8sh, s.Suite, s.settings.Namespace, tls)
runObjectE2ETest(s.helper, s.k8sh, s.installer, s.Suite, s.settings.Namespace, tls)
err := s.k8sh.Clientset.CoreV1().Secrets(s.settings.Namespace).Delete(context.TODO(), objectTLSSecretName, metav1.DeleteOptions{})
if err != nil {
if !errors.IsNotFound(err) {
@@ -114,21 +114,21 @@ func (s *ObjectSuite) TestWithoutTLS() {
tls := false
objectStoreServicePrefix = objectStoreServicePrefixUniq
runObjectE2ETest(s.helper, s.k8sh, s.Suite, s.settings.Namespace, tls)
runObjectE2ETest(s.helper, s.k8sh, s.installer, s.Suite, s.settings.Namespace, tls)
}
// Smoke Test for ObjectStore - Test check the following operations on ObjectStore in order
// Create object store, Create User, Connect to Object Store, Create Bucket, Read/Write/Delete to bucket,
// Check issues in MGRs, Delete Bucket and Delete user
// Test for ObjectStore with and without TLS enabled
func runObjectE2ETest(helper *clients.TestClient, k8sh *utils.K8sHelper, s suite.Suite, namespace string, tlsEnable bool) {
func runObjectE2ETest(helper *clients.TestClient, k8sh *utils.K8sHelper, installer *installer.CephInstaller, s suite.Suite, namespace string, tlsEnable bool) {
storeName := "test-store"
if tlsEnable {
storeName = objectStoreTLSName
}
logger.Infof("Running on Rook Cluster %s", namespace)
createCephObjectStore(s.T(), helper, k8sh, namespace, storeName, 3, tlsEnable)
createCephObjectStore(s.T(), helper, k8sh, installer, namespace, storeName, 3, tlsEnable)
// test that a second object store can be created (and deleted) while the first exists
s.T().Run("run a second object store", func(t *testing.T) {
@@ -136,7 +136,7 @@ func runObjectE2ETest(helper *clients.TestClient, k8sh *utils.K8sHelper, s suite
// The lite e2e test is perfect, as it only creates a cluster, checks that it is healthy,
// and then deletes it.
deleteStore := true
runObjectE2ETestLite(t, helper, k8sh, namespace, otherStoreName, 1, deleteStore, tlsEnable)
runObjectE2ETestLite(t, helper, k8sh, installer, namespace, otherStoreName, 1, deleteStore, tlsEnable)
})
if tlsEnable {
// test that a third object store can be created (and deleted) while the first exists
@@ -146,7 +146,7 @@ func runObjectE2ETest(helper *clients.TestClient, k8sh *utils.K8sHelper, s suite
// and then deletes it.
deleteStore := true
objectStoreServicePrefix = objectStoreServicePrefixUniq
runObjectE2ETestLite(t, helper, k8sh, namespace, otherStoreName, 1, deleteStore, tlsEnable)
runObjectE2ETestLite(t, helper, k8sh, installer, namespace, otherStoreName, 1, deleteStore, tlsEnable)
objectStoreServicePrefix = objectStoreServicePrefixUniq
})
}
@@ -155,7 +155,7 @@ func runObjectE2ETest(helper *clients.TestClient, k8sh *utils.K8sHelper, s suite
testObjectStoreOperations(s, helper, k8sh, namespace, storeName)
bucketNotificationTestStoreName := "bucket-notification-" + storeName
createCephObjectStore(s.T(), helper, k8sh, namespace, bucketNotificationTestStoreName, 1, tlsEnable)
createCephObjectStore(s.T(), helper, k8sh, installer, namespace, bucketNotificationTestStoreName, 1, tlsEnable)
testBucketNotifications(s, helper, k8sh, namespace, bucketNotificationTestStoreName)
}
+1 -1
View File
@@ -125,7 +125,7 @@ func (s *SmokeSuite) TestObjectStorage_SmokeTest() {
storeName := "lite-store"
deleteStore := true
tls := false
runObjectE2ETestLite(s.T(), s.helper, s.k8sh, s.settings.Namespace, storeName, 2, deleteStore, tls)
runObjectE2ETestLite(s.T(), s.helper, s.k8sh, s.installer, s.settings.Namespace, storeName, 2, deleteStore, tls)
}
// Test to make sure all rook components are installed and Running
+1 -1
View File
@@ -277,7 +277,7 @@ func (s *UpgradeSuite) deployClusterforUpgrade(objectUserID, preFilename string)
logger.Infof("Initializing object before the upgrade")
deleteStore := false
tls := false
runObjectE2ETestLite(s.T(), s.helper, s.k8sh, s.settings.Namespace, installer.ObjectStoreName, 1, deleteStore, tls)
runObjectE2ETestLite(s.T(), s.helper, s.k8sh, s.installer, s.settings.Namespace, installer.ObjectStoreName, 1, deleteStore, tls)
}
logger.Infof("Initializing object user before the upgrade")