forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
153 lines (132 loc) · 4.57 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright 2021 Google, LLC
#
# 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.
# Create a feed that sends notifications about compute instance updates under a
# particular organization.
resource "google_cloud_asset_organization_feed" "main" {
provider = google-beta
billing_project = var.project_id
org_id = var.org_id
feed_id = var.feed_id
content_type = "RESOURCE"
asset_types = [
"compute.googleapis.com/Firewall",
]
condition {
expression = "!temporal_asset.deleted"
title = "Not deleted"
description = "Send notification only when created"
}
feed_output_config {
pubsub_destination {
topic = google_pubsub_topic.feed_output.id
}
}
depends_on = [
google_project_service.cloudasset
]
}
# Allow the publishing role to the Cloud Asset service account of the project that
# was used for sending the notifications.
resource "google_pubsub_topic_iam_member" "cloud_asset_writer" {
project = var.project_id
topic = google_pubsub_topic.feed_output.id
role = "roles/pubsub.publisher"
member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-cloudasset.iam.gserviceaccount.com"
# Wait for the permission to be ready on the destination topic.
depends_on = [
google_cloud_asset_organization_feed.main
]
}
# The topic where the resource change notifications will be sent.
resource "google_pubsub_topic" "feed_output" {
project = var.project_id
name = var.feed_id
depends_on = [
google_project_service.pubsub,
google_project_service.rm
]
}
# Bucket to hold function code
resource "google_storage_bucket" "bucket" {
name = "${var.project_id}-fw"
uniform_bucket_level_access = true
}
# zip up cloud function
data "archive_file" "function_archive" {
type = "zip"
source_dir = "./function/"
output_path = "./function/index.zip"
}
# function source code
resource "google_storage_bucket_object" "archive" {
name = "${data.archive_file.function_archive.output_md5}-index"
bucket = google_storage_bucket.bucket.name
source = data.archive_file.function_archive.output_path
content_disposition = "attachment"
content_encoding = "gzip"
content_type = "application/zip"
}
# service account that runs cloud function
resource "google_service_account" "function" {
account_id = var.function_sa_name
display_name = var.function_sa_name
}
resource "google_cloudfunctions_function" "function" {
name = "firewall-enforcer"
description = "Deletes firewall rules"
runtime = "python39"
service_account_email = google_service_account.function.email
source_archive_bucket = google_storage_bucket.bucket.name
source_archive_object = google_storage_bucket_object.archive.name
entry_point = "main"
event_trigger {
event_type = "google.pubsub.topic.publish"
resource = google_pubsub_topic.feed_output.id
}
}
# Find the project number of the project whose identity will be used for sending
# the asset change notifications.
data "google_project" "project" {
project_id = var.project_id
}
resource "google_project_service" "cloudasset" {
project = var.project_id
service = "cloudasset.googleapis.com"
}
resource "google_project_service" "functions" {
project = var.project_id
service = "cloudfunctions.googleapis.com"
}
resource "google_project_service" "pubsub" {
project = var.project_id
service = "pubsub.googleapis.com"
}
resource "google_project_service" "build" {
project = var.project_id
service = "cloudbuild.googleapis.com"
}
resource "google_project_service" "rm" {
project = var.project_id
service = "cloudresourcemanager.googleapis.com"
}
resource "google_organization_iam_member" "function" {
org_id = var.org_id
member = "serviceAccount:${google_service_account.function.email}"
role = "roles/compute.securityAdmin"
}
resource "google_project_iam_member" "function_logger" {
project = var.project_id
member = "serviceAccount:${google_service_account.function.email}"
role = "roles/logging.logWriter"
}