Skip to content

Commit 8c3d066

Browse files
authored
Stabilize WPT Server Infrastructure (Bridge Networking, Port Alignment & State Migration) (#84)
* feat(security): run WPT server as unprivileged users and migrate state to GCS Hardened the WPT server environment by removing root execution and sudo dependencies. Migrated Terraform state to GCS and removed legacy Container VM agent overrides. Infrastructure (Terraform): - Created GCS bucket `gs://wpt-live-app-tfstate` with versioning for state storage. - Switched instance templates to use a dedicated Service Account (`wpt-tot-app-sa`). - Added IAM role `roles/storage.objectViewer` to the SA for certificate retrieval. - Migrated local state to `gs://wpt-live-app-tfstate/terraform/state`. - Retired legacy Container VM agent in favor of standard cloud-init. Web Server (Docker/Supervisor): - Created `wpt-server` (UID 1000) and `wpt-sync` (UID 1001) users. - Granted `CAP_NET_BIND_SERVICE` to python3.10 to allow unprivileged binding to ports 80/443. - Shifted all file paths and `git` operations inside `/home/wpt-sync` instead of `/root`. - Unified supervisord process execution without `sudo`. * address feedback * more tidy up
1 parent 7aa5f39 commit 8c3d066

File tree

15 files changed

+235
-1649
lines changed

15 files changed

+235
-1649
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
/.terraform/
1+
.terraform/
22
google-cloud-platform-credentials.json
3+
terraform.tfstate
34
terraform.tfstate.backup

.terraform.lock.hcl

Lines changed: 29 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infrastructure/docker-image/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
docker = {
66
source = "kreuzwerker/docker"
7-
version = "3.0.2"
7+
version = "3.9.0"
88
}
99
}
1010
}

infrastructure/web-platform-tests/.terraform.lock.hcl

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infrastructure/web-platform-tests/compute.tf

Lines changed: 78 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,46 @@
1010
# More information about how it was used previously: https://github.com/web-platform-tests/wpt.live/blob/67dc5976ccce2e64483f2028a35659d4d6e58891/infrastructure/web-platform-tests/main.tf#L69-L137
1111
########################################
1212

