Skip to content

Commit 68ddc2e

Browse files
authored
Merge branch 'stackrox:main' into env-value-from-feature
2 parents 394cce6 + 6767e13 commit 68ddc2e

30 files changed

+1436
-5
lines changed

docs/generated/checks.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,21 @@ dirs:
629629
- ^/sys$
630630
- ^/usr$
631631
```
632+
## sorted-keys
633+
634+
**Enabled by default**: No
635+
636+
**Description**: Check that YAML keys are sorted in alphabetical order wherever possible.
637+
638+
**Remediation**: Ensure that keys in your YAML manifest are sorted in alphabetical order to improve consistency and readability.
639+
640+
**Template**: [sorted-keys](templates.md#sorted-keys)
641+
642+
**Parameters**:
643+
644+
```yaml
645+
recursive: true
646+
```
632647
## ssh-port
633648
634649
**Enabled by default**: Yes

docs/generated/templates.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,25 @@ KubeLinter supports the following templates:
850850
type: string
851851
```
852852

853+
## Sorted Keys
854+
855+
**Key**: `sorted-keys`
856+
857+
**Description**: Flag YAML keys that are not sorted in alphabetical order
858+
859+
**Supported Objects**: Any
860+
861+
862+
**Parameters**:
863+
864+
```yaml
865+
- description: Recursive determines whether to check keys recursively at all nesting
866+
levels. Default is true.
867+
name: recursive
868+
required: false
869+
type: boolean
870+
```
871+
853872
## Startup Port Exposed
854873

855874
**Key**: `startup-port`

e2etests/bats-tests.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,25 @@ get_value_from() {
938938
[[ "${count}" == "2" ]]
939939
}
940940

