|
|
@@ -16,12 +16,10 @@ package gitlab
|
|
|
import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
+ "reflect"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
|
|
|
- utilpointer "k8s.io/utils/pointer"
|
|
|
-
|
|
|
- "github.com/IBM/go-sdk-core/core"
|
|
|
gitlab "github.com/xanzy/go-gitlab"
|
|
|
|
|
|
esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
|
|
|
@@ -29,31 +27,33 @@ import (
|
|
|
)
|
|
|
|
|
|
type secretManagerTestCase struct {
|
|
|
- mockClient *fakegitlab.GitlabMockClient
|
|
|
- apiInput *gitlab.ListProjectVariablesOptions
|
|
|
- apiOutput *gitlab.ProjectVariable
|
|
|
- ref *esv1alpha1.ExternalSecretDataRemoteRef
|
|
|
- serviceURL *string
|
|
|
- apiErr error
|
|
|
- expectError string
|
|
|
- expectedSecret string
|
|
|
+ mockClient *fakegitlab.GitlabMockClient
|
|
|
+ apiInputProjectID string
|
|
|
+ apiInputKey string
|
|
|
+ apiOutput *gitlab.ProjectVariable
|
|
|
+ ref *esv1alpha1.ExternalSecretDataRemoteRef
|
|
|
+ projectID *string
|
|
|
+ apiErr error
|
|
|
+ expectError string
|
|
|
+ expectedSecret string
|
|
|
// for testing secretmap
|
|
|
expectedData map[string][]byte
|
|
|
}
|
|
|
|
|
|
func makeValidSecretManagerTestCase() *secretManagerTestCase {
|
|
|
smtc := secretManagerTestCase{
|
|
|
- mockClient: &fakegitlab.GitlabMockClient{},
|
|
|
- apiInput: makeValidAPIInput(),
|
|
|
- ref: makeValidRef(),
|
|
|
- apiOutput: makeValidAPIOutput(),
|
|
|
- serviceURL: nil,
|
|
|
- apiErr: nil,
|
|
|
- expectError: "",
|
|
|
- expectedSecret: "",
|
|
|
- expectedData: map[string][]byte{},
|
|
|
+ mockClient: &fakegitlab.GitlabMockClient{},
|
|
|
+ apiInputProjectID: makeValidAPIInputProjectID(),
|
|
|
+ apiInputKey: makeValidAPIInputKey(),
|
|
|
+ ref: makeValidRef(),
|
|
|
+ projectID: nil,
|
|
|
+ apiOutput: makeValidAPIOutput(),
|
|
|
+ apiErr: nil,
|
|
|
+ expectError: "",
|
|
|
+ expectedSecret: "",
|
|
|
+ expectedData: map[string][]byte{},
|
|
|
}
|
|
|
- smtc.mockClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
|
|
|
+ smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiErr)
|
|
|
return &smtc
|
|
|
}
|
|
|
|
|
|
@@ -64,25 +64,18 @@ func makeValidRef() *esv1alpha1.ExternalSecretDataRemoteRef {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func makeValidAPIInput() *gitlab.ProjectVariable {
|
|
|
- return &gitlab.CreateProjectVariableOptions{
|
|
|
- VariableType: core.StringPtr(gitlab.),
|
|
|
- ID: utilpointer.StringPtr("test-secret"),
|
|
|
- }
|
|
|
+func makeValidAPIInputProjectID() string {
|
|
|
+ return "testID"
|
|
|
}
|
|
|
|
|
|
-func makeValidAPIOutput() *sm.GetSecret {
|
|
|
- secretData := make(map[string]interface{})
|
|
|
- secretData["payload"] = ""
|
|
|
+func makeValidAPIInputKey() string {
|
|
|
+ return "testKey"
|
|
|
+}
|
|
|
|
|
|
- return &gitlab.GetSecret{
|
|
|
- Resources: []gitlab.SecretResourceIntf{
|
|
|
- &gitlab.SecretResource{
|
|
|
- Type: utilpointer.StringPtr("testytype"),
|
|
|
- Name: utilpointer.StringPtr("testyname"),
|
|
|
- SecretData: secretData,
|
|
|
- },
|
|
|
- },
|
|
|
+func makeValidAPIOutput() *gitlab.ProjectVariable {
|
|
|
+ return &gitlab.ProjectVariable{
|
|
|
+ Key: "testKey",
|
|
|
+ Value: "",
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -91,7 +84,7 @@ func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTest
|
|
|
for _, fn := range tweaks {
|
|
|
fn(smtc)
|
|
|
}
|
|
|
- smtc.mockClient.WithValue(smtc.apiInput, smtc.apiOutput, smtc.apiErr)
|
|
|
+ smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiErr)
|
|
|
return smtc
|
|
|
}
|
|
|
|
|
|
@@ -104,47 +97,26 @@ var setAPIErr = func(smtc *secretManagerTestCase) {
|
|
|
|
|
|
var setNilMockClient = func(smtc *secretManagerTestCase) {
|
|
|
smtc.mockClient = nil
|
|
|
- smtc.expectError = errUninitalizedIBMProvider
|
|
|
+ smtc.expectError = errUninitalizedGitlabProvider
|
|
|
}
|
|
|
|
|
|
// test the sm<->gcp interface
|
|
|
// make sure correct values are passed and errors are handled accordingly.
|
|
|
-func TestIBMSecretManagerGetSecret(t *testing.T) {
|
|
|
- secretData := make(map[string]interface{})
|
|
|
+func TestGitlabSecretManagerGetSecret(t *testing.T) {
|
|
|
secretValue := "changedvalue"
|
|
|
- secretData["payload"] = secretValue
|
|
|
// good case: default version is set
|
|
|
// key is passed in, output is sent back
|
|
|
- setSecretString := func(smtc *secretManagerTestCase) {
|
|
|
- resources := []gitlab.SecretResourceIntf{
|
|
|
- &gitlab.SecretResource{
|
|
|
- Type: utilpointer.StringPtr("testytype"),
|
|
|
- Name: utilpointer.StringPtr("testyname"),
|
|
|
- SecretData: secretData,
|
|
|
- }}
|
|
|
|
|
|
- smtc.apiOutput.Resources = resources
|
|
|
- smtc.expectedSecret = secretValue
|
|
|
- }
|
|
|
-
|
|
|
- // good case: custom version set
|
|
|
- setCustomKey := func(smtc *secretManagerTestCase) {
|
|
|
- resources := []gitlab.SecretResourceIntf{
|
|
|
- &gitlab.SecretResource{
|
|
|
- Type: utilpointer.StringPtr("testytype"),
|
|
|
- Name: utilpointer.StringPtr("testyname"),
|
|
|
- SecretData: secretData,
|
|
|
- }}
|
|
|
- smtc.ref.Key = "testyname"
|
|
|
- smtc.apiInput.ID = utilpointer.StringPtr("testyname")
|
|
|
- smtc.apiOutput.Resources = resources
|
|
|
+ setSecretString := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.apiOutput = &gitlab.ProjectVariable{
|
|
|
+ Key: "testkey",
|
|
|
+ Value: "changedvalue",
|
|
|
+ }
|
|
|
smtc.expectedSecret = secretValue
|
|
|
}
|
|
|
|
|
|
successCases := []*secretManagerTestCase{
|
|
|
- makeValidSecretManagerTestCase(),
|
|
|
makeValidSecretManagerTestCaseCustom(setSecretString),
|
|
|
- makeValidSecretManagerTestCaseCustom(setCustomKey),
|
|
|
makeValidSecretManagerTestCaseCustom(setAPIErr),
|
|
|
makeValidSecretManagerTestCaseCustom(setNilMockClient),
|
|
|
}
|
|
|
@@ -162,59 +134,38 @@ func TestIBMSecretManagerGetSecret(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// func TestGetSecretMap(t *testing.T) {
|
|
|
-// // good case: default version & deserialization
|
|
|
-// setDeserialization := func(smtc *secretManagerTestCase) {
|
|
|
-// secretData := make(map[string]interface{})
|
|
|
-// secretValue := `{"foo":"bar"}`
|
|
|
-// secretData["payload"] = secretValue
|
|
|
-// resources := []sm.SecretResourceIntf{
|
|
|
-// &sm.SecretResource{
|
|
|
-// Type: utilpointer.StringPtr("testytype"),
|
|
|
-// Name: utilpointer.StringPtr("testyname"),
|
|
|
-// SecretData: secretData,
|
|
|
-// }}
|
|
|
-// smtc.apiOutput.Resources = resources
|
|
|
-// smtc.expectedData["foo"] = []byte("bar")
|
|
|
-// }
|
|
|
-
|
|
|
-// // bad case: invalid json
|
|
|
-// setInvalidJSON := func(smtc *secretManagerTestCase) {
|
|
|
-// secretData := make(map[string]interface{})
|
|
|
-
|
|
|
-// secretData["payload"] = `-----------------`
|
|
|
-
|
|
|
-// resources := []sm.SecretResourceIntf{
|
|
|
-// &sm.SecretResource{
|
|
|
-// Type: utilpointer.StringPtr("testytype"),
|
|
|
-// Name: utilpointer.StringPtr("testyname"),
|
|
|
-// SecretData: secretData,
|
|
|
-// }}
|
|
|
-
|
|
|
-// smtc.apiOutput.Resources = resources
|
|
|
+func TestGetSecretMap(t *testing.T) {
|
|
|
+ // good case: default version & deserialization
|
|
|
+ setDeserialization := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.apiOutput.Value = `{"foo":"bar"}`
|
|
|
+ smtc.expectedData["foo"] = []byte("bar")
|
|
|
+ }
|
|
|
|
|
|
-// smtc.expectError = "unable to unmarshal secret: invalid character '-' in numeric literal"
|
|
|
-// }
|
|
|
+ // bad case: invalid json
|
|
|
+ setInvalidJSON := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.apiOutput.Value = `-----------------`
|
|
|
+ smtc.expectError = "unable to unmarshal secret"
|
|
|
+ }
|
|
|
|
|
|
-// successCases := []*secretManagerTestCase{
|
|
|
-// makeValidSecretManagerTestCaseCustom(setDeserialization),
|
|
|
-// makeValidSecretManagerTestCaseCustom(setInvalidJSON),
|
|
|
-// makeValidSecretManagerTestCaseCustom(setNilMockClient),
|
|
|
-// makeValidSecretManagerTestCaseCustom(setAPIErr),
|
|
|
-// }
|
|
|
+ successCases := []*secretManagerTestCase{
|
|
|
+ makeValidSecretManagerTestCaseCustom(setDeserialization),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setInvalidJSON),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setNilMockClient),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setAPIErr),
|
|
|
+ }
|
|
|
|
|
|
-// sm := providerIBM{}
|
|
|
-// for k, v := range successCases {
|
|
|
-// sm.IBMClient = v.mockClient
|
|
|
-// out, err := sm.GetSecretMap(context.Background(), *v.ref)
|
|
|
-// if !ErrorContains(err, v.expectError) {
|
|
|
-// t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
|
|
|
-// }
|
|
|
-// if err == nil && !reflect.DeepEqual(out, v.expectedData) {
|
|
|
-// t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
+ sm := Gitlab{}
|
|
|
+ for k, v := range successCases {
|
|
|
+ sm.client = v.mockClient
|
|
|
+ out, err := sm.GetSecretMap(context.Background(), *v.ref)
|
|
|
+ if !ErrorContains(err, v.expectError) {
|
|
|
+ t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
|
|
|
+ }
|
|
|
+ if err == nil && !reflect.DeepEqual(out, v.expectedData) {
|
|
|
+ t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
func ErrorContains(out error, want string) bool {
|
|
|
if out == nil {
|
|
|
@@ -225,50 +176,3 @@ func ErrorContains(out error, want string) bool {
|
|
|
}
|
|
|
return strings.Contains(out.Error(), want)
|
|
|
}
|
|
|
-
|
|
|
-// NOT WORKING CURRENTLY
|
|
|
-
|
|
|
-// func TestCreateGitlabClient(t *testing.T) {
|
|
|
-// credentials := GitlabCredentials{Token: GITLAB_TOKEN}
|
|
|
-// gitlab := NewGitlabProvider()
|
|
|
-// gitlab.SetAuth(credentials, GITLAB_PROJECT_ID)
|
|
|
-
|
|
|
-// // user, _, _ := gitlab.client.Users.CurrentUser()
|
|
|
-// // fmt.Printf("Created client for username: %v", user)
|
|
|
-// }
|
|
|
-
|
|
|
-// func TestGetSecret(t *testing.T) {
|
|
|
-// ctx := context.Background()
|
|
|
-
|
|
|
-// ref := v1alpha1.ExternalSecretDataRemoteRef{Key: "mySecretBanana"}
|
|
|
-
|
|
|
-// credentials := GitlabCredentials{Token: GITLAB_TOKEN}
|
|
|
-// gitlab := NewGitlabProvider()
|
|
|
-// gitlab.SetAuth(credentials, GITLAB_PROJECT_ID)
|
|
|
-
|
|
|
-// secretData, err := gitlab.GetSecret(ctx, ref)
|
|
|
-
|
|
|
-// if err != nil {
|
|
|
-// fmt.Errorf("error retrieving secret, %w", err)
|
|
|
-// }
|
|
|
-
|
|
|
-// fmt.Printf("Got secret data %v", string(secretData))
|
|
|
-// }
|
|
|
-
|
|
|
-// func TestGetSecretMap(t *testing.T) {
|
|
|
-// ctx := context.Background()
|
|
|
-
|
|
|
-// ref := v1alpha1.ExternalSecretDataRemoteRef{Key: "myJsonSecret"}
|
|
|
-
|
|
|
-// credentials := GitlabCredentials{Token: GITLAB_TOKEN}
|
|
|
-// gitlab := NewGitlabProvider()
|
|
|
-// gitlab.SetAuth(credentials, GITLAB_PROJECT_ID)
|
|
|
-
|
|
|
-// secretData, err := gitlab.GetSecretMap(ctx, ref)
|
|
|
-
|
|
|
-// if err != nil {
|
|
|
-// fmt.Errorf("error retrieving secret map, %w", err)
|
|
|
-// }
|
|
|
-
|
|
|
-// fmt.Printf("Got secret map: %v", secretData)
|
|
|
-// }
|