# data-perimeter / aws / t1 "Standard" # # The identity perimeter on your resources and the resource perimeter on your # identities, with named outside accounts allowed through, encrypted transport # required, and a tag that exempts roles which must write to outside buckets. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) trusted = length(var.trusted_account_ids) > 0 } data "aws_iam_policy_document" "rcp" { statement { sid = "OnlyOurOrganizationUsesOurData" effect = "Deny" actions = ["s3:*", "kms:*", "sqs:*", "secretsmanager:*"] resources = ["*"] principals { type = "*" identifiers = ["*"] } # Refused only when the caller is outside the organisation AND outside the # trusted accounts AND is not an AWS service acting for you. condition { test = "StringNotEqualsIfExists" variable = "aws:PrincipalOrgID" values = [var.org_id] } dynamic "condition" { for_each = local.trusted ? [1] : [] content { test = "StringNotEqualsIfExists" variable = "aws:PrincipalAccount" values = var.trusted_account_ids } } condition { test = "BoolIfExists" variable = "aws:PrincipalIsAWSService" values = ["false"] } } statement { sid = "EncryptedTransportOnly" effect = "Deny" actions = ["s3:*", "kms:*", "sqs:*", "secretsmanager:*"] resources = ["*"] principals { type = "*" identifiers = ["*"] } condition { test = "BoolIfExists" variable = "aws:SecureTransport" values = ["false"] } } } data "aws_iam_policy_document" "scp" { statement { sid = "OurIdentitiesWriteOnlyToOurBuckets" effect = "Deny" actions = ["s3:PutObject", "s3:PutObjectAcl", "s3:ReplicateObject", "s3:RestoreObject"] resources = ["*"] condition { test = "StringNotEqualsIfExists" variable = "aws:ResourceOrgID" values = [var.org_id] } dynamic "condition" { for_each = local.trusted ? [1] : [] content { test = "StringNotEqualsIfExists" variable = "aws:ResourceAccount" values = var.trusted_account_ids } } condition { test = "StringNotEqualsIfExists" variable = "aws:PrincipalTag/${var.exempt_tag}" values = ["true"] } } # The exemption tag is only as safe as the ability to set it. statement { sid = "ProtectTheExemptionTag" effect = "Deny" # Creating a role or user with the tag already on it would skip the tag # calls, so creation is covered too. actions = ["iam:TagRole", "iam:UntagRole", "iam:TagUser", "iam:UntagUser", "iam:CreateRole", "iam:CreateUser"] resources = ["*"] condition { test = "ForAnyValue:StringEquals" variable = "aws:TagKeys" values = [var.exempt_tag] } } } resource "aws_organizations_policy" "rcp" { name = "data-perimeter-identities" description = "Only this organisation and named accounts can use its S3, KMS, SQS, and Secrets Manager resources, over encrypted connections." type = "RESOURCE_CONTROL_POLICY" content = data.aws_iam_policy_document.rcp.json } resource "aws_organizations_policy" "scp" { name = "data-perimeter-resources" description = "Identities here cannot write to S3 outside the organisation or named accounts, unless tagged ${var.exempt_tag}=true." type = "SERVICE_CONTROL_POLICY" content = data.aws_iam_policy_document.scp.json } resource "aws_organizations_policy_attachment" "rcp" { for_each = toset(var.target_ids) policy_id = aws_organizations_policy.rcp.id target_id = each.value } resource "aws_organizations_policy_attachment" "scp" { for_each = toset(var.target_ids) policy_id = aws_organizations_policy.scp.id target_id = each.value }