2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-03-02 09:08:31 -05:00
|
|
|
package awss3
|
2022-03-25 06:55:09 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
2023-03-02 09:08:31 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kms/storage"
|
2022-03-25 06:55:09 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stubAWSS3StorageClient struct {
|
|
|
|
getObjectOutputData []byte
|
|
|
|
getObjectErr error
|
|
|
|
putObjectErr error
|
|
|
|
savedObject []byte
|
|
|
|
createBucketCalled bool
|
|
|
|
createBucketErr error
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubAWSS3StorageClient) GetObject(_ context.Context, _ *s3.GetObjectInput, _ ...func(*s3.Options)) (*s3.GetObjectOutput, error) {
|
2022-03-25 06:55:09 -04:00
|
|
|
return &s3.GetObjectOutput{
|
|
|
|
Body: io.NopCloser(bytes.NewReader(s.getObjectOutputData)),
|
|
|
|
}, s.getObjectErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubAWSS3StorageClient) PutObject(_ context.Context, params *s3.PutObjectInput, _ ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
|
2022-03-25 06:55:09 -04:00
|
|
|
out, err := io.ReadAll(params.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s.savedObject = out
|
|
|
|
return &s3.PutObjectOutput{}, s.putObjectErr
|
|
|
|
}
|
|
|
|
|
2023-05-25 12:43:44 -04:00
|
|
|
func (s *stubAWSS3StorageClient) DeleteObject(_ context.Context, _ *s3.DeleteObjectInput, _ ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) {
|
|
|
|
return &s3.DeleteObjectOutput{}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubAWSS3StorageClient) CreateBucket(_ context.Context, _ *s3.CreateBucketInput, _ ...func(*s3.Options)) (*s3.CreateBucketOutput, error) {
|
2022-03-25 06:55:09 -04:00
|
|
|
s.createBucketCalled = true
|
|
|
|
return &s3.CreateBucketOutput{}, s.createBucketErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAWSS3Get(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client *stubAWSS3StorageClient
|
|
|
|
unsetError bool
|
|
|
|
wantErr bool
|
2022-03-25 06:55:09 -04:00
|
|
|
}{
|
|
|
|
"Get successful": {
|
|
|
|
client: &stubAWSS3StorageClient{getObjectOutputData: []byte("test-data")},
|
|
|
|
},
|
|
|
|
"GetObject fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: &stubAWSS3StorageClient{getObjectErr: errors.New("error")},
|
|
|
|
wantErr: true,
|
2022-03-25 06:55:09 -04:00
|
|
|
},
|
|
|
|
"GetObject fails with NoSuchKey": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: &stubAWSS3StorageClient{getObjectErr: &types.NoSuchKey{}},
|
|
|
|
wantErr: true,
|
|
|
|
unsetError: true,
|
2022-03-25 06:55:09 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-03-02 09:08:31 -05:00
|
|
|
store := &Storage{
|
2022-03-25 06:55:09 -04:00
|
|
|
client: tc.client,
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := store.Get(context.Background(), "test-key")
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-25 06:55:09 -04:00
|
|
|
assert.Error(err)
|
|
|
|
|
|
|
|
if tc.unsetError {
|
2023-03-02 09:08:31 -05:00
|
|
|
assert.ErrorIs(err, storage.ErrDEKUnset)
|
2022-03-25 06:55:09 -04:00
|
|
|
} else {
|
2023-03-02 09:08:31 -05:00
|
|
|
assert.False(errors.Is(err, storage.ErrDEKUnset))
|
2022-03-25 06:55:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.client.getObjectOutputData, out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAWSS3Put(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client *stubAWSS3StorageClient
|
|
|
|
wantErr bool
|
2022-03-25 06:55:09 -04:00
|
|
|
}{
|
|
|
|
"Put successful": {
|
|
|
|
client: &stubAWSS3StorageClient{},
|
|
|
|
},
|
|
|
|
"PutObject fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: &stubAWSS3StorageClient{putObjectErr: errors.New("error")},
|
|
|
|
wantErr: true,
|
2022-03-25 06:55:09 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-03-02 09:08:31 -05:00
|
|
|
store := &Storage{
|
2022-03-25 06:55:09 -04:00
|
|
|
client: tc.client,
|
|
|
|
}
|
|
|
|
|
|
|
|
testData := []byte{0x1, 0x2, 0x3}
|
|
|
|
|
|
|
|
err := store.Put(context.Background(), "test-key", testData)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-25 06:55:09 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(testData, tc.client.savedObject)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAWSS3CreateBucket(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client *stubAWSS3StorageClient
|
|
|
|
wantErr bool
|
2022-03-25 06:55:09 -04:00
|
|
|
}{
|
|
|
|
"CreateBucket successful": {
|
|
|
|
client: &stubAWSS3StorageClient{},
|
|
|
|
},
|
|
|
|
"CreateBucket fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: &stubAWSS3StorageClient{createBucketErr: errors.New("error")},
|
|
|
|
wantErr: true,
|
2022-03-25 06:55:09 -04:00
|
|
|
},
|
|
|
|
"CreateBucket fails with BucketAlreadyExists": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: &stubAWSS3StorageClient{createBucketErr: &types.BucketAlreadyExists{}},
|
|
|
|
wantErr: false,
|
2022-03-25 06:55:09 -04:00
|
|
|
},
|
|
|
|
"CreateBucket fails with BucketAlreadyOwnedByYou": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: &stubAWSS3StorageClient{createBucketErr: &types.BucketAlreadyOwnedByYou{}},
|
|
|
|
wantErr: false,
|
2022-03-25 06:55:09 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-03-02 09:08:31 -05:00
|
|
|
store := &Storage{
|
2022-03-25 06:55:09 -04:00
|
|
|
client: tc.client,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := store.createBucket(context.Background(), "test-bucket", "test-region")
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-25 06:55:09 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.True(tc.client.createBucketCalled)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|