Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

[GPII-3326]: Apply audit config in gcp-project module #246

Merged
merged 15 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from 11 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
50 changes: 50 additions & 0 deletions common/modules/gcp-project/audit.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
variable "nonce" {}

variable "gcloud_timeout" {
default = 30
}

resource "null_resource" "add_audit_config" {
triggers = {
nonce = "${var.nonce}"
}

depends_on = [
"google_project_iam_policy.project",
]

provisioner "local-exec" {
command = <<EOF
set -e

auditConfigs=$(echo '[${join(
natarajaya marked this conversation as resolved.
Show resolved Hide resolved
",",
formatlist(
"{\"auditLogConfigs\":[{\"logType\":\"DATA_READ\"},{\"logType\":\"DATA_WRITE\"}],\"service\":\"%s\"}",
concat(
var.audited_project_apis,
var.audited_apis
)
)
)}]' | jq -c -r -S .)

policy=$(timeout -t ${var.gcloud_timeout} gcloud projects get-iam-policy ${google_project.project.project_id} --format json)
natarajaya marked this conversation as resolved.
Show resolved Hide resolved
policy_bindings=$(echo $policy | jq -c -r .bindings)
natarajaya marked this conversation as resolved.
Show resolved Hide resolved
policy_auditConfigs=$(echo $policy | jq -c -r -S .auditConfigs)
natarajaya marked this conversation as resolved.
Show resolved Hide resolved

if [ "$(echo $auditConfigs | md5sum)" != "$(echo $policy_auditConfigs | md5sum)" ]; then
natarajaya marked this conversation as resolved.
Show resolved Hide resolved
jq -n -r \
--argjson auditConfigs "$auditConfigs" \
--argjson bindings "$policy_bindings" \
'{"auditConfigs":$auditConfigs,"bindings":$bindings}' > ${path.module}/${google_project.project.project_id}.iam.policy.json

echo "Applying audit configs..."
timeout -t ${var.gcloud_timeout} gcloud -q projects set-iam-policy ${google_project.project.project_id} ${path.module}/${google_project.project.project_id}.iam.policy.json

rm ${path.module}/${google_project.project.project_id}.iam.policy.json
else
echo "Audit configs are up-to-date..."
fi
EOF
}
}
69 changes: 44 additions & 25 deletions common/modules/gcp-project/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,49 @@ variable "organization_id" {}

variable "serviceaccount_key" {}

variable "project_id" {} # id of the project which owns the credentials used by the provider
# Id of the project which owns the credentials used by the provider
variable "project_id" {}

# List of APIs in use by the project without audit configuration
natarajaya marked this conversation as resolved.
Show resolved Hide resolved
variable "project_apis" {
natarajaya marked this conversation as resolved.
Show resolved Hide resolved
default = [
"bigquery-json.googleapis.com",
"cloudbilling.googleapis.com",
"cloudresourcemanager.googleapis.com",
"containerregistry.googleapis.com",
"deploymentmanager.googleapis.com",
"iamcredentials.googleapis.com",
"oslogin.googleapis.com",
"pubsub.googleapis.com",
"replicapool.googleapis.com",
"replicapoolupdater.googleapis.com",
"resourceviews.googleapis.com",
"stackdriver.googleapis.com",
"storage-api.googleapis.com",
]
}

# List of APIs in use by the project with audit configuration
variable "audited_project_apis" {
natarajaya marked this conversation as resolved.
Show resolved Hide resolved
default = [
"cloudkms.googleapis.com",
"cloudtrace.googleapis.com",
"compute.googleapis.com",
"container.googleapis.com",
"dns.googleapis.com",
"iam.googleapis.com",
"logging.googleapis.com",
"monitoring.googleapis.com",
"serviceusage.googleapis.com",
]
}

# List of APIs solely for audit configuration
variable "audited_apis" {
natarajaya marked this conversation as resolved.
Show resolved Hide resolved
default = [
"storage.googleapis.com",
]
}

data "google_iam_policy" "admin" {
binding {
Expand Down Expand Up @@ -196,30 +238,7 @@ resource "google_project" "project" {
resource "google_project_services" "project" {
project = "${google_project.project.project_id}"

services = [
"bigquery-json.googleapis.com",
"cloudbilling.googleapis.com",
"cloudkms.googleapis.com",
"cloudresourcemanager.googleapis.com",
"cloudtrace.googleapis.com",
"compute.googleapis.com",
"container.googleapis.com",
"containerregistry.googleapis.com",
"deploymentmanager.googleapis.com",
"dns.googleapis.com",
"iam.googleapis.com",
"iamcredentials.googleapis.com",
"logging.googleapis.com",
"monitoring.googleapis.com",
"oslogin.googleapis.com",
"pubsub.googleapis.com",
"replicapool.googleapis.com",
"replicapoolupdater.googleapis.com",
"resourceviews.googleapis.com",
"serviceusage.googleapis.com",
"stackdriver.googleapis.com",
"storage-api.googleapis.com",
]
services = "${concat(var.project_apis, var.audited_project_apis)}"
}

resource "google_project_iam_policy" "project" {
Expand Down
1 change: 1 addition & 0 deletions gcp/modules/gcp-secret-mgmt/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module "gcp-secret-mgmt" {
encryption_keys = "${var.encryption_keys}"
storage_location = "${var.storage_location}"
keyring_name = "${var.keyring_name}"
apply_audit_config = false
natarajaya marked this conversation as resolved.
Show resolved Hide resolved
}

# Re-export variable. See https://www.terraform.io/docs/providers/terraform/d/remote_state.html#root-outputs-only
Expand Down