44package dependency
55
66import (
7- cryptorand "crypto/rand"
87 "encoding/json"
98 "fmt"
109 "log"
11- "math/big "
10+ "math/rand "
1211 "sync"
1312 "time"
1413
@@ -186,15 +185,15 @@ func leaseCheckWait(s *Secret, retryCount int) time.Duration {
186185 sleep = sleep / 3.0
187186
188187 // Use some randomness so many clients do not hit Vault simultaneously.
189- sleep = sleep * (secureRandomFloat64 () + 1 ) / 2.0
188+ sleep = sleep * (rand . Float64 () + 1 ) / 2.0
190189 } else if ! rotatingSecret {
191190 // If the secret doesn't have a rotation period, this is a non-renewable leased
192191 // secret.
193192 // For non-renewable leases set the renew duration to use much of the secret
194193 // lease as possible. Use a stagger over the configured threshold
195194 // fraction of the lease duration so that many clients do not hit
196195 // Vault simultaneously.
197- finalFraction := VaultLeaseRenewalThreshold + (secureRandomFloat64 ()- 0.5 )* 0.1
196+ finalFraction := VaultLeaseRenewalThreshold + (rand . Float64 ()- 0.5 )* 0.1
198197 if finalFraction >= 1.0 || finalFraction <= 0.0 {
199198 // If the fraction randomly winds up outside of (0.0-1.0), clamp
200199 // back down to the VaultLeaseRenewalThreshold provided by the user,
@@ -209,25 +208,10 @@ func leaseCheckWait(s *Secret, retryCount int) time.Duration {
209208 return time .Duration (sleep )
210209}
211210
212- // secureRandomFloat64 generates a cryptographically secure random float64 in [0.0, 1.0).
213- // This is thread-safe and used to fix CWE-338.
214- func secureRandomFloat64 () float64 {
215- max := big .NewInt (1000000 )
216- n , err := cryptorand .Int (cryptorand .Reader , max )
217- if err != nil {
218- // Fallback to 0.5 if crypto/rand fails (rare but possible)
219- log .Printf ("[WARN] Failed to generate secure random number: %v" , err )
220- return 0.5
221- }
222- return float64 (n .Int64 ()) / 1000000.0
223- }
224-
225211// jitter adds randomness to a duration to prevent thundering herd.
226- // It reduces the duration by up to maxJitter (10%) randomly using crypto/rand.
227- // This function is thread-safe.
212+ // It reduces the duration by up to maxJitter (10%) randomly.
228213func jitter (t time.Duration ) time.Duration {
229- randomFloat := secureRandomFloat64 ()
230- f := float64 (t ) * (1.0 - maxJitter * randomFloat )
214+ f := float64 (t ) * (1.0 - maxJitter * rand .Float64 ())
231215 return time .Duration (f )
232216}
233217
0 commit comments