Skip to content

Commit 6f0899e

Browse files
authored
Adapt pipeline_definition to include SAST linting logs in OCM descriptor (#347)
* Adapt pipeline_definition to include SAST linting logs in OCM descriptor * Replace depricated golint with golangci-lint and run sast with the check script * Add a check step to pipeline definitions
1 parent 8556b61 commit 6f0899e

File tree

8 files changed

+64
-27
lines changed

8 files changed

+64
-27
lines changed

Diff for: .ci/check

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ if [[ "${SOURCE_PATH}" != *"src/k8s.io/autoscaler" ]]; then
3838
export PATH="${GOBIN}:${PATH}"
3939
fi
4040

41-
# Install Golint (linting tool).
42-
go get -u github.com/golang/lint/golint
43-
go get -u golang.org/x/lint/golint
41+
# Install golangci-lint (linting tool).
42+
GOLANGCI_LINT_VERSION=v1.60.3
43+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@"${GOLANGCI_LINT_VERSION}"
4444

4545
###############################################################################
4646

4747
PACKAGES="$(go list -e ./... | grep -vE '/tmp/|/vendor/')"
4848
LINT_FOLDERS="$(echo ${PACKAGES} | sed "s|k8s.io/autoscaler/cluster-autoscaler|.|g")"
4949

50-
#TODO: To add lint checking, after fixing issues
50+
# Run Static Application Security Testing (SAST) using gosec
51+
make sast-report -C cluster-autoscaler

Diff for: .ci/pipeline_definitions

+16-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@ autoscaler:
22
base_definition:
33
repo:
44
source_labels:
5-
- name: 'cloud.gardener.cnudie/dso/scanning-hints/source_analysis/v1'
5+
- name: cloud.gardener.cnudie/dso/scanning-hints/source_analysis/v1
66
value:
7-
policy: 'scan'
8-
path_config:
9-
exclude_paths:
10-
- '.*/aws-sdk-go/.*'
11-
- '^vendor/.*'
12-
- '.*/vendor/.*'
13-
- '.*/cloudprovider/((?!mcm/).)*/.*'
14-
- '^addon-resizer/.*'
15-
- '^vertical-pod-autoscaler/.*'
7+
policy: skip
8+
comment: |
9+
we use gosec for sast scanning. See attached log.
1610
traits:
1711
version:
1812
preprocess:
@@ -37,6 +31,8 @@ autoscaler:
3731
steps:
3832
test:
3933
image: 'golang:1.22.2'
34+
check:
35+
image: 'golang:1.22.2'
4036
build:
4137
image: 'golang:1.22.2'
4238
output_dir: 'binary'
@@ -62,6 +58,16 @@ autoscaler:
6258
image: europe-docker.pkg.dev/gardener-project/releases/gardener/autoscaler/cluster-autoscaler
6359
release:
6460
nextversion: 'bump_minor'
61+
assets:
62+
- type: build-step-log
63+
step_name: check
64+
purposes:
65+
- lint
66+
- sast
67+
- gosec
68+
comment: |
69+
we use gosec (linter) for SAST scans
70+
see: https://github.com/securego/gosec
6571
slack:
6672
default_channel: 'internal_scp_workspace'
6773
channel_cfgs:

Diff for: cluster-autoscaler/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ Session.vim
1212
.netrwhist
1313
.vscode
1414
./integration/logs
15+
16+
# gosec
17+
gosec-report.sarif

Diff for: cluster-autoscaler/cloudprovider/mcm/mcm_manager.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package mcm
2323

2424
import (
2525
"context"
26+
"crypto/rand"
2627
"encoding/json"
2728
"errors"
2829
"flag"
@@ -36,7 +37,8 @@ import (
3637
v1appslister "k8s.io/client-go/listers/apps/v1"
3738
"k8s.io/utils/pointer"
3839
"maps"
39-
"math/rand"
40+
"math"
41+
"math/big"
4042
"net/http"
4143
"os"
4244
"slices"
@@ -471,9 +473,9 @@ func (m *McmManager) SetMachineDeploymentSize(ctx context.Context, nodeGroup *no
471473
// don't scale down during rolling update, as that could remove ready node with workload
472474
if md.Spec.Replicas >= int32(size) && !isRollingUpdateFinished(md) {
473475
return false, fmt.Errorf("MachineDeployment %s is under rolling update , cannot reduce replica count", md.Name)
474-
}
476+
} // #nosec G115 (CWE-190) -- replicas will not overflow the range of int32
475477
clone := md.DeepCopy()
476-
clone.Spec.Replicas = int32(size)
478+
clone.Spec.Replicas = int32(size) // #nosec G115 (CWE-190) -- replicas will not overflow the range of int32
477479

478480
_, err = m.machineClient.MachineDeployments(nodeGroup.Namespace).Update(ctx, clone, metav1.UpdateOptions{})
479481
return true, err
@@ -922,7 +924,12 @@ func getZoneValueFromMCLabels(labels map[string]string) string {
922924

923925
func (m *McmManager) buildNodeFromTemplate(name string, template *nodeTemplate) (*apiv1.Node, error) {
924926
node := apiv1.Node{}
925-
nodeName := fmt.Sprintf("%s-%d", name, rand.Int63())
927+
n, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
928+
if err != nil {
929+
fmt.Println("error:", err)
930+
return &node, err
931+
}
932+
nodeName := fmt.Sprintf("%s-%d", name, n.Int64())
926933

927934
node.ObjectMeta = metav1.ObjectMeta{
928935
Name: nodeName,
@@ -1118,7 +1125,7 @@ func computeScaleDownData(md *v1alpha1.MachineDeployment, machineNamesForDeletio
11181125
data.RevisedScaledownAmount = uniqueForDeletionSet.Len()
11191126
data.RevisedMachineDeployment = nil
11201127

1121-
expectedReplicas := md.Spec.Replicas - int32(data.RevisedScaledownAmount)
1128+
expectedReplicas := md.Spec.Replicas - int32(data.RevisedScaledownAmount) // #nosec G115 (CWE-190) -- RevisedScaledownAmount will not overflow the range of int32
11221129
if expectedReplicas == md.Spec.Replicas {
11231130
klog.Infof("MachineDeployment %q is already set to %d, no need to scale-down", md.Name, expectedReplicas)
11241131
} else if expectedReplicas < 0 {

Diff for: cluster-autoscaler/cloudprovider/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func BuildKubeProxy(name string) *apiv1.Pod {
7777
priority := scheduling.SystemCriticalPriority
7878
return &apiv1.Pod{
7979
ObjectMeta: metav1.ObjectMeta{
80-
Name: fmt.Sprintf("kube-proxy-%s-%d", name, rand.Int63()),
80+
Name: fmt.Sprintf("kube-proxy-%s-%d", name, rand.Int63()), // #nosec G404 (CWE-338) -- code inherited from upstream
8181
Namespace: "kube-system",
8282
Annotations: map[string]string{
8383
kubetypes.ConfigSourceAnnotationKey: kubetypes.FileSource,

Diff for: cluster-autoscaler/hack/sast.sh

+20
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ dir_to_exclude="-exclude-dir=cloudprovider/alicloud
4343
-exclude-dir=cloudprovider/baiducloud
4444
-exclude-dir=cloudprovider/bizflycloud
4545
-exclude-dir=cloudprovider/brightbox
46+
-exclude-dir=cloudprovider/builder
4647
-exclude-dir=cloudprovider/cherryservers
4748
-exclude-dir=cloudprovider/civo
4849
-exclude-dir=cloudprovider/cloudstack
4950
-exclude-dir=cloudprovider/clusterapi
5051
-exclude-dir=cloudprovider/digitalocean
5152
-exclude-dir=cloudprovider/equinixmetal
5253
-exclude-dir=cloudprovider/exoscale
54+
-exclude-dir=cloudprovider/externalgrpc
5355
-exclude-dir=cloudprovider/gce
5456
-exclude-dir=cloudprovider/hetzner
5557
-exclude-dir=cloudprovider/huaweicloud
@@ -66,6 +68,24 @@ dir_to_exclude="-exclude-dir=cloudprovider/alicloud
6668
-exclude-dir=cloudprovider/tencentcloud
6769
-exclude-dir=cloudprovider/volcengine
6870
-exclude-dir=cloudprovider/vultr
71+
-exclude-dir=apis
72+
-exclude-dir=cluster-state
73+
-exclude-dir=config
74+
-exclude-dir=context
75+
-exclude-dir=core
76+
-exclude-dir=debuggingsnapshot
77+
-exclude-dir=estimator
78+
-exclude-dir=expander
79+
-exclude-dir=hack
80+
-exclude-dir=loop
81+
-exclude-dir=metrics
82+
-exclude-dir=observers
83+
-exclude-dir=processors
84+
-exclude-dir=proposals
85+
-exclude-dir=provisioningrequest
86+
-exclude-dir=simulator
87+
-exclude-dir=util
88+
-exclude-dir=version
6989
"
7090

7191
${TOOLS_BIN_DIR}/gosec -exclude-generated $dir_to_exclude $gosec_report_parse_flags ./...

Diff for: cluster-autoscaler/integration/framework.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ func rotateLogFile(fileName string) (*os.File, error) {
6464

6565
if _, err := os.Stat(fileName); err == nil { // !strings.Contains(err.Error(), "no such file or directory") {
6666
for i := 9; i > 0; i-- {
67-
os.Rename(fmt.Sprintf("%s.%d", fileName, i), fmt.Sprintf("%s.%d", fileName, i+1))
67+
_ = os.Rename(fmt.Sprintf("%s.%d", fileName, i), fmt.Sprintf("%s.%d", fileName, i+1))
6868
}
69-
os.Rename(fileName, fmt.Sprintf("%s.%d", fileName, 1))
69+
_ = os.Rename(fileName, fmt.Sprintf("%s.%d", fileName, 1))
7070
}
7171

72-
return os.Create(fileName)
72+
return os.Create(fileName) //#nosec G304 (CWE-22) -- this is used only for tests. Cannot be exploited
7373
}
7474

7575
func (driver *Driver) addTaintsToInitialNodes() error {
@@ -135,7 +135,7 @@ func (driver *Driver) adjustNodeGroups() error {
135135
}
136136

137137
// getNumberOfReadyNodes tries to retrieve the list of node objects in the cluster.
138-
func (c *Cluster) getNumberOfReadyNodes() int16 {
138+
func (c *Cluster) getNumberOfReadyNodes() int {
139139
nodes, _ := c.Clientset.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
140140
count := 0
141141
for _, n := range nodes.Items {
@@ -146,7 +146,7 @@ func (c *Cluster) getNumberOfReadyNodes() int16 {
146146
}
147147
}
148148
}
149-
return int16(count)
149+
return count
150150
}
151151

152152
func (driver *Driver) scaleAutoscaler(replicas int32) error {
@@ -206,7 +206,7 @@ func (driver *Driver) runAutoscaler() {
206206

207207
outputFile, err := rotateLogFile(CALogFile)
208208
gom.Expect(err).ShouldNot(gom.HaveOccurred())
209-
autoscalerSession, err = gexec.Start(exec.Command(args[0], args[1:]...), outputFile, outputFile)
209+
autoscalerSession, err = gexec.Start(exec.Command(args[0], args[1:]...), outputFile, outputFile) //#nosec G204 (CWE-78) -- this is used only for tests. Cannot be exploited
210210
gom.Expect(err).ShouldNot(gom.HaveOccurred())
211211
gom.Expect(autoscalerSession.ExitCode()).Should(gom.Equal(-1))
212212
}

Diff for: cluster-autoscaler/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ func main() {
669669
if *enableProfiling {
670670
routes.Profiling{}.Install(pathRecorderMux)
671671
}
672-
err := http.ListenAndServe(*address, pathRecorderMux)
672+
err := http.ListenAndServe(*address, pathRecorderMux) // #nosec G114 (CWE-676) -- code inherited from upstream
673673
klog.Fatalf("Failed to start metrics: %v", err)
674674
}()
675675

0 commit comments

Comments
 (0)