configs: prevent panic with invalid type name (#25562)

An invalid type name in a resource (or data source) could cause a panic
when determining the implied provider for the resource. This commit adds
verification that the type name is valid. It does not add a diagnostic,
since the invalid type name would have already been caught by the
parser.

Fixes #25560
This commit is contained in:
Kristin Laemmert 2020-07-13 09:47:16 -04:00 committed by GitHub
parent 662ea420d6
commit 9cb8456f3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View File

@ -289,7 +289,15 @@ func (m *Module) appendFile(file *File) hcl.Diagnostics {
if r.ProviderConfigRef != nil {
r.Provider = m.ProviderForLocalConfig(r.ProviderConfigAddr())
} else {
r.Provider = m.ImpliedProviderForUnqualifiedType(r.Addr().ImpliedProvider())
// an invalid resource name (for e.g. "null resource" instead of
// "null_resource") can cause a panic down the line in addrs:
// https://github.com/hashicorp/terraform/issues/25560
implied, err := addrs.ParseProviderPart(r.Addr().ImpliedProvider())
if err == nil {
r.Provider = m.ImpliedProviderForUnqualifiedType(implied)
}
// We don't return a diagnostic because the invalid resource name
// will already have been caught.
}
}
@ -310,7 +318,15 @@ func (m *Module) appendFile(file *File) hcl.Diagnostics {
if r.ProviderConfigRef != nil {
r.Provider = m.ProviderForLocalConfig(r.ProviderConfigAddr())
} else {
r.Provider = m.ImpliedProviderForUnqualifiedType(r.Addr().ImpliedProvider())
// an invalid data source name (for e.g. "null resource" instead of
// "null_resource") can cause a panic down the line in addrs:
// https://github.com/hashicorp/terraform/issues/25560
implied, err := addrs.ParseProviderPart(r.Addr().ImpliedProvider())
if err == nil {
r.Provider = m.ImpliedProviderForUnqualifiedType(implied)
}
// We don't return a diagnostic because the invalid resource name
// will already have been caught.
}
}

View File

@ -0,0 +1,7 @@
resource "test resource" "test_resource" {
}
data "test resource" "test_resource" {
}