|
|
@@ -41,11 +41,13 @@ const (
|
|
|
username = "user-name"
|
|
|
userkey = "user-key"
|
|
|
environment = "prod"
|
|
|
+ environmentTest = "test"
|
|
|
projectvalue = "projectvalue"
|
|
|
groupvalue = "groupvalue"
|
|
|
groupid = "groupId"
|
|
|
defaultErrorMessage = "[%d] unexpected error: [%s], expected: [%s]"
|
|
|
errMissingCredentials = "credentials are empty"
|
|
|
+ testKey = "testKey"
|
|
|
findTestPrefix = "test.*"
|
|
|
)
|
|
|
|
|
|
@@ -58,8 +60,10 @@ type secretManagerTestCase struct {
|
|
|
apiInputEnv string
|
|
|
projectAPIOutput *gitlab.ProjectVariable
|
|
|
projectAPIResponse *gitlab.Response
|
|
|
+ projectAPIOutputs []*fakegitlab.APIResponse[[]*gitlab.ProjectVariable]
|
|
|
projectGroupsAPIOutput []*gitlab.ProjectGroup
|
|
|
projectGroupsAPIResponse *gitlab.Response
|
|
|
+ groupAPIOutputs []*fakegitlab.APIResponse[[]*gitlab.GroupVariable]
|
|
|
groupAPIOutput *gitlab.GroupVariable
|
|
|
groupAPIResponse *gitlab.Response
|
|
|
ref *esv1beta1.ExternalSecretDataRemoteRef
|
|
|
@@ -99,14 +103,14 @@ func makeValidSecretManagerTestCase() *secretManagerTestCase {
|
|
|
expectedValidationResult: esv1beta1.ValidationResultReady,
|
|
|
expectedData: map[string][]byte{},
|
|
|
}
|
|
|
- smtc.mockProjectVarClient.WithValue(smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
|
|
|
- smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
|
|
|
+ prepareMockProjectVarClient(&smtc)
|
|
|
+ prepareMockGroupVarClient(&smtc)
|
|
|
return &smtc
|
|
|
}
|
|
|
|
|
|
func makeValidRef() *esv1beta1.ExternalSecretDataRemoteRef {
|
|
|
return &esv1beta1.ExternalSecretDataRemoteRef{
|
|
|
- Key: "test-secret",
|
|
|
+ Key: testKey,
|
|
|
Version: "default",
|
|
|
}
|
|
|
}
|
|
|
@@ -134,7 +138,7 @@ func makeValidAPIInputProjectID() string {
|
|
|
}
|
|
|
|
|
|
func makeValidAPIInputKey() string {
|
|
|
- return "testKey"
|
|
|
+ return testKey
|
|
|
}
|
|
|
|
|
|
func makeValidEnvironment() string {
|
|
|
@@ -146,6 +150,8 @@ func makeValidProjectAPIResponse() *gitlab.Response {
|
|
|
Response: &http.Response{
|
|
|
StatusCode: http.StatusOK,
|
|
|
},
|
|
|
+ CurrentPage: 1,
|
|
|
+ TotalPages: 1,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -154,6 +160,8 @@ func makeValidProjectGroupsAPIResponse() *gitlab.Response {
|
|
|
Response: &http.Response{
|
|
|
StatusCode: http.StatusOK,
|
|
|
},
|
|
|
+ CurrentPage: 1,
|
|
|
+ TotalPages: 1,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -162,12 +170,14 @@ func makeValidGroupAPIResponse() *gitlab.Response {
|
|
|
Response: &http.Response{
|
|
|
StatusCode: http.StatusOK,
|
|
|
},
|
|
|
+ CurrentPage: 1,
|
|
|
+ TotalPages: 1,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func makeValidProjectAPIOutput() *gitlab.ProjectVariable {
|
|
|
return &gitlab.ProjectVariable{
|
|
|
- Key: "testKey",
|
|
|
+ Key: testKey,
|
|
|
Value: "",
|
|
|
EnvironmentScope: environment,
|
|
|
}
|
|
|
@@ -203,8 +213,8 @@ func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTest
|
|
|
fn(smtc)
|
|
|
}
|
|
|
smtc.mockProjectsClient.WithValue(smtc.projectGroupsAPIOutput, smtc.projectGroupsAPIResponse, smtc.apiErr)
|
|
|
- smtc.mockProjectVarClient.WithValue(smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
|
|
|
- smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
|
|
|
+ prepareMockProjectVarClient(smtc)
|
|
|
+ prepareMockGroupVarClient(smtc)
|
|
|
return smtc
|
|
|
}
|
|
|
|
|
|
@@ -215,12 +225,33 @@ func makeValidSecretManagerGetAllTestCaseCustom(tweaks ...func(smtc *secretManag
|
|
|
for _, fn := range tweaks {
|
|
|
fn(smtc)
|
|
|
}
|
|
|
- smtc.mockProjectVarClient.WithValue(smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
|
|
|
- smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
|
|
|
-
|
|
|
+ prepareMockProjectVarClient(smtc)
|
|
|
+ prepareMockGroupVarClient(smtc)
|
|
|
return smtc
|
|
|
}
|
|
|
|
|
|
+func prepareMockProjectVarClient(smtc *secretManagerTestCase) {
|
|
|
+ responses := make([]fakegitlab.APIResponse[[]*gitlab.ProjectVariable], 0)
|
|
|
+ if smtc.projectAPIOutput != nil {
|
|
|
+ responses = append(responses, fakegitlab.APIResponse[[]*gitlab.ProjectVariable]{Output: []*gitlab.ProjectVariable{smtc.projectAPIOutput}, Response: smtc.projectAPIResponse, Error: smtc.apiErr})
|
|
|
+ }
|
|
|
+ for _, response := range smtc.projectAPIOutputs {
|
|
|
+ responses = append(responses, *response)
|
|
|
+ }
|
|
|
+ smtc.mockProjectVarClient.WithValues(responses)
|
|
|
+}
|
|
|
+
|
|
|
+func prepareMockGroupVarClient(smtc *secretManagerTestCase) {
|
|
|
+ responses := make([]fakegitlab.APIResponse[[]*gitlab.GroupVariable], 0)
|
|
|
+ if smtc.projectAPIOutput != nil {
|
|
|
+ responses = append(responses, fakegitlab.APIResponse[[]*gitlab.GroupVariable]{Output: []*gitlab.GroupVariable{smtc.groupAPIOutput}, Response: smtc.groupAPIResponse, Error: smtc.apiErr})
|
|
|
+ }
|
|
|
+ for _, response := range smtc.groupAPIOutputs {
|
|
|
+ responses = append(responses, *response)
|
|
|
+ }
|
|
|
+ smtc.mockGroupVarClient.WithValues(responses)
|
|
|
+}
|
|
|
+
|
|
|
// This case can be shared by both GetSecret and GetSecretMap tests.
|
|
|
// bad case: set apiErr.
|
|
|
var setAPIErr = func(smtc *secretManagerTestCase) {
|
|
|
@@ -380,14 +411,14 @@ func TestGetSecret(t *testing.T) {
|
|
|
}
|
|
|
groupSecretProjectOverride := func(smtc *secretManagerTestCase) {
|
|
|
smtc.projectAPIOutput.Value = projectvalue
|
|
|
- smtc.groupAPIOutput.Key = "testkey"
|
|
|
+ smtc.groupAPIOutput.Key = testKey
|
|
|
smtc.groupAPIOutput.Value = groupvalue
|
|
|
smtc.expectedSecret = smtc.projectAPIOutput.Value
|
|
|
}
|
|
|
groupWithoutProjectOverride := func(smtc *secretManagerTestCase) {
|
|
|
smtc.groupIDs = []string{groupid}
|
|
|
smtc.projectAPIResponse.Response.StatusCode = 404
|
|
|
- smtc.groupAPIOutput.Key = "testkey"
|
|
|
+ smtc.groupAPIOutput.Key = testKey
|
|
|
smtc.groupAPIOutput.Value = groupvalue
|
|
|
smtc.expectedSecret = smtc.groupAPIOutput.Value
|
|
|
}
|
|
|
@@ -452,7 +483,7 @@ func TestGetAllSecrets(t *testing.T) {
|
|
|
}
|
|
|
setMatchingSecretFindString := func(smtc *secretManagerTestCase) {
|
|
|
smtc.projectAPIOutput = &gitlab.ProjectVariable{
|
|
|
- Key: "testkey",
|
|
|
+ Key: testKey,
|
|
|
Value: projectvalue,
|
|
|
EnvironmentScope: environment,
|
|
|
}
|
|
|
@@ -461,25 +492,25 @@ func TestGetAllSecrets(t *testing.T) {
|
|
|
}
|
|
|
setNoMatchingRegexpFindString := func(smtc *secretManagerTestCase) {
|
|
|
smtc.projectAPIOutput = &gitlab.ProjectVariable{
|
|
|
- Key: "testkey",
|
|
|
+ Key: testKey,
|
|
|
Value: projectvalue,
|
|
|
- EnvironmentScope: "test",
|
|
|
+ EnvironmentScope: environmentTest,
|
|
|
}
|
|
|
smtc.expectedSecret = ""
|
|
|
smtc.refFind.Name = makeFindName("foo.*")
|
|
|
}
|
|
|
setUnmatchedEnvironmentFindString := func(smtc *secretManagerTestCase) {
|
|
|
smtc.projectAPIOutput = &gitlab.ProjectVariable{
|
|
|
- Key: "testkey",
|
|
|
+ Key: testKey,
|
|
|
Value: projectvalue,
|
|
|
- EnvironmentScope: "test",
|
|
|
+ EnvironmentScope: environmentTest,
|
|
|
}
|
|
|
smtc.expectedSecret = ""
|
|
|
smtc.refFind.Name = makeFindName(findTestPrefix)
|
|
|
}
|
|
|
setMatchingSecretFindTags := func(smtc *secretManagerTestCase) {
|
|
|
smtc.projectAPIOutput = &gitlab.ProjectVariable{
|
|
|
- Key: "testkey",
|
|
|
+ Key: testKey,
|
|
|
Value: projectvalue,
|
|
|
EnvironmentScope: environment,
|
|
|
}
|
|
|
@@ -492,6 +523,108 @@ func TestGetAllSecrets(t *testing.T) {
|
|
|
smtc.expectError = "'find.tags' is constrained by 'environment_scope' of the store"
|
|
|
smtc.refFind.Tags = map[string]string{"environment_scope": environment}
|
|
|
}
|
|
|
+ setWildcardDoesntOverwriteEnvironmentValue := func(smtc *secretManagerTestCase) {
|
|
|
+ var1 := gitlab.ProjectVariable{
|
|
|
+ Key: testKey,
|
|
|
+ Value: "wildcardValue",
|
|
|
+ EnvironmentScope: "*",
|
|
|
+ }
|
|
|
+ var2 := gitlab.ProjectVariable{
|
|
|
+ Key: testKey,
|
|
|
+ Value: "expectedValue",
|
|
|
+ EnvironmentScope: environmentTest,
|
|
|
+ }
|
|
|
+ var3 := gitlab.ProjectVariable{
|
|
|
+ Key: testKey,
|
|
|
+ Value: "wildcardValue",
|
|
|
+ EnvironmentScope: "*",
|
|
|
+ }
|
|
|
+ vars := []*gitlab.ProjectVariable{&var1, &var2, &var3}
|
|
|
+ smtc.projectAPIOutputs = []*fakegitlab.APIResponse[[]*gitlab.ProjectVariable]{{Output: vars, Response: smtc.projectAPIResponse, Error: nil}}
|
|
|
+ smtc.projectAPIOutput = nil
|
|
|
+ smtc.apiInputEnv = environmentTest
|
|
|
+ smtc.expectedSecret = "expectedValue"
|
|
|
+ smtc.refFind.Name = makeFindName(findTestPrefix)
|
|
|
+ }
|
|
|
+ setFilterByEnvironmentWithWildcard := func(smtc *secretManagerTestCase) {
|
|
|
+ var1 := gitlab.ProjectVariable{
|
|
|
+ Key: testKey,
|
|
|
+ Value: projectvalue,
|
|
|
+ EnvironmentScope: "*",
|
|
|
+ }
|
|
|
+ var2 := gitlab.ProjectVariable{
|
|
|
+ Key: "testKey2",
|
|
|
+ Value: "value2",
|
|
|
+ EnvironmentScope: environment,
|
|
|
+ }
|
|
|
+ var3 := gitlab.ProjectVariable{
|
|
|
+ Key: "testKey3",
|
|
|
+ Value: "value3",
|
|
|
+ EnvironmentScope: environmentTest,
|
|
|
+ }
|
|
|
+ var4 := gitlab.ProjectVariable{
|
|
|
+ Key: "anotherKey4",
|
|
|
+ Value: "value4",
|
|
|
+ EnvironmentScope: environment,
|
|
|
+ }
|
|
|
+ vars := []*gitlab.ProjectVariable{&var1, &var2, &var3, &var4}
|
|
|
+ smtc.projectAPIOutput = nil
|
|
|
+ smtc.projectAPIOutputs = []*fakegitlab.APIResponse[[]*gitlab.ProjectVariable]{{Output: vars, Response: smtc.projectAPIResponse, Error: nil}}
|
|
|
+ smtc.apiInputEnv = environment
|
|
|
+ smtc.expectedData = map[string][]byte{testKey: []byte(projectvalue), "testKey2": []byte("value2")}
|
|
|
+ smtc.refFind.Name = makeFindName(findTestPrefix)
|
|
|
+ }
|
|
|
+ setPaginationInGroupAndProjectVars := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.groupIDs = []string{groupid}
|
|
|
+ gvar1 := gitlab.GroupVariable{
|
|
|
+ Key: testKey + "Group",
|
|
|
+ Value: "groupValue1",
|
|
|
+ EnvironmentScope: environmentTest,
|
|
|
+ }
|
|
|
+ gvar2 := gitlab.GroupVariable{
|
|
|
+ Key: testKey,
|
|
|
+ Value: "groupValue2",
|
|
|
+ EnvironmentScope: environmentTest,
|
|
|
+ }
|
|
|
+ pvar1 := gitlab.ProjectVariable{
|
|
|
+ Key: testKey,
|
|
|
+ Value: "testValue1",
|
|
|
+ EnvironmentScope: environmentTest,
|
|
|
+ }
|
|
|
+ pvar2a := gitlab.ProjectVariable{
|
|
|
+ Key: testKey + "2a",
|
|
|
+ Value: "testValue2a",
|
|
|
+ EnvironmentScope: environmentTest,
|
|
|
+ }
|
|
|
+ pvar2b := gitlab.ProjectVariable{
|
|
|
+ Key: testKey + "2b",
|
|
|
+ Value: "testValue2b",
|
|
|
+ EnvironmentScope: environmentTest,
|
|
|
+ }
|
|
|
+ gPage1 := []*gitlab.GroupVariable{&gvar1}
|
|
|
+ gResponsePage1 := makeValidGroupAPIResponse()
|
|
|
+ gResponsePage1.TotalPages = 2
|
|
|
+ gResponsePage1.CurrentPage = 1
|
|
|
+ gPage2 := []*gitlab.GroupVariable{&gvar2}
|
|
|
+ gResponsePage2 := makeValidGroupAPIResponse()
|
|
|
+ gResponsePage2.TotalPages = 2
|
|
|
+ gResponsePage2.CurrentPage = 1
|
|
|
+ pPage1 := []*gitlab.ProjectVariable{&pvar1}
|
|
|
+ pResponsePage1 := makeValidProjectAPIResponse()
|
|
|
+ pResponsePage1.TotalPages = 2
|
|
|
+ pResponsePage1.CurrentPage = 1
|
|
|
+ pPage2 := []*gitlab.ProjectVariable{&pvar2a, &pvar2b}
|
|
|
+ pResponsePage2 := makeValidProjectAPIResponse()
|
|
|
+ pResponsePage2.TotalPages = 2
|
|
|
+ pResponsePage2.CurrentPage = 2
|
|
|
+ smtc.groupAPIOutputs = []*fakegitlab.APIResponse[[]*gitlab.GroupVariable]{{Output: gPage1, Response: gResponsePage1, Error: nil}, {Output: gPage2, Response: gResponsePage2, Error: nil}}
|
|
|
+ smtc.groupAPIOutput = nil
|
|
|
+ smtc.projectAPIOutputs = []*fakegitlab.APIResponse[[]*gitlab.ProjectVariable]{{Output: pPage1, Response: pResponsePage1, Error: nil}, {Output: pPage2, Response: pResponsePage2, Error: nil}}
|
|
|
+ smtc.projectAPIOutput = nil
|
|
|
+ smtc.apiInputEnv = environmentTest
|
|
|
+ smtc.expectedData = map[string][]byte{testKey: []byte("testValue1"), "testKey2a": []byte("testValue2a"), "testKey2b": []byte("testValue2b"), "testKeyGroup": []byte("groupValue1")}
|
|
|
+ smtc.refFind.Name = makeFindName(findTestPrefix)
|
|
|
+ }
|
|
|
|
|
|
cases := []*secretManagerTestCase{
|
|
|
makeValidSecretManagerGetAllTestCaseCustom(setMissingFindRegex),
|
|
|
@@ -501,7 +634,10 @@ func TestGetAllSecrets(t *testing.T) {
|
|
|
makeValidSecretManagerGetAllTestCaseCustom(setNoMatchingRegexpFindString),
|
|
|
makeValidSecretManagerGetAllTestCaseCustom(setUnmatchedEnvironmentFindString),
|
|
|
makeValidSecretManagerGetAllTestCaseCustom(setMatchingSecretFindTags),
|
|
|
+ makeValidSecretManagerGetAllTestCaseCustom(setWildcardDoesntOverwriteEnvironmentValue),
|
|
|
makeValidSecretManagerGetAllTestCaseCustom(setEnvironmentConstrainedByStore),
|
|
|
+ makeValidSecretManagerGetAllTestCaseCustom(setFilterByEnvironmentWithWildcard),
|
|
|
+ makeValidSecretManagerGetAllTestCaseCustom(setPaginationInGroupAndProjectVars),
|
|
|
makeValidSecretManagerGetAllTestCaseCustom(setAPIErr),
|
|
|
makeValidSecretManagerGetAllTestCaseCustom(setNilMockClient),
|
|
|
}
|
|
|
@@ -511,12 +647,16 @@ func TestGetAllSecrets(t *testing.T) {
|
|
|
sm.environment = v.apiInputEnv
|
|
|
sm.projectVariablesClient = v.mockProjectVarClient
|
|
|
sm.groupVariablesClient = v.mockGroupVarClient
|
|
|
+ sm.groupIDs = v.groupIDs
|
|
|
+ if v.expectedSecret != "" {
|
|
|
+ v.expectedData = map[string][]byte{testKey: []byte(v.expectedSecret)}
|
|
|
+ }
|
|
|
out, err := sm.GetAllSecrets(context.Background(), *v.refFind)
|
|
|
if !ErrorContains(err, v.expectError) {
|
|
|
t.Errorf(defaultErrorMessage, k, err.Error(), v.expectError)
|
|
|
}
|
|
|
- if v.expectError == "" && string(out[v.projectAPIOutput.Key]) != v.expectedSecret {
|
|
|
- t.Errorf("[%d] unexpected secret: [%s], expected [%s]", k, string(out[v.projectAPIOutput.Key]), v.expectedSecret)
|
|
|
+ if err == nil && !reflect.DeepEqual(out, v.expectedData) {
|
|
|
+ t.Errorf("[%d] unexpected secret data: [%#v], expected [%#v]", k, out, v.expectedData)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -533,7 +673,7 @@ func TestGetAllSecretsWithGroups(t *testing.T) {
|
|
|
smtc.groupIDs = []string{groupid}
|
|
|
smtc.projectAPIOutput.Value = projectvalue
|
|
|
smtc.groupAPIOutput.Value = groupvalue
|
|
|
- smtc.expectedData = map[string][]byte{"testKey": []byte(projectvalue), "groupKey": []byte(groupvalue)}
|
|
|
+ smtc.expectedData = map[string][]byte{testKey: []byte(projectvalue), "groupKey": []byte(groupvalue)}
|
|
|
smtc.refFind.Name = makeFindName(".*Key")
|
|
|
}
|
|
|
groupAndOverrideProjectSecrets := func(smtc *secretManagerTestCase) {
|
|
|
@@ -541,16 +681,16 @@ func TestGetAllSecretsWithGroups(t *testing.T) {
|
|
|
smtc.projectAPIOutput.Value = projectvalue
|
|
|
smtc.groupAPIOutput.Key = smtc.projectAPIOutput.Key
|
|
|
smtc.groupAPIOutput.Value = groupvalue
|
|
|
- smtc.expectedData = map[string][]byte{"testKey": []byte(projectvalue)}
|
|
|
+ smtc.expectedData = map[string][]byte{testKey: []byte(projectvalue)}
|
|
|
smtc.refFind.Name = makeFindName(".*Key")
|
|
|
}
|
|
|
groupAndProjectWithDifferentEnvSecrets := func(smtc *secretManagerTestCase) {
|
|
|
smtc.groupIDs = []string{groupid}
|
|
|
smtc.projectAPIOutput.Value = projectvalue
|
|
|
- smtc.projectAPIOutput.EnvironmentScope = "test"
|
|
|
+ smtc.projectAPIOutput.EnvironmentScope = environmentTest
|
|
|
smtc.groupAPIOutput.Key = smtc.projectAPIOutput.Key
|
|
|
smtc.groupAPIOutput.Value = groupvalue
|
|
|
- smtc.expectedData = map[string][]byte{"testKey": []byte(groupvalue)}
|
|
|
+ smtc.expectedData = map[string][]byte{testKey: []byte(groupvalue)}
|
|
|
smtc.refFind.Name = makeFindName(".*Key")
|
|
|
}
|
|
|
|
|
|
@@ -562,7 +702,7 @@ func TestGetAllSecretsWithGroups(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
sm := Gitlab{}
|
|
|
- sm.environment = "prod"
|
|
|
+ sm.environment = environment
|
|
|
for k, v := range cases {
|
|
|
sm.projectVariablesClient = v.mockProjectVarClient
|
|
|
sm.groupVariablesClient = v.mockGroupVarClient
|