|
| 1 | +data "sysdig_secure_tenant_external_id" "cloud_auth_external_id" {} |
| 2 | + |
| 3 | +data "sysdig_secure_trusted_cloud_identity" "trusted_identity" { |
| 4 | + cloud_provider = "aws" |
| 5 | +} |
| 6 | + |
| 7 | +########################################### |
| 8 | +# Workload Controller IAM roles and stuff # |
| 9 | +########################################### |
| 10 | + |
| 11 | +#----------------------------------------------------------------------------------------------------------------------- |
| 12 | +# Determine if this is an Organizational install, or a single account install. For Single Account installs, resources |
| 13 | +# are created directly using the AWS Terraform Provider (This is the default behaviour). For Organizational installs, |
| 14 | +# see organizational.tf, and the resources in this file are used to instrument the management account (StackSets do not |
| 15 | +# include the management account they are created in, even if this account is within the target Organization). |
| 16 | +#----------------------------------------------------------------------------------------------------------------------- |
| 17 | + |
| 18 | +#----------------------------------------------------------------------------------------------------------------------- |
| 19 | +# These resources create an Agentless Workload Scanning IAM Role and IAM Policy in the account. |
| 20 | +#----------------------------------------------------------------------------------------------------------------------- |
| 21 | + |
| 22 | +data "aws_iam_policy_document" "scanning" { |
| 23 | + # General ECR read permission, necessary for the fetching artifacts. |
| 24 | + statement { |
| 25 | + sid = "EcrReadPermissions" |
| 26 | + |
| 27 | + effect = "Allow" |
| 28 | + |
| 29 | + actions = [ |
| 30 | + "ecr:GetDownloadUrlForLayer", |
| 31 | + "ecr:BatchGetImage", |
| 32 | + "ecr:BatchCheckLayerAvailability", |
| 33 | + "ecr:ListImages", |
| 34 | + "ecr:GetAuthorizationToken", |
| 35 | + ] |
| 36 | + |
| 37 | + resources = [ |
| 38 | + "*", |
| 39 | + ] |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +data "aws_iam_policy_document" "functions" { |
| 44 | + count = var.lambda_scanning_enabled ? 1 : 0 |
| 45 | + |
| 46 | + statement { |
| 47 | + sid = "SysdigWorkloadFunctionsScanning" |
| 48 | + |
| 49 | + effect = "Allow" |
| 50 | + |
| 51 | + actions = [ |
| 52 | + "lambda:GetFunction", |
| 53 | + "lambda:GetFunctionConfiguration", |
| 54 | + "lambda:GetRuntimeManagementConfig", |
| 55 | + "lambda:ListFunctions", |
| 56 | + "lambda:ListTagsForResource", |
| 57 | + "lambda:GetLayerVersionByArn", |
| 58 | + "lambda:GetLayerVersion", |
| 59 | + "lambda:ListLayers", |
| 60 | + "lambda:ListLayerVersions" |
| 61 | + ] |
| 62 | + |
| 63 | + resources = [ |
| 64 | + "*", |
| 65 | + ] |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +resource "aws_iam_policy" "ecr_scanning" { |
| 70 | + count = var.is_organizational ? 0 : 1 |
| 71 | + |
| 72 | + name = "${local.ecr_role_name}-ecr" |
| 73 | + description = "Grants Sysdig Secure access to ECR images" |
| 74 | + policy = data.aws_iam_policy_document.scanning.json |
| 75 | + tags = var.tags |
| 76 | +} |
| 77 | + |
| 78 | +resource "aws_iam_policy" "functions_scanning" { |
| 79 | + count = var.lambda_scanning_enabled && !var.is_organizational? 1 : 0 |
| 80 | + |
| 81 | + name = "${local.ecr_role_name}-functions" |
| 82 | + description = "Grants Sysdig Secure access to AWS Lambda" |
| 83 | + policy = data.aws_iam_policy_document.functions[0].json |
| 84 | + tags = var.tags |
| 85 | +} |
| 86 | + |
| 87 | +data "aws_iam_policy_document" "scanning_assume_role_policy" { |
| 88 | + statement { |
| 89 | + sid = "SysdigWorkloadScanning" |
| 90 | + |
| 91 | + actions = [ |
| 92 | + "sts:AssumeRole" |
| 93 | + ] |
| 94 | + |
| 95 | + principals { |
| 96 | + type = "AWS" |
| 97 | + identifiers = [ |
| 98 | + data.sysdig_secure_trusted_cloud_identity.trusted_identity.identity |
| 99 | + ] |
| 100 | + } |
| 101 | + |
| 102 | + condition { |
| 103 | + test = "StringEquals" |
| 104 | + variable = "sts:ExternalId" |
| 105 | + values = [data.sysdig_secure_tenant_external_id.cloud_auth_external_id.external_id] |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +resource "aws_iam_role" "scanning" { |
| 111 | + count = var.is_organizational ? 0 : 1 |
| 112 | + |
| 113 | + name = local.ecr_role_name |
| 114 | + tags = var.tags |
| 115 | + assume_role_policy = data.aws_iam_policy_document.scanning_assume_role_policy.json |
| 116 | +} |
| 117 | + |
| 118 | +resource "aws_iam_policy_attachment" "scanning" { |
| 119 | + count = var.is_organizational ? 0 : 1 |
| 120 | + |
| 121 | + name = local.ecr_role_name |
| 122 | + roles = [aws_iam_role.scanning[0].name] |
| 123 | + policy_arn = aws_iam_policy.ecr_scanning[0].arn |
| 124 | +} |
| 125 | + |
| 126 | +resource "aws_iam_policy_attachment" "functions" { |
| 127 | + count = var.lambda_scanning_enabled && !var.is_organizational ? 1 : 0 |
| 128 | + |
| 129 | + name = local.ecr_role_name |
| 130 | + roles = [aws_iam_role.scanning[0].name] |
| 131 | + policy_arn = aws_iam_policy.functions_scanning[0].arn |
| 132 | +} |
| 133 | + |
| 134 | +#-------------------------------------------------------------------------------------------------------------- |
| 135 | +# Call Sysdig Backend to add the trusted role for Config Posture to the Sysdig Cloud Account |
| 136 | +# |
| 137 | +# Note (optional): To ensure this gets called after all cloud resources are created, add |
| 138 | +# explicit dependency using depends_on |
| 139 | +#-------------------------------------------------------------------------------------------------------------- |
| 140 | +resource "sysdig_secure_cloud_auth_account_component" "vm_workload_scanning_account_component" { |
| 141 | + account_id = var.sysdig_secure_account_id |
| 142 | + |
| 143 | + type = "COMPONENT_TRUSTED_ROLE" |
| 144 | + instance = "secure-vm-workload-scanning" |
| 145 | + version = "v0.1.0" |
| 146 | + trusted_role_metadata = jsonencode({ |
| 147 | + aws = { |
| 148 | + role_name = aws_iam_role.scanning[0].name |
| 149 | + } |
| 150 | + }) |
| 151 | + |
| 152 | + depends_on = [ |
| 153 | + aws_iam_policy.ecr_scanning, |
| 154 | + aws_iam_role.scanning, |
| 155 | + aws_iam_policy_attachment.scanning, |
| 156 | + aws_cloudformation_stack_set.scanning_role_stackset, |
| 157 | + aws_cloudformation_stack_set_instance.scanning_role_stackset_instance, |
| 158 | + aws_iam_policy.functions_scanning, |
| 159 | + aws_iam_policy_attachment.functions, |
| 160 | + ] |
| 161 | +} |
0 commit comments