-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat(image): add logic to guess base layer for docker-cis scan #4344
feat(image): add logic to guess base layer for docker-cis scan #4344
Conversation
pkg/fanal/artifact/image/image.go
Outdated
@@ -550,3 +527,31 @@ func (a Artifact) guessBaseLayers(diffIDs []string, configFile *v1.ConfigFile) [ | |||
} | |||
return baseDiffIDs | |||
} | |||
|
|||
func GuessBaseImageIndex(histories []v1.History) int { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move it under fanal/image/
? The existing file image.go
or a new file, history.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that we need to create new file for 1 function.
I moved function to fanal/image/image.go
in b2cce5f
@@ -128,3 +129,31 @@ func LayerIDs(img v1.Image) ([]string, error) { | |||
} | |||
return layerIDs, nil | |||
} | |||
|
|||
func GuessBaseImageIndex(histories []v1.History) int { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment should be moved to this function as the main logic exists here.
trivy/pkg/fanal/artifact/image/image.go
Lines 473 to 501 in 87844ca
// Guess layers in base image (call base layers). | |
// | |
// e.g. In the following example, we should detect layers in debian:8. | |
// | |
// FROM debian:8 | |
// RUN apt-get update | |
// COPY mysecret / | |
// ENTRYPOINT ["entrypoint.sh"] | |
// CMD ["somecmd"] | |
// | |
// debian:8 may be like | |
// | |
// ADD file:5d673d25da3a14ce1f6cf66e4c7fd4f4b85a3759a9d93efb3fd9ff852b5b56e4 in / | |
// CMD ["/bin/sh"] | |
// | |
// In total, it would be like: | |
// | |
// ADD file:5d673d25da3a14ce1f6cf66e4c7fd4f4b85a3759a9d93efb3fd9ff852b5b56e4 in / | |
// CMD ["/bin/sh"] # empty layer (detected) | |
// RUN apt-get update | |
// COPY mysecret / | |
// ENTRYPOINT ["entrypoint.sh"] # empty layer (skipped) | |
// CMD ["somecmd"] # empty layer (skipped) | |
// | |
// This method tries to detect CMD in the second line and assume the first line is a base layer. | |
// 1. Iterate histories from the bottom. | |
// 2. Skip all the empty layers at the bottom. In the above example, "entrypoint.sh" and "somecmd" will be skipped | |
// 3. If it finds CMD, it assumes that it is the end of base layers. | |
// 4. It gets all the layers as base layers above the CMD found in #3. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe keep the first line only in Artifact.guessBaseLayers().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. Made this in d8c4002
Description
Add logic to guess base layer for docker-cis scan.
See more in #3834.
Related issues
Checklist