2023-02-08 06:03:54 -05:00
|
|
|
//go:build integration
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kms/kms/aws"
|
2023-03-02 09:08:31 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kms/storage/awss3"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kms/storage/memfs"
|
2023-02-08 06:03:54 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kms/uri"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAwsStorage(t *testing.T) {
|
|
|
|
if !*runAwsStorage {
|
|
|
|
t.Skip("Skipping AWS storage test")
|
|
|
|
}
|
2023-03-02 09:08:31 -05:00
|
|
|
if *awsAccessKey == "" || *awsAccessKeyID == "" || *awsBucket == "" || *awsRegion == "" {
|
|
|
|
flag.Usage()
|
|
|
|
t.Fatal("Required flags not set: --aws-access-key, --aws-access-key-id, --aws-bucket, --aws-region")
|
|
|
|
}
|
2023-02-08 06:03:54 -05:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// create bucket
|
2023-03-02 09:08:31 -05:00
|
|
|
cfg := uri.AWSS3Config{
|
|
|
|
Bucket: *awsBucket,
|
|
|
|
AccessKeyID: *awsAccessKeyID,
|
|
|
|
AccessKey: *awsAccessKey,
|
|
|
|
Region: *awsRegion,
|
|
|
|
}
|
|
|
|
store, err := awss3.New(ctx, cfg)
|
2023-02-08 06:03:54 -05:00
|
|
|
require.NoError(err)
|
|
|
|
|
2023-03-02 09:08:31 -05:00
|
|
|
runStorageTest(t, store)
|
2023-02-08 06:03:54 -05:00
|
|
|
|
2023-03-02 09:08:31 -05:00
|
|
|
cleanUpBucket(ctx, require, *awsBucket, *awsRegion)
|
2023-02-08 06:03:54 -05:00
|
|
|
}
|
|
|
|
|
2023-03-02 09:08:31 -05:00
|
|
|
func cleanUpBucket(ctx context.Context, require *require.Assertions, bucketID, awsRegion string) {
|
|
|
|
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(awsRegion))
|
2023-02-08 06:03:54 -05:00
|
|
|
require.NoError(err)
|
2023-03-02 09:08:31 -05:00
|
|
|
client := s3.NewFromConfig(cfg)
|
2023-02-08 06:03:54 -05:00
|
|
|
|
|
|
|
// List all objects of the bucket
|
|
|
|
listObjectsInput := &s3.ListObjectsV2Input{
|
|
|
|
Bucket: &bucketID,
|
|
|
|
}
|
2023-03-02 09:08:31 -05:00
|
|
|
output, err := client.ListObjectsV2(ctx, listObjectsInput)
|
|
|
|
require.NoError(err)
|
2023-02-08 06:03:54 -05:00
|
|
|
var objects []string
|
|
|
|
var i int32
|
2024-01-09 10:18:33 -05:00
|
|
|
for i = 0; i < *output.KeyCount; i++ {
|
2023-02-08 06:03:54 -05:00
|
|
|
objects = append(objects, *output.Contents[i].Key)
|
|
|
|
}
|
|
|
|
// Delete all objects of the bucket
|
|
|
|
require.NoError(cleanUpObjects(ctx, client, bucketID, objects))
|
|
|
|
|
|
|
|
// Delete the bucket
|
|
|
|
deleteBucketInput := &s3.DeleteBucketInput{
|
|
|
|
Bucket: &bucketID,
|
|
|
|
}
|
|
|
|
_, err = client.DeleteBucket(ctx, deleteBucketInput)
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUpObjects(ctx context.Context, client *s3.Client, bucketID string, objectsToDelete []string) error {
|
|
|
|
var objectsIdentifier []types.ObjectIdentifier
|
|
|
|
for _, object := range objectsToDelete {
|
|
|
|
objectsIdentifier = append(objectsIdentifier, types.ObjectIdentifier{Key: func(s string) *string { return &s }(object)})
|
|
|
|
}
|
|
|
|
deleteObjectsInput := &s3.DeleteObjectsInput{
|
|
|
|
Bucket: &bucketID,
|
|
|
|
Delete: &types.Delete{Objects: objectsIdentifier},
|
|
|
|
}
|
|
|
|
_, err := client.DeleteObjects(ctx, deleteObjectsInput)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAwsKms(t *testing.T) {
|
|
|
|
if !*runAwsKms {
|
|
|
|
t.Skip("Skipping AWS KMS test")
|
|
|
|
}
|
|
|
|
|
|
|
|
if *kekID == "" || *awsAccessKeyID == "" || *awsAccessKey == "" || *awsRegion == "" {
|
|
|
|
flag.Usage()
|
|
|
|
t.Fatal("Required flags not set: --aws-access-key-id, --aws-access-key, --aws-region, --kek-id")
|
|
|
|
}
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-03-02 09:08:31 -05:00
|
|
|
store := memfs.New()
|
2023-02-08 06:03:54 -05:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cfg := uri.AWSConfig{
|
|
|
|
KeyName: *kekID,
|
|
|
|
Region: *awsRegion,
|
|
|
|
AccessKeyID: *awsAccessKeyID,
|
|
|
|
AccessKey: *awsAccessKey,
|
|
|
|
}
|
|
|
|
kmsClient, err := aws.New(ctx, store, cfg)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
runKMSTest(t, kmsClient)
|
|
|
|
}
|