-
Notifications
You must be signed in to change notification settings - Fork 238
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
add image-sha check #540
Open
VedantMahabaleshwarkar
wants to merge
4
commits into
stackrox:main
Choose a base branch
from
VedantMahabaleshwarkar:imageshatag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+370
−0
Open
add image-sha check #540
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,9 @@ | ||||||
name: "image-sha" | ||||||
description: "Indicates if there are any image references that are not references by sha256 tags" | ||||||
remediation: "Reference all images using their sha256 tags." | ||||||
scope: | ||||||
objectKinds: | ||||||
- "Any" | ||||||
template: "image-sha" | ||||||
params: | ||||||
AllowList: [".*:[a-fA-F0-9]{64}$"] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package params | ||
|
||
// Params represents the params accepted by this template. | ||
type Params struct { | ||
|
||
// list of regular expressions specifying pattern(s) for container images that will be blocked. */ | ||
BlockList []string | ||
|
||
// list of regular expressions specifying pattern(s) for container images that will be allowed. | ||
AllowList []string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package imageshatag | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/pkg/errors" | ||
"golang.stackrox.io/kube-linter/pkg/check" | ||
"golang.stackrox.io/kube-linter/pkg/config" | ||
"golang.stackrox.io/kube-linter/pkg/diagnostic" | ||
"golang.stackrox.io/kube-linter/pkg/objectkinds" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/imageshatag/internal/params" | ||
"golang.stackrox.io/kube-linter/pkg/templates/util" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
templateKey = "image-sha" | ||
) | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
HumanName: "Latest Tag", | ||
Key: templateKey, | ||
Description: "Flag applications running container images that do not satisfies \"allowList\" & \"blockList\" parameters criteria.", | ||
SupportedObjectKinds: config.ObjectKindsDesc{ | ||
ObjectKinds: []string{objectkinds.Any}, | ||
}, | ||
Parameters: params.ParamDescs, | ||
ParseAndValidateParams: params.ParseAndValidate, | ||
Instantiate: params.WrapInstantiateFunc(func(p params.Params) (check.Func, error) { | ||
|
||
blockedRegexes := make([]*regexp.Regexp, 0, len(p.BlockList)) | ||
for _, res := range p.BlockList { | ||
rg, err := regexp.Compile(res) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "invalid regex %s", res) | ||
} | ||
blockedRegexes = append(blockedRegexes, rg) | ||
} | ||
|
||
allowedRegexes := make([]*regexp.Regexp, 0, len(p.AllowList)) | ||
for _, res := range p.AllowList { | ||
rg, err := regexp.Compile(res) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "invalid regex %s", res) | ||
} | ||
allowedRegexes = append(allowedRegexes, rg) | ||
} | ||
|
||
if len(blockedRegexes) > 0 && len(allowedRegexes) > 0 { | ||
err := fmt.Errorf("check has both \"allowList\" & \"blockList\" parameter's values set") | ||
return nil, errors.Wrapf(err, "only one of the paramater lists can be used at a time") | ||
} | ||
|
||
return util.PerContainerCheck(func(container *v1.Container) (results []diagnostic.Diagnostic) { | ||
if len(blockedRegexes) > 0 && isInList(blockedRegexes, container.Image) { | ||
results = append(results, diagnostic.Diagnostic{Message: fmt.Sprintf("The container %q is using an invalid container image, %q. Please reference the image using a SHA256 tag.", container.Name, container.Image)}) | ||
} else if len(allowedRegexes) > 0 && !isInList(allowedRegexes, container.Image) { | ||
results = append(results, diagnostic.Diagnostic{Message: fmt.Sprintf("The container %q is using an invalid container image, %q. Please reference the image using a SHA256 tag.", container.Name, container.Image)}) | ||
} | ||
return results | ||
}), nil | ||
}), | ||
}) | ||
} | ||
|
||
// isInList returns true if a match found in the list for the given name | ||
func isInList(regexlist []*regexp.Regexp, name string) bool { | ||
for _, regex := range regexlist { | ||
if regex.MatchString(name) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package imageshatag | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
"golang.stackrox.io/kube-linter/pkg/diagnostic" | ||
"golang.stackrox.io/kube-linter/pkg/lintcontext/mocks" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/imageshatag/internal/params" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
var ( | ||
containerName = "test-container" | ||
) | ||
|
||
func TestContainerImage(t *testing.T) { | ||
suite.Run(t, new(ContainerImageTestSuite)) | ||
} | ||
|
||
type ContainerImageTestSuite struct { | ||
templates.TemplateTestSuite | ||
|
||
ctx *mocks.MockLintContext | ||
} | ||
|
||
func (s *ContainerImageTestSuite) SetupTest() { | ||
s.Init(templateKey) | ||
s.ctx = mocks.NewMockContext() | ||
} | ||
|
||
func (s *ContainerImageTestSuite) addDeploymentWithContainerImage(name, containerImage string) { | ||
s.ctx.AddMockDeployment(s.T(), name) | ||
s.ctx.AddContainerToDeployment(s.T(), name, v1.Container{Name: containerName, Image: containerImage}) | ||
} | ||
|
||
func (s *ContainerImageTestSuite) TestImproperContainerImage() { | ||
const ( | ||
depwithNotAllowedImageTag = "dep-with-not-allowed-image-tag" | ||
) | ||
|
||
s.addDeploymentWithContainerImage(depwithNotAllowedImageTag, "example.com/test:latest") | ||
|
||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{ | ||
AllowList: []string{".*:[a-fA-F0-9]{64}$"}, | ||
}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
depwithNotAllowedImageTag: { | ||
{Message: "The container \"test-container\" is using an invalid container image, \"example.com/test:latest\". Please reference the image using a SHA256 tag."}, | ||
}, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} | ||
|
||
func (s *ContainerImageTestSuite) TestAcceptableContainerImage() { | ||
const ( | ||
depWithAcceptableImageTag = "dep-with-acceptable-container-image" | ||
) | ||
|
||
s.addDeploymentWithContainerImage(depWithAcceptableImageTag, "example.com/latest@sha256:75bf9b911b6481dcf29f7942240d1555adaa607eec7fc61bedb7f624f87c36d4") | ||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{ | ||
AllowList: []string{".*:[a-fA-F0-9]{64}$"}, | ||
}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
depWithAcceptableImageTag: nil, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
We could reuse the latest-tag template. probably we should change it's name