Skip to content

Commit

Permalink
Merge pull request #3531 from wy65701436/fix-rand
Browse files Browse the repository at this point in the history
[release/2.7]fix go check issues
  • Loading branch information
milosgajdos committed Nov 17, 2021
2 parents 691e62e + 9a3ff11 commit 4313c14
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
12 changes: 10 additions & 2 deletions contrib/token-server/main.go
Expand Up @@ -2,9 +2,10 @@ package main

import (
"context"
"crypto/rand"
"encoding/json"
"flag"
"math/rand"
"math/big"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -141,8 +142,15 @@ const refreshTokenLength = 15

func newRefreshToken() string {
s := make([]rune, refreshTokenLength)
max := int64(len(refreshCharacters))
for i := range s {
s[i] = refreshCharacters[rand.Intn(len(refreshCharacters))]
randInt, err := rand.Int(rand.Reader, big.NewInt(max))
// let '0' serves the failure case
if err != nil {
logrus.Infof("Error on making refersh token: %v", err)
randInt = big.NewInt(0)
}
s[i] = refreshCharacters[randInt.Int64()]
}
return string(s)
}
Expand Down
16 changes: 11 additions & 5 deletions registry/handlers/app.go
Expand Up @@ -2,10 +2,11 @@ package handlers

import (
"context"
cryptorand "crypto/rand"
"crypto/rand"
"expvar"
"fmt"
"math/rand"
"math"
"math/big"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -610,7 +611,7 @@ func (app *App) configureLogHook(configuration *configuration.Configuration) {
func (app *App) configureSecret(configuration *configuration.Configuration) {
if configuration.HTTP.Secret == "" {
var secretBytes [randomSecretSize]byte
if _, err := cryptorand.Read(secretBytes[:]); err != nil {
if _, err := rand.Read(secretBytes[:]); err != nil {
panic(fmt.Sprintf("could not generate random bytes for HTTP secret: %v", err))
}
configuration.HTTP.Secret = string(secretBytes[:])
Expand Down Expand Up @@ -1060,8 +1061,13 @@ func startUploadPurger(ctx context.Context, storageDriver storagedriver.StorageD
}

go func() {
rand.Seed(time.Now().Unix())
jitter := time.Duration(rand.Int()%60) * time.Minute
randInt, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64))
if err != nil {
log.Infof("Failed to generate random jitter: %v", err)
// sleep 30min for failure case
randInt = big.NewInt(30)
}
jitter := time.Duration(randInt.Int64()%60) * time.Minute
log.Infof("Starting upload purge in %s", jitter)
time.Sleep(jitter)

Expand Down

0 comments on commit 4313c14

Please sign in to comment.