Skip to content

Commit

Permalink
feat: supports adding files inside the VM to support bundles
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Berning <[email protected]>
  • Loading branch information
sam-berning committed Aug 29, 2023
1 parent 6ea1499 commit 25a179c
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 36 deletions.
6 changes: 3 additions & 3 deletions cmd/finch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"io"
"os"

"github.com/spf13/afero"
"github.com/spf13/cobra"

"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/config"
"github.com/runfinch/finch/pkg/dependency"
Expand All @@ -26,6 +23,8 @@ import (
"github.com/runfinch/finch/pkg/support"
"github.com/runfinch/finch/pkg/system"
"github.com/runfinch/finch/pkg/version"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)

const finchRootCmd = "finch"
Expand Down Expand Up @@ -96,6 +95,7 @@ var newApp = func(logger flog.Logger, fp path.Finch, fs afero.Fs, fc *config.Fin
support.NewBundleConfig(fp, system.NewStdLib().Env("HOME")),
fp,
ecc,
lcc,
wrapper.NewLimaWrapper(),
)

Expand Down
2 changes: 2 additions & 0 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Command interface {
SetStderr(io.Writer)

Run() error
Start() error
Wait() error
Output() ([]byte, error)
CombinedOutput() ([]byte, error)
}
28 changes: 28 additions & 0 deletions pkg/mocks/command_command.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 98 additions & 27 deletions pkg/support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package support

