Skip to content

Commit

Permalink
Merge pull request openshift#739 from tbrisker/pdcreate
Browse files Browse the repository at this point in the history
Add helpers for creating a policy document and allowing actions
  • Loading branch information
openshift-merge-robot authored Jun 13, 2022
2 parents 2db3b60 + 896172b commit f79b478
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/aws/policy_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type PolicyStatement struct {
// federated user to which you would like to allow or deny access. If you are creating an
// IAM permissions policy to attach to a user or role, you cannot include this element.
// The principal is implied as that user or role.
Principal PolicyStatementPrincipal `json:"Principal,omitempty"`
Principal *PolicyStatementPrincipal `json:"Principal,omitempty"`
// Include a list of actions that the policy allows or denies.
// (i.e. ec2:StartInstances, iam:ChangePassword)
Action interface{} `json:"Action,omitempty"`
Expand All @@ -57,6 +57,10 @@ type PolicyStatementPrincipal struct {
Federated string `json:"Federated,omitempty"`
}

func NewPolicyDocument() *PolicyDocument {
return &PolicyDocument{Version: "2012-10-17"}
}

func ParsePolicyDocument(doc string) (PolicyDocument, error) {
policy := PolicyDocument{}
err := json.Unmarshal([]byte(doc), &policy)
Expand All @@ -83,7 +87,16 @@ func (p *PolicyStatement) GetAWSPrincipals() []string {
return awsArr
}

func (p *PolicyDocument) AllowsAction(wanted string) bool {
// AllowActions adds a statement to a policy allowing the provided actions for all Resources.
// If you need a more compilex statement it is better to construct it manually.
func (p *PolicyDocument) AllowActions(actions ...string) {
statement := PolicyStatement{Effect: "Allow", Action: actions, Resource: "*"}
p.Statement = append(p.Statement, statement)
}

// IsActionAllowed checks if any of the statements in the document allows the wanted action.
// It does not take into account Resource or Principal constraints on the action.
func (p *PolicyDocument) IsActionAllowed(wanted string) bool {
statements := p.Statement
if len(statements) == 0 {
return false
Expand Down

0 comments on commit f79b478

Please sign in to comment.