2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-08-13 21:03:24 +00:00
|
|
|
package alicloudoss
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-01-23 16:24:51 +00:00
|
|
|
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
2018-08-13 21:03:24 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2018-08-13 21:03:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAliCloudOSSBackend(t *testing.T) {
|
2018-10-01 17:05:08 +00:00
|
|
|
// ex. http://oss-us-east-1.aliyuncs.com
|
2020-01-30 20:08:24 +00:00
|
|
|
endpoint := os.Getenv(AlibabaCloudOSSEndpointEnv)
|
|
|
|
accessKeyID := os.Getenv(AlibabaCloudAccessKeyEnv)
|
|
|
|
accessKeySecret := os.Getenv(AlibabaCloudSecretKeyEnv)
|
2018-08-13 21:03:24 +00:00
|
|
|
|
|
|
|
if endpoint == "" || accessKeyID == "" || accessKeySecret == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := oss.New(endpoint, accessKeyID, accessKeySecret)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test client: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
randInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
|
2018-08-13 21:03:24 +00:00
|
|
|
bucket := fmt.Sprintf("vault-alibaba-testacc-%d", randInt)
|
|
|
|
|
|
|
|
err = conn.CreateBucket(bucket)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test bucket: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// Gotta list all the objects and delete them
|
|
|
|
// before being able to delete the bucket
|
|
|
|
b, err := conn.Bucket(bucket)
|
2020-01-30 20:08:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-08-13 21:03:24 +00:00
|
|
|
listResp, err := b.ListObjects()
|
2020-01-30 20:08:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-08-13 21:03:24 +00:00
|
|
|
|
|
|
|
objects := []string{}
|
|
|
|
for _, object := range listResp.Objects {
|
|
|
|
objects = append(objects, object.Key)
|
|
|
|
}
|
|
|
|
|
2020-01-30 20:08:24 +00:00
|
|
|
_, err = b.DeleteObjects(objects)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-08-13 21:03:24 +00:00
|
|
|
|
|
|
|
err = conn.DeleteBucket(bucket)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
|
|
|
|
|
|
|
// This uses the same logic to find the Alibaba credentials as we did at the beginning of the test
|
2020-01-30 20:08:24 +00:00
|
|
|
b, err := NewAliCloudOSSBackend(
|
|
|
|
map[string]string{"bucket": bucket},
|
|
|
|
logger,
|
|
|
|
)
|
2018-08-13 21:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
physical.ExerciseBackend(t, b)
|
|
|
|
physical.ExerciseBackend_ListPrefix(t, b)
|
|
|
|
}
|