Browse Source

gitlab: allow fallback to wildcard variable, when no environment specific value is defined (#1772)

Signed-off-by: Dominik Zeiger <dominik@zeiger.biz>

Signed-off-by: Dominik Zeiger <dominik@zeiger.biz>
Dominik Zeiger 3 years ago
parent
commit
8a0f711e96

+ 1 - 6
pkg/provider/gitlab/fake/fake.go

@@ -46,14 +46,9 @@ func (mc *GitlabMockProjectVariablesClient) ListVariables(pid interface{}, opt *
 	return mc.listVariables(pid)
 	return mc.listVariables(pid)
 }
 }
 
 
-func (mc *GitlabMockProjectVariablesClient) WithValue(envInput, keyInput string, output *gitlab.ProjectVariable, response *gitlab.Response, err error) {
+func (mc *GitlabMockProjectVariablesClient) WithValue(output *gitlab.ProjectVariable, response *gitlab.Response, err error) {
 	if mc != nil {
 	if mc != nil {
 		mc.getVariable = func(pid interface{}, key string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error) {
 		mc.getVariable = func(pid interface{}, key string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error) {
-			// type secretmanagerpb.AccessSecretVersionRequest contains unexported fields
-			// use cmpopts.IgnoreUnexported to ignore all the unexported fields in the cmp.
-			// if !cmp.Equal(paramReq, input, cmpopts.IgnoreUnexported(gitlab.ProjectVariable{})) {
-			// 	return nil, nil, fmt.Errorf("unexpected test argument")
-			// }
 			return output, response, err
 			return output, response, err
 		}
 		}
 
 

+ 7 - 2
pkg/provider/gitlab/gitlab.go

@@ -303,7 +303,12 @@ func (g *Gitlab) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretData
 	}
 	}
 
 
 	data, resp, err := g.projectVariablesClient.GetVariable(g.projectID, ref.Key, vopts)
 	data, resp, err := g.projectVariablesClient.GetVariable(g.projectID, ref.Key, vopts)
-	if resp.StatusCode >= 400 && resp.StatusCode != 404 && err != nil {
+	if !isEmptyOrWildcard(g.environment) && resp.StatusCode == http.StatusNotFound {
+		vopts.Filter.EnvironmentScope = "*"
+		data, resp, err = g.projectVariablesClient.GetVariable(g.projectID, ref.Key, vopts)
+	}
+
+	if resp.StatusCode >= 400 && resp.StatusCode != http.StatusNotFound && err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
@@ -324,7 +329,7 @@ func (g *Gitlab) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretData
 		}
 		}
 
 
 		groupVar, resp, err := g.groupVariablesClient.GetVariable(groupID, ref.Key, nil)
 		groupVar, resp, err := g.groupVariablesClient.GetVariable(groupID, ref.Key, nil)
-		if resp.StatusCode >= 400 && resp.StatusCode != 404 && err != nil {
+		if resp.StatusCode >= 400 && resp.StatusCode != http.StatusNotFound && err != nil {
 			return nil, err
 			return nil, err
 		}
 		}
 		if resp.StatusCode < 300 {
 		if resp.StatusCode < 300 {

+ 12 - 3
pkg/provider/gitlab/gitlab_test.go

@@ -99,7 +99,7 @@ func makeValidSecretManagerTestCase() *secretManagerTestCase {
 		expectedValidationResult: esv1beta1.ValidationResultReady,
 		expectedValidationResult: esv1beta1.ValidationResultReady,
 		expectedData:             map[string][]byte{},
 		expectedData:             map[string][]byte{},
 	}
 	}
-	smtc.mockProjectVarClient.WithValue(smtc.apiInputEnv, smtc.apiInputKey, smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
+	smtc.mockProjectVarClient.WithValue(smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
 	smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
 	smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
 	return &smtc
 	return &smtc
 }
 }
@@ -203,7 +203,7 @@ func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTest
 		fn(smtc)
 		fn(smtc)
 	}
 	}
 	smtc.mockProjectsClient.WithValue(smtc.projectGroupsAPIOutput, smtc.projectGroupsAPIResponse, 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.mockProjectVarClient.WithValue(smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
 	smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
 	smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
 	return smtc
 	return smtc
 }
 }
