Skip to content

feat(sdk): tcp_egress_guard env passthrough (redirect a client's DB endpoint at the proxy) - #103

Open
quanghexa94 wants to merge 1 commit into
mainfrom
lhq/feat/tcp_egress_env_redirect
Open

feat(sdk): tcp_egress_guard env passthrough (redirect a client's DB endpoint at the proxy)#103
quanghexa94 wants to merge 1 commit into
mainfrom
lhq/feat/tcp_egress_env_redirect

Conversation

@quanghexa94

Copy link
Copy Markdown
Collaborator

What & why

Hexgate is used by wrapping an agent from the outside (enforce_policy(agent, ...)), not by editing the code that opens each connection. HTTP egress already fits that: egress_guard sets HTTP_PROXY, and the agent's clients route through the proxy on their own.

tcp_egress_guard didn't fit it. It only handed back proxy.host / proxy.port for you to thread into a DSN by hand, which assumes you own the line that opens the database connection. In real usage that line is buried inside the agent or a tool.

This loosens that. tcp_egress_guard takes an env passthrough: templates with {host} / {port} placeholders that get pointed at the running proxy and restored on exit. An agent that reads its endpoint from the environment (PGHOST/PGPORT, or a DATABASE_URL) is now gated by wrapping it, no change to its connection code.

async with tcp_egress_guard(enforcer, user, target=("db.internal", 5432),
                            env={"PGHOST": "{host}", "PGPORT": "{port}"}):
    run_agent(...)   # its PGHOST/PGPORT-configured client is now gated

Where to look

File What
hexgate/egress/tcp.py tcp_egress_guard gains env; sets each templated var to the proxy address, restores on exit. Docstring now leads with the wrap-from-outside shape.
tests/egress/test_tcp.py env is set to the proxy inside the block and removed after; a pre-existing value is restored, not clobbered.

Honest limit (unchanged)

An agent that hardcodes host and port inside a DSN can't be redirected from the outside, the same class as an HTTP client with trust_env=False. Catching that case needs a transparent-redirect sandbox, which is out of scope for the library.

Full egress + TCP suites pass.

…he proxy

tcp_egress_guard gains an `env` passthrough: each entry is a template with
{host}/{port} placeholders, set to the running proxy's address for the duration
of the block and restored on exit. An agent that reads its database endpoint
from the environment (PGHOST/PGPORT, or a DATABASE_URL) is then gated by
wrapping it, with no change to the code that opens the connection. This is the
same wrap-from-outside shape as egress_guard, so the caller no longer has to
thread the DSN by hand.

    async with tcp_egress_guard(enforcer, user, target=("db.internal", 5432),
                                env={"PGHOST": "{host}", "PGPORT": "{port}"}):
        run_agent(...)

An agent that hardcodes host/port inside a DSN still can't be redirected from
the outside; that case needs a transparent-redirect sandbox.
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant