cloud-foundation-fabric/blueprints/data-solutions/shielded-folder/log-export.tf

108 lines
4.3 KiB
Terraform
Raw Normal View History

2023-01-20 16:08:51 -08:00
/**
2023-02-01 00:30:28 -08:00
* Copyright 2023 Google LLC
2023-01-20 16:08:51 -08:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
# tfdoc:file:description Audit log project and sink.
locals {
gcs_storage_class = (
2023-01-25 09:22:43 -08:00
length(split("-", var.log_locations.storage)) < 2
2023-01-20 16:08:51 -08:00
? "MULTI_REGIONAL"
: "REGIONAL"
)
log_types = toset([for k, v in var.log_sinks : v.type])
2023-01-30 14:32:13 -08:00
2023-01-31 15:50:22 -08:00
_log_keys = var.enable_features.encryption ? {
2023-01-30 14:32:13 -08:00
bq = var.enable_features.log_sink ? ["projects/${module.sec-project.0.project_id}/locations/${var.log_locations.bq}/keyRings/${var.log_locations.bq}/cryptoKeys/bq"] : null
pubsub = var.enable_features.log_sink ? ["projects/${module.sec-project.0.project_id}/locations/${var.log_locations.pubsub}/keyRings/${var.log_locations.pubsub}/cryptoKeys/pubsub"] : null
storage = var.enable_features.log_sink ? ["projects/${module.sec-project.0.project_id}/locations/${var.log_locations.storage}/keyRings/${var.log_locations.storage}/cryptoKeys/storage"] : null
} : {}
2023-01-20 16:08:51 -08:00
log_keys = {
for service, key in local._log_keys : service => key if key != null
}
}
module "log-export-project" {
2023-01-25 09:22:43 -08:00
count = var.enable_features.log_sink ? 1 : 0
2023-01-20 16:08:51 -08:00
source = "../../../modules/project"
2023-02-05 13:41:57 -08:00
name = var.project_config.project_ids["audit-logs"]
2023-01-20 16:08:51 -08:00
parent = module.folder.id
2023-02-05 13:41:57 -08:00
billing_account = var.project_config.billing_account_id
project_create = var.project_config.billing_account_id != null
prefix = var.project_config.billing_account_id == null ? null : var.prefix
2023-01-31 23:55:33 -08:00
group_iam = {
(local.groups.workload-security) = [
"roles/editor"
]
}
2023-01-20 16:08:51 -08:00
iam = {
# "roles/owner" = [module.automation-tf-bootstrap-sa.iam_email]
}
services = [
"bigquery.googleapis.com",
2023-01-25 09:22:43 -08:00
"pubsub.googleapis.com",
2023-01-20 16:08:51 -08:00
"storage.googleapis.com",
"stackdriver.googleapis.com"
]
2023-01-31 15:50:22 -08:00
service_encryption_key_ids = var.enable_features.encryption ? local.log_keys : {}
2023-01-25 09:22:43 -08:00
depends_on = [
module.log-kms
]
2023-01-20 16:08:51 -08:00
}
# one log export per type, with conditionals to skip those not needed
module "log-export-dataset" {
source = "../../../modules/bigquery-dataset"
2023-01-25 09:22:43 -08:00
count = var.enable_features.log_sink && contains(local.log_types, "bigquery") ? 1 : 0
project_id = module.log-export-project[0].project_id
2023-01-20 16:08:51 -08:00
id = "${var.prefix}_audit_export"
friendly_name = "Audit logs export."
location = replace(var.log_locations.bq, "europe", "EU")
encryption_key = var.enable_features.encryption ? module.log-kms[var.log_locations.bq].keys["bq"].id : null
2023-01-20 16:08:51 -08:00
}
module "log-export-gcs" {
source = "../../../modules/gcs"
2023-01-25 09:22:43 -08:00
count = var.enable_features.log_sink && contains(local.log_types, "storage") ? 1 : 0
project_id = module.log-export-project[0].project_id
2023-01-20 16:08:51 -08:00
name = "audit-logs"
prefix = var.prefix
2023-01-25 09:22:43 -08:00
location = replace(var.log_locations.storage, "europe", "EU")
2023-01-20 16:08:51 -08:00
storage_class = local.gcs_storage_class
2023-01-31 15:50:22 -08:00
encryption_key = var.enable_features.encryption ? module.log-kms[var.log_locations.storage].keys["storage"].id : null
2023-01-20 16:08:51 -08:00
}
module "log-export-logbucket" {
source = "../../../modules/logging-bucket"
2023-01-25 09:22:43 -08:00
for_each = var.enable_features.log_sink ? toset([for k, v in var.log_sinks : k if v.type == "logging"]) : []
2023-01-20 16:08:51 -08:00
parent_type = "project"
2023-01-25 09:22:43 -08:00
parent = module.log-export-project[0].project_id
2023-01-20 16:08:51 -08:00
id = "audit-logs-${each.key}"
location = var.log_locations.logging
#TODO check if logging bucket support encryption.
}
module "log-export-pubsub" {
source = "../../../modules/pubsub"
2023-01-25 09:22:43 -08:00
for_each = toset([for k, v in var.log_sinks : k if v.type == "pubsub" && var.enable_features.log_sink])
project_id = module.log-export-project[0].project_id
2023-01-20 16:08:51 -08:00
name = "audit-logs-${each.key}"
regions = [var.log_locations.pubsub]
2023-01-31 15:50:22 -08:00
kms_key = var.enable_features.encryption ? module.log-kms[var.log_locations.pubsub].keys["pubsub"].id : null
2023-01-20 16:08:51 -08:00
}