DataDog / terraform-provider-datadog

Terraform Datadog provider
https://www.terraform.io/docs/providers/datadog/
Mozilla Public License 2.0
399 stars 375 forks source link

Issues with Javascript browser variable and related extractFromJavascript browser step #2452

Open fabrideci opened 3 months ago

fabrideci commented 3 months ago

Datadog Terraform Provider Version

v3.39.0

Terraform Version

v1.8.3

What resources or data sources are affected?

datadog_synthetics_test

Terraform Configuration Files

browser_variable {
  type    = "javascript"
  name    = "PWD_EXAMPLE"
}

browser_variable {
  type    = "javascript"
  name    = "PWD_EXAMPLE_2"
  secure  = true // applicable only for variables of type "text"
}

browser_step {
  name = "Create new random password"
  type = "extractFromJavascript"
  params {
    code = <<EOT
const minLength = 8;
const lowerCaseChars = 'abcdefghijklmnopqrstuvwxyz';
const upperCaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numericChars = '0123456789';
const specialChars = '$!@#$%^&*)(+=._-';
const allChars = lowerCaseChars + upperCaseChars + numericChars + specialChars;

function getRandomCharFromString(str) {
  return str.charAt(Math.floor(Math.random() * str.length));
}

let randomString = getRandomCharFromString(lowerCaseChars)
  + getRandomCharFromString(upperCaseChars)
  + getRandomCharFromString(numericChars)
  + getRandomCharFromString(specialChars);

while (randomString.length < minLength) {
  randomString += getRandomCharFromString(allChars);
}

randomString = randomString.split('').sort(() => 0.5 - Math.random()).join('');

return randomString;
EOT
    variable {
      name = "PWD_EXAMPLE_3"
    }
  }
}

Relevant debug or panic output

No response

Expected Behavior

In a browser test, I should have the capability to declare a JavaScript variable and utilize the corresponding extractFromJavaScript step to assign it a value.

Actual Behavior

When attempting to set a simple JavaScript browser_variable in Terraform, such as PWD_EXAMPLE, an error occurs even if the 'Create new random password’ step points to PWD_EXAMPLE instead of PWD_EXAMPLE_3, as shown in the previous code example. The error message is as follows:

Error: error updating synthetics browser test from /api/v1/synthetics/tests/browser/8ss-wcy-akr: 400 Bad Request: {"errors":["'variables' value '{'example': '', 'name': 'PWD_EXAMPLE', 'pattern': '', 'type': 'javascript'}' is invalid"]}"

To circumvent this, I bypassed the browser variable declaration and directly used a browser_step of type extractFromJavascript, specifying only the destination variable. This approach successfully set the variable PWD_EXAMPLE_3.

However, I encountered an issue with obfuscating this value. The secure attribute for browser_variable, which enables obfuscation, is only applicable to variables of type text. It appears that pre-declaring a browser_variable of type javascript is not supported. While obfuscation is achievable through the Datadog UI, enabling this feature and running terraform apply results in persistent errors, even after disabling the option. The only solution is to delete the test from the UI and reapply the Terraform configuration:

Error: Invalid address to set: []string{"browser_step", "14", "params", "0", "variable", "0", "secure"}

Steps to Reproduce

No response

Important Factoids

No response

References

No response