Skip to content

Commit 097c21b

Browse files
committed
Add validate checks for all tftypes.NewValue() calls
1 parent 1947cf2 commit 097c21b

File tree

1 file changed

+128
-20
lines changed

1 file changed

+128
-20
lines changed

internal/fwserver/schema_propose_new_plan.go

Lines changed: 128 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ type ProposeNewStateResponse struct {
2424
}
2525

2626
func SchemaProposeNewState(ctx context.Context, s fwschema.Schema, req ProposeNewStateRequest, resp *ProposeNewStateResponse) {
27-
// TODO: This is in core's logic, but I'm not sure what how this scenario would be triggered
28-
// Need to verify if it's relevant...
29-
if req.Config.Raw.IsNull() && req.PriorState.Raw.IsNull() {
30-
resp.ProposedNewState = stateToPlan(req.PriorState)
31-
return
32-
}
33-
3427
if req.PriorState.Raw.IsNull() {
3528
// Populate prior state with a top-level round of nulls from the schema
3629
req.PriorState = tfsdk.State{
@@ -72,7 +65,11 @@ func proposedNew(ctx context.Context, s fwschema.Schema, path *tftypes.Attribute
7265
newAttrs[name] = proposeNewNestedBlock(ctx, s, blockType, path.WithAttributeName(name), priorVal, configVal)
7366
}
7467

75-
// TODO: validate before doing this? To avoid panic
68+
err := tftypes.ValidateValue(s.Type().TerraformType(ctx), newAttrs)
69+
if err != nil {
70+
// TODO: throw diag
71+
return tftypes.Value{}
72+
}
7673
return tftypes.NewValue(s.Type().TerraformType(ctx), newAttrs)
7774
}
7875

@@ -85,11 +82,23 @@ func proposedNewAttributes(ctx context.Context, s fwschema.Schema, attrs map[str
8582
switch {
8683
case priorObj.IsNull():
8784
priorObjType := priorObj.Type().(tftypes.Object) //nolint
88-
// TODO: validate before doing this? To avoid panic
85+
86+
err := tftypes.ValidateValue(priorObjType.AttributeTypes[name], nil)
87+
if err != nil {
88+
// TODO: handle error
89+
return nil
90+
}
91+
8992
priorVal = tftypes.NewValue(priorObjType.AttributeTypes[name], nil)
9093
case !priorObj.IsKnown():
9194
priorObjType := priorObj.Type().(tftypes.Object) //nolint
92-
// TODO: validate before doing this? To avoid panic
95+
96+
err := tftypes.ValidateValue(priorObjType.AttributeTypes[name], tftypes.UnknownValue)
97+
if err != nil {
98+
// TODO: handle error
99+
return nil
100+
}
101+
93102
priorVal = tftypes.NewValue(priorObjType.AttributeTypes[name], tftypes.UnknownValue)
94103
default:
95104
// TODO: handle error
@@ -105,11 +114,23 @@ func proposedNewAttributes(ctx context.Context, s fwschema.Schema, attrs map[str
105114
switch {
106115
case configObj.IsNull():
107116
configObjType := configObj.Type().(tftypes.Object) //nolint
108-
// TODO: validate before doing this? To avoid panic
117+
118+
err := tftypes.ValidateValue(configObjType.AttributeTypes[name], nil)
119+
if err != nil {
120+
// TODO: handle error
121+
return nil
122+
}
123+
109124
configVal = tftypes.NewValue(configObjType.AttributeTypes[name], nil)
110125
case !configObj.IsKnown():
111126
configObjType := configObj.Type().(tftypes.Object) //nolint
112-
// TODO: validate before doing this? To avoid panic
127+
128+
err := tftypes.ValidateValue(configObjType.AttributeTypes[name], tftypes.UnknownValue)
129+
if err != nil {
130+
// TODO: handle error
131+
return nil
132+
}
133+
113134
configVal = tftypes.NewValue(configObjType.AttributeTypes[name], tftypes.UnknownValue)
114135
default:
115136
// TODO: handle error
@@ -176,7 +197,13 @@ func proposeNewNestedBlockObject(ctx context.Context, s fwschema.Schema, nestedB
176197
var priorVal tftypes.Value
177198
if prior.IsNull() {
178199
priorObjType := prior.Type().(tftypes.Object) //nolint
179-
// TODO: validate before doing this? To avoid panic
200+
201+
err := tftypes.ValidateValue(priorObjType.AttributeTypes[name], nil)
202+
if err != nil {
203+
// TODO: handle error
204+
return tftypes.Value{}
205+
}
206+
180207
priorVal = tftypes.NewValue(priorObjType.AttributeTypes[name], nil)
181208
} else {
182209
// TODO: handle error
@@ -192,7 +219,12 @@ func proposeNewNestedBlockObject(ctx context.Context, s fwschema.Schema, nestedB
192219
valuesMap[name] = proposeNewNestedBlock(ctx, s, blockType, path.WithAttributeName(name), priorVal, configVal)
193220
}
194221

195-
// TODO: validate before doing this? To avoid panic
222+
err := tftypes.ValidateValue(nestedBlock.Type().TerraformType(ctx), valuesMap)
223+
if err != nil {
224+
// TODO: handle error
225+
return tftypes.Value{}
226+
}
227+
196228
return tftypes.NewValue(
197229
nestedBlock.Type().TerraformType(ctx),
198230
valuesMap,
@@ -259,15 +291,34 @@ func proposedNewMapNested(ctx context.Context, s fwschema.Schema, attr fwschema.
259291
// if the prior value was unknown the map won't have any
260292
// keys, so generate an unknown value.
261293
if !prior.IsKnown() {
294+
err := tftypes.ValidateValue(configEV.Type(), tftypes.UnknownValue)
295+
if err != nil {
296+
// TODO: handle error
297+
return tftypes.Value{}
298+
}
299+
262300
priorEV = tftypes.NewValue(configEV.Type(), tftypes.UnknownValue)
263301
} else {
302+
err := tftypes.ValidateValue(configEV.Type(), nil)
303+
if err != nil {
304+
// TODO: handle error
305+
return tftypes.Value{}
306+
}
307+
264308
priorEV = tftypes.NewValue(configEV.Type(), nil)
265309

266310
}
267311
}
268312

269313
newVals[name] = proposedNewObjectAttributes(ctx, s, attr, path.WithElementKeyString(name), priorEV, configEV)
270314
}
315+
316+
err := tftypes.ValidateValue(config.Type(), newVals)
317+
if err != nil {
318+
// TODO: handle error
319+
return tftypes.Value{}
320+
}
321+
271322
newVal = tftypes.NewValue(config.Type(), newVals)
272323
}
273324

@@ -311,6 +362,12 @@ func proposedNewBlockListNested(ctx context.Context, s fwschema.Schema, block fw
311362
newVals = append(newVals, proposedNewBlockObjectAttributes(ctx, s, block, path.WithElementKeyInt(idx), priorEV, configEV))
312363
}
313364

365+
err := tftypes.ValidateValue(config.Type(), newVals)
366+
if err != nil {
367+
// TODO: handle error
368+
return tftypes.Value{}
369+
}
370+
314371
newVal = tftypes.NewValue(config.Type(), newVals)
315372
}
316373

@@ -368,10 +425,22 @@ func proposedNewBlockSetNested(ctx context.Context, s fwschema.Schema, block fws
368425
}
369426

370427
if priorEV.IsNull() {
428+
err := tftypes.ValidateValue(block.GetNestedObject().Type().TerraformType(ctx), nil)
429+
if err != nil {
430+
// TODO: handle error
431+
return tftypes.Value{}
432+
}
433+
371434
priorEV = tftypes.NewValue(block.GetNestedObject().Type().TerraformType(ctx), nil)
372435
}
373436
newVals = append(newVals, proposeNewNestedBlockObject(ctx, s, block.GetNestedObject(), path.WithElementKeyValue(priorEV), priorEV, configEV))
374437
}
438+
439+
err := tftypes.ValidateValue(config.Type(), newVals)
440+
if err != nil {
441+
// TODO: handle error
442+
return tftypes.Value{}
443+
}
375444
newVal = tftypes.NewValue(config.Type(), newVals)
376445
}
377446

@@ -414,6 +483,13 @@ func proposedNewListNested(ctx context.Context, s fwschema.Schema, attr fwschema
414483
priorEV := priorVals[idx]
415484
newVals = append(newVals, proposedNewObjectAttributes(ctx, s, attr, path.WithElementKeyInt(idx), priorEV, configEV))
416485
}
486+
487+
err := tftypes.ValidateValue(config.Type(), newVals)
488+
if err != nil {
489+
// TODO: handle error
490+
return tftypes.Value{}
491+
}
492+
417493
newVal = tftypes.NewValue(config.Type(), newVals)
418494
}
419495

@@ -471,10 +547,22 @@ func proposedNewSetNested(ctx context.Context, s fwschema.Schema, attr fwschema.
471547
}
472548

473549
if priorEV.IsNull() {
550+
err := tftypes.ValidateValue(attr.GetNestedObject().Type().TerraformType(ctx), nil)
551+
if err != nil {
552+
// TODO: handle error
553+
return tftypes.Value{}
554+
}
555+
474556
priorEV = tftypes.NewValue(attr.GetNestedObject().Type().TerraformType(ctx), nil)
475557
}
476558
newVals = append(newVals, proposedNewObjectAttributes(ctx, s, attr, path.WithElementKeyValue(priorEV), priorEV, configEV))
477559
}
560+
err := tftypes.ValidateValue(config.Type(), newVals)
561+
if err != nil {
562+
// TODO: handle error
563+
return tftypes.Value{}
564+
}
565+
478566
newVal = tftypes.NewValue(config.Type(), newVals)
479567
}
480568

@@ -486,10 +574,21 @@ func proposedNewObjectAttributes(ctx context.Context, s fwschema.Schema, attr fw
486574
return config
487575
}
488576

489-
// TODO: validate before doing this? To avoid panic
577+
objType := attr.GetNestedObject().Type().TerraformType(ctx)
578+
newAttrs := proposedNewAttributes(ctx, s, attr.GetNestedObject().GetAttributes(), path, prior, config)
579+
580+
err := tftypes.ValidateValue(
581+
objType,
582+
newAttrs,
583+
)
584+
if err != nil {
585+
// TODO: handle error
586+
return tftypes.Value{}
587+
}
588+
490589
return tftypes.NewValue(
491-
attr.GetNestedObject().Type().TerraformType(ctx),
492-
proposedNewAttributes(ctx, s, attr.GetNestedObject().GetAttributes(), path, prior, config),
590+
objType,
591+
newAttrs,
493592
)
494593
}
495594

@@ -500,7 +599,6 @@ func proposedNewBlockObjectAttributes(ctx context.Context, s fwschema.Schema, bl
500599
valuesMap := proposedNewAttributes(ctx, s, block.GetNestedObject().GetAttributes(), path, prior, config)
501600

502601
for name, blockType := range block.GetNestedObject().GetBlocks() {
503-
//maps.Copy(valuesMap, proposedNewAttributes(ctx, s, blockType.GetNestedObject().GetAttributes(), tftypes.NewAttributePath().WithAttributeName(name).WithElementKeyInt(0), prior, config))
504602
attrVal, err := prior.ApplyTerraform5AttributePathStep(tftypes.AttributeName(name))
505603
//TODO handle panic
506604
if err != nil {
@@ -513,9 +611,19 @@ func proposedNewBlockObjectAttributes(ctx context.Context, s fwschema.Schema, bl
513611
valuesMap[name] = proposeNewNestedBlock(ctx, s, blockType, tftypes.NewAttributePath().WithAttributeName(name).WithElementKeyInt(0), priorVal, configVal)
514612
}
515613

516-
// TODO: validate before doing this? To avoid panic
614+
objType := block.GetNestedObject().Type().TerraformType(ctx)
615+
616+
err := tftypes.ValidateValue(
617+
objType,
618+
valuesMap,
619+
)
620+
if err != nil {
621+
// TODO: handle error
622+
return tftypes.Value{}
623+
}
624+
517625
return tftypes.NewValue(
518-
block.GetNestedObject().Type().TerraformType(ctx),
626+
objType,
519627
valuesMap,
520628
)
521629
}

0 commit comments

Comments
 (0)