@@ -215,7 +215,7 @@ func makeValidSecretManagerGetAllTestCaseCustom(tweaks ...func(smtc *secretManag
 	for _, fn := range tweaks {
 	for _, fn := range tweaks {
 		fn(smtc)
 		fn(smtc)
 	}
 	}
-	smtc.mockProjectVarClient.WithValue(smtc.apiInputEnv, smtc.apiInputKey, smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
+	smtc.mockProjectVarClient.WithValue(smtc.projectAPIOutput, smtc.projectAPIResponse, smtc.apiErr)
 	smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
 	smtc.mockGroupVarClient.WithValue(smtc.groupAPIOutput, smtc.groupAPIResponse, smtc.apiErr)
 
 
 	return smtc
 	return smtc
@@ -371,6 +371,13 @@ func TestGetSecret(t *testing.T) {
 		smtc.groupAPIOutput = nil
 		smtc.groupAPIOutput = nil
 		smtc.expectedSecret = smtc.projectAPIOutput.Value
 		smtc.expectedSecret = smtc.projectAPIOutput.Value
 	}
 	}
+	onlyWildcardSecret := func(smtc *secretManagerTestCase) {
+		smtc.projectAPIOutput.Value = ""
+		smtc.projectAPIResponse.Response.StatusCode = 404
+		smtc.groupAPIResponse = nil
+		smtc.groupAPIOutput = nil
+		smtc.expectedSecret = smtc.projectAPIOutput.Value
+	}
 	groupSecretProjectOverride := func(smtc *secretManagerTestCase) {
 	groupSecretProjectOverride := func(smtc *secretManagerTestCase) {
 		smtc.projectAPIOutput.Value = projectvalue
 		smtc.projectAPIOutput.Value = projectvalue
 		smtc.groupAPIOutput.Key = "testkey"
 		smtc.groupAPIOutput.Key = "testkey"
@@ -387,6 +394,7 @@ func TestGetSecret(t *testing.T) {
 
 
 	successCases := []*secretManagerTestCase{
 	successCases := []*secretManagerTestCase{
 		makeValidSecretManagerTestCaseCustom(onlyProjectSecret),
 		makeValidSecretManagerTestCaseCustom(onlyProjectSecret),
+		makeValidSecretManagerTestCaseCustom(onlyWildcardSecret),
 		makeValidSecretManagerTestCaseCustom(groupSecretProjectOverride),
 		makeValidSecretManagerTestCaseCustom(groupSecretProjectOverride),
 		makeValidSecretManagerTestCaseCustom(groupWithoutProjectOverride),
 		makeValidSecretManagerTestCaseCustom(groupWithoutProjectOverride),
 		makeValidSecretManagerTestCaseCustom(setAPIErr),
 		makeValidSecretManagerTestCaseCustom(setAPIErr),
@@ -399,6 +407,7 @@ func TestGetSecret(t *testing.T) {
 		sm.groupVariablesClient = v.mockGroupVarClient
 		sm.groupVariablesClient = v.mockGroupVarClient
 		sm.projectID = v.projectID
 		sm.projectID = v.projectID
 		sm.groupIDs = v.groupIDs
 		sm.groupIDs = v.groupIDs
+		sm.environment = v.apiInputEnv
 		out, err := sm.GetSecret(context.Background(), *v.ref)
 		out, err := sm.GetSecret(context.Background(), *v.ref)
 		if !ErrorContains(err, v.expectError) {
 		if !ErrorContains(err, v.expectError) {
 			t.Errorf(defaultErrorMessage, k, err.Error(), v.expectError)
 			t.Errorf(defaultErrorMessage, k, err.Error(), v.expectError)