2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-10 06:35:17 -04:00
|
|
|
package setup
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-29 03:52:47 -04:00
|
|
|
"encoding/base64"
|
2022-03-22 11:03:15 -04:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2023-01-16 05:19:03 -05:00
|
|
|
const constellationKekID = "Constellation"
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m,
|
|
|
|
// https://github.com/census-instrumentation/opencensus-go/issues/1262
|
|
|
|
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestGetStore(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri string
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"no store": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: NoStoreURI,
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"aws s3": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: fmt.Sprintf(AWSS3URI, ""),
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"azure blob": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: fmt.Sprintf(AzureBlobURI, "", ""),
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"gcp storage": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: fmt.Sprintf(GCPStorageURI, "", ""),
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"unknown store": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: "storage://unknown",
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"invalid scheme": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: ClusterKMSURI,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"not a url": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: ":/123",
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
_, err := getStore(context.Background(), tc.uri)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetKMS(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri string
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"cluster kms": {
|
2023-01-16 05:19:03 -05:00
|
|
|
uri: fmt.Sprintf(ClusterKMSURI, base64.URLEncoding.EncodeToString([]byte("key")), base64.URLEncoding.EncodeToString([]byte("salt"))),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"aws kms": {
|
2023-01-16 05:19:03 -05:00
|
|
|
uri: fmt.Sprintf(AWSKMSURI, "", ""),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"azure kms": {
|
2023-02-08 06:03:54 -05:00
|
|
|
uri: fmt.Sprintf(AzureKMSURI, "", "", "", "", "", ""),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"gcp kms": {
|
2023-01-16 05:19:03 -05:00
|
|
|
uri: fmt.Sprintf(GCPKMSURI, "", "", "", "", ""),
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"unknown kms": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: "kms://unknown",
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"invalid scheme": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: NoStoreURI,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"not a url": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: ":/123",
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
kms, err := getKMS(context.Background(), tc.uri, nil)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(kms)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetUpKMS(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-10-05 09:02:46 -04:00
|
|
|
kms, err := KMS(context.Background(), "storage://unknown", "kms://unknown")
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
assert.Nil(kms)
|
|
|
|
|
2023-01-16 05:19:03 -05:00
|
|
|
masterSecret := MasterSecret{Key: []byte("key"), Salt: []byte("salt")}
|
|
|
|
kms, err = KMS(context.Background(), "storage://no-store", masterSecret.EncodeToURI())
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(kms)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAWSKMSConfig(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
policy := "{keyPolicy: keyPolicy}"
|
|
|
|
escapedPolicy := url.QueryEscape(policy)
|
2023-01-16 05:19:03 -05:00
|
|
|
kekID := base64.URLEncoding.EncodeToString([]byte(constellationKekID))
|
|
|
|
uri, err := url.Parse(fmt.Sprintf(AWSKMSURI, escapedPolicy, kekID))
|
2022-03-22 11:03:15 -04:00
|
|
|
require.NoError(err)
|
2023-01-16 05:19:03 -05:00
|
|
|
policyProducer, rKekID, err := getAWSKMSConfig(uri)
|
2022-03-22 11:03:15 -04:00
|
|
|
require.NoError(err)
|
|
|
|
keyPolicy, err := policyProducer.CreateKeyPolicy("")
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(policy, keyPolicy)
|
2023-01-16 05:19:03 -05:00
|
|
|
assert.Equal(constellationKekID, rKekID)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAzureBlobConfig(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
connStr := "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=Q29uc3RlbGxhdGlvbg==;EndpointSuffix=core.windows.net"
|
|
|
|
escapedConnStr := url.QueryEscape(connStr)
|
|
|
|
container := "test"
|
|
|
|
uri, err := url.Parse(fmt.Sprintf(AzureBlobURI, container, escapedConnStr))
|
|
|
|
require.NoError(err)
|
|
|
|
rContainer, rConnStr, err := getAzureBlobConfig(uri)
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(container, rContainer)
|
|
|
|
assert.Equal(connStr, rConnStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetConfig(t *testing.T) {
|
2022-07-08 04:59:59 -04:00
|
|
|
const testURI = "test://config?name=test-name&data=test-data&value=test-value"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri string
|
|
|
|
keys []string
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2022-07-08 04:59:59 -04:00
|
|
|
uri: testURI,
|
2022-04-26 10:54:05 -04:00
|
|
|
keys: []string{"name", "data", "value"},
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"less keys than capture groups": {
|
2022-07-08 04:59:59 -04:00
|
|
|
uri: testURI,
|
2022-04-26 10:54:05 -04:00
|
|
|
keys: []string{"name", "data"},
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"invalid regex": {
|
2022-07-08 04:59:59 -04:00
|
|
|
uri: testURI,
|
2022-04-26 10:54:05 -04:00
|
|
|
keys: []string{"name", "data", "test-value"},
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"missing value": {
|
2022-04-26 10:54:05 -04:00
|
|
|
uri: "test://config?name=test-name&data=test-data&value",
|
|
|
|
keys: []string{"name", "data", "value"},
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"more keys than expected": {
|
2022-07-08 04:59:59 -04:00
|
|
|
uri: testURI,
|
2022-04-26 10:54:05 -04:00
|
|
|
keys: []string{"name", "data", "value", "anotherValue"},
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
uri, err := url.Parse(tc.uri)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
res, err := getConfig(uri.Query(), tc.keys)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
assert.Len(res, len(tc.keys))
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
require.Len(res, len(tc.keys))
|
|
|
|
for i := range tc.keys {
|
|
|
|
assert.NotEmpty(res[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|