Skip to content

Commit 9865819

Browse files
Merge pull request #2565 from vamshi-stepsecurity/fix/exempt-ImageAndTag
exempt by image and tag
2 parents 810897b + 6c4d618 commit 9865819

File tree

7 files changed

+81
-8
lines changed

7 files changed

+81
-8
lines changed

remediation/docker/securedockerfile.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ func SecureDockerFile(inputDockerFile string, opts ...DockerfileConfig) (*Secure
5353
var image string
5454
var tag string
5555
isPinned := false
56+
57+
// Check if image is exempted (skip pinning)
58+
if len(exemptedImages) > 0 && pin.ActionExists(temp, exemptedImages) {
59+
continue
60+
}
61+
5662
if strings.Contains(temp, ":") && !strings.Contains(temp, "sha256") {
5763
// case activates if image like: python:3.7
5864
split := strings.Split(temp, ":")
@@ -76,11 +82,6 @@ func SecureDockerFile(inputDockerFile string, opts ...DockerfileConfig) (*Secure
7682
isPinned = true
7783
}
7884

79-
// Check if image is exempted (skip pinning)
80-
if len(exemptedImages) > 0 && pin.ActionExists(image, exemptedImages) {
81-
continue
82-
}
83-
8485
if !isPinned {
8586
sha, err := getSHA(image, tag)
8687
if err != nil {

remediation/docker/securedockerfile_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ func TestSecureDockerFile(t *testing.T) {
3939

4040
httpmock.RegisterResponder("GET", "https://index.docker.io/v2/library/python/manifests/3.7", httpmock.NewStringResponder(200, resp))
4141

42+
httpmock.RegisterResponder("GET", "https://index.docker.io/v2/library/amazonlinux/manifests/2", httpmock.NewStringResponder(200, resp))
43+
httpmock.RegisterResponder("GET", "https://index.docker.io/v2/library/amazonlinux/manifests/2023", httpmock.NewStringResponder(200, resp))
44+
45+
httpmock.RegisterResponder("GET", "https://public.ecr.aws/v2/",
46+
httpmock.NewStringResponder(200, `{
47+
}`))
48+
httpmock.RegisterResponder("GET", "https://public.ecr.aws/v2/amazonlinux/amazonlinux/manifests/2023", httpmock.NewStringResponder(200, resp))
49+
4250
tests := []struct {
4351
fileName string
4452
isChanged bool
@@ -48,8 +56,10 @@ func TestSecureDockerFile(t *testing.T) {
4856
{fileName: "Dockerfile-not-pinned", isChanged: true, useExemptConfig: false},
4957
{fileName: "Dockerfile-not-pinned-as", isChanged: true, useExemptConfig: false},
5058
{fileName: "Dockerfile-multiple-images", isChanged: true, useExemptConfig: false},
51-
{fileName: "Dockerfile-exempted", isChanged: false, exemptedImages: []string{"python"}, useExemptConfig: true},
52-
{fileName: "Dockerfile-exempted-wildcard", isChanged: true, exemptedImages: []string{"amazon*", "alpine"}, useExemptConfig: true},
59+
{fileName: "Dockerfile-exempted", isChanged: false, exemptedImages: []string{"python:3.7"}, useExemptConfig: true},
60+
{fileName: "Dockerfile-exempted-wildcard", isChanged: true, exemptedImages: []string{"amazon*", "alpine:*"}, useExemptConfig: true},
61+
{fileName: "Dockerfile-imageandtag-exempted", isChanged: true, exemptedImages: []string{"amazonlinux:2"}, useExemptConfig: true},
62+
{fileName: "Dockerfile-imageandtag-exempted-2", isChanged: true, exemptedImages: []string{"public.ecr.aws/amazonlinux/amazonlinux:2023"}, useExemptConfig: true},
5363
}
5464

5565
for _, test := range tests {

remediation/workflow/pin/pinactions_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,28 @@ func TestActionExists(t *testing.T) {
453453
t.Errorf("ActionExists returned true for actions/checkout/something")
454454
}
455455

456+
result = ActionExists("amazonlinux:2023", []string{"amazonlinux:*"})
457+
t.Log(result)
458+
if !result {
459+
t.Errorf("ActionExists returned true for amazonlinux:2023")
460+
}
461+
456462
result = ActionExists("step-security/checkout-release/something", []string{"*/checkout-*"})
457463
t.Log(result)
458464
if !result {
459-
t.Errorf("ActionExists returned true for actions/checkout/something")
465+
t.Errorf("ActionExists returned true for step-security/checkout-release/something")
466+
}
467+
468+
result = ActionExists("amazonlinux:2023", []string{"amazonlinux:2023"})
469+
t.Log(result)
470+
if !result {
471+
t.Errorf("ActionExists returned true for amazonlinux:2023")
472+
}
473+
474+
result = ActionExists("amazonlinux:2023", []string{"amazonlinux*"})
475+
t.Log(result)
476+
if !result {
477+
t.Errorf("ActionExists returned true for amazonlinux:2023")
460478
}
461479

462480
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM --platform=linux/x86_64 amazonlinux:2
2+
3+
FROM --platform=linux/x86_64 amazonlinux:2023 as build_env
4+
5+
FROM python:3.7
6+
7+
RUN apt-get update && apt-get install -y vim
8+
9+
WORKDIR /app
10+
11+
FROM public.ecr.aws/amazonlinux/amazonlinux:2023
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM --platform=linux/x86_64 amazonlinux:2023 as build_env
2+
3+
FROM --platform=linux/x86_64 amazonlinux:2 as base
4+
5+
FROM python:3.7
6+
7+
RUN apt-get update && apt-get install -y vim
8+
9+
WORKDIR /app
10+
11+
FROM public.ecr.aws/amazonlinux/amazonlinux:2023
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM --platform=linux/x86_64 amazonlinux:2
2+
3+
FROM --platform=linux/x86_64 amazonlinux:2023@sha256:5fb6f4b9d73ddeb0e431c938bee25c69157a1e3c880a81ff72c43a8055628de5 as build_env
4+
5+
FROM python:3.7@sha256:5fb6f4b9d73ddeb0e431c938bee25c69157a1e3c880a81ff72c43a8055628de5
6+
7+
RUN apt-get update && apt-get install -y vim
8+
9+
WORKDIR /app
10+
11+
FROM public.ecr.aws/amazonlinux/amazonlinux:2023@sha256:5fb6f4b9d73ddeb0e431c938bee25c69157a1e3c880a81ff72c43a8055628de5
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM --platform=linux/x86_64 amazonlinux:2023@sha256:5fb6f4b9d73ddeb0e431c938bee25c69157a1e3c880a81ff72c43a8055628de5 as build_env
2+
3+
FROM --platform=linux/x86_64 amazonlinux:2@sha256:5fb6f4b9d73ddeb0e431c938bee25c69157a1e3c880a81ff72c43a8055628de5 as base
4+
5+
FROM python:3.7@sha256:5fb6f4b9d73ddeb0e431c938bee25c69157a1e3c880a81ff72c43a8055628de5
6+
7+
RUN apt-get update && apt-get install -y vim
8+
9+
WORKDIR /app
10+
11+
FROM public.ecr.aws/amazonlinux/amazonlinux:2023

0 commit comments

Comments
 (0)