Files
atlantis/server/core/config/valid/policies.go
Ross Strickland 684f2fa7e0 feat(policies): Add granular policy_sets (#3086)
* Initial work.

* Periodic push.

* Fmt and start adding args to approve_policies cmd.

* keep funcs for now.

* Periodic push.

* Periodic push.

* fmt.

* Move approve policies logic to project_command_runner.

* update some tests

* More test fixes.

* update more tests. fix som logic.

* more tests. add additional info to common data for custom templates.

* fix apply with policies bug. update more tests/fmt

* file perms

* fix error parsing for conftest results.

* Update more tests and linting.

* update documentation.

* Address no-fail case. Address comments.

* Forgot changes.

* fix markdown renderer

* Fix policy fail logic. remove uneeded tmpl var

* targeted policy approvals fix

* Address PR comments.

* empty commit to trigger build

---------

Co-authored-by: PePe Amengual <jose.amengual@gmail.com>
Co-authored-by: rkstrickland <ross.strickland@instacart.com>
Co-authored-by: Dylan Page <dylan.page@autodesk.com>
2023-04-20 21:53:46 -04:00

69 lines
1.4 KiB
Go

package valid
import (
"strings"
version "github.com/hashicorp/go-version"
)
const (
LocalPolicySet string = "local"
GithubPolicySet string = "github"
)
// PolicySets defines version of policy checker binary(conftest) and a list of
// PolicySet objects. PolicySets struct is used by PolicyCheck workflow to build
// context to enforce policies.
type PolicySets struct {
Version *version.Version
Owners PolicyOwners
ApproveCount int
PolicySets []PolicySet
}
type PolicyOwners struct {
Users []string
Teams []string
}
type PolicySet struct {
Source string
Path string
Name string
ApproveCount int
Owners PolicyOwners
}
func (p *PolicySets) HasPolicies() bool {
return len(p.PolicySets) > 0
}
// Check if any level of policy owners includes teams
func (p *PolicySets) HasTeamOwners() bool {
hasTeamOwners := len(p.Owners.Teams) > 0
for _, policySet := range p.PolicySets {
if len(policySet.Owners.Teams) > 0 {
hasTeamOwners = true
}
}
return hasTeamOwners
}
func (o *PolicyOwners) IsOwner(username string, userTeams []string) bool {
for _, uname := range o.Users {
if strings.EqualFold(uname, username) {
return true
}
}
for _, orgTeamName := range o.Teams {
for _, userTeamName := range userTeams {
if strings.EqualFold(orgTeamName, userTeamName) {
return true
}
}
}
return false
}