-
Notifications
You must be signed in to change notification settings - Fork 255
add check for validating improper container image tags #191
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
Merged
viswajithiii
merged 1 commit into
stackrox:main
from
Priyankasaggu11929:add-untrusted-images
Jul 7, 2021
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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: "latest-tag" | ||
description: "Indicates when a deployment-like object is running a container with a floating image tag, \"latest\"" | ||
remediation: "Use a container image with a proper image tag, outside the set blocked tag regex \".*:(latest)$\"." | ||
scope: | ||
objectKinds: | ||
- DeploymentLike | ||
template: "latest-tag" | ||
params: | ||
BlockList: [".*:(latest)$" ] | ||
This file contains hidden or 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 hidden or 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,8 @@ | ||
package params | ||
|
||
// Params represents the params accepted by this template. | ||
type Params struct { | ||
|
||
// list of regular expressions for blocked or bad container image tags | ||
BlockList []string | ||
} |
This file contains hidden or 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,73 @@ | ||
package latesttag | ||
|
||
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/extract" | ||
"golang.stackrox.io/kube-linter/pkg/lintcontext" | ||
"golang.stackrox.io/kube-linter/pkg/objectkinds" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/latesttag/internal/params" | ||
) | ||
|
||
const ( | ||
templateKey = "latest-tag" | ||
) | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
HumanName: "Latest Tag", | ||
Key: templateKey, | ||
Description: "Flag applications running containers with floating container image tag, \"latest\"", | ||
SupportedObjectKinds: config.ObjectKindsDesc{ | ||
ObjectKinds: []string{objectkinds.DeploymentLike}, | ||
}, | ||
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) | ||
} | ||
|
||
return func(_ lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic { | ||
podSpec, found := extract.PodSpec(object.K8sObject) | ||
if !found { | ||
return nil | ||
} | ||
|
||
var results []diagnostic.Diagnostic | ||
|
||
for _, container := range podSpec.Containers { | ||
if isInList(blockedRegexes, container.Image) { | ||
results = append(results, diagnostic.Diagnostic{Message: fmt.Sprintf("The container %q is using a floating image tag, %q.", 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 hidden or 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,83 @@ | ||
package latesttag | ||
|
||
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/latesttag/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) TestImproperContainerTag() { | ||
const ( | ||
depWithLatestAsContainerImageTag = "dep-with-latest-as-container-image-tag" | ||
) | ||
|
||
s.addDeploymentWithContainerImage(depWithLatestAsContainerImageTag, "example.com/test:latest") | ||
|
||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{ | ||
BlockList: []string{".*:(latest)$"}, | ||
}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
depWithLatestAsContainerImageTag: { | ||
{Message: "The container \"test-container\" is using a floating image tag, \"example.com/test:latest\"."}, | ||
}, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} | ||
|
||
func (s *ContainerImageTestSuite) TestAcceptableContainerImage() { | ||
const ( | ||
depWithLatestAsContainerImageName = "dep-with-latest-as-container-image-name" | ||
depWithAcceptableContainerImage = "dep-with-acceptable-container-image" | ||
) | ||
|
||
s.addDeploymentWithContainerImage(depWithLatestAsContainerImageName, "example.com/latest:v1.0.0") | ||
s.addDeploymentWithContainerImage(depWithAcceptableContainerImage, "example.com/test:v1.0.0") | ||
|
||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{ | ||
BlockList: []string{".*:(latest)$"}, | ||
}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
depWithLatestAsContainerImageName: nil, | ||
depWithAcceptableContainerImage: nil, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.