refactor : ICU-18284-Unify SecureRandomReader (#6303)
* Unify SecureRandomReader * vault changes updated * target changes added * reviwed changes updated
This commit is contained in:
1 parent
11fc97622f
commit
c4dc2a43cc
23 files changed
+184
-46
No files matched your search
@@ -5,6 +5,8 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
@@ -66,6 +68,7 @@ type options struct {
|
||||
withFilterWorkersByLocalStorageState bool
|
||||
WithReader db.Reader
|
||||
WithWriter db.Writer
|
||||
withRandomReader io.Reader
|
||||
}
|
||||
|
||||
func getDefaultOptions() options {
|
||||
@@ -73,6 +76,7 @@ func getDefaultOptions() options {
|
||||
withNewIdFunc: newWorkerId,
|
||||
withOperationalState: ActiveOperationalState.String(),
|
||||
withLocalStorageState: UnknownWorkerType.String(),
|
||||
withRandomReader: rand.Reader,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,3 +319,10 @@ func WithReaderWriter(r db.Reader, w db.Writer) Option {
|
||||
o.WithWriter = w
|
||||
}
|
||||
}
|
||||
|
||||
// WithRandomReader provides an option to specify a random reader.
|
||||
func WithRandomReader(reader io.Reader) Option {
|
||||
return func(o *options) {
|
||||
o.withRandomReader = reader
|
||||
}
|
||||
}
|
||||
@@ -71,10 +71,13 @@ func (w WorkerList) SupportsFeature(f version.Feature) WorkerList {
|
||||
// Shuffle returns a randomly-shuffled copy of the caller's Workers (using
|
||||
// crypto/rand). If the caller's WorkerList has one element or less, this
|
||||
// function is a no-op.
|
||||
func (w WorkerList) Shuffle() (WorkerList, error) {
|
||||
// Supported options:
|
||||
// - WithRandomReader
|
||||
func (w WorkerList) Shuffle(opt ...Option) (WorkerList, error) {
|
||||
if len(w) <= 1 {
|
||||
return w, nil
|
||||
}
|
||||
opts := GetOpts(opt...)
|
||||
|
||||
ret := make(WorkerList, len(w))
|
||||
copy(ret, w)
|
||||
@@ -83,7 +86,7 @@ func (w WorkerList) Shuffle() (WorkerList, error) {
|
||||
// math/rand.Shuffle, but using the crypto/rand package instead. The same
|
||||
// caveats as math/rand.Shuffle apply.
|
||||
for i := len(ret) - 1; i > 0; i-- {
|
||||
j, err := rand.Int(rand.Reader, big.NewInt(int64(i+1)))
|
||||
j, err := rand.Int(opts.withRandomReader, big.NewInt(int64(i+1)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user