fix: remove any duplicate statements in policy input (#9385)

Add support for removing duplicate statements automatically
This commit is contained in:
Harshavardhana
2020-04-17 21:26:42 -07:00
committed by GitHub
parent c4464e36c8
commit 75107d7698
4 changed files with 80 additions and 48 deletions

View File

@@ -86,8 +86,24 @@ func (policy Policy) isValid() error {
}
}
return nil
}
// MarshalJSON - encodes Policy to JSON data.
func (policy Policy) MarshalJSON() ([]byte, error) {
if err := policy.isValid(); err != nil {
return nil, err
}
// subtype to avoid recursive call to MarshalJSON()
type subPolicy Policy
return json.Marshal(subPolicy(policy))
}
func (policy *Policy) dropDuplicateStatements() {
redo:
for i := range policy.Statements {
for _, statement := range policy.Statements[i+1:] {
for j, statement := range policy.Statements[i+1:] {
if policy.Statements[i].Effect != statement.Effect {
continue
}
@@ -107,26 +123,10 @@ func (policy Policy) isValid() error {
if policy.Statements[i].Conditions.String() != statement.Conditions.String() {
continue
}
return Errorf("duplicate principal %v, actions %v, resouces %v found in statements %v, %v",
statement.Principal, statement.Actions,
statement.Resources, policy.Statements[i],
statement)
policy.Statements = append(policy.Statements[:j], policy.Statements[j+1:]...)
goto redo
}
}
return nil
}
// MarshalJSON - encodes Policy to JSON data.
func (policy Policy) MarshalJSON() ([]byte, error) {
if err := policy.isValid(); err != nil {
return nil, err
}
// subtype to avoid recursive call to MarshalJSON()
type subPolicy Policy
return json.Marshal(subPolicy(policy))
}
// UnmarshalJSON - decodes JSON data to Policy.
@@ -143,6 +143,8 @@ func (policy *Policy) UnmarshalJSON(data []byte) error {
return err
}
p.dropDuplicateStatements()
*policy = p
return nil