Skip to content

Commit

Permalink
Quote args and plan path that may contain spaces.
Browse files Browse the repository at this point in the history
Bitbucket repo owner names can contain spaces. Need to quote everywhere
these might be used because we're executing with sh -c.
  • Loading branch information
lkysow committed Oct 2, 2018
1 parent 5f26c81 commit e98caf3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
4 changes: 3 additions & 1 deletion server/events/runtime/apply_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ func (a *ApplyStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []stri
return "", fmt.Errorf("no plan found at path %q and workspace %q–did you run plan?", ctx.RepoRelDir, ctx.Workspace)
}

tfApplyCmd := append(append(append([]string{"apply", "-input=false", "-no-color"}, extraArgs...), ctx.CommentArgs...), planPath)
// NOTE: we need to quote the plan path because Bitbucket Server can
// have spaces in its repo owner names which is part of the path.
tfApplyCmd := append(append(append([]string{"apply", "-input=false", "-no-color"}, extraArgs...), ctx.CommentArgs...), fmt.Sprintf("%q", planPath))
var tfVersion *version.Version
if ctx.ProjectConfig != nil && ctx.ProjectConfig.TerraformVersion != nil {
tfVersion = ctx.ProjectConfig.TerraformVersion
Expand Down
7 changes: 4 additions & 3 deletions server/events/runtime/apply_step_runner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestRun_Success(t *testing.T) {
}, []string{"extra", "args"}, tmpDir)
Ok(t, err)
Equals(t, "output", output)
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", planPath}, nil, "workspace")
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", fmt.Sprintf("%q", planPath)}, nil, "workspace")
_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}
Expand Down Expand Up @@ -95,7 +96,7 @@ func TestRun_AppliesCorrectProjectPlan(t *testing.T) {
}, []string{"extra", "args"}, tmpDir)
Ok(t, err)
Equals(t, "output", output)
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", planPath}, nil, "default")
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", fmt.Sprintf("%q", planPath)}, nil, "default")
_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}
Expand Down Expand Up @@ -126,7 +127,7 @@ func TestRun_UsesConfiguredTFVersion(t *testing.T) {
}, []string{"extra", "args"}, tmpDir)
Ok(t, err)
Equals(t, "output", output)
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", planPath}, tfVersion, "workspace")
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", fmt.Sprintf("%q", planPath)}, tfVersion, "workspace")
_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}
10 changes: 6 additions & 4 deletions server/events/runtime/plan_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,17 @@ func (p *PlanStepRunner) buildPlanCmd(ctx models.ProjectCommandContext, extraArg
func (p *PlanStepRunner) tfVars(ctx models.ProjectCommandContext) []string {
// NOTE: not using maps and looping here because we need to keep the
// ordering for testing purposes.
// NOTE: quoting the values because in Bitbucket the owner can have
// spaces, ex -var atlantis_repo_owner="bitbucket owner".
return []string{
"-var",
fmt.Sprintf("%s=%s", "atlantis_user", ctx.User.Username),
fmt.Sprintf("%s=%q", "atlantis_user", ctx.User.Username),
"-var",
fmt.Sprintf("%s=%s", "atlantis_repo", ctx.BaseRepo.FullName),
fmt.Sprintf("%s=%q", "atlantis_repo", ctx.BaseRepo.FullName),
"-var",
fmt.Sprintf("%s=%s", "atlantis_repo_name", ctx.BaseRepo.Name),
fmt.Sprintf("%s=%q", "atlantis_repo_name", ctx.BaseRepo.Name),
"-var",
fmt.Sprintf("%s=%s", "atlantis_repo_owner", ctx.BaseRepo.Owner),
fmt.Sprintf("%s=%q", "atlantis_repo_owner", ctx.BaseRepo.Owner),
"-var",
fmt.Sprintf("%s=%d", "atlantis_pull_num", ctx.Pull.Num),
}
Expand Down
48 changes: 24 additions & 24 deletions server/events/runtime/plan_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ func TestRun_NoWorkspaceIn08(t *testing.T) {
"-out",
"\"/path/default.tfplan\"",
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
Expand Down Expand Up @@ -198,13 +198,13 @@ func TestRun_SwitchesWorkspace(t *testing.T) {
"-out",
"\"/path/workspace.tfplan\"",
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
Expand Down Expand Up @@ -267,13 +267,13 @@ func TestRun_CreatesWorkspace(t *testing.T) {
"-out",
"\"/path/workspace.tfplan\"",
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
Expand Down Expand Up @@ -327,13 +327,13 @@ func TestRun_NoWorkspaceSwitchIfNotNecessary(t *testing.T) {
"-out",
"\"/path/workspace.tfplan\"",
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
Expand Down Expand Up @@ -395,13 +395,13 @@ func TestRun_AddsEnvVarFile(t *testing.T) {
"-out",
fmt.Sprintf("%q", filepath.Join(tmpDir, "workspace.tfplan")),
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
Expand Down Expand Up @@ -456,13 +456,13 @@ func TestRun_UsesDiffPathForProject(t *testing.T) {
"-out",
"\"/path/projectname-default.tfplan\"",
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
Expand Down

0 comments on commit e98caf3

Please sign in to comment.