Skip to content

Commit 2690fcf

Browse files
authored
feat(cloud-logs): add role name as alternative to role ARN, to be provided with the bucket account (#71)
1 parent 29ed445 commit 2690fcf

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

modules/integrations/cloud-logs/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ No modules.
105105
| <a name="input_sysdig_secure_account_id"></a> [sysdig\_secure\_account\_id](#input\_sysdig\_secure\_account\_id) | (Required) ID of the Sysdig Cloud Account to enable Cloud Logs integration for (in case of organization, ID of the Sysdig management account) | `string` | n/a | yes |
106106
| <a name="input_bucket_arn"></a> [bucket\_arn](#input\_bucket\_arn) | (Required) The ARN of your CloudTrail Bucket | `string` | n/a | yes |
107107
| <a name="input_topic_arn"></a> [topic\_arn](#input\_topic\_arn) | SNS Topic ARN that will forward CloudTrail notifications to Sysdig Secure | `string` | n/a | yes |
108-
| <a name="input_create_topic"></a> [create\_topic](#input\_create\_topic) | true/false whether terraform should create the SNS Topic | `bool` | `false` | no |
109-
| <a name="input_kms_key_arn"></a> [kms\_key\_arn](#input\_kms\_key\_arn) | (Optional) ARN of the KMS key used to encrypt the S3 bucket. If provided, the IAM role will be granted permissions to decrypt using this key. | `string` | `null` | no |
108+
| <a name="input_create_topic"></a> [create\_topic](#input\_create\_topic) | true/false whether terraform should create the SNS Topic or subscribe to an existing one | `bool` | `false` | no |
109+
| <a name="input_role_arn"></a> [role\_arn](#input\_role\_arn) | ARN of the Role to create, used by Sysdig to access logs in the S3 Bucket. Alternative to the Role Name | `string` | `null` | no |
110+
| <a name="input_role_name"></a> [role\_name](#input\_role\_name) | Name of the Role to create, used by Sysdig to access logs in the S3 Bucket. Alternative to the Role ARN | `string` | `null` | no |
111+
| <a name="input_kms_key_arn"></a> [kms\_key\_arn](#input\_kms\_key\_arn) | (Optional) ARN of the KMS key used to encrypt the S3 bucket. If provided, the IAM role will be granted permissions to decrypt using this key. | `string` | `null` | no |
110112
| <a name="input_bucket_account_id"></a> [bucket\_account\_id](#input\_bucket\_account\_id) | (Optional) AWS Account ID that owns the S3 bucket, if different from the account where the module is being applied. Required for cross-account organizational deployments. | `string` | `null` | no |
111113
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) Sysdig secure-for-cloud tags. always include 'product' default tag for resource-group proper functioning | `map(string)` | <pre>{<br> "product": "sysdig-secure-for-cloud"<br>}</pre> | no |
112114
| <a name="input_name"></a> [name](#input\_name) | (Optional) Name to be assigned to all child resources. A suffix may be added internally when required. | `string` | sysdig-secure-cloudlogs | no |

modules/integrations/cloud-logs/main.tf

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,17 @@ data "aws_organizations_organization" "org" {
4747
count = var.is_organizational ? 1 : 0
4848
}
4949

50-
50+
resource "null_resource" "validation" {
51+
lifecycle {
52+
precondition {
53+
condition = (
54+
(var.role_name != null ? 1 : 0) +
55+
(var.role_arn != null ? 1 : 0)
56+
) == 1
57+
error_message = "either `role_arn` or `role_name` must be defined"
58+
}
59+
}
60+
}
5161
#-----------------------------------------------------------------------------------------
5262
# Generate a unique name for resources using random suffix and account ID hash
5363
#-----------------------------------------------------------------------------------------
@@ -73,7 +83,8 @@ locals {
7383
need_kms_policy = var.bucket_account_id != null && var.bucket_account_id != local.kms_account_id
7484

7585
# Role variables
76-
role_name = split("/", var.role_arn)[1]
86+
role_name = var.role_name != null ? var.role_name : split("/", var.role_arn)[1]
87+
role_arn = var.role_arn != null ? var.role_arn : "arn:${data.aws_partition.current.partition}:iam::${local.bucket_account_id}:role/${local.role_name}"
7788

7889
account_id_hash = substr(md5(local.bucket_account_id), 0, 4)
7990
# StackSet configuration
@@ -99,6 +110,11 @@ resource "aws_iam_role" "cloudlogs_s3_access" {
99110
name = local.role_name
100111
tags = var.tags
101112
assume_role_policy = data.aws_iam_policy_document.assume_cloudlogs_s3_access_role.json
113+
depends_on = [null_resource.validation]
114+
115+
lifecycle {
116+
precondition {
117+
condition = var.role_arn == null || split(":", var.role_arn)[4] == local.bucket_account_id
102118
}
103119

104120
// AWS IAM Role Policy
@@ -313,6 +329,7 @@ resource "sysdig_secure_cloud_auth_account_component" "aws_cloud_logs" {
313329
})
314330

315331
depends_on = [
332+
null_resource.validation,
316333
aws_iam_role.cloudlogs_s3_access,
317334
aws_cloudformation_stack_set_instance.cloudlogs_s3_access_bucket,
318335
aws_cloudformation_stack_set_instance.cloudlogs_s3_access_topic

modules/integrations/cloud-logs/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ output "kms_policy_instructions" {
99
value = (local.need_kms_policy) ? templatefile(
1010
"${path.module}/templates/kms_policy_instructions.tpl",
1111
{
12-
role_arn = var.role_arn
12+
role_arn = local.role_arn
1313
}
1414
) : ""
1515
}

modules/integrations/cloud-logs/variables.tf

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,27 @@ variable "topic_arn" {
6060
}
6161
}
6262

63+
variable "create_topic" {
64+
type = bool
65+
default = false
66+
description = "true/false whether terraform should create the SNS Topic"
67+
}
68+
6369
variable "role_arn" {
6470
type = string
6571
description = "ARN of the role that terraform will create to download the CloudTrail logs from the S3 bucket."
72+
default = null
6673

6774
validation {
68-
condition = var.role_arn != ""
69-
error_message = "Role ARN must not be empty"
70-
}
71-
72-
validation {
73-
condition = can(regex("^arn:(aws|aws-us-gov):iam::[0-9]+:role/.+$", var.role_arn))
75+
condition = var.role_arn == null || can(regex("^arn:(aws|aws-us-gov):iam::[0-9]+:role/.+$", var.role_arn))
7476
error_message = "Role ARN must be a valid IAM ARN format"
7577
}
7678
}
7779

78-
variable "create_topic" {
79-
type = bool
80-
default = false
81-
description = "true/false whether terraform should create the SNS Topic"
80+
variable "role_name" {
81+
type = string
82+
description = "Name for the Role that Terraform will create to download the CloudTrail logs from the S3 bucket. Alternative to the `role_arn`"
83+
default = null
8284
}
8385

8486
variable "bucket_account_id" {

0 commit comments

Comments
 (0)