diff --git a/baselines/guardrails/create_interval_policy/main.tf b/baselines/guardrails/create_interval_policy/main.tf new file mode 100644 index 00000000..f3ba03b8 --- /dev/null +++ b/baselines/guardrails/create_interval_policy/main.tf @@ -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" +} \ No newline at end of file diff --git a/baselines/guardrails/create_interval_policy/providers.tf b/baselines/guardrails/create_interval_policy/providers.tf new file mode 100644 index 00000000..3ede1821 --- /dev/null +++ b/baselines/guardrails/create_interval_policy/providers.tf @@ -0,0 +1,11 @@ +terraform { + required_providers { + turbot = { + source = "turbot/turbot" + version = ">= 1.11.0" + } + } +} + +provider "turbot" { +} diff --git a/guardrails_utilities/python_utils/turbot/turbot.py b/guardrails_utilities/python_utils/turbot/turbot.py index 0f972010..8f1a15e1 100644 --- a/guardrails_utilities/python_utils/turbot/turbot.py +++ b/guardrails_utilities/python_utils/turbot/turbot.py @@ -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() diff --git a/policy_packs/aws/iam/check_roles_use_source_identity/README.md b/policy_packs/aws/iam/check_roles_use_source_identity/README.md new file mode 100644 index 00000000..12d07acb --- /dev/null +++ b/policy_packs/aws/iam/check_roles_use_source_identity/README.md @@ -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 +``` \ No newline at end of file diff --git a/policy_packs/aws/iam/check_roles_use_source_identity/main.tf b/policy_packs/aws/iam/check_roles_use_source_identity/main.tf new file mode 100644 index 00000000..c97d609b --- /dev/null +++ b/policy_packs/aws/iam/check_roles_use_source_identity/main.tf @@ -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"] +} diff --git a/policy_packs/aws/iam/check_roles_use_source_identity/policies.tf b/policy_packs/aws/iam/check_roles_use_source_identity/policies.tf new file mode 100644 index 00000000..05e3d31e --- /dev/null +++ b/policy_packs/aws/iam/check_roles_use_source_identity/policies.tf @@ -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 +} + diff --git a/policy_packs/aws/iam/check_roles_use_source_identity/provider.tf b/policy_packs/aws/iam/check_roles_use_source_identity/provider.tf new file mode 100644 index 00000000..297b401d --- /dev/null +++ b/policy_packs/aws/iam/check_roles_use_source_identity/provider.tf @@ -0,0 +1,11 @@ +terraform { + required_providers { + turbot = { + source = "turbot/turbot" + version = ">= 1.11.0" + } + } +} + +provider "turbot" { +} \ No newline at end of file diff --git a/policy_packs/aws/iam/check_roles_use_source_identity/test-resources/main.tf b/policy_packs/aws/iam/check_roles_use_source_identity/test-resources/main.tf new file mode 100644 index 00000000..48af06a0 --- /dev/null +++ b/policy_packs/aws/iam/check_roles_use_source_identity/test-resources/main.tf @@ -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" + } + } + } + ] + }) +} \ No newline at end of file