internal/getproviders: fix panic with invalid path parts (#24940)

* internal/getproviders: fix panic with invalid path parts

If the search path is missing a directory, the provider installer would
try to create an addrs.Provider with the wrong parts. For example if the
hostname was missing (as in the test case), it would call
addrs.NewProvider with (namespace, typename, version). This adds a
validation step for each part before calling addrs.NewProvider to avoid
the panic.
This commit is contained in:
Kristin Laemmert 2020-05-13 13:16:09 -04:00 committed by GitHub
parent 95ad52de11
commit d350818126
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 38 additions and 0 deletions

View File

@ -92,6 +92,16 @@ func TestFilesystemMirrorSourceAllAvailablePackages(t *testing.T) {
}
}
// In this test the directory layout is invalid (missing the hostname
// subdirectory). The provider installer should ignore the invalid directory.
func TestFilesystemMirrorSourceAllAvailablePackages_invalid(t *testing.T) {
source := NewFilesystemMirrorSource("testdata/filesystem-mirror-invalid")
_, err := source.AllAvailablePackages()
if err != nil {
t.Fatal(err)
}
}
func TestFilesystemMirrorSourceAvailableVersions(t *testing.T) {
source := NewFilesystemMirrorSource("testdata/filesystem-mirror")
got, err := source.AvailableVersions(nullProvider)

View File

@ -52,6 +52,22 @@ func SearchLocalDirectory(baseDir string) (map[addrs.Provider]PackageMetaList, e
namespace := parts[1]
typeName := parts[2]
// validate each part
// The legacy provider namespace is a special case.
if namespace != addrs.LegacyProviderNamespace {
_, err = addrs.ParseProviderPart(namespace)
if err != nil {
log.Printf("[WARN] local provider path %q contains invalid namespace %q; ignoring", fullPath, namespace)
return nil
}
}
_, err = addrs.ParseProviderPart(typeName)
if err != nil {
log.Printf("[WARN] local provider path %q contains invalid type %q; ignoring", fullPath, typeName)
return nil
}
hostname, err := svchost.ForComparison(hostnameGiven)
if err != nil {
log.Printf("[WARN] local provider path %q contains invalid hostname %q; ignoring", fullPath, hostnameGiven)

View File

@ -0,0 +1 @@
# This is just a placeholder file for discovery testing, not a real provider plugin.

View File

@ -0,0 +1 @@
# This is just a placeholder file for discovery testing, not a real provider plugin.

View File

@ -0,0 +1 @@
This should be ignored because it doesn't follow the provider package naming scheme.

View File

@ -0,0 +1,5 @@
This is just a placeholder file for discovery testing, not a real provider package.
This file is what we'd find for mirrors using the "packed" mirror layout,
where the mirror maintainer can just download the packages from upstream and
have Terraform unpack them automatically when installing.

View File

@ -0,0 +1 @@
This should be ignored because it doesn't follow the provider package naming scheme.

View File

@ -0,0 +1 @@
This should be ignored because it doesn't follow the provider package naming scheme.

View File

@ -0,0 +1 @@
# This is just a placeholder file for discovery testing, not a real provider plugin.

View File

@ -0,0 +1 @@
# This is just a placeholder file for discovery testing, not a real provider plugin.