Files
my-rook-config/tests/integration/object/util/client/s3.go
T
Joshua Hoblitt b7725553d0 test: add a generic S3 agent builder to the object test client
Add client.NewS3Agent, which builds an rgw S3 agent for an object store
from a set of credentials and the store's endpoint. The object tests build
S3 clients from several credential sources (the store's dashboard admin
user, an OBC's provisioned secret); this factors out the endpoint and agent
construction they share.

Signed-off-by: Joshua Hoblitt <josh@hoblitt.com>
2026-07-01 14:42:39 -07:00

92 lines
2.9 KiB
Go

/*
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 client
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"
rgw "github.com/rook/rook/pkg/operator/ceph/object"
"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, port := "http://", "80"
if tlsEnable {
schema, port = "https://", "443"
}
endpoint := schema + svc.Spec.ClusterIP + ":" + port
return endpoint, nil
}
// NewS3Agent builds an S3 client for objectStore using the given credentials.
func NewS3Agent(objectStore *cephv1.CephObjectStore, k8sh *utils.K8sHelper, tlsEnable bool, accessKey, secretKey string) (*rgw.S3Agent, error) {
endpoint, err := GetS3Endpoint(objectStore, k8sh, tlsEnable)
if err != nil {
return nil, errors.Wrap(err, "failed to get s3 endpoint")
}
agent, err := rgw.NewS3Agent(accessKey, secretKey, endpoint, true, nil, tlsEnable, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create s3 agent")
}
return agent, nil
}