Skip to content

Commit ba95b54

Browse files
committed
add gosec action + fix gosec issues
1 parent 7ba8483 commit ba95b54

File tree

9 files changed

+60
-20
lines changed

9 files changed

+60
-20
lines changed

.github/workflows/gosec.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: gosec
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- master
8+
- '**'
9+
pull_request:
10+
workflow_dispatch:
11+
jobs:
12+
tests:
13+
runs-on: ubuntu-latest
14+
env:
15+
GO111MODULE: on
16+
steps:
17+
- name: Checkout Source
18+
uses: actions/checkout@v2
19+
- name: Run Gosec Security Scanner
20+
uses: securego/gosec@master
21+
with:
22+
args: ./...

config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ func defaults() (c *config) {
300300
discoveryInterval: DefaultDiscoveryInterval,
301301
balancingConfig: DefaultBalancer,
302302
tlsConfig: &tls.Config{
303-
RootCAs: certPool,
303+
MinVersion: tls.VersionTLS12,
304+
RootCAs: certPool,
304305
},
305306
}
306307
}

internal/driver/cluster/balancer/balancer.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package balancer
22

33
import (
44
"errors"
5-
"math/rand"
6-
"time"
75

86
"github.com/ydb-platform/ydb-go-sdk/v3/config"
97
"github.com/ydb-platform/ydb-go-sdk/v3/internal/driver/cluster/balancer/conn"
@@ -48,19 +46,15 @@ type Balancer interface {
4846
}
4947

5048
func defaultBalancer() Balancer {
51-
return &randomChoice{
52-
r: rand.New(rand.NewSource(time.Now().UnixNano())),
53-
}
49+
return &randomChoice{}
5450
}
5551

5652
func newBalancer(cfg config.BalancerConfig) Balancer {
5753
switch cfg.Algorithm {
5854
case config.BalancingAlgorithmRoundRobin:
5955
return &roundRobin{}
6056
case config.BalancingAlgorithmRandomChoice:
61-
return &randomChoice{
62-
r: rand.New(rand.NewSource(time.Now().UnixNano())),
63-
}
57+
return &randomChoice{}
6458
default:
6559
return defaultBalancer()
6660
}

internal/driver/cluster/balancer/rr.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package balancer
33
import (
44
"container/heap"
55
"math"
6-
"math/rand"
76
"sync"
87
"sync/atomic"
98

109
"github.com/ydb-platform/ydb-go-sdk/v3/internal/driver/cluster/balancer/conn"
1110
"github.com/ydb-platform/ydb-go-sdk/v3/internal/driver/cluster/balancer/conn/info"
1211
"github.com/ydb-platform/ydb-go-sdk/v3/internal/driver/cluster/balancer/conn/list"
1312
"github.com/ydb-platform/ydb-go-sdk/v3/internal/driver/cluster/balancer/state"
13+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/rand"
1414
)
1515

1616
// roundRobin is an implementation of weighted round-robin balancing algorithm.
@@ -28,7 +28,6 @@ type roundRobin struct {
2828

2929
type randomChoice struct {
3030
roundRobin
31-
r *rand.Rand // without seed by default
3231
m sync.Mutex
3332
}
3433

@@ -46,7 +45,7 @@ func (r *randomChoice) Next() conn.Conn {
4645
return nil
4746
}
4847
r.m.Lock()
49-
i := r.belt[r.r.Intn(len(r.belt))]
48+
i := r.belt[rand.Int(len(r.belt))]
5049
r.m.Unlock()
5150
return r.conns[i].Conn
5251
}

internal/rand/rand.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package rand
2+
3+
import (
4+
"crypto/rand"
5+
"math/big"
6+
)
7+
8+
func int64n(max int64) int64 {
9+
n, err := rand.Int(rand.Reader, big.NewInt(max))
10+
if err != nil {
11+
panic(err) // err on negative max
12+
}
13+
return n.Int64()
14+
}
15+
16+
func Int64(max int64) int64 {
17+
return int64n(max)
18+
}
19+
20+
func Int(max int) int {
21+
return int(int64n(int64(max)))
22+
}

options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func WithCertificatesFromFile(caFile string) Option {
181181
}
182182
caFile = filepath.Join(home, caFile[1:])
183183
}
184-
bytes, err := os.ReadFile(caFile)
184+
bytes, err := os.ReadFile(filepath.Clean(caFile))
185185
if err != nil {
186186
return err
187187
}

retry/retry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package retry
33
import (
44
"context"
55
"math"
6-
"math/rand"
76
"time"
87

98
"github.com/ydb-platform/ydb-go-sdk/v3/internal/errors"
9+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/rand"
1010
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
1111
)
1212

@@ -155,7 +155,7 @@ func (b logBackoff) delay(i int) time.Duration {
155155
if f == d {
156156
return f
157157
}
158-
return f + time.Duration(rand.Intn(int(d-f)+1))
158+
return f + time.Duration(rand.Int64(int64(d-f)+1))
159159
}
160160

161161
func min(a, b uint) uint {

sugar/sugar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"path"
77
"strings"
88

9-
"github.com/ydb-platform/ydb-go-sdk/v3"
9+
ydb "github.com/ydb-platform/ydb-go-sdk/v3"
1010
"github.com/ydb-platform/ydb-go-sdk/v3/internal/errors"
1111
"github.com/ydb-platform/ydb-go-sdk/v3/scheme"
1212
"github.com/ydb-platform/ydb-go-sdk/v3/table"

testutil/session.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package testutil
22

33
import (
44
"fmt"
5-
"math/rand"
5+
"math"
66
"strconv"
7+
8+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/rand"
79
)
810

911
type (
@@ -23,9 +25,9 @@ func WithServiceID(serviceID uint32) sessionIDOption {
2325

2426
func SessionID(opts ...sessionIDOption) string {
2527
h := &sessionIDHolder{
26-
serviceID: rand.Uint32(),
27-
nodeID: rand.Uint32(),
28-
hash: strconv.FormatUint(rand.Uint64(), 16),
28+
serviceID: uint32(rand.Int64(math.MaxUint32)),
29+
nodeID: uint32(rand.Int64(math.MaxUint32)),
30+
hash: strconv.FormatInt(rand.Int64(math.MaxInt64), 16),
2931
}
3032
for _, o := range opts {
3133
o(h)

0 commit comments

Comments
 (0)