Search code examples
pythonamazon-web-servicessslflaskaws-elb

Verify hostname of the server who invoked the API


I have an AWS ELB connected with multiple EC2s that are running the AWS Flask server. I am not sure if AWS ELB passes the complete request to EC2 or not. I know we can do the restrictions at ELB level but I want to put restrictions on only one endpoint and verify the hostname of the server who invoked the endpoint in Flask. Is it possible?


Solution

  • You could try the following:

    import socket
    from flask import request
    
    
    @app.route("/your_route", methods=["GET"])
    def your_route():
        hostname, aliaslist, ipaddrlist = socket.gethostbyaddr(request.remote_addr)
    

    Note that relying on the remote_addr is unreliable, however as this is unrelated to the topic I will refer to this answer which makes use of ProxyFix:

    For more information on socket.gethostbyaddr() please check out: socket.gethostbyaddr()