- Go 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| datasource | package | from | to | | ---------- | --------------------------------- | ------ | ------- | | go | golang.org/x/vuln/cmd/govulncheck | v1.9.0 | v1.10.0 | |
||
| .github/workflows | ||
| docs | ||
| testdata/fuzz | ||
| .gitignore | ||
| .golangci.yaml | ||
| certificate.go | ||
| certificate_test.go | ||
| cli_test.go | ||
| fuzz_config_test.go | ||
| fuzz_harness_test.go | ||
| fuzz_pki_test.go | ||
| fuzz_proxy_test.go | ||
| fuzz_wire_test.go | ||
| go.mod | ||
| go.sum | ||
| keychain_darwin.go | ||
| keychain_other.go | ||
| keychain_other_test.go | ||
| LICENSE | ||
| main.go | ||
| main_test.go | ||
| proxy_shutdown_test.go | ||
| proxy_streaming_test.go | ||
| proxy_transport_test.go | ||
| README.md | ||
| readme_test.go | ||
| renovate.json | ||
mtls-proxy
A small loopback proxy for tools that cannot present a TLS client certificate.
It listens on a loopback address, adds your client certificate, and forwards
each listener to one upstream host. Terraform providers are the usual case:
many have no client certificate setting and ignore HTTPS_PROXY.
The client certificate can come from the macOS Keychain, so the private key never has to be exported, from PEM files, or from environment variables.
Install
go install github.com/zekihan/mtls-proxy@latest
With -identity, macOS grants Keychain access per binary, so keep the binary at a
fixed path. The Keychain needs cgo, which is the default on macOS. For a static Linux
binary, build with CGO_ENABLED=0.
Usage
# macOS Keychain identity, selected by certificate common name
mtls-proxy -identity clients-laptop -ca-out ~/.local/state/mtls-proxy/ca.pem \
-route 127.0.0.1:18443=app.example.com
# PEM files
mtls-proxy -cert client.pem -key client.key -ca-out ca.pem \
-route 127.0.0.1:18443=app.example.com
# PEM values in environment variables, cleared once read
CERT_PEM="$cert" KEY_PEM="$key" mtls-proxy -cert-env CERT_PEM -key-env KEY_PEM \
-ca-out ca.pem -route 127.0.0.1:18443=app.example.com
Set the variables only on the proxy's command, so other processes do not inherit the
key. Do not do this in a bash -x script, because tracing prints the values.
Clearing them does not erase the environment the proxy started with. Processes running
as your user can still read the key there (/proc/<pid>/environ on Linux, ps -E on
macOS) until the proxy exits. To keep it out of the environment, pass it through process
substitution instead:
mtls-proxy -cert <(your-secret-command cert) -key <(your-secret-command key) \
-ca-out ca.pem -route 127.0.0.1:18443=app.example.com
-route and -forward can be repeated and mixed. -ca-out is required for
any -route. A forward-only proxy does not write a certificate.
The proxy only listens on loopback IPs and refuses anything else. It refuses to start when the client certificate is expired or not yet valid, and it logs a warning when that certificate expires within 30 days.
A client has ten seconds to send request headers. The upstream TLS handshake has the same limit. After sending the full request, the proxy waits up to one minute for upstream response headers.
On interrupt or SIGTERM, the proxy stops accepting connections and gives active HTTP requests up to five seconds to finish.
-route: HTTPS reverse proxy
Point the client at https://127.0.0.1:18443 (or wss:// for WebSockets). The proxy
serves HTTPS using a self-signed certificate generated at startup. The key stays in
memory, and the certificate is written to -ca-out for the client to trust. It changes
on every start. The proxy rewrites Host and SNI to the upstream.
-forward: HTTP_PROXY route for signed requests
Clients that sign the hostname, such as S3 SigV4, cannot use -route because the
Host rewrite invalidates the signature. -forward keeps the hostname.
Start the proxy in one terminal.
mtls-proxy -cert client.pem -key client.key -forward 127.0.0.1:18445=s3.example.com
Run the client in another terminal. Set both spellings of the HTTP proxy variable for client compatibility and clear any inherited proxy bypass for this command.
HTTP_PROXY=http://127.0.0.1:18445 http_proxy=http://127.0.0.1:18445 \
NO_PROXY= no_proxy= your-client --endpoint http://s3.example.com
It accepts only that upstream and refuses CONNECT, so it cannot be used as an open
proxy. This hop is plain HTTP on loopback, and TLS starts at the proxy. A direct
request to the http:// endpoint sends data in plaintext.
Security
- Upstream certificates are verified against the system trust store.
- Any local process can use your client identity through the proxy while it runs. Stop it when you finish.
- Requests a browser makes on behalf of another site (
Sec-Fetch-Site: cross-siteorsame-site) are refused, so a web page cannot use your client identity.