22package test
33
44import (
5+ "crypto/rand"
6+ "encoding/base64"
57 "fmt"
68 "log"
7- "math/rand "
9+ "math/big "
810 "os"
911 "strings"
1012 "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 {
@@ -104,6 +106,7 @@ func TestRunStandardSolutionSchematics(t *testing.T) {
104106 {Name : "service_credential_names" , Value : "{\" admin_test\" : \" Administrator\" , \" editor_test\" : \" Editor\" }" , DataType : "map(string)" },
105107 {Name : "existing_secrets_manager_instance_crn" , Value : permanentResources ["secretsManagerCRN" ], DataType : "string" },
106108 {Name : "service_credential_secrets" , Value : serviceCredentialSecrets , DataType : "list(object)" },
109+ {Name : "admin_pass" , Value : GetRandomAdminPassword (t ), DataType : "string" },
107110 {Name : "admin_pass_secrets_manager_secret_group" , Value : options .Prefix , DataType : "string" },
108111 {Name : "admin_pass_secrets_manager_secret_name" , Value : options .Prefix , DataType : "string" },
109112 {Name : "enable_kibana_dashboard" , Value : true , DataType : "bool" },
@@ -126,7 +129,7 @@ func TestRunStandardUpgradeSolution(t *testing.T) {
126129 CheckApplyResultForUpgrade : true ,
127130 })
128131
129- options .TerraformVars = map [string ]interface {} {
132+ options .TerraformVars = map [string ]any {
130133 "access_tags" : permanentResources ["accessTags" ],
131134 "existing_kms_instance_crn" : permanentResources ["hpcs_south_crn" ],
132135 "kms_endpoint_type" : "public" ,
@@ -150,7 +153,12 @@ func TestRunExistingInstance(t *testing.T) {
150153 prefix := fmt .Sprintf ("elastic-t-%s" , strings .ToLower (random .UniqueId ()))
151154 realTerraformDir := ".."
152155 tempTerraformDir , _ := files .CopyTerraformFolderToTemp (realTerraformDir , fmt .Sprintf (prefix + "-%s" , strings .ToLower (random .UniqueId ())))
153- region := validICDRegions [rand .Intn (len (validICDRegions ))]
156+
157+ index , err := rand .Int (rand .Reader , big .NewInt (int64 (len (validICDRegions ))))
158+ if err != nil {
159+ log .Fatalf ("Failed to generate a secure random index: %v" , err )
160+ }
161+ region := validICDRegions [index .Int64 ()]
154162
155163 // Verify ibmcloud_api_key variable is set
156164 checkVariable := "TF_VAR_ibmcloud_api_key"
@@ -161,7 +169,7 @@ func TestRunExistingInstance(t *testing.T) {
161169 logger .Log (t , "Tempdir: " , tempTerraformDir )
162170 existingTerraformOptions := terraform .WithDefaultRetryableErrors (t , & terraform.Options {
163171 TerraformDir : tempTerraformDir + "/examples/basic" ,
164- Vars : map [string ]interface {} {
172+ Vars : map [string ]any {
165173 "prefix" : prefix ,
166174 "region" : region ,
167175 "elasticsearch_version" : latestVersion ,
@@ -217,7 +225,6 @@ func TestRunExistingInstance(t *testing.T) {
217225 terraform .WorkspaceDelete (t , existingTerraformOptions , prefix )
218226 logger .Log (t , "END: Destroy (existing resources)" )
219227 }
220-
221228}
222229
223230// Test the DA when using IBM owned encryption keys
@@ -232,7 +239,7 @@ func TestRunStandardSolutionIBMKeys(t *testing.T) {
232239 ResourceGroup : resourceGroup ,
233240 })
234241
235- options .TerraformVars = map [string ]interface {} {
242+ options .TerraformVars = map [string ]any {
236243 "elasticsearch_version" : "8.12" ,
237244 "provider_visibility" : "public" ,
238245 "resource_group_name" : options .Prefix ,
@@ -243,3 +250,12 @@ func TestRunStandardSolutionIBMKeys(t *testing.T) {
243250 assert .Nil (t , err , "This should not have errored" )
244251 assert .NotNil (t , output , "Expected some output" )
245252}
253+
254+ func GetRandomAdminPassword (t * testing.T ) string {
255+ // Generate a 15 char long random string for the admin_pass
256+ randomBytes := make ([]byte , 13 )
257+ _ , randErr := rand .Read (randomBytes )
258+ require .Nil (t , randErr ) // do not proceed if we can't gen a random password
259+ randomPass := "A1" + base64 .URLEncoding .EncodeToString (randomBytes )[:13 ]
260+ return randomPass
261+ }
0 commit comments