diff --git a/solutions/standard/main.tf b/solutions/standard/main.tf index c755526b..4909ba26 100644 --- a/solutions/standard/main.tf +++ b/solutions/standard/main.tf @@ -235,10 +235,7 @@ locals { # if - replace first char with J # elseif _ replace first char with K # else use asis - generated_admin_password = startswith(random_password.admin_password[0].result, "-") ? "J${substr(random_password.admin_password[0].result, 1, -1)}" : startswith(random_password.admin_password[0].result, "_") ? "K${substr(random_password.admin_password[0].result, 1, -1)}" : random_password.admin_password[0].result - - # admin password to use - admin_pass = var.admin_pass == null ? local.generated_admin_password : var.admin_pass + admin_pass = var.admin_pass == null ? (startswith(random_password.admin_password[0].result, "-") ? "J${substr(random_password.admin_password[0].result, 1, -1)}" : startswith(random_password.admin_password[0].result, "_") ? "K${substr(random_password.admin_password[0].result, 1, -1)}" : random_password.admin_password[0].result) : var.admin_pass } ####################################################################################################################### diff --git a/tests/pr_test.go b/tests/pr_test.go index 948b4ff5..b9e9059d 100644 --- a/tests/pr_test.go +++ b/tests/pr_test.go @@ -2,9 +2,11 @@ package test import ( + "crypto/rand" + "encoding/base64" "fmt" "log" - "math/rand" + "math/big" "os" "strings" "testing" @@ -35,7 +37,7 @@ const regionSelectionPath = "../common-dev-assets/common-go-assets/icd-region-pr // Define a struct with fields that match the structure of the YAML data const yamlLocation = "../common-dev-assets/common-go-assets/common-permanent-resources.yaml" -var permanentResources map[string]interface{} +var permanentResources map[string]any var sharedInfoSvc *cloudinfo.CloudInfoService var validICDRegions = []string{ @@ -76,7 +78,7 @@ func TestRunStandardSolutionSchematics(t *testing.T) { WaitJobCompleteMinutes: 60, }) - serviceCredentialSecrets := []map[string]interface{}{ + serviceCredentialSecrets := []map[string]any{ { "secret_group_name": fmt.Sprintf("%s-secret-group", options.Prefix), "service_credentials": []map[string]string{ @@ -104,6 +106,7 @@ func TestRunStandardSolutionSchematics(t *testing.T) { {Name: "service_credential_names", Value: "{\"admin_test\": \"Administrator\", \"editor_test\": \"Editor\"}", DataType: "map(string)"}, {Name: "existing_secrets_manager_instance_crn", Value: permanentResources["secretsManagerCRN"], DataType: "string"}, {Name: "service_credential_secrets", Value: serviceCredentialSecrets, DataType: "list(object)"}, + {Name: "admin_pass", Value: GetRandomAdminPassword(t), DataType: "string"}, {Name: "admin_pass_secrets_manager_secret_group", Value: options.Prefix, DataType: "string"}, {Name: "admin_pass_secrets_manager_secret_name", Value: options.Prefix, DataType: "string"}, {Name: "enable_kibana_dashboard", Value: true, DataType: "bool"}, @@ -126,7 +129,7 @@ func TestRunStandardUpgradeSolution(t *testing.T) { CheckApplyResultForUpgrade: true, }) - options.TerraformVars = map[string]interface{}{ + options.TerraformVars = map[string]any{ "access_tags": permanentResources["accessTags"], "existing_kms_instance_crn": permanentResources["hpcs_south_crn"], "kms_endpoint_type": "public", @@ -150,7 +153,12 @@ func TestRunExistingInstance(t *testing.T) { prefix := fmt.Sprintf("elastic-t-%s", strings.ToLower(random.UniqueId())) realTerraformDir := ".." tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId()))) - region := validICDRegions[rand.Intn(len(validICDRegions))] + + index, err := rand.Int(rand.Reader, big.NewInt(int64(len(validICDRegions)))) + if err != nil { + log.Fatalf("Failed to generate a secure random index: %v", err) + } + region := validICDRegions[index.Int64()] // Verify ibmcloud_api_key variable is set checkVariable := "TF_VAR_ibmcloud_api_key" @@ -161,7 +169,7 @@ func TestRunExistingInstance(t *testing.T) { logger.Log(t, "Tempdir: ", tempTerraformDir) existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ TerraformDir: tempTerraformDir + "/examples/basic", - Vars: map[string]interface{}{ + Vars: map[string]any{ "prefix": prefix, "region": region, "elasticsearch_version": latestVersion, @@ -217,7 +225,6 @@ func TestRunExistingInstance(t *testing.T) { terraform.WorkspaceDelete(t, existingTerraformOptions, prefix) logger.Log(t, "END: Destroy (existing resources)") } - } // Test the DA when using IBM owned encryption keys @@ -232,7 +239,7 @@ func TestRunStandardSolutionIBMKeys(t *testing.T) { ResourceGroup: resourceGroup, }) - options.TerraformVars = map[string]interface{}{ + options.TerraformVars = map[string]any{ "elasticsearch_version": "8.12", "provider_visibility": "public", "resource_group_name": options.Prefix, @@ -243,3 +250,12 @@ func TestRunStandardSolutionIBMKeys(t *testing.T) { assert.Nil(t, err, "This should not have errored") assert.NotNil(t, output, "Expected some output") } + +func GetRandomAdminPassword(t *testing.T) string { + // Generate a 15 char long random string for the admin_pass + randomBytes := make([]byte, 13) + _, randErr := rand.Read(randomBytes) + require.Nil(t, randErr) // do not proceed if we can't gen a random password + randomPass := "A1" + base64.URLEncoding.EncodeToString(randomBytes)[:13] + return randomPass +}