# data-perimeter / aws / t0 "Quick start"
#
# Two policies that keep data inside your AWS organisation, whatever any one
# bucket or key policy says:
#   - a resource control policy: nobody outside the organisation can use your
#     S3 buckets, KMS keys, SQS queues, or Secrets Manager secrets
#   - a service control policy: your own identities cannot write to S3 buckets
#     outside the organisation, which is how stolen credentials usually copy
#     data out
#
# Run it in the management account, with resource control policies switched
# on for the organisation (see the prerequisites). Attach to a test OU first.
#
# Change the two values below, then:
#   terraform init && terraform apply
#
# Verify, from a member account under the target:
#   aws s3 cp /etc/hostname s3://<a bucket in another organisation you can write to>/
#   Expect: AccessDenied ... with an explicit deny in a service control policy.

locals {
  org_id    = "o-xxxxxxxxxx"     # change me: aws organizations describe-organization
  target_id = "ou-xxxx-xxxxxxxx" # change me: an OU ID, or a 12-digit account ID
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

provider "aws" {}

# On your resources: callers from outside the organisation are refused. AWS
# services acting for you, such as CloudTrail writing to its bucket, are left
# alone.
data "aws_iam_policy_document" "rcp" {
  statement {
    sid       = "OnlyOurOrganizationUsesOurData"
    effect    = "Deny"
    actions   = ["s3:*", "kms:*", "sqs:*", "secretsmanager:*"]
    resources = ["*"]
    principals {
      type        = "*"
      identifiers = ["*"]
    }
    condition {
      test     = "StringNotEqualsIfExists"
      variable = "aws:PrincipalOrgID"
      values   = [local.org_id]
    }
    condition {
      test     = "BoolIfExists"
      variable = "aws:PrincipalIsAWSService"
      values   = ["false"]
    }
  }
}

# On your identities: writes to buckets outside the organisation are refused.
data "aws_iam_policy_document" "scp" {
  statement {
    sid       = "OurIdentitiesWriteOnlyToOurBuckets"
    effect    = "Deny"
    actions   = ["s3:PutObject", "s3:PutObjectAcl", "s3:ReplicateObject"]
    resources = ["*"]
    condition {
      test     = "StringNotEqualsIfExists"
      variable = "aws:ResourceOrgID"
      values   = [local.org_id]
    }
  }
}

resource "aws_organizations_policy" "rcp" {
  name        = "data-perimeter-identities"
  description = "Only principals in this organisation can use its S3, KMS, SQS, and Secrets Manager resources."
  type        = "RESOURCE_CONTROL_POLICY"
  content     = data.aws_iam_policy_document.rcp.json
}

resource "aws_organizations_policy" "scp" {
  name        = "data-perimeter-resources"
  description = "Identities in this organisation cannot write to S3 buckets outside it."
  type        = "SERVICE_CONTROL_POLICY"
  content     = data.aws_iam_policy_document.scp.json
}

resource "aws_organizations_policy_attachment" "rcp" {
  policy_id = aws_organizations_policy.rcp.id
  target_id = local.target_id
}

resource "aws_organizations_policy_attachment" "scp" {
  policy_id = aws_organizations_policy.scp.id
  target_id = local.target_id
}
