Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit 10a81f7

Browse files
authored
fix: unescape dots in env names (#2452)
Signed-off-by: Thorsten Klein <[email protected]>
1 parent 5010053 commit 10a81f7

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

pkg/controller/appdefinition/deploy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ func toEnv(envs []v1.EnvVar, appEnvs []v1.NameValue, interpolator *secrets.Inter
177177
if appEnvNames.Has(env.Name) {
178178
continue
179179
}
180-
if strings.Contains(env.Secret.Name, ".") {
180+
if secrets.HasUnescapedDot(env.Secret.Name) {
181181
interpolated, noDotInKey := interpolator.ToEnv(env.Name, fmt.Sprintf("@{secrets.%s.%s}", env.Secret.Name, env.Secret.Key))
182182
if noDotInKey {
183183
interpolated.Name = strings.ReplaceAll(interpolated.Name, "\\.", ".") // restore dots that were escaped during unmarshalling
184184
result = append(result, interpolated)
185185
}
186186
} else {
187187
result = append(result, corev1.EnvVar{
188-
Name: env.Name,
188+
Name: strings.ReplaceAll(env.Name, "\\.", "."), // restore dots that were escaped during unmarshalling
189189
ValueFrom: &corev1.EnvVarSource{
190190
SecretKeyRef: &corev1.SecretKeySelector{
191191
LocalObjectReference: corev1.LocalObjectReference{

pkg/controller/appdefinition/deploy_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ func TestEnvironment(t *testing.T) {
131131
Name: "foo\\.bar",
132132
Value: "baz",
133133
},
134+
{
135+
Name: "foo\\.bar\\.baz",
136+
Secret: v1.SecretReference{
137+
Name: "somesecret",
138+
Key: "somesecretkey",
139+
},
140+
},
134141
},
135142
},
136143
},
@@ -150,6 +157,17 @@ func TestEnvironment(t *testing.T) {
150157
Name: "foo.bar",
151158
Value: "baz",
152159
},
160+
{
161+
Name: "foo.bar.baz",
162+
ValueFrom: &corev1.EnvVarSource{
163+
SecretKeyRef: &corev1.SecretKeySelector{
164+
Key: "somesecretkey",
165+
LocalObjectReference: corev1.LocalObjectReference{
166+
Name: "somesecret",
167+
},
168+
},
169+
},
170+
},
153171
}, dep.Spec.Template.Spec.Containers[0].Env)
154172
}
155173

pkg/secrets/interpolation.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,8 @@ func (i *Interpolator) getContainerOrJobName() string {
561561
return i.jobName
562562
}
563563

564-
// hasUnescapedDot checks if a string contains an unescaped dot (i.e., a dot not escaped by a backslash).
565-
func hasUnescapedDot(s string) bool {
564+
// HasUnescapedDot checks if a string contains an unescaped dot (i.e., a dot not escaped by a backslash).
565+
func HasUnescapedDot(s string) bool {
566566
for i := 0; i < len(s); i++ {
567567
if s[i] == '.' {
568568
// Match if it's the first character or not escaped by a backslash
@@ -584,7 +584,7 @@ func (i *Interpolator) ToEnv(key, value string) (corev1.EnvVar, bool) {
584584
return corev1.EnvVar{
585585
Name: key,
586586
Value: value,
587-
}, !hasUnescapedDot(key)
587+
}, !HasUnescapedDot(key)
588588
}
589589

590590
newValue, err := i.Replace(value)
@@ -593,13 +593,13 @@ func (i *Interpolator) ToEnv(key, value string) (corev1.EnvVar, bool) {
593593
return corev1.EnvVar{
594594
Name: newKey,
595595
Value: value,
596-
}, !hasUnescapedDot(key)
596+
}, !HasUnescapedDot(key)
597597
}
598598
if value == newValue {
599599
return corev1.EnvVar{
600600
Name: newKey,
601601
Value: value,
602-
}, !hasUnescapedDot(newKey)
602+
}, !HasUnescapedDot(newKey)
603603
}
604604

605605
return corev1.EnvVar{
@@ -612,7 +612,7 @@ func (i *Interpolator) ToEnv(key, value string) (corev1.EnvVar, bool) {
612612
Key: i.addContent(newValue),
613613
},
614614
},
615-
}, !hasUnescapedDot(newKey)
615+
}, !HasUnescapedDot(newKey)
616616
}
617617

618618
func (i *Interpolator) Objects() []kclient.Object {

0 commit comments

Comments
 (0)