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:
parent
95ad52de11
commit
d350818126
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
# This is just a placeholder file for discovery testing, not a real provider plugin.
|
|
@ -0,0 +1 @@
|
|||
# This is just a placeholder file for discovery testing, not a real provider plugin.
|
1
internal/getproviders/testdata/filesystem-mirror-invalid/hashicorp/null/invalid
vendored
Normal file
1
internal/getproviders/testdata/filesystem-mirror-invalid/hashicorp/null/invalid
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
This should be ignored because it doesn't follow the provider package naming scheme.
|
|
@ -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.
|
|
@ -0,0 +1 @@
|
|||
This should be ignored because it doesn't follow the provider package naming scheme.
|
|
@ -0,0 +1 @@
|
|||
This should be ignored because it doesn't follow the provider package naming scheme.
|
|
@ -0,0 +1 @@
|
|||
# This is just a placeholder file for discovery testing, not a real provider plugin.
|
|
@ -0,0 +1 @@
|
|||
# This is just a placeholder file for discovery testing, not a real provider plugin.
|
Loading…
Reference in New Issue