Project

General

Profile

Setting Up a VPN into Amazon's Public Cloud VPC » History » Version 5

Yaron Sheffer, 26.12.2011 23:09

1 1 Yaron Sheffer
h1. Setting Up a VPN into Amazon's Public Cloud VPC
2 1 Yaron Sheffer
3 1 Yaron Sheffer
The AWS VPC (Virtual Private Cloud) is somewhat inconvenient for developers. The standard way to access it is through an IPsec "hardware VPN". In practice this means having to deal with BGP, in addition to IPsec. This howto simplifies things by using StrongSwan to access the VPC instances. Neither hardware nor BGP are required.
4 1 Yaron Sheffer
5 1 Yaron Sheffer
h2. Scenario
6 1 Yaron Sheffer
7 5 Yaron Sheffer
We assume a single VPC subnet with Internet access (i.e., located behind an Internet Gateway). We have a small number of clients accessing the VPC remotely, from Linux machines. I believe the solution can be tweaked to allow for larger deployments. For example, you will want to replace preshared key by certificate-based authentication to support a large number of clients.
8 1 Yaron Sheffer
9 1 Yaron Sheffer
h2. Solution Overview
10 1 Yaron Sheffer
11 1 Yaron Sheffer
We create a new, dedicated instance serving as a VPN gateway for the whole VPC. The solution uses tunnel-mode IPsec with IKEv2 and a virtual IP pool. StrongSwan is deployed on both client and gateway.
12 1 Yaron Sheffer
13 1 Yaron Sheffer
h2. Solution Steps
14 1 Yaron Sheffer
15 4 Yaron Sheffer
# Create a new VPC instance (the minimal instance type in VPC is Small). We have used a standard Ubuntu Oneiric image, @ami-a562a9cc@. You can use an existing instance but that would be much less secure. Note that the VPN Gateway instance can be stopped when not in use, and later restarted.
16 2 Yaron Sheffer
# Disable source/destination check on the instance.
17 1 Yaron Sheffer
# Assign an Elastic IP for the instance. This will be the gateway's address, but first we will use it to access the gateway and install it.
18 4 Yaron Sheffer
# Install StrongSwan on the gateway (and on your client, too). We have used the version available in the repository, 4.5.2. Modify the configuration files per the next section.
19 4 Yaron Sheffer
# Enable IP forwarding on the gateway:
20 4 Yaron Sheffer
 * For the current session, run @echo 1 > /proc/sys/net/ipv4/ip_forward@
21 4 Yaron Sheffer
 * We also want this setting to persist after a reboot: edit @/etc/sysctl.conf@ and uncomment the line @net.ipv4.ip_forward=1@.
22 1 Yaron Sheffer
# Define the gateway's security group(s) to allow incoming TCP/22, UDP/500 and UDP/4500.
23 1 Yaron Sheffer
 * Once the gateway is fully set up, you will be able to disable TCP/22 in the security group and tunnel SSH through IPsec instead of directly. You simply SSH into the gateway's private IP address.
24 1 Yaron Sheffer
# Define a subnet for the virtual IP pool. It doesn't need to be inside the VPC. In our example the VPC encompasses 10.10.0.0/16, and the virtual IP pool will be drawn from 10.100.0.0/16.
25 3 Yaron Sheffer
# IPsec clients will be assigned addresses from the virtual address pool. Note the instance ID of the VPN gateway. Then locate the route table associated with the subnet of protected instances (this may or may not be the main route table), and add a routing rule that routes all traffic destined to the pool's subnet (10.100.0.0./16) through the gateway.
26 4 Yaron Sheffer
# Allow any incoming traffic from this subnet into all VPC instances. For example, by adding an "all traffic" rule to the @default@ security group of your VPC.
27 5 Yaron Sheffer
# Finally, @sudo ipsec restart@ on the gateway and the client, and you are good to go!
28 1 Yaron Sheffer
29 1 Yaron Sheffer
h2. Configuration Files
30 1 Yaron Sheffer
31 5 Yaron Sheffer
h3. /etc/ipsec.conf on the Client
32 5 Yaron Sheffer
33 4 Yaron Sheffer
<pre>
34 4 Yaron Sheffer
# ipsec.conf - strongSwan IPsec configuration file
35 4 Yaron Sheffer
36 4 Yaron Sheffer
# basic configuration
37 4 Yaron Sheffer
38 4 Yaron Sheffer
config setup
39 4 Yaron Sheffer
	# nat_traversal=yes
40 4 Yaron Sheffer
	charonstart=yes
41 4 Yaron Sheffer
	plutostart=no
42 4 Yaron Sheffer
	# charondebug="ike 2, knl 2, cfg 2, mgr 2, chd 2, net 2"
