22package test
33
44import (
5+ "crypto/rand"
56 "fmt"
67 "log"
8+ "math/big"
79 "os"
810 "strings"
911 "testing"
1012
11- "math/rand/v2"
12-
1313 "github.com/gruntwork-io/terratest/modules/files"
1414 "github.com/gruntwork-io/terratest/modules/logger"
1515 "github.com/gruntwork-io/terratest/modules/random"
@@ -61,6 +61,15 @@ func TestMain(m *testing.M) {
6161 os .Exit (m .Run ())
6262}
6363
64+ // secureRandomInt generates a cryptographically secure random integer in the range [0, max)
65+ func secureRandomInt (max int ) (int , error ) {
66+ nBig , err := rand .Int (rand .Reader , big .NewInt (int64 (max )))
67+ if err != nil {
68+ return 0 , err
69+ }
70+ return int (nBig .Int64 ()), nil
71+ }
72+
6473func setupOptions (t * testing.T , prefix string , terraformDir string ) * testhelper.TestOptions {
6574
6675 options := testhelper .TestOptionsDefaultWithVars (& testhelper.TestOptions {
@@ -81,7 +90,9 @@ func setupOptions(t *testing.T, prefix string, terraformDir string) *testhelper.
8190func TestFullyConfigurableSolution (t * testing.T ) {
8291 t .Parallel ()
8392
84- var region = validRegions [rand .IntN (len (validRegions ))]
93+ randomIndex , err := secureRandomInt (len (validRegions ))
94+ require .NoError (t , err , "Failed to generate random index" )
95+ var region = validRegions [randomIndex ]
8596
8697 // ------------------------------------------------------------------------------------------------------
8798 // Deploy OCP Cluster and Logs instance since it is needed to deploy Logs Agent
@@ -167,7 +178,9 @@ func TestFullyConfigurableSolution(t *testing.T) {
167178func TestFullyConfigurableUpgradeSolution (t * testing.T ) {
168179 t .Parallel ()
169180
170- var region = validRegions [rand .IntN (len (validRegions ))]
181+ randomIndex , err := secureRandomInt (len (validRegions ))
182+ require .NoError (t , err , "Failed to generate random index" )
183+ var region = validRegions [randomIndex ]
171184
172185 // ------------------------------------------------------------------------------------------------------
173186 // Deploy OCP Cluster and Observability instances since it is needed to deploy Logs Agent
0 commit comments