Skip to content

Commit b7f3e6f

Browse files
authored
helper/schema: Add validation to prevent write-only attributes in set nested blocks (#1427)
* Add validation to prevent write-only attributes in set nested blocks * Add changelog entries
1 parent 83d80f8 commit b7f3e6f

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: BUG FIXES
2+
body: 'helper/schema: Fixed bug that allowed write-only attributes within set nested blocks.
3+
Any attribute within a set nested block with `WriteOnly` set to `true` will now trigger an error message.'
4+
time: 2025-02-18T17:24:40.023079-05:00
5+
custom:
6+
Issue: "1427"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: NOTES
2+
body: Write-only attribute support is in technical preview and offered without compatibility promises until Terraform 1.11 is generally available.
3+
time: 2025-02-18T17:26:25.941391-05:00
4+
custom:
5+
Issue: "1375"

helper/schema/schema.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,13 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
975975
case *Resource:
976976
attrsOnly := attrsOnly || v.ConfigMode == SchemaConfigModeAttr
977977

978-
if v.Computed && schemaMap(t.SchemaMap()).hasWriteOnly() {
978+
blockHasWriteOnly := schemaMap(t.SchemaMap()).hasWriteOnly()
979+
980+
if v.Type == TypeSet && blockHasWriteOnly {
981+
return fmt.Errorf("%s: Set Block type cannot contain WriteOnly attributes", k)
982+
}
983+
984+
if v.Computed && blockHasWriteOnly {
979985
return fmt.Errorf("%s: Block types with Computed set to true cannot contain WriteOnly attributes", k)
980986
}
981987

helper/schema/schema_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -5303,7 +5303,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
53035303
},
53045304
true,
53055305
},
5306-
"Set configuration block nested attribute with WriteOnly set returns no errors": {
5306+
"Set configuration block nested attribute with WriteOnly set returns error": {
53075307
map[string]*Schema{
53085308
"config_block_attr": {
53095309
Type: TypeSet,
@@ -5319,7 +5319,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
53195319
},
53205320
},
53215321
},
5322-
false,
5322+
true,
53235323
},
53245324
"List configuration block with ConfigModeAttr set, sub block nested attribute with WriteOnly set returns no errors": {
53255325
map[string]*Schema{
@@ -5350,7 +5350,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
53505350
false,
53515351
},
53525352

5353-
"Set configuration block with ConfigModeAttr set, sub block nested attribute with WriteOnly set returns no errors": {
5353+
"Set configuration block with ConfigModeAttr set, sub block nested attribute with WriteOnly set returns error": {
53545354
map[string]*Schema{
53555355
"block": {
53565356
Type: TypeSet,
@@ -5376,7 +5376,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
53765376
},
53775377
},
53785378
},
5379-
false,
5379+
true,
53805380
},
53815381
"List computed block nested attribute with WriteOnly set returns error": {
53825382
map[string]*Schema{

website/docs/plugin/sdkv2/resources/write-only-arguments.mdx

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ which should either use the value by making the appropriate change to the API or
1616

1717
The following are high level differences between `Required`/`Optional` arguments and write-only arguments:
1818

19-
- Write-only arguments can accept ephemeral and non-ephemeral values
19+
- Write-only arguments can accept ephemeral and non-ephemeral values.
20+
21+
- Write-only arguments cannot be used with set attributes and set nested blocks.
2022

2123
- Write-only argument values are only available in the configuration. The prior state, planned state, and final state values for
2224
write-only arguments should always be `null`.
@@ -51,6 +53,7 @@ write-only arguments should always be `null`.
5153
- Cannot be used when `Default` is `specified`
5254
- Cannot be used with `DefaultFunc`
5355
- Cannot be used with aggregate schema types (e.g. `typeMap`, `typeList`, `typeSet`), but non-computed nested block types can contain write-only arguments.
56+
- Cannot be used within a set nested block type.
5457

5558
## Retrieving Write-only Values
5659

website/docs/plugin/sdkv2/schemas/schema-behaviors.mdx

+2-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ resource "example_instance" "ex" {
213213
- Cannot be used when `ForceNew` is `true`
214214
- Cannot be used when `Default` is `specified`
215215
- Cannot be used with `DefaultFunc`
216-
- Cannot be used with aggregate schema types (e.g. `typeMap`, `typeList`, `typeSet`), but non-computed nested block types can contain write-only arguments.
216+
- Cannot be used with aggregate schema types (e.g. `typeMap`, `typeList`, `typeSet`), but non-computed list nested block types can contain write-only arguments.
217+
- Cannot be used within a set nested block type
217218

218219
`WriteOnly` should be used for arguments that handle secret values that do not need to be persisted in Terraform plan or state,
219220
such as passwords, API keys, etc. Write-only argument values are not sent to Terraform and do not

0 commit comments

Comments
 (0)