Double proxy (with mTLS encryption)

    Sandbox environment

    Setup your sandbox environment with Docker and Docker Compose, and clone the Envoy repository with Git.

    Used to make HTTP requests.

    openssl

    Generate SSL keys and certificates.

    This sandbox demonstrates a basic “double proxy” configuration, in which a simple Flask app connects to a PostgreSQL database, with two Envoy proxies in between.

    Envoy (front) -> Flask -> Envoy (postgres-front) -> Envoy (postgres-back) -> PostgreSQL

    This type of setup is common in a service mesh where Envoy acts as a “sidecar” between individual services.

    It can also be useful as a way of providing access for application servers to upstream services or databases that may be in a different location or subnet, outside of a service mesh or sidecar-based setup.

    This example encrypts the transmission of data between the two middle proxies and provides mutual authentication using mTLS.

    This can be useful if the proxies are physically separated or transmit data over untrusted networks.

    In order to use the sandbox you will first need to generate the necessary SSL keys and certificates.

    This example walks through creating a certificate authority, and using it to create a domain key and sign certificates for the proxies.

    Change to the examples/double-proxy directory.

    First create a key for the certificate authority:

    Now use the key to generate a certificate authority certificate.

    If you wish, you can interactively alter the fields in the certificate.

    For the purpose of this example, the defaults should be sufficient.

    1. $ openssl req -x509 -new -nodes -key certs/ca.key -sha256 -days 1024 -out certs/ca.crt
    2. You are about to be asked to enter information that will be incorporated
    3. into your certificate request.
    4. What you are about to enter is what is called a Distinguished Name or a DN.
    5. For some fields there will be a default value,
    6. -----
    7. Country Name (2 letter code) [AU]:
    8. State or Province Name (full name) [Some-State]:
    9. Locality Name (eg, city) []:
    10. Organization Name (eg, company) [Internet Widgits Pty Ltd]:
    11. Organizational Unit Name (eg, section) []:
    12. Common Name (e.g. server FQDN or YOUR name) []:
    13. Email Address []:

    Step 2: Create a domain key

    Create a key for the example domain:

    1. $ openssl req -new -sha256 \
    2. -key certs/example.com.key \
    3. -subj "/C=US/ST=CA/O=MyExample, Inc./CN=proxy-postgres-frontend.example.com" \
    4. -out certs/proxy-postgres-frontend.example.com.csr
    5. $ openssl req -new -sha256 \
    6. -out certs/proxy-postgres-backend.example.com.csr

    Step 4: Sign the proxy certificates

    You can now use the certificate authority that you created to sign the certificate requests.

    Note the subjectAltName. This is used for reciprocally matching and validating the certificates.

    At this point you should have the necessary keys and certificates to secure the connection between the proxies.

    The keys and certificates are stored in the certs/ directory.

    Build and start the containers.

    This will load the required keys and certificates into the frontend and backend proxies.

    1. $ pwd
    2. envoy/examples/double-proxy
    3. $ docker-compose build --pull
    4. $ docker-compose up -d
    5. $ docker-compose ps
    6. Name Command State Ports
    7. --------------------------------------------------------------------------------------------------------
    8. double-proxy_app_1 python3 /code/service.py Up
    9. double-proxy_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp
    10. double-proxy_proxy-frontend_1 /docker-entrypoint.sh /usr ... Up 0.0.0.0:10000->10000/tcp
    11. double-proxy_proxy-postgres-frontend_1 /docker-entrypoint.sh /usr ... Up 10000/tcp

    Step 6: Check the flask app can connect to the database

    Checking the response at http://localhost:10000, you should see the output from the Flask app:

    See also

    Outline of key concepts for securing Envoy.

    Examples of various termination patterns with Envoy.