Skip to content

Commit

Permalink
golangci: add gosec to linter
Browse files Browse the repository at this point in the history
    - change json format to yaml
    - skip G204 (gosec)
    - fix others lint errors

Signed-off-by: Vicente Cheng <vicente.cheng@suse.com>
  • Loading branch information
Vicente-Cheng committed Jul 3, 2023
1 parent e487f04 commit e6c1457
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 38 deletions.
26 changes: 0 additions & 26 deletions .golangci.json

This file was deleted.

30 changes: 30 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
linters:
disable-all: true
enable:
- goimports
- gofmt
- misspell
- revive
- gosec
- prealloc
run:
skip-files:
- /zz_generated_
- _generated
skip-dirs:
- generated
deadline: 5m
tests: true
build-tags:
- test
linters-settings:
gosec:
# https://github.com/securego/gosec#available-rules
excludes:
- G101 # Look for hard coded credentials
- G108 # Profiling endpoint is automatically exposed on /debug/pprof
- G401 # Use of weak cryptographic primitive
- G402 # TLS InsecureSkipVerify set true
- G505 # Blocklisted import crypto/md5: weak cryptographic primitive
config:
G306: "0644" # Poor file permissions used when writing to a new file
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
_ "net/http/pprof"
"os"
"sync"
"time"

"github.com/ehazlett/simplelog"
"github.com/rancher/wrangler/pkg/kubeconfig"
Expand Down Expand Up @@ -150,7 +151,11 @@ func initProfiling(opt *option.Option) {
// enable profiler
if opt.ProfilerAddress != "" {
go func() {
log.Println(http.ListenAndServe(opt.ProfilerAddress, nil))
profilerServer := &http.Server{
Addr: opt.ProfilerAddress,
ReadHeaderTimeout: 10 * time.Second,
}
log.Println(profilerServer.ListenAndServe())
}()
}
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/block/blkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import (
"github.com/sirupsen/logrus"
)

const (
BLKIDCMD = "blkid"
)

func doCommandBlkid(partition string, param string) ([]byte, error) {
if !strings.HasPrefix(partition, "/dev") {
partition = "/dev/" + partition
}
args := []string{
"blkid",
"-s",
param,
partition,
"-o",
"value",
}
return exec.Command(args[0], args[1:]...).Output()
return exec.Command(BLKIDCMD, args[0:]...).Output() // #nosec G204
}

func GetFileSystemType(part string) string {
Expand Down
11 changes: 7 additions & 4 deletions pkg/block/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/sirupsen/logrus"
)

const (
LSBLKCMD = "lsblk"
)

func GetParentDevName(devPath string) (string, error) {
return lsblk(devPath, "pkname")
}
Expand All @@ -34,8 +38,8 @@ func GetPartType(devPath string) string {
}

func GetDevPathByPTUUID(ptUUID string) (string, error) {
args := []string{"lsblk", "-dJo", "PATH,PTUUID"}
out, err := exec.Command(args[0], args[1:]...).Output()
args := []string{"-dJo", "PATH,PTUUID"}
out, err := exec.Command(LSBLKCMD, args[0:]...).Output() // #nosec G204
if err != nil {
return "", fmt.Errorf("failed to execute `%s` for PTUUID %s: %w", strings.Join(args, " "), ptUUID, err)
}
Expand Down Expand Up @@ -64,12 +68,11 @@ func lsblk(devPath, output string) (string, error) {
devPath = "/dev/" + devPath
}
args := []string{
"lsblk",
"-dno",
output,
devPath,
}
out, err := exec.Command(args[0], args[1:]...).Output()
out, err := exec.Command(LSBLKCMD, args[0:]...).Output() // #nosec G204
if err != nil {
return "", fmt.Errorf("failed to execute `%s`: %s", strings.Join(args, " "), err.Error())
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/controller/blockdevice/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package blockdevice

import (
"context"
"crypto/rand"
"errors"
"fmt"
"math/rand"
"math/big"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -101,9 +102,6 @@ func Register(
opt *option.Option,
scanner *Scanner,
) error {
// Initialize random seed.
rand.Seed(time.Now().UnixNano())

controller := &Controller{
Namespace: opt.Namespace,
NodeName: opt.NodeName,
Expand Down Expand Up @@ -662,7 +660,12 @@ func needUpdateMountPoint(bd *diskv1.BlockDevice, filesystem *block.FileSystemIn
// jitterEnqueueDelay returns a random duration between 7 to 13.
func jitterEnqueueDelay() time.Duration {
enqueueDelay := 10
return time.Duration(rand.Intn(3)+enqueueDelay) * time.Second
randInt, err := rand.Int(rand.Reader, big.NewInt(3))
if err != nil {
logrus.Errorf("Failed to generate random number: %v", err)
randInt = big.NewInt(0)
}
return time.Duration(randInt.Sign()+enqueueDelay) * time.Second
}

func convertMountStr(mountOP NeedMountUpdateOP) string {
Expand Down

0 comments on commit e6c1457

Please sign in to comment.