|
| 1 | +#----------------------------------------------------------------------------------------------------------------------- |
| 2 | +# The only resource needed to make Sysdig's backend start to fetch data from the CloudTrail associated s3 bucket is a |
| 3 | +# properly set AWS IAM Role. Sysdig's trusted identity act as the Principal in the assume role Policy, namely the role |
| 4 | +# that the backend will use to assume the Client's role. At that point, given the permission set granted to the newly |
| 5 | +# created Role in the Client's account, Sysdig's backend will be able to perform all the required actions in order to |
| 6 | +# retrieve the log files that are automatically published in the target s3 bucket. |
| 7 | +# |
| 8 | +# Note: this setup assumes that the Customer has already properly set up an AWS CloudTrail Trail and the associated bucket. |
| 9 | +# Sysdig's Secure UI provides the necessary information to make the Customer perform the |
| 10 | +# required setup operations before applying the Terraform module. |
| 11 | +#----------------------------------------------------------------------------------------------------------------------- |
| 12 | + |
| 13 | +#----------------------------------------------------------------------------------------- |
| 14 | +# Fetch the data sources |
| 15 | +#----------------------------------------------------------------------------------------- |
| 16 | +data "aws_caller_identity" "current" {} |
| 17 | + |
| 18 | +data "sysdig_secure_trusted_cloud_identity" "trusted_identity" { |
| 19 | + cloud_provider = "aws" |
| 20 | +} |
| 21 | + |
| 22 | +data "sysdig_secure_tenant_external_id" "external_id" {} |
| 23 | + |
| 24 | +#----------------------------------------------------------------------------------------- |
| 25 | +# Generate a unique name for resources using random suffix and account ID hash |
| 26 | +#----------------------------------------------------------------------------------------- |
| 27 | +locals { |
| 28 | + account_id_hash = substr(md5(data.aws_caller_identity.current.account_id), 0, 4) |
| 29 | + role_name = "${var.name}-${random_id.suffix.hex}-${local.account_id_hash}" |
| 30 | + |
| 31 | + bucket_arn = regex("^([^/]+)", var.folder_arn)[0] |
| 32 | +} |
| 33 | + |
| 34 | +#----------------------------------------------------------------------------------------------------------------------- |
| 35 | +# A random resource is used to generate unique role name suffix. |
| 36 | +# This prevents conflicts when recreating an role with the same name. |
| 37 | +#----------------------------------------------------------------------------------------------------------------------- |
| 38 | +resource "random_id" "suffix" { |
| 39 | + byte_length = 3 |
| 40 | +} |
| 41 | + |
| 42 | +# AWS IAM Role that will be used by CloudIngestion to access the CloudTrail-associated s3 bucket |
| 43 | +resource "aws_iam_role" "cloudlogs_s3_access" { |
| 44 | + name = local.role_name |
| 45 | + tags = var.tags |
| 46 | + |
| 47 | + assume_role_policy = data.aws_iam_policy_document.assume_cloudlogs_s3_access_role.json |
| 48 | + inline_policy { |
| 49 | + name = "cloudlogs_s3_access_policy" |
| 50 | + policy = data.aws_iam_policy_document.cloudlogs_s3_access.json |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +# IAM Policy Document used for the assume role policy |
| 55 | +data "aws_iam_policy_document" "assume_cloudlogs_s3_access_role" { |
| 56 | + statement { |
| 57 | + effect = "Allow" |
| 58 | + |
| 59 | + principals { |
| 60 | + type = "AWS" |
| 61 | + identifiers = [data.sysdig_secure_trusted_cloud_identity.trusted_identity.identity] |
| 62 | + } |
| 63 | + |
| 64 | + actions = ["sts:AssumeRole"] |
| 65 | + |
| 66 | + condition { |
| 67 | + test = "StringEquals" |
| 68 | + variable = "sts:ExternalId" |
| 69 | + values = [data.sysdig_secure_tenant_external_id.external_id.external_id] |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +# IAM Policy Document used for the bucket access policy |
| 75 | +data "aws_iam_policy_document" "cloudlogs_s3_access" { |
| 76 | + statement { |
| 77 | + sid = "CloudlogsS3AccessGet" |
| 78 | + |
| 79 | + effect = "Allow" |
| 80 | + |
| 81 | + actions = [ |
| 82 | + "s3:Get*", |
| 83 | + ] |
| 84 | + |
| 85 | + resources = [ |
| 86 | + local.bucket_arn, |
| 87 | + "${local.bucket_arn}/*" |
| 88 | + ] |
| 89 | + } |
| 90 | + |
| 91 | + statement { |
| 92 | + sid = "CloudlogsS3AccessList" |
| 93 | + |
| 94 | + effect = "Allow" |
| 95 | + |
| 96 | + actions = [ |
| 97 | + "s3:List*" |
| 98 | + ] |
| 99 | + |
| 100 | + resources = [ |
| 101 | + local.bucket_arn, |
| 102 | + "${local.bucket_arn}/*" |
| 103 | + ] |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +#----------------------------------------------------------------------------------------------------------------------------------------- |
| 108 | +# Call Sysdig Backend to add the cloud logs integration to the Sysdig Cloud Account |
| 109 | +# |
| 110 | +# Note (optional): To ensure this gets called after all cloud resources are created, add |
| 111 | +# explicit dependency using depends_on |
| 112 | +#----------------------------------------------------------------------------------------------------------------------------------------- |
| 113 | +resource "sysdig_secure_cloud_auth_account_component" "aws_cloud_logs" { |
| 114 | + account_id = var.sysdig_secure_account_id |
| 115 | + type = "COMPONENT_CLOUD_LOGS" |
| 116 | + instance = "secure-runtime" |
| 117 | + version = "v0.1.0" |
| 118 | + cloud_logs_metadata = jsonencode({ |
| 119 | + aws = { |
| 120 | + cloudtrailS3Bucket = { |
| 121 | + folder_arn = var.folder_arn |
| 122 | + role_name = local.role_name |
| 123 | + } |
| 124 | + } |
| 125 | + }) |
| 126 | +} |
0 commit comments