Skip to content

Commit f12683c

Browse files
authored
Merge pull request #1 from jmhale/feature/multi-client-support
Add multi-client support
2 parents 36557ba + 3ea846f commit f12683c

File tree

9 files changed

+60
-24
lines changed

9 files changed

+60
-24
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
## [0.0.2] - 2019-03-02
5+
### Added
6+
- Multi-client support via the module variable.
7+
- This CHANGELOG
8+
### Removed
9+
- Single-client public key via AWS SSM as it now conflicts with the module variable method.
10+
11+
## [0.0.1] - 2019-02-24
12+
### Added
13+
- Working module to deploy WireGuard with single client support.

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ A Terraform module to deploy a WireGuard VPN server on AWS.
66
Before using this module, you'll need to generate a key pair for your server and client, and store the server's private key and client's public key in AWS SSM, which cloud-init will source and add to WireGuard's configuration.
77

88
- Install the WireGuard tools for your OS: https://www.wireguard.com/install/
9-
- Generate a key pair for the client
10-
- `wg genkey | tee client-privatekey | wg pubkey > client-publickey`
9+
- Generate a key pair for each client
10+
- `wg genkey | tee client1-privatekey | wg pubkey > client1-publickey`
1111
- Generate a key pair for the server
1212
- `wg genkey | tee server-privatekey | wg pubkey > server-publickey`
13-
14-
- Add the client public key to the AWS SSM parameter: `/wireguard/wg-laptop-public-key`
15-
- `aws ssm put-parameter --name /wireguard/wg-laptop-public-key --type SecureString --value $ClientPublicKeyValue`
1613
- Add the server private key to the AWS SSM parameter: `/wireguard/wg-server-private-key`
1714
- `aws ssm put-parameter --name /wireguard/wg-server-private-key --type SecureString --value $ServerPrivateKeyValue`
15+
- Add each client's public key, along with the next available IP address as a key:value pair to the wg_client_public_keys map. See Usage for details.
1816

1917
## Variables
2018
| Variable Name | Type | Required |Description |
@@ -24,6 +22,8 @@ Before using this module, you'll need to generate a key pair for your server and
2422
|`vpc_id`|`string`|Yes|The VPC ID in which Terraform will launch the resources.|
2523
|`ingress_security_group_id`|`string`|Yes|The ID of the Security Group to allow SSH access from.|
2624
|`ami_id`|`string`|No. Defaults to Ubuntu 16.04 AMI in us-east-1|The AMI ID to use.|
25+
|`env`|`string`|No. Defaults "prod"|The environment for WireGuard|
26+
|`wg_client_public_keys`|`list`|Yes.|List of maps of client IPs and public keys. See Usage for details.|
2727

2828
## Usage
2929
```
@@ -32,6 +32,11 @@ module "wireguard" {
3232
ssh_key_id = "ssh-key-id-0987654"
3333
vpc_id = "vpc-01234567"
3434
public_subnet_ids = ["subnet-01234567"]
35+
wg_client_public_keys = [
36+
{"192.168.2.2/32" = "QFX/DXxUv56mleCJbfYyhN/KnLCrgp7Fq2fyVOk/FWU="},
37+
{"192.168.2.3/32" = "+IEmKgaapYosHeehKW8MCcU65Tf5e4aXIvXGdcUlI0Q="},
38+
{"192.168.2.4/32" = "WO0tKrpUWlqbl/xWv6riJIXipiMfAEKi51qvHFUU30E="},
39+
]
3540
}
3641
```
3742

main.tf

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@ data "template_file" "user_data" {
33

44
vars {
55
wg_server_private_key = "${data.aws_ssm_parameter.wg_server_private_key.value}"
6-
wg_laptop_public_key = "${data.aws_ssm_parameter.wg_laptop_public_key.value}"
6+
peers = "${join("\n", data.template_file.wg_client_data_json.*.rendered)}"
77
eip_id = "${aws_eip.wireguard_eip.id}"
88
}
99
}
1010

