Setting Up a VPN into Amazon's Public Cloud VPC » History » Version 12
Adam Feuer, 18.05.2012 00:02
1 | 1 | Yaron Sheffer | h1. Setting Up a VPN into Amazon's Public Cloud VPC |
---|---|---|---|
2 | 1 | Yaron Sheffer | |
3 | 10 | Tobias Brunner | "Amazon Web Services' VPC":http://aws.amazon.com/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 not just with IPsec, but also with BGP. This document 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 | 6 | 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, all running on Linux machines. I believe the solution can be tweaked to allow for larger deployments. For example, you will want to replace preshared key authentication 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 | 10 | Tobias Brunner | 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. For simplicity, we use preshared keys rather than certificates. strongSwan is deployed on both client and gateway. |
12 | 1 | Yaron Sheffer | |
13 | 6 | Yaron Sheffer | h2. General Warnings |
14 | 6 | Yaron Sheffer | |
15 | 6 | Yaron Sheffer | * Debugging IPsec is hard. Debugging networking on public cloud virtual machines is hard. Please *follow these instructions carefully*. |
16 | 12 | Adam Feuer | * Amazon's cloud is constantly changing, mostly for the better. This document has been validated with the current feature set, as of today (May 2012). It may not be valid tomorrow. |
17 | 6 | Yaron Sheffer | |
18 | 1 | Yaron Sheffer | h2. Solution Steps |
19 | 1 | Yaron Sheffer | |
20 | 6 | Yaron Sheffer | # Create a new VPC instance (the minimal instance type in VPC is @m1.small@). This will become your VPN Gateway. We have used a standard Ubuntu Oneiric image, @ami-a562a9cc@. Instead of launching a new, dedicated instance, you can reuse 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. |
21 | 6 | Yaron Sheffer | # Disable source/destination check on the VPN Gateway instance. |
22 | 4 | 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. |
23 | 10 | Tobias Brunner | # 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. |
24 | 8 | Yaron Sheffer | # Enable IP forwarding on the gateway (you need to do _both_ of the following): |
25 | 6 | Yaron Sheffer | #* For the current session, run @echo 1 > /proc/sys/net/ipv4/ip_forward@ |
26 | 6 | Yaron Sheffer | #* To ensure this setting persists after a reboot: edit @/etc/sysctl.conf@ and uncomment the line @net.ipv4.ip_forward=1@. |
27 | 1 | Yaron Sheffer | # Define the gateway's security group(s) to allow incoming TCP/22, UDP/500 and UDP/4500. |
28 | 6 | 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. |
29 | 3 | 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. |
30 | 10 | Tobias Brunner | # The strongSwan Gateway will assign addresses for IPsec clients from the virtual address pool. But it needs a bit of routing help: First, note the instance ID of the VPN gateway. Then locate the routing table associated with the subnet of protected instances (this may or may not be the main routing table), and add a routing rule that routes all traffic destined to the pool's subnet (10.100.0.0/16) through the gateway. |
31 | 1 | Yaron Sheffer | # Allow any incoming traffic from the pool's subnet into all VPC instances. For example, by adding an "all traffic" rule to the @default@ security group of your VPC. |
32 | 12 | Adam Feuer | # For each instance that is in the VPC, disable the instance's Source/Dest. check (from the EC2 Instances page). |
33 | 1 | Yaron Sheffer | # Finally, @sudo ipsec restart@ on the gateway and the client, and you are good to go! |
34 | 1 | Yaron Sheffer | |
35 | 9 | Yaron Sheffer | h2. Bonus |
36 | 9 | Yaron Sheffer | |
37 | 9 | Yaron Sheffer | The only job of Amazon's NAT instance is to run a single iptables rule. You can deploy that rule on your VPN gateway and save the price of a dedicated NAT instance. |
38 | 9 | Yaron Sheffer | |
39 | 9 | Yaron Sheffer | # Add the "masquerade" NAT rule for the entire VPC: @sudo iptables --table nat --append POSTROUTING --source 10.10.0.0/16 -j MASQUERADE@ |
40 | 11 | Yaron Sheffer | # Add the same line into @/etc/rc.local@ so that it will survive a reboot. |
41 | 9 | Yaron Sheffer | # Change the routing table so that the VPN gateway (rather than the NAT instance) becomes the default route of your private subnet. |
42 | 9 | Yaron Sheffer | |
43 | 1 | Yaron Sheffer | h2. Configuration Files |
44 | 5 | Yaron Sheffer | |
45 | 5 | Yaron Sheffer | h3. /etc/ipsec.conf on the Client |
46 | 4 | Yaron Sheffer | |
47 | 4 | Yaron Sheffer | <pre> |
48 | 4 | Yaron Sheffer | # ipsec.conf - strongSwan IPsec configuration file |
49 | 4 | Yaron Sheffer | |
50 | 4 | Yaron Sheffer | # basic configuration |
51 | 4 | Yaron Sheffer | |
52 | 4 | Yaron Sheffer | config setup |
53 | 4 | Yaron Sheffer | # nat_traversal=yes |
54 | 4 | Yaron Sheffer | charonstart=yes |
55 | 4 | Yaron Sheffer | plutostart=no |
56 | 4 | Yaron Sheffer | # charondebug="ike 2, knl 2, cfg 2, mgr 2, chd 2, net 2" |
57 | 4 | Yaron Sheffer | |
58 | 4 | Yaron Sheffer | # Connections into AWS VPC |
59 | 4 | Yaron Sheffer | conn %default |
60 | 4 | Yaron Sheffer | ikelifetime=60m |
61 | 4 | Yaron Sheffer | keylife=20m |
62 | 4 | Yaron Sheffer | rekeymargin=3m |
63 | 1 | Yaron Sheffer | keyingtries=1 |
64 | 4 | Yaron Sheffer | keyexchange=ikev2 |
65 | 1 | Yaron Sheffer | authby=secret |
66 | 4 | Yaron Sheffer | |
67 | 4 | Yaron Sheffer | conn us-east-1-vpc |
68 | 4 | Yaron Sheffer | left=%any |
69 | 4 | Yaron Sheffer | leftsourceip=%config |
70 | 4 | Yaron Sheffer | leftid=<my-email-address> |
71 | 4 | Yaron Sheffer | leftfirewall=yes |
72 | 4 | Yaron Sheffer | right=<gateway's elastic IP> |
73 | 1 | Yaron Sheffer | rightsubnet=10.10.0.0/16 |
74 | 6 | Yaron Sheffer | rightid=@us-east-gw.example.com |
75 | 4 | Yaron Sheffer | auto=start |
76 | 4 | Yaron Sheffer | |
77 | 4 | Yaron Sheffer | # Add connections here. |
78 | 4 | Yaron Sheffer | |
79 | 4 | Yaron Sheffer | # include /var/lib/strongswan/ipsec.conf.inc |
80 | 4 | Yaron Sheffer | </pre> |
81 | 4 | Yaron Sheffer | |
82 | 4 | Yaron Sheffer | h3. /etc/ipsec.secrets on the Client |
83 | 4 | Yaron Sheffer | |
84 | 4 | Yaron Sheffer | <pre> |
85 | 6 | Yaron Sheffer | us-east-gw.example.com : PSK "aa82c7a776e2175114213acc02dda9951a6bc25deb433e6d5d6ef7058626c589" |
86 | 4 | Yaron Sheffer | </pre> |
87 | 4 | Yaron Sheffer | |
88 | 4 | Yaron Sheffer | h3. /etc/ipsec.conf on the Gateway |
89 | 4 | Yaron Sheffer | |
90 | 4 | Yaron Sheffer | <pre> |
91 | 4 | Yaron Sheffer | # ipsec.conf - strongSwan IPsec configuration file |
92 | 4 | Yaron Sheffer | |
93 | 4 | Yaron Sheffer | # basic configuration |
94 | 4 | Yaron Sheffer | |
95 | 4 | Yaron Sheffer | config setup |
96 | 4 | Yaron Sheffer | # nat_traversal=yes |
97 | 4 | Yaron Sheffer | charonstart=yes |
98 | 4 | Yaron Sheffer | plutostart=no |
99 | 7 | Yaron Sheffer | # charondebug="ike 2, knl 2, cfg 2, mgr 3, chd 2, net 2" |
100 | 1 | Yaron Sheffer | |
101 | 4 | Yaron Sheffer | # /etc/ipsec.conf - strongSwan IPsec configuration file |
102 | 4 | Yaron Sheffer | |
103 | 4 | Yaron Sheffer | conn %default |
104 | 4 | Yaron Sheffer | ikelifetime=60m |
105 | 4 | Yaron Sheffer | keylife=20m |
106 | 4 | Yaron Sheffer | rekeymargin=3m |
107 | 4 | Yaron Sheffer | keyingtries=1 |
108 | 4 | Yaron Sheffer | keyexchange=ikev2 |
109 | 4 | Yaron Sheffer | authby=secret |
110 | 4 | Yaron Sheffer | |
111 | 4 | Yaron Sheffer | conn client |
112 | 7 | Yaron Sheffer | # The leftid parameter is not a real DNS name |
113 | 4 | Yaron Sheffer | leftid=us-east-gw.example.com |
114 | 7 | Yaron Sheffer | # The "left" parameter is the gateway's private IP |
115 | 4 | Yaron Sheffer | left=10.10.0.10 |
116 | 4 | Yaron Sheffer | # We are protecting the entire VPC, not just this subnet |
117 | 1 | Yaron Sheffer | leftsubnet=10.10.0.0/16 |
118 | 4 | Yaron Sheffer | leftfirewall=yes |
119 | 4 | Yaron Sheffer | right=%any |
120 | 4 | Yaron Sheffer | # The virtual IP pool is outside the VPC! |
121 | 4 | Yaron Sheffer | rightsourceip=10.100.255.0/28 |
122 | 4 | Yaron Sheffer | auto=add |
123 | 4 | Yaron Sheffer | |
124 | 4 | Yaron Sheffer | # Add connections here. |
125 | 4 | Yaron Sheffer | |
126 | 1 | Yaron Sheffer | # include /var/lib/strongswan/ipsec.conf.inc |
127 | 4 | Yaron Sheffer | </pre> |
128 | 4 | Yaron Sheffer | |
129 | 4 | Yaron Sheffer | h3. /etc/ipsec.secrets on the Gateway |
130 | 6 | Yaron Sheffer | |
131 | 5 | Yaron Sheffer | <pre> |
132 | 4 | Yaron Sheffer | <my-email-address> : PSK "aa82c7a776e2175114213acc02dda9951a6bc25deb433e6d5d6ef7058626c589" |
133 | 1 | Yaron Sheffer | </pre> |