← All posts

Developers · 11 min

See what your app really sends: Postgres, MySQL, MariaDB and Redis on the wire

An app log tells you what the app thinks it asked. One command puts mitmproxy between your app and each database in your via.yaml, and open source decoders show the question and the answer for Postgres, MySQL, MariaDB and Redis. With the code to build it yourself, the demo, the tests, a CI step and a lesson.

  • cli
  • mitmproxy
  • postgres
  • mysql
  • mariadb
  • redis
  • debugging
  • developer-tools
The video loads from YouTube after you press play. Until then, nothing is sent to YouTube.

This film on its own page

TL;DR: List your databases as services in via.yaml and run via net apply --inspect. The Viaalink CLI puts mitmproxy between your app and that database, on the same machine. Open source decoders turn the bytes into the query, its parameters, the answer and the error, for Postgres, MySQL, MariaDB and Redis. A plain via net apply takes it out again. The film (3:16) is at the top of this post. Further down: how the decoders work, with their code, how to run the demo and the tests yourself, how to read the wire of a failing CI job, and the film as a lesson.


The question a log cannot answer

Your shop on staging shows an empty page, and the app log is empty too. There are two possible causes. The database had nothing, or the app asked the wrong question.

The app log cannot tell them apart. It records what the app thinks it sent. The database knows what it was really asked, and what it answered. That conversation only exists on the connection between the two, at the moment it happens.

This post shows how to read that conversation live, for four databases, without adding one log line to the app.

What it is built from

Three parts, each usable without the next:

  1. The decoders. Three mitmproxy addons that read the Postgres, MySQL/MariaDB and Redis protocols. They are open source (MIT), in the caddy-api-client repository. All three ship in the npm package @viaalink/caddy-api-client since version 0.14.0.
  2. The Viaalink CLI. It reads via.yaml, starts mitmproxy with those decoders, and routes each database connection through it.
  3. Your own Relay. It brings the inspector’s page to your browser over HTTPS, behind a login, when the server is somewhere else.

We go through them in that order.

Step 1: the decoders on their own

The decoders are plain mitmproxy addons, one Python file each. The npm package brings all three; load them into mitmweb:

bash

Each --mode line opens a port that forwards to one database. Point your app at port 15432 instead of 5432 and its Postgres traffic shows up in mitmweb, decoded.

No npm on that machine? Download the three files from GitHub instead:

bash
AddonReadsWhat you see
pgwirePostgreSQLSQL, bind parameters, columns, rows, errors
mysqlwireMySQL and MariaDBwhich server answered and its version, SQL, prepared statements, errors
rediswireRedis (RESP2 and RESP3)commands with their arguments, replies summarised

A decoder picks its flows by what the protocol looks like, never by port number. One mitmweb can load all three. How that works is further down.

This works, but the plumbing is yours. You pick the ports, change the app’s connection settings, restart it, and put everything back afterwards. On a server you also have to get the mitmweb page to your own browser safely.

Step 2: the same thing from one file

The Viaalink CLI does that plumbing from via.yaml. Each database is a service, and tunnelProtocol: tcp says it speaks raw TCP. Inspect is off unless you ask for it.

yaml

Only web has public: true. The databases do not, so they are never published. Their traffic stays on the machine.

Ask for the inspector on the command line, for the four databases:

bash

--inspect turns the inspector on for this run, and --ids names the services it is for. The file stays as it is. If you want a service inspected every time, put inspect: true on it in the file instead.

The CLI starts mitmproxy with the three decoders and gives each database a local address. For every database it prints one line that ends in queries inspected. via net urls lists the addresses, for example:

text

Your app connects to that Local TCP address instead of the database’s own port. This is the one change on the app’s side, and you make it once: with inspect off, the same address leads straight to the database.

If the server is not the machine you sit at, the inspector’s page comes to you through your Relay: encrypted, and behind a login.

What the wire showed

The demo shop in the film has three bugs. None of them is in the app log.

Postgres: a category that matches nothing. Pick the category Electronics, and the page is empty. This is the query as Postgres received it:

text

The shop asked for electronics in lower case and got zero rows. A search for “keyboard” returns a row whose category is Electronics, with a capital E. The button sends the wrong value.

MySQL: an error the app swallowed. Sort the orders by shipping date, and the shop says “something went wrong”. The server was more precise:

text

The sort option shipped. The column never reached this server.

MariaDB: which server is this? Customers load fine, but the first line of the connection is worth reading:

text

MariaDB speaks the MySQL protocol, so the same decoder reads it and tells you which of the two answered.

Redis: two keys, one letter apart. Add a keyboard to the cart, open the cart, and it is empty. Redis is not SQL, so you read its commands instead. The shop reads the list cart:anna. This is what it wrote a moment before:

text

It writes to cart:Anna and reads from cart:anna.

Logins never show

Look at the CONNECT line above: (credentials not shown). Each decoder leaves login data out of what it shows, and blanks it in the stored flow once it has been forwarded. That covers Postgres password messages, the MySQL and MariaDB auth exchange, and Redis AUTH.

How a decoder knows its protocol

mitmproxy hands every addon every TCP message. An addon that guessed by port would break the moment you run a database somewhere else, so each one looks at the first bytes instead. That is less simple than it sounds: in Redis the client speaks first, in MySQL the server does.

Redis, client first. A client opens with a RESP array (* and a digit) or an inline command made of letters. Anything that looks like GET / HTTP/1.1 is HTTP, so the addon leaves it alone. From rediswire.py:

rediswire.pypython

