Skip to content

Commit

Permalink
Let DbUsers use ConfigMap data in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
allanger committed Dec 20, 2023
1 parent d330998 commit 73133b0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
11 changes: 4 additions & 7 deletions api/v1beta1/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,17 @@ import (

var (
helpers []string = []string{"Protocol", "Hostname", "Port", "Password", "Username", "Password", "Database"}
functions []string = []string{"Secret", "ConfigMap", "Query"}
userFunctions []string = []string{"ConfigMap"}
allowedFunctions []string = []string{"Secret", "ConfigMap", "Query"}
)

// Make sure that credentials.templates are correct
// ConfigMaps templating is not allowed for DbUser, that's why we have
//
// the second argument: cmAllowed. It should be set to false when
// validation is called by dbuser_webhook.
// the second argument: cmAllowed. It should be set to false when
// validation is called by dbuser_webhook.
func ValidateTemplates(templates Templates, cmAllowed bool) error {
for _, template := range templates {
allowedFunctions := functions
if !cmAllowed {
allowedFunctions = userFunctions
if !template.Secret {
err := errors.New("ConfigMap templating is not allowed for that kind. Please set .secret to true")
return err
Expand Down Expand Up @@ -74,7 +71,7 @@ func validHelperField(field string) bool {
}

func validFunctionField(field string) bool {
return slices.Contains(functions, field)
return slices.Contains(allowedFunctions, field)
}

func validFunctionArg(field string) bool {
Expand Down
6 changes: 0 additions & 6 deletions api/v1beta1/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,4 @@ func TestUnitTemplatesValidator(t *testing.T) {
assert.NoErrorf(t, err, "expected no error: %v", err)
err = v1beta1.ValidateTemplates(cmTemplates, false)
assert.ErrorContains(t, err, "ConfigMap templating is not allowed for that kind. Please set .secret to true")

cmTemplates = v1beta1.Templates{
{Name: "TEMPLATE_1", Template: "{{ .ConfigMap \"test\" }}", Secret: false},
}
err = v1beta1.ValidateTemplates(cmTemplates, false)
assert.ErrorContains(t, err, "ConfigMap templating is not allowed for that kind. Please set .secret to true")
}

0 comments on commit 73133b0

Please sign in to comment.