-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
192 lines (173 loc) · 4.74 KB
/
main.tf
File metadata and controls
192 lines (173 loc) · 4.74 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
provider "google" {
project = var.project
region = substr(var.zone, 0, length(var.zone) - 2)
zone = var.zone
}
resource "random_password" "psk" {
length = 32
lower = true
upper = true
number = true
special = true
override_special = "+/"
}
locals {
ddclientconf = <<-EOT
# Configuration file for ddclient
#
# /etc/ddclient.conf
protocol=dyndns2
use=web
server=${var.dyndns.server}
ssl=yes
login=${var.dyndns.user}
password='${var.dyndns.password}'
${var.hostname}
EOT
ddclient = <<-EOT
# Configuration for ddclient scripts
#
# /etc/default/ddclient
# Set to "true" if ddclient should be run every time DHCP client ('dhclient'
# from package isc-dhcp-client) updates the systems IP address.
run_dhclient="false"
# Set to "true" if ddclient should be run every time a new ppp connection is
# established. This might be useful, if you are using dial-on-demand.
run_ipup="false"
# Set to "true" if ddclient should run in daemon mode
# If this is changed to true, run_ipup and run_dhclient must be set to false.
run_daemon="true"
# Set the time interval between the updates of the dynamic DNS name in
# seconds.
# This option only takes effect if the ddclient runs in daemon mode.
daemon_interval="300"
EOT
ikev2psk = <<-EOT
conn ikev2-psk-${var.ipsec_identifier}
authby=secret
left=%defaultroute
leftid=@${var.hostname}
leftsubnet=0.0.0.0/0
# Clients
right=%any
# your addresspool to use
# you might need NAT rules if providing full internet to clients
rightaddresspool=192.168.66.1-192.168.66.254
rightid=@${var.ipsec_identifier}
#
# connection configuration
# DNS servers for clients to use
modecfgdns=8.8.8.8,8.8.4.4
narrowing=yes
# recommended dpd/liveness to cleanup vanished clients
dpddelay=30
dpdtimeout=120
dpdaction=clear
auto=add
ikev2=insist
rekey=no
# ikev2 fragmentation support requires libreswan 3.14 or newer
fragmentation=yes
# optional PAM username verification (eg to implement bandwidth quota
# pam-authorize=yes
ike=aes_gcm-aes_xcbc,aes_cbc-sha2
EOT
config = <<-EOT
#cloud-config
packages:
- libreswan
- nftables
- firewalld
- ddclient
package_update: true
package_upgrade: true
package_reboot_if_required: true
write_files:
- encoding: b64
content: ${base64encode(local.ddclientconf)}
owner: root:root
path: /etc/ddclient.conf
permissions: '0600'
- encoding: b64
content: ${base64encode(local.ddclient)}
owner: root:root
path: /etc/default/ddclient
permissions: '0600'
- encoding: b64
content: ${base64encode(local.ikev2psk)}
owner: root:root
path: /etc/ipsec.d/ikev2-psk-${var.ipsec_identifier}.conf
- encoding: b64
content: ${
base64encode(
format(
"@${var.ipsec_identifier} @${var.hostname}: PSK \"%s\"",
random_password.psk.result
)
)
}
owner: root:root
path: /etc/ipsec.d/ikev2-psk.secrets
permissions: '0600'
runcmd:
- [ sed,
-i,
"s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g",
/etc/sysctl.conf ]
- [ sysctl, -w, net.ipv4.ip_forward=1 ]
- [ systemctl, enable, ipsec.service ]
- [ systemctl, start, ipsec.service ]
- [ systemctl, restart, ddclient.service ]
- [ sed,
-i,
"s/FirewallBackend=iptables/FirewallBackend=nftables/g",
/etc/firewalld/firewalld.conf ]
- [ systemctl, restart, firewalld.service ]
- firewall-cmd --zone=external
--change-interface="$(ip route show to default | awk '{printf $5}')"
--permanent
- [ firewall-cmd,
--zone=external,
--add-port=500/udp,
--add-port=4500/udp,
--permanent ]
- [ firewall-cmd,
--zone=external,
--add-source=192.168.66.0/24,
--permanent ]
- [ firewall-cmd, --reload ]
EOT
}
resource "google_compute_firewall" "vpn" {
name = "allow-isakmp-ipsec-nat-t"
network = "default"
target_tags = ["vpn-server"]
allow {
protocol = "udp"
ports = ["500", "4500"]
}
}
resource "google_compute_instance" "vpn" {
name = var.instance_name
machine_type = "e2-micro"
can_ip_forward = true
tags = ["vpn-server"]
metadata = {
"user-data" = local.config
}
boot_disk {
auto_delete = true
initialize_params {
image = "ubuntu-os-cloud/ubuntu-minimal-2004-lts"
}
}
network_interface {
network = "default"
access_config {
}
}
shielded_instance_config {
enable_vtpm = true
enable_integrity_monitoring = true
}
}