-
Notifications
You must be signed in to change notification settings - Fork 267
Add supply-chain and secrets access checks #1214
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| name: cluster-wide-secrets-access | ||
| description: >- | ||
| ClusterRoleBinding granting secrets access (get, list, watch, create, | ||
| update, patch, delete) across all namespaces. When bound to an operator | ||
| service account that reconciles tenant-controlled custom resources, this | ||
| enables cross-namespace secret exfiltration (confused deputy). | ||
| remediation: >- | ||
| Use namespaced RoleBindings to limit secrets access to required namespaces, | ||
| or restrict the ClusterRole to specific resource names if cluster-wide | ||
| access is necessary. | ||
| scope: | ||
| objectKinds: | ||
| - ClusterRoleBinding | ||
| template: "access-to-resources" | ||
| params: | ||
| resources: ["^secrets$"] | ||
| verbs: ["^get$", "^list$", "^watch$", "^create$", "^update$", "^patch$", "^delete$", "^\\*$"] | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||||||||||||||||||||||||||
| name: configmap-with-credentials | ||||||||||||||||||||||||||||||
| description: >- | ||||||||||||||||||||||||||||||
| ConfigMap contains keys with credential-like names (password, secret, | ||||||||||||||||||||||||||||||
| token, api_key). Credentials should be stored in Secrets, not ConfigMaps. | ||||||||||||||||||||||||||||||
| remediation: >- | ||||||||||||||||||||||||||||||
| Move credential values to a Secret resource. Reference the Secret from | ||||||||||||||||||||||||||||||
| pod specs using secretKeyRef instead of configMapKeyRef. | ||||||||||||||||||||||||||||||
| scope: | ||||||||||||||||||||||||||||||
| objectKinds: | ||||||||||||||||||||||||||||||
| - Any | ||||||||||||||||||||||||||||||
| template: cel-expression | ||||||||||||||||||||||||||||||
| params: | ||||||||||||||||||||||||||||||
| check: > | ||||||||||||||||||||||||||||||
| object.kind == 'ConfigMap' && | ||||||||||||||||||||||||||||||
| has(object.data) && | ||||||||||||||||||||||||||||||
| object.data.exists(k, | ||||||||||||||||||||||||||||||
| k.contains('password') || k.contains('secret') || k.contains('token') || | ||||||||||||||||||||||||||||||
| k.contains('api_key') || k.contains('apikey') || k.contains('private_key') || | ||||||||||||||||||||||||||||||
| k.contains('credential') | ||||||||||||||||||||||||||||||
| ) ? 'ConfigMap contains credential-like keys — use Secrets instead' : '' | ||||||||||||||||||||||||||||||
|
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. 🔒 Security & Privacy | 🟠 Major 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
# Locate the target file and any CEL-related implementation/tests.
git ls-files 'pkg/builtinchecks/yamls/*' 'pkg/**' | sed -n '1,200p'
printf '\n--- target file outline ---\n'
ast-grep outline pkg/builtinchecks/yamls/configmap-with-credentials.yaml --view expanded || true
printf '\n--- search for matches()/regex usage in yamls checks ---\n'
rg -n --hidden --glob '!**/node_modules/**' --glob '!**/dist/**' --glob '!**/build/**' \
"matches\(|contains\(|binaryData|api[_-]?key|private[_-]?key|credential" pkg/builtinchecks/yamls pkg 2>/dev/null | sed -n '1,240p'Repository: stackrox/kube-linter Length of output: 15758 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Read the relevant YAML file in a narrow slice with line numbers.
wc -l pkg/builtinchecks/yamls/configmap-with-credentials.yaml
printf '\n--- file excerpt ---\n'
cat -n pkg/builtinchecks/yamls/configmap-with-credentials.yaml | sed -n '1,120p'Repository: stackrox/kube-linter Length of output: 1155 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Inspect CEL template/runtime wiring and any tests around supported functions.
ast-grep outline pkg/templates/cel/template.go --view expanded || true
printf '\n--- cel template excerpt ---\n'
cat -n pkg/templates/cel/template.go | sed -n '1,260p'
printf '\n--- search for cel-go dependency and matches support references ---\n'
rg -n "cel-go|google/cel|matches\(|startsWith\(|endsWith\(|contains\(" go.mod go.sum pkg test .github 2>/dev/null | sed -n '1,260p'Repository: stackrox/kube-linter Length of output: 5375 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Inspect the CEL template and tests for expression evaluation behavior.
wc -l pkg/templates/cel/template.go pkg/templates/cel/template_test.go
printf '\n--- template.go excerpt ---\n'
cat -n pkg/templates/cel/template.go | sed -n '1,260p'
printf '\n--- template_test.go excerpt ---\n'
cat -n pkg/templates/cel/template_test.go | sed -n '1,260p'Repository: stackrox/kube-linter Length of output: 9252 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Find the CEL environment construction and any custom function declarations.
rg -n "cel\.NewEnv|cel\.EnvOption|functions\.|matches|strings\." pkg go.mod go.sum 2>/dev/null | sed -n '1,260p'Repository: stackrox/kube-linter Length of output: 30628 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Locate CEL environment creation and the exact expression evaluator used by built-in checks.
rg -n "cel\.NewEnv|cel\.Compile|cel\.Program|Evaluate|cel-expression|NewProgram|EnvOption|FunctionDecl|matches\(" pkg | sed -n '1,260p'Repository: stackrox/kube-linter Length of output: 662 🌐 Web query:
💡 Result: In the Common Expression Language (CEL), the string method Citations:
🌐 Web query:
💡 Result: In cel-go v0.28.1, the Citations:
Expand the check to inspect
Suggested change- has(object.data) &&
- object.data.exists(k,
- k.contains('password') || k.contains('secret') || k.contains('token') ||
- k.contains('api_key') || k.contains('apikey') || k.contains('private_key') ||
- k.contains('credential')
- ) ? 'ConfigMap contains credential-like keys — use Secrets instead' : ''
+ (
+ (has(object.data) && object.data.exists(k,
+ k.matches('(?i).*(password|secret|token|api[_-]?key|private[_-]?key|credential).*')
+ )) ||
+ (has(object.binaryData) && object.binaryData.exists(k,
+ k.matches('(?i).*(password|secret|token|api[_-]?key|private[_-]?key|credential).*')
+ ))
+ ) ? 'ConfigMap contains credential-like keys — use Secrets instead' : ''📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| name: externalname-service-redirect | ||
| description: >- | ||
| Service with type: ExternalName redirects internal traffic to an external | ||
| DNS name. If the external name resolves to an attacker-controlled host, | ||
| this enables SSRF at the infrastructure level. | ||
| remediation: >- | ||
| Use type: ClusterIP with an Endpoints object pointing to a known-safe IP, | ||
| or validate that the externalName resolves to a trusted destination. | ||
| scope: | ||
| objectKinds: | ||
| - Any | ||
| template: cel-expression | ||
| params: | ||
| check: > | ||
| object.spec.type == 'ExternalName' && | ||
| has(object.spec.externalName) | ||
| ? 'ExternalName service redirects cluster traffic to external DNS — potential SSRF vector' : '' | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| name: "image-not-pinned-by-digest" | ||
| description: >- | ||
| Container images referenced by tag are mutable — a tag can be moved to | ||
| point to a different image at any time. Pinning by digest guarantees | ||
| the exact image content that will run. | ||
| remediation: >- | ||
| Reference container images by digest (e.g. image@sha256:abc123...) | ||
| instead of by tag. | ||
| scope: | ||
| objectKinds: | ||
| - DeploymentLike | ||
| template: "image-not-pinned-by-digest" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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 specifying pattern(s) for container images to exempt from the digest check. | ||
| AllowList []string | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package imagedigest | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "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/imagedigest/internal/params" | ||
| "golang.stackrox.io/kube-linter/pkg/templates/util" | ||
| v1 "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| templateKey = "image-not-pinned-by-digest" | ||
| ) | ||
|
|
||
| func init() { | ||
| templates.Register(check.Template{ | ||
| HumanName: "Image Not Pinned By Digest", | ||
| Key: templateKey, | ||
| Description: "Flag container images that are not pinned to a specific digest", | ||
| SupportedObjectKinds: config.ObjectKindsDesc{ | ||
| ObjectKinds: []string{objectkinds.DeploymentLike}, | ||
| }, | ||
| Parameters: params.ParamDescs, | ||
| ParseAndValidateParams: params.ParseAndValidate, | ||
| Instantiate: params.WrapInstantiateFunc(func(p params.Params) (check.Func, error) { | ||
| allowedRegexes := make([]*regexp.Regexp, 0, len(p.AllowList)) | ||
| for _, res := range p.AllowList { | ||
| rg, err := regexp.Compile(res) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid regex %s: %w", res, err) | ||
| } | ||
| allowedRegexes = append(allowedRegexes, rg) | ||
| } | ||
|
|
||
| return util.PerContainerCheck(func(container *v1.Container) (results []diagnostic.Diagnostic) { | ||
| if isAllowed(allowedRegexes, container.Image) { | ||
| return nil | ||
| } | ||
| if !strings.Contains(container.Image, "@sha256:") { | ||
| results = append(results, diagnostic.Diagnostic{ | ||
| Message: fmt.Sprintf("container %q image %q is not pinned by digest", container.Name, container.Image), | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| return results | ||
| }), nil | ||
| }), | ||
| }) | ||
| } | ||
|
|
||
| func isAllowed(regexlist []*regexp.Regexp, name string) bool { | ||
| for _, regex := range regexlist { | ||
| if regex.MatchString(name) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: ClusterRole | ||
| metadata: | ||
| name: secrets-reader | ||
| rules: | ||
| - apiGroups: [""] | ||
| resources: ["secrets"] | ||
| verbs: ["get", "list"] | ||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: ClusterRoleBinding | ||
| metadata: | ||
| name: fire-cluster-secrets | ||
| subjects: | ||
| - kind: ServiceAccount | ||
| name: operator-sa | ||
| namespace: operators | ||
| roleRef: | ||
| apiGroup: rbac.authorization.k8s.io | ||
| kind: ClusterRole | ||
| name: secrets-reader | ||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: Role | ||
| metadata: | ||
| name: ns-secrets-reader | ||
| namespace: default | ||
| rules: | ||
| - apiGroups: [""] | ||
| resources: ["secrets"] | ||
| verbs: ["get"] | ||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: RoleBinding | ||
| metadata: | ||
| name: dont-fire-ns-scoped | ||
| namespace: default | ||
| subjects: | ||
| - kind: ServiceAccount | ||
| name: app-sa | ||
| namespace: default | ||
| roleRef: | ||
| apiGroup: rbac.authorization.k8s.io | ||
| kind: Role | ||
| name: ns-secrets-reader |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: dont-fire | ||
| data: | ||
| config.yaml: | | ||
| logLevel: info | ||
| --- | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: fire-password-key | ||
| data: | ||
| database_password: "hunter2" | ||
| connection_string: "postgres://localhost:5432" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: dont-fire | ||
| spec: | ||
| type: ClusterIP | ||
| ports: | ||
| - port: 80 | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: fire-externalname | ||
| spec: | ||
| type: ExternalName | ||
| externalName: attacker.example.com |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: dont-fire | ||
| spec: | ||
| template: | ||
| spec: | ||
| containers: | ||
| - name: app | ||
| image: registry.example.com/app@sha256:abc123def456 | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: fire-tag-only | ||
| spec: | ||
| template: | ||
| spec: | ||
| containers: | ||
| - name: app | ||
| image: registry.example.com/app:v1.2.3 | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: fire-no-tag | ||
| spec: | ||
| template: | ||
| spec: | ||
| containers: | ||
| - name: app | ||
| image: registry.example.com/app |
Uh oh!
There was an error while loading. Please reload this page.