Skip to content

Commit

Permalink
Fix/verify dockerfile parser (#433)
Browse files Browse the repository at this point in the history
* add failing test case for lowercase 'as'

Signed-off-by: Furkan <[email protected]>
Co-authored-by: Batuhan Apaydın <[email protected]>
Co-authored-by: Jake Sanders <[email protected]>
  • Loading branch information
3 people authored Jul 21, 2021
1 parent 981d702 commit 840f9a6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/cosign/cli/verify_dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ func getImagesFromDockerfile(dockerfile io.Reader) ([]string, error) {
func getImageFromLine(line string) string {
line = strings.TrimPrefix(line, "FROM") // Remove "FROM" prefix
line = os.ExpandEnv(line) // Substitute templated vars
line = strings.Split(line, " AS ")[0] // Remove the "AS" portion of line
fields := strings.Fields(line)
for i := len(fields) - 1; i > 0; i-- {
// Remove the "AS" portion of line
if strings.EqualFold(fields[i], "AS") {
fields = fields[:i]
break
}
}
return fields[len(fields)-1] // The image should be the last portion of the line that remains
}
5 changes: 5 additions & 0 deletions cmd/cosign/cli/verify_dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func TestGetImagesFromDockerfile(t *testing.T) {
fileContents: `FROM gcr.io/test/image:latest`,
expected: []string{"gcr.io/test/image:latest"},
},
{
name: "tag with as",
fileContents: `FROM gcr.io/test/image:1.16.5 as build`,
expected: []string{"gcr.io/test/image:1.16.5"},
},
{
name: "digest",
fileContents: `FROM gcr.io/test/image@sha256:d131624e6f5d8695e9aea7a0439f7bac0fcc50051282e0c3d4d627cab8845ba5`,
Expand Down

0 comments on commit 840f9a6

Please sign in to comment.