|
| 1 | +package testfunctional_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" |
| 9 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-testing/knownvalue" |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/plancheck" |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" |
| 13 | + "github.com/hashicorp/terraform-plugin-testing/tfversion" |
| 14 | +) |
| 15 | + |
| 16 | +// This test proves that in the framework, sensitive attributes are not exposed by default in the output. |
| 17 | +// They can be accessed in the output block, but it must be properly marked. |
| 18 | +func TestAcc_TerraformPluginFrameworkFunctional_NestedSensitive(t *testing.T) { |
| 19 | + id := sdk.NewAccountObjectIdentifier("abc") |
| 20 | + resourceType := fmt.Sprintf("%s_nested_sensitive", PluginFrameworkFunctionalTestsProviderName) |
| 21 | + resourceReference := fmt.Sprintf("%s.test", resourceType) |
| 22 | + |
| 23 | + resource.Test(t, resource.TestCase{ |
| 24 | + ProtoV6ProviderFactories: providerForPluginFrameworkFunctionalTestsFactories, |
| 25 | + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ |
| 26 | + tfversion.RequireAbove(tfversion.Version1_5_0), |
| 27 | + }, |
| 28 | + Steps: []resource.TestStep{ |
| 29 | + { |
| 30 | + Config: nestedSensitiveConfig(id, resourceType), |
| 31 | + Check: resource.ComposeTestCheckFunc( |
| 32 | + resource.TestCheckResourceAttr(resourceReference, "id", id.FullyQualifiedName()), |
| 33 | + resource.TestCheckResourceAttr(resourceReference, "output.0.string_sensitive", "SECRET"), |
| 34 | + ), |
| 35 | + ExpectError: regexp.MustCompile("Output refers to sensitive values"), |
| 36 | + }, |
| 37 | + { |
| 38 | + Config: nestedSensitiveConfigWholeOutput(id, resourceType), |
| 39 | + Check: resource.ComposeTestCheckFunc( |
| 40 | + resource.TestCheckResourceAttr(resourceReference, "id", id.FullyQualifiedName()), |
| 41 | + resource.TestCheckResourceAttr(resourceReference, "output.0.string_sensitive", "SECRET"), |
| 42 | + ), |
| 43 | + ExpectError: regexp.MustCompile("Output refers to sensitive values"), |
| 44 | + }, |
| 45 | + { |
| 46 | + Config: nestedSensitiveConfigMarked(id, resourceType), |
| 47 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 48 | + PostApplyPostRefresh: []plancheck.PlanCheck{ |
| 49 | + plancheck.ExpectSensitiveValue(resourceReference, tfjsonpath.New("output").AtSliceIndex(0).AtMapKey("string_sensitive")), |
| 50 | + plancheck.ExpectKnownOutputValue("nested_sensitive", knownvalue.StringExact("SECRET")), |
| 51 | + }, |
| 52 | + }, |
| 53 | + Check: resource.ComposeTestCheckFunc( |
| 54 | + resource.TestCheckResourceAttr(resourceReference, "id", id.FullyQualifiedName()), |
| 55 | + resource.TestCheckResourceAttr(resourceReference, "output.0.string_sensitive", "SECRET"), |
| 56 | + ), |
| 57 | + }, |
| 58 | + }, |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func nestedSensitiveConfig(id sdk.AccountObjectIdentifier, resourceType string) string { |
| 63 | + return nestedSensitiveResourceConfig(id, resourceType) + fmt.Sprintf(` |
| 64 | +output "nested_sensitive" { |
| 65 | + value = resource.%s.test.output[0].string_sensitive |
| 66 | +} |
| 67 | +`, resourceType) |
| 68 | +} |
| 69 | + |
| 70 | +func nestedSensitiveConfigWholeOutput(id sdk.AccountObjectIdentifier, resourceType string) string { |
| 71 | + return nestedSensitiveResourceConfig(id, resourceType) + fmt.Sprintf(` |
| 72 | +output "nested_sensitive" { |
| 73 | + value = resource.%s.test.output[0] |
| 74 | +} |
| 75 | +`, resourceType) |
| 76 | +} |
| 77 | + |
| 78 | +func nestedSensitiveConfigMarked(id sdk.AccountObjectIdentifier, resourceType string) string { |
| 79 | + return nestedSensitiveResourceConfig(id, resourceType) + fmt.Sprintf(` |
| 80 | +output "nested_sensitive" { |
| 81 | + value = resource.%s.test.output[0].string_sensitive |
| 82 | + sensitive = true |
| 83 | +} |
| 84 | +`, resourceType) |
| 85 | +} |
| 86 | + |
| 87 | +func nestedSensitiveResourceConfig(id sdk.AccountObjectIdentifier, resourceType string) string { |
| 88 | + return fmt.Sprintf(` |
| 89 | +resource "%[2]s" "test" { |
| 90 | + provider = "%[3]s" |
| 91 | +
|
| 92 | + name = "%[1]s" |
| 93 | +} |
| 94 | +`, id.Name(), resourceType, PluginFrameworkFunctionalTestsProviderName) |
| 95 | +} |
0 commit comments