Project

General

Profile

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

Yaron Sheffer, 26.12.2011 23:04

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 1 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.
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 4 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 4 Yaron Sheffer
h3. /etc/ipsec.conf on the client
31 4 Yaron Sheffer
32 4 Yaron Sheffer
<pre>
33 4 Yaron Sheffer
# ipsec.conf - strongSwan IPsec configuration file
34 4 Yaron Sheffer
35 4 Yaron Sheffer
# basic configuration
36 4 Yaron Sheffer
37 4 Yaron Sheffer
config setup
38 4 Yaron Sheffer
	# nat_traversal=yes
39 4 Yaron Sheffer
	charonstart=yes
40 4 Yaron Sheffer
	plutostart=no
41 4 Yaron Sheffer
	# charondebug="ike 2, knl 2, cfg 2, mgr 2, chd 2, net 2"
42 4 Yaron Sheffer
43 4 Yaron Sheffer
# Connections into AWS VPC
44 4 Yaron Sheffer
conn %default
45 4 Yaron Sheffer
	ikelifetime=60m
46 4 Yaron Sheffer
	keylife=20m
47 4 Yaron Sheffer
	rekeymargin=3m
48 4 Yaron Sheffer
	keyingtries=1
49 4 Yaron Sheffer
	keyexchange=ikev2
50 4 Yaron Sheffer
	authby=secret
51 4 Yaron Sheffer
52 4 Yaron Sheffer
conn us-east-1-vpc
53 4 Yaron Sheffer
	left=%any
54 4 Yaron Sheffer
	leftsourceip=%config
55 4 Yaron Sheffer
	leftid=yaronf@porticor.com
56 4 Yaron Sheffer
	leftfirewall=yes
57 4 Yaron Sheffer
	right=<gateway's elastic IP>
58 4 Yaron Sheffer
	rightsubnet=10.10.0.0/16
59 4 Yaron Sheffer
	rightid=@us-east-gw.porticor.com
60 4 Yaron Sheffer
	auto=start
61 4 Yaron Sheffer
62 4 Yaron Sheffer
# Add connections here.
63 4 Yaron Sheffer
64 4 Yaron Sheffer
# include /var/lib/strongswan/ipsec.conf.inc
65 4 Yaron Sheffer
</pre>
66 4 Yaron Sheffer
67 4 Yaron Sheffer
h3. /etc/ipsec.secrets on the Client
68 4 Yaron Sheffer
69 4 Yaron Sheffer
<pre>
70 4 Yaron Sheffer
us-east-gw.porticor.com : PSK "aa82c7a776e2175114213acc02dda9951a6bc25deb433e6d5d6ef7058626c589"
71 4 Yaron Sheffer
</pre>
72 4 Yaron Sheffer
73 4 Yaron Sheffer
h3. /etc/ipsec.conf on the Gateway
74 4 Yaron Sheffer
75 4 Yaron Sheffer
<pre>
76 4 Yaron Sheffer
# ipsec.conf - strongSwan IPsec configuration file
77 4 Yaron Sheffer
78 4 Yaron Sheffer
# basic configuration
79 4 Yaron Sheffer
80 4 Yaron Sheffer
config setup
81 4 Yaron Sheffer
	# nat_traversal=yes
82 4 Yaron Sheffer
	charonstart=yes
83 4 Yaron Sheffer
	plutostart=no
84 4 Yaron Sheffer
        # charondebug="ike 2, knl 2, cfg 2, mgr 3, chd 2, net 2"
85 4 Yaron Sheffer
86 4 Yaron Sheffer
# /etc/ipsec.conf - strongSwan IPsec configuration file
87 4 Yaron Sheffer
88 4 Yaron Sheffer
conn %default
89 4 Yaron Sheffer
	ikelifetime=60m
90 4 Yaron Sheffer
	keylife=20m
91 4 Yaron Sheffer
	rekeymargin=3m
92 4 Yaron Sheffer
	keyingtries=1
93 4 Yaron Sheffer
	keyexchange=ikev2
94 4 Yaron Sheffer
	authby=secret
95 4 Yaron Sheffer
96 4 Yaron Sheffer
conn client
97 4 Yaron Sheffer
	# the leftid is not a real DNS name
98 4 Yaron Sheffer
	leftid=us-east-gw.porticor.com
99 4 Yaron Sheffer
	left=10.10.0.10
100 4 Yaron Sheffer
	# We are protecting the entire VPC, not just this subnet
101 4 Yaron Sheffer
	leftsubnet=10.10.0.0/16
102 4 Yaron Sheffer
	leftfirewall=yes
103 4 Yaron Sheffer
	right=%any
104 4 Yaron Sheffer
	# The virtual IP pool is outside the VPC!
105 4 Yaron Sheffer
	rightsourceip=10.100.255.0/28
106 4 Yaron Sheffer
	auto=add
107 4 Yaron Sheffer
108 4 Yaron Sheffer
# Add connections here.
109 4 Yaron Sheffer
110 4 Yaron Sheffer
# include /var/lib/strongswan/ipsec.conf.inc
111 4 Yaron Sheffer
</pre>
112 4 Yaron Sheffer
113 4 Yaron Sheffer
h3. /etc/ipsec.secrets on the Gateway
114 4 Yaron Sheffer
<pre>
115 4 Yaron Sheffer
yaronf@porticor.com : PSK "aa82c7a776e2175114213acc02dda9951a6bc25deb433e6d5d6ef7058626c589"
116 4 Yaron Sheffer
</pre>