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

refactor(query): policies for CloudFormation #4540

Merged
merged 2 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions assets/libraries/cloudformation.rego
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ checkAction(currentAction, actionToCompare) {
} else {
is_string(currentAction)
contains(lower(currentAction), actionToCompare)
} else {
is_array(currentAction)
action := currentAction[_]
action == "*"
action == actionToCompare
} else {
is_array(currentAction)
action := currentAction[_]
contains(lower(action), actionToCompare)
}

# Dictionary of UDP ports
Expand Down
20 changes: 20 additions & 0 deletions assets/libraries/common.rego
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,23 @@ get_encryption_if_exists(resource) = encryption {
} else = encryption {
encryption := "unencrypted"
}

get_statement(policy) = st {
is_object(policy.Statement)
st = [policy.Statement]
} else = st {
is_array(policy.Statement)
st = policy.Statement
}

is_allow_effect(statement) {
not valid_key(statement, "Effect")
} else {
statement.Effect == "Allow"
}

get_policy(p) = policy {
policy = json_unmarshal(p)
} else = policy {
policy = p
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package Cx

import data.generic.common as common_lib

CxPolicy[result] {
resource := input.document[i].Resources[name]
resource.Type == "AWS::ECR::Repository"
policy := resource.Properties.RepositoryPolicyText
statement := policy.Statement[_]
statement.Effect == "Allow"
st := common_lib.get_statement(common_lib.get_policy(policy))
statement := st[_]

common_lib.is_allow_effect(statement)
contains(statement.Principal, "*")

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("Resources.%s.Properties.RepositoryPolicyText.Statement.Principal", [name]),
"searchKey": sprintf("Resources.%s.Properties.RepositoryPolicyText", [name]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("Resources.%s.Properties.RepositoryPolicyText.Statement.Principal doesn't contain '*'", [name]),
"keyActualValue": sprintf("Resources.%s.Properties.RepositoryPolicyText.Statement.Principal contains '*'", [name]),
"searchLine": common_lib.build_search_line(["Resource", name, "Properties", "RepositoryPolicyTexts"], []),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{
"queryName": "ECR Repository Is Publicly Accessible",
"severity": "MEDIUM",
"line": 12,
"line": 6,
"fileName": "positive1.yaml"
},
{
"queryName": "ECR Repository Is Publicly Accessible",
"severity": "MEDIUM",
"line": 13,
"line": 7,
"fileName": "positive2.json"
}
]
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package Cx

import data.generic.common as common_lib

CxPolicy[result] {
resource := input.document[i].Resources[name]
resource.Type == "AWS::IAM::Policy"
checkPolicy(resource.Properties.PolicyDocument.Statement[_])

policy := resource.Properties.PolicyDocument
st := common_lib.get_statement(common_lib.get_policy(policy))
statement := st[_]

common_lib.is_allow_effect(statement)
common_lib.equalsOrInArray(statement.Resource, "*")
common_lib.equalsOrInArray(statement.Action, "*")

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("Resources.%s.Properties.PolicyDocument.Statement", [name]),
"searchKey": sprintf("Resources.%s.Properties.PolicyDocumentt", [name]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"searchKey": sprintf("Resources.%s.Properties.PolicyDocumentt", [name]),
"searchKey": sprintf("Resources.%s.Properties.PolicyDocument", [name]),

"issueType": "IncorrectValue",
"keyExpectedValue": "'Resources.Properties.PolicyDocument.Statement' doesn't contain '*'",
"keyActualValue": "'Resources.Properties.PolicyDocument.Statement' contains '*'",
"searchLine": common_lib.build_search_line(["Resource", name, "Properties", "PolicyDocument"], []),
}
}

checkPolicy(pol) {
pol.Effect == "Allow"
pol.Resource == "*"
checkAction(pol.Action)
}

checkAction(act) {
act == "*"
}

checkAction(act) {
act[_] == "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
{
"queryName": "IAM Policies With Full Privileges",
"severity": "HIGH",
"line": 10,
"line": 6,
"fileName": "positive1.yaml"
},
{
"queryName": "IAM Policies With Full Privileges",
"severity": "HIGH",
"line": 23,
"line": 19,
"fileName": "positive1.yaml"
},
{
"queryName": "IAM Policies With Full Privileges",
"severity": "HIGH",
"line": 11,
"line": 7,
"fileName": "positive2.json"
},
{
"queryName": "IAM Policies With Full Privileges",
"severity": "HIGH",
"line": 33,
"line": 29,
"fileName": "positive2.json"
}
]
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package Cx

import data.generic.common as common_lib

CxPolicy[result] {
resource := input.document[i].Resources[name]
resource.Type == "AWS::IAM::Policy"
statements := resource.Properties.PolicyDocument.Statement

contains(statements[_].Action[_], "sts:AssumeRole")

policy := resource.Properties.PolicyDocument
st := common_lib.get_statement(common_lib.get_policy(policy))
statement := st[_]

contains(statements[_].Resource, "*")
common_lib.is_allow_effect(statement)
common_lib.equalsOrInArray(statement.Resource, "*")
common_lib.equalsOrInArray(statement.Action, lower("sts:AssumeRole"))

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("Resources.%s.Properties.PolicyDocument.Statement", [name]),
"searchKey": sprintf("Resources.%s.Properties.PolicyDocument", [name]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("'Resources.%s.Properties.PolicyDocument.Statement' with AssumeRole action does not grant access in all services ('*')", [name]),
"keyActualValue": sprintf("'Resources.%s.Properties.PolicyDocument.Statement' with AssumeRole action is granting access in all services ('*')", [name]),
"searchLine": common_lib.build_search_line(["Resource", name, "Properties", "PolicyDocument"], []),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{
"queryName": "IAM Policy Grants 'AssumeRole' Permission Across All Services",
"severity": "LOW",
"line": 10,
"line": 8,
"fileName": "positive1.yaml"
},
{
"queryName": "IAM Policy Grants 'AssumeRole' Permission Across All Services",
"severity": "LOW",
"line": 10,
"line": 8,
"fileName": "positive2.json"
}
]
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
package Cx

import data.generic.common as common_lib

CxPolicy[result] {
resource := input.document[i].Resources[name]
resource.Type == "AWS::IAM::Policy"

statements := resource.Properties.PolicyDocument.Statement
checkPolicy(statements[s])
policy := resource.Properties.PolicyDocument
st := common_lib.get_statement(common_lib.get_policy(policy))
statement := st[_]

common_lib.is_allow_effect(statement)
not common_lib.equalsOrInArray(statement.Resource, lower("arn:aws:iam::aws:policy/AdministratorAccess"))
common_lib.equalsOrInArray(statement.Action, "*")

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("Resources.%s.Properties.PolicyDocument.Statement.Resource=%s", [name, statements[s].Resource]),
"searchKey": sprintf("Resources.%s.Properties.PolicyDocument", [name]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("'Resources.%s.Properties.PolicyDocument.Statement.Resource=%s' does not have full permissions or is Admin", [name, statements[s].Resource]),
"keyActualValue": sprintf("'Resources.%s.Properties.PolicyDocument.Statement.Resource=%s' has full permissions and is not Admin.", [name, statements[s].Resource]),
"keyExpectedValue": sprintf("'Resources.%s.Properties.PolicyDocument.Statement.Resource' does not have full permissions or is Admin", [name]),
"keyActualValue": sprintf("'Resources.%s.Properties.PolicyDocument.Statement.Resource' has full permissions and is not Admin.", [name]),
"searchLine": common_lib.build_search_line(["Resource", name, "Properties", "PolicyDocument"], []),
}
}

checkPolicy(pol) {
pol.Effect == "Allow"
pol.Resource != "arn:aws:iam::aws:policy/AdministratorAccess"
checkAction(pol.Action)
}

checkAction(act) {
act == "*"
}

checkAction(act) {
act[_] == "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
{
"queryName": "IAM Policy Grants Full Permissions",
"severity": "LOW",
"line": 13,
"line": 8,
"fileName": "positive1.yaml"
},
{
"queryName": "IAM Policy Grants Full Permissions",
"severity": "LOW",
"line": 26,
"fileName": "positive1.yaml",
"queryName": "IAM Policy Grants Full Permissions"
"line": 21,
"fileName": "positive1.yaml"
},
{
"queryName": "IAM Policy Grants Full Permissions",
"severity": "LOW",
"line": 37,
"line": 29,
"fileName": "positive2.json"
},
{
"queryName": "IAM Policy Grants Full Permissions",
"severity": "LOW",
"line": 14,
"line": 9,
"fileName": "positive2.json"
}
]
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
package Cx

import data.generic.common as commonLib
import data.generic.common as common_lib

CxPolicy[result] {
resource := input.document[i].Resources[Name]
resource := input.document[i].Resources[name]
resource.Type == "AWS::IAM::Role"

out := commonLib.json_unmarshal(resource.Properties.AssumeRolePolicyDocument)
aws := out.Statement[_].Principal.AWS
policy := resource.Properties.AssumeRolePolicyDocument
st := common_lib.get_statement(common_lib.get_policy(policy))
statement := st[_]

commonLib.allowsAllPrincipalsToAssume(aws, out.Statement[_])

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("Resources.%s.Properties.AssumeRolePolicyDocument.Statement.Principal.AWS", [Name]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("Resources.%s.Properties.AssumeRolePolicyDocument.Statement.Principal.AWS does not contain ':root'", [Name]),
"keyActualValue": sprintf("Resources.%s.Properties.AssumeRolePolicyDocument.Statement.Principal.AWS contains ':root'", [Name]),
}
}

CxPolicy[result] {
resource := input.document[i].Resources[Name]
resource.Type == "AWS::IAM::Role"

statement := resource.Properties.AssumeRolePolicyDocument.Statement[_]
common_lib.is_allow_effect(statement)
aws := statement.Principal.AWS

commonLib.allowsAllPrincipalsToAssume(aws, statement)
common_lib.allowsAllPrincipalsToAssume(aws, statement)

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("Resources.%s.Properties.AssumeRolePolicyDocument.Statement.Principal.AWS", [Name]),
"searchKey": sprintf("Resources.%s.Properties.AssumeRolePolicyDocument", [name]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("Resources.%s.Properties.AssumeRolePolicyDocument.Statement.Principal.AWS does not contain ':root'", [Name]),
"keyActualValue": sprintf("Resources.%s.Properties.AssumeRolePolicyDocument.Statement.Principal.AWS contains ':root'", [Name]),
"keyExpectedValue": sprintf("Resources.%s.Properties.AssumeRolePolicyDocument.Statement.Principal.AWS does not contain ':root'", [name]),
"keyActualValue": sprintf("Resources.%s.Properties.AssumeRolePolicyDocument.Statement.Principal.AWS contains ':root'", [name]),
"searchLine": common_lib.build_search_line(["Resource", name, "Properties", "AssumeRolePolicyDocument"], []),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{
"queryName": "IAM Role Allows All Principals To Assume",
"severity": "LOW",
"line": 13,
"line": 6,
"fileName": "positive1.yaml"
},
{
"queryName": "IAM Role Allows All Principals To Assume",
"severity": "LOW",
"line": 13,
"line": 7,
"fileName": "positive2.json"
}
]
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package Cx

import data.generic.common as common_lib

CxPolicy[result] {
resources := input.document[i].Resources[name]
resources.Type == "AWS::IoT::Policy"
document := resources.Properties.PolicyDocument
statements = document.Statement
statements[k].Action == "*"

policy := resources.Properties.PolicyDocument
st := common_lib.get_statement(common_lib.get_policy(policy))
statement := st[_]

common_lib.is_allow_effect(statement)
common_lib.equalsOrInArray(statement.Action, "*")

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("Resources.%s.Properties.PolicyDocument.Statement.Effect=%s.Action", [name, statements[k].Effect]),
"searchKey": sprintf("Resources.%s.Properties.PolicyDocument", [name]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("Resources.%s.Properties.PolicyDocument.Statement[%s].Action is not '*'", [name, statements[k].Effect]),
"keyActualValue": sprintf("Resources.%s.Properties.PolicyDocument.Statement[%s].Action is '*'", [name, statements[k].Effect]),
"keyExpectedValue": sprintf("Resources.%s.Properties.PolicyDocument.Statement.Action is not '*'", [name]),
"keyActualValue": sprintf("Resources.%s.Properties.PolicyDocument.Statement.Action is '*'", [name]),
"searchLine": common_lib.build_search_line(["Resource", name, "Properties", "PolicyDocument"], []),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{
"queryName": "IoT Policy Allows Action as Wildcard",
"severity": "MEDIUM",
"line": 11,
"line": 7,
"fileName": "positive1.yaml"
},
{
"queryName": "IoT Policy Allows Action as Wildcard",
"severity": "MEDIUM",
"line": 11,
"line": 6,
"fileName": "positive2.json"
}
]
Loading