-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.tf
159 lines (140 loc) · 4.27 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
154
155
156
157
158
159
locals {
vault_config = jsonencode(
{
"storage" = {
"gcs" = {
"bucket" = local.vault_storage_bucket_name
"ha_enabled" = "false"
}
},
"seal" = {
"gcpckms" = {
"project" = var.project,
"region" = var.location,
"key_ring" = local.vault_kms_keyring_name,
"crypto_key" = google_kms_crypto_key.vault.name
}
},
"default_lease_ttl" = "168h",
"max_lease_ttl" = "720h",
"disable_mlock" = "true",
"listener" = {
"tcp" = {
"address" = "0.0.0.0:8080",
"tls_disable" = "1"
}
},
"ui" = var.vault_ui
}
)
vault_kms_keyring_name = var.vault_kms_keyring_name != "" ? var.vault_kms_keyring_name : "${var.name}-${lower(random_id.vault.hex)}-kr"
vault_storage_bucket_name = var.vault_storage_bucket_name != "" ? var.vault_storage_bucket_name : "${var.name}-${lower(random_id.vault.hex)}-bucket"
}
resource "random_id" "vault" {
byte_length = 2
}
resource "google_service_account" "vault" {
project = var.project
account_id = var.vault_service_account_id
display_name = "Vault Service Account for KMS auto-unseal"
}
resource "google_storage_bucket" "vault" {
name = local.vault_storage_bucket_name
project = var.project
force_destroy = var.bucket_force_destroy
}
resource "google_storage_bucket_iam_member" "member" {
bucket = google_storage_bucket.vault.name
role = "roles/storage.objectAdmin"
member = "serviceAccount:${google_service_account.vault.email}"
}
# Create a KMS key ring
resource "google_kms_key_ring" "vault" {
name = local.vault_kms_keyring_name
project = var.project
location = var.location
}
# Create a crypto key for the key ring, rotate daily
resource "google_kms_crypto_key" "vault" {
name = "${var.name}-key"
key_ring = google_kms_key_ring.vault.self_link
rotation_period = var.vault_kms_key_rotation
version_template {
algorithm = var.vault_kms_key_algorithm
protection_level = var.vault_kms_key_protection_level
}
}
# Add the service account to the Keyring
resource "google_kms_key_ring_iam_member" "vault" {
key_ring_id = google_kms_key_ring.vault.id
role = "roles/owner"
member = "serviceAccount:${google_service_account.vault.email}"
}
resource "google_cloud_run_service" "default" {
name = var.name
project = var.project
location = var.location
autogenerate_revision_name = true
metadata {
namespace = var.project
}
template {
metadata {
annotations = {
"autoscaling.knative.dev/maxScale" = 1 # HA not Supported
"run.googleapis.com/vpc-access-connector" = var.vpc_connector != "" ? var.vpc_connector : null
# Hardcoded here after a change in the Cloud Run API response
"run.googleapis.com/sandbox" = "gvisor"
}
}
spec {
service_account_name = google_service_account.vault.email
container_concurrency = var.container_concurrency
containers {
# Specifying args seems to require the command / entrypoint
image = var.vault_image
command = ["/usr/local/bin/docker-entrypoint.sh"]
args = ["server"]
env {
name = "SKIP_SETCAP"
value = "true"
}
env {
name = "VAULT_LOCAL_CONFIG"
value = local.vault_config
}
env {
name = "VAULT_API_ADDR"
value = var.vault_api_addr
}
resources {
limits = {
"cpu" = "1000m"
"memory" = "256Mi"
}
requests = {}
}
}
}
}
}
data "google_iam_policy" "noauth" {
binding {
role = "roles/run.invoker"
members = [
"allUsers",
]
}
}
resource "google_cloud_run_service_iam_policy" "noauth" {
location = google_cloud_run_service.default.location
project = google_cloud_run_service.default.project
service = google_cloud_run_service.default.name
policy_data = data.google_iam_policy.noauth.policy_data
}
output "app_url" {
value = google_cloud_run_service.default.status[0].url
}
output "service_account_email" {
value = google_service_account.vault.email
}