MySQL and MariaDB, server first. The server’s greeting is a packet with sequence number 0, protocol version 10 and a printable version string. From mysqlwire.py:

mysqlwire.pypython

The version string also tells the two servers apart. MariaDB prefixes 5.5.5- for old clients and names itself, so server_kind() turns 5.5.5-11.4.13-MariaDB-ubu2404 into MariaDB 11.4.13.

Three rules make this safe when several addons run in one mitmweb:

  • Not sure yet, so wait. With too few bytes, both functions return None. The addon keeps the messages and decodes them all once it knows.
  • First come, first served. The addon that recognises a flow writes its name in flow.metadata["wire_protocol"], and the others skip that flow.
  • A decoder bug never breaks the connection. Decoding runs in a try; a failure becomes the line (could not decode: …), and the bytes go through as they were.

Keeping passwords off the screen

A decoder shows AUTH (credentials not shown), but mitmweb also keeps the raw bytes of each message, so those must be blanked too. There is a catch: mitmproxy forwards a message after the tcp_message hook returns. Change the message inside the hook and the database receives the blanked bytes, and the login fails.

So the addon notes which messages carried a password and blanks them on the next message, once they are out. From rediswire.py:

rediswire.pypython

A password can arrive split over several TCP messages. The connection keeps one buffer per direction, and Direction.take() returns which messages the bytes it consumed came from, so every message that held a piece of the password is blanked.

Behind Caddy: swap the route, no restart

The CLI does this for you. If you run Caddy yourself, the same library does it from code. Caddy reaches each database through a caddy-l4 listener. caddy-l4 has no matcher for MySQL or Redis, and a MySQL server speaks first, so there is nothing to match on: those routes have no match. From the demo’s backend:

demo/api/server.tstypescript

enable and disable change the route through Caddy’s admin API, without restarting Caddy. A TCP connection is routed when it opens, so the next new connection takes the new route. The demo keeps its connection pools short-lived for that reason.

Run the demo yourself

Everything in the film runs on your own machine. You need Docker, just, Bun and the Viaalink CLI.

bash

mitmweb asks for a password: demo, unless you set DEMO_MITMWEB_PASSWORD in .env.

The dashboard has a tab per database. Click MITM on a tab to swap that route through mitmweb, then use the shop on the left. Each checkout gets its own ports, so two clones run side by side. What runs is in demo/docker-compose.yml. The mitmweb for MySQL, for example, comes down to this:

yaml

Run the tests

bash

just test-addons feeds recorded conversations through each addon. A test reads like the transcript you see in mitmweb, from test_rediswire.py:

text

Writing a decoder of your own? Use the two helpers in helpers.py. play() sends messages through your addon the way mitmproxy does. bytewise() splits every message into single bytes, the worst split TCP can give you. A decoder that passes with bytewise() handles any real network:

helpers.pypython

When it fails in CI

A database test that only fails in CI is where reading the wire helps most, and the CI runner is exactly where you cannot look. With Viaalink CI/CD you can. Run the tests, and when they fail, put the job’s mitmweb on an address of its own through your Relay, behind a login, together with a terminal on the runner:

yaml

When a test fails, just test-layer4 stops and leaves its stack running, so the mitmweb that saw the failing test (one mitmweb with all three decoders, on port 18082) is still there. Open its address, sign in, and read what the test really sent. When the job ends, the address answers 404.

Use it in class

The film and the demo make a lesson on how an application talks to a database. Students need the demo running on their own laptop, nothing else.

  1. Watch the film to the first bug (Postgres, the empty category). Stop. Ask: did the database have nothing, or did the app ask the wrong question? How would you find out?
  2. Let them find it. In the demo, switch the Postgres tab to MITM and click Electronics. The answer is in the BIND line.
  3. Do the other three alone: the MySQL error the app swallowed, the MariaDB greeting, the Redis cart. Each takes one click and one line of transcript.
  4. Compare with logs. Turn on log_statement in Postgres or MONITOR in Redis. What does the log show, and what does it miss? (The answer: the reply.)
  5. For the advanced group: read looks_like_redis() above, then write a test with bytewise() that sends a Redis command one byte at a time.

Why not just turn on the logs?

You can. Every one of these databases can log queries. This is what it takes:

DatabaseSettingWhat you getWhat is missing
PostgreSQLlog_statement = 'all'statements and their parametersthe answer: no rows, no row count
MySQL and MariaDBgeneral_log = 'ON'every statement, from every clientthe answer, and the error the client got
RedisMONITORevery command, from every clientthe replies; and it slows a busy server down

That is four settings, in four formats, in four places. Each needs administrator rights on the database server, which you often do not have on a managed database. And the log mixes all clients together, while the bug you are after belongs to one connection.

The larger difference is the answer. Three of the four bugs above were only visible in what the database sent back: zero rows, an error, a server greeting. A query log records the question.

Logs are still the right tool for other jobs. Use them for a permanent record, for auditing, and for finding slow queries over days. Reading the wire is for the moment you ask “what did this click just do?“.

Good to know

  • It reads plain connections. When a client turns on TLS, the decoder says so and leaves the connection alone.
  • What you read is real data. Staging is often a copy of production, so turn the inspector off when you are done.

Turn it off when you are done

bash

The file does not ask for inspect, so applying it turns the inspector off. The local addresses now lead straight to the databases, the app keeps running with the same settings, and nothing is recorded.

Get started

The narration in the film uses an AI voice clone of Kelvin’s voice. The screen recording is real: a shop that talks to four running databases.

Contact

Have a topic for us?

Write to us.