From 36f1a3308fa73ec08ed091b07afa9b28184bf06e Mon Sep 17 00:00:00 2001 From: Joshua Hoblitt Date: Fri, 1 Aug 2025 14:43:50 -0700 Subject: [PATCH] test: factor out util/admin package for setup of ceph admin client Signed-off-by: Joshua Hoblitt --- .../object/bucketowner/bucketowner.go | 47 +----------- .../object/user/userkeys/userkeys.go | 47 +----------- tests/integration/object/util/admin/admin.go | 67 +++++++++++++++++ tests/integration/object/util/s3/s3.go | 75 +++++++++++++++++++ tests/integration/object/util/sns/sns.go | 62 +-------------- 5 files changed, 151 insertions(+), 147 deletions(-) create mode 100644 tests/integration/object/util/admin/admin.go create mode 100644 tests/integration/object/util/s3/s3.go diff --git a/tests/integration/object/bucketowner/bucketowner.go b/tests/integration/object/bucketowner/bucketowner.go index 463d0c15b..e12237bcd 100644 --- a/tests/integration/object/bucketowner/bucketowner.go +++ b/tests/integration/object/bucketowner/bucketowner.go @@ -19,11 +19,8 @@ package bucketowner import ( "bufio" "context" - "crypto/tls" - "encoding/json" "fmt" "log" - "net/http" "strings" "testing" "time" @@ -34,6 +31,7 @@ import ( cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" "github.com/rook/rook/tests/framework/installer" "github.com/rook/rook/tests/framework/utils" + utiladmin "github.com/rook/rook/tests/integration/object/util/admin" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" @@ -341,47 +339,8 @@ func TestObjectBucketClaimBucketOwner(t *testing.T, k8sh *utils.K8sHelper, insta // the rgw admin api is used to verify bucket ownership t.Run("setup rgw admin api client", func(t *testing.T) { - output, err := installer.Execute("radosgw-admin", []string{"user", "info", "--uid=dashboard-admin", fmt.Sprintf("--rgw-realm=%s", objectStore.Name)}, objectStore.Namespace) - require.NoError(t, err) - - // extract api creds from json output - var userInfo map[string]interface{} - err = json.Unmarshal([]byte(output), &userInfo) - require.NoError(t, err) - - s3AccessKey, ok := userInfo["keys"].([]interface{})[0].(map[string]interface{})["access_key"].(string) - require.True(t, ok) - require.NotEmpty(t, s3AccessKey) - - s3SecretKey, ok := userInfo["keys"].([]interface{})[0].(map[string]interface{})["secret_key"].(string) - require.True(t, ok) - require.NotEmpty(t, s3SecretKey) - - // extract rgw endpoint from k8s svc - svc, err := k8sh.Clientset.CoreV1().Services(objectStore.Namespace).Get(ctx, objectStore.Name, metav1.GetOptions{}) - require.NoError(t, err) - - schema := "http://" - httpClient := &http.Client{} - - if tlsEnable { - schema = "https://" - httpClient.Transport = &http.Transport{ - TLSClientConfig: &tls.Config{ - // nolint:gosec // skip TLS verification as this is a test - InsecureSkipVerify: true, - }, - } - } - s3Endpoint := schema + svc.Spec.ClusterIP + ":80" - - logger.Infof("endpoint (%s) Accesskey (%s) secret (%s)", s3Endpoint, s3AccessKey, s3SecretKey) - - adminClient, err = admin.New(s3Endpoint, s3AccessKey, s3SecretKey, httpClient) - require.NoError(t, err) - - // verify that admin api is working - _, err = adminClient.GetInfo(ctx) + var err error + adminClient, err = utiladmin.NewAdminClient(objectStore, installer, k8sh, tlsEnable) require.NoError(t, err) }) diff --git a/tests/integration/object/user/userkeys/userkeys.go b/tests/integration/object/user/userkeys/userkeys.go index e1cfbae5c..51833b926 100644 --- a/tests/integration/object/user/userkeys/userkeys.go +++ b/tests/integration/object/user/userkeys/userkeys.go @@ -18,10 +18,7 @@ package userkeys import ( "context" - "crypto/tls" - "encoding/json" "fmt" - "net/http" "testing" "time" @@ -36,6 +33,7 @@ import ( cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" "github.com/rook/rook/tests/framework/installer" "github.com/rook/rook/tests/framework/utils" + utiladmin "github.com/rook/rook/tests/integration/object/util/admin" ) var ( @@ -371,47 +369,8 @@ func TestObjectStoreUserKeys(t *testing.T, k8sh *utils.K8sHelper, installer *ins }) t.Run("setup rgw admin api client", func(t *testing.T) { - output, err := installer.Execute("radosgw-admin", []string{"user", "info", "--uid=dashboard-admin", fmt.Sprintf("--rgw-realm=%s", objectStore.Name)}, objectStore.Namespace) - require.NoError(t, err) - - // extract api creds from json output - var userInfo map[string]interface{} - err = json.Unmarshal([]byte(output), &userInfo) - require.NoError(t, err) - - s3AccessKey, ok := userInfo["keys"].([]interface{})[0].(map[string]interface{})["access_key"].(string) - require.True(t, ok) - require.NotEmpty(t, s3AccessKey) - - s3SecretKey, ok := userInfo["keys"].([]interface{})[0].(map[string]interface{})["secret_key"].(string) - require.True(t, ok) - require.NotEmpty(t, s3SecretKey) - - // extract rgw endpoint from k8s svc - svc, err := k8sh.Clientset.CoreV1().Services(objectStore.Namespace).Get(ctx, objectStore.Name, metav1.GetOptions{}) - require.NoError(t, err) - - schema := "http://" - httpClient := &http.Client{} - - if tlsEnable { - schema = "https://" - httpClient.Transport = &http.Transport{ - TLSClientConfig: &tls.Config{ - // nolint:gosec // skip TLS verification as this is a test - InsecureSkipVerify: true, - }, - } - } - s3Endpoint := schema + svc.Spec.ClusterIP + ":80" - - logger.Infof("endpoint (%s) Accesskey (%s) secret (%s)", s3Endpoint, s3AccessKey, s3SecretKey) - - adminClient, err = admin.New(s3Endpoint, s3AccessKey, s3SecretKey, httpClient) - require.NoError(t, err) - - // verify that admin api is working - _, err = adminClient.GetInfo(ctx) + var err error + adminClient, err = utiladmin.NewAdminClient(objectStore, installer, k8sh, tlsEnable) require.NoError(t, err) }) diff --git a/tests/integration/object/util/admin/admin.go b/tests/integration/object/util/admin/admin.go new file mode 100644 index 000000000..a3e9b2ea6 --- /dev/null +++ b/tests/integration/object/util/admin/admin.go @@ -0,0 +1,67 @@ +/* +Copyright 2025 The Rook Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package admin + +import ( + "context" + "crypto/tls" + "net/http" + + "github.com/ceph/go-ceph/rgw/admin" + "github.com/pkg/errors" + + cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" + "github.com/rook/rook/tests/framework/installer" + "github.com/rook/rook/tests/framework/utils" + utils3 "github.com/rook/rook/tests/integration/object/util/s3" +) + +func NewAdminClient(objectStore *cephv1.CephObjectStore, installer *installer.CephInstaller, k8sh *utils.K8sHelper, tlsEnable bool) (*admin.API, error) { + accessKey, secretKey, err := utils3.GetS3Credentials(objectStore, installer) + if err != nil { + return nil, errors.Wrap(err, "failed to get s3 credentials") + } + + endpoint, err := utils3.GetS3Endpoint(objectStore, k8sh, tlsEnable) + if err != nil { + return nil, errors.Wrap(err, "failed to get s3 endpoint") + } + + httpClient := &http.Client{} + + if tlsEnable { + httpClient.Transport = &http.Transport{ + TLSClientConfig: &tls.Config{ + // nolint:gosec // skip TLS verification as this is a test + InsecureSkipVerify: true, + }, + } + } + + adminClient, err := admin.New(endpoint, accessKey, secretKey, httpClient) + if err != nil { + return nil, errors.Wrap(err, "failed to setup ceph admin client") + } + + // verify that admin api is working + _, err = adminClient.GetInfo(context.Background()) + if err != nil { + return nil, errors.Wrap(err, "admin client is not working") + } + + return adminClient, nil +} diff --git a/tests/integration/object/util/s3/s3.go b/tests/integration/object/util/s3/s3.go new file mode 100644 index 000000000..584577aa4 --- /dev/null +++ b/tests/integration/object/util/s3/s3.go @@ -0,0 +1,75 @@ +/* +Copyright 2025 The Rook Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package s3 + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/pkg/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" + "github.com/rook/rook/tests/framework/installer" + "github.com/rook/rook/tests/framework/utils" +) + +func GetS3Credentials(objectStore *cephv1.CephObjectStore, installer *installer.CephInstaller) (string, string, error) { + output, err := installer.Execute("radosgw-admin", []string{"user", "info", "--uid=dashboard-admin", fmt.Sprintf("--rgw-realm=%s", objectStore.Name)}, objectStore.Namespace) + if err != nil { + return "", "", errors.Wrap(err, "failed to get user info") + } + + // extract api creds from json output + var userInfo map[string]interface{} + err = json.Unmarshal([]byte(output), &userInfo) + if err != nil { + return "", "", errors.Wrap(err, "failed to unmarshal user info") + } + + accessKey, ok := userInfo["keys"].([]interface{})[0].(map[string]interface{})["access_key"].(string) + if !ok { + return "", "", errors.New("failed to get access key") + } + + secretKey, ok := userInfo["keys"].([]interface{})[0].(map[string]interface{})["secret_key"].(string) + if !ok { + return "", "", errors.New("failed to get secret key") + } + + return accessKey, secretKey, nil +} + +func GetS3Endpoint(objectStore *cephv1.CephObjectStore, k8sh *utils.K8sHelper, tlsEnable bool) (string, error) { + ctx := context.TODO() + + // extract rgw endpoint from k8s svc + svc, err := k8sh.Clientset.CoreV1().Services(objectStore.Namespace).Get(ctx, objectStore.Name, metav1.GetOptions{}) + if err != nil { + return "", errors.Wrap(err, "failed to get objectstore svc") + } + + schema := "http://" + if tlsEnable { + schema = "https://" + } + + endpoint := schema + svc.Spec.ClusterIP + ":80" + + return endpoint, nil +} diff --git a/tests/integration/object/util/sns/sns.go b/tests/integration/object/util/sns/sns.go index 358934596..d537cd220 100644 --- a/tests/integration/object/util/sns/sns.go +++ b/tests/integration/object/util/sns/sns.go @@ -18,10 +18,6 @@ package sns import ( "context" - "crypto/tls" - "encoding/json" - "fmt" - "net/http" "net/url" "github.com/aws/aws-sdk-go-v2/config" @@ -30,11 +26,11 @@ import ( smithyendpoints "github.com/aws/smithy-go/endpoints" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" "github.com/rook/rook/tests/framework/installer" "github.com/rook/rook/tests/framework/utils" + utils3 "github.com/rook/rook/tests/integration/object/util/s3" ) // based on the s3 endpoint example in the official docs: @@ -53,62 +49,10 @@ func (r *snsResolverV2) ResolveEndpoint(ctx context.Context, params sns.Endpoint }, nil } -func GetS3Credentials(objectStore *cephv1.CephObjectStore, installer *installer.CephInstaller) (string, string, error) { - output, err := installer.Execute("radosgw-admin", []string{"user", "info", "--uid=dashboard-admin", fmt.Sprintf("--rgw-realm=%s", objectStore.Name)}, objectStore.Namespace) - if err != nil { - return "", "", errors.Wrap(err, "failed to get user info") - } - - // extract api creds from json output - var userInfo map[string]interface{} - err = json.Unmarshal([]byte(output), &userInfo) - if err != nil { - return "", "", errors.Wrap(err, "failed to unmarshal user info") - } - - accessKey, ok := userInfo["keys"].([]interface{})[0].(map[string]interface{})["access_key"].(string) - if !ok { - return "", "", errors.New("failed to get access key") - } - - secretKey, ok := userInfo["keys"].([]interface{})[0].(map[string]interface{})["secret_key"].(string) - if !ok { - return "", "", errors.New("failed to get secret key") - } - - return accessKey, secretKey, nil -} - -func GetS3Endpoint(objectStore *cephv1.CephObjectStore, k8sh *utils.K8sHelper, tlsEnable bool) (string, error) { - ctx := context.TODO() - - // extract rgw endpoint from k8s svc - svc, err := k8sh.Clientset.CoreV1().Services(objectStore.Namespace).Get(ctx, objectStore.Name, metav1.GetOptions{}) - if err != nil { - return "", errors.Wrap(err, "failed to get objectstore svc") - } - - schema := "http://" - httpClient := &http.Client{} - - if tlsEnable { - schema = "https://" - httpClient.Transport = &http.Transport{ - TLSClientConfig: &tls.Config{ - // nolint:gosec // skip TLS verification as this is a test - InsecureSkipVerify: true, - }, - } - } - endpoint := schema + svc.Spec.ClusterIP + ":80" - - return endpoint, nil -} - func NewClient(objectStore *cephv1.CephObjectStore, svc *corev1.Service, k8sh *utils.K8sHelper, installer *installer.CephInstaller, tlsEnable bool) (*sns.Client, error) { ctx := context.TODO() - accessKey, secretKey, err := GetS3Credentials(objectStore, installer) + accessKey, secretKey, err := utils3.GetS3Credentials(objectStore, installer) if err != nil { return nil, errors.Wrap(err, "failed to get s3 credentials") } @@ -128,7 +72,7 @@ func NewClient(objectStore *cephv1.CephObjectStore, svc *corev1.Service, k8sh *u return nil, errors.Wrap(err, "failed to load default aws config") } - endpoint, err := GetS3Endpoint(objectStore, k8sh, tlsEnable) + endpoint, err := utils3.GetS3Endpoint(objectStore, k8sh, tlsEnable) if err != nil { return nil, errors.Wrap(err, "failed to get sns endpoint") }