Skip to content

add policy pack for check_roles_use_source_identity #926

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions baselines/guardrails/create_interval_policy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# https://hub.guardrails.turbot.com/mods/turbot/policies/turbot/interval
resource "turbot_policy_setting" "turbot_interval_snapshot_cmdb" {
resource = "tmod:@turbot/azure-compute#/control/types/snapshotCmdb"
type = "tmod:@turbot/turbot#/policy/types/interval"
value = "days: 1"
}

# https://hub.guardrails.turbot.com/mods/turbot/policies/turbot/interval
resource "turbot_policy_setting" "turbot_interval_snapshot_cmdb" {
resource = "tmod:@turbot/azure-compute#/control/types/snapshotDiscovery"
type = "tmod:@turbot/turbot#/policy/types/interval"
value = "days: 1"
}
11 changes: 11 additions & 0 deletions baselines/guardrails/create_interval_policy/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_providers {
turbot = {
source = "turbot/turbot"
version = ">= 1.11.0"
}
}
}

provider "turbot" {
}
2 changes: 1 addition & 1 deletion guardrails_utilities/python_utils/turbot/turbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(self, custom_config_file, config_profile):

def test_health(self):
try:
r = requests.get(self.health_endpoint)
r = requests.get(self.health_endpoint, verify=False)
except requests.exceptions.ConnectionError:
print("Failed to connect, exiting.")
exit()
Expand Down
109 changes: 109 additions & 0 deletions policy_packs/aws/iam/check_roles_use_source_identity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
categories: ["access management", "security"]
primary_category: "access management"
---

# Check if AWS IAM Roles Use Source Identity

Enforcing that AWS IAM roles have trusted access is essential for ensuring that only authorized entities can assume roles and access resources. This measure restricts role assumption to only allow roles that provide source identity information
when assuming into the role.

