Skip to content

feat(cli): allow direct tar upload in template update/create #5720

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 3 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Revert "feat(provisionersdk): follow symlinks while archiving"
This reverts commit f38ecdb.
  • Loading branch information
ammario committed Jan 15, 2023
commit aac180b6e7167320ce32ae80bfc5152017cb7c5c
80 changes: 29 additions & 51 deletions provisionersdk/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"archive/tar"
"bytes"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -32,37 +31,43 @@ func dirHasExt(dir string, ext string) (bool, error) {
return false, nil
}

// Tar archives a Terraform directory.
func Tar(directory string, limit int64) ([]byte, error) {
var buffer bytes.Buffer
tarWriter := tar.NewWriter(&buffer)
totalSize := int64(0)

type archiver struct {
const tfExt = ".tf"
hasTf, err := dirHasExt(directory, tfExt)
if err != nil {
return nil, err
}
if !hasTf {
absPath, err := filepath.Abs(directory)
if err != nil {
return nil, err
}

}
// Show absolute path to aid in debugging. E.g. showing "." is
// useless.
return nil, xerrors.Errorf(
"%s is not a valid template since it has no %s files",
absPath, tfExt,
)
}

func (a *archiver) walkFn(path string, fileInfo os.FileInfo, err error) error {
err = filepath.Walk(directory, func(file string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
var link string
if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
// Per https://github.com/coder/coder/issues/5677, we want to
// follow symlinks.
var linkDest string
linkDest, err = os.Readlink(file)
link, err = os.Readlink(file)
if err != nil {
return err
}

destInfo, err := os.Stat(linkDest)
if err != nil {
return err
}
if destInfo.IsDir() {
return filepath.Walk(linkDest, func(path string, info fs.FileInfo, err error) error {
walkFn(path, info, err)
})
}
return nil
}

header, err := tar.FileInfoHeader(fileInfo, "")
header, err := tar.FileInfoHeader(fileInfo, link)
if err != nil {
return err
}
Expand All @@ -71,10 +76,11 @@ func (a *archiver) walkFn(path string, fileInfo os.FileInfo, err error) error {
return err
}
if strings.HasPrefix(rel, ".") || strings.HasPrefix(filepath.Base(rel), ".") {
// Don't archive hidden files!
if fileInfo.IsDir() && rel != "." {
// Don't archive hidden files!
return filepath.SkipDir
}
// Don't archive hidden files!
return nil
}
if strings.Contains(rel, ".tfstate") {
Expand Down Expand Up @@ -103,35 +109,7 @@ func (a *archiver) walkFn(path string, fileInfo os.FileInfo, err error) error {
return xerrors.Errorf("Archive too big. Must be <= %d bytes", limit)
}
return data.Close()
}
}

// Tar archives a Terraform directory.
func Tar(directory string, limit int64) ([]byte, error) {
var buffer bytes.Buffer
tarWriter := tar.NewWriter(&buffer)
totalSize := int64(0)

const tfExt = ".tf"
hasTf, err := dirHasExt(directory, tfExt)
if err != nil {
return nil, err
}
if !hasTf {
absPath, err := filepath.Abs(directory)
if err != nil {
return nil, err
}

// Show absolute path to aid in debugging. E.g. showing "." is
// useless.
return nil, xerrors.Errorf(
"%s is not a valid template since it has no %s files",
absPath, tfExt,
)
}

err = filepath.Walk(directory, tarWalkFn())
})
if err != nil {
return nil, err
}
Expand Down
66 changes: 11 additions & 55 deletions provisionersdk/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

func TestTar(t *testing.T) {
t.Parallel()

t.Run("NoTF", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
Expand Down Expand Up @@ -91,58 +90,15 @@ func TestTar(t *testing.T) {

func TestUntar(t *testing.T) {
t.Parallel()
t.Run("Normal", func(t *testing.T) {
t.Parallel()

dir := t.TempDir()
file, err := os.CreateTemp(dir, "*.tf")
require.NoError(t, err)
_ = file.Close()
archive, err := provisionersdk.Tar(dir, 1024)
require.NoError(t, err)
dir = t.TempDir()
err = provisionersdk.Untar(dir, archive)
require.NoError(t, err)
_, err = os.Stat(filepath.Join(dir, filepath.Base(file.Name())))
require.NoError(t, err)
})
t.Run("FollowSymlinks", func(t *testing.T) {
t.Parallel()

externalDir := t.TempDir()
externalFile, err := os.CreateTemp(externalDir, "")
require.NoError(t, err)
const externalFileContents = "dogdogdog"
externalFile.WriteString(externalFileContents)
externalFile.Close()

dir := t.TempDir()

file, err := os.CreateTemp(dir, "*.tf")
require.NoError(t, err)
_ = file.Close()

err = os.Symlink(
externalDir,
filepath.Join(dir, "link"),
)
require.NoError(t, err)

checkDir := func(dir string) {
gotContents, err := os.ReadFile(filepath.Join(dir, "link", filepath.Base(externalFile.Name())))
require.NoError(t, err)
require.EqualValues(t, externalFileContents, gotContents)
}

checkDir(dir)

archive, err := provisionersdk.Tar(dir, 1024)
require.NoError(t, err)

extractDir := t.TempDir()
err = provisionersdk.Untar(extractDir, archive)
require.NoError(t, err)

checkDir(extractDir)
})
dir := t.TempDir()
file, err := os.CreateTemp(dir, "*.tf")
require.NoError(t, err)
_ = file.Close()
archive, err := provisionersdk.Tar(dir, 1024)
require.NoError(t, err)
dir = t.TempDir()
err = provisionersdk.Untar(dir, archive)
require.NoError(t, err)
_, err = os.Stat(filepath.Join(dir, filepath.Base(file.Name())))
require.NoError(t, err)
}