TonicAI / masquerade

A Postgres Proxy to Mask Data in Realtime

Home Page:https://www.tonic.ai

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proxy terminates connection when running within a docker container

binarydev opened this issue · comments

Hey guys,

Awesome tool you've created here, but I've run into some snags when attempting to dockerize it.

I've gotten the tool itself running successfully within a docker container. It can see my dockerized DB, and it's able to connect according to the following output:

$ docker-compose up proxy
Recreating masquerade-proxy_proxy_1 ... done
Attaching to masquerade-proxy_proxy_1
proxy_1  | Starting Proxy...
proxy_1  | Proxy Running:
proxy_1  | 	Proxy Port: 20000
proxy_1  | 	Database Details: postgres@172.18.0.7:5432/test_db_proxy

Docker status while running shows it online and forwarding the proper port:

CONTAINER ID        IMAGE                    COMMAND                  CREATED              STATUS              PORTS                      NAMES
a7bc6476f7fe        masquerade-proxy_proxy   "/bin/sh -c ./start.…"   About a minute ago   Up About a minute   0.0.0.0:20000->20000/tcp   masquerade-proxy_proxy_1

Here's my dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2
RUN apt-get update && apt-get -y install git
WORKDIR /app
RUN git clone https://github.com/TonicAI/masquerade.git .
COPY start.sh ./
RUN chmod +x start.sh
CMD ./start.sh

the start.sh entrypoint:

#!/bin/bash
# Find the IP of the PG container and use it to populate the config.json file
POSTGRES_IP=`getent hosts postgres | awk '{ print $1 }'`
sed 's/POSTGRES_IP/'"$POSTGRES_IP"'/g' config.sample.json > config.json
dotnet run

docker-compose file:

version: "3"
services:
  proxy:
    build: .
    ports:
      - 20000:20000
    external_links:
      - postgres_db:postgres
    volumes:
      - "~/Projects/masquerade-proxy/config.sample.json:/app/config.sample.json"
networks:
  default:
    external:
      name: test_default

and config.sample.json as well:

{
  "proxy_port":20000,
  "db_connection_details": {
      "port": 5432,
      "ip": "POSTGRES_IP",
      "user":"postgres",
      "password":"dev",
      "database":"test_db_proxy"
  },
  "masking_options": {
      "preserve_keys": false,
      "column_masks": [{
          "column":"full_name",
          "table":"users",
          "schema":"public",
          "masking_function":"maskx"
      }],
      "data_type_masks": [
          {
              "data_type": "text",
              "masking_function":"maskcharacters"
          }
      ]
  }
}

However, when I try to connect to the proxy, which has port 20000 exposed to the docker host, I get the following:

$ psql "host=127.0.0.1 port=20000 dbname=test_db_proxy sslmode=disable" postgres
psql: server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.

When I terminate the proxy, I get the expected error of Connection refused because the PG server cannot be found, like you would if you tried to connect on a random port where nothing is listening. This would indicate that my DB clients (tried psql, Postico, and DBeaver) are able to see the proxy, but they cannot properly connect to it.

Any ideas as to what could be causing this would be appreciated!

After further investigation, it's caused by the proxy binding to the localhost IP of 127.0.0.1, so any connection being made by the host to the Docker container is being ignored. Will open up a PR to add a config setting for which source IP the proxy should be listening to, if you guys are willing to consider it!

Hi, I'll look at your PR in the next day or so. Sorry for the delay in this response and thanks for the PR!

Just merged PR. Thanks for your assistance.