22package test
33
44import (
5+ "crypto/rand"
6+ "encoding/base64"
57 "encoding/json"
68 "fmt"
79 "log"
8- "math/rand "
10+ "math/big "
911 "os"
1012 "strings"
1113 "testing"
@@ -35,7 +37,7 @@ const regionSelectionPath = "../common-dev-assets/common-go-assets/icd-region-pr
3537// Define a struct with fields that match the structure of the YAML data
3638const yamlLocation = "../common-dev-assets/common-go-assets/common-permanent-resources.yaml"
3739
38- var permanentResources map [string ]interface {}
40+ var permanentResources map [string ]any
3941
4042var sharedInfoSvc * cloudinfo.CloudInfoService
4143var validICDRegions = []string {
@@ -76,7 +78,7 @@ func TestRunStandardSolutionSchematics(t *testing.T) {
7678 WaitJobCompleteMinutes : 60 ,
7779 })
7880
79- serviceCredentialSecrets := []map [string ]interface {} {
81+ serviceCredentialSecrets := []map [string ]any {
8082 {
8183 "secret_group_name" : fmt .Sprintf ("%s-secret-group" , options .Prefix ),
8284 "service_credentials" : []map [string ]string {
@@ -114,6 +116,7 @@ func TestRunStandardSolutionSchematics(t *testing.T) {
114116 {Name : "service_credential_names" , Value : string (serviceCredentialNamesJSON ), DataType : "map(string)" },
115117 {Name : "existing_secrets_manager_instance_crn" , Value : permanentResources ["secretsManagerCRN" ], DataType : "string" },
116118 {Name : "service_credential_secrets" , Value : serviceCredentialSecrets , DataType : "list(object)" },
119+ {Name : "admin_pass" , Value : GetRandomAdminPassword (t ), DataType : "string" },
117120 {Name : "admin_pass_secrets_manager_secret_group" , Value : options .Prefix , DataType : "string" },
118121 {Name : "admin_pass_secrets_manager_secret_name" , Value : options .Prefix , DataType : "string" },
119122 }
@@ -132,7 +135,7 @@ func TestRunStandardUpgradeSolution(t *testing.T) {
132135 ResourceGroup : resourceGroup ,
133136 })
134137
135- options .TerraformVars = map [string ]interface {} {
138+ options .TerraformVars = map [string ]any {
136139 "access_tags" : permanentResources ["accessTags" ],
137140 "existing_kms_instance_crn" : permanentResources ["hpcs_south_crn" ],
138141 "kms_endpoint_type" : "public" ,
@@ -158,7 +161,7 @@ func TestPlanValidation(t *testing.T) {
158161 options .TestSetup ()
159162 options .TerraformOptions .NoColor = true
160163 options .TerraformOptions .Logger = logger .Discard
161- options .TerraformOptions .Vars = map [string ]interface {} {
164+ options .TerraformOptions .Vars = map [string ]any {
162165 "prefix" : options .Prefix ,
163166 "region" : "us-south" ,
164167 "rabbitmq_version" : "3.13" ,
@@ -167,18 +170,18 @@ func TestPlanValidation(t *testing.T) {
167170 }
168171
169172 // Test the DA when using an existing KMS instance
170- var standardSolutionWithExistingKms = map [string ]interface {} {
173+ var standardSolutionWithExistingKms = map [string ]any {
171174 "access_tags" : permanentResources ["accessTags" ],
172175 "existing_kms_instance_crn" : permanentResources ["hpcs_south_crn" ],
173176 }
174177
175178 // Test the DA when using IBM owned encryption key
176- var standardSolutionWithUseIbmOwnedEncKey = map [string ]interface {} {
179+ var standardSolutionWithUseIbmOwnedEncKey = map [string ]any {
177180 "use_ibm_owned_encryption_key" : true ,
178181 }
179182
180183 // Create a map of the variables
181- tfVarsMap := map [string ]map [string ]interface {} {
184+ tfVarsMap := map [string ]map [string ]any {
182185 "standardSolutionWithExistingKms" : standardSolutionWithExistingKms ,
183186 "standardSolutionWithUseIbmOwnedEncKey" : standardSolutionWithUseIbmOwnedEncKey ,
184187 }
@@ -209,7 +212,12 @@ func TestRunExistingInstance(t *testing.T) {
209212 prefix := fmt .Sprintf ("rabbitmq-t-%s" , strings .ToLower (random .UniqueId ()))
210213 realTerraformDir := ".."
211214 tempTerraformDir , _ := files .CopyTerraformFolderToTemp (realTerraformDir , fmt .Sprintf (prefix + "-%s" , strings .ToLower (random .UniqueId ())))
212- region := validICDRegions [rand .Intn (len (validICDRegions ))]
215+
216+ index , err := rand .Int (rand .Reader , big .NewInt (int64 (len (validICDRegions ))))
217+ if err != nil {
218+ log .Fatalf ("Failed to generate a secure random index: %v" , err )
219+ }
220+ region := validICDRegions [index .Int64 ()]
213221
214222 // Verify ibmcloud_api_key variable is set
215223 checkVariable := "TF_VAR_ibmcloud_api_key"
@@ -220,7 +228,7 @@ func TestRunExistingInstance(t *testing.T) {
220228 logger .Log (t , "Tempdir: " , tempTerraformDir )
221229 existingTerraformOptions := terraform .WithDefaultRetryableErrors (t , & terraform.Options {
222230 TerraformDir : tempTerraformDir + "/examples/basic" ,
223- Vars : map [string ]interface {} {
231+ Vars : map [string ]any {
224232 "prefix" : prefix ,
225233 "region" : region ,
226234 "rabbitmq_version" : latestVersion ,
@@ -276,5 +284,13 @@ func TestRunExistingInstance(t *testing.T) {
276284 terraform .WorkspaceDelete (t , existingTerraformOptions , prefix )
277285 logger .Log (t , "END: Destroy (existing resources)" )
278286 }
287+ }
279288
289+ func GetRandomAdminPassword (t * testing.T ) string {
290+ // Generate a 15 char long random string for the admin_pass
291+ randomBytes := make ([]byte , 13 )
292+ _ , randErr := rand .Read (randomBytes )
293+ require .Nil (t , randErr ) // do not proceed if we can't gen a random password
294+ randomPass := "A1" + base64 .URLEncoding .EncodeToString (randomBytes )[:13 ]
295+ return randomPass
280296}
0 commit comments