43 4 Yaron Sheffer
44 4 Yaron Sheffer
# Connections into AWS VPC
45 4 Yaron Sheffer
conn %default
46 4 Yaron Sheffer
	ikelifetime=60m
47 4 Yaron Sheffer
	keylife=20m
48 4 Yaron Sheffer
	rekeymargin=3m
49 4 Yaron Sheffer
	keyingtries=1
50 4 Yaron Sheffer
	keyexchange=ikev2
51 4 Yaron Sheffer
	authby=secret
52 1 Yaron Sheffer
53 4 Yaron Sheffer
conn us-east-1-vpc
54 4 Yaron Sheffer
	left=%any
55 4 Yaron Sheffer
	leftsourceip=%config
56 5 Yaron Sheffer
	leftid=<my-email-address>
57 4 Yaron Sheffer
	leftfirewall=yes
58 4 Yaron Sheffer
	right=<gateway's elastic IP>
59 4 Yaron Sheffer
	rightsubnet=10.10.0.0/16
60 4 Yaron Sheffer
	rightid=@us-east-gw.porticor.com
61 4 Yaron Sheffer
	auto=start
62 4 Yaron Sheffer
63 4 Yaron Sheffer
# Add connections here.
64 4 Yaron Sheffer
65 4 Yaron Sheffer
# include /var/lib/strongswan/ipsec.conf.inc
66 4 Yaron Sheffer
</pre>
67 4 Yaron Sheffer
68 4 Yaron Sheffer
h3. /etc/ipsec.secrets on the Client
69 4 Yaron Sheffer
70 4 Yaron Sheffer
<pre>
71 4 Yaron Sheffer
us-east-gw.porticor.com : PSK "aa82c7a776e2175114213acc02dda9951a6bc25deb433e6d5d6ef7058626c589"
72 4 Yaron Sheffer
</pre>
73 4 Yaron Sheffer
74 4 Yaron Sheffer
h3. /etc/ipsec.conf on the Gateway
75 4 Yaron Sheffer
76 4 Yaron Sheffer
<pre>
77 4 Yaron Sheffer
# ipsec.conf - strongSwan IPsec configuration file
78 4 Yaron Sheffer
79 4 Yaron Sheffer
# basic configuration
80 4 Yaron Sheffer
81 4 Yaron Sheffer
config setup
82 4 Yaron Sheffer
	# nat_traversal=yes
83 4 Yaron Sheffer
	charonstart=yes
84 4 Yaron Sheffer
	plutostart=no
85 4 Yaron Sheffer
        # charondebug="ike 2, knl 2, cfg 2, mgr 3, chd 2, net 2"
86 4 Yaron Sheffer
87 4 Yaron Sheffer
# /etc/ipsec.conf - strongSwan IPsec configuration file
88 4 Yaron Sheffer
89 4 Yaron Sheffer
conn %default
90 4 Yaron Sheffer
	ikelifetime=60m
91 4 Yaron Sheffer
	keylife=20m
92 4 Yaron Sheffer
	rekeymargin=3m
93 4 Yaron Sheffer
	keyingtries=1
94 4 Yaron Sheffer
	keyexchange=ikev2
95 4 Yaron Sheffer
	authby=secret
96 4 Yaron Sheffer
97 4 Yaron Sheffer
conn client
98 4 Yaron Sheffer
	# the leftid is not a real DNS name
99 4 Yaron Sheffer
	leftid=us-east-gw.porticor.com
100 4 Yaron Sheffer
	left=10.10.0.10
101 4 Yaron Sheffer
	# We are protecting the entire VPC, not just this subnet
102 4 Yaron Sheffer
	leftsubnet=10.10.0.0/16
103 4 Yaron Sheffer
	leftfirewall=yes
104 4 Yaron Sheffer
	right=%any
105 4 Yaron Sheffer
	# The virtual IP pool is outside the VPC!
106 4 Yaron Sheffer
	rightsourceip=10.100.255.0/28
107 4 Yaron Sheffer
	auto=add
108 4 Yaron Sheffer
109 4 Yaron Sheffer
# Add connections here.
110 4 Yaron Sheffer
111 1 Yaron Sheffer
# include /var/lib/strongswan/ipsec.conf.inc
112 4 Yaron Sheffer
</pre>
113 4 Yaron Sheffer
114 4 Yaron Sheffer
h3. /etc/ipsec.secrets on the Gateway
115 4 Yaron Sheffer
<pre>
116 5 Yaron Sheffer
<my-email-address> : PSK "aa82c7a776e2175114213acc02dda9951a6bc25deb433e6d5d6ef7058626c589"
117 4 Yaron Sheffer
</pre>