11+
data "template_file" "wg_client_data_json" {
12+
template = "${file("${path.module}/templates/client-data.tpl")}"
13+
count = "${length(var.wg_client_public_keys)}"
14+
15+
vars {
16+
client_pub_key = "${element(values(var.wg_client_public_keys[count.index]), 0)}"
17+
client_ip = "${element(keys(var.wg_client_public_keys[count.index]), 0)}"
18+
}
19+
}
20+
1121
data "template_cloudinit_config" "config" {
1222
part {
1323
content_type = "text/cloud-config"
@@ -20,7 +30,7 @@ resource "aws_eip" "wireguard_eip" {
2030
}
2131

2232
resource "aws_launch_configuration" "wireguard_launch_config" {
23-
name_prefix = "wireguard-lc-"
33+
name_prefix = "wireguard-${var.env}-lc-"
2434
image_id = "${var.ami_id}"
2535
instance_type = "t2.micro"
2636
key_name = "${var.ssh_key_id}"
@@ -35,7 +45,7 @@ resource "aws_launch_configuration" "wireguard_launch_config" {
3545
}
3646

3747
resource "aws_autoscaling_group" "wireguard_asg" {
38-
name_prefix = "wireguard-asg-"
48+
name_prefix = "wireguard-${var.env}-asg-"
3949
max_size = 1
4050
min_size = 1
4151
launch_configuration = "${aws_launch_configuration.wireguard_launch_config.name}"
@@ -50,7 +60,7 @@ resource "aws_autoscaling_group" "wireguard_asg" {
5060
tags = [
5161
{
5262
key = "Name"
53-
value = "wireguard"
63+
value = "wireguard-${var.env}"
5464
propagate_at_launch = true
5565
},
5666
{

templates/client-data.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[Peer]
2+
PublicKey = ${client_pub_key}
3+
AllowedIPs = ${client_ip}

templates/user-data.tpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ write_files:
1717
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
1818
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
1919

20-
[Peer]
21-
PublicKey = ${wg_laptop_public_key}
22-
AllowedIPs = 192.168.2.2/32
20+
${peers}
2321
runcmd:
2422
- export INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
2523
- export REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep -oP '\"region\"[[:space:]]*:[[:space:]]*\"\K[^\"]+')

variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
variable "ssh_key_id" {}
2+
23
variable "vpc_id" {}
34

45
variable "ami_id" {
@@ -8,3 +9,11 @@ variable "ami_id" {
89
variable "public_subnet_ids" {
910
type = "list"
1011
}
12+
13+
variable "wg_client_public_keys" {
14+
type = "list"
15+
}
16+
17+
variable "env" {
18+
default = "prod"
19+
}

wireguard-iam.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ data "aws_iam_policy_document" "wireguard_policy_doc" {
1717
"ec2:AssociateAddress",
1818
]
1919

20-
resources = ["*"] ## TODO: See if we can scope this to wireguard_eip
20+
resources = ["*"]
2121
}
2222
}
2323

2424
resource "aws_iam_policy" "wireguard_policy" {
25-
name = "tf-wireguard"
25+
name = "tf-wireguard-${var.env}"
2626
description = "Terraform Managed. Allows Wireguard instance to attach EIP."
2727
policy = "${data.aws_iam_policy_document.wireguard_policy_doc.json}"
2828
}
2929

3030
resource "aws_iam_role" "wireguard_role" {
31-
name = "tf-wireguard"
31+
name = "tf-wireguard-${var.env}"
3232
description = "Terraform Managed. Role to allow Wireguard instance to attach EIP."
3333
path = "/"
3434
assume_role_policy = "${data.aws_iam_policy_document.ec2_assume_role.json}"
@@ -40,6 +40,6 @@ resource "aws_iam_role_policy_attachment" "wireguard_roleattach" {
4040
}
4141

4242
resource "aws_iam_instance_profile" "wireguard_profile" {
43-
name = "tf-wireguard"
43+
name = "tf-wireguard-${var.env}"
4444
role = "${aws_iam_role.wireguard_role.name}"
4545
}

wireguard-securitygroups.tf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
resource "aws_security_group" "sg_wireguard_external" {
2-
name = "wireguard-external"
2+
name = "wireguard-${var.env}-external"
33
description = "Terraform Managed. Allow Wireguard client traffic from internet."
44
vpc_id = "${var.vpc_id}"
55

66
tags {
7-
Name = "wireguard-external"
7+
Name = "wireguard-${var.env}-external"
88
Project = "wireguard"
99
tf-managed = "True"
10+
env = "${var.env}"
1011
}
1112

1213
ingress {
@@ -25,14 +26,15 @@ resource "aws_security_group" "sg_wireguard_external" {
2526
}
2627

2728
resource "aws_security_group" "sg_wireguard_admin" {
28-
name = "wireguard-admin"
29+
name = "wireguard-${var.env}-admin"
2930
description = "Terraform Managed. Allow admin traffic to internal resources from VPN"
3031
vpc_id = "${var.vpc_id}"
3132

3233
tags {
33-
Name = "wireguard-admin"
34+
Name = "wireguard-${var.env}-admin"
3435
Project = "vpn"
3536
tf-managed = "True"
37+
env = "${var.env}"
3638
}
3739

3840
ingress {

wireguard-ssm.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
data "aws_ssm_parameter" "wg_server_private_key" {
22
name = "/wireguard/wg-server-private-key"
33
}
4-
5-
data "aws_ssm_parameter" "wg_laptop_public_key" {
6-
name = "/wireguard/wg-laptop-public-key"
7-
}

0 commit comments

Comments
 (0)