FTP server on GCE doesn't connect

Viewed 103

I am trying to run an FTP server in a Google Cloud Engine VM but I cannot connect to it even after writing the necessary firewall rules.

I am running the FTP server via python:

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import os
from google.cloud import storage

class MyHandler(FTPHandler):
    def on_file_received(self, file):
        storage_client = storage.Client.from_service_account_json(os.getenv('GCS_WORKER_CREDENTIALS'))
        bucket = storage_client.get_bucket(os.getenv('GCS_BUCKET'))
        blob = bucket.blob(file[5:]) # strip leading /tmp/
        blob.upload_from_filename(file)
        #os.remove(file)

def run_server():
    authorizer = DummyAuthorizer()
    authorizer.add_user(os.getenv('FTP_USER'), os.getenv('FTP_PASSWD'), os.getenv('FTP_PATH'), perm=os.getenv('FTP_PERMISSIONS'))
    handler = MyHandler

    handler.authorizer = authorizer
    handler.masquerade_address = os.getenv('VM_EXTERNAL_IP')
    handler.passive_ports = range(60000, 60999)
    handler.timeout = 300
    handler.banner = 'Welcome to GCP-FTP.'
    handler.max_login_attempts = 3
    handler.use_sendfile = False

    server = FTPServer(("127.0.0.1", os.getenv('FTP_PORT')), handler)
    server.serve_forever()

if __name__=='__main__':
    run_server()

I tried to run the FTP on both port 21 and 6999 but both give me the same error.

When I run the server everything looks fine

root@poc-worker:/home/xxxxxxxxx/etl-cloud# python3 run_ftp_server.py 
[I 2021-06-15 12:55:16] concurrency model: async
[I 2021-06-15 12:55:16] masquerade (NAT) address: 130.211.xxx.xxx
[I 2021-06-15 12:55:16] passive ports: 60000->60998
[I 2021-06-15 12:55:16] >>> starting FTP server on 127.0.0.1:21, pid=1021 <<<

Network rule in GC:

Name: ftp-allow
Type: Incoming
Destination: Apply to all
IP: 0.0.0.0/0
tcp: 60000-60999,6999,21
Action: Allow
Priority: 1000
Network: default

When I test the network rule it also shows me that everything should be fine, I am not sure what I am doing wrong here, any help would be greatly appreciated.

1 Answers

I found the issue, as specified here: Google Cloud Compute Engine refusing connections despite firewall rule

I am binding the server to 127.0.0.1 which is local connections only, binding it to 0.0.0.0 fixed the issue

server = FTPServer(("127.0.0.1", os.getenv('FTP_PORT')), handler)

becomes

server = FTPServer(("0.0.0.0", os.getenv('FTP_PORT')), handler)
Related