Skip to content

Commit ff38a3f

Browse files
authored
Merge pull request #291 from fluxcd/var-validation
Validate the var names before substitution
2 parents f2c986a + cb72a77 commit ff38a3f

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

controllers/kustomization_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ var _ = Describe("KustomizationReconciler", func() {
200200
Validation: "client",
201201
Force: false,
202202
PostBuild: &kustomizev1.PostBuild{
203-
Substitute: map[string]string{"region": "eu-central-1"},
203+
Substitute: map[string]string{"_Region": "eu-central-1"},
204204
SubstituteFrom: []kustomizev1.SubstituteReference{
205205
{
206206
Kind: "ConfigMap",
@@ -274,7 +274,7 @@ metadata:
274274
namespace: test
275275
labels:
276276
environment: ${env:=dev}
277-
region: "${region}"
277+
region: "${_Region}"
278278
zone: "${zone}"
279279
`,
280280
},

controllers/kustomization_varsub.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controllers
33
import (
44
"context"
55
"fmt"
6+
"regexp"
67
"strings"
78

89
"github.com/drone/envsubst"
@@ -15,6 +16,10 @@ import (
1516
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
1617
)
1718

19+
// varsubRegex is the regular expression used to validate
20+
// the var names before substitution
21+
const varsubRegex = "^[_[:alpha:]][_[:alpha:][:digit:]]*$"
22+
1823
// substituteVariables replaces the vars with their values in the specified resource.
1924
// If a resource is labeled or annotated with
2025
// 'kustomize.toolkit.fluxcd.io/substitute: disabled' the substitution is skipped.
@@ -68,6 +73,13 @@ func substituteVariables(
6873

6974
// run bash variable substitutions
7075
if len(vars) > 0 {
76+
r, _ := regexp.Compile(varsubRegex)
77+
for v := range vars {
78+
if !r.MatchString(v) {
79+
return nil, fmt.Errorf("'%s' var name is invalid, must match '%s'", v, varsubRegex)
80+
}
81+
}
82+
7183
output, err := envsubst.Eval(string(resData), func(s string) string {
7284
return vars[s]
7385
})

docs/spec/v1beta1/kustomization.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,10 @@ for [bash string replacement functions](https://github.com/drone/envsubst) e.g.:
716716
- `${var:position:length}`
717717
- `${var/substring/replacement}`
718718

719+
Note that the name of a variable can contain only alphanumeric and underscore characters.
720+
The controller validates the var names using this regular expression:
721+
`^[_[:alpha:]][_[:alpha:][:digit:]]*$`.
722+
719723
Assuming you have manifests with the following variables:
720724

721725
```yaml

0 commit comments

Comments
 (0)