Skip to content

Commit 9f08a93

Browse files
committed
terraform: adjust worker config
Remove the load balancer config. We will not be using a load balancer. Make more variables private, by removing them from the config. Add a README. Improve how the issue repo is specified. Configure a GitHub access token. Add a .gitignore file for terraform's internal data. Change-Id: Ifeaa4528faa9988be424af3c943d50791c0405ff Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/373674 Trust: Jonathan Amsterdam <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Julie Qiu <[email protected]>
1 parent 6574ac5 commit 9f08a93

File tree

4 files changed

+130
-85
lines changed

4 files changed

+130
-85
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**/.terraform/*
2+
.terraform.lock.hcl
3+
terraform/terraform.tfvars

terraform/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Terraform configuration for vuln worker
2+
3+
## External variables
4+
5+
Some inputs to this config are not checked into the repo.
6+
You can provide them on the `terraform` command line,
7+
or create a `terraform.tfvars` file in this directory
8+
with the information, like this one:
9+
10+
```
11+
prod_project = "prod-project"
12+
prod_issue_repo = "org/repo"
13+
prod_client_id = "[email protected]"
14+
15+
dev_project = "dev-project"
16+
dev_issue_repo = "org/dev-repo"
17+
dev_client_id = "abc@@apps.googleusercontent.com"
18+
```
19+
20+
`terraform.tfvars` is in the repo's `.gitignore` file, so it won't show up in
21+
`git status`. **Do not** check it into the repo.
22+
23+
## Cloud Run image
24+
25+
We use terraform to set up the Cloud Run service, but we deploy in other ways.
26+
Our deploy process changes only the Docker image for the service. If we
27+
hardcoded a Docker image into the config, our config would often be out of date
28+
(since we apply it rarely compared to deploying), and we would risk overwriting
29+
a newer image with the old one in the config.
30+
31+
For that reason, the Docker image in the config is obtained from the service
32+
itself, by using a `data` block:
33+
34+
```
35+
resource "google_cloud_run_service" "worker" {
36+
...
37+
template {
38+
spec {
39+
containers {
40+
image = data.google_cloud_run_service.worker.template[0].spec[0].containers[0].image
41+
...
42+
}
43+
44+
data "google_cloud_run_service" "worker" {
45+
name = "${var.env}-vuln-worker"
46+
project = var.project
47+
location = var.region
48+
}
49+
```
50+
51+
This works fine once the service exists, but before it does we have a circularity:
52+
to create the service we need to get the image from the service!
53+
54+
So to create the service:
55+
56+
1. Build and push a Docker image.
57+
2. Replace the `data.google_cloud_run_service.worker` expressions (there are
58+
two) with the actual image label.
59+
3. Run `terraform apply`.
60+
4. Undo the replacement.

terraform/environment/worker.tf

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ variable "oauth_client_id" {
3737
type = string
3838
}
3939

40-
variable "oauth_client_secret" {
41-
description = "OAuth 2 client ID (visit APIs & Services > Credentials, click on client)"
40+
variable "issue_repo" {
41+
description = "name of GitHub repo to post issues on"
4242
type = string
4343
}
4444

@@ -47,6 +47,7 @@ variable "oauth_client_secret" {
4747
# Cloud Run service.
4848

4949
resource "google_cloud_run_service" "worker" {
50+
provider = google-beta
5051

5152
lifecycle {
5253
ignore_changes = [
@@ -64,8 +65,9 @@ resource "google_cloud_run_service" "worker" {
6465
template {
6566
spec {
6667
containers {
67-
# Don't hardcode the image here; get it from GCP. See the "data" block
68-
# below for more.
68+
# Get the image from GCP (see the "data" block below).
69+
# Exception: when first creating the service, replace this with a hardcoded
70+
# image tag.
6971
image = data.google_cloud_run_service.worker.template[0].spec[0].containers[0].image
7072
env {
7173
name = "GOOGLE_CLOUD_PROJECT"
@@ -81,7 +83,16 @@ resource "google_cloud_run_service" "worker" {
8183
}
8284
env {
8385
name = "VULN_WORKER_ISSUE_REPO"
84-
value = var.env == "dev"? "": "golang/vulndb"
86+
value = var.issue_repo
87+
}
88+
env {
89+
name = "VULN_GITHUB_ACCESS_TOKEN"
90+
value_from {
91+
secret_key_ref {
92+
name = google_secret_manager_secret.vuln_github_access_token.secret_id
93+
key = "latest"
94+
}
95+
}
8596
}
8697
env{
8798
name = "VULN_WORKER_USE_PROFILER"
@@ -95,7 +106,7 @@ resource "google_cloud_run_service" "worker" {
95106
}
96107
}
97108

98-
service_account_name = "frontend@${var.project}.iam.gserviceaccount.com"
109+
service_account_name = data.google_compute_default_service_account.default.email
99110
# 60 minutes is the maximum Cloud Run request time.
100111
timeout_seconds = 60 * 60
101112
}
@@ -104,7 +115,7 @@ resource "google_cloud_run_service" "worker" {
104115
annotations = {
105116
"autoscaling.knative.dev/minScale" = var.min_frontend_instances
106117
"autoscaling.knative.dev/maxScale" = "1"
107-
"client.knative.dev/user-image" = data.google_cloud_run_service.worker.template[0].spec[0].containers[0].image
118+
#"client.knative.dev/user-image" = data.google_cloud_run_service.worker.template[0].spec[0].containers[0].image
108119
}
109120
}
110121
}
@@ -129,83 +140,31 @@ data "google_cloud_run_service" "worker" {
129140
}
130141

131142
################################################################
132-
# Load balancer for Cloud Run service.
143+
# Other components.
133144

134-
resource "google_compute_region_network_endpoint_group" "worker" {
135-
count = var.oauth_client_secret == ""? 0: 1
136-
name = "${var.env}-vuln-worker-neg"
137-
network_endpoint_type = "SERVERLESS"
138-
project = var.project
139-
region = var.region
140-
cloud_run {
141-
service = google_cloud_run_service.worker.name
142-
}
145+
locals {
146+
tz = "America/New_York"
143147
}
144148

145-
module "worker_lb" {
146-
count = var.oauth_client_secret == ""? 0: 1
147-
source = "GoogleCloudPlatform/lb-http/google//modules/serverless_negs"
148-
version = "~> 6.1.1"
149-
150-
name = "${var.env}-vuln-worker-lb"
149+
resource google_secret_manager_secret "vuln_github_access_token" {
150+
secret_id = "vuln-${var.env}-github-access-token"
151151
project = var.project
152-
153-
ssl = true
154-
managed_ssl_certificate_domains = ["${var.env}-vuln-worker.go.dev"]
155-
https_redirect = true
156-
157-
backends = {
158-
default = {
159-
description = null
160-
groups = [
161-
{
162-
group = google_compute_region_network_endpoint_group.worker[0].id
163-
}
164-
]
165-
enable_cdn = false
166-
security_policy = null
167-
custom_request_headers = null
168-
custom_response_headers = null
169-
170-
iap_config = {
171-
enable = true
172-
oauth2_client_id = var.oauth_client_id
173-
oauth2_client_secret = var.oauth_client_secret
174-
}
175-
log_config = {
176-
enable = false
177-
sample_rate = null
178-
}
179-
}
152+
replication {
153+
automatic = true
180154
}
181155
}
182156

183-
output "worker_url" {
184-
value = data.google_cloud_run_service.worker.status[0].url
185-
}
186-
187-
output "load_balancer_ip" {
188-
value = var.oauth_client_secret == ""? "": module.worker_lb[0].external_ip
189-
}
190-
191-
################################################################
192-
# Other components.
193-
194-
locals {
195-
tz = "America/New_York"
196-
}
197-
198157
data "google_compute_default_service_account" "default" {
199158
project = var.project
200159
}
201160

202-
resource "google_cloud_scheduler_job" "issue_triage" {
203-
name = "${var.env}-issue-triage"
161+
resource "google_cloud_scheduler_job" "vuln_issue_triage" {
162+
name = "vuln-${var.env}-issue-triage"
204163
description = "Updates the DB and files issues."
205164
schedule = "0 * * * *" # every hour
206165
time_zone = local.tz
207166
project = var.project
208-
attempt_deadline = format("%ds", 60 * 60)
167+
attempt_deadline = format("%ds", 30 * 60)
209168

210169
http_target {
211170
http_method = "POST"

terraform/main.tf

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,58 @@ provider "google" {
3333
# by you, and pass them to terraform.
3434
# See https://www.terraform.io/docs/language/values/variables.html#variable-definitions-tfvars-files.
3535

36-
variable "prod_client_secret" {
37-
description = "OAuth 2 client secret for prod"
36+
37+
variable "prod_project" {
38+
description = "GCP project where resources live"
3839
type = string
39-
sensitive = true
4040
}
4141

42+
variable "prod_issue_repo" {
43+
description = "repo where issues are filed"
44+
type = string
45+
}
4246

47+
variable "prod_client_id" {
48+
description = "OAuth2 client ID"
49+
type = string
50+
}
51+
52+
variable "dev_project" {
53+
description = "GCP project where resources live"
54+
type = string
55+
}
56+
57+
variable "dev_issue_repo" {
58+
description = "repo where issues are filed"
59+
type = string
60+
}
61+
62+
variable "dev_client_id" {
63+
description = "OAuth2 client ID"
64+
type = string
65+
}
4366

4467
# Deployment environments
4568

4669
module "dev" {
4770
source = "./environment"
4871
env = "dev"
49-
project = "go-discovery-exp"
72+
project = var.dev_project
5073
region = local.region
5174
use_profiler = false
5275
min_frontend_instances = 0
53-
oauth_client_id = "55665122702-tk2rogkaalgru7pqibvbltqs7geev8j5.apps.googleusercontent.com"
54-
oauth_client_secret = "" # go-discovery-exp does not allow external load balancers
76+
oauth_client_id = var.dev_client_id
77+
issue_repo = var.dev_issue_repo
5578
}
5679

57-
# module "prod" {
58-
# source = "./environment"
59-
# env = "prod"
60-
# project = "golang-org"
61-
# region = local.region
62-
# use_profiler = true
63-
# min_frontend_instances = 1
64-
# client_id = "unknown"
65-
# client_secret = var.prod_client_secret
66-
# }
80+
module "prod" {
81+
source = "./environment"
82+
env = "prod"
83+
project = var.prod_project
84+
region = local.region
85+
use_profiler = true
86+
min_frontend_instances = 1
87+
oauth_client_id = var.prod_client_id
88+
issue_repo = var.prod_issue_repo
89+
}
6790

0 commit comments

Comments
 (0)