Skip to content

Commit 9239dd6

Browse files
authored
actions: enforce ephemeral into write-only attributes behaviour (#37701)
* actions: add test that validates ephemeral behaviour * actuall prevent ephemeral values into non write-only attributes * remove old invalid test
1 parent 7663739 commit 9239dd6

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

internal/terraform/context_plan_actions_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,52 @@ resource "test_object" "a" {
13511351
},
13521352
},
13531353

1354+
"ephemeral values": {
1355+
module: map[string]string{
1356+
"main.tf": `
1357+
variable "secret" {
1358+
type = string
1359+
ephemeral = true
1360+
}
1361+
action "test_action" "hello" {
1362+
config {
1363+
attr = var.secret
1364+
}
1365+
}
1366+
resource "test_object" "a" {
1367+
lifecycle {
1368+
action_trigger {
1369+
events = [before_create]
1370+
actions = [action.test_action.hello]
1371+
}
1372+
}
1373+
}
1374+
`,
1375+
},
1376+
planOpts: &PlanOpts{
1377+
Mode: plans.NormalMode,
1378+
SetVariables: InputValues{
1379+
"secret": &InputValue{
1380+
Value: cty.StringVal("secret"),
1381+
SourceType: ValueFromCLIArg,
1382+
}},
1383+
},
1384+
expectPlanActionCalled: false,
1385+
assertValidateDiagnostics: func(t *testing.T, diags tfdiags.Diagnostics) {
1386+
if len(diags) != 1 {
1387+
t.Fatalf("expected exactly 1 diagnostic but had %d", len(diags))
1388+
}
1389+
1390+
if diags[0].Severity() != tfdiags.Error {
1391+
t.Error("expected error diagnostic")
1392+
}
1393+
1394+
if diags[0].Description().Summary != "Invalid use of ephemeral value" {
1395+
t.Errorf("expected diagnostics to be because of ephemeral values but was %s", diags[0].Description().Summary)
1396+
}
1397+
},
1398+
},
1399+
13541400
"write-only attributes": {
13551401
module: map[string]string{
13561402
"main.tf": `

internal/terraform/node_action_instance.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ func (n *NodeActionDeclarationInstance) Execute(ctx EvalContext, _ walkOperation
6464
configVal, _, configDiags = ctx.EvaluateBlock(n.Config.Config, n.Schema.ConfigSchema.DeepCopy(), nil, keyData)
6565

6666
diags = diags.Append(configDiags)
67-
if diags.HasErrors() {
67+
if configDiags.HasErrors() {
68+
return diags
69+
}
70+
71+
valDiags := validateResourceForbiddenEphemeralValues(ctx, configVal, n.Schema.ConfigSchema)
72+
diags = diags.Append(valDiags.InConfigBody(n.Config.Config, n.Addr.String()))
73+
74+
if valDiags.HasErrors() {
6875
return diags
6976
}
7077
}

internal/terraform/node_action_validate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ func (n *NodeValidatableAction) Execute(ctx EvalContext, _ walkOperation) tfdiag
102102
}
103103
}
104104

105+
valDiags = validateResourceForbiddenEphemeralValues(ctx, configVal, schema.ConfigSchema)
106+
diags = diags.Append(valDiags.InConfigBody(config, n.Addr.String()))
107+
105108
// Use unmarked value for validate request
106109
unmarkedConfigVal, _ := configVal.UnmarkDeep()
107110
log.Printf("[TRACE] Validating config for %q", n.Addr)

0 commit comments

Comments
 (0)