|
|
@@ -5,6 +5,7 @@ import (
|
|
|
"testing"
|
|
|
|
|
|
"github.com/opencost/opencost/core/pkg/log"
|
|
|
+ "github.com/opencost/opencost/core/pkg/storage"
|
|
|
"github.com/opencost/opencost/core/pkg/util/json"
|
|
|
"github.com/opencost/opencost/pkg/cloud"
|
|
|
)
|
|
|
@@ -122,6 +123,30 @@ func TestStorageConfiguration_Validate(t *testing.T) {
|
|
|
},
|
|
|
expected: nil,
|
|
|
},
|
|
|
+ "valid config StorageConnectionStringCredential": {
|
|
|
+ config: StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{
|
|
|
+ StorageConnectionString: "storageConnectionString",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ expected: nil,
|
|
|
+ },
|
|
|
+ "missing storage connection string": {
|
|
|
+ config: StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{},
|
|
|
+ },
|
|
|
+ expected: fmt.Errorf("StorageConnectionStringCredential: missing storage connection string"),
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
for name, testCase := range testCases {
|
|
|
@@ -426,6 +451,79 @@ func TestStorageConfiguration_Equals(t *testing.T) {
|
|
|
},
|
|
|
expected: false,
|
|
|
},
|
|
|
+ "matching config StorageConnectionStringCredential": {
|
|
|
+ left: StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{
|
|
|
+ StorageConnectionString: "storageConnectionString",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ right: &StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{
|
|
|
+ StorageConnectionString: "storageConnectionString",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ expected: true,
|
|
|
+ },
|
|
|
+ "different StorageConnectionString in StorageConnectionStringCredential": {
|
|
|
+ left: StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{
|
|
|
+ StorageConnectionString: "storageConnectionString1",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ right: &StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{
|
|
|
+ StorageConnectionString: "storageConnectionString2",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ expected: false,
|
|
|
+ },
|
|
|
+ "different HTTPConfig in StorageConnectionStringCredential": {
|
|
|
+ left: StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{
|
|
|
+ StorageConnectionString: "storageConnectionString",
|
|
|
+ HTTPConfig: defaultHTTPConfig,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ right: &StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{
|
|
|
+ StorageConnectionString: "storageConnectionString",
|
|
|
+ HTTPConfig: storage.HTTPConfig{
|
|
|
+ InsecureSkipVerify: true,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ expected: false,
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
for name, testCase := range testCases {
|
|
|
@@ -440,13 +538,19 @@ func TestStorageConfiguration_Equals(t *testing.T) {
|
|
|
|
|
|
func TestStorageConfiguration_JSON(t *testing.T) {
|
|
|
testCases := map[string]struct {
|
|
|
- config StorageConfiguration
|
|
|
+ input map[string]interface{}
|
|
|
+ afterUnmarshal StorageConfiguration
|
|
|
}{
|
|
|
- "Empty Config": {
|
|
|
- config: StorageConfiguration{},
|
|
|
- },
|
|
|
"Nil Authorizer": {
|
|
|
- config: StorageConfiguration{
|
|
|
+ input: map[string]interface{}{
|
|
|
+ "subscriptionID": "subscriptionID",
|
|
|
+ "account": "account",
|
|
|
+ "container": "container",
|
|
|
+ "path": "path",
|
|
|
+ "cloud": "cloud",
|
|
|
+ "authorizer": nil,
|
|
|
+ },
|
|
|
+ afterUnmarshal: StorageConfiguration{
|
|
|
SubscriptionID: "subscriptionID",
|
|
|
Account: "account",
|
|
|
Container: "container",
|
|
|
@@ -456,7 +560,19 @@ func TestStorageConfiguration_JSON(t *testing.T) {
|
|
|
},
|
|
|
},
|
|
|
"SharedKeyCredential Authorizer": {
|
|
|
- config: StorageConfiguration{
|
|
|
+ input: map[string]interface{}{
|
|
|
+ "subscriptionID": "subscriptionID",
|
|
|
+ "account": "account",
|
|
|
+ "container": "container",
|
|
|
+ "path": "path",
|
|
|
+ "cloud": "cloud",
|
|
|
+ "authorizer": map[string]interface{}{
|
|
|
+ "authorizerType": "AzureAccessKey",
|
|
|
+ "accessKey": "accessKey",
|
|
|
+ "account": "account",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ afterUnmarshal: StorageConfiguration{
|
|
|
SubscriptionID: "subscriptionID",
|
|
|
Account: "account",
|
|
|
Container: "container",
|
|
|
@@ -469,7 +585,17 @@ func TestStorageConfiguration_JSON(t *testing.T) {
|
|
|
},
|
|
|
},
|
|
|
"Default AuthorizerHolder Authorizer": {
|
|
|
- config: StorageConfiguration{
|
|
|
+ input: map[string]interface{}{
|
|
|
+ "subscriptionID": "subscriptionID",
|
|
|
+ "account": "account",
|
|
|
+ "container": "container",
|
|
|
+ "path": "path",
|
|
|
+ "cloud": "cloud",
|
|
|
+ "authorizer": map[string]interface{}{
|
|
|
+ "authorizerType": "AzureDefaultCredential",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ afterUnmarshal: StorageConfiguration{
|
|
|
SubscriptionID: "subscriptionID",
|
|
|
Account: "account",
|
|
|
Container: "container",
|
|
|
@@ -481,7 +607,20 @@ func TestStorageConfiguration_JSON(t *testing.T) {
|
|
|
},
|
|
|
},
|
|
|
"ClientSecretCredential Authorizer": {
|
|
|
- config: StorageConfiguration{
|
|
|
+ input: map[string]interface{}{
|
|
|
+ "subscriptionID": "subscriptionID",
|
|
|
+ "account": "account",
|
|
|
+ "container": "container",
|
|
|
+ "path": "path",
|
|
|
+ "cloud": "cloud",
|
|
|
+ "authorizer": map[string]interface{}{
|
|
|
+ "authorizerType": "AzureClientSecretCredential",
|
|
|
+ "tenantID": "tenantID",
|
|
|
+ "clientID": "clientID",
|
|
|
+ "clientSecret": "clientSecret",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ afterUnmarshal: StorageConfiguration{
|
|
|
SubscriptionID: "subscriptionID",
|
|
|
Account: "account",
|
|
|
Container: "container",
|
|
|
@@ -496,12 +635,43 @@ func TestStorageConfiguration_JSON(t *testing.T) {
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
+ "StorageConnectionStringCredential Authorizer": {
|
|
|
+ input: map[string]interface{}{
|
|
|
+ "subscriptionID": "subscriptionID",
|
|
|
+ "account": "account",
|
|
|
+ "container": "container",
|
|
|
+ "path": "path",
|
|
|
+ "cloud": "cloud",
|
|
|
+ "authorizer": map[string]interface{}{
|
|
|
+ "authorizerType": "AzureStorageConnectionString",
|
|
|
+ "storageConnectionString": "storageConnectionString",
|
|
|
+ "httpConfig": map[string]interface{}{
|
|
|
+ "insecureSkipVerify": true,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ afterUnmarshal: StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{
|
|
|
+ StorageConnectionString: "storageConnectionString",
|
|
|
+ HTTPConfig: func() storage.HTTPConfig {
|
|
|
+ cfg := defaultHTTPConfig
|
|
|
+ cfg.InsecureSkipVerify = true
|
|
|
+ return cfg
|
|
|
+ }(),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
for name, testCase := range testCases {
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
// test JSON Marshalling
|
|
|
- configJSON, err := json.Marshal(testCase.config)
|
|
|
+ configJSON, err := json.Marshal(testCase.input)
|
|
|
if err != nil {
|
|
|
t.Errorf("failed to marshal configuration: %s", err.Error())
|
|
|
}
|
|
|
@@ -512,9 +682,75 @@ func TestStorageConfiguration_JSON(t *testing.T) {
|
|
|
t.Errorf("failed to unmarshal configuration: %s", err.Error())
|
|
|
}
|
|
|
|
|
|
- if !testCase.config.Equals(unmarshalledConfig) {
|
|
|
+ if !testCase.afterUnmarshal.Equals(unmarshalledConfig) {
|
|
|
t.Error("config does not equal unmarshalled config")
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func TestStorageConfiguration_Sanitize(t *testing.T) {
|
|
|
+ testCases := map[string]struct {
|
|
|
+ config StorageConfiguration
|
|
|
+ expected StorageConfiguration
|
|
|
+ }{
|
|
|
+ "Sanitize StorageConnectionStringCredential": {
|
|
|
+ config: StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{
|
|
|
+ StorageConnectionString: "storageConnectionString",
|
|
|
+ HTTPConfig: defaultHTTPConfig,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ expected: StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &StorageConnectionStringCredential{
|
|
|
+ StorageConnectionString: cloud.Redacted,
|
|
|
+ HTTPConfig: defaultHTTPConfig,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ "Sanitize SharedKeyCredential": {
|
|
|
+ config: StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &SharedKeyCredential{
|
|
|
+ AccessKey: "accessKey",
|
|
|
+ Account: "account",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ expected: StorageConfiguration{
|
|
|
+ SubscriptionID: "subscriptionID",
|
|
|
+ Account: "account",
|
|
|
+ Container: "container",
|
|
|
+ Path: "path",
|
|
|
+ Cloud: "cloud",
|
|
|
+ Authorizer: &SharedKeyCredential{
|
|
|
+ AccessKey: cloud.Redacted,
|
|
|
+ Account: "account",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ for name, testCase := range testCases {
|
|
|
+ t.Run(name, func(t *testing.T) {
|
|
|
+ actual := testCase.config.Sanitize()
|
|
|
+
|
|
|
+ if !testCase.expected.Equals(actual) {
|
|
|
+ t.Errorf("incorrect result: got %#v, want %#v", actual, testCase.expected)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|