Skip to content

Commit 350ec44

Browse files
authored
Scan images not dir (#3136)
* Scan a container image with grype not the directory to avoid scanning earthly injected binaries (like earth_debugger) Signed-off-by: Dimitris Karakasilis <[email protected]> * Do the same for trivy Signed-off-by: Dimitris Karakasilis <[email protected]> --------- Signed-off-by: Dimitris Karakasilis <[email protected]>
1 parent 24ca209 commit 350ec44

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

Earthfile

+23-19
Original file line numberDiff line numberDiff line change
@@ -788,21 +788,25 @@ trivy-scan:
788788

789789
# Use base-image so it can read original os-release file
790790
FROM +base-image
791-
792-
ARG ISO_NAME=$(cat /etc/kairos-release | grep 'KAIROS_ARTIFACT' | sed 's/KAIROS_ARTIFACT=\"//' | sed 's/\"//')
793-
791+
RUN mkdir build
792+
WORKDIR /
794793
COPY +trivy/trivy /trivy
795794
COPY +trivy/contrib /contrib
796795
# This repo seems to have no request limit
797796
ENV TRIVY_DB_REPOSITORY=public.ecr.aws/aquasecurity/trivy-db:2
798797

799-
WORKDIR /build
800-
RUN /trivy filesystem --skip-dirs /tmp --timeout 30m --format sarif -o report.sarif --no-progress /
801-
RUN /trivy filesystem --skip-dirs /tmp --timeout 30m --format template --template "@/contrib/html.tpl" -o report.html --no-progress /
802-
RUN /trivy filesystem --skip-dirs /tmp --timeout 30m -f json -o results.json --no-progress /
803-
SAVE ARTIFACT /build/report.sarif report.sarif AS LOCAL build/${ISO_NAME}-trivy.sarif
804-
SAVE ARTIFACT /build/report.html report.html AS LOCAL build/${ISO_NAME}-trivy.html
805-
SAVE ARTIFACT /build/results.json results.json AS LOCAL build/${ISO_NAME}-trivy.json
798+
ARG NAME=$(cat /etc/kairos-release | grep 'KAIROS_ARTIFACT' | sed 's/KAIROS_ARTIFACT=\"//' | sed 's/\"//')
799+
800+
# We could scan the "/" directory but earthly injects binaries like earth_debugger which are not in the image
801+
WITH DOCKER --load image=+base-image
802+
RUN /trivy image --skip-dirs /tmp --timeout 30m --format sarif -o /build/report.sarif --no-progress image && \
803+
/trivy image --skip-dirs /tmp --timeout 30m --format template --template "@/contrib/html.tpl" -o /build/report.html --no-progress image && \
804+
/trivy image --skip-dirs /tmp --timeout 30m -f json -o /build/results.json --no-progress image
805+
END
806+
807+
SAVE ARTIFACT /build/report.sarif report.sarif AS LOCAL build/${NAME}-trivy.sarif
808+
SAVE ARTIFACT /build/report.html report.html AS LOCAL build/${NAME}-trivy.html
809+
SAVE ARTIFACT /build/results.json results.json AS LOCAL build/${NAME}-trivy.json
806810

807811
grype:
808812
ARG GRYPE_VERSION
@@ -814,19 +818,19 @@ grype-scan:
814818

815819
# Use base-image so it can read original os-release file
816820
FROM +base-image
817-
818821
WORKDIR /
819-
822+
RUN mkdir build
820823
COPY +grype/grype grype
824+
ARG NAME=$(cat /etc/kairos-release | grep 'KAIROS_ARTIFACT' | sed 's/KAIROS_ARTIFACT=\"//' | sed 's/\"//')
821825

822-
ARG ISO_NAME=$(cat /etc/kairos-release | grep 'KAIROS_ARTIFACT' | sed 's/KAIROS_ARTIFACT=\"//' | sed 's/\"//')
823-
824-
RUN mkdir build
825-
RUN ./grype dir:. --output sarif --add-cpes-if-none --file /build/report.sarif
826-
RUN ./grype dir:. --output json --add-cpes-if-none --file /build/report.json
827-
SAVE ARTIFACT /build/report.sarif report.sarif AS LOCAL build/${ISO_NAME}-grype.sarif
828-
SAVE ARTIFACT /build/report.json report.json AS LOCAL build/${ISO_NAME}-grype.json
826+
# We could scan the "/" directory but earthly injects binaries like earth_debugger which are not in the image
827+
WITH DOCKER --load image=+base-image
828+
RUN ./grype docker:image --output json --add-cpes-if-none --file /build/report.json && \
829+
./grype docker:image --output sarif --add-cpes-if-none --file /build/report.sarif
830+
END
829831

832+
SAVE ARTIFACT /build/report.sarif report.sarif AS LOCAL build/${NAME}-grype.sarif
833+
SAVE ARTIFACT /build/report.json report.json AS LOCAL build/${NAME}-grype.json
830834

831835
###
832836
### Test targets

0 commit comments

Comments
 (0)