| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title>ProFTPD: AWS</title> |
| 5 |
</head> |
| 6 |
|
| 7 |
<body bgcolor=white> |
| 8 |
|
| 9 |
<hr> |
| 10 |
<center><h2><b>AWS and ProFTPD</b></h2></center> |
| 11 |
<hr> |
| 12 |
|
| 13 |
<p> |
| 14 |
So you want to run ProFTPD on an AWS |
| 15 |
<a href="https://aws.amazon.com/ec2/">EC2</a> instance? Due to FTP's nature as |
| 16 |
a multi-connection protocol, it is not as straightforward to use FTP within AWS |
| 17 |
EC2, but it <b>can be done</b>. Read on to find out how. <i>Note</i> that the |
| 18 |
following documentation assumes that you know how to install and configure |
| 19 |
ProFTPD already. |
| 20 |
|
| 21 |
If you are only running individual FTP servers, then the sections on |
| 22 |
<a href="#SGS">AWS security groups</a> and <a href="#Addresses">addresses</a> |
| 23 |
are relevant. If you want to provide a "scalable" <em>pool/cluster</em> of |
| 24 |
FTP servers, then the <a href="#ELBs">AWS Elastic Load Balancing</a> and |
| 25 |
<a href="#Route53">AWS Route53</a> sections will also be of interest. |
| 26 |
|
| 27 |
<p><a name="SGs"> |
| 28 |
<b>Security Groups</b><br> |
| 29 |
Every EC2 instance belongs to one or more AWS <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html">Security Groups</a> |
| 30 |
(often abbreviated as simply "SGs"). As the AWS documentation states, a |
| 31 |
"security group" is a effectively a set of firewall rules controlling network |
| 32 |
access to your EC2 instance. I tend to think of SGs more like NAT rules, |
| 33 |
since the "firewall" is the EC2 network perimeter managed by Amazon, and |
| 34 |
an SG dictates what holes to allow from the outside world into the EC2 internal |
| 35 |
networks. |
| 36 |
|
| 37 |
<p> |
| 38 |
Clients wishing to make a connection to the <code>proftpd</code> running on |
| 39 |
your EC2 instance, be it FTP, FTPS, SFTP, or SCP, will thus need to be allowed |
| 40 |
to connect by one (or more) of your SGs. Assuming your <code>proftpd</code> |
| 41 |
listens on the standard FTP control port (21), you would configure one of your |
| 42 |
SGs to allow access to that port, from any IP address, using the AWS CLI like |
| 43 |
so: |
| 44 |
<pre> |
| 45 |
$ aws ec2 authorize-security-group-ingress \ |
| 46 |
--group-id <em>sg-XXXX</em> \ |
| 47 |
--protocol tcp \ |
| 48 |
--port 21 \ |
| 49 |
--cidr 0.0.0.0/0 |
| 50 |
</pre> |
| 51 |
<b>Note</b> that you do <b>not</b> need to allow access to port 20! Many, |
| 52 |
many sites/howtos recommend opening port 20 in addition to port 21 for FTP |
| 53 |
access, but it <i>simply not needed</i>. For <i>active</i> data transfers |
| 54 |
(<i>i.e.</i> where the FTP server <i>actively</i> connects back to the |
| 55 |
client machine for the data transfer), the <i>source</i> port will be port 20. |
| 56 |
But <i>incoming</i> connections for FTP will <b>never</b> be to port 20. |
| 57 |
|
| 58 |
<p> |
| 59 |
If you are allowing SFTP/SCP connections, <i>e.g.</i> to your |
| 60 |
<code>proftpd</code>, running the <a href="http://www.proftpd.org/docs/contrib/mod_sftp.html"><code>mod_sftp</code></a> module on the standard SSH port (22): |
| 61 |
<pre> |
| 62 |
$ aws ec2 authorize-security-group-ingress \ |
| 63 |
--group-id <em>sg-YYYY</em> \ |
| 64 |
--protocol tcp \ |
| 65 |
--port 22 \ |
| 66 |
--cidr 0.0.0.0/0 |
| 67 |
</pre> |
| 68 |
<b>Note</b>: I recommend using different SGs for your FTP/FTPS rules and your |
| 69 |
SFTP/SCP rules. FTP/FTPS rules are more complex, and it is more clear to |
| 70 |
manage an SG named "FTP", with all of the related FTP rules, and separately |
| 71 |
to have an SG named "SFTP", with the SFTP/SCP related rules. |
| 72 |
|
| 73 |
<p> |
| 74 |
If you are <em>only</em> allowing SFTP/SCP access, that should suffice for |
| 75 |
the security group configuration for your instance. Allowing FTP/FTPS |
| 76 |
connections requires more security group tweaks. |
| 77 |
|
| 78 |
<p> |
| 79 |
FTP uses multiple TCP connections: one for the control connection, and separate |
| 80 |
<em>other</em> connections for data transfers (directory listings and file |
| 81 |
uploads/downloads). The ports used for these data connections are dynamically |
| 82 |
negotiated over the control connection; it is this dynamic nature of the |
| 83 |
data connections which causes complexity with network access rules. This |
| 84 |
site does a great job of describing these issues more in detail: |
| 85 |
<pre> |
| 86 |
<a href="http://slacksite.com/other/ftp.html">http://slacksite.com/other/ftp.html</a> |
| 87 |
</pre> |
| 88 |
Remember how I said that SGs are similar to NAT rules? This similarity is |
| 89 |
one of the reasons why the ProFTPD <a href="http://www.proftpd.org/docs/howto/NAT.html">NAT</a> howto is relevant here as well. |
| 90 |
|
| 91 |
<p> |
| 92 |
We want to configure ProFTPD to use a known range of ports for its passive |
| 93 |
data transfers, and then we want to configure our FTP SG to allow access to |
| 94 |
that known port range. Thus we would use something like this in the |
| 95 |
<code>proftpd.conf</code>: |
| 96 |
<pre> |
| 97 |
PassivePorts 60000 65535 |
| 98 |
</pre> |
| 99 |
And then, to configure the SG to allow those ports: |
| 100 |
<pre> |
| 101 |
$ aws ec2 authorize-security-group-ingress \ |
| 102 |
--group-id <em>sg-XXXX</em> \ |
| 103 |
--protocol tcp \ |
| 104 |
--port 60000-65534 \ |
| 105 |
--cidr 0.0.0.0/0 |
| 106 |
</pre> |
| 107 |
The SFTP/SCP protocols only use a single TCP connection, and thus they do not |
| 108 |
require any other special configuration/access rules. |
| 109 |
|
| 110 |
<p><a name="Addresses"> |
| 111 |
<b>Public <i>vs</i> Private Instance Addresses</b><br> |
| 112 |
Every EC2 instance with have its own local/private IP address and DNS name, |
| 113 |
automatically assigned by AWS. Instances <i>may</i> also be automatically |
| 114 |
assigned <i>public</i> IP addresses/DNS names as well, depending on various |
| 115 |
factors. The AWS docs on <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html">instance addressing</a> discuss those |
| 116 |
factors in greater detail. |
| 117 |
|
| 118 |
<p> |
| 119 |
If your EC2 instance will be supporting FTP/FTPS sessions, then you will need |
| 120 |
to determine whether your instance has a public address. If so, that address |
| 121 |
needs to be configured using the <a href="http://www.proftpd.org/docs/modules/mod_core.html#MasqueradeAddress"><code>MasqueradeAddress</code></a> directive. |
| 122 |
Why? When an FTP client negotiates a |
| 123 |
<a href="http://slacksite.com/other/ftp.html">passive data transfer</a>, |
| 124 |
ProFTPD tells that FTP client an address, and a port, to which to connect to |
| 125 |
transfer the data. For EC2 instances with a public address, that public |
| 126 |
address is what ProFTPD needs to convey to the FTP client, and the |
| 127 |
<code>MasqueradeAddress</code> is the directive that does so. |
| 128 |
|
| 129 |
<p> |
| 130 |
So how can you tell what the public address of your EC2 instance is, if it |
| 131 |
even has one? You can use the EC2 <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html">instance metadata</a>, via |
| 132 |
<code>curl</code>, like so: |
| 133 |
<pre> |
| 134 |
$ curl http://169.254.169.254/latest/meta-data/public-hostname |
| 135 |
</pre> |
| 136 |
<b>If</b> your instance has a public address, the DNS name to use would be |
| 137 |
returned. Otherwise, you might see something like this: |
| 138 |
<pre> |
| 139 |
$ curl http://169.254.169.254/latest/meta-data/public-hostname |
| 140 |
<?xml version="1.0" encoding="iso-8859-1"?> |
| 141 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 142 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 143 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 144 |
<head> |
| 145 |
<title>404 - Not Found</title> |
| 146 |
</head> |
| 147 |
<body> |
| 148 |
<h1>404 - Not Found</h1> |
| 149 |
</body> |
| 150 |
</html> |
| 151 |
</pre> |
| 152 |
which indicates that your EC2 instances does <b>not</b> have a public address. |
| 153 |
And if your instance does not <em>have</em> a public address, then you do |
| 154 |
<em>not</em> need to use the <code>MasqueradeAddress</code> directive. |
| 155 |
|
| 156 |
<p> |
| 157 |
Here's one solution for handling this situation: obtain the public hostname |
| 158 |
for your instance, store it in an environment variable, and then use that |
| 159 |
environment variable in your <code>proftpd.conf</code>: |
| 160 |
<pre> |
| 161 |
$ export EC2_PUBLIC_HOSTNAME=`curl -f -s http://169.254.169.254/latest/meta-data/public-hostname` |
| 162 |
</pre> |
| 163 |
The <code>-f</code> option is necessary, in case the instance does not |
| 164 |
<i>have</i> a public address. The <code>-s</code> option simply makes for |
| 165 |
quieter shell scripts. Then, in your <code>proftpd.conf</code>, you might |
| 166 |
use: |
| 167 |
<pre> |
| 168 |
MasqueradeAddress %{env:EC2_PUBLIC_HOSTNAME} |
| 169 |
</pre> |
| 170 |
If the instance does not have a public address, though, that environment |
| 171 |
variable will be the empty string, and <code>proftpd</code> will fail to start |
| 172 |
up because of that. Better would be to automatically handle the |
| 173 |
"no public address" case, if we can. Assume you have a shell script for |
| 174 |
starting <code>proftpd</code> which does something like this, using our |
| 175 |
<code>EC2_PUBLIC_HOSTNAME</code> environment variable: |
| 176 |
<pre> |
| 177 |
PROFTPD_ARGS="" |
| 178 |
|
| 179 |
# If we have a public hostname, then the string will not be |
| 180 |
# zero length, and we define a property for ProFTPD's use. |
| 181 |
if [ ! -z "$EC2_PUBLIC_HOSTNAME" ]; then |
| 182 |
PROFTPD_ARGS="$PROFTPD_ARGS -DUSE_MASQ_ADDR" |
| 183 |
fi |
| 184 |
</pre> |
| 185 |
Then, in your <code>proftpd.conf</code>, you use <i>both</i> that property |
| 186 |
<i>and</i> the environment variable notation: |
| 187 |
<pre> |
| 188 |
<IfDefined USE_MASQ_ADDR> |
| 189 |
MasqueradeAddress %{env:EC2_PUBLIC_HOSTNAME} |
| 190 |
</IfDefined> |
| 191 |
</pre> |
| 192 |
|
| 193 |
<p> |
| 194 |
Fortunately the EC2 instance addressing does not require any additional |
| 195 |
changes/tweaks to the AWS Security Groups. |
| 196 |
|
| 197 |
<p><a name="ELBs"> |
| 198 |
<b>Elastic Load Balancing</b><br> |
| 199 |
Now that you have ProFTPD up and running on your EC2 instance, and you can |
| 200 |
connect using FTP/FTPS and SFTP/SCP, and browse directories and upload and |
| 201 |
download files, you are probably thinking about how to have more than one |
| 202 |
instance for your FTP service. After all, you want redundancy for your FTP |
| 203 |
servers just like you have for your HTTP servers, right? And for HTTP servers, |
| 204 |
you would use an AWS <a href="http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/elastic-load-balancing.html">Elastic Load Balancer</a> |
| 205 |
(often called an "ELB"). Why not use the same technique for FTP? <i>Can</i> |
| 206 |
you configure an ELB for FTP? |
| 207 |
|
| 208 |
<p> |
| 209 |
Yes, ELBs can be used for FTP. Like SGs, though, it's complicated by FTP's |
| 210 |
use of multiple TCP connections; for SFTP/SCP, ELBs are simpler to configure. |
| 211 |
|
| 212 |
<p> |
| 213 |
The first thing to keep in mind is that ELBs only distribute (<i>i.e.</i> |
| 214 |
"balance") connections in a <a href="http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/how-elb-works.html">round-robin fashion</a> among |
| 215 |
the backend TCP servers; they do <b>not</b> distribute connections based on the |
| 216 |
<em>load</em> of those backend servers. (The balancing algorithm is slightly |
| 217 |
different for HTTP servers, but that does not apply to ProFTPD.) This means |
| 218 |
that <em>any</em> user might connect to <em>any</em> of your ProFTPD instances; |
| 219 |
this, in turn, means that users must be able to login on all instances, |
| 220 |
<b>and</b> that the files for all users should be available on all instances. |
| 221 |
These requirements lead to the requirements for centralized/shared |
| 222 |
authentication data, and for shared filesystems. The centralized/shared |
| 223 |
authentication data can be handled by using <i>e.g.</i> SQL databases, |
| 224 |
LDAP directories, or even synchronized password files. For shared filesystems, |
| 225 |
the popular approaches are: |
| 226 |
<ul> |
| 227 |
<li><a href="https://github.com/s3fs-fuse/s3fs-fuse">s3fs</a> |
| 228 |
<li>NFS |
| 229 |
<li>Samba |
| 230 |
<li>Gluster |
| 231 |
<li><a href="https://aws.amazon.com/efs/">AWS EFS</a> |
| 232 |
</ul> |
| 233 |
There are probably other solutions as well; the key is to have the users' |
| 234 |
files available on any/every instance. |
| 235 |
|
| 236 |
<p> |
| 237 |
The next thing to keep in mind is whether you have an EC2 Classic account, |
| 238 |
or whether you are using AWS <a href="https://aws.amazon.com/vpc/">VPC</a>. |
| 239 |
Chances are that you are using a VPC. ELBs for an EC2 Classic account can |
| 240 |
only be configured to listen on a <a href="http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/elb-listener-config.html">restricted list</a> |
| 241 |
of ports, <i>i.e.</i>: |
| 242 |
<ul> |
| 243 |
<li>25 (SMTP) |
| 244 |
<li>80 (HTTP) |
| 245 |
<li>443 (HTTPS) |
| 246 |
<li>465 (SMTPS) |
| 247 |
<li>587 (SMTP mail submission) |
| 248 |
<li>1024-65535 |
| 249 |
</ul> |
| 250 |
This means that if you have an EC2 Classic account <b>and</b> want to use |
| 251 |
an ELB for your FTP/SFTP servers, you will need to run those servers on |
| 252 |
non-standard ports, in the 1024-65535 range. ELBs within a VPC, on the |
| 253 |
other hand, can listen on <b>any</b> port. |
| 254 |
|
| 255 |
<p> |
| 256 |
Let's assume that you are using a VPC, and thus you configure a TCP listener |
| 257 |
on your ELB for port 21, which uses the instance port 21. And for SFTP/SCP, |
| 258 |
it would be a TCP listener for port 22, using instance port 22. Obviously |
| 259 |
you would not use HTTP or HTTPS listeners, but what about an SSL listener, |
| 260 |
for FTPS? No. An SSL listener performs the SSL/TLS handshake <i>first</i>, |
| 261 |
then forwards the plaintext messages to the backend instance. But FTPS is |
| 262 |
a <a href="https://en.wikipedia.org/wiki/STARTTLS">"STARTTLS"</a> protocol, |
| 263 |
which means the connection is <i>first</i> unencrypted, and then feature |
| 264 |
negotiation happens on that connection, and <i>then</i> the SSL/TLS handshake |
| 265 |
happens. ELBs do not support STARTTLS protocols, thus you cannot use them |
| 266 |
for terminating SSL/TLS sessions for FTP servers. |
| 267 |
|
| 268 |
<p> |
| 269 |
Your ProFTPD configuration might use multiple different ports, for different |
| 270 |
<code><VirtualHost></code>s. Your ELB would need a different TCP |
| 271 |
listener for each of those separate ports. However, now that ProFTPD supports |
| 272 |
the FTP <code>HOST</code> command (which allows for proper name-based virtual |
| 273 |
hosts in FTP, just like HTTP 1.1 has via its <code>Host</code> header), you |
| 274 |
<i>should</i> only need on TCP listener now. |
| 275 |
|
| 276 |
<p> |
| 277 |
An ELB wants to perform health checks on its backend instances, to know that |
| 278 |
that instance is up, running, and available to handle connections. ELBs |
| 279 |
can perform HTTP requests as healthchecks, or make TCP connections. ProFTPD |
| 280 |
is not an HTTP server, so using TCP health checks is necessary. You would |
| 281 |
configure the ELB to make TCP connections to ProFTPD port, <i>e.g.</i> port |
| 282 |
21 for FTP/FTPS, and/or port 22 for SFTP/SCP. |
| 283 |
|
| 284 |
<p> |
| 285 |
What about the range of ports defined via <code>PassivePorts</code>, that |
| 286 |
you had to allow in your SG? Does your ELB need TCP listeners for all of |
| 287 |
those ports, too? No. To understand why, we need to examine in detail just |
| 288 |
how passive data transfers work in FTP. An FTP client connects to your |
| 289 |
FTP server, through the ELB, like this, for its control connection: |
| 290 |
<pre> |
| 291 |
client --- <i>ctrl</i> ---> ELB:21 --- <i>ctrl</i> ---> instance:21 |
| 292 |
</pre> |
| 293 |
The client and server negotiate a passive data transfer; the FTP server |
| 294 |
tells the client, over the control connection, an address and port to which |
| 295 |
to connect. Now, let's assume that ProFTPD gives the address of the ELB, |
| 296 |
and one of the <code>PassivePorts</code>; we'l use port 65000 for this example. |
| 297 |
The FTP client connects to the address/port <em>on the ELB</em>, like this: |
| 298 |
<pre> |
| 299 |
client --- <i>data</i> ---> ELB:65000 --- <i>data</i> ---> instance:65000 |
| 300 |
</pre> |
| 301 |
This would mean that the ELB <i>would</i> need TCP listeners for the |
| 302 |
<code>PassivePorts</code>, <i>and</i> that <code>MasqueradeAddress</code> would |
| 303 |
need to point to the ELB DNS name. So why did I say that the ELB did not |
| 304 |
need those extra TCP listeners? |
| 305 |
|
| 306 |
<p> |
| 307 |
<b>If</b> your ELB will only ever have <b>just one backend instance</b>, then |
| 308 |
the above configuration would work. Your EC2 instance might be in a VPC, |
| 309 |
with no public address, and thus perhaps the <i>only</i> way to make your |
| 310 |
FTP server there reachable <i>is</i> using an ELB. Where forcing passive |
| 311 |
data connections through an ELB starts to fail is when there are <i>multiple |
| 312 |
backend instances</i>. Consider the case where your ELB might have 3 instances: |
| 313 |
<pre> |
| 314 |
+--> instance1:21 |
| 315 |
ELB:21 --|--> instance2:21 |
| 316 |
+--> instance3:21 |
| 317 |
</pre> |
| 318 |
An FTP client connects to the ELB, and the ELB selects instance #2: |
| 319 |
<pre> |
| 320 |
client --- <i>ctrl</i> ---> ELB:21 --- <i>ctrl</i> ---> instance2:21 |
| 321 |
</pre> |
| 322 |
So far, so good. The client requests a passive data transfer; the FTP server |
| 323 |
tells the client to connect to the ELB address, port 65000, <b>but</b> the |
| 324 |
ELB sends that connection to instance <i>#3</i>, <b>not</b> instance #2: |
| 325 |
<pre> |
| 326 |
client --- <i>data</i> ---> ELB:65000 --- <i>data</i> ---> instance3:65000 |
| 327 |
</pre> |
| 328 |
This can happen because the ELB does not understand FTP; it does not know |
| 329 |
that the data connection is related, in any way, to any other connections. To |
| 330 |
the ELB, all TCP connections are independent, and thus any connection will be |
| 331 |
routed, round-robin, to any backend instance. There is no guarantee that the |
| 332 |
data connections, going through the ELB, will connect to the proper backend |
| 333 |
instance. If there is only <b>one backend instance</b>, though, everything |
| 334 |
will work as expected. |
| 335 |
|
| 336 |
<p> |
| 337 |
In order to properly support multiple backend instances (which is one of the |
| 338 |
goals/benefits of using an ELB in the first place) for FTP, then, the trick |
| 339 |
is to <b>not</b> force data connections through the ELB. Instead, the |
| 340 |
<code>MasqueradeAddress</code> directive points to each backend instance's |
| 341 |
respective public hostname. With this configuration, the FTP client connects |
| 342 |
to the ELB for its control connection, like usual: |
| 343 |
<pre> |
| 344 |
client --- <i>ctrl</i> ---> ELB:21 --- <i>ctrl</i> ---> instance2:21 |
| 345 |
</pre> |
| 346 |
And for the data transfer, ProFTPD tells the client the instance public |
| 347 |
hostname, and port 65000: |
| 348 |
<pre> |
| 349 |
client -------------- <i>data</i> -------------> instance2:65000 |
| 350 |
</pre> |
| 351 |
Notice how, with this configuration, the TCP connection for the data transfer |
| 352 |
bypasses the ELB completely. <i>This</i> is why you do not need to configure |
| 353 |
any TCP listeners on the ELB for those <code>PassivePorts</code>, and why |
| 354 |
you do <b>not</b> want <code>MasqueradeAddress</code> using the ELB DNS name; |
| 355 |
you do not want passive data connections going through the ELB. |
| 356 |
|
| 357 |
<p> |
| 358 |
Now you have an ELB with multiple backend FTP servers. Success, right? Maybe. |
| 359 |
There <i>are</i> some caveats. FTP clients might notice that they connect to |
| 360 |
one name (the ELB DNS name), <i>but</i> for data transfers, they are being told |
| 361 |
(by the FTP server) to connect to a <i>different</i> name; some FTP clients |
| 362 |
might warn/complain about this mismatch. ProFTPD would definitely complain |
| 363 |
about this mismatch, for it would see the control connection as originating |
| 364 |
from the ELB, but the data connection originating from a different address, |
| 365 |
and would refuse the data transfer. To allow data transfers to work, then, |
| 366 |
you would need to add the following to your <code>proftpd.conf</code>: |
| 367 |
<pre> |
| 368 |
# Allow "site-to-site" transfers, since that is what FTP traffic with |
| 369 |
# an ELB looks like. |
| 370 |
AllowForeignAddress on |
| 371 |
</pre> |
| 372 |
which has its own <a href="http://www.proftpd.org/docs/howto/FXP.html">security implications</a>. |
| 373 |
|
| 374 |
<p> |
| 375 |
Next, there is the ELB <a href="http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/config-idle-timeout.html">idle timeout</a> setting to |
| 376 |
adjust. The default is 60 seconds. During a data transfer, most FTP clients |
| 377 |
will be handling the data connection, and the control connection is idle. |
| 378 |
Thus if the data transfer lasts longer than 60 seconds, the ELB might terminate |
| 379 |
the idle control connection, and the FTP session is lost. Unfortunately the |
| 380 |
maximum allowed idle timeout for ELBs is 1 hour (3600 seconds); for <i>large</i> |
| 381 |
(or slow) data transfers, even that timeout could be a problem. There are |
| 382 |
ways of keeping the control connection from being idle for too long, using |
| 383 |
<a href="http://www.proftpd.org/docs/howto/KeepAlives.html">keepalives</a>. |
| 384 |
<b>Note</b> that this idle timeout is not really an issue for SFTP/SCP |
| 385 |
sessions, as all data transfers for them use the same single TCP connection. |
| 386 |
|
| 387 |
<p> |
| 388 |
Last, using an ELB only for FTP control connections, and using direct |
| 389 |
connections for the FTP data transfers only works if your backend EC2 instances |
| 390 |
<i>have</i> public hostnames; for instances in a VPC, that may not be true. |
| 391 |
So how can we use an ELB for multiple backend instances that only have private |
| 392 |
addresses? Sadly, the answer is: you can't. For load balancing FTP sessions |
| 393 |
among multiple backend EC2 instances with private addresses, you need an |
| 394 |
FTP-aware proxy, such as ProFTPD with the <a href="https://github.com/Castaglia/proftpd-mod_proxy"><code>mod_proxy</code></a> module. This means running your |
| 395 |
own instance for doing that load balancing, rather than having AWS manage it. |
| 396 |
Of course, if the clients using your ELB for FTP services are <i>also</i> |
| 397 |
within your VPC, then the lack of public hostnames for your EC2 instances |
| 398 |
is not an issue, and using an ELB as described above will work. |
| 399 |
|
| 400 |
<p><a name="Route53"> |
| 401 |
<b>DNS and AWS Route53</b><br> |
| 402 |
Using an ELB for balancing connections across your pool of FTP servers is |
| 403 |
rather complex. Are there alternatives? Yes: "DNS load balancing". |
| 404 |
|
| 405 |
<p> |
| 406 |
Instead of using an AWS ELB for balancing/distributing connections across |
| 407 |
your pool of ProFTPD-running instances, you can use DNS tricks to implement |
| 408 |
the same functionality. Note, however, these DNS tricks still assume that |
| 409 |
your EC2 instances are publicly reachable, <i>i.e.</i> have public hostnames. |
| 410 |
|
| 411 |
<p> |
| 412 |
With DNS load balancing, the client resolves a DNS name to an IP address, |
| 413 |
and connects to that IP address: |
| 414 |
<pre> |
| 415 |
client1 ----------------- <i>ctrl</i> ----------------> instance2:21 |
| 416 |
client1 ----------------- <i>data</i> ----------------> instance2:65000 |
| 417 |
</pre> |
| 418 |
But the DNS server might be configured with <i>several</i> IP addresses for |
| 419 |
the same DNS name; the client then chooses <i>one</i> IP address from the given |
| 420 |
list (usually the first address), and connects to that. Some DNS servers will |
| 421 |
<i>shuffle</i> the list of returned addresses for a name, so that clients will |
| 422 |
choose different addresses, and thus distribute/balance their connections |
| 423 |
across all of the addresses: |
| 424 |
<pre> |
| 425 |
client1 ----------------- <i>ctrl</i> ----------------> instance2:21 |
| 426 |
client1 ----------------- <i>data</i> ----------------> instance2:65000 |
| 427 |
|
| 428 |
client2 ----------------- <i>ctrl</i> ----------------> instance1:21 |
| 429 |
client2 ----------------- <i>data</i> ----------------> instance1:65000 |
| 430 |
|
| 431 |
client3 ----------------- <i>ctrl</i> ----------------> instance3:21 |
| 432 |
client3 ----------------- <i>data</i> ----------------> instance3:65000 |
| 433 |
</pre> |
| 434 |
|
| 435 |
<p> |
| 436 |
Within AWS, the <a href="https://aws.amazon.com/route53/">Route53</a> service |
| 437 |
can be used as the DNS service for your domain names. AWS Route53 calls this round robin of addresses a <a href="http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-policy.html#routing-policy-weighted">weighted</a> routing |
| 438 |
policy, as each address associated with a name can be given a "weight", |
| 439 |
affecting the probability that that address will be returned, by Route53, |
| 440 |
when the DNS name is resolved to an IP address. Other routing policies are |
| 441 |
supported, <i>e.g.</i> <a href="http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-policy.html#routing-policy-latency">latency-based</a> routing |
| 442 |
(so that the instance with the fastest response time is chosen), and |
| 443 |
<a href="http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-policy.html#routing-policy-geo">geolocation-based</a> routing (the instance address |
| 444 |
chosen is based on the location of the resolving client). |
| 445 |
|
| 446 |
<p> |
| 447 |
If you are using AWS Route53, then you will need to configure health checks, |
| 448 |
just as you would for an ELB. Route53 supports TCP health checks, which |
| 449 |
you would point at your FTP/FTPS port (21) or SFTP/SCP port (22) on your |
| 450 |
instances. |
| 451 |
|
| 452 |
<p> |
| 453 |
Since any/all clients could connect to any/all of the EC2 instances associated |
| 454 |
with your DNS name, all of the users would need to be able to login on any |
| 455 |
instance, and have their files/data available. Thus using a shared filesystem |
| 456 |
for the files (such as <a href="https://github.com/s3fs-fuse/s3fs-fuse">s3fs</a>, NFS, Samba, gluster, <i>etc</i>) and a centralized/shared authentication |
| 457 |
mechanism (<i>e.g.</i> SQL database, LDAP directory, <i>etc</i>) would be |
| 458 |
needed. |
| 459 |
|
| 460 |
<p><a name="FutureWork"> |
| 461 |
<b>Future Work</b><br> |
| 462 |
In order to automate much of the above manual steps, work is progressing on |
| 463 |
a <a href="https://github.com/Castaglia/proftpd-mod_aws/"><code>mod_aws</code></a> module for ProFTPD, which will eventually: |
| 464 |
<ul> |
| 465 |
<li>automatically set <code>PassivePorts</code> for FTP/FTPS vhost, if needed |
| 466 |
<li>automatically set <code>MasqueradeAddress</code> if needed |
| 467 |
<li>automatically adjust Security Group rules for FTP/FTPS, SFTP/SCP |
| 468 |
</ul> |
| 469 |
in addition to other interactions with AWS services. |
| 470 |
|
| 471 |
<p><a name="FAQ"> |
| 472 |
<b>Frequently Asked Questions</b><br> |
| 473 |
<p><a name="PerUserLoadBalancing"> |
| 474 |
<font color=red>Question</font>: I need to send particular users only to |
| 475 |
a particular instance/set of instances. How do I configure AWS to do this?<br> |
| 476 |
<font color=blue>Answer</font>: Short answer: you cannot. But it <b>can</b> |
| 477 |
be done! |
| 478 |
|
| 479 |
<p> |
| 480 |
The AWS services like ELBs and Route53 understand TCP connections, and the |
| 481 |
HTTP protocol, but they do not understand FTP. And understanding of the |
| 482 |
protocol is necessary, so that you know how/when to expect the user name, and |
| 483 |
how to redirect/proxy the backend connection. This is why you cannot use |
| 484 |
<i>AWS</i> to do per-user balancing. However, you <i>can</i> use the |
| 485 |
<a href="https://github.com/Castaglia/proftpd-mod_proxy"><code>mod_proxy</code></a> module for ProFTPD, which <i>is</i> protocol-aware, and thus can balance |
| 486 |
FTP/FTPS connections in multiple ways, including per-user. |
| 487 |
|
| 488 |
<p><a name="XForwardedFor"> |
| 489 |
<font color=red>Question</font>: I am using ELBs for my pool of ProFTPD |
| 490 |
servers. I would like my logs to show the IP address of the connecting |
| 491 |
clients, but all I get is the IP address of the ELB. Is there a way to get |
| 492 |
the original IP address, an equivalent to the <code>X-Forwarded-For</code> |
| 493 |
HTTP header?<br> |
| 494 |
<font color=blue>Answer</font>: Yes, there is an equivalent mechanism |
| 495 |
that is supported by ELBs for TCP listeners: the <a href="http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt">PROXY protocol</a>. |
| 496 |
|
| 497 |
<p> |
| 498 |
To enable use of the <code>PROXY</code> protocol by your ELB, see <a href="http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/enable-proxy-protocol.html">here</a>. You will <b>also</b> need to tell ProFTPD to expect |
| 499 |
the <code>PROXY</code> protocol, which means using the <a href="https://github.com/Castaglia/proftpd-mod_proxy_protocol"><code>mod_proxy_protocol</code></a> |
| 500 |
module. |
| 501 |
|
| 502 |
<p> |
| 503 |
The <code>PROXY</code> protocol, and the <code>mod_proxy_protocol</code> module, |
| 504 |
work equally well for FTP/FTPS and SFTP/SCP sessions. |
| 505 |
|
| 506 |
<p><a name="PerInstanceFirewall"> |
| 507 |
<font color=red>Question</font>: Should I run a firewall on my instance as well?<br> |
| 508 |
<font color=blue>Answer</font>: It is considered a good network security |
| 509 |
practice to do so, as it provides security in depth. <b>However</b>, care |
| 510 |
must be taken with those firewall rules; they need to allow the same ports/ |
| 511 |
addresses as your SGs. (Also note that local/instance firewall rules CANNOT |
| 512 |
be applied to the connecting client's IP address when connecting through ELB.) |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
<p> |
| 524 |
<hr> |
| 525 |
<font size=2><b><i> |
| 526 |
© Copyright 2017 The ProFTPD Project<br> |
| 527 |
All Rights Reserved<br> |
| 528 |
</i></b></font> |
| 529 |
<hr> |
| 530 |
|
| 531 |
</body> |
| 532 |
</html> |