forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add GitHub team allowlist configuration option (runatlantis#1694)
* Add GitHub team allowlist configuration option Co-authored-by: PePe (Jose) Amengual <[email protected]> Co-authored-by: Troy Neeriemer <[email protected]> Co-authored-by: Ted Roby <[email protected]> Co-authored-by: Paul Erickson <[email protected]> * Check team allowlist in command runner, rather than event controller * Remove unneeded trimming * Test wildcard groups and commands * Improve error logging Co-authored-by: PePe (Jose) Amengual <[email protected]> Co-authored-by: Troy Neeriemer <[email protected]> Co-authored-by: Ted Roby <[email protected]>
- Loading branch information
Showing
16 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package events | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// Wildcard matches all teams and all commands | ||
const wildcard = "*" | ||
|
||
// mapOfStrings is an alias for map[string]string | ||
type mapOfStrings map[string]string | ||
|
||
// TeamAllowlistChecker implements checking the teams and the operations that the members | ||
// of a particular team are allowed to perform | ||
type TeamAllowlistChecker struct { | ||
rules []mapOfStrings | ||
} | ||
|
||
// NewTeamAllowlistChecker constructs a new checker | ||
func NewTeamAllowlistChecker(allowlist string) (*TeamAllowlistChecker, error) { | ||
var rules []mapOfStrings | ||
pairs := strings.Split(allowlist, ",") | ||
if pairs[0] != "" { | ||
for _, pair := range pairs { | ||
values := strings.Split(pair, ":") | ||
team := strings.TrimSpace(values[0]) | ||
command := strings.TrimSpace(values[1]) | ||
m := mapOfStrings{team: command} | ||
rules = append(rules, m) | ||
} | ||
} | ||
return &TeamAllowlistChecker{ | ||
rules: rules, | ||
}, nil | ||
} | ||
|
||
// IsCommandAllowedForTeam returns true if the team is allowed to execute the command | ||
// and false otherwise. | ||
func (checker *TeamAllowlistChecker) IsCommandAllowedForTeam(team string, command string) bool { | ||
for _, rule := range checker.rules { | ||
for key, value := range rule { | ||
if (key == wildcard || strings.EqualFold(key, team)) && (value == wildcard || strings.EqualFold(value, command)) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// IsCommandAllowedForAnyTeam returns true if any of the teams is allowed to execute the command | ||
// and false otherwise. | ||
func (checker *TeamAllowlistChecker) IsCommandAllowedForAnyTeam(teams []string, command string) bool { | ||
if len(teams) == 0 { | ||
for _, rule := range checker.rules { | ||
for key, value := range rule { | ||
if (key == wildcard) && (value == wildcard || strings.EqualFold(value, command)) { | ||
return true | ||
} | ||
} | ||
} | ||
} else { | ||
for _, t := range teams { | ||
if checker.IsCommandAllowedForTeam(t, command) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package events_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/runatlantis/atlantis/server/events" | ||
. "github.com/runatlantis/atlantis/testing" | ||
) | ||
|
||
func TestNewTeamAllowListChecker(t *testing.T) { | ||
allowlist := `bob:plan, dave:apply` | ||
_, err := events.NewTeamAllowlistChecker(allowlist) | ||
Ok(t, err) | ||
} | ||
|
||
func TestIsCommandAllowedForTeam(t *testing.T) { | ||
allowlist := `bob:plan, dave:apply, connie:plan, connie:apply` | ||
checker, err := events.NewTeamAllowlistChecker(allowlist) | ||
Ok(t, err) | ||
Equals(t, true, checker.IsCommandAllowedForTeam("connie", "plan")) | ||
Equals(t, true, checker.IsCommandAllowedForTeam("connie", "apply")) | ||
Equals(t, true, checker.IsCommandAllowedForTeam("dave", "apply")) | ||
Equals(t, true, checker.IsCommandAllowedForTeam("bob", "plan")) | ||
Equals(t, false, checker.IsCommandAllowedForTeam("bob", "apply")) | ||
} | ||
|
||
func TestIsCommandAllowedForAnyTeam(t *testing.T) { | ||
allowlist := `alpha:plan,beta:release,*:unlock,nobody:*` | ||
teams := []string{`alpha`, `beta`} | ||
checker, err := events.NewTeamAllowlistChecker(allowlist) | ||
Ok(t, err) | ||
Equals(t, true, checker.IsCommandAllowedForAnyTeam(teams, `plan`)) | ||
Equals(t, true, checker.IsCommandAllowedForAnyTeam(teams, `release`)) | ||
Equals(t, true, checker.IsCommandAllowedForAnyTeam(teams, `unlock`)) | ||
Equals(t, false, checker.IsCommandAllowedForAnyTeam(teams, `noop`)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.