Skip to content
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

TDRD-22 - Add "condition" to the trust polices #574

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions modules/consignment-api/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ data "aws_iam_policy_document" "consignment_api_ecs_execution" {
"arn:aws:ecr:eu-west-2:${local.ecr_account_number}:repository/consignment-api",
var.aws_guardduty_ecr_arn
]
condition {
test = "StringEquals"
values = [local.ecr_account_number]
variable = "AWS:SourceAccount"
}
}
statement {
actions = ["ecr:GetAuthorizationToken"]
Expand Down
7 changes: 6 additions & 1 deletion modules/consignment-api/templates/allow_iam_db_auth.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"Action": "rds-db:connect",
"Resource": [
"arn:aws:rds-db:eu-west-2:${account_id}:dbuser:${resource_id}/consignment_api_user"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
}
]
}
Expand Down
23 changes: 19 additions & 4 deletions modules/database-migrations/templates/migration_lambda.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"Action": "rds-db:connect",
"Resource": [
"arn:aws:rds-db:eu-west-2:${account_id}:dbuser:${instance_id}/migrations_user"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
},
{
"Effect": "Allow",
Expand All @@ -20,7 +25,12 @@
"arn:aws:s3:::tdr-database-migrations/*",
"arn:aws:s3:::tdr-database-migrations",
"${log_group_arn}"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
},
{
"Effect": "Allow",
Expand All @@ -32,7 +42,12 @@
"ec2:DescribeNetworkInterfaces",
"autoscaling:CompleteLifecycleAction"
],
"Resource": ["*"]
"Resource": ["*"],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
}
]
}
}
5 changes: 5 additions & 0 deletions modules/shared-vpc/flowlogs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ data "aws_iam_policy_document" "tdr_flowlog_policy" {
resources = [
"arn:aws:logs:*:*:log-group:/flowlogs/tdr-vpc-*"
]
condition {
test = "StringEquals"
values = [data.aws_caller_identity.current.account_id]
variable = "AWS:SourceAccount"
}
}
}

Expand Down
17 changes: 16 additions & 1 deletion modules/transfer-frontend/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ data "aws_iam_policy_document" "sns_notifications_publish" {
statement {
actions = ["sns:Publish"]
resources = [var.notification_sns_topic_arn]
condition {
test = "StringEquals"
values = [data.aws_caller_identity.current.account_id]
variable = "AWS:SourceAccount"
}
}
}

Expand All @@ -147,6 +152,11 @@ data "aws_iam_policy_document" "frontend_kms_key_use" {
"kms:GenerateDataKey"
]
resources = [var.notifications_topic_kms_key_arn]
condition {
test = "StringEquals"
values = [data.aws_caller_identity.current.account_id]
variable = "AWS:SourceAccount"
}
}
}

