-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathload-balancer.tf
140 lines (121 loc) · 4.04 KB
/
load-balancer.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
resource "google_compute_managed_ssl_certificate" "lb_proxy_ssl_cert" {
provider = google.offensive-pipeline
count = var.external_hostname != "" ? 1 : 0
name = "${local.gitlab_instance_name}-ssl-cert"
managed {
domains = [var.external_hostname]
}
}
resource "google_compute_health_check" "gitlab_service_hc" {
provider = google.offensive-pipeline
name = "${local.gitlab_instance_name}-backend-hc"
timeout_sec = 5
check_interval_sec = 30
healthy_threshold = 1
unhealthy_threshold = 3
dynamic "http_health_check" {
for_each = var.gitlab_instance_protocol == "http" ? [1] : []
content {
port_name = "http"
port_specification = "USE_NAMED_PORT"
request_path = "/robots.txt"
proxy_header = "NONE"
}
}
dynamic "https_health_check" {
for_each = var.gitlab_instance_protocol == "https" ? [1] : []
content {
port_name = "https"
port_specification = "USE_NAMED_PORT"
request_path = "/robots.txt"
proxy_header = "NONE"
}
}
}
resource "google_compute_security_policy" "lb_allow_operators_policy" {
provider = google.offensive-pipeline
name = "${local.gitlab_instance_name}-backend-allowed-ips"
rule {
action = "allow"
priority = "1000"
description = "Allow specific IPs"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = var.operator_ips
}
}
}
# Conditionally create rule for external integration address ranges
dynamic "rule" {
for_each = var.external_integration_ranges != null ? [1] : []
content {
action = "allow"
priority = 1001
description = "External Integration IP addresses"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = var.external_integration_ranges
}
}
}
}
rule {
action = "deny(502)"
priority = "2147483647"
description = "Default rule deny all addresses"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["*"]
}
}
}
}
resource "google_compute_backend_service" "gitlab_instance_service" {
provider = google.offensive-pipeline
name = "${local.gitlab_instance_name}-backend"
port_name = var.gitlab_instance_protocol
protocol = upper(var.gitlab_instance_protocol)
timeout_sec = 10
security_policy = google_compute_security_policy.lb_allow_operators_policy.self_link
backend {
group = google_compute_instance_group.gitlab_instance.self_link
}
health_checks = [
google_compute_health_check.gitlab_service_hc.self_link,
]
}
resource "google_compute_url_map" "service_url_map" {
provider = google.offensive-pipeline
name = "${local.gitlab_instance_name}-urlmap"
default_service = google_compute_backend_service.gitlab_instance_service.self_link
}
resource "google_compute_target_https_proxy" "lb_https_proxy" {
provider = google.offensive-pipeline
name = "${local.gitlab_instance_name}-https-proxy"
url_map = google_compute_url_map.service_url_map.self_link
ssl_certificates = (
var.external_hostname != "" ?
[google_compute_managed_ssl_certificate.lb_proxy_ssl_cert.0.self_link] : []
)
}
resource "google_compute_global_forwarding_rule" "lb_forward_rule" {
provider = google.offensive-pipeline
name = "${local.gitlab_instance_name}-fwd-rule"
target = google_compute_target_https_proxy.lb_https_proxy.self_link
port_range = "443"
}
# https://cloud.google.com/load-balancing/docs/firewall-rules
resource "google_compute_firewall" "allow_lb_gitlab_access" {
provider = google.offensive-pipeline
name = "${local.gitlab_instance_name}-allow-lb"
network = module.gcp-network.network_name
source_ranges = ["130.211.0.0/22", "35.191.0.0/16"]
target_tags = google_compute_instance.gitlab.tags
allow {
protocol = "tcp"
ports = var.operator_ports
}
}