Skip to content

Commit 65b4a2d

Browse files
authored
chore: fix github code scanning alerts (#9635)
1 parent a73ab1e commit 65b4a2d

6 files changed

Lines changed: 36 additions & 36 deletions

File tree

pkg/common/password.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package common
2222
import (
2323
"crypto/sha256"
2424
"encoding/binary"
25-
mathrand "math/rand"
25+
"math/rand"
2626
"strings"
2727
"time"
2828
"unicode"
@@ -33,24 +33,24 @@ import (
3333
)
3434

3535
const (
36-
// DefaultSymbols is the list of default symbols to generate password.
37-
DefaultSymbols = "!@#&*"
36+
// defaultSymbols is the list of default symbols to generate password.
37+
defaultSymbols = "!@#&*"
3838
)
3939

40-
type PasswordReader struct {
41-
rand *mathrand.Rand
40+
type passwordReader struct {
41+
rand *rand.Rand
4242
}
4343

44-
func (r *PasswordReader) Read(data []byte) (int, error) {
44+
func (r *passwordReader) Read(data []byte) (int, error) {
4545
return r.rand.Read(data)
4646
}
4747

48-
func (r *PasswordReader) Seed(seed int64) {
48+
func (r *passwordReader) Seed(seed int64) {
4949
r.rand.Seed(seed)
5050
}
5151

5252
func GeneratePasswordByConfig(config appsv1.PasswordConfig) (string, error) {
53-
passwd, err := GeneratePassword((int)(config.Length), (int)(config.NumDigits), (int)(config.NumSymbols), config.Seed, config.SymbolCharacters)
53+
passwd, err := generatePassword((int)(config.Length), (int)(config.NumDigits), (int)(config.NumSymbols), config.Seed, config.SymbolCharacters)
5454
if err != nil {
5555
return "", err
5656
}
@@ -60,38 +60,38 @@ func GeneratePasswordByConfig(config appsv1.PasswordConfig) (string, error) {
6060
case appsv1.LowerCases:
6161
passwd = strings.ToLower(passwd)
6262
case appsv1.MixedCases:
63-
passwd, err = EnsureMixedCase(passwd, config.Seed)
63+
passwd, err = ensureMixedCase(passwd, config.Seed)
6464
}
6565
return passwd, err
6666
}
6767

68-
// GeneratePassword generates a password with the given requirements and seed in lowercase.
69-
func GeneratePassword(length, numDigits, numSymbols int, seed string, symbols string) (string, error) {
70-
rand, err := newRngFromSeed(seed)
68+
// generatePassword generates a password with the given requirements and seed in lowercase.
69+
func generatePassword(length, numDigits, numSymbols int, seed string, symbols string) (string, error) {
70+
// #nosec G404
71+
rand, err := newRandFromSeed(seed)
7172
if err != nil {
7273
return "", err
7374
}
74-
passwordReader := &PasswordReader{rand: rand}
7575
if symbols == "" {
76-
symbols = DefaultSymbols
76+
symbols = defaultSymbols
7777
}
7878
gen, err := password.NewGenerator(&password.GeneratorInput{
7979
LowerLetters: password.LowerLetters,
8080
UpperLetters: password.UpperLetters,
8181
Symbols: symbols,
8282
Digits: password.Digits,
83-
Reader: passwordReader,
83+
Reader: &passwordReader{rand: rand},
8484
})
8585
if err != nil {
8686
return "", err
8787
}
8888
return gen.Generate(length, numDigits, numSymbols, true, true)
8989
}
9090

91-
// EnsureMixedCase randomizes the letter casing in the given string, ensuring
91+
// ensureMixedCase randomizes the letter casing in the given string, ensuring
9292
// that the result contains at least one uppercase and one lowercase letter.
9393
// If the give string only has one letter, it is returned unmodified.
94-
func EnsureMixedCase(in, seed string) (string, error) {
94+
func ensureMixedCase(in, seed string) (string, error) {
9595
runes := []rune(in)
9696
letterIndices := make([]int, 0, len(runes))
9797
for i, r := range runes {
@@ -107,11 +107,11 @@ func EnsureMixedCase(in, seed string) (string, error) {
107107

108108
// Get a random number x in [1, 2^L - 2], whose binary list will be used to determine the letter casing.
109109
// avoid the all-0 and all-1 patterns, which cause all-lowercase and all-uppercase password.
110-
rng, err := newRngFromSeed(seed)
110+
rand, err := newRandFromSeed(seed)
111111
if err != nil {
112112
return in, err
113113
}
114-
x := uint64(rng.Int63n(int64((1<<L)-2))) + 1
114+
x := uint64(rand.Int63n(int64((1<<L)-2))) + 1
115115

116116
for i := 0; i < L; i++ {
117117
bit := (x >> i) & 1
@@ -125,11 +125,9 @@ func EnsureMixedCase(in, seed string) (string, error) {
125125
return string(runes), nil
126126
}
127127

128-
// newRngFromSeed initializes a *mathrand.Rand from the given seed. If seed is empty,
129-
// it uses the current time, making the output non-deterministic.
130-
func newRngFromSeed(seed string) (*mathrand.Rand, error) {
128+
func newRandFromSeed(seed string) (*rand.Rand, error) {
131129
if seed == "" {
132-
return mathrand.New(mathrand.NewSource(time.Now().UnixNano())), nil
130+
return rand.New(rand.NewSource(time.Now().UnixNano())), nil
133131
}
134132
// Convert seed string to a 64-bit integer via SHA-256
135133
h := sha256.New()
@@ -138,5 +136,5 @@ func newRngFromSeed(seed string) (*mathrand.Rand, error) {
138136
}
139137
sum := h.Sum(nil)
140138
uSeed := binary.BigEndian.Uint64(sum)
141-
return mathrand.New(mathrand.NewSource(int64(uSeed))), nil
139+
return rand.New(rand.NewSource(int64(uSeed))), nil
142140
}

pkg/common/password_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func testGeneratorGeneratePasswordWithSeed(t *testing.T) {
3535
resultSeedFirstTime := ""
3636
resultSeedEachTime := ""
3737
for i := 0; i < N; i++ {
38-
res, err := GeneratePassword(10, 5, 0, seed, "")
38+
res, err := generatePassword(10, 5, 0, seed, "")
3939
if err != nil {
4040
t.Error(err)
4141
}
@@ -53,11 +53,11 @@ func testGeneratorGeneratePassword(t *testing.T) {
5353
t.Run("exceeds_length", func(t *testing.T) {
5454
t.Parallel()
5555

56-
if _, err := GeneratePassword(0, 1, 0, "", ""); err != password.ErrExceedsTotalLength {
56+
if _, err := generatePassword(0, 1, 0, "", ""); err != password.ErrExceedsTotalLength {
5757
t.Errorf("expected %q to be %q", err, password.ErrExceedsTotalLength)
5858
}
5959

60-
if _, err := GeneratePassword(0, 0, 1, "", ""); err != password.ErrExceedsTotalLength {
60+
if _, err := generatePassword(0, 0, 1, "", ""); err != password.ErrExceedsTotalLength {
6161
t.Errorf("expected %q to be %q", err, password.ErrExceedsTotalLength)
6262
}
6363
})
@@ -67,7 +67,7 @@ func testGeneratorGeneratePassword(t *testing.T) {
6767

6868
symbols := "!$_#"
6969
for i := 0; i < N; i++ {
70-
res, err := GeneratePassword(10, 0, 5, "", symbols)
70+
res, err := generatePassword(10, 0, 5, "", symbols)
7171
if err != nil {
7272
t.Error(err)
7373
}
@@ -89,7 +89,7 @@ func testGeneratorGeneratePassword(t *testing.T) {
8989
resultSeedEachTime := ""
9090
hasDiffPassword := false
9191
for i := 0; i < N; i++ {
92-
res, err := GeneratePassword(i%(len(password.LowerLetters)+len(password.UpperLetters)), 0, 0, "", "")
92+
res, err := generatePassword(i%(len(password.LowerLetters)+len(password.UpperLetters)), 0, 0, "", "")
9393
if err != nil {
9494
t.Error(err)
9595
}
@@ -148,11 +148,11 @@ func TestGeneratorEnsureMixedCase(t *testing.T) {
148148

149149
// Generate multiple passwords and check they have both upper and lower letters.
150150
for i := 0; i < 100; i++ {
151-
pwd, err := GeneratePassword(length, numDigits, numSymbols, seed, "")
151+
pwd, err := generatePassword(length, numDigits, numSymbols, seed, "")
152152
if err != nil {
153153
t.Fatalf("unexpected error generating password: %v", err)
154154
}
155-
pwd, err = EnsureMixedCase(pwd, seed)
155+
pwd, err = ensureMixedCase(pwd, seed)
156156
if err != nil {
157157
t.Fatalf("unexpected error Ensuring mixed-case password: %v", err)
158158
}
@@ -170,11 +170,11 @@ func TestGeneratorEnsureMixedCase(t *testing.T) {
170170

171171
var firstPwd string
172172
for i := 0; i < 50; i++ {
173-
pwd, err := GeneratePassword(length, numDigits, numSymbols, seed, "")
173+
pwd, err := generatePassword(length, numDigits, numSymbols, seed, "")
174174
if err != nil {
175175
t.Fatalf("unexpected error generating password with seed: %v", err)
176176
}
177-
pwd, err = EnsureMixedCase(pwd, seed)
177+
pwd, err = ensureMixedCase(pwd, seed)
178178
if err != nil {
179179
t.Fatalf("unexpected error Ensuring mixed-case password: %v", err)
180180
}

pkg/controller/component/workload_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func GetTemplateNameAndOrdinal(workloadName, podName string) (string, int32, err
231231
templateName = podSuffix[0:lastDashIndex]
232232
indexStr = podSuffix[lastDashIndex+1:]
233233
}
234-
index, err := strconv.Atoi(indexStr)
234+
index, err := strconv.ParseInt(indexStr, 10, 32)
235235
if err != nil {
236236
return "", 0, fmt.Errorf("failed to obtain pod ordinal")
237237
}

pkg/controller/sharding/legacy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func listShardingCompSpecs4Test(ctx context.Context, cli client.Reader,
132132
return nil, err
133133
}
134134

135-
compSpecList := make([]*appsv1.ClusterComponentSpec, 0, len(undeletedShardingComps)+len(deletingShardingComps))
135+
compSpecList := make([]*appsv1.ClusterComponentSpec, 0)
136136
shardTpl := sharding.Template
137137

138138
processComps := func(comps []appsv1.Component) error {

pkg/kbagent/service/reconfigure.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func checkReconfigureUpdated(req *proto.ActionRequest) error {
107107
}
108108

109109
func checkLocalFileExist(file string) (bool, error) {
110+
// #nosec G304
110111
_, err := os.Stat(file)
111112
if err != nil {
112113
if os.IsNotExist(err) {
@@ -118,6 +119,7 @@ func checkLocalFileExist(file string) (bool, error) {
118119
}
119120

120121
func checkLocalFileUpToDate(file, checksum string) error {
122+
// #nosec G304
121123
content, err := os.ReadFile(file)
122124
if err != nil {
123125
return err

pkg/operations/horizontal_scaling.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (hs horizontalScalingOpsHandler) restoreDataFromBackup(reqCtx intctrlutil.R
283283
for podName, templateName := range createdPodSet {
284284
idx := strings.LastIndex(podName, "-")
285285
podIndex := podName[idx+1:]
286-
podIndexInt, _ := strconv.Atoi(podIndex)
286+
podIndexInt, _ := strconv.ParseInt(podIndex, 10, 32)
287287
restoreMGR := plan.NewRestoreManager(reqCtx.Ctx, cli, opsRes.Cluster, model.GetScheme(), map[string]string{
288288
constant.OpsRequestNameLabelKey: opsRes.OpsRequest.Name,
289289
}, 1, int32(podIndexInt))

0 commit comments

Comments
 (0)