Skip to content

Commit 2b2008c

Browse files
committed
read: disallow null resource identities
1 parent 0d05dea commit 2b2008c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

internal/fwserver/server_readresource.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ func (s *Server) ReadResource(ctx context.Context, req *ReadResourceRequest, res
198198
}
199199
}
200200

201+
if req.IdentitySchema != nil {
202+
if resp.NewIdentity.Raw.IsFullyNull() {
203+
resp.Diagnostics.AddError(
204+
"Missing Resource Identity After Read",
205+
"The Terraform Provider unexpectedly returned no resource identity data after having no errors in the resource read. "+
206+
"This is always an issue in the Terraform Provider and should be reported to the provider developers.",
207+
)
208+
return
209+
}
210+
}
211+
201212
semanticEqualityReq := SchemaSemanticEqualityRequest{
202213
PriorData: fwschemadata.Data{
203214
Description: fwschemadata.DataDescriptionState,

internal/fwserver/server_readresource_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ func TestServerReadResource(t *testing.T) {
164164
Schema: testIdentitySchema,
165165
}
166166

167+
testEmptyIdentity := &tfsdk.ResourceIdentity{
168+
Schema: testIdentitySchema,
169+
Raw: tftypes.NewValue(testIdentitySchema.Type().TerraformType(context.Background()), nil),
170+
}
171+
167172
testNewStateRemoved := &tfsdk.State{
168173
Raw: tftypes.NewValue(testType, nil),
169174
Schema: testSchema,
@@ -636,6 +641,35 @@ func TestServerReadResource(t *testing.T) {
636641
Private: testEmptyPrivate,
637642
},
638643
},
644+
"response-invalid-nil-identity": {
645+
server: &fwserver.Server{
646+
Provider: &testprovider.Provider{},
647+
},
648+
request: &fwserver.ReadResourceRequest{
649+
CurrentState: testCurrentState,
650+
CurrentIdentity: nil,
651+
IdentitySchema: testIdentitySchema,
652+
Resource: &testprovider.ResourceWithIdentity{
653+
Resource: &testprovider.Resource{
654+
ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
655+
resp.Identity = req.Identity
656+
},
657+
},
658+
},
659+
},
660+
expectedResponse: &fwserver.ReadResourceResponse{
661+
Diagnostics: diag.Diagnostics{
662+
diag.NewErrorDiagnostic(
663+
"Missing Resource Identity After Read",
664+
"The Terraform Provider unexpectedly returned no resource identity data after having no errors in the resource read. "+
665+
"This is always an issue in the Terraform Provider and should be reported to the provider developers.",
666+
),
667+
},
668+
NewState: testCurrentState,
669+
NewIdentity: testEmptyIdentity,
670+
Private: testEmptyPrivate,
671+
},
672+
},
639673
"response-identity-valid-update-null-currentidentity": {
640674
server: &fwserver.Server{
641675
Provider: &testprovider.Provider{},

0 commit comments

Comments
 (0)