diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..520523b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.zip + +.terraform* +.terraform + +terraform.tfstate* \ No newline at end of file diff --git a/GNUmakefile b/GNUmakefile index 0078f1c..bab1be2 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -18,3 +18,7 @@ ci: ## *CI ONLY* Runs tests on CI pipeline .PHONY: release release: ci ## *CI ONLY* Prepares a release of the Terraform module scripts/release.sh prepare + +.PHONY: terraform-docs +terraform-docs: + scripts/terraform-docs.sh diff --git a/README.md b/README.md index 891ed20..bdea407 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Terraform module for configuring an integration with Lacework and AWS for cloud | Name | Version | |------|---------| +| [archive](#provider\_archive) | n/a | | [aws](#provider\_aws) | >= 3.35.0 | | [random](#provider\_random) | >= 2.1 | @@ -39,6 +40,7 @@ No modules. | [aws_kms_key.lacework_kms_key](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | | [aws_lambda_function.lacework_copy_zip_files](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource | | [aws_lambda_function.lacework_setup_function](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource | +| [aws_lambda_invocation.lacework_copy_zip_files](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_invocation) | resource | | [aws_lambda_permission.lacework_lambda_permission](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource | | [aws_s3_bucket.lacework_org_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource | | [aws_s3_bucket_ownership_controls.lacework_org_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_ownership_controls) | resource | @@ -49,10 +51,13 @@ No modules. | [aws_sns_topic.lacework_sns_topic](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource | | [aws_sns_topic_policy.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_policy) | resource | | [aws_sns_topic_subscription.lacework_sns_subscription](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_subscription) | resource | -| [random_id.uniq](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource | +| [random_string.uniq](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource | +| [archive_file.lambda_zip_file](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source | | [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | | [aws_iam_policy_document.kms_key_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_policy_document.lacework_copy_zip_files_assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_iam_policy_document.lacework_copy_zip_files_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_policy_document.lacework_setup_function_assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_iam_policy_document.lacework_setup_function_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_iam_policy_document.sns_topic_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source | diff --git a/examples/default/README.md b/examples/default/README.md index 74dc015..4f6ea50 100644 --- a/examples/default/README.md +++ b/examples/default/README.md @@ -15,7 +15,9 @@ terraform { provider "lacework" {} -provider "aws" {} +provider "aws" { + region = "us-west-2" +} module "aws_org_configuration" { source = "../../" diff --git a/examples/default/main.tf b/examples/default/main.tf index 92fb42f..ed4b542 100644 --- a/examples/default/main.tf +++ b/examples/default/main.tf @@ -1,6 +1,8 @@ provider "lacework" {} -provider "aws" {} +provider "aws" { + region = "us-west-2" +} module "aws_org_configuration" { source = "../../" diff --git a/lambda.zip b/lambda.zip deleted file mode 100644 index 61272d8..0000000 Binary files a/lambda.zip and /dev/null differ diff --git a/main.tf b/main.tf index ede7051..2fd66d1 100644 --- a/main.tf +++ b/main.tf @@ -1,21 +1,24 @@ locals { - account_id = data.aws_caller_identity.current.account_id - external_id = "lweid:aws:v2:${var.lacework_account}:${local.account_id}:${random_id.uniq.id}" - kms_key_arn = (length(var.kms_key_arn) > 0 ? var.kms_key_arn : aws_kms_key.lacework_kms_key[0].arn) - stack_name = "lacework-aws-org-configuration" + account_id = data.aws_caller_identity.current.account_id + external_id = "lweid:aws:v2:${local.lacework_tenant}:${local.account_id}:${random_string.uniq.id}" + lacework_tenant = length(var.lacework_sub_account) > 0 ? var.lacework_sub_account : var.lacework_account + kms_key_arn = length(var.kms_key_arn) > 0 ? var.kms_key_arn : aws_kms_key.lacework_kms_key[0].arn + stack_name = "lacework-aws-org-configuration" } data "aws_caller_identity" "current" {} -resource "random_id" "uniq" { - byte_length = 10 +resource "random_string" "uniq" { + length = 10 + special = false } #tfsec:ignore:aws-s3-enable-bucket-encryption #tfsec:ignore:aws-s3-enable-bucket-logging #tfsec:ignore:aws-s3-encryption-customer-key resource "aws_s3_bucket" "lacework_org_lambda" { - bucket = "lacework_org_lambda" + bucket_prefix = "lacework-org-lambda-" + force_destroy = true } resource "aws_s3_bucket_versioning" "lacework_org_lambda" { @@ -55,69 +58,80 @@ resource "aws_lambda_function" "lacework_copy_zip_files" { tracing_config { mode = "Active" } + + environment { + variables = { + src_bucket = var.s3_bucket + dst_bucket = aws_s3_bucket.lacework_org_lambda.id + prefix = var.s3_prefix + object = "/lambda/LaceworkIntegrationSetup1.1.2.zip" + } + } } data "archive_file" "lambda_zip_file" { + excludes = ["__init__.py", "*.pyc"] output_path = "${path.module}/lambda.zip" source_dir = "${path.module}/python" - excludes = ["__init__.py", "*.pyc"] type = "zip" } resource "aws_iam_role" "lacework_copy_zip_files_role" { - assume_role_policy = data.aws_iam_policy_document.lacework_setup_function_role.json + assume_role_policy = data.aws_iam_policy_document.lacework_copy_zip_files_assume_role.json + + inline_policy { + name = "zip-role" + policy = data.aws_iam_policy_document.lacework_copy_zip_files_role.json + } + managed_policy_arns = ["arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"] name = "lacework_copy_zip_files_role" path = "/" } -data "aws_iam_policy_document" "lacework_copy_zip_files_role" { +data "aws_iam_policy_document" "lacework_copy_zip_files_assume_role" { statement { actions = ["sts:AssumeRole"] - effect = "Allow" principals { type = "Service" identifiers = ["lambda.amazonaws.com"] } } +} +data "aws_iam_policy_document" "lacework_copy_zip_files_role" { statement { actions = [ "s3:GetObject", "s3:GetObjectTagging", ] effect = "Allow" - resources = ["aws:${data.aws_partition.current.partition}:s3:::${var.s3_bucket}/${var.s3_prefix}*"] + resources = [ + "arn:${data.aws_partition.current.partition}:s3:::${var.s3_bucket}/${var.s3_prefix}/*" + ] } statement { actions = [ - "s3:DeleteObject", - "s3:PutObject", - "s3:PutObjectTagging", + "s3:*", ] effect = "Allow" - resources = [aws_s3_bucket.lacework_org_lambda.arn] + resources = [ + aws_s3_bucket.lacework_org_lambda.arn, + "${aws_s3_bucket.lacework_org_lambda.arn}/*", + ] } version = "2012-10-17" } -data "aws_lambda_invocation" "lacework_copy_zip_files" { - function_name = aws_lambda_function.lacework_copy_zip_files.id - - input = <