|
| 1 | +################################################################################ |
| 2 | +# Permission Boundary: javabin-developer-boundary |
| 3 | +# |
| 4 | +# This is the cornerstone of the IAM security model. Any role carrying this |
| 5 | +# boundary can only create roles that also carry it (self-replicating). |
| 6 | +# |
| 7 | +# The boundary defines the maximum possible permissions for any non-platform |
| 8 | +# role. Hard denies here cannot be overridden by any policy attached to |
| 9 | +# roles that carry this boundary. |
| 10 | +# |
| 11 | +# IMPORTANT: This resource lives in terraform/org/ (human-applied) because |
| 12 | +# the boundary's self-protection denies iam:CreatePolicyVersion on itself. |
| 13 | +# The CI pipeline (which carries the boundary) cannot modify it. |
| 14 | +################################################################################ |
| 15 | + |
| 16 | +resource "aws_iam_policy" "developer_boundary" { |
| 17 | + name = "${var.project}-developer-boundary" |
| 18 | + description = "Permission boundary for all non-platform roles. Self-replicating: roles with this boundary can only create roles that also carry it." |
| 19 | + |
| 20 | + policy = jsonencode({ |
| 21 | + Version = "2012-10-17" |
| 22 | + Statement = [ |
| 23 | + ######################################################################## |
| 24 | + # Allow everything not explicitly denied below |
| 25 | + ######################################################################## |
| 26 | + { |
| 27 | + Sid = "AllowAll" |
| 28 | + Effect = "Allow" |
| 29 | + Action = "*" |
| 30 | + Resource = "*" |
| 31 | + }, |
| 32 | + |
| 33 | + ######################################################################## |
| 34 | + # Self-replicating: deny creating/modifying roles without this boundary |
| 35 | + ######################################################################## |
| 36 | + { |
| 37 | + Sid = "DenyRolesWithoutBoundary" |
| 38 | + Effect = "Deny" |
| 39 | + Action = [ |
| 40 | + "iam:CreateRole", |
| 41 | + "iam:PutRolePermissionsBoundary" |
| 42 | + ] |
| 43 | + Resource = "*" |
| 44 | + Condition = { |
| 45 | + StringNotEquals = { |
| 46 | + "iam:PermissionsBoundary" = "arn:aws:iam::${var.aws_account_id}:policy/${var.project}-developer-boundary" |
| 47 | + } |
| 48 | + } |
| 49 | + }, |
| 50 | + |
| 51 | + ######################################################################## |
| 52 | + # Deny creating IAM users and access keys (console/programmatic) |
| 53 | + ######################################################################## |
| 54 | + { |
| 55 | + Sid = "DenyIAMUserCreation" |
| 56 | + Effect = "Deny" |
| 57 | + Action = [ |
| 58 | + "iam:CreateUser", |
| 59 | + "iam:CreateLoginProfile", |
| 60 | + "iam:UpdateLoginProfile", |
| 61 | + "iam:CreateAccessKey", |
| 62 | + "iam:DeleteAccountPasswordPolicy", |
| 63 | + "iam:CreateVirtualMFADevice", |
| 64 | + "iam:DeactivateMFADevice" |
| 65 | + ] |
| 66 | + Resource = "*" |
| 67 | + }, |
| 68 | + |
| 69 | + ######################################################################## |
| 70 | + # Deny modifying or deleting this boundary policy itself |
| 71 | + ######################################################################## |
| 72 | + { |
| 73 | + Sid = "DenyBoundaryTampering" |
| 74 | + Effect = "Deny" |
| 75 | + Action = [ |
| 76 | + "iam:DeletePolicy", |
| 77 | + "iam:DeletePolicyVersion", |
| 78 | + "iam:CreatePolicyVersion", |
| 79 | + "iam:SetDefaultPolicyVersion" |
| 80 | + ] |
| 81 | + Resource = "arn:aws:iam::${var.aws_account_id}:policy/${var.project}-developer-boundary" |
| 82 | + }, |
| 83 | + { |
| 84 | + Sid = "DenyDeleteRoleBoundary" |
| 85 | + Effect = "Deny" |
| 86 | + Action = [ |
| 87 | + "iam:DeleteRolePermissionsBoundary" |
| 88 | + ] |
| 89 | + Resource = "*" |
| 90 | + }, |
| 91 | + |
| 92 | + ######################################################################## |
| 93 | + # Deny IAM Identity Center, Organizations, SCPs |
| 94 | + ######################################################################## |
| 95 | + { |
| 96 | + Sid = "DenyIdentityCenterAndOrgs" |
| 97 | + Effect = "Deny" |
| 98 | + Action = [ |
| 99 | + "organizations:*", |
| 100 | + "sso:*", |
| 101 | + "sso-directory:*", |
| 102 | + "identitystore:*", |
| 103 | + "account:*" |
| 104 | + ] |
| 105 | + Resource = "*" |
| 106 | + }, |
| 107 | + |
| 108 | + ######################################################################## |
| 109 | + # Deny disabling/deleting protective services |
| 110 | + ######################################################################## |
| 111 | + { |
| 112 | + Sid = "DenyProtectiveServicesTampering" |
| 113 | + Effect = "Deny" |
| 114 | + Action = [ |
| 115 | + "guardduty:DeleteDetector", |
| 116 | + "guardduty:DeleteMembers", |
| 117 | + "guardduty:DisassociateFromMasterAccount", |
| 118 | + "guardduty:UpdateDetector", |
| 119 | + "config:DeleteConfigurationRecorder", |
| 120 | + "config:StopConfigurationRecorder", |
| 121 | + "config:DeleteDeliveryChannel", |
| 122 | + "config:DeleteRetentionConfiguration", |
| 123 | + "securityhub:DisableSecurityHub", |
| 124 | + "securityhub:DeleteMembers", |
| 125 | + "securityhub:DisassociateFromMasterAccount", |
| 126 | + "cloudtrail:DeleteTrail", |
| 127 | + "cloudtrail:StopLogging", |
| 128 | + "cloudtrail:UpdateTrail", |
| 129 | + "cloudtrail:PutEventSelectors" |
| 130 | + ] |
| 131 | + Resource = "*" |
| 132 | + }, |
| 133 | + |
| 134 | + ######################################################################## |
| 135 | + # Deny platform networking: VPC, subnets, IGW, NAT gateway |
| 136 | + ######################################################################## |
| 137 | + { |
| 138 | + Sid = "DenyPlatformNetworking" |
| 139 | + Effect = "Deny" |
| 140 | + Action = [ |
| 141 | + "ec2:CreateVpc", |
| 142 | + "ec2:DeleteVpc", |
| 143 | + "ec2:ModifyVpcAttribute", |
| 144 | + "ec2:CreateSubnet", |
| 145 | + "ec2:DeleteSubnet", |
| 146 | + "ec2:CreateInternetGateway", |
| 147 | + "ec2:DeleteInternetGateway", |
| 148 | + "ec2:AttachInternetGateway", |
| 149 | + "ec2:DetachInternetGateway", |
| 150 | + "ec2:CreateNatGateway", |
| 151 | + "ec2:DeleteNatGateway", |
| 152 | + "ec2:CreateRouteTable", |
| 153 | + "ec2:DeleteRouteTable" |
| 154 | + ] |
| 155 | + Resource = "*" |
| 156 | + }, |
| 157 | + |
| 158 | + ######################################################################## |
| 159 | + # Protect platform security groups |
| 160 | + # |
| 161 | + # Teams CAN create security groups (needed for RDS, custom services). |
| 162 | + # Teams CANNOT modify or delete platform-owned security groups. |
| 163 | + # Platform SGs are named javabin-* (e.g., javabin-alb-sg, javabin-ecs-tasks-sg). |
| 164 | + # App SGs use app-name prefix (e.g., moresleep-rds-sg). |
| 165 | + ######################################################################## |
| 166 | + { |
| 167 | + Sid = "DenyPlatformSecurityGroups" |
| 168 | + Effect = "Deny" |
| 169 | + Action = [ |
| 170 | + "ec2:DeleteSecurityGroup", |
| 171 | + "ec2:AuthorizeSecurityGroupIngress", |
| 172 | + "ec2:RevokeSecurityGroupIngress", |
| 173 | + "ec2:AuthorizeSecurityGroupEgress", |
| 174 | + "ec2:RevokeSecurityGroupEgress", |
| 175 | + "ec2:ModifySecurityGroupRules", |
| 176 | + ] |
| 177 | + Resource = "arn:aws:ec2:${var.region}:${var.aws_account_id}:security-group/*" |
| 178 | + Condition = { |
| 179 | + StringLike = { |
| 180 | + "ec2:ResourceTag/Name" = "${var.project}-*" |
| 181 | + } |
| 182 | + } |
| 183 | + }, |
| 184 | + |
| 185 | + ######################################################################## |
| 186 | + # Deny platform ECS cluster, ALB, ACM certs |
| 187 | + ######################################################################## |
| 188 | + { |
| 189 | + Sid = "DenyPlatformECSCluster" |
| 190 | + Effect = "Deny" |
| 191 | + Action = [ |
| 192 | + "ecs:DeleteCluster", |
| 193 | + "ecs:UpdateCluster" |
| 194 | + ] |
| 195 | + Resource = "arn:aws:ecs:${var.region}:${var.aws_account_id}:cluster/${var.project}-platform" |
| 196 | + }, |
| 197 | + { |
| 198 | + Sid = "DenyPlatformALB" |
| 199 | + Effect = "Deny" |
| 200 | + Action = [ |
| 201 | + "elasticloadbalancing:DeleteLoadBalancer", |
| 202 | + "elasticloadbalancing:ModifyLoadBalancerAttributes" |
| 203 | + ] |
| 204 | + Resource = "arn:aws:elasticloadbalancing:${var.region}:${var.aws_account_id}:loadbalancer/app/${var.project}-*" |
| 205 | + }, |
| 206 | + { |
| 207 | + Sid = "DenyPlatformACM" |
| 208 | + Effect = "Deny" |
| 209 | + Action = [ |
| 210 | + "acm:DeleteCertificate" |
| 211 | + ] |
| 212 | + Resource = "arn:aws:acm:${var.region}:${var.aws_account_id}:certificate/*" |
| 213 | + }, |
| 214 | + |
| 215 | + ######################################################################## |
| 216 | + # Deny access to state and CI artifact buckets/tables |
| 217 | + ######################################################################## |
| 218 | + { |
| 219 | + Sid = "DenyStateBuckets" |
| 220 | + Effect = "Deny" |
| 221 | + Action = [ |
| 222 | + "s3:DeleteBucket", |
| 223 | + "s3:PutBucketPolicy", |
| 224 | + "s3:DeleteBucketPolicy", |
| 225 | + "s3:PutBucketVersioning", |
| 226 | + "s3:PutEncryptionConfiguration" |
| 227 | + ] |
| 228 | + Resource = [ |
| 229 | + "arn:aws:s3:::${var.project}-terraform-*", |
| 230 | + "arn:aws:s3:::${var.project}-ci-*" |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + Sid = "DenyStateTables" |
| 235 | + Effect = "Deny" |
| 236 | + Action = [ |
| 237 | + "dynamodb:DeleteTable", |
| 238 | + "dynamodb:UpdateTable" |
| 239 | + ] |
| 240 | + Resource = "arn:aws:dynamodb:${var.region}:${var.aws_account_id}:table/${var.project}-terraform-*" |
| 241 | + } |
| 242 | + ] |
| 243 | + }) |
| 244 | + |
| 245 | + tags = { |
| 246 | + Name = "${var.project}-developer-boundary" |
| 247 | + } |
| 248 | +} |
0 commit comments