13-
resource "google_compute_health_check" "wpt_health_check" {
14-
name = "${var.name}-wpt-servers"
1513

16-
check_interval_sec = 10
17-
timeout_sec = 10
18-
healthy_threshold = 3
19-
unhealthy_threshold = 6
14+
resource "google_service_account" "wpt_live_sa" {
15+
account_id = "${var.name}-sa"
16+
display_name = "WPT Live Node Service Account"
17+
}
2018

21-
https_health_check {
22-
port = "443"
23-
# A query parameter is used to distinguish the health check in the server's
24-
# request logs.
25-
request_path = "/?gcp-health-check"
26-
}
19+
resource "google_project_iam_member" "sa_logging" {
20+
project = data.google_project.project.project_id
21+
role = "roles/logging.logWriter"
22+
member = "serviceAccount:${google_service_account.wpt_live_sa.email}"
2723
}
2824

29-
resource "google_compute_instance_group_manager" "wpt_servers" {
30-
name = "${var.name}-wpt-servers"
31-
zone = var.zone
32-
description = "compute VM Instance Group"
33-
wait_for_instances = false
34-
base_instance_name = "${var.name}-wpt-servers"
25+
resource "google_storage_bucket_iam_member" "sa_storage" {
26+
bucket = google_storage_bucket.certificates.name
27+
role = "roles/storage.objectViewer"
28+
member = "serviceAccount:${google_service_account.wpt_live_sa.email}"
29+
}
30+
31+
resource "google_project_iam_member" "sa_artifactregistry" {
32+
project = data.google_project.project.project_id
33+
role = "roles/artifactregistry.reader"
34+
member = "serviceAccount:${google_service_account.wpt_live_sa.email}"
35+
}
36+
37+
resource "google_compute_region_instance_group_manager" "wpt_server_cloud_init" {
38+
name = "${var.name}-instance-group-cloud-init"
39+
base_instance_name = "${var.name}-cloud-init"
40+
region = var.region
41+
distribution_policy_zones = [var.zone]
42+
3543
version {
36-
name = "${var.name}-wpt-servers-default"
37-
instance_template = google_compute_instance_template.wpt_server.self_link
44+
instance_template = google_compute_instance_template.wpt_server_cloud_init.id
3845
}
46+
3947
update_policy {
40-
type = local.update_policy.type
41-
minimal_action = local.update_policy.minimal_action
48+
minimal_action = local.update_policy.minimal_action
49+
type = local.update_policy.type
4250
max_unavailable_fixed = local.update_policy.max_unavailable_fixed
4351
}
52+
4453
target_pools = [google_compute_target_pool.default.self_link]
4554
target_size = 2
4655

@@ -54,43 +63,15 @@ resource "google_compute_instance_group_manager" "wpt_servers" {
5463

5564
auto_healing_policies {
5665
health_check = google_compute_health_check.wpt_health_check.self_link
57-
initial_delay_sec = 30
58-
}
59-
}
60-
61-
resource "google_compute_firewall" "wpt-server-mig-health-check" {
62-
name = "${var.name}-wpt-servers-vm-hc"
63-
network = var.network_name
64-
65-
allow {
66-
protocol = "tcp"
67-
# https port
68-
ports = [var.wpt_server_ports[2].port]
66+
initial_delay_sec = 180
6967
}
70-
71-
# This range comes from this module that was used previously:
72-
# https://github.com/Ecosystem-Infra/terraform-google-multi-port-managed-instance-group/blob/master/main.tf#L347
73-
source_ranges = ["130.211.0.0/22", "35.191.0.0/16"]
7468
}
7569

76-
resource "google_compute_instance_template" "wpt_server" {
77-
name_prefix = "default-"
70+
resource "google_compute_instance_template" "wpt_server_cloud_init" {
71+
name_prefix = "cloud-init-"
7872

79-
# As of 2020-06-17, we were running into OOM issues with the 1.7 GB
80-
# "g1-small" instance[1]. This was suspected to be due to 'git gc' needing
81-
# more memory, so we upgraded to "e2-medium" (4 GB of RAM).
82-
#
83-
# [1] https://github.com/web-platform-tests/wpt.live/issues/30
8473
machine_type = "e2-medium"
8574

86-
# The "google-logging-enabled" metadata is undocumented, but it is apparently
87-
# necessary to enable the capture of logs from the Docker image.
88-
#
89-
# https://github.com/GoogleCloudPlatform/konlet/issues/56
90-
labels = {
91-
"${module.wpt-server-container.vm_container_label_key}" = module.wpt-server-container.vm_container_label
92-
}
93-
9475
network_interface {
9576
network = var.network_name
9677
subnetwork = var.subnetwork_name
@@ -101,41 +82,75 @@ resource "google_compute_instance_template" "wpt_server" {
10182

10283
can_ip_forward = false
10384

104-
// Create a new boot disk from an image
10585
disk {
10686
auto_delete = true
10787
boot = true
108-
source_image = module.wpt-server-container.source_image
88+
source_image = data.google_compute_image.cos.self_link
10989
type = "PERSISTENT"
11090
disk_type = "pd-ssd"
11191
disk_size_gb = var.wpt_server_disk_size
11292
mode = "READ_WRITE"
11393
}
11494

11595
service_account {
116-
email = "default"
117-
scopes = ["storage-ro", "logging-write"]
96+
email = google_service_account.wpt_live_sa.email
97+
scopes = ["cloud-platform"]
11898
}
11999

120100
scheduling {
121101
automatic_restart = true
122102
on_host_maintenance = "MIGRATE"
123103
}
124104

125-
# startup-script and tf_depends_id comes from the module previously used for wpt-server. (see link at top)
126-
# TODO: evaluate if those two should be removed.
127105
metadata = {
128-
"${module.wpt-server-container.metadata_key}" = module.wpt-server-container.metadata_value
129-
"startup-script" = ""
130-
"tf_depends_id" = ""
131-
"google-logging-enabled" = "true"
106+
"user-data" = templatefile("${path.module}/../../src/cloud-init.yaml", {
107+
WPT_HOST = var.host_name
108+
WPT_ALT_HOST = var.alt_host_name
109+
WPT_BUCKET = local.bucket_name
110+
WPT_SERVER_IMAGE = var.wpt_server_image
111+
})
112+
"google-logging-enabled" = "true"
132113
}
133114

134115
lifecycle {
135116
create_before_destroy = true
136117
}
137118
}
138119

120+
resource "google_compute_firewall" "wpt-server-mig-health-check" {
121+
name = "${var.name}-wpt-servers-vm-hc"
122+
network = var.network_name
123+
124+
allow {
125+
protocol = "tcp"
126+
# https port
127+
ports = [var.wpt_server_ports[2].port]
128+
}
129+
130+
# This range comes from this module that was used previously:
131+
# https://github.com/Ecosystem-Infra/terraform-google-multi-port-managed-instance-group/blob/master/main.tf#L347
132+
source_ranges = ["130.211.0.0/22", "35.191.0.0/16"]
133+
}
134+
135+
136+
137+
resource "google_compute_health_check" "wpt_health_check" {
138+
name = "${var.name}-wpt-servers"
139+
140+
check_interval_sec = 10
141+
timeout_sec = 10
142+
healthy_threshold = 3
143+
unhealthy_threshold = 6
144+
145+
https_health_check {
146+
port = "443"
147+
# A query parameter is used to distinguish the health check in the server's
148+
# request logs.
149+
request_path = "/?gcp-health-check"
150+
}
151+
}
152+
153+
139154
########################################
140155
# Cert Renewers
141156
########################################

infrastructure/web-platform-tests/load-balancing.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ resource "google_compute_target_pool" "default" {
2929
]
3030
}
3131

32+
3233
resource "google_compute_http_health_check" "default" {
3334
name = "${local.lb_name}-health-check"
3435

infrastructure/web-platform-tests/main.tf

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,22 @@ locals {
33

44
update_policy = {
55
type = "PROACTIVE"
6-
minimal_action = "RESTART"
6+
minimal_action = "REPLACE"
77
# > maxUnavailable must be greater than 0 when minimal action is set to
88
# > RESTART
99
max_unavailable_fixed = 1
1010
}
1111

1212
}
1313

14-
module "wpt-server-container" {
15-
source = "terraform-google-modules/container-vm/google"
16-
version = "3.0.0"
17-
18-
container = {
19-
image = var.wpt_server_image
20-
env = [
21-
{
22-
name = "WPT_HOST"
23-
value = var.host_name
24-
},
25-
{
26-
name = "WPT_ALT_HOST"
27-
value = var.alt_host_name
28-
},
29-
{
30-
name = "WPT_BUCKET"
31-
value = local.bucket_name
32-
},
33-
]
34-
}
35-
36-
restart_policy = "Always"
37-
}
3814

3915
resource "google_storage_bucket" "certificates" {
4016
name = local.bucket_name
4117
location = "US"
4218
uniform_bucket_level_access = true
4319
}
20+
21+
data "google_compute_image" "cos" {
22+
family = "cos-stable"
23+
project = "cos-cloud"
24+
}

0 commit comments

Comments
 (0)