Skip to content

Commit 253310a

Browse files
committed
fix unterminated quotes in a long names
1 parent 6cb59e4 commit 253310a

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

examples/app/templates/my-secret-vars.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ metadata:
55
labels:
66
{{- include "app.labels" . | nindent 4 }}
77
data:
8+
ELASTIC_FOOBAR_HUNTER123_MEOWTOWN_VERIFY: {{ required "mySecretVars.elasticFoobarHunter123MeowtownVerify is required" .Values.mySecretVars.elasticFoobarHunter123MeowtownVerify | b64enc
9+
| quote }}
810
VAR1: {{ required "mySecretVars.var1 is required" .Values.mySecretVars.var1 | b64enc
911
| quote }}
1012
VAR2: {{ required "mySecretVars.var2 is required" .Values.mySecretVars.var2 | b64enc

examples/app/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ myConfigProps:
3737
mySecretCa:
3838
caCrt: ""
3939
mySecretVars:
40+
elasticFoobarHunter123MeowtownVerify: ""
4041
str: ""
4142
var1: ""
4243
var2: ""

pkg/format/fix_quotes.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package format
2+
3+
import (
4+
"strings"
5+
)
6+
7+
// FixUnterminatedQuotes check for Unterminated Quotes in helm templated strings
8+
// See https://github.com/arttor/helmify/issues/12
9+
func FixUnterminatedQuotes(in string) string {
10+
sb := strings.Builder{}
11+
hasUntermQuotes := false
12+
lines := strings.Split(in, "\n")
13+
for i, line := range lines {
14+
if hasUntermQuotes {
15+
line = " " + strings.TrimSpace(line)
16+
hasUntermQuotes = false
17+
} else {
18+
hasUntermQuotes = strings.Count(line, "\"")%2 != 0
19+
}
20+
sb.WriteString(line)
21+
if !hasUntermQuotes && i != len(lines)-1 {
22+
sb.WriteString("\n")
23+
}
24+
}
25+
return sb.String()
26+
}

pkg/format/fix_quotes_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package format
2+
3+
import "testing"
4+
5+
func TestFixUnterminatedQuotes(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
in string
9+
want string
10+
}{
11+
{
12+
name: "remove line break for unterminated quotes",
13+
in: `apiVersion: v1
14+
kind: Secret
15+
metadata:
16+
name: {{ include "app.fullname" . }}-my-secret-vars
17+
labels:
18+
{{- include "app.labels" . | nindent 4 }}
19+
data:
20+
ELASTIC_FOOBAR_HUNTER123_MEOWTOWN_VERIFY: {{ required "mySecretVars.elasticFoobarHunter123MeowtownVerify
21+
is required" .Values.mySecretVars.elasticFoobarHunter123MeowtownVerify | b64enc
22+
| quote }}
23+
VAR1: {{ required "mySecretVars.var1 is required" .Values.mySecretVars.var1 | b64enc
24+
| quote }}
25+
VAR2: {{ required "mySecretVars.var2 is required" .Values.mySecretVars.var2 | b64enc
26+
| quote }}
27+
stringData:
28+
str: {{ required "mySecretVars.str is required" .Values.mySecretVars.str | quote
29+
}}
30+
type: opaque`,
31+
want: `apiVersion: v1
32+
kind: Secret
33+
metadata:
34+
name: {{ include "app.fullname" . }}-my-secret-vars
35+
labels:
36+
{{- include "app.labels" . | nindent 4 }}
37+
data:
38+
ELASTIC_FOOBAR_HUNTER123_MEOWTOWN_VERIFY: {{ required "mySecretVars.elasticFoobarHunter123MeowtownVerify is required" .Values.mySecretVars.elasticFoobarHunter123MeowtownVerify | b64enc
39+
| quote }}
40+
VAR1: {{ required "mySecretVars.var1 is required" .Values.mySecretVars.var1 | b64enc
41+
| quote }}
42+
VAR2: {{ required "mySecretVars.var2 is required" .Values.mySecretVars.var2 | b64enc
43+
| quote }}
44+
stringData:
45+
str: {{ required "mySecretVars.str is required" .Values.mySecretVars.str | quote
46+
}}
47+
type: opaque`,
48+
},
49+
}
50+
for _, tt := range tests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
if got := FixUnterminatedQuotes(tt.in); got != tt.want {
53+
t.Errorf("FixUnterminatedQuotes() = %v, want %v", got, tt.want)
54+
}
55+
})
56+
}
57+
}

pkg/processor/secret/secret.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package secret
22

33
import (
4+
"github.com/arttor/helmify/pkg/format"
45
"io"
56
"strings"
67
"text/template"
@@ -88,6 +89,7 @@ func (d secret) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
8889
return true, nil, err
8990
}
9091
data = strings.ReplaceAll(data, "'", "")
92+
data = format.FixUnterminatedQuotes(data)
9193
}
9294

9395
templatedData = map[string]string{}
@@ -108,6 +110,7 @@ func (d secret) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
108110
return true, nil, err
109111
}
110112
stringData = strings.ReplaceAll(stringData, "'", "")
113+
stringData = format.FixUnterminatedQuotes(stringData)
111114
}
112115

113116
return true, &result{

test_data/sample-app.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ type: opaque
166166
data:
167167
VAR1: bXlfc2VjcmV0X3Zhcl8x
168168
VAR2: bXlfc2VjcmV0X3Zhcl8y
169+
ELASTIC_FOOBAR_HUNTER123_MEOWTOWN_VERIFY: bXlfc2VjcmV0X3Zhcl8y
169170
stringData:
170171
str: |
171172
some big not so secret string with

0 commit comments

Comments
 (0)