Expand Down Expand Up @@ -193,7 +203,7 @@ resource "aws_iam_policy" "frontend_draft_metadata" {
"modules/transfer-frontend/templates/draft_metadata_policy.json.tpl", {
environment = var.environment,
titleEnvironment = title(var.environment),
account = data.aws_caller_identity.current.account_id,
account_id = data.aws_caller_identity.current.account_id,
kms_bucket_key_arns = var.draft_metadata_s3_kms_keys
})
}
Expand Down Expand Up @@ -225,6 +235,11 @@ data "aws_iam_policy_document" "frontend_ecs_execution" {
"arn:aws:logs:eu-west-2:${data.aws_caller_identity.current.account_id}:log-group:/aws/events/ecs-task-events-${var.environment}:*",
var.aws_guardduty_ecr_arn
]
condition {
test = "StringEquals"
values = [data.aws_caller_identity.current.account_id]
variable = "AWS:SourceAccount"
}
}
statement {
actions = ["ecr:GetAuthorizationToken"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"kms:GenerateDataKey"
],
"Effect": "Allow",
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
},
"Resource": ${kms_bucket_key_arns}
},
{
Expand All @@ -20,7 +25,12 @@
"Resource": [
"arn:aws:s3:::tdr-draft-metadata-${environment}/*",
"arn:aws:s3:::tdr-draft-metadata-${environment}"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
}
],
"Version": "2012-10-17"
Expand Down
1 change: 1 addition & 0 deletions root_backend_checks.tf
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,6 @@ module "backend_checks_step_function" {
notification_lambda_arn = module.notification_lambda.ecr_scan_notification_lambda_arn[0],
backend_checks_bucket_arn = module.backend_lambda_function_bucket.s3_bucket_arn
state_machine_arn = module.backend_checks_step_function.state_machine_arn
account_id = data.aws_caller_identity.current.account_id
})
}
6 changes: 4 additions & 2 deletions root_consignment_export.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module "consignment_export_cloudwatch" {

module "consignment_export_execution_role" {
source = "./tdr-terraform-modules/iam_role"
assume_role_policy = templatefile("./templates/iam_policy/ecs_assume_role_policy.json.tpl", {})
assume_role_policy = templatefile("./templates/iam_policy/ecs_assume_role_policy.json.tpl", { account_id = data.aws_caller_identity.current.account_id })
common_tags = local.common_tags
name = "TDRConsignmentExportECSExecutionRole${title(local.environment)}"
policy_attachments = {
Expand All @@ -41,7 +41,8 @@ module "consignment_export_execution_policy" {
log_group_arn = "${module.consignment_export_cloudwatch.log_group_arn}:*",
file_system_arn = module.export_efs.file_system_arn,
management_account_number = data.aws_ssm_parameter.mgmt_account_number.value,
aws_guardduty_ecr_arn = local.aws_guardduty_ecr_arn
aws_guardduty_ecr_arn = local.aws_guardduty_ecr_arn,
account_id = data.aws_caller_identity.current.account_id
}
)
}
Expand All @@ -51,6 +52,7 @@ module "consignment_export_task_policy" {
name = "TDRConsignmentExportECSTaskPolicy${title(local.environment)}"
policy_string = templatefile(
"${path.module}/templates/iam_policy/consignment_export_task_policy.json.tpl", {
account_id = data.aws_caller_identity.current.account_id
environment = local.environment,
titleEnvironment = title(local.environment),
aws_region = local.region,
Expand Down
1 change: 1 addition & 0 deletions root_github.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ module "run_update_keycloak_execution_policy" {
source = "./tdr-terraform-modules/iam_policy"
name = "TDRKeycloakUpdateECSExecutionPolicy${title(local.environment)}"
policy_string = templatefile("${path.module}/templates/iam_policy/keycloak_update_execution_policy.json.tpl", {
account_id = data.aws_caller_identity.current.account_id
log_group_arn = module.keycloak_update_cloudwatch.log_group_arn,
management_account_number = data.aws_ssm_parameter.mgmt_account_number.value,
aws_guardduty_ecr_arn = local.aws_guardduty_ecr_arn
Expand Down
3 changes: 2 additions & 1 deletion root_keycloak.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module "keycloak_ecs_execution_policy" {
policy_string = templatefile("${path.module}/templates/iam_policy/keycloak_ecs_execution_policy.json.tpl", {
cloudwatch_log_group = module.keycloak_cloudwatch.log_group_arn,
ecr_account_number = local.ecr_account_number,
aws_guardduty_ecr_arn = local.aws_guardduty_ecr_arn
aws_guardduty_ecr_arn = local.aws_guardduty_ecr_arn,
account_id = data.aws_caller_identity.current.account_id
})
}

Expand Down
4 changes: 2 additions & 2 deletions root_transfer_service.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ locals {
module "transfer_service_execution_role" {
count = local.transfer_service_count
source = "./da-terraform-modules/iam_role"
assume_role_policy = templatefile("./templates/iam_policy/ecs_assume_role_policy.json.tpl", {})
assume_role_policy = templatefile("./templates/iam_policy/ecs_assume_role_policy.json.tpl", { account_id = data.aws_caller_identity.current.account_id })
tags = local.common_tags
name = "TDRTransferServiceECSExecutionRole${title(local.environment)}"
policy_attachments = {
Expand All @@ -23,7 +23,7 @@ module "transfer_service_execution_role" {
module "transfer_service_task_role" {
count = local.transfer_service_count
source = "./da-terraform-modules/iam_role"
assume_role_policy = templatefile("./templates/iam_policy/ecs_assume_role_policy.json.tpl", {})
assume_role_policy = templatefile("./templates/iam_policy/ecs_assume_role_policy.json.tpl", { account_id = data.aws_caller_identity.current.account_id })
tags = local.common_tags
name = "TDRTransferServiceECSTaskRole${title(local.environment)}"
policy_attachments = {
Expand Down
35 changes: 30 additions & 5 deletions templates/iam_policy/allow_iam_db_auth.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
"Resource": [
"arn:aws:logs:eu-west-2:${account_id}:log-group:/aws/lambda/${function_name}",
"arn:aws:logs:eu-west-2:${account_id}:log-group:/aws/lambda/${function_name}:log-stream:*"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
},
{
"Effect": "Allow",
Expand All @@ -19,14 +24,24 @@
"ec2:DeleteNetworkInterface",
"ec2:DescribeNetworkInterfaces"
],
"Resource": "*"
"Resource": "*",
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
},
{
"Effect": "Allow",
"Action": "rds-db:connect",
"Resource": [
"arn:aws:rds-db:eu-west-2:${account_id}:dbuser:${resource_id}/${user_name}"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
},
{
"Effect": "Allow",
Expand All @@ -38,14 +53,24 @@
"Resource": [
"arn:aws:s3:::${bucket_name}",
"arn:aws:s3:::${bucket_name}/*"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
},
{
"Effect": "Allow",
"Action": [
"ssm:GetParameter"
],
"Resource": "arn:aws:ssm:eu-west-2:${account_id}:parameter${parameter_name}"
"Resource": "arn:aws:ssm:eu-west-2:${account_id}:parameter${parameter_name}",
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
}
]
}
Expand Down
14 changes: 12 additions & 2 deletions templates/iam_policy/api_gateway_state_machine_policy.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
"Resource": [
"arn:aws:logs:eu-west-2:${account_id}:log-group:*",
"arn:aws:logs:eu-west-2:${account_id}:log-group:*:*"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
},
{
"Effect": "Allow",
Expand All @@ -19,7 +24,12 @@
],
"Resource": [
"${state_machine_arn}"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
}
]
}
21 changes: 18 additions & 3 deletions templates/iam_policy/backend_check_policy.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
"${redacted_files_lambda_arn}",
"${statuses_lambda_arn}",
"${yara_av_v2_lambda_arn}"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
},
{
"Effect": "Allow",
Expand All @@ -32,7 +37,12 @@
"${backend_checks_bucket_arn}/*",
"${backend_checks_bucket_arn}",
"${state_machine_arn}"
]
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
},
{
"Effect": "Allow",
Expand All @@ -42,7 +52,12 @@
"xray:GetSamplingRules",
"xray:GetSamplingTargets"
],
"Resource": "*"
"Resource": "*",
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "${account_id}"
}
}
}
]
}
Loading
Loading