Merge pull request #557 from cloudskiff/fix/globEdgeCase

Ensure enumerated paths are not folders
main
Raphaël 2021-05-31 15:05:17 +02:00 committed by GitHub
commit f4c4394c1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 1 deletions

View File

@ -125,6 +125,15 @@ func TestFileEnumerator_Enumerate(t *testing.T) {
want: nil, want: nil,
err: "no Terraform state was found in testdata/no_state_here/test/*, exiting", err: "no Terraform state was found in testdata/no_state_here/test/*, exiting",
}, },
{
name: "test folder terraform.tfstate is not recognized as a file",
config: config.SupplierConfig{
Path: "testdata/edge_cases/**/*.tfstate",
},
want: []string{
"testdata/edge_cases/terraform.tfstate/terraform.tfstate",
},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@ -1,6 +1,7 @@
package enumerator package enumerator
import ( import (
"io/fs"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -46,7 +47,16 @@ func Glob(pattern string) ([]string, error) {
return filepath.Glob(pattern) return filepath.Glob(pattern)
} }
files, err := doublestar.Glob(os.DirFS("."), path.Clean(pattern)) var files []string
err := doublestar.GlobWalk(os.DirFS("."), path.Clean(pattern), func(path string, d fs.DirEntry) error {
// Ensure paths aren't actually directories
// For example when the directory matches the glob pattern like it's a file
if !d.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -353,6 +353,34 @@ func TestS3Enumerator_Enumerate(t *testing.T) {
want: []string{}, want: []string{},
err: "no Terraform state was found in bucket-name/a/nested/prefix/**/*.tfstate, exiting", err: "no Terraform state was found in bucket-name/a/nested/prefix/**/*.tfstate, exiting",
}, },
{
name: "test folder terraform.tfstate is not recognized as a file",
config: config.SupplierConfig{
Path: "bucket-name/a/nested/**/*.tfstate",
},
mocks: func(client *awstest.MockFakeS3) {
input := &s3.ListObjectsV2Input{
Bucket: awssdk.String("bucket-name"),
Prefix: awssdk.String("a/nested"),
}
client.On(
"ListObjectsV2Pages",
input,
mock.MatchedBy(func(callback func(res *s3.ListObjectsV2Output, lastPage bool) bool) bool {
callback(&s3.ListObjectsV2Output{
Contents: []*s3.Object{
{
Key: awssdk.String("a/nested/prefix/terraform.tfstate/terraform.tfstate"),
Size: awssdk.Int64(5),
},
},
}, true)
return true
}),
).Return(nil)
},
want: []string{"bucket-name/a/nested/prefix/terraform.tfstate/terraform.tfstate"},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {