Skip to content
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

Removing tf show output when streaming logs #104

Merged
merged 6 commits into from
Sep 16, 2021
Merged
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
22 changes: 20 additions & 2 deletions server/events/terraform/terraform_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"github.com/runatlantis/atlantis/server/logging"
)

var LogStreamingValidCmds = [...]string{"init", "plan", "apply"}

//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_terraform_client.go Client

type Client interface {
Expand Down Expand Up @@ -440,21 +442,28 @@ func (c *DefaultClient) RunCommandAsync(ctx models.ProjectCommandContext, path s

// Asynchronously copy from stdout/err to outCh.
go func() {
// Don't stream terraform show output to outCh
cmds := strings.Split(tfCmd, " ")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to actually only allow plan and apply specifically instead of just having a deny list?

Upstream has support for using terraform version. and we might want to eventually support terraform import.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it makes sense to have an allow list for better control over what's being streamed to the web UI. I've added a LogStreamingValidCmds with init, plan, and apply commands for now. We can add more commands as we go :)

c.projectCmdOutputHandler.Send(ctx, fmt.Sprintf("\n----- running terraform %s -----\n", args[0]))
s := bufio.NewScanner(stdout)
for s.Scan() {
message := s.Text()
outCh <- Line{Line: message}
c.projectCmdOutputHandler.Send(ctx, message)
if isValidCommand(cmds[1]) {
c.projectCmdOutputHandler.Send(ctx, message)
}
}
wg.Done()
}()
go func() {
cmds := strings.Split(tfCmd, " ")
s := bufio.NewScanner(stderr)
for s.Scan() {
message := s.Text()
outCh <- Line{Line: message}
c.projectCmdOutputHandler.Send(ctx, message)
if isValidCommand(cmds[1]) {
c.projectCmdOutputHandler.Send(ctx, message)
}
}
wg.Done()
}()
Expand Down Expand Up @@ -557,6 +566,15 @@ func generateRCFile(tfeToken string, tfeHostname string, home string) error {
return nil
}

func isValidCommand(cmd string) bool {
for _, validCmd := range LogStreamingValidCmds {
if validCmd == cmd {
return true
}
}
return false
}

func getVersion(tfBinary string) (*version.Version, error) {
versionOutBytes, err := exec.Command(tfBinary, "version").Output() // #nosec
versionOutput := string(versionOutBytes)
Expand Down