Skip to content

Commit

Permalink
chore: Enable G404 rule for gosec (influxdata#13095)
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel authored May 2, 2023
1 parent 1f57283 commit 30b6036
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 29 deletions.
9 changes: 5 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ linters-settings:
- G306
- G401
- G403
- G404
- G501
- G502
- G503
Expand Down Expand Up @@ -257,13 +258,13 @@ issues:
- govet

- path: cmd/telegraf/(main|printer).go
text: "Error return value of `outputBuffer.Write` is not checked"
text: "Error return value of `outputBuffer.Write` is not checked" #errcheck

- path: cmd/telegraf/(main|printer).go
text: "unhandled-error: Unhandled error in call to function outputBuffer.Write"
- path: _test\.go
text: "Potential hardcoded credentials" #gosec:G101

- path: _test\.go
text: "Potential hardcoded credentials"
text: "Use of weak random number generator" #gosec:G404

# Independently of option `exclude` we use default exclude patterns,
# it can be disabled by this option.
Expand Down
14 changes: 5 additions & 9 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,14 @@ func SnakeCase(in string) string {
}

// RandomSleep will sleep for a random amount of time up to max.
// If the shutdown channel is closed, it will return before it has finished
// sleeping.
// If the shutdown channel is closed, it will return before it has finished sleeping.
func RandomSleep(max time.Duration, shutdown chan struct{}) {
if max == 0 {
sleepDuration := RandomDuration(max)
if sleepDuration == 0 {
return
}

sleepns := rand.Int63n(max.Nanoseconds())

t := time.NewTimer(time.Nanosecond * time.Duration(sleepns))
t := time.NewTimer(time.Nanosecond * sleepDuration)
select {
case <-t.C:
return
Expand All @@ -143,9 +141,7 @@ func RandomDuration(max time.Duration) time.Duration {
return 0
}

sleepns := rand.Int63n(max.Nanoseconds())

return time.Duration(sleepns)
return time.Duration(rand.Int63n(max.Nanoseconds())) //nolint:gosec // G404: not security critical
}

// SleepContext sleeps until the context is closed or the duration is reached.
Expand Down
21 changes: 14 additions & 7 deletions plugins/inputs/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
package example

import (
"crypto/rand"
_ "embed"
"fmt"
"math/rand"
"math"
"math/big"
"time"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -81,14 +83,14 @@ func (m *Example) Init() error {

// Gather defines what data the plugin will gather.
func (m *Example) Gather(acc telegraf.Accumulator) error {
// Imagine some completely arbitrary error occuring here
// Imagine some completely arbitrary error occurring here
if m.NumberFields > 10 {
return fmt.Errorf("too many fields")
}

// For illustration we gather three metrics in one go
// For illustration, we gather three metrics in one go
for run := 0; run < 3; run++ {
// Imagine an error occurs here but you want to keep the other
// Imagine an error occurs here, but you want to keep the other
// metrics, then you cannot simply return, as this would drop
// all later metrics. Simply accumulate errors in this case
// and ignore the metric.
Expand All @@ -101,11 +103,16 @@ func (m *Example) Gather(acc telegraf.Accumulator) error {
fields := map[string]interface{}{"count": m.count}
for i := int64(1); i < m.NumberFields; i++ {
name := fmt.Sprintf("field%d", i)
value := 0.0
var err error
value := big.NewInt(0)
if m.EnableRandomVariable {
value = rand.Float64()
value, err = rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
if err != nil {
acc.AddError(err)
continue
}
}
fields[name] = value
fields[name] = float64(value.Int64())
}

// Construct the tags
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (*Mock) SampleConfig() string {
}

func (m *Mock) Init() error {
m.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
m.rand = rand.New(rand.NewSource(time.Now().UnixNano())) //nolint:gosec // G404: not security critical
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/powerdns_recursor/protocol_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package powerdns_recursor

import (
"fmt"
"math/rand"
"net"
"os"
"path/filepath"
"time"

"github.com/google/uuid"

"github.com/influxdata/telegraf"
)

Expand All @@ -19,8 +20,7 @@ import (
// The `data` field contains a list of commands to execute with
// the \n character after every command.
func (p *PowerdnsRecursor) gatherFromV1Server(address string, acc telegraf.Accumulator) error {
randomNumber := rand.Int63()
recvSocket := filepath.Join(p.SocketDir, fmt.Sprintf("pdns_recursor_telegraf%d", randomNumber))
recvSocket := filepath.Join(p.SocketDir, fmt.Sprintf("pdns_recursor_telegraf%s", uuid.New().String()))

laddr, err := net.ResolveUnixAddr("unixgram", recvSocket)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/powerdns_recursor/protocol_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package powerdns_recursor

import (
"fmt"
"math/rand"
"net"
"os"
"path/filepath"
"time"

"github.com/google/uuid"

"github.com/influxdata/telegraf"
)

Expand All @@ -17,8 +18,7 @@ import (
// Datagram 1 => status: uint32
// Datagram 2 => data: byte[] (max 16_384 bytes)
func (p *PowerdnsRecursor) gatherFromV2Server(address string, acc telegraf.Accumulator) error {
randomNumber := rand.Int63()
recvSocket := filepath.Join(p.SocketDir, fmt.Sprintf("pdns_recursor_telegraf%d", randomNumber))
recvSocket := filepath.Join(p.SocketDir, fmt.Sprintf("pdns_recursor_telegraf%s", uuid.New().String()))

laddr, err := net.ResolveUnixAddr("unixgram", recvSocket)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/statsd/running_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (rs *RunningStats) AddValue(v float64) {
rs.perc = append(rs.perc, v)
} else {
// Reached limit, choose random index to overwrite in the percentile array
rs.perc[rand.Intn(len(rs.perc))] = v
rs.perc[rand.Intn(len(rs.perc))] = v //nolint:gosec // G404: not security critical
}

if len(rs.med) < rs.MedLimit {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/vsphere/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func (e *Endpoint) complexMetadataSelect(ctx context.Context, res *resourceKind,
if n > maxMetadataSamples {
// Shuffle samples into the maxMetadataSamples positions
for i := 0; i < maxMetadataSamples; i++ {
j := int(rand.Int31n(int32(i + 1)))
j := int(rand.Int31n(int32(i + 1))) //nolint:gosec // G404: not security critical
t := sampledObjects[i]
sampledObjects[i] = sampledObjects[j]
sampledObjects[j] = t
Expand Down

0 comments on commit 30b6036

Please sign in to comment.