Skip to content

fix(provisionersdk): allow .terraform.lock.hcl files to be archived #7604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion provisionersdk/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func Tar(w io.Writer, directory string, limit int64) error {
if err != nil {
return err
}
if strings.HasPrefix(rel, ".") || strings.HasPrefix(filepath.Base(rel), ".") {
// We want to allow .terraform.lock.hcl files to be archived. This
// allows provider plugins to be cached.
if (strings.HasPrefix(rel, ".") || strings.HasPrefix(filepath.Base(rel), ".")) && filepath.Base(rel) != ".terraform.lock.hcl" {
if fileInfo.IsDir() && rel != "." {
// Don't archive hidden files!
return filepath.SkipDir
Expand Down
71 changes: 47 additions & 24 deletions provisionersdk/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -39,25 +40,39 @@ func TestTar(t *testing.T) {
Name string
Archives bool
}
files := []*file{{
Name: "*.tf",
Archives: true,
}, {
Name: ".*",
Archives: false,
}, {
Name: "./testing/.test/*.tf",
Archives: false,
}, {
Name: "./testing/asd.*",
Archives: true,
}, {
Name: ".terraform/.*",
Archives: false,
}, {
Name: "example/.terraform/*",
Archives: false,
}}
files := []*file{
{
Name: "*.tf",
Archives: true,
}, {
Name: ".*",
Archives: false,
}, {
Name: "./testing/.test/*.tf",
Archives: false,
}, {
Name: "./testing/asd.*",
Archives: true,
}, {
Name: ".terraform/.*",
Archives: false,
}, {
Name: "example/.terraform/*",
Archives: false,
}, {
Name: ".terraform.lock.hcl",
Archives: true,
}, {
Name: "example/.terraform.lock.hcl",
Archives: true,
}, {
Name: ".terraform/.terraform.lock.hcl",
Archives: false,
}, {
Name: "terraform.tfstate",
Archives: false,
},
}
for _, file := range files {
newDir := dir
file.Name = filepath.FromSlash(file.Name)
Expand All @@ -67,11 +82,19 @@ func TestTar(t *testing.T) {
require.NoError(t, err)
file.Name = filepath.Base(file.Name)
}
tmpFile, err := os.CreateTemp(newDir, file.Name)
require.NoError(t, err)
_ = tmpFile.Close()
file.Name, err = filepath.Rel(dir, tmpFile.Name())
require.NoError(t, err)
if strings.Contains(file.Name, "*") {
tmpFile, err := os.CreateTemp(newDir, file.Name)
require.NoError(t, err)
_ = tmpFile.Close()
file.Name, err = filepath.Rel(dir, tmpFile.Name())
require.NoError(t, err)
} else {
name := filepath.Join(newDir, file.Name)
err := os.WriteFile(name, []byte{}, 0o600)
require.NoError(t, err)
file.Name, err = filepath.Rel(dir, name)
require.NoError(t, err)
}
}
archive := new(bytes.Buffer)
err := provisionersdk.Tar(archive, dir, 1024)
Expand Down