Skip to content

Add control for creating and attaching policies #29

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

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 11 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
locals {
iam_role_arn = module.lacework_cfg_iam_role.created ? module.lacework_cfg_iam_role.arn : var.iam_role_arn
iam_role_name = module.lacework_cfg_iam_role.created ? module.lacework_cfg_iam_role.name : var.iam_role_name
iam_role_external_id = module.lacework_cfg_iam_role.created ? module.lacework_cfg_iam_role.external_id : var.iam_role_external_id
iam_role_arn = module.lacework_cfg_iam_role.created ? module.lacework_cfg_iam_role.arn : var.iam_role_arn
iam_role_name = module.lacework_cfg_iam_role.created ? module.lacework_cfg_iam_role.name : var.iam_role_name
iam_role_external_id = module.lacework_cfg_iam_role.created ? module.lacework_cfg_iam_role.external_id : var.iam_role_external_id
lacework_audit_policy_name = (
length(var.lacework_audit_policy_name) > 0 ? var.lacework_audit_policy_name : "lwaudit-policy-${random_id.uniq.hex}"
)
Expand All @@ -22,6 +22,8 @@ module "lacework_cfg_iam_role" {
}

resource "aws_iam_role_policy_attachment" "security_audit_policy_attachment" {
count = var.create_policies ? 1 : 0

role = local.iam_role_name
policy_arn = "arn:aws:iam::aws:policy/SecurityAudit"
depends_on = [module.lacework_cfg_iam_role]
Expand All @@ -39,22 +41,26 @@ data "aws_iam_policy_document" "lacework_audit_policy" {
}

resource "aws_iam_policy" "lacework_audit_policy" {
count = var.create_policies ? 1 : 0

name = local.lacework_audit_policy_name
description = "An audit policy to allow Lacework to read configs (extends SecurityAudit)"
policy = data.aws_iam_policy_document.lacework_audit_policy.json
}

resource "aws_iam_role_policy_attachment" "lacework_audit_policy_attachment" {
count = var.create_policies ? 1 : 0

role = local.iam_role_name
policy_arn = aws_iam_policy.lacework_audit_policy.arn
policy_arn = aws_iam_policy.lacework_audit_policy[0].arn
depends_on = [module.lacework_cfg_iam_role]
}

# wait for X seconds for things to settle down in the AWS side
# before trying to create the Lacework external integration
resource "time_sleep" "wait_time" {
create_duration = var.wait_time
depends_on = [
depends_on = [
aws_iam_role_policy_attachment.security_audit_policy_attachment,
aws_iam_role_policy_attachment.lacework_audit_policy_attachment,
]
Expand Down
9 changes: 7 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
variable "create_policies" {
type = bool
default = true
description = "Set this to false when using a role who's policy is managed outside this module"
}

variable "use_existing_iam_role" {
type = bool
Expand Down Expand Up @@ -42,8 +47,8 @@ variable "lacework_integration_name" {
}

variable "lacework_audit_policy_name" {
type = string
default = ""
type = string
default = ""
description = "The name of the custom audit policy (which extends SecurityAudit) to allow Lacework to read configs. Defaults to lwaudit-policy-$${random_id.uniq.hex} when empty"
}

Expand Down