|
| 1 | +import { dedent } from 'ts-dedent'; |
| 2 | + |
| 3 | +import { AwsOptions } from '@/generators/addons/aws'; |
| 4 | +import { |
| 5 | + isAwsModuleAdded, |
| 6 | + requireAwsModules, |
| 7 | +} from '@/generators/addons/aws/dependencies'; |
| 8 | +import { |
| 9 | + INFRA_CORE_LOCALS_PATH, |
| 10 | + INFRA_CORE_MAIN_PATH, |
| 11 | + INFRA_CORE_VARIABLES_PATH, |
| 12 | + MODULES_LOCALS_INDICATOR, |
| 13 | +} from '@/generators/terraform/constants'; |
| 14 | +import { appendToFile, copy, injectToFile } from '@/helpers/file'; |
| 15 | + |
| 16 | +import { AWS_TEMPLATE_PATH } from '../constants'; |
| 17 | + |
| 18 | +const vpcFlowLogLocalesContent = ` ###VPC Flow Log### |
| 19 | + vpc_flow_log_s3_bucket_policy = { |
| 20 | + Version = "2012-10-17" |
| 21 | + Statement = [ |
| 22 | + { |
| 23 | + Sid = "AWSLogDeliveryWrite" |
| 24 | + Effect = "Allow" |
| 25 | + Principal = { |
| 26 | + Service = "delivery.logs.amazonaws.com" |
| 27 | + } |
| 28 | + Action = "s3:PutObject" |
| 29 | + Resource = "arn:aws:s3:::\${module.s3_flow_log.aws_s3_bucket_name}/*" |
| 30 | + Condition = { |
| 31 | + StringEquals = { |
| 32 | + "aws:SourceAccount" = data.aws_caller_identity.current.account_id |
| 33 | + "s3:x-amz-acl" = "bucket-owner-full-control" |
| 34 | + } |
| 35 | + ArnLike = { |
| 36 | + "aws:SourceArn" = "arn:aws:logs:\${data.aws_region.current.region}:\${data.aws_caller_identity.current.account_id}:*" |
| 37 | + } |
| 38 | + } |
| 39 | + }, |
| 40 | + { |
| 41 | + Sid = "AWSLogDeliveryAclCheck" |
| 42 | + Effect = "Allow" |
| 43 | + Principal = { |
| 44 | + Service = "delivery.logs.amazonaws.com" |
| 45 | + } |
| 46 | + Action = [ |
| 47 | + "s3:GetBucketAcl", |
| 48 | + "s3:ListBucket" |
| 49 | + ] |
| 50 | + Resource = "arn:aws:s3:::\${module.s3_flow_log.aws_s3_bucket_name}" |
| 51 | + Condition = { |
| 52 | + StringEquals = { |
| 53 | + "aws:SourceAccount" = data.aws_caller_identity.current.account_id |
| 54 | + } |
| 55 | + ArnLike = { |
| 56 | + "aws:SourceArn" = "arn:aws:logs:\${data.aws_region.current.region}:\${data.aws_caller_identity.current.account_id}:*" |
| 57 | + } |
| 58 | + } |
| 59 | + }, |
| 60 | + { |
| 61 | + Effect = "Deny" |
| 62 | + Principal = "*" |
| 63 | + Action = "s3:*" |
| 64 | + Resource = [ |
| 65 | + "arn:aws:s3:::\${module.s3_flow_log.aws_s3_bucket_name}", |
| 66 | + "arn:aws:s3:::\${module.s3_flow_log.aws_s3_bucket_name}/*" |
| 67 | + ] |
| 68 | + Condition = { |
| 69 | + Bool = { |
| 70 | + "aws:SecureTransport" = "false" |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + ] |
| 75 | + }`; |
| 76 | + |
| 77 | +const vpcFlowLogVariablesContent = dedent` |
| 78 | + variable "vpc_flow_log_retention_days" { |
| 79 | + description = "The number of days to retain VPC Flow Logs in S3" |
| 80 | + type = number |
| 81 | + default = 90 |
| 82 | + }`; |
| 83 | + |
| 84 | +const vpcFlowLogModuleContent = dedent` |
| 85 | + module "s3_flow_log" { |
| 86 | + source = "../modules/s3" |
| 87 | +
|
| 88 | + env_namespace = local.env_namespace |
| 89 | + bucket_name = "\${local.env_namespace}-flow-logs-\${data.aws_caller_identity.current.account_id}" |
| 90 | + force_destroy = true |
| 91 | + object_ownership = "BucketOwnerPreferred" |
| 92 | + } |
| 93 | +
|
| 94 | + module "s3_flow_log_policy" { |
| 95 | + source = "../modules/s3/bucket_policy" |
| 96 | +
|
| 97 | + s3_bucket_name = module.s3_flow_log.aws_s3_bucket_name |
| 98 | + s3_bucket_policy = local.vpc_flow_log_s3_bucket_policy |
| 99 | + } |
| 100 | +
|
| 101 | + module "vpc_flow_log" { |
| 102 | + source = "../modules/vpc_flow_log" |
| 103 | +
|
| 104 | + env_namespace = local.env_namespace |
| 105 | + vpc_id = module.vpc.vpc_id |
| 106 | + s3_bucket_name = module.s3_flow_log.aws_s3_bucket_name |
| 107 | + log_retention_days = var.vpc_flow_log_retention_days |
| 108 | + }`; |
| 109 | + |
| 110 | +const applyAwsVpcFlowLog = async (options: AwsOptions) => { |
| 111 | + if (isAwsModuleAdded('vpcFlowLog', options.projectName)) { |
| 112 | + return; |
| 113 | + } |
| 114 | + await requireAwsModules('vpcFlowLog', 'vpc', options); |
| 115 | + |
| 116 | + copy( |
| 117 | + `${AWS_TEMPLATE_PATH}/modules/vpc_flow_log`, |
| 118 | + 'modules/vpc_flow_log', |
| 119 | + options.projectName |
| 120 | + ); |
| 121 | + injectToFile( |
| 122 | + INFRA_CORE_LOCALS_PATH, |
| 123 | + vpcFlowLogLocalesContent, |
| 124 | + options.projectName, |
| 125 | + { |
| 126 | + insertAfter: MODULES_LOCALS_INDICATOR, |
| 127 | + } |
| 128 | + ); |
| 129 | + appendToFile( |
| 130 | + INFRA_CORE_VARIABLES_PATH, |
| 131 | + vpcFlowLogVariablesContent, |
| 132 | + options.projectName |
| 133 | + ); |
| 134 | + appendToFile( |
| 135 | + INFRA_CORE_MAIN_PATH, |
| 136 | + vpcFlowLogModuleContent, |
| 137 | + options.projectName |
| 138 | + ); |
| 139 | +}; |
| 140 | + |
| 141 | +export default applyAwsVpcFlowLog; |
| 142 | +export { vpcFlowLogModuleContent, vpcFlowLogVariablesContent }; |
0 commit comments