|
|
@@ -41,20 +41,32 @@ const (
|
|
|
username = "user-name"
|
|
|
userkey = "user-key"
|
|
|
environment = "prod"
|
|
|
+ projectvalue = "projectvalue"
|
|
|
+ groupvalue = "groupvalue"
|
|
|
+ groupid = "groupId"
|
|
|
defaultErrorMessage = "[%d] unexpected error: %s, expected: '%s'"
|
|
|
errMissingCredentials = "credentials are empty"
|
|
|
+ findTestPrefix = "test.*"
|
|
|
)
|
|
|
|
|
|
type secretManagerTestCase struct {
|
|
|
- mockClient *fakegitlab.GitlabMockClient
|
|
|
+ mockProjectsClient *fakegitlab.GitlabMockProjectsClient
|
|
|
+ mockProjectVarClient *fakegitlab.GitlabMockProjectVariablesClient
|
|
|
+ mockGroupVarClient *fakegitlab.GitlabMockGroupVariablesClient
|
|
|
apiInputProjectID string
|
|
|
apiInputKey string
|
|
|
apiInputEnv string
|
|
|
- apiOutput *gitlab.ProjectVariable
|
|
|
- apiResponse *gitlab.Response
|
|
|
+ projectAPIOutput *gitlab.ProjectVariable
|
|
|
+ projectAPIResponse *gitlab.Response
|
|
|
+ projectGroupsAPIOutput []*gitlab.ProjectGroup
|
|
|
+ projectGroupsAPIResponse *gitlab.Response
|
|
|
+ groupAPIOutput *gitlab.GroupVariable
|
|
|
+ groupAPIResponse *gitlab.Response
|
|
|
ref *esv1beta1.ExternalSecretDataRemoteRef
|
|
|
refFind *esv1beta1.ExternalSecretFind
|
|
|
- projectID *string
|
|
|
+ projectID string
|
|
|
+ groupIDs []string
|
|
|
+ inheritFromGroups bool
|
|
|
apiErr error
|
|
|
expectError string
|
|
|
expectedSecret string
|
|
|
@@ -65,22 +77,30 @@ type secretManagerTestCase struct {
|
|
|
|
|
|
func makeValidSecretManagerTestCase() *secretManagerTestCase {
|
|
|
smtc := secretManagerTestCase{
|
|
|
- mockClient: &fakegitlab.GitlabMockClient{},
|
|
|
+ mockProjectsClient: &fakegitlab.GitlabMockProjectsClient{},
|
|
|
+ mockProjectVarClient: &fakegitlab.GitlabMockProjectVariablesClient{},
|
|
|
+ mockGroupVarClient: &fakegitlab.GitlabMockGroupVariablesClient{},
|
|
|
apiInputProjectID: makeValidAPIInputProjectID(),
|
|
|
apiInputKey: makeValidAPIInputKey(),
|
|
|
apiInputEnv: makeValidEnvironment(),
|
|
|
ref: makeValidRef(),
|
|
|
refFind: makeValidFindRef(),
|
|
|
- projectID: nil,
|
|
|
- apiOutput: makeValidAPIOutput(),
|
|
|
- apiResponse: makeValidAPIResponse(),
|
|
|
+ projectID: makeValidProjectID(),
|
|
|
+ groupIDs: makeEmptyGroupIds(),
|
|
|
+ projectAPIOutput: makeValidProjectAPIOutput(),
|
|
|
+ projectAPIResponse: makeValidProjectAPIResponse(),
|
|
|
+ projectGroupsAPIOutput: makeValidProjectGroupsAPIOutput(),
|
|
|
+ projectGroupsAPIResponse: makeValidProjectGroupsAPIResponse(),
|
|
|
+ groupAPIOutput: makeValidGroupAPIOutput(),
|
|
|
+ groupAPIResponse: makeValidGroupAPIResponse(),
|
|
|
apiErr: nil,
|
|
|
expectError: "",
|
|
|
expectedSecret: "",
|
|
|
expectedValidationResult: esv1beta1.ValidationResultReady,
|
|
|
expectedData: map[string][]byte{},
|
|
|
}
|
|
|
- smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputEnv, smtc.apiInputKey, smtc.apiOutput, smtc.apiResponse, smtc.apiErr)
|
|
|
+ smtc.mockProjectVarClient.WithValue(smtc.apiInputEnv, smtc.apiInputKey, smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
|
|
|
+ smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
|
|
|
return &smtc
|
|
|
}
|
|
|
|
|
|
@@ -95,6 +115,14 @@ func makeValidFindRef() *esv1beta1.ExternalSecretFind {
|
|
|
return &esv1beta1.ExternalSecretFind{}
|
|
|
}
|
|
|
|
|
|
+func makeValidProjectID() string {
|
|
|
+ return "projectId"
|
|
|
+}
|
|
|
+
|
|
|
+func makeEmptyGroupIds() []string {
|
|
|
+ return []string{}
|
|
|
+}
|
|
|
+
|
|
|
func makeFindName(regexp string) *esv1beta1.FindName {
|
|
|
return &esv1beta1.FindName{
|
|
|
RegExp: regexp,
|
|
|
@@ -110,10 +138,26 @@ func makeValidAPIInputKey() string {
|
|
|
}
|
|
|
|
|
|
func makeValidEnvironment() string {
|
|
|
- return "prod"
|
|
|
+ return environment
|
|
|
+}
|
|
|
+
|
|
|
+func makeValidProjectAPIResponse() *gitlab.Response {
|
|
|
+ return &gitlab.Response{
|
|
|
+ Response: &http.Response{
|
|
|
+ StatusCode: http.StatusOK,
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func makeValidProjectGroupsAPIResponse() *gitlab.Response {
|
|
|
+ return &gitlab.Response{
|
|
|
+ Response: &http.Response{
|
|
|
+ StatusCode: http.StatusOK,
|
|
|
+ },
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func makeValidAPIResponse() *gitlab.Response {
|
|
|
+func makeValidGroupAPIResponse() *gitlab.Response {
|
|
|
return &gitlab.Response{
|
|
|
Response: &http.Response{
|
|
|
StatusCode: http.StatusOK,
|
|
|
@@ -121,10 +165,35 @@ func makeValidAPIResponse() *gitlab.Response {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func makeValidAPIOutput() *gitlab.ProjectVariable {
|
|
|
+func makeValidProjectAPIOutput() *gitlab.ProjectVariable {
|
|
|
return &gitlab.ProjectVariable{
|
|
|
- Key: "testKey",
|
|
|
- Value: "",
|
|
|
+ Key: "testKey",
|
|
|
+ Value: "",
|
|
|
+ EnvironmentScope: environment,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func makeValidProjectGroupsAPIOutput() []*gitlab.ProjectGroup {
|
|
|
+ return []*gitlab.ProjectGroup{{
|
|
|
+ ID: 1,
|
|
|
+ Name: "Group (1)",
|
|
|
+ FullPath: "foo",
|
|
|
+ }, {
|
|
|
+ ID: 100,
|
|
|
+ Name: "Group (100)",
|
|
|
+ FullPath: "foo/bar/baz",
|
|
|
+ }, {
|
|
|
+ ID: 10,
|
|
|
+ Name: "Group (10)",
|
|
|
+ FullPath: "foo/bar",
|
|
|
+ }}
|
|
|
+}
|
|
|
+
|
|
|
+func makeValidGroupAPIOutput() *gitlab.GroupVariable {
|
|
|
+ return &gitlab.GroupVariable{
|
|
|
+ Key: "groupKey",
|
|
|
+ Value: "",
|
|
|
+ EnvironmentScope: environment,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -133,7 +202,9 @@ func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTest
|
|
|
for _, fn := range tweaks {
|
|
|
fn(smtc)
|
|
|
}
|
|
|
- smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputEnv, smtc.apiInputKey, smtc.apiOutput, smtc.apiResponse, smtc.apiErr)
|
|
|
+ smtc.mockProjectsClient.WithValue(smtc.projectGroupsAPIOutput, smtc.projectGroupsAPIResponse, smtc.apiErr)
|
|
|
+ smtc.mockProjectVarClient.WithValue(smtc.apiInputEnv, smtc.apiInputKey, smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
|
|
|
+ smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
|
|
|
return smtc
|
|
|
}
|
|
|
|
|
|
@@ -144,7 +215,9 @@ func makeValidSecretManagerGetAllTestCaseCustom(tweaks ...func(smtc *secretManag
|
|
|
for _, fn := range tweaks {
|
|
|
fn(smtc)
|
|
|
}
|
|
|
- smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputEnv, smtc.apiInputKey, smtc.apiOutput, smtc.apiResponse, smtc.apiErr)
|
|
|
+ smtc.mockProjectVarClient.WithValue(smtc.apiInputEnv, smtc.apiInputKey, smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
|
|
|
+ smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
|
|
|
+
|
|
|
return smtc
|
|
|
}
|
|
|
|
|
|
@@ -153,6 +226,7 @@ func makeValidSecretManagerGetAllTestCaseCustom(tweaks ...func(smtc *secretManag
|
|
|
var setAPIErr = func(smtc *secretManagerTestCase) {
|
|
|
smtc.apiErr = fmt.Errorf("oh no")
|
|
|
smtc.expectError = "oh no"
|
|
|
+ smtc.projectAPIResponse.Response.StatusCode = http.StatusInternalServerError
|
|
|
smtc.expectedValidationResult = esv1beta1.ValidationResultError
|
|
|
}
|
|
|
|
|
|
@@ -163,20 +237,44 @@ var setListAPIErr = func(smtc *secretManagerTestCase) {
|
|
|
smtc.expectedValidationResult = esv1beta1.ValidationResultError
|
|
|
}
|
|
|
|
|
|
-var setListAPIRespNil = func(smtc *secretManagerTestCase) {
|
|
|
- smtc.apiResponse = nil
|
|
|
- smtc.expectError = errAuth
|
|
|
+var setProjectListAPIRespNil = func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.projectAPIResponse = nil
|
|
|
+ smtc.expectError = fmt.Errorf(errProjectAuth, smtc.projectID).Error()
|
|
|
+ smtc.expectedValidationResult = esv1beta1.ValidationResultError
|
|
|
+}
|
|
|
+
|
|
|
+var setGroupListAPIRespNil = func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.groupIDs = []string{groupid}
|
|
|
+ smtc.groupAPIResponse = nil
|
|
|
+ smtc.expectError = fmt.Errorf(errGroupAuth, groupid).Error()
|
|
|
+ smtc.expectedValidationResult = esv1beta1.ValidationResultError
|
|
|
+}
|
|
|
+
|
|
|
+var setProjectAndGroup = func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.groupIDs = []string{groupid}
|
|
|
+}
|
|
|
+
|
|
|
+var setProjectAndInheritFromGroups = func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.groupIDs = nil
|
|
|
+ smtc.inheritFromGroups = true
|
|
|
+}
|
|
|
+
|
|
|
+var setProjectListAPIRespBadCode = func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.projectAPIResponse.StatusCode = http.StatusUnauthorized
|
|
|
+ smtc.expectError = fmt.Errorf(errProjectAuth, smtc.projectID).Error()
|
|
|
smtc.expectedValidationResult = esv1beta1.ValidationResultError
|
|
|
}
|
|
|
|
|
|
-var setListAPIRespBadCode = func(smtc *secretManagerTestCase) {
|
|
|
- smtc.apiResponse.StatusCode = http.StatusUnauthorized
|
|
|
- smtc.expectError = errAuth
|
|
|
+var setGroupListAPIRespBadCode = func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.groupIDs = []string{groupid}
|
|
|
+ smtc.groupAPIResponse.StatusCode = http.StatusUnauthorized
|
|
|
+ smtc.expectError = fmt.Errorf(errGroupAuth, groupid).Error()
|
|
|
smtc.expectedValidationResult = esv1beta1.ValidationResultError
|
|
|
}
|
|
|
|
|
|
var setNilMockClient = func(smtc *secretManagerTestCase) {
|
|
|
- smtc.mockClient = nil
|
|
|
+ smtc.mockProjectVarClient = nil
|
|
|
+ smtc.mockGroupVarClient = nil
|
|
|
smtc.expectError = errUninitializedGitlabProvider
|
|
|
}
|
|
|
|
|
|
@@ -265,27 +363,42 @@ func newFakeAuthorizedKey() *iamkey.Key {
|
|
|
// test the sm<->gcp interface
|
|
|
// make sure correct values are passed and errors are handled accordingly.
|
|
|
func TestGetSecret(t *testing.T) {
|
|
|
- secretValue := "changedvalue"
|
|
|
// good case: default version is set
|
|
|
// key is passed in, output is sent back
|
|
|
-
|
|
|
- setSecretString := func(smtc *secretManagerTestCase) {
|
|
|
- smtc.apiOutput = &gitlab.ProjectVariable{
|
|
|
- Key: "testkey",
|
|
|
- Value: "changedvalue",
|
|
|
- }
|
|
|
- smtc.expectedSecret = secretValue
|
|
|
+ onlyProjectSecret := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.projectAPIOutput.Value = projectvalue
|
|
|
+ smtc.groupAPIResponse = nil
|
|
|
+ smtc.groupAPIOutput = nil
|
|
|
+ smtc.expectedSecret = smtc.projectAPIOutput.Value
|
|
|
+ }
|
|
|
+ groupSecretProjectOverride := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.projectAPIOutput.Value = projectvalue
|
|
|
+ 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.Value = groupvalue
|
|
|
+ smtc.expectedSecret = smtc.groupAPIOutput.Value
|
|
|
}
|
|
|
|
|
|
successCases := []*secretManagerTestCase{
|
|
|
- makeValidSecretManagerTestCaseCustom(setSecretString),
|
|
|
+ makeValidSecretManagerTestCaseCustom(onlyProjectSecret),
|
|
|
+ makeValidSecretManagerTestCaseCustom(groupSecretProjectOverride),
|
|
|
+ makeValidSecretManagerTestCaseCustom(groupWithoutProjectOverride),
|
|
|
makeValidSecretManagerTestCaseCustom(setAPIErr),
|
|
|
makeValidSecretManagerTestCaseCustom(setNilMockClient),
|
|
|
}
|
|
|
|
|
|
sm := Gitlab{}
|
|
|
for k, v := range successCases {
|
|
|
- sm.client = v.mockClient
|
|
|
+ sm.projectVariablesClient = v.mockProjectVarClient
|
|
|
+ sm.groupVariablesClient = v.mockGroupVarClient
|
|
|
+ sm.projectID = v.projectID
|
|
|
+ sm.groupIDs = v.groupIDs
|
|
|
out, err := sm.GetSecret(context.Background(), *v.ref)
|
|
|
if !ErrorContains(err, v.expectError) {
|
|
|
t.Errorf(defaultErrorMessage, k, err.Error(), v.expectError)
|
|
|
@@ -296,8 +409,22 @@ func TestGetSecret(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestResolveGroupIds(t *testing.T) {
|
|
|
+ v := makeValidSecretManagerTestCaseCustom()
|
|
|
+ sm := Gitlab{}
|
|
|
+ sm.projectsClient = v.mockProjectsClient
|
|
|
+ sm.projectID = v.projectID
|
|
|
+ sm.inheritFromGroups = true
|
|
|
+ err := sm.ResolveGroupIds()
|
|
|
+ if err != nil {
|
|
|
+ t.Errorf(defaultErrorMessage, 0, err.Error(), "")
|
|
|
+ }
|
|
|
+ if !reflect.DeepEqual(sm.groupIDs, []string{"1", "10", "100"}) {
|
|
|
+ t.Errorf("Expected groupIds %s, got %s", []string{"1", "10", "100"}, sm.groupIDs)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func TestGetAllSecrets(t *testing.T) {
|
|
|
- secretValue := "changedvalue"
|
|
|
// good case: default version is set
|
|
|
// key is passed in, output is sent back
|
|
|
|
|
|
@@ -315,16 +442,16 @@ func TestGetAllSecrets(t *testing.T) {
|
|
|
smtc.expectError = "'find.path' is not implemented in the Gitlab provider"
|
|
|
}
|
|
|
setMatchingSecretFindString := func(smtc *secretManagerTestCase) {
|
|
|
- smtc.apiOutput = &gitlab.ProjectVariable{
|
|
|
+ smtc.projectAPIOutput = &gitlab.ProjectVariable{
|
|
|
Key: "testkey",
|
|
|
Value: "changedvalue",
|
|
|
EnvironmentScope: "test",
|
|
|
}
|
|
|
- smtc.expectedSecret = secretValue
|
|
|
- smtc.refFind.Name = makeFindName("test.*")
|
|
|
+ smtc.expectedSecret = "changedvalue"
|
|
|
+ smtc.refFind.Name = makeFindName(findTestPrefix)
|
|
|
}
|
|
|
setNoMatchingRegexpFindString := func(smtc *secretManagerTestCase) {
|
|
|
- smtc.apiOutput = &gitlab.ProjectVariable{
|
|
|
+ smtc.projectAPIOutput = &gitlab.ProjectVariable{
|
|
|
Key: "testkey",
|
|
|
Value: "changedvalue",
|
|
|
EnvironmentScope: "test",
|
|
|
@@ -333,13 +460,13 @@ func TestGetAllSecrets(t *testing.T) {
|
|
|
smtc.refFind.Name = makeFindName("foo.*")
|
|
|
}
|
|
|
setUnmatchedEnvironmentFindString := func(smtc *secretManagerTestCase) {
|
|
|
- smtc.apiOutput = &gitlab.ProjectVariable{
|
|
|
+ smtc.projectAPIOutput = &gitlab.ProjectVariable{
|
|
|
Key: "testkey",
|
|
|
Value: "changedvalue",
|
|
|
EnvironmentScope: "prod",
|
|
|
}
|
|
|
smtc.expectedSecret = ""
|
|
|
- smtc.refFind.Name = makeFindName("test.*")
|
|
|
+ smtc.refFind.Name = makeFindName(findTestPrefix)
|
|
|
}
|
|
|
|
|
|
cases := []*secretManagerTestCase{
|
|
|
@@ -356,13 +483,77 @@ func TestGetAllSecrets(t *testing.T) {
|
|
|
sm := Gitlab{}
|
|
|
sm.environment = "test"
|
|
|
for k, v := range cases {
|
|
|
- sm.client = v.mockClient
|
|
|
+ sm.projectVariablesClient = v.mockProjectVarClient
|
|
|
+ sm.groupVariablesClient = v.mockGroupVarClient
|
|
|
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.apiOutput.Key]) != v.expectedSecret {
|
|
|
- t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out[v.apiOutput.Key]))
|
|
|
+ if v.expectError == "" && string(out[v.projectAPIOutput.Key]) != v.expectedSecret {
|
|
|
+ t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out[v.projectAPIOutput.Key]))
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestGetAllSecretsWithGroups(t *testing.T) {
|
|
|
+ onlyProjectSecret := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.projectAPIOutput.Value = projectvalue
|
|
|
+ smtc.refFind.Name = makeFindName(findTestPrefix)
|
|
|
+ smtc.groupAPIResponse = nil
|
|
|
+ smtc.groupAPIOutput = nil
|
|
|
+ smtc.expectedSecret = smtc.projectAPIOutput.Value
|
|
|
+ }
|
|
|
+ groupAndProjectSecrets := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.groupIDs = []string{groupid}
|
|
|
+ smtc.projectAPIOutput.Value = projectvalue
|
|
|
+ smtc.groupAPIOutput.Value = groupvalue
|
|
|
+ smtc.expectedData = map[string][]byte{"testKey": []byte(projectvalue), "groupKey": []byte(groupvalue)}
|
|
|
+ smtc.refFind.Name = makeFindName(".*Key")
|
|
|
+ }
|
|
|
+ groupAndOverrideProjectSecrets := func(smtc *secretManagerTestCase) {
|
|
|
+ smtc.groupIDs = []string{groupid}
|
|
|
+ smtc.projectAPIOutput.Value = projectvalue
|
|
|
+ smtc.groupAPIOutput.Key = smtc.projectAPIOutput.Key
|
|
|
+ smtc.groupAPIOutput.Value = groupvalue
|
|
|
+ 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.groupAPIOutput.Key = smtc.projectAPIOutput.Key
|
|
|
+ smtc.groupAPIOutput.Value = groupvalue
|
|
|
+ smtc.expectedData = map[string][]byte{"testKey": []byte(groupvalue)}
|
|
|
+ smtc.refFind.Name = makeFindName(".*Key")
|
|
|
+ }
|
|
|
+
|
|
|
+ cases := []*secretManagerTestCase{
|
|
|
+ makeValidSecretManagerGetAllTestCaseCustom(onlyProjectSecret),
|
|
|
+ makeValidSecretManagerGetAllTestCaseCustom(groupAndProjectSecrets),
|
|
|
+ makeValidSecretManagerGetAllTestCaseCustom(groupAndOverrideProjectSecrets),
|
|
|
+ makeValidSecretManagerGetAllTestCaseCustom(groupAndProjectWithDifferentEnvSecrets),
|
|
|
+ }
|
|
|
+
|
|
|
+ sm := Gitlab{}
|
|
|
+ sm.environment = "prod"
|
|
|
+ for k, v := range cases {
|
|
|
+ sm.projectVariablesClient = v.mockProjectVarClient
|
|
|
+ sm.groupVariablesClient = v.mockGroupVarClient
|
|
|
+ sm.projectID = v.projectID
|
|
|
+ sm.groupIDs = v.groupIDs
|
|
|
+ out, err := sm.GetAllSecrets(context.Background(), *v.refFind)
|
|
|
+ if !ErrorContains(err, v.expectError) {
|
|
|
+ t.Errorf(defaultErrorMessage, k, err.Error(), v.expectError)
|
|
|
+ }
|
|
|
+ if v.expectError == "" {
|
|
|
+ if len(v.expectedData) > 0 {
|
|
|
+ if !reflect.DeepEqual(v.expectedData, out) {
|
|
|
+ t.Errorf("[%d] Unexpected secrets. Expected [%s], got [%s]", k, v.expectedData, out)
|
|
|
+ }
|
|
|
+ } else if string(out[v.projectAPIOutput.Key]) != v.expectedSecret {
|
|
|
+ t.Errorf("[%d] Unexpected secret. Expected [%s], got [%s]", k, v.expectedSecret, string(out[v.projectAPIOutput.Key]))
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -370,13 +561,22 @@ func TestGetAllSecrets(t *testing.T) {
|
|
|
func TestValidate(t *testing.T) {
|
|
|
successCases := []*secretManagerTestCase{
|
|
|
makeValidSecretManagerTestCaseCustom(),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setProjectAndInheritFromGroups),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setProjectAndGroup),
|
|
|
makeValidSecretManagerTestCaseCustom(setListAPIErr),
|
|
|
- makeValidSecretManagerTestCaseCustom(setListAPIRespNil),
|
|
|
- makeValidSecretManagerTestCaseCustom(setListAPIRespBadCode),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setProjectListAPIRespNil),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setProjectListAPIRespBadCode),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setGroupListAPIRespNil),
|
|
|
+ makeValidSecretManagerTestCaseCustom(setGroupListAPIRespBadCode),
|
|
|
}
|
|
|
sm := Gitlab{}
|
|
|
for k, v := range successCases {
|
|
|
- sm.client = v.mockClient
|
|
|
+ sm.projectsClient = v.mockProjectsClient
|
|
|
+ sm.projectVariablesClient = v.mockProjectVarClient
|
|
|
+ sm.groupVariablesClient = v.mockGroupVarClient
|
|
|
+ sm.projectID = v.projectID
|
|
|
+ sm.groupIDs = v.groupIDs
|
|
|
+ sm.inheritFromGroups = v.inheritFromGroups
|
|
|
t.Logf("%+v", v)
|
|
|
validationResult, err := sm.Validate()
|
|
|
if !ErrorContains(err, v.expectError) {
|
|
|
@@ -385,19 +585,22 @@ func TestValidate(t *testing.T) {
|
|
|
if validationResult != v.expectedValidationResult {
|
|
|
t.Errorf("[%d], unexpected validationResult: %s, expected: '%s'", k, validationResult, v.expectedValidationResult)
|
|
|
}
|
|
|
+ if sm.inheritFromGroups && sm.groupIDs[0] != "1" {
|
|
|
+ t.Errorf("[%d], Expected groupID '1'", k)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestGetSecretMap(t *testing.T) {
|
|
|
// good case: default version & deserialization
|
|
|
setDeserialization := func(smtc *secretManagerTestCase) {
|
|
|
- smtc.apiOutput.Value = `{"foo":"bar"}`
|
|
|
+ smtc.projectAPIOutput.Value = `{"foo":"bar"}`
|
|
|
smtc.expectedData["foo"] = []byte("bar")
|
|
|
}
|
|
|
|
|
|
// bad case: invalid json
|
|
|
setInvalidJSON := func(smtc *secretManagerTestCase) {
|
|
|
- smtc.apiOutput.Value = `-----------------`
|
|
|
+ smtc.projectAPIOutput.Value = `-----------------`
|
|
|
smtc.expectError = "unable to unmarshal secret"
|
|
|
}
|
|
|
|
|
|
@@ -410,7 +613,8 @@ func TestGetSecretMap(t *testing.T) {
|
|
|
|
|
|
sm := Gitlab{}
|
|
|
for k, v := range successCases {
|
|
|
- sm.client = v.mockClient
|
|
|
+ sm.projectVariablesClient = v.mockProjectVarClient
|
|
|
+ sm.groupVariablesClient = v.mockGroupVarClient
|
|
|
out, err := sm.GetSecretMap(context.Background(), *v.ref)
|
|
|
if !ErrorContains(err, v.expectError) {
|
|
|
t.Errorf(defaultErrorMessage, k, err.Error(), v.expectError)
|
|
|
@@ -462,6 +666,14 @@ func withAccessToken(name, key string, namespace *string) storeModifier {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func withGroups(ids []string, inherit bool) storeModifier {
|
|
|
+ return func(store *esv1beta1.SecretStore) *esv1beta1.SecretStore {
|
|
|
+ store.Spec.Provider.Gitlab.GroupIDs = ids
|
|
|
+ store.Spec.Provider.Gitlab.InheritFromGroups = inherit
|
|
|
+ return store
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
type ValidateStoreTestCase struct {
|
|
|
store *esv1beta1.SecretStore
|
|
|
err error
|
|
|
@@ -472,7 +684,11 @@ func TestValidateStore(t *testing.T) {
|
|
|
testCases := []ValidateStoreTestCase{
|
|
|
{
|
|
|
store: makeSecretStore("", environment),
|
|
|
- err: fmt.Errorf("projectID cannot be empty"),
|
|
|
+ err: fmt.Errorf("projectID and groupIDs must not both be empty"),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ store: makeSecretStore(project, environment, withGroups([]string{"group1"}, true)),
|
|
|
+ err: fmt.Errorf("defining groupIDs and inheritFromGroups = true is not allowed"),
|
|
|
},
|
|
|
{
|
|
|
store: makeSecretStore(project, environment, withAccessToken("", userkey, nil)),
|
|
|
@@ -490,6 +706,10 @@ func TestValidateStore(t *testing.T) {
|
|
|
store: makeSecretStore(project, environment, withAccessToken("userName", "userKey", nil)),
|
|
|
err: nil,
|
|
|
},
|
|
|
+ {
|
|
|
+ store: makeSecretStore("", environment, withGroups([]string{"group1"}, false), withAccessToken("userName", "userKey", nil)),
|
|
|
+ err: nil,
|
|
|
+ },
|
|
|
}
|
|
|
p := Gitlab{}
|
|
|
for _, tc := range testCases {
|