941+
@test "sorted-keys" {
942+
tmp="tests/checks/sorted-keys.yaml"
943+
cmd="${KUBE_LINTER_BIN} lint --include sorted-keys --do-not-auto-add-defaults --format json ${tmp}"
944+
run ${cmd}
945+
946+
print_info "${status}" "${output}" "${cmd}" "${tmp}"
947+
[ "$status" -eq 1 ]
948+
949+
message1=$(get_value_from "${lines[0]}" '.Reports[0].Object.K8sObject.GroupVersionKind.Kind + ": " + .Reports[0].Diagnostic.Message')
950+
message2=$(get_value_from "${lines[0]}" '.Reports[1].Object.K8sObject.GroupVersionKind.Kind + ": " + .Reports[1].Diagnostic.Message')
951+
message3=$(get_value_from "${lines[0]}" '.Reports[2].Object.K8sObject.GroupVersionKind.Kind + ": " + .Reports[2].Diagnostic.Message')
952+
count=$(get_value_from "${lines[0]}" '.Reports | length')
953+
954+
[[ "${message1}" == "Deployment: Keys are not sorted at spec.template.spec.containers[0]. Expected order: [image, name, ports], got: [name, image, ports]" ]]
955+
[[ "${message2}" == "Deployment: Keys are not sorted at root. Expected order: [apiVersion, kind, metadata, spec], got: [apiVersion, metadata, spec, kind]" ]]
956+
[[ "${message3}" == "Deployment: Keys are not sorted at spec.template. Expected order: [metadata, spec], got: [spec, metadata]" ]]
957+
[[ "${count}" == "27" ]]
958+
}
959+
941960
@test "ssh-port" {
942961
tmp="tests/checks/ssh-port.yml"
943962
cmd="${KUBE_LINTER_BIN} lint --include ssh-port --do-not-auto-add-defaults --format json ${tmp}"
@@ -1112,7 +1131,7 @@ get_value_from() {
11121131

11131132
@test "flag-ignore-paths" {
11141133
tmp="."
1115-
cmd="${KUBE_LINTER_BIN} lint --ignore-paths \"tests/**\" --ignore-paths \"e2etests/**\" ${tmp}"
1134+
cmd="${KUBE_LINTER_BIN} lint --ignore-paths \"tests/**\" --ignore-paths \"e2etests/**\" --ignore-paths \"pkg/**/testdata/**\" ${tmp}"
11161135
run ${cmd}
11171136
print_info "${status}" "${output}" "${cmd}" "${tmp}"
11181137
[ "$status" -eq 0 ]

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/cert-manager/cert-manager v1.19.1
99
github.com/fatih/color v1.18.0
1010
github.com/go-viper/mapstructure/v2 v2.4.0
11+
github.com/goccy/go-yaml v1.18.0
1112
github.com/google/cel-go v0.26.1
1213
github.com/mitchellh/go-homedir v1.1.0
1314
github.com/mitchellh/mapstructure v1.5.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9L
111111
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
112112
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
113113
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
114+
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
115+
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
114116
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
115117
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
116118
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: "sorted-keys"
2+
description: "Check that YAML keys are sorted in alphabetical order wherever possible."
3+
remediation: "Ensure that keys in your YAML manifest are sorted in alphabetical order to improve consistency and readability."
4+
scope:
5+
objectKinds:
6+
- Any
7+
template: "sorted-keys"
8+
params:
9+
recursive: true

pkg/lintcontext/mocks/context.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ import (
77

88
// MockLintContext is mock implementation of the LintContext used in unit tests
99
type MockLintContext struct {
10-
objects map[string]k8sutil.Object
10+
objects map[string]k8sutil.Object
11+
rawObjects map[string][]byte
1112
}
1213

1314
// Objects returns all the objects under this MockLintContext
1415
func (l *MockLintContext) Objects() []lintcontext.Object {
1516
result := make([]lintcontext.Object, 0, len(l.objects))
16-
for _, p := range l.objects {
17-
result = append(result, lintcontext.Object{Metadata: lintcontext.ObjectMetadata{}, K8sObject: p})
17+
for key, p := range l.objects {
18+
metadata := lintcontext.ObjectMetadata{}
19+
if raw, ok := l.rawObjects[key]; ok {
20+
metadata.Raw = raw
21+
}
22+
result = append(result, lintcontext.Object{Metadata: metadata, K8sObject: p})
1823
}
1924
return result
2025
}
@@ -26,10 +31,19 @@ func (l *MockLintContext) InvalidObjects() []lintcontext.InvalidObject {
2631

2732
// NewMockContext returns an empty mockLintContext
2833
func NewMockContext() *MockLintContext {
29-
return &MockLintContext{objects: make(map[string]k8sutil.Object)}
34+
return &MockLintContext{
35+
objects: make(map[string]k8sutil.Object),
36+
rawObjects: make(map[string][]byte),
37+
}
3038
}
3139

3240
// AddObject adds an object to the MockLintContext
3341
func (l *MockLintContext) AddObject(key string, obj k8sutil.Object) {
3442
l.objects[key] = obj
3543
}
44+
45+
// AddObjectWithRaw adds an object to the MockLintContext with raw YAML data
46+
func (l *MockLintContext) AddObjectWithRaw(key string, obj k8sutil.Object, raw []byte) {
47+
l.objects[key] = obj
48+
l.rawObjects[key] = raw
49+
}

pkg/templates/all/all.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import (
5757
_ "golang.stackrox.io/kube-linter/pkg/templates/sccdenypriv"
5858
_ "golang.stackrox.io/kube-linter/pkg/templates/serviceaccount"
5959
_ "golang.stackrox.io/kube-linter/pkg/templates/servicetype"
60+
_ "golang.stackrox.io/kube-linter/pkg/templates/sortedkeys"
6061
_ "golang.stackrox.io/kube-linter/pkg/templates/startupport"
6162
_ "golang.stackrox.io/kube-linter/pkg/templates/sysctl"
6263
_ "golang.stackrox.io/kube-linter/pkg/templates/targetport"

pkg/templates/sortedkeys/internal/params/gen-params.go

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package params
2+
3+
// Params represents the params accepted by this template.
4+
type Params struct {
5+
// Recursive determines whether to check keys recursively at all nesting levels.
6+
// Default is true.
7+
Recursive bool
8+
}

0 commit comments

Comments
 (0)