import (
"archive/zip"
"bufio"
"bytes"
"errors"
"fmt"
Expand All @@ -15,14 +16,13 @@ import (
"strings"
"time"

"github.com/spf13/afero"
"gopkg.in/yaml.v3"

"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/lima/wrapper"
fpath "github.com/runfinch/finch/pkg/path"
"github.com/runfinch/finch/pkg/version"
"github.com/spf13/afero"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -51,6 +51,7 @@ type bundleBuilder struct {
config BundleConfig
finch fpath.Finch
ecc command.Creator
lcc command.LimaCmdCreator
lima wrapper.LimaWrapper
}

Expand All @@ -61,6 +62,7 @@ func NewBundleBuilder(
config BundleConfig,
finch fpath.Finch,
ecc command.Creator,
lcc command.LimaCmdCreator,
lima wrapper.LimaWrapper,
) BundleBuilder {
return &bundleBuilder{
Expand All @@ -69,6 +71,7 @@ func NewBundleBuilder(
config: config,
finch: finch,
ecc: ecc,
lcc: lcc,
lima: lima,
}
}
Expand Down Expand Up @@ -108,7 +111,13 @@ func (bb *bundleBuilder) GenerateSupportBundle(additionalFiles []string, exclude
bb.logger.Infof("Excluding %s...", file)
continue
}
err := bb.copyInFile(writer, file, path.Join(zipPrefix, logPrefix))
bb.logger.Debugf("Copying %s...", file)
var err error
if isFileFromVM(file) {
err = bb.streamFileFromVM(writer, file, path.Join(zipPrefix, logPrefix))
} else {
err = bb.copyInFile(writer, file, path.Join(zipPrefix, logPrefix))
}
if err != nil {
bb.logger.Warnf("Could not copy in %q. Error: %s", file, err)
}
Expand All @@ -120,7 +129,13 @@ func (bb *bundleBuilder) GenerateSupportBundle(additionalFiles []string, exclude
bb.logger.Infof("Excluding %s...", file)
continue
}
err := bb.copyInFile(writer, file, path.Join(zipPrefix, configPrefix))
bb.logger.Debugf("Copying %s...", file)
var err error
if isFileFromVM(file) {
err = bb.streamFileFromVM(writer, file, path.Join(zipPrefix, configPrefix))
} else {
err = bb.copyInFile(writer, file, path.Join(zipPrefix, configPrefix))
}
if err != nil {
bb.logger.Warnf("Could not copy in %q. Error: %s", file, err)
}
Expand All @@ -132,7 +147,13 @@ func (bb *bundleBuilder) GenerateSupportBundle(additionalFiles []string, exclude
bb.logger.Infof("Excluding %s...", file)
continue
}
err := bb.copyInFile(writer, file, path.Join(zipPrefix, additionalPrefix))
bb.logger.Debugf("Copying %s...", file)
var err error
if isFileFromVM(file) {
err = bb.streamFileFromVM(writer, file, path.Join(zipPrefix, additionalPrefix))
} else {
err = bb.copyInFile(writer, file, path.Join(zipPrefix, additionalPrefix))
}
if err != nil {
bb.logger.Warnf("Could not add additional file %s. Error: %s", file, err)
}
Expand All @@ -146,30 +167,21 @@ func (bb *bundleBuilder) GenerateSupportBundle(additionalFiles []string, exclude
return zipFileName, nil
}

func (bb *bundleBuilder) copyInFile(writer *zip.Writer, fileName string, prefix string) error {
f, err := bb.fs.Open(fileName)
if err != nil {
return err
}

bb.logger.Debugf("Copying %s...", fileName)

var buf bytes.Buffer
_, err = buf.ReadFrom(f)
if err != nil {
return err
}
type bufReader interface {
ReadBytes(delim byte) ([]byte, error)
}

var redacted []byte
func (bb *bundleBuilder) copyAndRedactFile(writer io.Writer, reader bufReader) error {
var bufErr error
for bufErr == nil {
var line []byte
line, bufErr = buf.ReadBytes('\n')
line, bufErr = reader.ReadBytes('\n')
if bufErr != nil && !errors.Is(bufErr, io.EOF) {
bb.logger.Error(bufErr.Error())
continue
}

line, err = redactFinchInstall(line, bb.finch)
line, err := redactFinchInstall(line, bb.finch)
if err != nil {
return err
}
Expand All @@ -187,7 +199,20 @@ func (bb *bundleBuilder) copyInFile(writer *zip.Writer, fileName string, prefix
line = redactPorts(line)
line = redactSSHKeys(line)

redacted = append(redacted, line...)
_, err = writer.Write(line)
if err != nil {
return err
}
}

return nil
}

func (bb *bundleBuilder) copyInFile(writer *zip.Writer, fileName string, prefix string) error {
// check filename validity?
f, err := bb.fs.Open(fileName)
if err != nil {
return err
}

baseName := path.Base(fileName)
Expand All @@ -196,12 +221,50 @@ func (bb *bundleBuilder) copyInFile(writer *zip.Writer, fileName string, prefix
return err
}

_, err = zipCopy.Write(redacted)
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(f)
if err != nil {
return err
}

return nil
return bb.copyAndRedactFile(zipCopy, buf)
}

func (bb *bundleBuilder) streamFileFromVM(writer *zip.Writer, filename, prefix string) error {
pipeReader, pipeWriter := io.Pipe()
errBuf := new(bytes.Buffer)

_, filePathInVM, _ := strings.Cut(filename, ":")
cmd := bb.lcc.CreateWithoutStdio("shell", "finch", "sudo", "cat", filePathInVM)
cmd.SetStdout(pipeWriter)
cmd.SetStderr(errBuf)

err := cmd.Start()
if err != nil {
return err
}

waitStatus := make(chan error)
go func() {
err := cmd.Wait()
pipeWriter.Close()
waitStatus <- err
}()

baseName := path.Base(filename)
zipCopy, err := writer.Create(path.Join(prefix, baseName))
if err != nil {
return err
}

bufReader := bufio.NewReader(pipeReader)

err = bb.copyAndRedactFile(zipCopy, bufReader)
if err != nil {
return err
}

return <-waitStatus
}

func (bb *bundleBuilder) getPlatformData() (*PlatformData, error) {
Expand Down Expand Up @@ -280,7 +343,11 @@ func bundleFileName() string {
}

func fileShouldBeExcluded(filename string, exclude []string) bool {
fileAbs, err := filepath.Abs(filename)
realFilename := filename
if isFileFromVM(filename) {
_, realFilename, _ = strings.Cut(filename, ":")
}
fileAbs, err := filepath.Abs(realFilename)
if err != nil {
return true
}
Expand All @@ -292,9 +359,13 @@ func fileShouldBeExcluded(filename string, exclude []string) bool {
if fileAbs == excludeAbs {
return true
}
if path.Base(filename) == excludeFile {
if path.Base(realFilename) == excludeFile {
return true
}
}
return false
}

func isFileFromVM(filename string) bool {
return strings.HasPrefix(filename, "vm:")
}
Loading

0 comments on commit 25a179c

Please sign in to comment.