This [policy pack](https://turbot.com/guardrails/docs/concepts/policy-packs) can help you configure the following settings for IAM roles:

- Check if iam roles have correctly configured `sts:AssumeRolewithSAML` statements
- Check if iam roles have correctly configured `sts:SetSourceIdentity` statements
- Check if iam those statements are in individual policy statements and not present in the same statement.

## Documentation

- **[Review Policy settings →](https://hub.guardrails.turbot.com/policy-packs/aws_iam_check_roles_use_source_identity/settings)**

## Getting Started

### Requirements

- [Terraform](https://developer.hashicorp.com/terraform/install)
- Guardrails mods:
- [@turbot/aws-iam](https://hub.guardrails.turbot.com/mods/aws/mods/aws-iam)

### Credentials

To create a policy pack through Terraform:

- Ensure you have `Turbot/Admin` permissions (or higher) in Guardrails
- [Create access keys](https://turbot.com/guardrails/docs/guides/iam/access-keys#generate-a-new-guardrails-api-access-key) in Guardrails

And then set your credentials:

```sh
export TURBOT_WORKSPACE=myworkspace.acme.com
export TURBOT_ACCESS_KEY=acce6ac5-access-key-here
export TURBOT_SECRET_KEY=a8af61ec-secret-key-here
```

Please see [Turbot Guardrails Provider authentication](https://registry.terraform.io/providers/turbot/turbot/latest/docs#authentication) for additional authentication methods.

## Usage

### Install Policy Pack

> [!NOTE]
> By default, installed policy packs are not attached to any resources.
>
> Policy packs must be attached to resources in order for their policy settings to take effect.

Clone:

```sh
git clone https://github.com/turbot/guardrails-samples.git
cd guardrails-samples/policy_packs/aws/iam/check_roles_use_source_identity
```

Run the Terraform to create the policy pack in your workspace:

```sh
terraform init
terraform plan
```

Then apply the changes:

```sh
terraform apply
```

### Apply Policy Pack

Log into your Guardrails workspace and [attach the policy pack to a resource](https://turbot.com/guardrails/docs/guides/policy-packs#attach-a-policy-pack-to-a-resource).

If this policy pack is attached to a Guardrails folder, its policies will be applied to all accounts and resources in that folder. The policy pack can also be attached to multiple resources.

For more information, please see [Policy Packs](https://turbot.com/guardrails/docs/concepts/policy-packs).

### Enable Enforcement

> [!TIP]
> You can also update the policy settings in this policy pack directly in the Guardrails console.
>
> Please note your Terraform state file will then become out of sync and the policy settings should then only be managed in the console.

By default, the policies are set to `Check` in the pack's policy settings. To enable automated enforcements, you can switch these policies settings by adding a comment to the `Check` setting and removing the comment from one of the listed enforcement options:

```hcl
# AWS > IAM > Role > Approved
resource "turbot_policy_setting" "aws_iam_role_approved" {
resource = turbot_policy_pack.main.id
type = "tmod:@turbot/aws-iam#/policy/types/roleApproved"
# value = "Check: Approved"
value = "Enforce: Delete unapproved"
# value = "Enforce: Delete unapproved if new"
# value = "Enforce: Quarantine unapproved"
}
```

Then re-apply the changes:

```sh
terraform plan
terraform apply
```
5 changes: 5 additions & 0 deletions policy_packs/aws/iam/check_roles_use_source_identity/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "turbot_policy_pack" "main" {
title = "Check if AWS IAM Roles Use Source Identity"
description = "This guardrail checks that all IAM roles are using correct source identity statements in the role trust policy."
akas = ["aws_iam_check_roles_use_source_identity"]
}
64 changes: 64 additions & 0 deletions policy_packs/aws/iam/check_roles_use_source_identity/policies.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

# AWS > IAM > Role > Approved
resource "turbot_policy_setting" "aws_iam_role_approved" {
resource = turbot_policy_pack.main.id
type = "tmod:@turbot/aws-iam#/policy/types/roleApproved"
value = "Check: Approved"
# value = "Enforce: Delete unapproved"
# value = "Enforce: Delete unapproved if new"
# value = "Enforce: Quarantine unapproved"
}

# AWS > IAM > Role > Approved > Custom
resource "turbot_policy_setting" "aws_iam_role_approved_custom" {
resource = turbot_policy_pack.main.id
type = "tmod:@turbot/aws-iam#/policy/types/roleApprovedCustom"
template_input = <<-EOT
{
role {
AssumeRolePolicyDocument
}
}
EOT
template = <<-EOT
{#- Set default values of custom approval conditions -#}
{%- set assumeRoleApproved = "Not approved" -%}
{%- set assumeRoleApprovedReason = "No AssumeRolewithSAML statement" -%}
{%- set sourceIdentityApproved = "Not approved" -%}
{%- set sourceIdentityApprovedReason = "No SetSourceIdentity statement" -%}
{%- set uniqueStatementApproved = "Approved" -%}
{%- set uniqueStatementApprovedReason = "AssumeRolewithSAML and SetSourceIdentity not present in same statement." -%}

{%- for statement in $.role.AssumeRolePolicyDocument.Statement -%}
{#- statement.Action can either be a string or array, these two statements converts either to an array: -#}
{%- set action_string = statement.Action | string -%}
{%- set actions = action_string.split(",") -%}
{%- if "sts:AssumeRoleWithSAML" in actions -%}
{%- set assumeRoleApproved = "Approved" -%}
{%- set assumeRoleApprovedReason = "AssumeRolewithSAML statement exists" -%}
{%- if (actions | length) > 1 -%}
{%- set uniqueStatementApproved = "Not approved" -%}
{%- set uniqueStatementApprovedReason = "Multiple actions present in AssumeRolewithSAML statement." -%}
{%- endif -%}
{%- endif -%}
{%- if "sts:SetSourceIdentity" in actions -%}
{%- set sourceIdentityApproved = "Approved" -%}
{%- set sourceIdentityApprovedReason = "SetSourceIdentity statement exists" -%}
{%- if (actions | length) > 1 -%}
{%- set uniqueStatementApproved = "Not approved" -%}
{%- set uniqueStatementApprovedReason = "Multiple actions present in SetSourceIdentity statement." -%}
{%- endif -%}
{%- endif -%}
{% endfor -%}
- title: "Assume Role Trust Statement"
result: "{{assumeRoleApproved}}"
message: "{{assumeRoleApprovedReason}}"
- title: "Source Identity Trust Statement"
result: "{{sourceIdentityApproved}}"
message: "{{sourceIdentityApprovedReason}}"
- title: "Unique Statement Contstraint"
result: "{{uniqueStatementApproved}}"
message: "{{uniqueStatementApprovedReason}}"
EOT
}

11 changes: 11 additions & 0 deletions policy_packs/aws/iam/check_roles_use_source_identity/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_providers {
turbot = {
source = "turbot/turbot"
version = ">= 1.11.0"
}
}
}

provider "turbot" {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

provider "aws" {
region = "us-east-1"
}

# 1. Correct Role with Two Distinct Statements
resource "aws_iam_role" "correct" {
name = "correct_role"
assume_role_policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::111222333444:saml-provider/azprod"
},
"Action": "sts:AssumeRoleWithSAML",
"Condition": {
"StringEquals": {
"SAML:aud": "https://signin.aws.amazon.com/saml"
}
}
},
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::111222333444:saml-provider/azprod"
},
"Action": "sts:SetSourceIdentity",
"Condition": {
"StringEquals": {
"SAML:aud": "https://signin.aws.amazon.com/saml"
}
}
}
]
})
}

# 2. Role Without Either Statement
resource "aws_iam_role" "missing_both" {
name = "missing_both_role"
assume_role_policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
})
}

# 3. Role Missing the AssumeRoleWithSAML Statement
resource "aws_iam_role" "missing_assume_role_with_saml" {
name = "missing_assume_role_with_saml_role"
assume_role_policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::111222333444:saml-provider/azprod"
},
"Action": "sts:SetSourceIdentity",
"Condition": {
"StringEquals": {
"SAML:aud": "https://signin.aws.amazon.com/saml"
}
}
}
]
})
}

# 4. Role Missing the STS-SetSourceIdentity Statement
resource "aws_iam_role" "missing_set_source_identity" {
name = "missing_set_source_identity_role"
assume_role_policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::111222333444:saml-provider/azprod"
},
"Action": "sts:AssumeRoleWithSAML",
"Condition": {
"StringEquals": {
"SAML:aud": "https://signin.aws.amazon.com/saml"
}
}
}
]
})
}

# 5. Role with Both Actions in a Single Statement
resource "aws_iam_role" "combined" {
name = "combined_role"
assume_role_policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::111222333444:saml-provider/azprod"
},
"Action": [
"sts:AssumeRoleWithSAML",
"sts:SetSourceIdentity"
],
"Condition": {
"StringEquals": {
"SAML:aud": "https://signin.aws.amazon.com/saml"
}
}
}
]
})
}