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

fix(team-authz): add missing envs into external team allowlist runner context #4951

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
50 changes: 50 additions & 0 deletions server/events/command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/runatlantis/atlantis/server/core/config/valid"
"github.com/runatlantis/atlantis/server/core/db"
"github.com/runatlantis/atlantis/server/core/locking"
"github.com/runatlantis/atlantis/server/core/runtime"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/metrics"
Expand Down Expand Up @@ -335,6 +336,55 @@ func TestRunCommentCommand_TeamAllowListChecker(t *testing.T) {
vcsClient.VerifyWasCalledOnce().CreateComment(
Any[logging.SimpleLogging](), Eq(testdata.GithubRepo), Eq(modelPull.Num), Eq("Ran Plan for 0 projects:"), Eq("plan"))
})

t.Run("check environment variables", func(t *testing.T) {
vcsClient := setup(t)

ch.TeamAllowlistChecker = &events.ExternalTeamAllowlistChecker{
Command: strings.Join([]string{
"script() {",
"[ \"$1\" != \"arg1\" ] && exit 1",
"[ \"$2\" != \"arg2\" ] && exit 1",
"[ \"$3\" != \"apply\" ] && exit 1",
fmt.Sprintf("[ \"$4\" != \"%s\" ] && exit 1", testdata.GithubRepo.FullName),
fmt.Sprintf("[ \"$5\" != \"%s/%s\" ] && exit 1", testdata.GithubRepo.Owner, testdata.User.Teams[0]),
"[ -z \"${BASE_REPO_NAME}\" ] && exit 1",
"[ -z \"${BASE_REPO_OWNER}\" ] && exit 1",
"[ -z \"${COMMAND_NAME}\" ] && exit 1",
"[ -z \"${USER_NAME}\" ] && exit 1",
"[ -z \"${BASE_BRANCH_NAME}\" ] && exit 1",
"[ -z \"${COMMENT_ARGS}\" ] && exit 1",
"[ -z \"${HEAD_REPO_NAME}\" ] && exit 1",
"[ -z \"${HEAD_REPO_OWNER}\" ] && exit 1",
"[ -z \"${HEAD_BRANCH_NAME}\" ] && exit 1",
"[ -z \"${HEAD_COMMIT}\" ] && exit 1",
"[ -z \"${PROJECT_NAME}\" ] && exit 1",
"[ -z \"${PULL_NUM}\" ] && exit 1",
"[ -z \"${PULL_URL}\" ] && exit 1",
"[ -z \"${PULL_AUTHOR}\" ] && exit 1",
"[ -z \"${REPO_ROOT}\" ] && exit 1",
"[ -z \"${REPO_REL_PATH}\" ] && exit 1",
"echo pass",
"}",
"script",
}, "\n"),
ExtraArgs: []string{"arg1", "arg2"},
ExternalTeamAllowlistRunner: &runtime.DefaultExternalTeamAllowlistRunner{},
}
var pull github.PullRequest
modelPull := models.PullRequest{
BaseRepo: testdata.GithubRepo,
State: models.OpenPullState,
}
When(githubGetter.GetPullRequest(Any[logging.SimpleLogging](), Eq(testdata.GithubRepo), Eq(testdata.Pull.Num))).ThenReturn(&pull, nil)
When(eventParsing.ParseGithubPull(Any[logging.SimpleLogging](), Eq(&pull))).ThenReturn(modelPull, modelPull.BaseRepo, testdata.GithubRepo, nil)
When(vcsClient.GetTeamNamesForUser(Eq(testdata.GithubRepo), Eq(testdata.User))).ThenReturn(testdata.User.Teams, nil)

ch.RunCommentCommand(testdata.GithubRepo, nil, nil, testdata.User, testdata.Pull.Num, &events.CommentCommand{Name: command.Apply})
vcsClient.VerifyWasCalledOnce().GetTeamNamesForUser(testdata.GithubRepo, testdata.User)
vcsClient.VerifyWasCalledOnce().CreateComment(
Any[logging.SimpleLogging](), Eq(testdata.GithubRepo), Eq(modelPull.Num), Eq("Ran Apply for 0 projects:"), Eq("apply"))
})
}

func TestRunCommentCommand_ForkPRDisabled(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions server/events/models/testdata/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ var GitlabRepo = models.Repo{

var User = models.User{
Username: "lkysow",
Teams: []string{
"atlantis-team",
},
}

var projectName = "test-project"
Expand Down
Loading