Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/verify dockerfile parser #433

Merged
merged 3 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/cosign/cli/verify_dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,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