Skip to content

Commit 6508387

Browse files
authored
Ensure image values are parsed from the same line as the image key (#567)
The previous regular expression would search for the expected images declaration and then parse both horizontal and vertical space for the value of that key. This function intended on grabbing a value of a given yaml key, but by traversing vertical space, would grab a completely separate key and value of an object. This replacement regular expression more accurately captures the intended behavior of evaluating a given line of a template and searching for the an image value. This is done by enabling multiline mode and searching strictly horizontal space in the line for the value of the image. Signed-off-by: Jose R. Gonzalez <komish@flutes.dev>
1 parent efe8291 commit 6508387

2 files changed

Lines changed: 36 additions & 23 deletions

File tree

internal/chartverifier/checks/helm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func getImageReferences(chartURI string, vals map[string]interface{}, serverKube
235235
// getImagesFromContent evaluates generated templates from
236236
// helm and extracts images which are returned in a slice
237237
func getImagesFromContent(content string) ([]string, error) {
238-
re, err := regexp.Compile(`\s+image\:\s+(?P<image>.*)\n`)
238+
re, err := regexp.Compile(`(?m)\s+image\:[ \t]+(?P<image>\S.*)\s*$`)
239239
if err != nil {
240240
return nil, fmt.Errorf("error getting images; %v", err)
241241
}

internal/chartverifier/checks/helm_test.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -191,39 +191,52 @@ func TestLongLineTemplate(t *testing.T) {
191191
}
192192

193193
func TestGetImagesFromContent(t *testing.T) {
194-
test := struct {
194+
testCases := []struct {
195195
name string
196196
content string
197197
want []string
198198
}{
199-
name: "find images in yaml",
200-
content: `
199+
{
200+
name: "find images in yaml",
201+
content: `
201202
image: "registry.access.redhat.com/rhscl/postgresql-10-rhel7:1-161"
202203
image: 'busybox'
203204
image: " "
204205
image: registry.redhat.io/cpopen/ibmcloud-object-storage-driver@sha256:fc17bb3e89d00b3eb0f50b3ea83aa75c52e43d8e56cf2e0f17475e934eeeeb5f
205206
`,
206-
want: []string{
207-
"registry.access.redhat.com/rhscl/postgresql-10-rhel7:1-161",
208-
"busybox",
209-
"",
210-
"registry.redhat.io/cpopen/ibmcloud-object-storage-driver@sha256:fc17bb3e89d00b3eb0f50b3ea83aa75c52e43d8e56cf2e0f17475e934eeeeb5f",
207+
want: []string{
208+
"registry.access.redhat.com/rhscl/postgresql-10-rhel7:1-161",
209+
"busybox",
210+
"",
211+
"registry.redhat.io/cpopen/ibmcloud-object-storage-driver@sha256:fc17bb3e89d00b3eb0f50b3ea83aa75c52e43d8e56cf2e0f17475e934eeeeb5f",
212+
},
213+
},
214+
{
215+
name: "do not match against mappings",
216+
content: `
217+
image:
218+
repository: "registry.access.redhat.com/rhscl/postgresql-10-rhel7:1-161"
219+
`,
220+
want: []string{},
211221
},
212222
}
213223

214-
t.Run(test.name, func(t *testing.T) {
215-
got, err := getImagesFromContent(test.content)
216-
require.Nil(t, err)
217-
if testing.Verbose() {
218-
t.Logf("got %d images", len(got))
219-
}
220-
if len(got) != len(test.want) {
221-
t.Errorf("got %d images but, want %d", len(got), len(test.want))
222-
}
223-
for _, image := range got {
224-
if strings.TrimSpace(image) == "" {
225-
t.Logf("Found empty image")
224+
for _, tc := range testCases {
225+
t.Run(tc.name, func(t *testing.T) {
226+
got, err := getImagesFromContent(tc.content)
227+
require.Nil(t, err)
228+
if testing.Verbose() {
229+
t.Logf("got %d images", len(got))
230+
t.Logf("got: %s", got)
226231
}
227-
}
228-
})
232+
if len(got) != len(tc.want) {
233+
t.Errorf("got %d images but, want %d", len(got), len(tc.want))
234+
}
235+
for _, image := range got {
236+
if strings.TrimSpace(image) == "" {
237+
t.Logf("Found empty image")
238+
}
239+
}
240+
})
241+
}
229242
}

0 commit comments

Comments
 (0)