diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a6d1777..5edf864 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,15 +10,15 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - uses: ironsh/iron-proxy-action@fa1fd82ec32396d044deba7040a6db576bec927a # v0.1.4 with: egress-rules: egress-rules.yaml - - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - name: Run tests run: go test -v -race -count=1 ./... @@ -29,7 +29,7 @@ jobs: integration-test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 # Install the gcloud SDK (gcloud + bq) before the egress proxy locks # down outbound traffic, so the SDK download isn't blocked. The bq CLI @@ -41,9 +41,9 @@ jobs: egress-rules: egress-rules.yaml disable-docker: false - - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - name: Build iron-proxy run: go build -o iron-proxy ./cmd/iron-proxy @@ -123,15 +123,15 @@ jobs: vet: runs-on: ubuntu-latest steps: - - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - uses: ironsh/iron-proxy-action@fa1fd82ec32396d044deba7040a6db576bec927a # v0.1.4 with: egress-rules: egress-rules.yaml - - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - name: go vet run: go vet ./... @@ -142,17 +142,17 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - uses: ironsh/iron-proxy-action@fa1fd82ec32396d044deba7040a6db576bec927a # v0.1.4 with: egress-rules: egress-rules.yaml - - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - - uses: golangci/golangci-lint-action@82606bf257cbaff209d206a39f5134f0cfbfd2ee # v9.2.1 + - uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a # v9.3.0 with: version: latest install-mode: binary @@ -163,15 +163,15 @@ jobs: tidy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - uses: ironsh/iron-proxy-action@fa1fd82ec32396d044deba7040a6db576bec927a # v0.1.4 with: egress-rules: egress-rules.yaml - - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - name: Check go mod tidy run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ef1bbdb..bc266e9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,7 +15,7 @@ jobs: if: github.event_name == 'pull_request' || startsWith(github.ref, 'refs/tags/v') runs-on: ubuntu-latest steps: - - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 @@ -24,12 +24,12 @@ jobs: egress-rules: egress-rules.yaml disable-docker: false - - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - go-version: "1.26" + go-version-file: go.mod - name: Set up QEMU - uses: docker/setup-qemu-action@06116385d9baf250c9f4dcb4858b16962ea869c3 # v4.1.0 + uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4.2.0 - name: Set up Docker Buildx uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0 @@ -42,7 +42,7 @@ jobs: password: ${{ secrets.DOCKER_HUB_TOKEN }} - name: Run GoReleaser (iron-proxy) - uses: goreleaser/goreleaser-action@5daf1e915a5f0af01ddbcd89a43b8061ff4f1a89 # v7.2.2 + uses: goreleaser/goreleaser-action@f06c13b6b1a9625abc9e6e439d9c05a8f2190e94 # v7.2.3 with: distribution: goreleaser-pro version: latest diff --git a/AGENTS.md b/AGENTS.md index 8c79f2d..442ce9c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -6,8 +6,8 @@ tools. ## Repo at a glance -- Single Go module: `github.com/ironsh/iron-proxy` (Go 1.26+). -- One binary: `cmd/iron-proxy` (the MITM egress proxy). +- Main Go module: `github.com/ironsh/iron-proxy` (version in `go.mod`). +- One binary: `cmd/iron-proxy`, with an opt-in SSH listener in `internal/sshproxy/`. - Core packages live under `internal/`. The transform pipeline lives in `internal/transform/` with one subpackage per transform (`allowlist`, `secrets`, `bodycapture`, `headerallowlist`, `annotate`, diff --git a/README.md b/README.md index a3f4a8a..10bfce5 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,10 @@ Single binary. Single YAML config. - **Upstream IP deny list.** Even when a host is allowed, the proxy refuses to dial it if its resolved address falls inside a denied CIDR — closing the SSRF/DNS-rebinding gap where an allowlisted hostname points at IMDS - or loopback. Cloud metadata endpoints (`169.254.169.254`) and loopback are - denied by default; override via `proxy.upstream_deny_cidrs`. + or loopback. Cloud metadata endpoints (`169.254.169.254`, + `fd00:ec2::254`, and `fd20:ce::254`) and loopback are denied by default; + override via `proxy.upstream_deny_cidrs` or + `IRON_PROXY_UPSTREAM_DENY_CIDRS`. - **Boundary-level secret injection.** Workloads send proxy tokens; iron-proxy replaces them with real secrets before the request leaves. If the sandbox is compromised, the attacker gets tokens that are useless outside the proxy. @@ -40,12 +42,20 @@ Single binary. Single YAML config. - **Streaming-aware.** WebSocket upgrades and Server-Sent Events are proxied natively. No special configuration for agent workloads that hold long-lived connections. -- **CONNECT and SOCKS5 support.** Optional tunnel listener for tools that - natively support proxy configuration via `HTTPS_PROXY` or SOCKS5 settings. +- **Explicit proxy support.** Optional tunnel listener for tools that natively + support proxy configuration via `HTTP_PROXY`, `HTTPS_PROXY`, or SOCKS5 + settings. - **PostgreSQL MITM proxy.** Optional listener that authenticates clients against proxy-managed credentials, injects `SET ROLE` on the upstream session, and rejects client attempts to mutate the role (`SET ROLE`, - `set_config('role', ...)`, DO blocks, etc.) via a SQL AST walk. Pairs with + any `set_config` call, DO blocks, unsafe function definitions, definitions + that bind functions as callbacks such as `CREATE AGGREGATE`, etc.) via a + SQL AST walk. SQL and PL/pgSQL routine bodies are inspected recursively; + dynamic PL/pgSQL and unsupported routine languages are rejected. As defense + in depth, revoke `EXECUTE` on `pg_catalog.set_config` and unneeded + `TEMPORARY` / `CREATE` privileges from the injected role (e.g. from + `PUBLIC`), granting `set_config` back only to the login user that the proxy + uses for session setup. Pairs with PostgreSQL row-level security to give per-tenant data isolation when the application connects as a shared service-account user. **Requires PgBouncer (if used) to run in `pool_mode = session`** — transaction or @@ -278,6 +288,36 @@ through the proxy. Exceptions: `*.internal.corp`). Traffic to these hosts bypasses the proxy entirely. - **`records`:** static A or CNAME records. Highest precedence. +### Response retry handler + +Set `IRON_RESPONSE_RETRY_HANDLER_URL`, +`IRON_RESPONSE_RETRY_COMPLETE_URL`, `IRON_RESPONSE_RETRY_HANDLER_TOKEN`, +`IRON_RESPONSE_RETRY_HANDLER_SANDBOX_ID`, and a comma-separated +`IRON_RESPONSE_RETRY_STATUSES` list to enable externally authorized response +retries. The authorization handler receives the exact upstream scheme, authority, method, +path/query, replayability, response status and headers, trace context, and +sandbox identity. It may return request headers plus an attempt ID for one +exact replay. The completion handler then receives the replay status and +response headers selected by `IRON_RESPONSE_RETRY_COMPLETION_HEADERS`, which +defaults to `Payment-Receipt`. + +Response bodies are never sent to either handler, destinations cannot change, +and connection/framing headers are rejected. Requests over +`proxy.max_request_body_bytes` proceed normally but are marked non-replayable; +if challenged, their original response is returned. Handler failures also +preserve the original response. Handler URLs must use HTTPS unless loopback or +`IRON_RESPONSE_RETRY_HANDLER_ALLOW_HTTP=true` is explicitly configured for a +trusted internal network. Redirects are rejected, and the response retry token +must be configured independently from the control-plane token. WebSocket, +gRPC, and unknown-length streaming requests bypass response retry handling. + +When a trusted handler resolves inside `proxy.upstream_deny_cidrs`, set +`IRON_RESPONSE_RETRY_HANDLER_ALLOW_CIDRS` to a comma-separated list of the +narrow private CIDRs it may use. This exception applies only to the exact +configured authorize and complete endpoints; ordinary proxied traffic remains +subject to the full upstream deny list. Public, loopback, link-local, and cloud +metadata ranges cannot be added through this setting. + ### Allowlist Default-deny. Requests must match at least one domain glob or CIDR to proceed. @@ -407,7 +447,7 @@ The sandbox never holds real credentials. Instead: 1. Configure iron-proxy with the real secret source: environment variables, a file on disk, AWS Secrets Manager, AWS Systems Manager Parameter Store, - 1Password (service account), or 1Password Connect. + Vault KV, 1Password (service account), or 1Password Connect. 2. Give the sandbox a proxy token (e.g., `proxy-openai-abc123`). 3. Configure the `secrets` transform to map proxy tokens to those sources. @@ -450,6 +490,14 @@ Secret sources: `region`, `with_decryption`, `ttl`, and `failure_ttl` are supported. `with_decryption` defaults to `true`, which is the expected setting for `SecureString` parameters. +- **`vault_kv`:** reads the secret map at `path` from the Vault KV engine mounted + at `mount`. `kv_version` may be `1` or `2` and defaults to `2`. The map is + returned as JSON; set `json_key` to select one top-level string field. The + official Vault client reads its address, token, namespace, TLS, and proxy + settings from standard `VAULT_*` environment variables such as `VAULT_ADDR`, + `VAULT_TOKEN`, `VAULT_NAMESPACE`, and `VAULT_CACERT`. Optional `ttl` and + `failure_ttl` are supported. This source handles static KV data only; dynamic + secrets with renewable leases are not supported. - **`1password`:** resolves `secret_ref` (an `op://vault/item/[section/]field` reference) using a 1Password service account token. The token is read from `OP_SERVICE_ACCOUNT_TOKEN`. Optional `ttl` and `failure_ttl` are supported. @@ -467,6 +515,24 @@ extracted. Use it to pull one field out of a JSON secret. cached before retrying; it defaults to 1m and is independent of `ttl`, so a long success TTL does not delay recovery from a transient backend outage. +For example, to select `api_key` from a KV v2 secret stored at +`secret/services/openai`: + +```yaml +source: + type: vault_kv + mount: secret + path: services/openai + json_key: api_key + ttl: 5m + failure_ttl: 15s +``` + +For production machine authentication, run Vault Agent or Vault Proxy with +auto-auth and point the Vault client environment at its local listener. This +keeps AppRole, Kubernetes, or cloud authentication and token renewal outside +iron-proxy. + > **Note:** a bug in `onepassword-sdk-go` breaks builds with `CGO_ENABLED=0`, > so iron-proxy pins a [fork](https://github.com/ironsh/onepassword-sdk-go) > via a `replace` directive in `go.mod` until the fix lands upstream. @@ -576,6 +642,40 @@ Behavior: Pipeline ordering: the MCP interceptor runs after the transform pipeline, so `allowlist` still gates which hosts can be reached and `secrets` has already swapped proxy tokens by the time the interceptor evaluates the body. +### MCP Gateway + +`mcp_gateway` routes client-facing MCP hosts to concrete upstream servers after the MCP policy accepts the request. This lets agents call stable internal hosts while iron-proxy forwards to the real upstream and injects credentials that never enter the sandbox. + +Gateway routes only apply to requests that matched an MCP server. The MCP policy still enforces the tool allowlist first. If policy denies a `tools/call`, the gateway route is not applied and upstream is not reached. + +```yaml +mcp: + servers: + - name: github + rules: + - host: "github.mcp.local" + paths: ["/mcp", "/mcp/*"] + tools: + - name: "search_repositories" + +mcp_gateway: + routes: + - name: github + rules: + - host: "github.mcp.local" + paths: ["/mcp", "/mcp/*"] + upstream: "https://mcp.github.com/v1" + credentials: + - source: + type: env + var: GITHUB_MCP_TOKEN + inject: + header: Authorization + formatter: "Bearer {{ .Value }}" +``` + +Credentials use the same secret sources as the `secrets` transform. They are required by default. Set `require: false` on a credential to skip it when unavailable. Audit logs record the route, upstream URL, and credential injection locations, but never the injected credential values. + Limitations in v1: - Only Streamable HTTP transport is supported. The legacy HTTP+SSE transport (separate `/messages` and `/sse` endpoints) is not. @@ -599,12 +699,13 @@ Bodies are buffered incrementally as transforms read them, and automatically rewound between pipeline stages. If a transform doesn't read the body, no buffering occurs and the body streams through untouched. -### Tunnel listener (CONNECT/SOCKS5) +### Tunnel listener (HTTP/CONNECT/SOCKS5) -The tunnel listener accepts HTTP CONNECT and SOCKS5 connections on a -dedicated port. This is useful for tools that natively support proxy -configuration via `HTTPS_PROXY`/`ALL_PROXY` environment variables or -SOCKS5 settings, rather than relying on DNS-based routing. +The tunnel listener accepts absolute-form HTTP proxy requests, HTTP CONNECT, +and SOCKS5 connections on a dedicated port. This is useful for tools that +natively support proxy configuration via `HTTP_PROXY`/`HTTPS_PROXY`/`ALL_PROXY` +environment variables or SOCKS5 settings, rather than relying on DNS-based +routing. To enable it, set `tunnel_listen` under `proxy`: @@ -615,10 +716,11 @@ proxy: When omitted, the tunnel listener is disabled. -Both protocols go through the same transform pipeline as regular HTTP/HTTPS -requests. The proxy evaluates a synthetic CONNECT request against your -allowlist and secrets transforms, so tunnel connections are subject to the -same default-deny policy. +All protocols go through the same transform pipeline as regular HTTP/HTTPS +requests. Absolute-form HTTP requests are handled by the normal HTTP proxy +path. For CONNECT and SOCKS5, the proxy evaluates a synthetic CONNECT request +against your allowlist and secrets transforms, so tunnel connections are +subject to the same default-deny policy. After the CONNECT or SOCKS5 handshake, the proxy peeks at the first byte to detect the inner protocol: @@ -637,6 +739,13 @@ curl -x http://172.20.0.2:8080 \ https://httpbin.org/get ``` +**Plain HTTP proxy example:** + +```bash +curl -x http://172.20.0.2:8080 \ + http://httpbin.org/get +``` + **SOCKS5 example:** ```bash @@ -649,6 +758,7 @@ You can also set the standard environment variables so all tools route through the tunnel automatically: ```bash +export HTTP_PROXY=http://172.20.0.2:8080 export HTTPS_PROXY=http://172.20.0.2:8080 export ALL_PROXY=socks5h://172.20.0.2:8080 ``` @@ -959,9 +1069,9 @@ curl -X POST http://127.0.0.1:9092/v1/reload \ ## iron.sh -Need Vault/KMS secret backends, a Kubernetes operator, or centralized policy -management? [iron.sh](https://iron.sh) builds on iron-proxy with enterprise -features for teams running this at scale. +Need dynamic Vault secret engines, KMS backends, a Kubernetes operator, or +centralized policy management? [iron.sh](https://iron.sh) builds on iron-proxy +with enterprise features for teams running this at scale. ## Verify release signatures @@ -993,3 +1103,10 @@ To verify a specific binary against the signed checksum list (example: `iron-pro ```bash shasum -a 256 iron-proxy-linux-amd64 | grep -F "$(grep -F 'iron-proxy-linux-amd64' checksums.txt | awk '{print $1}')" ``` + +### SSH credential proxy (local development) + +The opt-in [SSH listener](internal/sshproxy/README.md) maps a sandbox's disposable identity +onto a separately held upstream key. It supports non-interactive +exec, SFTP, and wstunnel v11 WSS endpoints, with pinned SSH host identities and +renewable authorization leases. HTTP and SSH share one executable and process. diff --git a/charts/iron-proxy/README.md b/charts/iron-proxy/README.md index eb14a0a..dace566 100644 --- a/charts/iron-proxy/README.md +++ b/charts/iron-proxy/README.md @@ -33,7 +33,7 @@ listeners, configured under `.Values.listeners`: | HTTP | `http` | 80/TCP | yes | | HTTPS | `https` | 443/TCP | yes | | Metrics / health (`GET /healthz`) | `metrics` | 9090/TCP | yes | -| Tunnel (CONNECT/SOCKS5) | `tunnel` | 8080/TCP | no | +| Tunnel (HTTP/CONNECT/SOCKS5) | `tunnel` | 8080/TCP | no | | Postgres MITM | `postgres` | 5432/TCP | no | | Management API | `management` | 9092/TCP | no | @@ -57,7 +57,7 @@ listeners: https: port: 8443 # Service port, container port, and proxy bind all become 8443 tunnel: - enabled: true # turn on the optional CONNECT/SOCKS5 tunnel + enabled: true # turn on the optional HTTP/CONNECT/SOCKS5 explicit proxy dns: enabled: false # disable the DNS server entirely (IRON_DNS_ENABLED=false) ``` @@ -123,6 +123,7 @@ managed: existingTokenSecret: iron-proxy-token # Secret with a "token" key # tokenSecretKey: token # controlPlaneURL: https://api.iron.sh # override if self-hosting + # pollInterval: 10s # -> IRON_CONTROL_PLANE_POLL_INTERVAL proxyIP: "203.0.113.10" # REQUIRED -> IRON_DNS_PROXY_IP # tlsMode: "sni-only" # -> IRON_TLS_MODE (set when ca.mode=none) # logLevel: "info" # -> IRON_LOG_LEVEL @@ -192,6 +193,7 @@ credentials with: | `managed.existingTokenSecret` | `""` | Existing Secret holding the token. | | `managed.tokenSecretKey` | `token` | Key within that Secret. | | `managed.controlPlaneURL` | `""` | Override `IRON_CONTROL_PLANE_URL`. | +| `managed.pollInterval` | `""` | Override `IRON_CONTROL_PLANE_POLL_INTERVAL`; binary default is `10s`. | | `managed.proxyIP` | `""` | `IRON_DNS_PROXY_IP`. Required in managed mode. | | `managed.tlsMode` | `""` | `IRON_TLS_MODE` (`mitm`/`sni-only`). | | `managed.logLevel` | `""` | `IRON_LOG_LEVEL`. | diff --git a/charts/iron-proxy/templates/_helpers.tpl b/charts/iron-proxy/templates/_helpers.tpl index a4fa920..1c8e32a 100644 --- a/charts/iron-proxy/templates/_helpers.tpl +++ b/charts/iron-proxy/templates/_helpers.tpl @@ -144,6 +144,10 @@ from the CA mount. The control-plane token is emitted separately by the Deployme - name: IRON_CONTROL_PLANE_URL value: {{ . | quote }} {{- end }} +{{- with .Values.managed.pollInterval }} +- name: IRON_CONTROL_PLANE_POLL_INTERVAL + value: {{ . | quote }} +{{- end }} {{- with .Values.managed.proxyIP }} - name: IRON_DNS_PROXY_IP value: {{ . | quote }} diff --git a/charts/iron-proxy/values.yaml b/charts/iron-proxy/values.yaml index ff6c0e8..0e48134 100644 --- a/charts/iron-proxy/values.yaml +++ b/charts/iron-proxy/values.yaml @@ -81,6 +81,9 @@ managed: # -- Override the control plane URL (sets IRON_CONTROL_PLANE_URL). Empty uses # the binary default (https://api.iron.sh). controlPlaneURL: "" + # -- Control-plane poll interval (sets IRON_CONTROL_PLANE_POLL_INTERVAL). + # Empty uses the binary default (10s). + pollInterval: "" # -- REQUIRED in managed mode: IP returned for intercepted DNS queries # (IRON_DNS_PROXY_IP). Same meaning as config.dns.proxy_ip in standalone mode. proxyIP: "" diff --git a/cmd/iron-proxy/main.go b/cmd/iron-proxy/main.go index c8c756f..3b8c894 100644 --- a/cmd/iron-proxy/main.go +++ b/cmd/iron-proxy/main.go @@ -1,4 +1,4 @@ -// Command iron-proxy runs the MITM HTTP/HTTPS proxy with built-in DNS server. +// Command iron-proxy runs HTTP/HTTPS, DNS, and optional Postgres and SSH listeners. package main import ( @@ -11,6 +11,8 @@ import ( "net" "os" "os/signal" + "strconv" + "strings" "syscall" "time" @@ -21,10 +23,13 @@ import ( "github.com/ironsh/iron-proxy/internal/dnsguard" "github.com/ironsh/iron-proxy/internal/management" "github.com/ironsh/iron-proxy/internal/mcp" + "github.com/ironsh/iron-proxy/internal/mcpgateway" "github.com/ironsh/iron-proxy/internal/metrics" iotel "github.com/ironsh/iron-proxy/internal/otel" "github.com/ironsh/iron-proxy/internal/postgres" "github.com/ironsh/iron-proxy/internal/proxy" + "github.com/ironsh/iron-proxy/internal/responseretry" + "github.com/ironsh/iron-proxy/internal/sshproxy" "github.com/ironsh/iron-proxy/internal/transform" "github.com/ironsh/iron-proxy/internal/version" @@ -34,6 +39,7 @@ import ( _ "github.com/ironsh/iron-proxy/internal/transform/awsauth" _ "github.com/ironsh/iron-proxy/internal/transform/bodycapture" _ "github.com/ironsh/iron-proxy/internal/transform/gcpauth" + _ "github.com/ironsh/iron-proxy/internal/transform/gcpidtoken" _ "github.com/ironsh/iron-proxy/internal/transform/grpc" _ "github.com/ironsh/iron-proxy/internal/transform/headerallowlist" _ "github.com/ironsh/iron-proxy/internal/transform/hmacsign" @@ -55,6 +61,7 @@ func main() { } configPath := flag.String("config", "", "path to iron-proxy YAML config file") + requireSSH := flag.Bool("ssh", false, "require SSH configuration at startup") tokenFlag := flag.String("token", "", "control plane bearer token (managed mode)") flag.Parse() @@ -103,7 +110,7 @@ func main() { // errc collects fatal errors from all background goroutines: servers // and (in managed mode) the config poller. - errc := make(chan error, 5) + errc := make(chan error, 6) // The postgres listener comes from the local YAML postgres: block // (self-managed proxies, inline DSNs) and, in managed mode, has additional @@ -119,12 +126,13 @@ func main() { var holder *transform.PipelineHolder var mcpHolder *mcp.PolicyHolder + var gatewayHolder *mcpgateway.Holder var otelCfg iotel.ExportConfig var poller *controlplane.Poller if managed { var ingestToken string - holder, mcpHolder, ingestToken, pgListener, poller = initManaged(ctx, cfg, bodyLimits, errc, proxyToken, pgManager, localPgListener, logger) + holder, mcpHolder, gatewayHolder, ingestToken, pgListener, poller = initManaged(ctx, cfg, bodyLimits, errc, proxyToken, pgManager, localPgListener, logger) if ingestToken != "" { otelCfg.DefaultEndpoint = "https://ingest.iron.sh/v1/logs" otelCfg.DefaultHeaders = map[string]string{ @@ -132,7 +140,7 @@ func main() { } } } else { - holder, mcpHolder = initStandalone(cfg, bodyLimits, logger) + holder, mcpHolder, gatewayHolder = initStandalone(cfg, bodyLimits, logger) } // 5. Validate the fully-assembled config. @@ -208,6 +216,36 @@ func main() { ) } + sshServer, sshListener, err := sshproxy.LoadFromNode(ctx, cfg.SSH, guard, auditFunc, logger) + if err == nil && *requireSSH && sshServer == nil { + err = fmt.Errorf("SSH configuration required") + } + if err != nil { + logger.Error("initializing SSH listener", slog.String("error", err.Error())) + os.Exit(1) + } + if sshListener != nil { + defer func() { + if err := sshListener.Close(); err != nil && !errors.Is(err, net.ErrClosed) { + logger.Error("closing SSH listener", slog.String("error", err.Error())) + } + }() + } + + responseRetryHandler, responseRetryStatuses, err := responseRetryHandlerFromEnv( + os.Getenv, + time.Duration(cfg.Proxy.UpstreamResponseHeaderTimeout), + resolver, + guard, + ) + if err != nil { + logger.Error("initializing response retry handler", slog.String("error", err.Error())) + os.Exit(1) + } + if responseRetryHandler != nil { + logger.Info("response retry handler enabled", slog.Any("statuses", responseRetryStatuses)) + } + // Initialize proxy. p := proxy.New(proxy.Options{ HTTPAddr: cfg.Proxy.HTTPListen, @@ -219,6 +257,8 @@ func main() { Resolver: resolver, Guard: guard, MCPPolicy: mcpHolder, + MCPGateway: gatewayHolder, + ResponseRetryHandler: responseRetryHandler, Logger: logger, UpstreamResponseHeaderTimeout: time.Duration(cfg.Proxy.UpstreamResponseHeaderTimeout), UpstreamProxy: cfg.Proxy.UpstreamProxy.ProxyFunc(), @@ -245,11 +285,22 @@ func main() { mgmtOpts.Status = func() any { return poller.Status() } mgmtOpts.SyncNow = poller.Poke } else { - mgmtOpts.Reload = newReloadFunc(*configPath, holder, mcpHolder, pgManager, bodyLimits, logger) + mgmtOpts.Reload = newReloadFunc(*configPath, holder, mcpHolder, gatewayHolder, pgManager, bodyLimits, logger) } mgmtServer = management.New(mgmtOpts) } + var sshDone chan struct{} + if sshServer != nil { + sshDone = make(chan struct{}) + go func() { + defer close(sshDone) + if err := sshServer.Serve(ctx, sshListener); err != nil { + errc <- fmt.Errorf("ssh: %w", err) + } + }() + } + // Start services. if dnsServer != nil { go func() { errc <- fmt.Errorf("dns: %w", dnsServer.ListenAndServe()) }() @@ -321,6 +372,14 @@ func main() { logger.Error("postgres server shutdown error", slog.String("error", err.Error())) } + if sshDone != nil { + select { + case <-sshDone: + case <-shutdownCtx.Done(): + logger.Error("SSH shutdown timed out") + } + } + logger.Info("iron-proxy stopped") } @@ -332,7 +391,7 @@ func main() { // // Initial MCP policy preference: control-plane-supplied mcp block first, then // fall back to cfg.MCP from the YAML if the sync did not include one. -func initManaged(ctx context.Context, cfg *config.Config, bodyLimits transform.BodyLimits, errc chan<- error, proxyToken string, pgManager *postgres.Manager, localPgListener *postgres.Listener, logger *slog.Logger) (*transform.PipelineHolder, *mcp.PolicyHolder, string, *postgres.Listener, *controlplane.Poller) { +func initManaged(ctx context.Context, cfg *config.Config, bodyLimits transform.BodyLimits, errc chan<- error, proxyToken string, pgManager *postgres.Manager, localPgListener *postgres.Listener, logger *slog.Logger) (*transform.PipelineHolder, *mcp.PolicyHolder, *mcpgateway.Holder, string, *postgres.Listener, *controlplane.Poller) { cpURL := envOrDefault("IRON_CONTROL_PLANE_URL", "https://api.iron.sh") logger.Info("starting in managed mode", slog.String("control_plane_url", cpURL)) @@ -389,6 +448,15 @@ func initManaged(ctx context.Context, cfg *config.Config, bodyLimits transform.B logger.Error("building initial mcp policy", slog.String("error", err.Error())) os.Exit(1) } + gateway, err := mcpgateway.LoadFromNode(cfg.MCPGateway) + if err != nil { + logger.Error("building initial mcp gateway", slog.String("error", err.Error())) + os.Exit(1) + } + if gateway != nil { + logger.Info("mcp gateway enabled") + } + gatewayHolder := mcpgateway.NewHolder(gateway) // Compute the initial postgres listener: the local YAML listener with any // control-plane-synced routes layered on when its env vars are present. If @@ -400,7 +468,7 @@ func initManaged(ctx context.Context, cfg *config.Config, bodyLimits transform.B } // Start config poller. - poller := controlplane.NewPoller(client, configHash, func(u controlplane.SyncUpdate) error { + poller := controlplane.NewPollerWithInterval(client, configHash, func(u controlplane.SyncUpdate) error { if u.Rules != nil || u.Secrets != nil || u.Transforms != nil { if err := applyPipelineSync(holder, bodyLimits, logger, u.Rules, u.Secrets, u.Transforms); err != nil { return err @@ -417,7 +485,7 @@ func initManaged(ctx context.Context, cfg *config.Config, bodyLimits transform.B } } return nil - }, logger) + }, logger, time.Duration(cfg.ControlPlane.PollInterval)) // Seed the poller's status from the startup sync so /v1/status (and the // fail-closed gate) reflect it before the polling loop's first pass. @@ -427,7 +495,7 @@ func initManaged(ctx context.Context, cfg *config.Config, bodyLimits transform.B errc <- poller.Run(ctx) }() - return holder, mcpHolder, ingestToken, pgListener, poller + return holder, mcpHolder, gatewayHolder, ingestToken, pgListener, poller } // managedReady gates the proxy on the first applied control-plane config. @@ -461,7 +529,7 @@ func buildInitialMCPHolder(cfg *config.Config, initialMCP json.RawMessage, logge } // initStandalone builds the pipeline and MCP policy from the YAML config. -func initStandalone(cfg *config.Config, bodyLimits transform.BodyLimits, logger *slog.Logger) (*transform.PipelineHolder, *mcp.PolicyHolder) { +func initStandalone(cfg *config.Config, bodyLimits transform.BodyLimits, logger *slog.Logger) (*transform.PipelineHolder, *mcp.PolicyHolder, *mcpgateway.Holder) { pipeline, err := buildPipeline(cfg.Transforms, bodyLimits, logger) if err != nil { logger.Error("building transform pipeline", slog.String("error", err.Error())) @@ -475,7 +543,15 @@ func initStandalone(cfg *config.Config, bodyLimits transform.BodyLimits, logger if mcpPolicy != nil { logger.Info("mcp policy enabled") } - return transform.NewPipelineHolder(pipeline), mcp.NewPolicyHolder(mcpPolicy) + gateway, err := mcpgateway.LoadFromNode(cfg.MCPGateway) + if err != nil { + logger.Error("loading mcp gateway", slog.String("error", err.Error())) + os.Exit(1) + } + if gateway != nil { + logger.Info("mcp gateway enabled") + } + return transform.NewPipelineHolder(pipeline), mcp.NewPolicyHolder(mcpPolicy), mcpgateway.NewHolder(gateway) } // applyPipelineSync builds a new pipeline from a sync payload and atomically @@ -633,7 +709,7 @@ func applyPostgresSync(ctx context.Context, mgr *postgres.Manager, local *postgr // wrapped in *management.ValidationError so the management server returns // 422 and the existing state is left untouched. Validation runs for every // component before any state is mutated. -func newReloadFunc(configPath string, holder *transform.PipelineHolder, mcpHolder *mcp.PolicyHolder, pgManager *postgres.Manager, bodyLimits transform.BodyLimits, logger *slog.Logger) management.ReloadFunc { +func newReloadFunc(configPath string, holder *transform.PipelineHolder, mcpHolder *mcp.PolicyHolder, gatewayHolder *mcpgateway.Holder, pgManager *postgres.Manager, bodyLimits transform.BodyLimits, logger *slog.Logger) management.ReloadFunc { return func(ctx context.Context) error { newCfg, err := config.LoadConfig(configPath) if err != nil { @@ -650,6 +726,10 @@ func newReloadFunc(configPath string, holder *transform.PipelineHolder, mcpHolde if err != nil { return &management.ValidationError{Err: err} } + newGateway, err := mcpgateway.LoadFromNode(newCfg.MCPGateway) + if err != nil { + return &management.ValidationError{Err: err} + } newPgListener, err := postgres.LoadFromNode(newCfg.Postgres, logger) if err != nil { return &management.ValidationError{Err: err} @@ -657,6 +737,7 @@ func newReloadFunc(configPath string, holder *transform.PipelineHolder, mcpHolde newPipeline.SetAuditFunc(holder.Load().AuditFunc()) holder.Store(newPipeline) mcpHolder.Store(newPolicy) + gatewayHolder.Store(newGateway) pgManager.Reload(ctx, newPgListener) logger.Info("pipeline reloaded via management API", slog.String("transforms", newPipeline.Names()), @@ -689,3 +770,65 @@ func envOrDefault(key, def string) string { } return def } + +func parseResponseRetryStatuses(value string) ([]int, error) { + var statuses []int + for _, part := range splitCommaSeparated(value) { + status, err := strconv.Atoi(part) + if err != nil { + return nil, fmt.Errorf("invalid status %q", part) + } + statuses = append(statuses, status) + } + return statuses, nil +} + +func responseRetryHandlerFromEnv(getenv func(string) string, timeout time.Duration, resolver *net.Resolver, guard *dnsguard.Guard) (*responseretry.Handler, []int, error) { + handlerURL := getenv("IRON_RESPONSE_RETRY_HANDLER_URL") + if handlerURL == "" { + return nil, nil, nil + } + allowHTTPValue := getenv("IRON_RESPONSE_RETRY_HANDLER_ALLOW_HTTP") + if allowHTTPValue == "" { + allowHTTPValue = "false" + } + allowHTTP, err := strconv.ParseBool(allowHTTPValue) + if err != nil { + return nil, nil, fmt.Errorf("parse HTTP allowance: %w", err) + } + statuses, err := parseResponseRetryStatuses(getenv("IRON_RESPONSE_RETRY_STATUSES")) + if err != nil { + return nil, nil, fmt.Errorf("parse statuses: %w", err) + } + completionHeaders := getenv("IRON_RESPONSE_RETRY_COMPLETION_HEADERS") + if completionHeaders == "" { + completionHeaders = "Payment-Receipt" + } + handler, err := responseretry.New(responseretry.Options{ + AuthorizeEndpoint: handlerURL, + CompleteEndpoint: getenv("IRON_RESPONSE_RETRY_COMPLETE_URL"), + Token: getenv("IRON_RESPONSE_RETRY_HANDLER_TOKEN"), + SandboxID: getenv("IRON_RESPONSE_RETRY_HANDLER_SANDBOX_ID"), + Statuses: statuses, + AllowHTTP: allowHTTP, + CompletionHeaders: splitCommaSeparated(completionHeaders), + AllowCIDRs: splitCommaSeparated(getenv("IRON_RESPONSE_RETRY_HANDLER_ALLOW_CIDRS")), + Resolver: resolver, + Guard: guard, + ClientTimeout: timeout, + }) + if err != nil { + return nil, nil, err + } + return handler, statuses, nil +} + +func splitCommaSeparated(value string) []string { + var values []string + for _, part := range strings.Split(value, ",") { + if part = strings.TrimSpace(part); part != "" { + values = append(values, part) + } + } + return values +} diff --git a/cmd/iron-proxy/main_test.go b/cmd/iron-proxy/main_test.go index 2795e6b..563a34d 100644 --- a/cmd/iron-proxy/main_test.go +++ b/cmd/iron-proxy/main_test.go @@ -7,6 +7,7 @@ import ( "io" "log/slog" "testing" + "time" "github.com/stretchr/testify/require" @@ -32,6 +33,109 @@ func discardLogger() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) } +func TestParseResponseRetryStatuses(t *testing.T) { + cases := []struct { + name string + value string + want []int + wantErr string + }{ + {name: "multiple", value: "402, 409", want: []int{402, 409}}, + {name: "empty entries", value: " ,402,, ", want: []int{402}}, + {name: "empty", value: "", want: nil}, + {name: "invalid", value: "402,nope", wantErr: `invalid status "nope"`}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, err := parseResponseRetryStatuses(tc.value) + if tc.wantErr != "" { + require.ErrorContains(t, err, tc.wantErr) + return + } + require.NoError(t, err) + require.Equal(t, tc.want, got) + }) + } +} + +func TestSplitCommaSeparated(t *testing.T) { + require.Equal(t, []string{"Payment-Receipt", "X-Receipt"}, splitCommaSeparated(" Payment-Receipt, ,X-Receipt ")) +} + +func TestResponseRetryHandlerFromEnv(t *testing.T) { + base := map[string]string{ + "IRON_RESPONSE_RETRY_HANDLER_URL": "http://127.0.0.1/authorize", + "IRON_RESPONSE_RETRY_COMPLETE_URL": "http://127.0.0.1/complete", + "IRON_RESPONSE_RETRY_HANDLER_TOKEN": "handler-token", + "IRON_RESPONSE_RETRY_HANDLER_SANDBOX_ID": "sandbox-1", + "IRON_RESPONSE_RETRY_STATUSES": "402,409", + "IRON_RESPONSE_RETRY_COMPLETION_HEADERS": "X-Receipt", + "IRON_RESPONSE_RETRY_HANDLER_ALLOW_CIDRS": "10.43.0.0/16", + } + + handler, statuses, err := responseRetryHandlerFromEnv(mapEnv(base), time.Second, nil, nil) + + require.NoError(t, err) + require.NotNil(t, handler) + require.Equal(t, []int{402, 409}, statuses) +} + +func TestResponseRetryHandlerFromEnvRejectsUnsafeAllowCIDRs(t *testing.T) { + cases := []struct { + name string + value string + want string + }{ + {name: "missing prefix", value: "10.43.0.1", want: "must use CIDR notation"}, + {name: "public range", value: "203.0.113.0/24", want: "private address range"}, + {name: "link local", value: "169.254.0.0/16", want: "private address range"}, + {name: "AWS IPv6 metadata", value: "fd00::/8", want: "metadata address"}, + {name: "GCP IPv6 metadata", value: "fd20:ce::/64", want: "metadata address"}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + env := map[string]string{ + "IRON_RESPONSE_RETRY_HANDLER_URL": "http://127.0.0.1/authorize", + "IRON_RESPONSE_RETRY_COMPLETE_URL": "http://127.0.0.1/complete", + "IRON_RESPONSE_RETRY_HANDLER_TOKEN": "handler-token", + "IRON_RESPONSE_RETRY_HANDLER_SANDBOX_ID": "sandbox-1", + "IRON_RESPONSE_RETRY_STATUSES": "402", + "IRON_RESPONSE_RETRY_HANDLER_ALLOW_CIDRS": tc.value, + } + + handler, _, err := responseRetryHandlerFromEnv(mapEnv(env), time.Second, nil, nil) + + require.Nil(t, handler) + require.ErrorContains(t, err, tc.want) + }) + } +} + +func TestResponseRetryHandlerFromEnvDisabled(t *testing.T) { + handler, statuses, err := responseRetryHandlerFromEnv(mapEnv(nil), time.Second, nil, nil) + + require.NoError(t, err) + require.Nil(t, handler) + require.Nil(t, statuses) +} + +func TestResponseRetryHandlerFromEnvRequiresDedicatedToken(t *testing.T) { + env := map[string]string{ + "IRON_RESPONSE_RETRY_HANDLER_URL": "http://127.0.0.1/authorize", + "IRON_RESPONSE_RETRY_COMPLETE_URL": "http://127.0.0.1/complete", + "IRON_RESPONSE_RETRY_HANDLER_SANDBOX_ID": "sandbox-1", + "IRON_RESPONSE_RETRY_STATUSES": "402", + "IRON_PROXY_TOKEN": "control-plane-token", + } + + handler, _, err := responseRetryHandlerFromEnv(mapEnv(env), time.Second, nil, nil) + + require.Nil(t, handler) + require.ErrorContains(t, err, "handler token is required") +} + // localListener builds a single-upstream local listener (with its own shared // client credential) for conflict/passthrough tests. func localListener(t *testing.T, database string) *postgres.Listener { @@ -194,6 +298,27 @@ func TestApplyPipelineSync_ValidConfig_Swaps(t *testing.T) { require.Contains(t, logBuf.String(), "pipeline reloaded") } +func TestApplyPipelineSync_GCPIDTokenTransformFromControlPlane(t *testing.T) { + original := transform.NewPipeline(nil, transform.BodyLimits{}, slog.New(slog.NewTextHandler(io.Discard, nil))) + holder := transform.NewPipelineHolder(original) + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + + transforms := json.RawMessage(`[ + { + "name": "gcp_id_token", + "config": { + "keyfile_path": "/does/not/read/until/request.json", + "audience": "https://private-service-abc123-uc.a.run.app", + "rules": [{"host":"private-service-abc123-uc.a.run.app"}] + } + } + ]`) + require.NoError(t, applyPipelineSync(holder, transform.BodyLimits{}, logger, nil, nil, transforms)) + + require.NotSame(t, original, holder.Load(), "pipeline should have been swapped") + require.Equal(t, "gcp_id_token", holder.Load().Names()) +} + func TestApplyPipelineSync_InvalidJSON_KeepsExistingPipeline(t *testing.T) { original := transform.NewPipeline(nil, transform.BodyLimits{}, slog.New(slog.NewTextHandler(io.Discard, nil))) holder := transform.NewPipelineHolder(original) diff --git a/examples/docker-compose/Dockerfile b/examples/docker-compose/Dockerfile index 85a2a95..ab67610 100644 --- a/examples/docker-compose/Dockerfile +++ b/examples/docker-compose/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.26-alpine AS build +FROM golang:1.27.3-alpine AS build RUN apk add --no-cache git WORKDIR /src COPY go.mod go.sum ./ diff --git a/examples/nftables/Dockerfile b/examples/nftables/Dockerfile index a91caa9..7a4aabd 100644 --- a/examples/nftables/Dockerfile +++ b/examples/nftables/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.26-alpine AS build +FROM golang:1.27.3-alpine AS build RUN apk add --no-cache git WORKDIR /src COPY go.mod go.sum ./ diff --git a/go.mod b/go.mod index 1d0be61..f585d9e 100644 --- a/go.mod +++ b/go.mod @@ -1,23 +1,26 @@ module github.com/ironsh/iron-proxy -go 1.26.1 +go 1.27.3 require ( github.com/1Password/connect-sdk-go v1.5.3 github.com/1password/onepassword-sdk-go v0.4.0 - github.com/aws/aws-sdk-go-v2 v1.41.11 - github.com/aws/aws-sdk-go-v2/config v1.32.22 - github.com/aws/aws-sdk-go-v2/service/s3 v1.103.1 - github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.42.1 - github.com/aws/aws-sdk-go-v2/service/ssm v1.69.1 + github.com/aws/aws-sdk-go-v2 v1.45.1 + github.com/aws/aws-sdk-go-v2/config v1.33.1 + github.com/aws/aws-sdk-go-v2/service/s3 v1.109.1 + github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.46.1 + github.com/aws/aws-sdk-go-v2/service/ssm v1.75.1 github.com/cenkalti/backoff/v5 v5.0.3 + github.com/gobwas/ws v1.4.0 github.com/hashicorp/golang-lru/v2 v2.0.7 + github.com/hashicorp/vault/api v1.23.0 github.com/jackc/pgx/v5 v5.10.0 github.com/miekg/dns v1.1.72 github.com/pganalyze/pg_query_go/v6 v6.2.2 + github.com/pkg/sftp v1.13.12 github.com/stretchr/testify v1.11.1 - github.com/testcontainers/testcontainers-go v0.42.0 - github.com/testcontainers/testcontainers-go/modules/postgres v0.42.0 + github.com/testcontainers/testcontainers-go v0.44.0 + github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 github.com/wasilibs/go-pgquery v0.0.0-20260428021157-dca720e45577 github.com/zeebo/blake3 v0.2.4 go.opentelemetry.io/otel v1.44.0 @@ -26,9 +29,10 @@ require ( go.opentelemetry.io/otel/log v0.20.0 go.opentelemetry.io/otel/sdk v1.44.0 go.opentelemetry.io/otel/sdk/log v0.20.0 - golang.org/x/net v0.55.0 + golang.org/x/crypto v0.55.0 + golang.org/x/net v0.58.0 golang.org/x/oauth2 v0.36.0 - google.golang.org/grpc v1.81.1 + google.golang.org/grpc v1.83.0 google.golang.org/protobuf v1.36.11 gopkg.in/yaml.v3 v3.0.1 ) @@ -38,21 +42,21 @@ require ( dario.cat/mergo v1.0.2 // indirect github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect github.com/Microsoft/go-winio v0.6.2 // indirect - github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.12 // indirect - github.com/aws/aws-sdk-go-v2/credentials v1.19.21 // indirect - github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.27 // indirect - github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.27 // indirect - github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.27 // indirect - github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.28 // indirect - github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.11 // indirect - github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.20 // indirect - github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.27 // indirect - github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.27 // indirect - github.com/aws/aws-sdk-go-v2/service/signin v1.1.3 // indirect - github.com/aws/aws-sdk-go-v2/service/sso v1.31.1 // indirect - github.com/aws/aws-sdk-go-v2/service/ssooidc v1.36.4 // indirect - github.com/aws/aws-sdk-go-v2/service/sts v1.43.1 // indirect - github.com/aws/smithy-go v1.27.0 // indirect + github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.20 // indirect + github.com/aws/aws-sdk-go-v2/credentials v1.20.1 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.19.1 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.1 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.1 // indirect + github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.1 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.19 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.1 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.1 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.1 // indirect + github.com/aws/aws-sdk-go-v2/service/signin v1.7.1 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.35.1 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.40.1 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.47.1 // indirect + github.com/aws/smithy-go v1.28.1 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/containerd/errdefs v1.0.0 // indirect @@ -62,31 +66,46 @@ require ( github.com/cpuguy83/dockercfg v0.3.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/distribution/reference v0.6.0 // indirect - github.com/docker/go-connections v0.6.0 // indirect + github.com/docker/go-connections v0.7.0 // indirect github.com/docker/go-units v0.5.0 // indirect github.com/dylibso/observe-sdk/go v0.0.0-20240828172851-9145d8ad07e1 // indirect - github.com/ebitengine/purego v0.10.0 // indirect + github.com/ebitengine/purego v0.10.1 // indirect github.com/extism/go-sdk v1.7.1 // indirect - github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/felixge/httpsnoop v1.1.0 // indirect + github.com/go-jose/go-jose/v4 v4.1.4 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect - github.com/go-ole/go-ole v1.2.6 // indirect + github.com/go-ole/go-ole v1.3.0 // indirect github.com/gobwas/glob v0.2.3 // indirect + github.com/gobwas/httphead v0.1.0 // indirect + github.com/gobwas/pool v0.2.1 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0 // indirect + github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/hashicorp/go-cleanhttp v0.5.2 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect + github.com/hashicorp/go-retryablehttp v0.7.8 // indirect + github.com/hashicorp/go-rootcerts v1.0.2 // indirect + github.com/hashicorp/go-secure-stdlib/parseutil v0.2.0 // indirect + github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect + github.com/hashicorp/go-sockaddr v1.0.7 // indirect + github.com/hashicorp/hcl v1.0.1-vault-7 // indirect github.com/ianlancetaylor/demangle v0.0.0-20251118225945-96ee0021ea0f // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect - github.com/klauspost/compress v1.18.5 // indirect + github.com/klauspost/compress v1.18.6 // indirect github.com/klauspost/cpuid/v2 v2.0.12 // indirect - github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect + github.com/kr/fs v0.1.0 // indirect + github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e // indirect github.com/magiconair/properties v1.8.10 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/moby/docker-image-spec v1.3.1 // indirect github.com/moby/go-archive v0.2.0 // indirect - github.com/moby/moby/api v1.54.1 // indirect - github.com/moby/moby/client v0.4.0 // indirect + github.com/moby/moby/api v1.55.0 // indirect + github.com/moby/moby/client v0.5.0 // indirect github.com/moby/patternmatcher v0.6.1 // indirect - github.com/moby/sys/sequential v0.6.0 // indirect + github.com/moby/sys/sequential v0.7.0 // indirect github.com/moby/sys/user v0.4.0 // indirect github.com/moby/sys/userns v0.1.0 // indirect github.com/moby/term v0.5.2 // indirect @@ -96,28 +115,29 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect - github.com/shirou/gopsutil/v4 v4.26.3 // indirect + github.com/ryanuber/go-glob v1.0.0 // indirect + github.com/shirou/gopsutil/v4 v4.26.6 // indirect github.com/sirupsen/logrus v1.9.4 // indirect github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834 // indirect github.com/tetratelabs/wazero v1.11.0 // indirect - github.com/tklauser/go-sysconf v0.3.16 // indirect - github.com/tklauser/numcpus v0.11.0 // indirect + github.com/tklauser/go-sysconf v0.4.0 // indirect + github.com/tklauser/numcpus v0.12.0 // indirect github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect github.com/uber/jaeger-lib v2.4.1+incompatible // indirect github.com/wasilibs/wazero-helpers v0.0.0-20250123031827-cd30c44769bb // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect go.opentelemetry.io/auto/sdk v1.2.1 // indirect - go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0 // indirect go.opentelemetry.io/otel/metric v1.44.0 // indirect go.opentelemetry.io/otel/trace v1.44.0 // indirect go.opentelemetry.io/proto/otlp v1.10.0 // indirect go.uber.org/atomic v1.9.0 // indirect - golang.org/x/crypto v0.51.0 // indirect - golang.org/x/mod v0.35.0 // indirect - golang.org/x/sync v0.20.0 // indirect - golang.org/x/sys v0.45.0 // indirect - golang.org/x/text v0.37.0 // indirect - golang.org/x/tools v0.44.0 // indirect + golang.org/x/mod v0.38.0 // indirect + golang.org/x/sync v0.22.0 // indirect + golang.org/x/sys v0.47.0 // indirect + golang.org/x/text v0.41.0 // indirect + golang.org/x/time v0.12.0 // indirect + golang.org/x/tools v0.48.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa // indirect ) diff --git a/go.sum b/go.sum index 91a94e6..373d77e 100644 --- a/go.sum +++ b/go.sum @@ -12,46 +12,46 @@ github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo= github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= -github.com/aws/aws-sdk-go-v2 v1.41.11 h1:9PRf7jyTMEUM6fuNRAJa2mO/skJfrF50rENJwf2LXqw= -github.com/aws/aws-sdk-go-v2 v1.41.11/go.mod h1:iiUX27gOXRuYaoeUVXhUpPwjJHzISfPAjjcuhUbLSVs= -github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.12 h1:oRtsqWgxbpeXrOlxOoQStx2M9WNbIkPq4C4Xn1or6bc= -github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.12/go.mod h1:Zg0Oe9qT+9wcezlm1a64wGJp2qZdRElVxo/seJf7jYU= -github.com/aws/aws-sdk-go-v2/config v1.32.22 h1:Vfvp7+fYKsVCADcWOEllqEV47aIBXhNchvyDFu1B5fY= -github.com/aws/aws-sdk-go-v2/config v1.32.22/go.mod h1:0+H+0nPKbvWltf5vSIGkApv+hGbaQ4FfwTjGIYQREcw= -github.com/aws/aws-sdk-go-v2/credentials v1.19.21 h1:0+HscFXtNa4+3buV4IlG6v5lnOdzi5TrpicFGjKHgh4= -github.com/aws/aws-sdk-go-v2/credentials v1.19.21/go.mod h1:UE8+9t5zudFwu5k5ShC1PKArVEdOkQQdCXIHQAVNUcU= -github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.27 h1:BEfN1sjtiKEdikRDxYkjZNE4tyvw/YbGWCbl3xDZgRw= -github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.27/go.mod h1:ISGSFNbOHRS+JV/17yStzRTPBUHHqF92kCpRLLyH3Nk= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.27 h1:8sPbKi1/KRHwl5oR3qN9mUXestCeHuaRutxylnr/eVY= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.27/go.mod h1:QV9IVIopJ1dpQUno0f9VYDUwOEjj8u0iEJ4JiZVre3Y= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.27 h1:9d8AoASQY9UwrOSmiJ7uSM0MGUPFhnenwSvpaFfat2c= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.27/go.mod h1:x0rldpsnUQaQIs4Rh+Vwm9Z/0vI6BxadGtsgJfZFb8s= -github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.28 h1:eaS9vwQ5ym4Y9S6+G/K3d3lgZhxs9Sldcn/YS7cmdKY= -github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.28/go.mod h1:oTdbDr+BMs7gAYrNpD0LDTyqQfv6yOYgTDv46+xbwFY= -github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.11 h1:rFSsqDfCMPAmG70JOsYqFZCHXkyatoGa1K4YEt/BggQ= -github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.11/go.mod h1:XG68qW+YLLFH0vnSDCou43Cgj5TeAG83O5NRSJgt04Y= -github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.20 h1:yt2fjgev3Hqm33zPw0ZWtki3sZ0SLcr+PkuvXDAAf/8= -github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.20/go.mod h1:wnPjCjPJ6x5GBhrER8f0QakaQ2LokfhCVYxmAZBpPjY= -github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.27 h1:2/pUo42hhVmQcM21ttZoBOLHQymyUH8qEnZGTIuGBT8= -github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.27/go.mod h1:p7hwgbwompjCRNTdB3ytlldddNt1rDBgVVMqWEVG1II= -github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.27 h1:JEXSW4wztrl1MoL5EMvJMO7lc/TRZloztrJKNl96SW8= -github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.27/go.mod h1:8eL+YgEqy6IYqjwW6PG0Ubn59a2xtCzbz7Pi18JBu04= -github.com/aws/aws-sdk-go-v2/service/s3 v1.103.1 h1:WkX5IXwcxgO/WPTvhEqoSW2L1GB1OyIxk0vuzzdTftc= -github.com/aws/aws-sdk-go-v2/service/s3 v1.103.1/go.mod h1:9Q9ZHyiTItraw8BXpO48pk398Mou0YCSI+xvFcaGgxU= -github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.42.1 h1:AH4hyaVe/2+p7zx/GkmjQLXOfVTul6EFMTRy8QVl71Q= -github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.42.1/go.mod h1:BXtBSoY9TdCQwc46RZtaxNb+kKfcCbHOvk4h8drQUqM= -github.com/aws/aws-sdk-go-v2/service/signin v1.1.3 h1:t6U7sowMfOjTeZXtDOtgEJXsoJyX4MDag+sfWGwUM9M= -github.com/aws/aws-sdk-go-v2/service/signin v1.1.3/go.mod h1:WhO1EH3phjFWValQDsExaxncgEWJsHeoTvuyQAj3jwU= -github.com/aws/aws-sdk-go-v2/service/ssm v1.69.1 h1:SpBGMGefOnKHkhwfygyPbfYbxdTUfN83bK+SqVJyWs4= -github.com/aws/aws-sdk-go-v2/service/ssm v1.69.1/go.mod h1:XqVRRari2Ds7jlKh/lQyS6nGpnxH6S/JUtUti+F6ezQ= -github.com/aws/aws-sdk-go-v2/service/sso v1.31.1 h1:TUV8oytPCX1PfVgZn0N8/sPZx7T0YasaMCBHox1erlw= -github.com/aws/aws-sdk-go-v2/service/sso v1.31.1/go.mod h1:tEL1hqCrkgwrDVL04HuLxz1SLUXdh+4kKhWv1pXKeiY= -github.com/aws/aws-sdk-go-v2/service/ssooidc v1.36.4 h1:p9+Fizo2sUB6wI5Yb3K5lmykQAGs5JrKLBV/me6613Y= -github.com/aws/aws-sdk-go-v2/service/ssooidc v1.36.4/go.mod h1:0x10Wy0dVS4Gn552xhHY5th2QdYpfJf44EsfyYGV194= -github.com/aws/aws-sdk-go-v2/service/sts v1.43.1 h1:r/vUkpLilfCA3sxbRnkHbJejaoVHEdj4FEhv+Zva4DU= -github.com/aws/aws-sdk-go-v2/service/sts v1.43.1/go.mod h1:t01JURC8Fe5M+7R1K0vzIZ2NT04HqvZR+FjlHrHDT2A= -github.com/aws/smithy-go v1.27.0 h1:ZoFioDKJxkSIW2otF9T0aPtNlUwhdVCcuZh/rzH9Hus= -github.com/aws/smithy-go v1.27.0/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc= +github.com/aws/aws-sdk-go-v2 v1.45.1 h1:iIoG3NaLhV6UZpPXyPXlDj2I9oS8tV/nMcMnITCC6Ks= +github.com/aws/aws-sdk-go-v2 v1.45.1/go.mod h1:bttEH6JqnUL8LepvDVfdrds/fZ5bCIxzpe3abyUrhDU= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.20 h1:GPRlPwz40I2B2VrBEASOA3Bi77NyeqejNLkifosX0rs= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.20/go.mod h1:g7PNzKcsOKWb4fkSRBA7BZVAS6Y8IcxzN+nRohhQ1Q8= +github.com/aws/aws-sdk-go-v2/config v1.33.1 h1:bq9jze1hQ5YTCLoVxNnbp0T7rglrlOE7N9YsHqjGkEw= +github.com/aws/aws-sdk-go-v2/config v1.33.1/go.mod h1:2A3HQwG4zaL5Tm80rc6RZj8LmWWv4WYT5v8raSz/L7A= +github.com/aws/aws-sdk-go-v2/credentials v1.20.1 h1:Z8GRNEx0u9sDkZOq4PUnN8mjGwbUQGRzMSXpvt3d8xQ= +github.com/aws/aws-sdk-go-v2/credentials v1.20.1/go.mod h1:uBIK00kFo95dnemqfFMTWx0X8YRqsh6ecIoCjjOkZqM= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.19.1 h1:YIEBqcqRnpi4Pfv0YHImtgi6czGCwKHANC7SwmUAVD0= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.19.1/go.mod h1:imEf0oufgAo8KAkCHhrOdqGEC0YWx1PPBQH82shSxGw= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.1 h1:pc138gM1CW+XPc60rEwUlwwuwWFQK16CI1T7v1F9Oec= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.1/go.mod h1:1+koxpPIbfBdfzP6vojm5/zTpTQ/micYwlxIiNB3TxI= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.1 h1:K0JsbZQj+1h208Ro1zHeA4l7bMp0NvRffHQ91q8Ol1s= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.1/go.mod h1:W3/vL6EtCIatICGy9ab29QhMuae+cOKPWcMxv02CO+Q= +github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.1 h1:yhw5KD1phVyP9vijxOUzDfEtJx+bt+L63k+VfuiYFAA= +github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.1/go.mod h1:ZW2e0d7DYlRxlS9hEiMXE47gTdX5KRN4byUiNbUpG+Q= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.19 h1:bAdDl/HkGCcGPoe25ToSHEw23VIxt6CT5fLcg111BKg= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.19/go.mod h1:KaUzbLxv4CeSxh6ZCl9B4m7CuFenS8kUEaDs+f/DQr4= +github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.1 h1:s67hBfG5t9rn1NCvDuB4E3QIep3UFhHPtaIqFDjV3N8= +github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.1/go.mod h1:FpvjBMXtSNMLPmDJsWwcY5cRnqJlpS2y1R6n4pvzs4k= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.1 h1:RmmWQPREQdk9U+PfqeHW3MqZaBaNK7TpV9W3RY+b+7g= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.1/go.mod h1:0A3W4F+68ZnNk5XcNL/e9HFMwnP8RlEicFfy6eOEDyw= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.1 h1:ZMbtPZZQRca+3+XYQne9PBvRiYpHZlNJJOZfE9WNfT0= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.1/go.mod h1:YAGWQdCYlVCoqrzvfv3RLxO6zKwti7gsAULOGWPLYv4= +github.com/aws/aws-sdk-go-v2/service/s3 v1.109.1 h1:kVpzaDBzOdRtOftmiSpTdQbWVqRg0kONLXijktiwXnk= +github.com/aws/aws-sdk-go-v2/service/s3 v1.109.1/go.mod h1:CUr46sCpGAg/rHaclRyhJX0LJAmH73uWSJPPSaMUrSk= +github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.46.1 h1:WnWkXm0wdenvdzG5QhMvTMWlG7Kh4MNTPcjQn4iuD4M= +github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.46.1/go.mod h1:2rdJeO95tT+EGmjF4jWmIzNKRwIMpr7Dnf7W6mIB9EQ= +github.com/aws/aws-sdk-go-v2/service/signin v1.7.1 h1:mdMtSVKdQ3+mzBh+l0ogrFYZVQUCg6pJZOirA2ARsYE= +github.com/aws/aws-sdk-go-v2/service/signin v1.7.1/go.mod h1:9IqUlsJDbUPcg6cgx3WEzXdjrbWzLDQrak0aaSqlTcI= +github.com/aws/aws-sdk-go-v2/service/ssm v1.75.1 h1:u1DO++wfJop+QfEA0D2Ks2XmV0LiKyxpzwPhBUZmWe4= +github.com/aws/aws-sdk-go-v2/service/ssm v1.75.1/go.mod h1:l+OkBEbZ5r0XEHQ+R7c27Zw3LO89fau+FBxsDoAW1TU= +github.com/aws/aws-sdk-go-v2/service/sso v1.35.1 h1:B6WFn91tobD6gG4724ONHaqrpKsoETGnv98LHe/yIGM= +github.com/aws/aws-sdk-go-v2/service/sso v1.35.1/go.mod h1:tWuiVBUtPBr8/rgRiYS8Uf85sHcAN+G7XS3D3CEoUh8= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.40.1 h1:6yeYCWFvgbI2TI3K6jr9LtBNhXgJ7g4xqD+DEiaDDmM= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.40.1/go.mod h1:naFe83jSMuYkH+QjQPX8n1MLhBkeCFM5Lsnh5m5wz3c= +github.com/aws/aws-sdk-go-v2/service/sts v1.47.1 h1:Sv2xPnRHlThSUtVujYuUBPI/Il8si6UPHXL8DMiB/F0= +github.com/aws/aws-sdk-go-v2/service/sts v1.47.1/go.mod h1:mKo/CzaCz8qytGW70NG4vIIGAx1HXTlb5lHNkC5k3lk= +github.com/aws/smithy-go v1.28.1 h1:R/nXH00c8qcfCzQVELtRw+eLQWtzv+VAIEFJ1/xxXlQ= +github.com/aws/smithy-go v1.28.1/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM= @@ -75,38 +75,73 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= -github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94= -github.com/docker/go-connections v0.6.0/go.mod h1:AahvXYshr6JgfUJGdDCs2b5EZG/vmaMAntpSFH5BFKE= +github.com/docker/go-connections v0.7.0 h1:6SsRfJddP22WMrCkj19x9WKjEDTB+ahsdiGYf0mN39c= +github.com/docker/go-connections v0.7.0/go.mod h1:no1qkHdjq7kLMGUXYAduOhYPSJxxvgWBh7ogVvptn3Q= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/dylibso/observe-sdk/go v0.0.0-20240828172851-9145d8ad07e1 h1:idfl8M8rPW93NehFw5H1qqH8yG158t5POr+LX9avbJY= github.com/dylibso/observe-sdk/go v0.0.0-20240828172851-9145d8ad07e1/go.mod h1:C8DzXehI4zAbrdlbtOByKX6pfivJTBiV9Jjqv56Yd9Q= -github.com/ebitengine/purego v0.10.0 h1:QIw4xfpWT6GWTzaW5XEKy3HXoqrJGx1ijYHzTF0/ISU= -github.com/ebitengine/purego v0.10.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= +github.com/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY= +github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= github.com/extism/go-sdk v1.7.1 h1:lWJos6uY+tRFdlIHR+SJjwFDApY7OypS/2nMhiVQ9Sw= github.com/extism/go-sdk v1.7.1/go.mod h1:IT+Xdg5AZM9hVtpFUA+uZCJMge/hbvshl8bwzLtFyKA= -github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= -github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= +github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU= +github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc= +github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE= +github.com/go-jose/go-jose/v4 v4.1.4 h1:moDMcTHmvE6Groj34emNPLs/qtYXRVcd6S7NHbHz3kA= +github.com/go-jose/go-jose/v4 v4.1.4/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= -github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= +github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= +github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U= +github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= +github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= +github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= +github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= +github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs= +github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= -github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0 h1:5VipnvEpbqr2gA2VbM+nYVbkIF28c5ZQfqCBQ5g2xfk= github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0/go.mod h1:Hyl3n6Twe1hvtd9XUXDec4pTvgMSEixRuQKPTMH2bNs= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= +github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= +github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= +github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= +github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/hashicorp/go-retryablehttp v0.7.8 h1:ylXZWnqa7Lhqpk0L1P1LzDtGcCR0rPVUrx/c8Unxc48= +github.com/hashicorp/go-retryablehttp v0.7.8/go.mod h1:rjiScheydd+CxvumBsIrFKlx3iS0jrZ7LvzFGFmuKbw= +github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc= +github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8= +github.com/hashicorp/go-secure-stdlib/parseutil v0.2.0 h1:U+kC2dOhMFQctRfhK0gRctKAPTloZdMU5ZJxaesJ/VM= +github.com/hashicorp/go-secure-stdlib/parseutil v0.2.0/go.mod h1:Ll013mhdmsVDuoIXVfBtvgGJsXDYkTw1kooNcoCXuE0= +github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 h1:kes8mmyCpxJsI7FTwtzRqEy9CdjCtrXrXGuOpxEA7Ts= +github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25LAKCLuM+y9U2T4hlwvT1yprcna4= +github.com/hashicorp/go-sockaddr v1.0.7 h1:G+pTkSO01HpR5qCxg7lxfsFEZaG+C0VssTy/9dbT+Fw= +github.com/hashicorp/go-sockaddr v1.0.7/go.mod h1:FZQbEYa1pxkQ7WLpyXJ6cbjpT8q0YgQaK/JakXqGyWw= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/hashicorp/hcl v1.0.1-vault-7 h1:ag5OxFVy3QYTFTJODRzTKVZ6xvdfLLCA1cy/Y6xGI0I= +github.com/hashicorp/hcl v1.0.1-vault-7/go.mod h1:XYhtn6ijBSAj6n4YqAaf7RBPS4I06AItNorpy+MoQNM= +github.com/hashicorp/vault/api v1.23.0 h1:gXgluBsSECfRWTSW9niY2jwg2e9mMJc4WoHNv4g3h6A= +github.com/hashicorp/vault/api v1.23.0/go.mod h1:zransKiB9ftp+kgY8ydjnvCU7Wk8i9L0DYWpXeMj9ko= github.com/ianlancetaylor/demangle v0.0.0-20251118225945-96ee0021ea0f h1:Fnl4pzx8SR7k7JuzyW8lEtSFH6EQ8xgcypgIn8pcGIE= github.com/ianlancetaylor/demangle v0.0.0-20251118225945-96ee0021ea0f/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw= github.com/ironsh/onepassword-sdk-go v0.0.0-20260430210150-17395b1aedb5 h1:Jw858CPkrtJvWCvuAgS971UbzJzMdaI9GWjxK5sPMjA= @@ -119,36 +154,46 @@ github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0= github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= -github.com/klauspost/compress v1.18.5 h1:/h1gH5Ce+VWNLSWqPzOVn6XBO+vJbCNGvjoaGBFW2IE= -github.com/klauspost/compress v1.18.5/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/klauspost/compress v1.18.6 h1:2jupLlAwFm95+YDR+NwD2MEfFO9d4z4Prjl1XXDjuao= +github.com/klauspost/compress v1.18.6/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE= github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= +github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= +github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= -github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= +github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e h1:Q6MvJtQK/iRcRtzAscm/zF23XxJlbECiGPyRicsX+Ak= +github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg= github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE= github.com/magiconair/properties v1.8.10/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= +github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI= github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o= github.com/miekg/dns v1.1.72 h1:vhmr+TF2A3tuoGNkLDFK9zi36F2LS+hKTRW0Uf8kbzI= github.com/miekg/dns v1.1.72/go.mod h1:+EuEPhdHOsfk6Wk5TT2CzssZdqkmFhf8r+aVyDEToIs= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= github.com/moby/go-archive v0.2.0 h1:zg5QDUM2mi0JIM9fdQZWC7U8+2ZfixfTYoHL7rWUcP8= github.com/moby/go-archive v0.2.0/go.mod h1:mNeivT14o8xU+5q1YnNrkQVpK+dnNe/K6fHqnTg4qPU= -github.com/moby/moby/api v1.54.1 h1:TqVzuJkOLsgLDDwNLmYqACUuTehOHRGKiPhvH8V3Nn4= -github.com/moby/moby/api v1.54.1/go.mod h1:+RQ6wluLwtYaTd1WnPLykIDPekkuyD/ROWQClE83pzs= -github.com/moby/moby/client v0.4.0 h1:S+2XegzHQrrvTCvF6s5HFzcrywWQmuVnhOXe2kiWjIw= -github.com/moby/moby/client v0.4.0/go.mod h1:QWPbvWchQbxBNdaLSpoKpCdf5E+WxFAgNHogCWDoa7g= +github.com/moby/moby/api v1.55.0 h1:2/sexvQyqIWS8pRSCFddBfpW2qE7vR7FCL+vN8pxwMc= +github.com/moby/moby/api v1.55.0/go.mod h1:+RQ6wluLwtYaTd1WnPLykIDPekkuyD/ROWQClE83pzs= +github.com/moby/moby/client v0.5.0 h1:5XhyPk2fuOWf6RlSFa3MkIIgDZkF25xToXW8Q/BH7cc= +github.com/moby/moby/client v0.5.0/go.mod h1:rcVpF8ncl9vo5gaIBdol6CnbEtSj1uxMvEV/UrykF/s= github.com/moby/patternmatcher v0.6.1 h1:qlhtafmr6kgMIJjKJMDmMWq7WLkKIo23hsrpR3x084U= github.com/moby/patternmatcher v0.6.1/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc= -github.com/moby/sys/sequential v0.6.0 h1:qrx7XFUd/5DxtqcoH1h438hF5TmOvzC/lspjy7zgvCU= -github.com/moby/sys/sequential v0.6.0/go.mod h1:uyv8EUTrca5PnDsdMGXhZe6CCe8U/UiTWd+lL+7b/Ko= +github.com/moby/sys/sequential v0.7.0 h1:ASQNGNROJSuOO6LL6bPHbKvuZu6NU8P4ldPWk31zj/8= +github.com/moby/sys/sequential v0.7.0/go.mod h1:NfSTAp6V3fw4tmkD62PEcOKeZKquXT8VKCkf7aVR79o= github.com/moby/sys/user v0.4.0 h1:jhcMKit7SA80hivmFJcbB1vqmw//wU61Zdui2eQXuMs= github.com/moby/sys/user v0.4.0/go.mod h1:bG+tYYYJgaMtRKgEmuueC0hJEAZWwtIbZTB+85uoHjs= github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g= @@ -165,14 +210,18 @@ github.com/pganalyze/pg_query_go/v6 v6.2.2 h1:O0L6zMC226R82RF3X5n0Ki6HjytDsoAzuz github.com/pganalyze/pg_query_go/v6 v6.2.2/go.mod h1:Cn6+j4870kJz3iYNsb0VsNG04vpSWgEvBwc590J4qD0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/sftp v1.13.12 h1:Myhzy/ZzLPnCtE2F2WtDHsgs8RfgMnH2gfbHuCJhkMU= +github.com/pkg/sftp v1.13.12/go.mod h1:uNkH9roSXglNJqM+glJJi+TQXQUm0fXFWqCFmT8hsN0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU= github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= -github.com/shirou/gopsutil/v4 v4.26.3 h1:2ESdQt90yU3oXF/CdOlRCJxrP+Am1aBYubTMTfxJ1qc= -github.com/shirou/gopsutil/v4 v4.26.3/go.mod h1:LZ6ewCSkBqUpvSOf+LsTGnRinC6iaNUNMGBtDkJBaLQ= +github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk= +github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= +github.com/shirou/gopsutil/v4 v4.26.6 h1:Mzr/npDtQC/xpeEuQKHZt8Zo9CmPvhTj8nkR8w5TLDs= +github.com/shirou/gopsutil/v4 v4.26.6/go.mod h1:LZ6ewCSkBqUpvSOf+LsTGnRinC6iaNUNMGBtDkJBaLQ= github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w= github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -182,18 +231,18 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= -github.com/testcontainers/testcontainers-go v0.42.0 h1:He3IhTzTZOygSXLJPMX7n44XtK+qhjat1nI9cneBbUY= -github.com/testcontainers/testcontainers-go v0.42.0/go.mod h1:vZjdY1YmUA1qEForxOIOazfsrdyORJAbhi0bp8plN30= -github.com/testcontainers/testcontainers-go/modules/postgres v0.42.0 h1:GCbb1ndrF7OTDiIvxXyItaDab4qkzTFJ48LKFdM7EIo= -github.com/testcontainers/testcontainers-go/modules/postgres v0.42.0/go.mod h1:IRPBaI8jXdrNfD0e4Zm7Fbcgaz5shKxOQv4axiL09xs= +github.com/testcontainers/testcontainers-go v0.44.0 h1:/Fwh6HY1mIikhnm9e7HwoxGycx0lzRAE0f5VQpjFxzI= +github.com/testcontainers/testcontainers-go v0.44.0/go.mod h1:IcnwQrYTO86xHXu5bvMaBH7ATlbS3Qn1M1QWW3c66rE= +github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 h1:8fdv/9y3JMxjQ+ULAcOG8RtgeNu5t9XF9LolSXDuTwM= +github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0/go.mod h1:CFr2LncGYokw+OKjXcr8ARCKG1SaC2UEnGxFBovE86g= github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834 h1:ZF+QBjOI+tILZjBaFj3HgFonKXUcwgJ4djLb6i42S3Q= github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834/go.mod h1:m9ymHTgNSEjuxvw8E7WWe4Pl4hZQHXONY8wE6dMLaRk= github.com/tetratelabs/wazero v1.11.0 h1:+gKemEuKCTevU4d7ZTzlsvgd1uaToIDtlQlmNbwqYhA= github.com/tetratelabs/wazero v1.11.0/go.mod h1:eV28rsN8Q+xwjogd7f4/Pp4xFxO7uOGbLcD/LzB1wiU= -github.com/tklauser/go-sysconf v0.3.16 h1:frioLaCQSsF5Cy1jgRBrzr6t502KIIwQ0MArYICU0nA= -github.com/tklauser/go-sysconf v0.3.16/go.mod h1:/qNL9xxDhc7tx3HSRsLWNnuzbVfh3e7gh/BmM179nYI= -github.com/tklauser/numcpus v0.11.0 h1:nSTwhKH5e1dMNsCdVBukSZrURJRoHbSEQjdEbY+9RXw= -github.com/tklauser/numcpus v0.11.0/go.mod h1:z+LwcLq54uWZTX0u/bGobaV34u6V7KNlTZejzM6/3MQ= +github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU= +github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI= +github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4= +github.com/tklauser/numcpus v0.12.0/go.mod h1:ABHeXzJnr/qqwguhClkZKT1/8VABcYrsyUiUGobwWJg= github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o= github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg= @@ -212,8 +261,8 @@ github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo= github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4= go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= -go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 h1:sbiXRNDSWJOTobXh5HyQKjq6wUC5tNybqjIqDpAY4CU= -go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0/go.mod h1:69uWxva0WgAA/4bu2Yy70SLDBwZXuQ6PbBpbsa5iZrQ= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0 h1:8tvICD4vSTOOsNrsI4Ljf6C+6UKvpTEH5XY3JMoyPoo= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0/go.mod h1:z9+yiacE0IHRqM4qFfkbt/JYlmYXgss8GY/jXoNuPJI= go.opentelemetry.io/otel v1.44.0 h1:JjwHmHpA4iZ3wBxluu2fbbE7j4kqlE8jXyAyPXH7HqU= go.opentelemetry.io/otel v1.44.0/go.mod h1:BMgjTHL9WPRlRjL2oZCBTL4whCGtXch2H4BhOPIAyYc= go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.20.0 h1:rydZ9sxbcFdm/oWrVyfLTjHIygMgv0bEeMd+3B/BvoM= @@ -238,36 +287,39 @@ go.opentelemetry.io/proto/otlp v1.10.0 h1:IQRWgT5srOCYfiWnpqUYz9CVmbO8bFmKcwYxpu go.opentelemetry.io/proto/otlp v1.10.0/go.mod h1:/CV4QoCR/S9yaPj8utp3lvQPoqMtxXdzn7ozvvozVqk= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI= -golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8= -golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM= -golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU= -golang.org/x/net v0.55.0 h1:bcvxaJn3e1U6InsFWt1JUq1aSjnRxLzT2rtD2KfkDF8= -golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww= +golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M= +golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis= +golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk= +golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40= +golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To= +golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU= golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs= golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q= -golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4= -golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= +golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= +golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY= -golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= -golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4= -golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk= -golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc= -golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38= -golang.org/x/tools v0.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c= -golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= +golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/term v0.45.0 h1:NwWyBmoJCbfTHpxrWoZ9C6/VxOf7ic219I8xZZFdrf0= +golang.org/x/term v0.45.0/go.mod h1:9aqxs0blBcrm/n0L9QW0aRVD+ktan8ssZromtqJC43w= +golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= +golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= +golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE= +golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= +golang.org/x/tools v0.48.0 h1:3+hClM1aLL5mjMKm5ovokw9epgRXPuu2tILgismM6RE= +golang.org/x/tools v0.48.0/go.mod h1:08xX0orndb/F7jJxGDicx061tyd5pcMto75YMAXr6lk= gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa h1:Kjn0N0tCrDgiAFW+lGO4JZ3ck44CehvJQMAwj9QF0G8= google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa/go.mod h1:q4lMZS6kskjT5HvCPrnnypcDPVJqT/f4nfxmkE7gryY= google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa h1:mZHHdPZl0dbGHCflZgAq/Q468DWVFcU2whhB2KAo8fk= google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8= -google.golang.org/grpc v1.81.1 h1:VnnIIZ88UzOOKLukQi+ImGz8O1Wdp8nAGGnvOfEIWQQ= -google.golang.org/grpc v1.81.1/go.mod h1:xGH9GfzOyMTGIOXBJmXt+BX/V0kcdQbdcuwQ/zNw42I= +google.golang.org/grpc v1.83.0 h1:JeNZEKJFbQxArAMl+hiytHauacDNqJUllNfmIMmpqnQ= +google.golang.org/grpc v1.83.0/go.mod h1:kDyl6SKsiHKt0uylY5gtn5cEjkrIOhQOGDgIc4JGwzQ= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/integration_test/gcp_id_token_test.go b/integration_test/gcp_id_token_test.go new file mode 100644 index 0000000..9d0a4b5 --- /dev/null +++ b/integration_test/gcp_id_token_test.go @@ -0,0 +1,149 @@ +package integration_test + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +// TestGCPIDToken drives the gcp_id_token transform end-to-end without real GCP +// infra by steering the service-account token_uri at a local httptest server. +func TestGCPIDToken(t *testing.T) { + tmpDir := t.TempDir() + binary := proxyBinary(t) + + const audience = "https://private-service.run.app" + var ( + tokenCalls atomic.Int64 + gotAud atomic.Value + ) + tokenSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + tokenCalls.Add(1) + require.NoError(t, r.ParseForm()) + assertion := r.PostForm.Get("assertion") + claims := decodeJWTClaims(t, assertion) + targetAudience, ok := claims["target_audience"].(string) + require.True(t, ok, "service account assertion must include target_audience") + gotAud.Store(targetAudience) + idToken := fakeGoogleIDToken(t, targetAudience) + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "id_token": idToken, + "token_type": "Bearer", + "expires_in": 3600, + }) + })) + defer tokenSrv.Close() + + var ( + mu sync.Mutex + gotAuth string + ) + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + gotAuth = r.Header.Get("Authorization") + mu.Unlock() + w.WriteHeader(http.StatusOK) + })) + defer upstream.Close() + + keyfile := writeIDTokenKeyfile(t, tmpDir, tokenSrv.URL) + cfgPath := renderConfig(t, tmpDir, "gcp_id_token.yaml", struct { + KeyfilePath string + Audience string + }{ + KeyfilePath: keyfile, + Audience: audience, + }) + proxy := startProxy(t, binary, cfgPath, nil) + upstreamHost := upstream.Listener.Addr().String() + + req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/private", proxy.HTTPAddr), nil) + require.NoError(t, err) + req.Host = upstreamHost + req.Header.Set("Authorization", "Bearer agent-placeholder") + + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + _, err = io.Copy(io.Discard, resp.Body) + require.NoError(t, err) + require.Equal(t, http.StatusOK, resp.StatusCode) + + mu.Lock() + defer mu.Unlock() + require.NotEmpty(t, gotAuth) + require.NotContains(t, gotAuth, "agent-placeholder", "agent placeholder bearer must be replaced") + require.True(t, strings.HasPrefix(gotAuth, "Bearer "), "ID token must be injected as a bearer") + claims := decodeJWTClaims(t, strings.TrimPrefix(gotAuth, "Bearer ")) + require.Equal(t, audience, claims["aud"]) + require.Equal(t, audience, gotAud.Load()) + require.Equal(t, int64(1), tokenCalls.Load(), "token endpoint should be hit exactly once and then cached") +} + +func writeIDTokenKeyfile(t *testing.T, dir, tokenURI string) string { + t.Helper() + keyfile := map[string]string{ + "type": "service_account", + "project_id": "iron-proxy-id-token-test", + "private_key": generateServiceAccountKeyPEM(t), + "client_email": "cloud-run-caller@iron-proxy-test.iam.gserviceaccount.com", + "token_uri": tokenURI, + } + data, err := json.MarshalIndent(keyfile, "", " ") + require.NoError(t, err) + + path := filepath.Join(dir, "cloud-run-sa.json") + require.NoError(t, os.WriteFile(path, data, 0o600)) + return path +} + +func fakeGoogleIDToken(t *testing.T, audience string) string { + t.Helper() + now := time.Now() + header := map[string]any{ + "alg": "RS256", + "typ": "JWT", + } + claims := map[string]any{ + "iss": "https://accounts.google.com", + "aud": audience, + "azp": "112010400000000710080", + "sub": "112010400000000710080", + "email": "cloud-run-caller@iron-proxy-test.iam.gserviceaccount.com", + "email_verified": true, + "iat": now.Unix(), + "exp": now.Add(time.Hour).Unix(), + } + return encodeJWTPart(t, header) + "." + encodeJWTPart(t, claims) + "." + encodeJWTPart(t, "signature") +} + +func decodeJWTClaims(t *testing.T, token string) map[string]any { + t.Helper() + parts := strings.Split(token, ".") + require.Len(t, parts, 3, "token is not a JWT") + raw, err := base64.RawURLEncoding.DecodeString(parts[1]) + require.NoError(t, err) + var claims map[string]any + require.NoError(t, json.Unmarshal(raw, &claims)) + return claims +} + +func encodeJWTPart(t *testing.T, v any) string { + t.Helper() + b, err := json.Marshal(v) + require.NoError(t, err) + return base64.RawURLEncoding.EncodeToString(b) +} diff --git a/integration_test/postgres_test.go b/integration_test/postgres_test.go index 21cca18..f707932 100644 --- a/integration_test/postgres_test.go +++ b/integration_test/postgres_test.go @@ -307,13 +307,31 @@ func TestPostgresPolicy(t *testing.T) { require.Equal(t, pgRole, role) }) - t.Run("set_config for other gucs passes through", func(t *testing.T) { - // Only role-mutating GUCs trigger the rejection; set_config for benign - // parameters should pass through. - conn := dial(t, pgClientPassword) - results, err := conn.Exec(context.Background(), "SELECT set_config('application_name', 'iron-test', false)").ReadAll() + t.Run("parameterized set_config target is rejected", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + conn, err := pgx.Connect(ctx, connStr(pgClientPassword)) require.NoError(t, err) - require.Len(t, results, 1) + t.Cleanup(func() { require.NoError(t, conn.Close(context.Background())) }) + + _, err = conn.Exec(ctx, "SELECT set_config($1, $2, false)", "role", "other_role") + require.Error(t, err) + var pgErr *pgconn.PgError + require.True(t, errors.As(err, &pgErr)) + require.Contains(t, pgErr.Message, "set_config is not supported") + + var role string + require.NoError(t, conn.QueryRow(ctx, "SELECT current_role").Scan(&role)) + require.Equal(t, pgRole, role) + }) + + t.Run("set_config for other gucs is rejected", func(t *testing.T) { + conn := dial(t, pgClientPassword) + _, err := conn.Exec(context.Background(), "SELECT set_config('application_name', 'iron-test', false)").ReadAll() + require.Error(t, err) + var pgErr *pgconn.PgError + require.True(t, errors.As(err, &pgErr)) + require.Contains(t, pgErr.Message, "set_config is not supported") }) t.Run("session setting is injected at session start", func(t *testing.T) { @@ -355,6 +373,34 @@ func TestPostgresPolicy(t *testing.T) { require.Equal(t, pgRole, currentRole(t, conn)) }) + t.Run("pg_settings writes are rejected", func(t *testing.T) { + // pg_settings' update rule calls set_config, and an auto-updatable + // view over it forwards updates, so both would bypass pinned settings. + conn := dial(t, pgClientPassword) + for _, sql := range []string{ + "UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", + "CREATE TEMP VIEW settings_view AS SELECT * FROM pg_settings", + } { + err := exec(t, conn, sql) + require.Error(t, err) + var pgErr *pgconn.PgError + require.True(t, errors.As(err, &pgErr)) + require.Contains(t, pgErr.Message, "writing to pg_settings") + } + }) + + t.Run("callback definition aliasing set_config is rejected", func(t *testing.T) { + // An aggregate whose SFUNC is set_config would let the client invoke + // set_config under another name, bypassing the FuncCall check. + conn := dial(t, pgClientPassword) + err := exec(t, conn, "CREATE AGGREGATE pg_temp.cfg(text, boolean) (SFUNC = pg_catalog.set_config, STYPE = text, INITCOND = 'role')") + require.Error(t, err) + var pgErr *pgconn.PgError + require.True(t, errors.As(err, &pgErr)) + require.Contains(t, pgErr.Message, "bind functions as callbacks") + require.Equal(t, pgRole, currentRole(t, conn)) + }) + t.Run("do block is rejected", func(t *testing.T) { conn := dial(t, pgClientPassword) err := exec(t, conn, "DO $$ BEGIN PERFORM 1; END $$") diff --git a/integration_test/testdata/gcp_id_token.yaml b/integration_test/testdata/gcp_id_token.yaml new file mode 100644 index 0000000..c09ce89 --- /dev/null +++ b/integration_test/testdata/gcp_id_token.yaml @@ -0,0 +1,32 @@ +dns: + proxy_ip: "127.0.0.1" + listen: "127.0.0.1:0" + +proxy: + http_listen: "127.0.0.1:0" + https_listen: "127.0.0.1:0" + max_request_body_bytes: 1048576 + upstream_deny_cidrs: [] + +tls: + ca_cert: tmp/ca.crt + ca_key: tmp/ca.key + +transforms: + - name: allowlist + config: + domains: + - "127.0.0.1" + + - name: gcp_id_token + config: + keyfile_path: "{{ .KeyfilePath }}" + audience: "{{ .Audience }}" + rules: + - host: "127.0.0.1" + +metrics: + listen: "127.0.0.1:0" + +log: + level: info diff --git a/integration_test/testdata/vault_kv.yaml b/integration_test/testdata/vault_kv.yaml new file mode 100644 index 0000000..c6d2563 --- /dev/null +++ b/integration_test/testdata/vault_kv.yaml @@ -0,0 +1,38 @@ +dns: + proxy_ip: "127.0.0.1" + listen: "127.0.0.1:0" + +proxy: + http_listen: "127.0.0.1:0" + https_listen: "127.0.0.1:0" + max_request_body_bytes: 1048576 + upstream_deny_cidrs: [] + +tls: + ca_cert: tmp/ca.crt + ca_key: tmp/ca.key + +transforms: + - name: allowlist + config: + domains: + - "127.0.0.1" + + - name: secrets + config: + secrets: + - source: + type: vault_kv + mount: "secret" + path: "services/openai" + json_key: "api_key" + proxy_value: "proxy-vault-secret" + match_headers: ["X-Vault-Secret"] + rules: + - host: "127.0.0.1" + +metrics: + listen: "127.0.0.1:0" + +log: + level: info diff --git a/integration_test/vault_kv_test.go b/integration_test/vault_kv_test.go new file mode 100644 index 0000000..571d334 --- /dev/null +++ b/integration_test/vault_kv_test.go @@ -0,0 +1,53 @@ +package integration_test + +import ( + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/require" +) + +// TestVaultKV boots the proxy against a Vault-compatible KV v2 endpoint and +// verifies that the official client configuration and secret transform work +// together without requiring external Vault credentials. +func TestVaultKV(t *testing.T) { + type vaultRequest struct { + path string + token string + } + requests := make(chan vaultRequest, 1) + vault := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests <- vaultRequest{path: r.URL.Path, token: r.Header.Get("X-Vault-Token")} + w.Header().Set("Content-Type", "application/json") + // The in-memory server has no useful recovery for a client disconnect; + // the proxy request below reports the resulting read failure. + _, _ = io.WriteString(w, `{"data":{"data":{"api_key":"real-vault-secret"},"metadata":{}}}`) + })) + t.Cleanup(vault.Close) + + upstreamHost := echoHeadersUpstream(t, "X-Vault-Secret") + cfgPath := renderConfig(t, t.TempDir(), "vault_kv.yaml", nil) + proxy := startProxy(t, proxyBinary(t), cfgPath, []string{ + "VAULT_ADDR=" + vault.URL, + "VAULT_TOKEN=integration-token", + "VAULT_NAMESPACE=", + "VAULT_AGENT_ADDR=", + "VAULT_PROXY_ADDR=", + "VAULT_CACERT=", + "VAULT_CAPATH=", + "VAULT_CLIENT_CERT=", + "VAULT_CLIENT_KEY=", + }) + + status, headers := proxyGet(t, proxy.HTTPAddr, upstreamHost, map[string]string{ + "X-Vault-Secret": "proxy-vault-secret", + }) + require.Equal(t, http.StatusOK, status) + require.Equal(t, "real-vault-secret", headers.Get("X-Got-Vault-Secret")) + + request := <-requests + require.Equal(t, "/v1/secret/data/services/openai", request.path) + require.Equal(t, "integration-token", request.token) +} diff --git a/internal/certcache/cache.go b/internal/certcache/cache.go index f1d8ad8..c2fd96f 100644 --- a/internal/certcache/cache.go +++ b/internal/certcache/cache.go @@ -68,7 +68,13 @@ func (c *Cache) GetOrCreate(domain string) (*tls.Certificate, error) { defer c.mu.Unlock() if cert, ok := c.cache.Get(domain); ok { - return cert, nil + // The LRU expiry uses Go's monotonic clock, which does not advance while + // the host is suspended. Certificate validity uses wall time, so reject a + // cached leaf that has expired even when its LRU entry is still alive. + if cert.Leaf != nil && time.Now().Before(cert.Leaf.NotAfter) { + return cert, nil + } + c.cache.Remove(domain) } cert, err := c.generate(domain) diff --git a/internal/certcache/cache_test.go b/internal/certcache/cache_test.go index dd17f2c..2495676 100644 --- a/internal/certcache/cache_test.go +++ b/internal/certcache/cache_test.go @@ -328,6 +328,22 @@ func TestGetOrCreate_CacheHit(t *testing.T) { require.Equal(t, 1, c.Len()) } +func TestGetOrCreate_ExpiredLeaf(t *testing.T) { + caCert, caKey := generateTestCA(t) + c := newTestCache(t, caCert, caKey, 10) + + cert1, err := c.GetOrCreate("example.com") + require.NoError(t, err) + cert1.Leaf.NotAfter = time.Now().Add(-time.Minute) + + cert2, err := c.GetOrCreate("example.com") + require.NoError(t, err) + + require.NotSame(t, cert1, cert2) + require.True(t, cert2.Leaf.NotAfter.After(time.Now())) + require.Equal(t, 1, c.Len()) +} + func TestGetOrCreate_DifferentDomains(t *testing.T) { caCert, caKey := generateTestCA(t) c := newTestCache(t, caCert, caKey, 10) diff --git a/internal/config/config.go b/internal/config/config.go index 5ed8e11..4efc442 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -12,9 +12,15 @@ import ( "github.com/ironsh/iron-proxy/internal/dnsguard" ) -// DefaultUpstreamResponseHeaderTimeout is the default cap on how long the -// proxy waits for upstream response headers before returning 502. -const DefaultUpstreamResponseHeaderTimeout = 30 * time.Second +const ( + // DefaultUpstreamResponseHeaderTimeout is the default cap on how long the + // proxy waits for upstream response headers before returning 502. + DefaultUpstreamResponseHeaderTimeout = 30 * time.Second + + // DefaultControlPlanePollInterval is the default base interval between + // control-plane sync calls. The poller applies jitter around this value. + DefaultControlPlanePollInterval = 10 * time.Second +) // Duration is a time.Duration that decodes YAML strings using // time.ParseDuration ("30s", "5m", "2h"). The zero value indicates "unset" @@ -46,15 +52,18 @@ func (d *Duration) UnmarshalYAML(value *yaml.Node) error { // package stays decoupled from the consumer's typed schema, and validation // errors surface where the value is actually used. type Config struct { - DNS DNS `yaml:"dns"` - Proxy Proxy `yaml:"proxy"` - TLS TLS `yaml:"tls"` - Transforms []Transform `yaml:"transforms"` - MCP yaml.Node `yaml:"mcp"` - Postgres yaml.Node `yaml:"postgres"` - Metrics Metrics `yaml:"metrics"` - Management Management `yaml:"management"` - Log Log `yaml:"log"` + SSH yaml.Node `yaml:"ssh"` + DNS DNS `yaml:"dns"` + Proxy Proxy `yaml:"proxy"` + TLS TLS `yaml:"tls"` + Transforms []Transform `yaml:"transforms"` + MCP yaml.Node `yaml:"mcp"` + MCPGateway yaml.Node `yaml:"mcp_gateway"` + Postgres yaml.Node `yaml:"postgres"` + Metrics Metrics `yaml:"metrics"` + Management Management `yaml:"management"` + ControlPlane ControlPlane `yaml:"control_plane"` + Log Log `yaml:"log"` } // DNS configures the built-in DNS server. @@ -166,6 +175,14 @@ type Management struct { APIKeyEnv string `yaml:"api_key_env"` } +// ControlPlane configures managed-mode control-plane behavior. +type ControlPlane struct { + // PollInterval is the base interval between control-plane sync calls. + // Accepts Go duration syntax: "10s" (default), "1m", "500ms". + // The poller applies jitter around this value. + PollInterval Duration `yaml:"poll_interval"` +} + // Log configures structured logging. type Log struct { Level string `yaml:"level"` @@ -257,6 +274,9 @@ func applyDefaults(cfg *Config) { if cfg.Management.Listen != "" && cfg.Management.APIKeyEnv == "" { cfg.Management.APIKeyEnv = "IRON_MANAGEMENT_API_KEY" } + if cfg.ControlPlane.PollInterval == 0 { + cfg.ControlPlane.PollInterval = Duration(DefaultControlPlanePollInterval) + } if cfg.Log.Level == "" { cfg.Log.Level = "info" } @@ -289,6 +309,9 @@ func Validate(cfg *Config) error { if cfg.Proxy.UpstreamResponseHeaderTimeout <= 0 { return fmt.Errorf("proxy.upstream_response_header_timeout must be positive; got %s", time.Duration(cfg.Proxy.UpstreamResponseHeaderTimeout)) } + if cfg.ControlPlane.PollInterval <= 0 { + return fmt.Errorf("control_plane.poll_interval must be positive; got %s", time.Duration(cfg.ControlPlane.PollInterval)) + } if err := dnsguard.ValidateCIDRs(cfg.Proxy.UpstreamDenyCIDRs.Values); err != nil { return fmt.Errorf("proxy.upstream_deny_cidrs: %w", err) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 0b537dd..b7c4910 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -538,3 +538,63 @@ tls: require.Contains(t, err.Error(), "must be positive") }) } + +func TestLoad_ControlPlanePollInterval(t *testing.T) { + t.Run("default applied when unset", func(t *testing.T) { + yaml := ` +dns: + proxy_ip: "10.0.0.1" +tls: + ca_cert: "/tmp/ca.crt" + ca_key: "/tmp/ca.key" +` + cfg, err := Load(strings.NewReader(yaml)) + require.NoError(t, err) + require.Equal(t, 10*time.Second, time.Duration(cfg.ControlPlane.PollInterval)) + }) + + t.Run("valid duration accepted", func(t *testing.T) { + yaml := ` +dns: + proxy_ip: "10.0.0.1" +control_plane: + poll_interval: "30s" +tls: + ca_cert: "/tmp/ca.crt" + ca_key: "/tmp/ca.key" +` + cfg, err := Load(strings.NewReader(yaml)) + require.NoError(t, err) + require.Equal(t, 30*time.Second, time.Duration(cfg.ControlPlane.PollInterval)) + }) + + t.Run("invalid duration rejected at parse", func(t *testing.T) { + yaml := ` +dns: + proxy_ip: "10.0.0.1" +control_plane: + poll_interval: "not-a-duration" +tls: + ca_cert: "/tmp/ca.crt" + ca_key: "/tmp/ca.key" +` + _, err := Load(strings.NewReader(yaml)) + require.Error(t, err) + require.Contains(t, err.Error(), "invalid duration") + }) + + t.Run("negative duration rejected at validate", func(t *testing.T) { + yaml := ` +dns: + proxy_ip: "10.0.0.1" +control_plane: + poll_interval: "-5s" +tls: + ca_cert: "/tmp/ca.crt" + ca_key: "/tmp/ca.key" +` + _, err := Load(strings.NewReader(yaml)) + require.Error(t, err) + require.Contains(t, err.Error(), "control_plane.poll_interval must be positive") + }) +} diff --git a/internal/config/env.go b/internal/config/env.go index 8458f80..9fbdba3 100644 --- a/internal/config/env.go +++ b/internal/config/env.go @@ -4,12 +4,23 @@ import ( "fmt" "os" "strconv" + "strings" "time" + + "gopkg.in/yaml.v3" + + "github.com/ironsh/iron-proxy/internal/dnsguard" ) // applyEnvOverrides layers IRON_* environment variables on top of an existing // Config. Only non-empty environment variables override the corresponding field. func applyEnvOverrides(cfg *Config) error { + if raw := os.Getenv("IRON_SSH_CONFIG_JSON"); raw != "" { + if yaml.Unmarshal([]byte(raw), &cfg.SSH) != nil { + return fmt.Errorf("IRON_SSH_CONFIG_JSON is invalid") + } + } + if v := os.Getenv("IRON_DNS_ENABLED"); v != "" { b, err := strconv.ParseBool(v) if err != nil { @@ -94,5 +105,38 @@ func applyEnvOverrides(cfg *Config) error { cfg.Proxy.UpstreamResponseHeaderTimeout = Duration(d) } + if v := os.Getenv("IRON_PROXY_UPSTREAM_DENY_CIDRS"); v != "" { + cidrs, err := splitCIDREnvList(v) + if err != nil { + return fmt.Errorf("IRON_PROXY_UPSTREAM_DENY_CIDRS: %w", err) + } + if err := dnsguard.ValidateCIDRs(cidrs); err != nil { + return fmt.Errorf("IRON_PROXY_UPSTREAM_DENY_CIDRS: %w", err) + } + cfg.Proxy.UpstreamDenyCIDRs.Values = cidrs + cfg.Proxy.UpstreamDenyCIDRs.Set = true + } + + if v := os.Getenv("IRON_CONTROL_PLANE_POLL_INTERVAL"); v != "" { + d, err := time.ParseDuration(v) + if err != nil { + return fmt.Errorf("IRON_CONTROL_PLANE_POLL_INTERVAL: %w", err) + } + cfg.ControlPlane.PollInterval = Duration(d) + } + return nil } + +func splitCIDREnvList(v string) ([]string, error) { + parts := strings.Split(v, ",") + values := make([]string, 0, len(parts)) + for _, part := range parts { + trimmed := strings.TrimSpace(part) + if trimmed == "" { + return nil, fmt.Errorf("empty CIDR entry") + } + values = append(values, trimmed) + } + return values, nil +} diff --git a/internal/config/env_test.go b/internal/config/env_test.go index d377abc..a0cbab7 100644 --- a/internal/config/env_test.go +++ b/internal/config/env_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/ironsh/iron-proxy/internal/dnsguard" "github.com/stretchr/testify/require" ) @@ -47,6 +48,7 @@ func TestLoadConfig_EnvOnly_AllOverrides(t *testing.T) { "IRON_PROXY_MAX_REQUEST_BODY_BYTES": "2097152", "IRON_PROXY_MAX_RESPONSE_BODY_BYTES": "4194304", "IRON_PROXY_UPSTREAM_RESPONSE_HEADER_TIMEOUT": "5m", + "IRON_PROXY_UPSTREAM_DENY_CIDRS": "10.0.0.0/8, 192.168.0.0/16", "IRON_TLS_CA_CERT": "/custom/ca.pem", "IRON_TLS_CA_KEY": "/custom/ca-key.pem", "IRON_TLS_CERT_CACHE_SIZE": "500", @@ -66,6 +68,8 @@ func TestLoadConfig_EnvOnly_AllOverrides(t *testing.T) { require.Equal(t, ":1080", cfg.Proxy.TunnelListen) require.Equal(t, int64(2097152), cfg.Proxy.MaxRequestBodyBytes) require.Equal(t, int64(4194304), cfg.Proxy.MaxResponseBodyBytes) + require.True(t, cfg.Proxy.UpstreamDenyCIDRs.Set) + require.Equal(t, []string{"10.0.0.0/8", "192.168.0.0/16"}, cfg.Proxy.UpstreamDenyCIDRs.Values) require.Equal(t, "/custom/ca.pem", cfg.TLS.CACert) require.Equal(t, "/custom/ca-key.pem", cfg.TLS.CAKey) require.Equal(t, 500, cfg.TLS.CertCacheSize) @@ -264,6 +268,99 @@ func TestApplyEnvOverrides_UpstreamResponseHeaderTimeout(t *testing.T) { }) } +func TestApplyEnvOverrides_UpstreamDenyCIDRs(t *testing.T) { + t.Run("env override applied", func(t *testing.T) { + setEnvs(t, map[string]string{ + "IRON_PROXY_UPSTREAM_DENY_CIDRS": "10.0.0.0/8,192.168.0.0/16", + }) + + var cfg Config + require.NoError(t, applyEnvOverrides(&cfg)) + require.True(t, cfg.Proxy.UpstreamDenyCIDRs.Set) + require.Equal(t, []string{"10.0.0.0/8", "192.168.0.0/16"}, cfg.Proxy.UpstreamDenyCIDRs.Values) + }) + + t.Run("env override beats yaml value", func(t *testing.T) { + setEnvs(t, map[string]string{ + "IRON_PROXY_UPSTREAM_DENY_CIDRS": "10.0.0.0/8", + }) + + cfg := Config{Proxy: Proxy{UpstreamDenyCIDRs: CIDRList{ + Values: []string{"169.254.169.254/32"}, + Set: true, + }}} + require.NoError(t, applyEnvOverrides(&cfg)) + require.True(t, cfg.Proxy.UpstreamDenyCIDRs.Set) + require.Equal(t, []string{"10.0.0.0/8"}, cfg.Proxy.UpstreamDenyCIDRs.Values) + }) + + t.Run("unset keeps default", func(t *testing.T) { + setEnvs(t, map[string]string{}) + + cfg, err := LoadConfig("") + require.NoError(t, err) + require.True(t, cfg.Proxy.UpstreamDenyCIDRs.Set) + require.Equal(t, dnsguard.DefaultDenyCIDRs, cfg.Proxy.UpstreamDenyCIDRs.Values) + }) + + t.Run("invalid cidr rejected", func(t *testing.T) { + setEnvs(t, map[string]string{ + "IRON_PROXY_UPSTREAM_DENY_CIDRS": "1.2.3.4", + }) + + var cfg Config + err := applyEnvOverrides(&cfg) + require.Error(t, err) + require.Contains(t, err.Error(), "IRON_PROXY_UPSTREAM_DENY_CIDRS") + require.Contains(t, err.Error(), "must be CIDR notation") + }) + + t.Run("empty entry rejected", func(t *testing.T) { + setEnvs(t, map[string]string{ + "IRON_PROXY_UPSTREAM_DENY_CIDRS": "10.0.0.0/8,", + }) + + var cfg Config + err := applyEnvOverrides(&cfg) + require.Error(t, err) + require.Contains(t, err.Error(), "IRON_PROXY_UPSTREAM_DENY_CIDRS") + require.Contains(t, err.Error(), "empty CIDR entry") + }) +} + +func TestApplyEnvOverrides_ControlPlanePollInterval(t *testing.T) { + t.Run("env override applied", func(t *testing.T) { + setEnvs(t, map[string]string{ + "IRON_CONTROL_PLANE_POLL_INTERVAL": "15s", + }) + + var cfg Config + require.NoError(t, applyEnvOverrides(&cfg)) + require.Equal(t, 15*time.Second, time.Duration(cfg.ControlPlane.PollInterval)) + }) + + t.Run("env override beats yaml value", func(t *testing.T) { + setEnvs(t, map[string]string{ + "IRON_CONTROL_PLANE_POLL_INTERVAL": "20s", + }) + + cfg := Config{ControlPlane: ControlPlane{PollInterval: Duration(time.Minute)}} + require.NoError(t, applyEnvOverrides(&cfg)) + require.Equal(t, 20*time.Second, time.Duration(cfg.ControlPlane.PollInterval)) + }) + + t.Run("invalid duration rejected", func(t *testing.T) { + setEnvs(t, map[string]string{ + "IRON_CONTROL_PLANE_POLL_INTERVAL": "not-a-duration", + }) + + var cfg Config + err := applyEnvOverrides(&cfg) + require.Error(t, err) + require.Contains(t, err.Error(), "IRON_CONTROL_PLANE_POLL_INTERVAL") + }) +} + func TestApplyEnvOverrides_DNSEnabled(t *testing.T) { t.Run("false disables dns", func(t *testing.T) { setEnvs(t, map[string]string{"IRON_DNS_ENABLED": "false"}) @@ -331,6 +428,7 @@ func setEnvs(t *testing.T, envs map[string]string) { // Clear all IRON_* env vars to prevent cross-test leakage. ironKeys := []string{ + "IRON_SSH_CONFIG_JSON", "IRON_DNS_ENABLED", "IRON_DNS_LISTEN", "IRON_DNS_PROXY_IP", @@ -341,6 +439,7 @@ func setEnvs(t *testing.T, envs map[string]string) { "IRON_PROXY_MAX_REQUEST_BODY_BYTES", "IRON_PROXY_MAX_RESPONSE_BODY_BYTES", "IRON_PROXY_UPSTREAM_RESPONSE_HEADER_TIMEOUT", + "IRON_PROXY_UPSTREAM_DENY_CIDRS", "IRON_TLS_CA_CERT", "IRON_TLS_CA_KEY", "IRON_TLS_CERT_CACHE_SIZE", @@ -356,3 +455,17 @@ func setEnvs(t *testing.T, envs map[string]string) { t.Setenv(k, v) } } + +func TestSSHEnvironmentConfig(t *testing.T) { + setEnvs(t, map[string]string{"IRON_SSH_CONFIG_JSON": `{"listen":"127.0.0.1:2222"}`}) + cfg, err := LoadConfig("") + require.NoError(t, err) + var ssh struct { + Listen string `yaml:"listen"` + } + require.NoError(t, cfg.SSH.Decode(&ssh)) + require.Equal(t, "127.0.0.1:2222", ssh.Listen) + t.Setenv("IRON_SSH_CONFIG_JSON", "sensitive-value: [") + _, err = LoadConfig("") + require.EqualError(t, err, "IRON_SSH_CONFIG_JSON is invalid") +} diff --git a/internal/config/sync.go b/internal/config/sync.go index 13640c3..f765c0f 100644 --- a/internal/config/sync.go +++ b/internal/config/sync.go @@ -17,8 +17,8 @@ import ( // for the allowlist transform, secrets → {"secrets": [...]} for the secrets // transform. The transforms field is the control plane's already-shaped // transform array — each element a {name, config} object bundling the -// gcp_auth, hmac_sign, and oauth_token transforms granted to the proxy's -// principal. +// gcp_auth, gcp_id_token, hmac_sign, and oauth_token transforms granted to the +// proxy's principal. // // Pipeline order is allowlist, then secrets, then the control-plane transforms // in delivered order. Secrets runs before the transforms so a body-mutating diff --git a/internal/config/sync_test.go b/internal/config/sync_test.go index 127ba07..3c778bd 100644 --- a/internal/config/sync_test.go +++ b/internal/config/sync_test.go @@ -276,6 +276,43 @@ func TestTransformsFromSync_OAuthTokenTransform(t *testing.T) { require.Equal(t, "slack.com", decoded.Tokens[0].Rules[0].Host) } +func TestTransformsFromSync_GCPIDTokenTransform(t *testing.T) { + transformsRaw := json.RawMessage(`[ + { + "name": "gcp_id_token", + "config": { + "keyfile": {"type": "control_plane", "value": "{\"type\":\"service_account\"}"}, + "audience": "https://private-service-abc123-uc.a.run.app", + "header": "x-serverless-authorization", + "rules": [{"host": "private-service-abc123-uc.a.run.app"}] + } + } + ]`) + + transforms, err := TransformsFromSync(nil, nil, transformsRaw) + require.NoError(t, err) + require.Len(t, transforms, 1) + require.Equal(t, "gcp_id_token", transforms[0].Name) + + var decoded struct { + Keyfile struct { + Type string `yaml:"type"` + Value string `yaml:"value"` + } `yaml:"keyfile"` + Audience string `yaml:"audience"` + Header string `yaml:"header"` + Rules []struct { + Host string `yaml:"host"` + } `yaml:"rules"` + } + require.NoError(t, transforms[0].Config.Decode(&decoded)) + require.Equal(t, "control_plane", decoded.Keyfile.Type) + require.JSONEq(t, `{"type":"service_account"}`, decoded.Keyfile.Value) + require.Equal(t, "https://private-service-abc123-uc.a.run.app", decoded.Audience) + require.Equal(t, "x-serverless-authorization", decoded.Header) + require.Equal(t, "private-service-abc123-uc.a.run.app", decoded.Rules[0].Host) +} + func TestTransformsFromSync_TransformsAfterSecrets(t *testing.T) { // allowlist, then secrets, then the control-plane transforms in delivered // order — so a body-mutating secret swap runs before hmac_sign signs. diff --git a/internal/controlplane/client.go b/internal/controlplane/client.go index 9f1a17f..2cb6e18 100644 --- a/internal/controlplane/client.go +++ b/internal/controlplane/client.go @@ -16,9 +16,9 @@ type SyncResponse struct { Rules json.RawMessage `json:"rules"` Secrets json.RawMessage `json:"secrets"` // Transforms is the control plane's pre-shaped transform array, bundling - // the gcp_auth, hmac_sign, and oauth_token transforms granted to the - // proxy's principal. Unlike rules/secrets it is already in {name, config} - // form. + // the gcp_auth, gcp_id_token, hmac_sign, and oauth_token transforms granted + // to the proxy's principal. Unlike rules/secrets it is already in {name, + // config} form. Transforms json.RawMessage `json:"transforms"` MCP json.RawMessage `json:"mcp"` Postgres json.RawMessage `json:"postgres"` diff --git a/internal/controlplane/poller.go b/internal/controlplane/poller.go index dc0a476..af3e284 100644 --- a/internal/controlplane/poller.go +++ b/internal/controlplane/poller.go @@ -10,8 +10,8 @@ import ( "time" ) -// PollInterval is the base interval between sync calls. -const PollInterval = 5 * time.Second +// DefaultPollInterval is the default base interval between sync calls. +const DefaultPollInterval = 10 * time.Second // SyncUpdate is the slice of a SyncResponse passed to the poller's update // callback. Fields are nil or JSON null when the control plane did not include @@ -42,6 +42,7 @@ type Poller struct { configHash string onUpdate func(SyncUpdate) error logger *slog.Logger + interval time.Duration mu sync.RWMutex status Status @@ -50,11 +51,20 @@ type Poller struct { // NewPoller creates a new sync poller. func NewPoller(client *Client, initialConfigHash string, onUpdate func(SyncUpdate) error, logger *slog.Logger) *Poller { + return NewPollerWithInterval(client, initialConfigHash, onUpdate, logger, DefaultPollInterval) +} + +// NewPollerWithInterval creates a new sync poller with a custom base interval. +func NewPollerWithInterval(client *Client, initialConfigHash string, onUpdate func(SyncUpdate) error, logger *slog.Logger, interval time.Duration) *Poller { + if interval <= 0 { + interval = DefaultPollInterval + } return &Poller{ client: client, configHash: initialConfigHash, onUpdate: onUpdate, logger: logger, + interval: interval, poke: make(chan struct{}, 1), } } @@ -103,7 +113,7 @@ func (p *Poller) recordSync(resp *SyncResponse) { } // Run starts the polling loop. It performs an initial sync immediately, then -// polls on PollInterval with ±10% jitter; a Poke wakes it early. Returns when +// polls on the configured interval with ±10% jitter; a Poke wakes it early. Returns when // ctx is canceled or a revocation error is received. func (p *Poller) Run(ctx context.Context) error { if err := p.sync(ctx); err != nil { @@ -114,7 +124,7 @@ func (p *Poller) Run(ctx context.Context) error { } for { - delay := jitteredInterval(PollInterval, 0.1) + delay := jitteredInterval(p.interval, 0.1) timer := time.NewTimer(delay) select { diff --git a/internal/controlplane/poller_test.go b/internal/controlplane/poller_test.go index ca982c3..c7c6410 100644 --- a/internal/controlplane/poller_test.go +++ b/internal/controlplane/poller_test.go @@ -169,7 +169,7 @@ func TestPollerContinuesOnTransientError(t *testing.T) { poller := NewPoller(client, "", nil, slog.New(slog.NewTextHandler(io.Discard, nil))) - // Run briefly -- the 30s interval means we won't get past the initial sync in 200ms, + // Run briefly. The 10s interval means we won't get past the initial sync in 200ms, // but the initial sync error should be handled gracefully. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) defer cancel() @@ -241,7 +241,7 @@ func TestPollerPokeTriggersImmediateSync(t *testing.T) { }, 2*time.Second, 10*time.Millisecond) require.Equal(t, "sha256:one", poller.Status().ConfigHash) - // A poke must trigger the second sync long before the 5s poll interval. + // A poke must trigger the second sync long before the 10s poll interval. poller.Poke() require.Eventually(t, func() bool { return poller.Status().ConfigHash == "sha256:two" @@ -257,6 +257,35 @@ func TestPollerPokeTriggersImmediateSync(t *testing.T) { require.NoError(t, <-done) } +func TestPollerCustomIntervalTriggersSync(t *testing.T) { + var syncCalls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + syncCalls.Add(1) + w.WriteHeader(http.StatusOK) + _ = json.NewEncoder(w).Encode(SyncResponse{ConfigHash: "sha256:ok"}) + })) + defer server.Close() + + client := NewClient(server.URL, "irpt_test", testLogger()) + poller := NewPollerWithInterval(client, "", nil, testLogger(), 20*time.Millisecond) + + ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond) + defer cancel() + + err := poller.Run(ctx) + require.NoError(t, err) + require.GreaterOrEqual(t, syncCalls.Load(), int32(2)) +} + +func TestJitteredIntervalRange(t *testing.T) { + base := time.Second + for range 100 { + got := jitteredInterval(base, 0.1) + require.GreaterOrEqual(t, got, 900*time.Millisecond) + require.LessOrEqual(t, got, 1100*time.Millisecond) + } +} + func TestPollerStatusRetainsPrincipalOnHashMatch(t *testing.T) { var syncCalls atomic.Int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/internal/dnsguard/dnsguard.go b/internal/dnsguard/dnsguard.go index 0830c3f..bb3d9a6 100644 --- a/internal/dnsguard/dnsguard.go +++ b/internal/dnsguard/dnsguard.go @@ -13,15 +13,35 @@ import ( "syscall" ) +var metadataAddresses = []netip.Addr{ + netip.MustParseAddr("169.254.169.254"), + netip.MustParseAddr("fd00:ec2::254"), + netip.MustParseAddr("fd20:ce::254"), +} + // DefaultDenyCIDRs is the secure-default list applied when the operator has // not configured upstream_deny_cidrs. It blocks cloud instance metadata // endpoints and loopback. RFC1918 is intentionally excluded — many legitimate // iron-proxy deployments target private corporate networks. -var DefaultDenyCIDRs = []string{ - "169.254.169.254/32", - "fd00:ec2::254/128", - "127.0.0.0/8", - "::1/128", +var DefaultDenyCIDRs = defaultDenyCIDRs() + +func defaultDenyCIDRs() []string { + cidrs := make([]string, 0, len(metadataAddresses)+2) + for _, address := range metadataAddresses { + cidrs = append(cidrs, netip.PrefixFrom(address, address.BitLen()).String()) + } + return append(cidrs, "127.0.0.0/8", "::1/128") +} + +// ContainsMetadataAddress reports whether prefix contains a known cloud +// instance metadata address. +func ContainsMetadataAddress(prefix netip.Prefix) bool { + for _, address := range metadataAddresses { + if prefix.Contains(address) { + return true + } + } + return false } // DenyError reports a connection refused because the resolved address falls diff --git a/internal/dnsguard/dnsguard_test.go b/internal/dnsguard/dnsguard_test.go index 08182dc..05fa95c 100644 --- a/internal/dnsguard/dnsguard_test.go +++ b/internal/dnsguard/dnsguard_test.go @@ -50,6 +50,41 @@ func TestNew_AcceptsValid(t *testing.T) { require.NotNil(t, g) } +func TestDefaultDenyCIDRsBlockMetadataAddresses(t *testing.T) { + guard, err := New(DefaultDenyCIDRs) + require.NoError(t, err) + + for _, address := range []string{ + "169.254.169.254", + "fd00:ec2::254", + "fd20:ce::254", + } { + t.Run(address, func(t *testing.T) { + require.True(t, guard.IsDenied(netip.MustParseAddr(address))) + }) + } +} + +func TestContainsMetadataAddress(t *testing.T) { + tests := []struct { + name string + prefix string + contains bool + }{ + {name: "IPv4 metadata", prefix: "169.254.169.254/32", contains: true}, + {name: "AWS IPv6 metadata", prefix: "fd00::/8", contains: true}, + {name: "GCP IPv6 metadata", prefix: "fd20:ce::/64", contains: true}, + {name: "unrelated private IPv4", prefix: "10.0.0.0/8", contains: false}, + {name: "unrelated unique local IPv6", prefix: "fd12:3456::/48", contains: false}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + require.Equal(t, tc.contains, ContainsMetadataAddress(netip.MustParsePrefix(tc.prefix))) + }) + } +} + func TestNew_NilAndEmpty(t *testing.T) { g, err := New(nil) require.NoError(t, err) diff --git a/internal/hostmatch/hostmatch.go b/internal/hostmatch/hostmatch.go index 8013f5e..53da084 100644 --- a/internal/hostmatch/hostmatch.go +++ b/internal/hostmatch/hostmatch.go @@ -58,8 +58,10 @@ func (m *Matcher) Matches(host string) bool { // MatchGlob matches a domain against a glob pattern. // "*" matches any host. "*.example.com" matches any subdomain depth and -// "example.com" itself. +// "example.com" itself. Matching is case-insensitive per RFC 4343. func MatchGlob(pattern, name string) bool { + pattern = strings.ToLower(pattern) + name = strings.ToLower(name) if pattern == "*" { return true } @@ -72,10 +74,11 @@ func MatchGlob(pattern, name string) bool { } // StripPort removes the port from a host:port string. If there's no port, -// the host is returned unchanged. +// the host is returned. IPv6 brackets (e.g. [::1]) are stripped for clean +// IP parsing and domain matching. func StripPort(host string) string { if h, _, err := net.SplitHostPort(host); err == nil { - return h + return strings.Trim(h, "[]") } - return host + return strings.Trim(host, "[]") } diff --git a/internal/hostmatch/hostmatch_test.go b/internal/hostmatch/hostmatch_test.go index 21d780e..c524770 100644 --- a/internal/hostmatch/hostmatch_test.go +++ b/internal/hostmatch/hostmatch_test.go @@ -14,10 +14,14 @@ func TestMatchGlob(t *testing.T) { want bool }{ {"*.example.com", "foo.example.com", true}, + {"*.example.com", "FOO.EXAMPLE.COM", true}, + {"*.EXAMPLE.COM", "foo.example.com", true}, {"*.example.com", "bar.baz.example.com", true}, {"*.example.com", "example.com", true}, + {"*.example.com", "EXAMPLE.COM", true}, {"*.example.com", "notexample.com", false}, {"exact.example.com", "exact.example.com", true}, + {"exact.example.com", "EXACT.EXAMPLE.COM", true}, {"exact.example.com", "other.example.com", false}, {"*", "anything.example.com", true}, {"*", "1.2.3.4", true}, @@ -30,3 +34,23 @@ func TestMatchGlob(t *testing.T) { }) } } + +func TestStripPort(t *testing.T) { + tests := []struct { + host string + want string + }{ + {"example.com:8080", "example.com"}, + {"example.com", "example.com"}, + {"[::1]:8080", "::1"}, + {"[::1]", "::1"}, + {"127.0.0.1:9090", "127.0.0.1"}, + {"127.0.0.1", "127.0.0.1"}, + } + + for _, tt := range tests { + t.Run(tt.host, func(t *testing.T) { + require.Equal(t, tt.want, StripPort(tt.host)) + }) + } +} diff --git a/internal/mcp/audit.go b/internal/mcp/audit.go index 29f8370..6cbd87f 100644 --- a/internal/mcp/audit.go +++ b/internal/mcp/audit.go @@ -9,8 +9,9 @@ import ( // collects one Message entry per JSON-RPC message observed on either side of // the proxy, in the order they were processed. type Trace struct { - Server string `json:"server"` - Messages []Message `json:"messages,omitempty"` + Server string `json:"server"` + Messages []Message `json:"messages,omitempty"` + Gateway map[string]any `json:"gateway,omitempty"` mu sync.Mutex // toolsListIDs is the set of JSON-RPC request IDs whose method was @@ -87,6 +88,17 @@ func (t *Trace) Append(m Message) { t.mu.Unlock() } +// SetGateway records MCP gateway metadata on the trace. Values must be +// JSON-serializable and must not include real credential values. +func (t *Trace) SetGateway(gateway map[string]any) { + if t == nil { + return + } + t.mu.Lock() + t.Gateway = gateway + t.mu.Unlock() +} + // recordToolsListID notes that this trace saw a request with method // tools/list and the supplied id, so the response-side filter knows to // rewrite the matching response. Notification ids (absent or null) are @@ -160,6 +172,23 @@ func (t *Trace) MessagesSnapshot() []Message { return out } +// GatewaySnapshot returns a copy of gateway metadata safe for audit rendering. +func (t *Trace) GatewaySnapshot() map[string]any { + if t == nil { + return nil + } + t.mu.Lock() + defer t.mu.Unlock() + if len(t.Gateway) == 0 { + return nil + } + out := make(map[string]any, len(t.Gateway)) + for k, v := range t.Gateway { + out[k] = v + } + return out +} + // MCPServer implements transform.MCPAudit. func (t *Trace) MCPServer() string { if t == nil { @@ -204,3 +233,8 @@ func (t *Trace) MCPMessages() []map[string]any { } return out } + +// MCPGateway implements transform.MCPAudit. +func (t *Trace) MCPGateway() map[string]any { + return t.GatewaySnapshot() +} diff --git a/internal/mcp/intercept.go b/internal/mcp/intercept.go index 214ac65..1ef9929 100644 --- a/internal/mcp/intercept.go +++ b/internal/mcp/intercept.go @@ -47,15 +47,14 @@ func (p *Policy) EvaluateRequest(server *Server, req *http.Request, trace *Trace return nil, nil } - // We only inspect application/json POST requests. GET listeners and other - // shapes pass through; their responses are still wrapped if they arrive - // over text/event-stream so the listener stream is filtered. + // Streamable HTTP sends JSON-RPC messages via POST. Inspect every POST + // regardless of its declared media type so clients cannot bypass policy by + // omitting or falsifying Content-Type. GET listeners and other shapes pass + // through; their responses are still wrapped if they arrive over + // text/event-stream so the listener stream is filtered. if req.Method != http.MethodPost { return nil, nil } - if !mediaTypeIs(req.Header.Get("Content-Type"), mediaTypeJSON) { - return nil, nil - } bb, ok := req.Body.(*transform.BufferedBody) if !ok { diff --git a/internal/mcp/intercept_test.go b/internal/mcp/intercept_test.go index ad83197..4e607d7 100644 --- a/internal/mcp/intercept_test.go +++ b/internal/mcp/intercept_test.go @@ -280,33 +280,42 @@ func TestWrapResponseBodySSELeavesUnrelatedToolsArrayAlone(t *testing.T) { require.Contains(t, string(out), "another_unknown") } -// TestEvaluateRequestUppercaseContentType is a regression test for the bug -// where Content-Type comparison was case-sensitive. Per RFC 7231, media -// types are case-insensitive, so "Application/JSON" must trigger MCP -// inspection just like "application/json". -func TestEvaluateRequestUppercaseContentType(t *testing.T) { +func TestEvaluateRequestEnforcesPostRegardlessOfContentType(t *testing.T) { p := newTestPolicy(t) - body := `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"delete_repo"}}` - u, _ := url.Parse("https://mcp.github.com/mcp") - req := &http.Request{ - Method: http.MethodPost, - Host: "mcp.github.com", - URL: u, - Header: http.Header{"Content-Type": {"Application/JSON; charset=utf-8"}}, - ContentLength: int64(len(body)), - Body: transform.NewBufferedBody(io.NopCloser(strings.NewReader(body)), 0), + + cases := []struct { + name string + contentType string + }{ + {name: "json", contentType: "application/json"}, + {name: "json with casing and parameter", contentType: "Application/JSON; charset=utf-8"}, + {name: "plain text", contentType: "text/plain"}, + {name: "form", contentType: "application/x-www-form-urlencoded"}, + {name: "missing"}, + {name: "malformed", contentType: "application/json, text/plain"}, } - s := p.MatchServer(req) - require.NotNil(t, s) + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + req := newJSONRequest(t, body) + if tc.contentType == "" { + req.Header.Del("Content-Type") + } else { + req.Header.Set("Content-Type", tc.contentType) + } + s := p.MatchServer(req) + require.NotNil(t, s) - tr := &Trace{Server: s.Name} - resp, err := p.EvaluateRequest(s, req, tr) - require.NoError(t, err) - require.NotNil(t, resp, "uppercase Content-Type must still trigger MCP enforcement") - require.Len(t, tr.Messages, 1) - require.Equal(t, DecisionDeny, tr.Messages[0].Decision) + tr := &Trace{Server: s.Name} + resp, err := p.EvaluateRequest(s, req, tr) + require.NoError(t, err) + require.NotNil(t, resp) + require.Len(t, tr.Messages, 1) + require.Equal(t, DecisionDeny, tr.Messages[0].Decision) + require.Equal(t, ReasonToolNotAllowed, tr.Messages[0].Reason) + }) + } } func TestWrapResponseBodyOtherContentTypePassesThrough(t *testing.T) { diff --git a/internal/mcpgateway/gateway.go b/internal/mcpgateway/gateway.go new file mode 100644 index 0000000..28637de --- /dev/null +++ b/internal/mcpgateway/gateway.go @@ -0,0 +1,277 @@ +// Package mcpgateway implements host-based routing for MCP Streamable HTTP +// servers after MCP policy enforcement has allowed a request. +package mcpgateway + +import ( + "context" + "encoding/base64" + "fmt" + "net/http" + "net/url" + "strings" + "text/template" + + "gopkg.in/yaml.v3" + + "github.com/ironsh/iron-proxy/internal/hostmatch" + "github.com/ironsh/iron-proxy/internal/transform/secrets" +) + +// Config is the YAML shape of the top-level mcp_gateway block. +type Config struct { + Routes []RouteConfig `yaml:"routes"` +} + +// RouteConfig maps one client-facing MCP host/path to a concrete upstream. +type RouteConfig struct { + Name string `yaml:"name"` + Rules []hostmatch.RuleConfig `yaml:"rules"` + Upstream string `yaml:"upstream"` + Credentials []CredentialConfig `yaml:"credentials"` +} + +// CredentialConfig injects a secret into a matched upstream request. +type CredentialConfig struct { + Source yaml.Node `yaml:"source"` + Inject InjectConfig `yaml:"inject"` + Require *bool `yaml:"require,omitempty"` +} + +// InjectConfig chooses the outbound location and optional formatting for a +// gateway credential. +type InjectConfig struct { + Header string `yaml:"header,omitempty"` + QueryParam string `yaml:"query_param,omitempty"` + Formatter string `yaml:"formatter,omitempty"` +} + +// Gateway is the compiled, immutable form of Config. +type Gateway struct { + routes []*Route +} + +// Route is a compiled gateway route. +type Route struct { + Name string + rules []hostmatch.Rule + upstream url.URL + credentials []credential +} + +type credential struct { + source secrets.Source + inject InjectConfig + require bool + formatter *template.Template + sourceName string +} + +// AppliedRoute describes the gateway rewrite applied to a request. +type AppliedRoute struct { + Name string + Upstream string + RequestURL string + InjectedCredentialIDs []string +} + +// LoadFromNode decodes a raw yaml.Node into a Config and compiles it. An empty +// node returns nil so callers can treat an absent mcp_gateway block normally. +func LoadFromNode(node yaml.Node) (*Gateway, error) { + if node.Kind == 0 { + return nil, nil + } + var c Config + if err := node.Decode(&c); err != nil { + return nil, fmt.Errorf("decoding mcp_gateway config: %w", err) + } + return Compile(c) +} + +// Compile validates and compiles a Config into a Gateway. Returns nil when no +// routes are configured. +func Compile(c Config) (*Gateway, error) { + if len(c.Routes) == 0 { + return nil, nil + } + g := &Gateway{routes: make([]*Route, 0, len(c.Routes))} + seen := make(map[string]bool, len(c.Routes)) + for i, rc := range c.Routes { + if rc.Name == "" { + return nil, fmt.Errorf("mcp_gateway.routes[%d]: name is required", i) + } + if seen[rc.Name] { + return nil, fmt.Errorf("mcp_gateway.routes[%d]: duplicate route name %q", i, rc.Name) + } + seen[rc.Name] = true + if len(rc.Rules) == 0 { + return nil, fmt.Errorf("mcp_gateway.routes[%q]: at least one rule is required", rc.Name) + } + rules, err := hostmatch.CompileRules(rc.Rules, fmt.Sprintf("mcp_gateway.routes[%q]", rc.Name)) + if err != nil { + return nil, err + } + upstream, err := normalizeUpstream(rc.Name, rc.Upstream) + if err != nil { + return nil, err + } + creds, err := compileCredentials(rc.Name, rc.Credentials) + if err != nil { + return nil, err + } + g.routes = append(g.routes, &Route{ + Name: rc.Name, + rules: rules, + upstream: upstream, + credentials: creds, + }) + } + return g, nil +} + +func normalizeUpstream(routeName, raw string) (url.URL, error) { + if raw == "" { + return url.URL{}, fmt.Errorf("mcp_gateway.routes[%q].upstream is required", routeName) + } + u, err := url.Parse(raw) + if err != nil { + return url.URL{}, fmt.Errorf("mcp_gateway.routes[%q].upstream must be a valid URL: %w", routeName, err) + } + if u.Scheme != "http" && u.Scheme != "https" { + return url.URL{}, fmt.Errorf("mcp_gateway.routes[%q].upstream must use http or https", routeName) + } + if u.Host == "" { + return url.URL{}, fmt.Errorf("mcp_gateway.routes[%q].upstream must include a host", routeName) + } + if u.Fragment != "" { + return url.URL{}, fmt.Errorf("mcp_gateway.routes[%q].upstream must not include a fragment", routeName) + } + return *u, nil +} + +func compileCredentials(routeName string, configs []CredentialConfig) ([]credential, error) { + creds := make([]credential, 0, len(configs)) + for i, cc := range configs { + src, err := secrets.BuildSource(cc.Source, nil) + if err != nil { + return nil, fmt.Errorf("mcp_gateway.routes[%q].credentials[%d]: %w", routeName, i, err) + } + if err := validateInject(routeName, i, cc.Inject); err != nil { + return nil, err + } + require := true + if cc.Require != nil { + require = *cc.Require + } + cred := credential{ + source: src, + inject: cc.Inject, + require: require, + sourceName: src.Name(), + } + if cc.Inject.Formatter != "" { + tmpl, err := template.New(fmt.Sprintf("mcp_gateway.routes[%q].credentials[%d]", routeName, i)).Funcs(templateFuncs).Parse(cc.Inject.Formatter) + if err != nil { + return nil, fmt.Errorf("mcp_gateway.routes[%q].credentials[%d]: parsing formatter: %w", routeName, i, err) + } + cred.formatter = tmpl + } + creds = append(creds, cred) + } + return creds, nil +} + +var templateFuncs = template.FuncMap{ + "base64": func(parts ...string) string { + return base64.StdEncoding.EncodeToString([]byte(strings.Join(parts, ""))) + }, +} + +func validateInject(routeName string, i int, cfg InjectConfig) error { + hasHeader := cfg.Header != "" + hasQuery := cfg.QueryParam != "" + if !hasHeader && !hasQuery { + return fmt.Errorf("mcp_gateway.routes[%q].credentials[%d]: inject must specify either header or query_param", routeName, i) + } + if hasHeader && hasQuery { + return fmt.Errorf("mcp_gateway.routes[%q].credentials[%d]: inject cannot specify both header and query_param", routeName, i) + } + return nil +} + +// Match returns the first route whose rules match the request, or nil. +func (g *Gateway) Match(req *http.Request) *Route { + if g == nil { + return nil + } + host := hostmatch.StripPort(req.Host) + for _, route := range g.routes { + for _, rule := range route.rules { + if rule.Matches(host, req.Method, req.URL.Path) { + return route + } + } + } + return nil +} + +// Apply injects route credentials into req and returns the upstream target +// components the proxy should use when constructing the outbound request. +func (r *Route) Apply(ctx context.Context, req *http.Request) (*AppliedRoute, error) { + if r == nil { + return nil, nil + } + upstream := r.upstream + applied := &AppliedRoute{ + Name: r.Name, + } + for _, cred := range r.credentials { + value, err := cred.source.Get(ctx) + if err != nil { + if cred.require { + return nil, fmt.Errorf("gateway credential %q unavailable: %w", cred.sourceName, err) + } + continue + } + value, err = formatCredential(cred.formatter, value) + if err != nil { + return nil, fmt.Errorf("formatting gateway credential %q: %w", cred.sourceName, err) + } + if cred.inject.Header != "" { + req.Header.Set(cred.inject.Header, value) + applied.InjectedCredentialIDs = append(applied.InjectedCredentialIDs, cred.sourceName+":header:"+http.CanonicalHeaderKey(cred.inject.Header)) + continue + } + q := req.URL.Query() + q.Set(cred.inject.QueryParam, value) + req.URL.RawQuery = q.Encode() + applied.InjectedCredentialIDs = append(applied.InjectedCredentialIDs, cred.sourceName+":query:"+cred.inject.QueryParam) + } + applied.Upstream = upstream.String() + upstream.RawQuery = mergeRawQuery(r.upstream.RawQuery, req.URL.RawQuery) + applied.RequestURL = upstream.String() + return applied, nil +} + +func formatCredential(tmpl *template.Template, value string) (string, error) { + if tmpl == nil { + return value, nil + } + var b strings.Builder + err := tmpl.Execute(&b, struct { + Value string + }{Value: value}) + if err != nil { + return "", err + } + return b.String(), nil +} + +func mergeRawQuery(base, request string) string { + if base == "" { + return request + } + if request == "" { + return base + } + return base + "&" + request +} diff --git a/internal/mcpgateway/gateway_test.go b/internal/mcpgateway/gateway_test.go new file mode 100644 index 0000000..dcbf665 --- /dev/null +++ b/internal/mcpgateway/gateway_test.go @@ -0,0 +1,223 @@ +package mcpgateway + +import ( + "context" + "net/http" + "net/url" + "strings" + "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" + + "github.com/ironsh/iron-proxy/internal/hostmatch" +) + +func boolPtr(v bool) *bool { return &v } + +func TestCompileAndApply(t *testing.T) { + t.Setenv("MCP_TOKEN", "real-token") + + g, err := Compile(Config{ + Routes: []RouteConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local", Paths: []string{"/mcp", "/mcp/*"}}}, + Upstream: "https://mcp.github.com/v1", + Credentials: []CredentialConfig{{ + Source: mustYAMLNode(t, `type: env +var: MCP_TOKEN +`), + Inject: InjectConfig{ + Header: "Authorization", + Formatter: "Bearer {{ .Value }}", + }, + }}, + }}, + }) + require.NoError(t, err) + + req := newRequest(t, "https://github.mcp.local/mcp/tools?existing=1") + route := g.Match(req) + require.NotNil(t, route) + + applied, err := route.Apply(context.Background(), req) + require.NoError(t, err) + require.Equal(t, "github", applied.Name) + require.Equal(t, "https://mcp.github.com/v1", applied.Upstream) + require.Equal(t, "https://mcp.github.com/v1?existing=1", applied.RequestURL) + require.Equal(t, "Bearer real-token", req.Header.Get("Authorization")) + require.Equal(t, []string{"MCP_TOKEN:header:Authorization"}, applied.InjectedCredentialIDs) +} + +func TestApplyInjectsQueryParam(t *testing.T) { + t.Setenv("MCP_TOKEN", "real-token") + + g, err := Compile(Config{ + Routes: []RouteConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local"}}, + Upstream: "https://mcp.github.com", + Credentials: []CredentialConfig{{ + Source: mustYAMLNode(t, `type: env +var: MCP_TOKEN +`), + Inject: InjectConfig{QueryParam: "access_token"}, + }}, + }}, + }) + require.NoError(t, err) + + req := newRequest(t, "https://github.mcp.local/mcp?existing=1") + applied, err := g.Match(req).Apply(context.Background(), req) + require.NoError(t, err) + require.Equal(t, "real-token", req.URL.Query().Get("access_token")) + require.Equal(t, "https://mcp.github.com", applied.Upstream) + require.Equal(t, "https://mcp.github.com?access_token=real-token&existing=1", applied.RequestURL) + require.Equal(t, []string{"MCP_TOKEN:query:access_token"}, applied.InjectedCredentialIDs) +} + +func TestApplyUsesFixedUpstreamPath(t *testing.T) { + g, err := Compile(Config{ + Routes: []RouteConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local"}}, + Upstream: "https://mcp.github.com/v1/", + }}, + }) + require.NoError(t, err) + + req := newRequest(t, "https://github.mcp.local/mcp/") + applied, err := g.Match(req).Apply(context.Background(), req) + require.NoError(t, err) + require.Equal(t, "https://mcp.github.com/v1/", applied.Upstream) + require.Equal(t, "https://mcp.github.com/v1/", applied.RequestURL) +} + +func TestApplyRequiredCredentialFailure(t *testing.T) { + g, err := Compile(Config{ + Routes: []RouteConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local"}}, + Upstream: "https://mcp.github.com", + Credentials: []CredentialConfig{{ + Source: mustYAMLNode(t, `type: env +var: MISSING_MCP_TOKEN +`), + Inject: InjectConfig{Header: "Authorization"}, + }}, + }}, + }) + require.NoError(t, err) + + req := newRequest(t, "https://github.mcp.local/mcp") + _, err = g.Match(req).Apply(context.Background(), req) + require.Error(t, err) + require.Contains(t, err.Error(), `gateway credential "MISSING_MCP_TOKEN" unavailable`) +} + +func TestApplyOptionalCredentialFailure(t *testing.T) { + g, err := Compile(Config{ + Routes: []RouteConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local"}}, + Upstream: "https://mcp.github.com", + Credentials: []CredentialConfig{{ + Source: mustYAMLNode(t, `type: env +var: MISSING_MCP_TOKEN +`), + Inject: InjectConfig{Header: "Authorization"}, + Require: boolPtr(false), + }}, + }}, + }) + require.NoError(t, err) + + req := newRequest(t, "https://github.mcp.local/mcp") + applied, err := g.Match(req).Apply(context.Background(), req) + require.NoError(t, err) + require.Empty(t, req.Header.Get("Authorization")) + require.Empty(t, applied.InjectedCredentialIDs) +} + +func TestCompileValidation(t *testing.T) { + cases := []struct { + name string + cfg Config + wantErr string + }{ + { + name: "missing name", + cfg: Config{Routes: []RouteConfig{{Upstream: "https://example.com"}}}, + wantErr: "name is required", + }, + { + name: "missing rules", + cfg: Config{Routes: []RouteConfig{{Name: "github", Upstream: "https://example.com"}}}, + wantErr: "at least one rule is required", + }, + { + name: "invalid upstream scheme", + cfg: Config{Routes: []RouteConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local"}}, + Upstream: "ftp://example.com", + }}}, + wantErr: "upstream must use http or https", + }, + { + name: "missing upstream host", + cfg: Config{Routes: []RouteConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local"}}, + Upstream: "https:///v1", + }}}, + wantErr: "upstream must include a host", + }, + { + name: "invalid credential injection", + cfg: Config{Routes: []RouteConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local"}}, + Upstream: "https://example.com", + Credentials: []CredentialConfig{{ + Source: mustYAMLNode(t, `type: env +var: MCP_TOKEN +`), + Inject: InjectConfig{Header: "Authorization", QueryParam: "token"}, + }}, + }}}, + wantErr: "inject cannot specify both header and query_param", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + _, err := Compile(tc.cfg) + require.Error(t, err) + require.Contains(t, err.Error(), tc.wantErr) + }) + } +} + +func newRequest(t *testing.T, rawURL string) *http.Request { + t.Helper() + u, err := url.Parse(rawURL) + require.NoError(t, err) + return &http.Request{ + Method: http.MethodPost, + URL: u, + Host: u.Host, + Header: http.Header{}, + Body: http.NoBody, + } +} + +func mustYAMLNode(t *testing.T, src string) yaml.Node { + t.Helper() + var doc yaml.Node + err := yaml.NewDecoder(strings.NewReader(src)).Decode(&doc) + require.NoError(t, err) + require.Equal(t, yaml.DocumentNode, doc.Kind) + require.NotEmpty(t, doc.Content) + return *doc.Content[0] +} diff --git a/internal/mcpgateway/holder.go b/internal/mcpgateway/holder.go new file mode 100644 index 0000000..317bafb --- /dev/null +++ b/internal/mcpgateway/holder.go @@ -0,0 +1,28 @@ +package mcpgateway + +import "sync/atomic" + +// Holder holds an atomically swappable Gateway pointer. +type Holder struct { + g atomic.Pointer[Gateway] +} + +// NewHolder creates a Holder with the given initial gateway. +func NewHolder(initial *Gateway) *Holder { + h := &Holder{} + h.g.Store(initial) + return h +} + +// Load returns the current gateway snapshot, or nil if none is configured. +func (h *Holder) Load() *Gateway { + if h == nil { + return nil + } + return h.g.Load() +} + +// Store atomically replaces the current gateway with next. +func (h *Holder) Store(next *Gateway) { + h.g.Store(next) +} diff --git a/internal/postgres/config.go b/internal/postgres/config.go index 3323f71..e40edd3 100644 --- a/internal/postgres/config.go +++ b/internal/postgres/config.go @@ -17,12 +17,19 @@ // While the relay is running the proxy is mostly transparent. It rejects // client-issued role-changing statements (`SET ROLE`, `RESET ROLE`, // `SET SESSION AUTHORIZATION`, `RESET SESSION AUTHORIZATION`) and their -// function-call equivalents (`set_config('role', ...)`); any `SET`, `RESET`, or -// `set_config` of a setting the upstream pins; the reset-everything statements +// function-call equivalents; all calls to `set_config`; any `SET` or `RESET` +// of a setting the upstream pins; the reset-everything statements // `RESET ALL` and `DISCARD ALL` (which would clear the managed role and pinned -// settings); and DO blocks. Multi-statement Simple Queries are allowed as long -// as every statement passes; a batch is rejected if any statement is rejected. -// Extended Query, COPY, and prepared statements pass through unchanged. +// settings); UPDATE or INSERT on pg_settings and view or rule definitions over +// it; DO blocks; uninspectable function and procedure definitions; +// definitions that bind functions as implicit callbacks (CREATE AGGREGATE, +// CREATE OPERATOR, CREATE TYPE, CREATE CAST, and similar); and +// the legacy wire-level FunctionCall protocol. SQL and PL/pgSQL routine bodies +// are inspected recursively, while dynamic PL/pgSQL and other languages are +// rejected. +// Multi-statement Simple Queries are allowed as long as every statement passes; +// a batch is rejected if any statement is rejected. Extended Query, COPY, and +// prepared statements pass through unchanged. // // The proxy runs a single postgres listener fronting multiple upstream // databases: the top-level postgres: block is one object with a listen address diff --git a/internal/postgres/policy.go b/internal/postgres/policy.go index 414a5b0..e1ed529 100644 --- a/internal/postgres/policy.go +++ b/internal/postgres/policy.go @@ -14,10 +14,9 @@ const ( // The plpgsql body is opaque to our SQL-level AST walker; rejecting // avoids the risk of an embedded role change. RejectDoBlock - // RejectPinnedSetting — the client tried to SET / RESET / set_config a - // session variable the upstream pins. The proxy sets these at session start - // and forbids overrides so a setting used as a security boundary can't be - // changed. + // RejectPinnedSetting — the client tried to SET or RESET a session variable + // the upstream pins. The proxy sets these at session start and forbids + // overrides so a setting used as a security boundary can't be changed. RejectPinnedSetting // RejectResetAll — the client tried RESET ALL, which would reset the // proxy-managed role and every pinned setting. @@ -25,6 +24,24 @@ const ( // RejectDiscardAll — the client tried DISCARD ALL, which (like RESET ALL) // resets the proxy-managed role and every pinned setting. RejectDiscardAll + // RejectSetConfig — the client called set_config. Dynamic targets cannot be + // inspected safely, so all calls are rejected regardless of the GUC name. + RejectSetConfig + // RejectFunctionCall — the client used the legacy wire-level FunctionCall + // protocol, which invokes a function by OID without inspectable SQL text. + RejectFunctionCall + // RejectUninspectableRoutine — the client tried to define a function or + // procedure whose body cannot be inspected safely. + RejectUninspectableRoutine + // RejectSettingsCatalogWrite — the client tried to UPDATE or INSERT into + // pg_settings, or define a view or rule over it. Writes through pg_settings + // call set_config and would bypass pinned settings. + RejectSettingsCatalogWrite + // RejectCallbackDefinition — the client tried to define an aggregate, + // operator, type, cast, or other object that binds existing functions as + // implicitly invoked callbacks. These create executable aliases (e.g. an + // aggregate whose SFUNC is set_config) the classifier cannot see. + RejectCallbackDefinition ) // ClassifyClientStatement inspects sql and returns whether the relay should @@ -32,8 +49,10 @@ const ( // the lowercased set of setting names this upstream forbids the client from // mutating; pass nil when the upstream pins nothing. // -// Beyond the role policy, the proxy rejects any SET / RESET / set_config of a -// pinned setting, and any RESET ALL / DISCARD ALL (which would reset the +// Beyond the role policy, the proxy rejects every set_config call, writes to +// pg_settings (directly or via a client-defined view or rule), definitions that +// bind functions as implicit callbacks (CREATE AGGREGATE and friends), any SET or RESET +// of a pinned setting, and any RESET ALL / DISCARD ALL (which would reset the // managed role and every pinned setting at once). // // Multi-statement Simple Queries are allowed when every statement passes; @@ -47,11 +66,19 @@ func ClassifyClientStatement(sql string, pinned map[string]struct{}) (allowed bo switch op.Kind { case OpSetRole, OpSetSessionAuthorization, OpResetRole, OpResetSessionAuthorization: return false, RejectClientRoleChange + case OpSetConfig: + return false, RejectSetConfig case OpDoBlock: // DO blocks contain opaque plpgsql we can't introspect from the SQL // AST. Rather than risk an embedded role change slipping through, // we reject them outright. return false, RejectDoBlock + case OpUninspectableRoutine: + return false, RejectUninspectableRoutine + case OpSettingsCatalogWrite: + return false, RejectSettingsCatalogWrite + case OpCallbackDefinition: + return false, RejectCallbackDefinition } // RESET ALL / DISCARD ALL reset the proxy-managed role regardless of which // settings the upstream pins, so they are always rejected. diff --git a/internal/postgres/policy_test.go b/internal/postgres/policy_test.go index 555e966..c8c3cf2 100644 --- a/internal/postgres/policy_test.go +++ b/internal/postgres/policy_test.go @@ -9,7 +9,10 @@ import ( func TestClassifyClientStatement(t *testing.T) { // pinned models an upstream that pins one custom setting; nil means an // upstream that pins nothing beyond the always-on role policy. - pinned := map[string]struct{}{"centaur.slack_channel_id": {}} + pinned := map[string]struct{}{ + "centaur.slack_channel_id": {}, + "centaur.slack_user": {}, + } tests := []struct { name string @@ -54,7 +57,7 @@ func TestClassifyClientStatement(t *testing.T) { {name: "set pinned rejected", sql: "SET centaur.slack_channel_id = 'C999'", pinned: pinned, reason: RejectPinnedSetting}, {name: "set local pinned rejected", sql: "SET LOCAL centaur.slack_channel_id = 'C999'", pinned: pinned, reason: RejectPinnedSetting}, {name: "reset pinned rejected", sql: "RESET centaur.slack_channel_id", pinned: pinned, reason: RejectPinnedSetting}, - {name: "set_config pinned rejected", sql: "SELECT set_config('centaur.slack_channel_id', 'C999', false)", pinned: pinned, reason: RejectPinnedSetting}, + {name: "set_config pinned rejected", sql: "SELECT set_config('centaur.slack_channel_id', 'C999', false)", pinned: pinned, reason: RejectSetConfig}, {name: "set pinned buried in batch rejected", sql: "SELECT 1; SET centaur.slack_channel_id = 'C999'", pinned: pinned, reason: RejectPinnedSetting}, {name: "reset all rejected even with pin", sql: "RESET ALL", pinned: pinned, reason: RejectResetAll}, // Reading a pinned setting is fine; only writes are blocked. @@ -64,13 +67,72 @@ func TestClassifyClientStatement(t *testing.T) { // Without a pin, the same SET is allowed. {name: "set formerly-pinned allowed without pin", sql: "SET centaur.slack_channel_id = 'C999'", allowed: true}, + // Inspectable SQL-language routine bodies are checked recursively. Calls + // to routines already installed by the database remain the operator's + // responsibility; definitions in opaque languages are rejected. + { + name: "benign sql function allowed", + sql: "CREATE FUNCTION f() RETURNS integer LANGUAGE sql AS $$ SELECT 1 $$", + allowed: true, + }, + { + name: "sql function cannot change pinned guc", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql AS $$ SELECT set_config('centaur.slack_user', 'U999', false) $$", + pinned: pinned, + reason: RejectSetConfig, + }, + { + name: "sql function set clause cannot change pinned guc", + sql: "CREATE FUNCTION f() RETURNS integer LANGUAGE sql SET centaur.slack_user = 'U999' AS $$ SELECT 1 $$", + pinned: pinned, + reason: RejectPinnedSetting, + }, + { + name: "sql procedure cannot reset settings", + sql: "CREATE PROCEDURE p() LANGUAGE sql AS $$ RESET ALL $$", + reason: RejectResetAll, + }, + { + name: "benign plpgsql function allowed", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN 'ok'; END $$", + allowed: true, + }, + { + name: "plpgsql function cannot change pinned guc", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN set_config('centaur.slack_user', 'U999', false); END $$", + pinned: pinned, + reason: RejectSetConfig, + }, + { + name: "plpgsql procedure cannot set pinned guc", + sql: "CREATE PROCEDURE p() LANGUAGE plpgsql AS $$ BEGIN SET centaur.slack_user = 'U999'; END $$", + pinned: pinned, + reason: RejectPinnedSetting, + }, + { + name: "plpgsql dynamic sql rejected", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN EXECUTE 'SELECT 1'; END $$", + reason: RejectUninspectableRoutine, + }, + { + name: "sql function may call existing routine", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql AS $$ SELECT existing_wrapper() $$", + allowed: true, + }, + // Function-call bypass attempts — caught by AST walker. - {name: "set_config role rejected", sql: "SELECT set_config('role', 'admin', false)", reason: RejectClientRoleChange}, - {name: "pg_catalog set_config role rejected", sql: "SELECT pg_catalog.set_config('role', 'admin', false)", reason: RejectClientRoleChange}, - {name: "set_config session_authorization rejected", sql: "SELECT set_config('session_authorization', 'admin', false)", reason: RejectClientRoleChange}, - {name: "set_config role in cte rejected", sql: "WITH x AS (SELECT set_config('role', 'admin', false)) SELECT * FROM x", reason: RejectClientRoleChange}, + {name: "set_config role rejected", sql: "SELECT set_config('role', 'admin', false)", reason: RejectSetConfig}, + {name: "pg_catalog set_config role rejected", sql: "SELECT pg_catalog.set_config('role', 'admin', false)", reason: RejectSetConfig}, + {name: "set_config session_authorization rejected", sql: "SELECT set_config('session_authorization', 'admin', false)", reason: RejectSetConfig}, + {name: "set_config role in cte rejected", sql: "WITH x AS (SELECT set_config('role', 'admin', false)) SELECT * FROM x", reason: RejectSetConfig}, + {name: "set_config non-role setting rejected", sql: "SELECT set_config('search_path', 'public', false)", reason: RejectSetConfig}, + {name: "set_config parameter target rejected", sql: "SELECT set_config($1, $2, false)", reason: RejectSetConfig}, + {name: "set_config cast target rejected", sql: "SELECT set_config('role'::text, 'admin', false)", reason: RejectSetConfig}, {name: "current_setting role allowed", sql: "SELECT current_setting('role')", allowed: true}, - {name: "set_config search_path allowed", sql: "SELECT set_config('search_path', 'public', false)", allowed: true}, + {name: "update pg_settings rejected", sql: "UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", reason: RejectSettingsCatalogWrite}, + {name: "view over pg_settings rejected", sql: "CREATE TEMP VIEW v AS SELECT * FROM pg_settings", reason: RejectSettingsCatalogWrite}, + {name: "aggregate aliasing set_config rejected", sql: "CREATE AGGREGATE pg_temp.a(text, boolean) (SFUNC = pg_catalog.set_config, STYPE = text)", reason: RejectCallbackDefinition}, + {name: "select pg_settings allowed", sql: "SELECT name, setting FROM pg_settings", allowed: true}, // DO blocks rejected outright. {name: "do block rejected", sql: "DO $$ BEGIN END $$", reason: RejectDoBlock}, diff --git a/internal/postgres/relay_test.go b/internal/postgres/relay_test.go new file mode 100644 index 0000000..617c70e --- /dev/null +++ b/internal/postgres/relay_test.go @@ -0,0 +1,242 @@ +package postgres + +import ( + "encoding/binary" + "io" + "log/slog" + "net" + "testing" + "time" + + "github.com/jackc/pgx/v5/pgproto3" + "github.com/stretchr/testify/require" +) + +func TestRelayRejectsFunctionCall(t *testing.T) { + client, relayClient := net.Pipe() + relayUpstream, upstream := net.Pipe() + clientClosed := false + t.Cleanup(func() { + if !clientClosed { + require.NoError(t, client.Close()) + } + require.NoError(t, relayClient.Close()) + require.NoError(t, relayUpstream.Close()) + require.NoError(t, upstream.Close()) + }) + + backend := newClientBackend(relayClient) + frontend := pgproto3.NewFrontend(relayUpstream, relayUpstream) + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + r := newRelay(relayClient, relayUpstream, backend, frontend, &Upstream{}, logger) + r.txStatus = 'T' + + done := make(chan struct{}) + go func() { + defer close(done) + r.clientToServer() + }() + + clientProtocol := pgproto3.NewFrontend(client, client) + clientProtocol.Send(&pgproto3.FunctionCall{ + Function: 1, + ArgFormatCodes: []uint16{0}, + Arguments: [][]byte{[]byte("role"), []byte("other_role"), []byte("false")}, + ResultFormatCode: 0, + }) + require.NoError(t, clientProtocol.Flush()) + + msg, err := clientProtocol.Receive() + require.NoError(t, err) + pgErr, ok := msg.(*pgproto3.ErrorResponse) + require.True(t, ok) + require.Equal(t, "0A000", pgErr.Code) + require.Contains(t, pgErr.Message, "FunctionCall") + + msg, err = clientProtocol.Receive() + require.NoError(t, err) + ready, ok := msg.(*pgproto3.ReadyForQuery) + require.True(t, ok) + require.Equal(t, byte('T'), ready.TxStatus) + + require.NoError(t, upstream.SetReadDeadline(time.Now().Add(50*time.Millisecond))) + _, err = upstream.Read(make([]byte, 1)) + var netErr net.Error + require.ErrorAs(t, err, &netErr) + require.True(t, netErr.Timeout()) + require.NoError(t, upstream.SetReadDeadline(time.Time{})) + + clientProtocol.Send(&pgproto3.Query{String: "SELECT 1"}) + require.NoError(t, clientProtocol.Flush()) + upstreamProtocol := pgproto3.NewBackend(upstream, upstream) + upstreamMsg, err := upstreamProtocol.Receive() + require.NoError(t, err) + query, ok := upstreamMsg.(*pgproto3.Query) + require.True(t, ok) + require.Equal(t, "SELECT 1", query.String) + + require.NoError(t, client.Close()) + clientClosed = true + select { + case <-done: + case <-time.After(time.Second): + require.FailNow(t, "client-to-server relay did not stop") + } +} + +func TestRelayClosesConnectionForOversizedFrontendMessage(t *testing.T) { + client, relayClient := net.Pipe() + relayUpstream, upstream := net.Pipe() + clientClosed := false + upstreamClosed := false + t.Cleanup(func() { + if !clientClosed { + require.NoError(t, client.Close()) + } + if !upstreamClosed { + require.NoError(t, upstream.Close()) + } + }) + + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + r := newRelay( + relayClient, + relayUpstream, + newClientBackend(relayClient), + pgproto3.NewFrontend(relayUpstream, relayUpstream), + &Upstream{}, + logger, + ) + + done := make(chan struct{}) + go func() { + defer close(done) + r.run() + }() + + header := make([]byte, 5) + header[0] = 'Q' + // The protocol length includes its own four bytes, but not the message type. + binary.BigEndian.PutUint32(header[1:], uint32(maxFrontendMessageBodyLen+5)) + _, err := client.Write(header) + require.NoError(t, err) + + require.NoError(t, client.SetReadDeadline(time.Now().Add(time.Second))) + _, err = client.Read(make([]byte, 1)) + require.ErrorIs(t, err, io.EOF) + + select { + case <-done: + case <-time.After(time.Second): + require.FailNow(t, "relay did not stop after oversized frontend message") + } + + require.NoError(t, client.Close()) + clientClosed = true + require.NoError(t, upstream.Close()) + upstreamClosed = true +} + +func TestRelayPreservesUpstreamTransactionStateOnReject(t *testing.T) { + statuses := []struct { + name string + value byte + }{ + {name: "transaction", value: 'T'}, + {name: "failed transaction", value: 'E'}, + } + protocols := []struct { + name string + send func(*pgproto3.Frontend) error + sync bool + }{ + { + name: "simple query", + send: func(client *pgproto3.Frontend) error { + client.Send(&pgproto3.Query{String: "SET ROLE other_role"}) + return client.Flush() + }, + }, + { + name: "extended query", + send: func(client *pgproto3.Frontend) error { + client.Send(&pgproto3.Parse{Query: "SET ROLE other_role"}) + return client.Flush() + }, + sync: true, + }, + } + + for _, status := range statuses { + for _, protocol := range protocols { + t.Run(status.name+"/"+protocol.name, func(t *testing.T) { + client, relayClient := net.Pipe() + relayUpstream, upstream := net.Pipe() + clientClosed := false + upstreamClosed := false + t.Cleanup(func() { + if !clientClosed { + require.NoError(t, client.Close()) + } + if !upstreamClosed { + require.NoError(t, upstream.Close()) + } + }) + + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + r := newRelay( + relayClient, + relayUpstream, + newClientBackend(relayClient), + pgproto3.NewFrontend(relayUpstream, relayUpstream), + &Upstream{}, + logger, + ) + + done := make(chan struct{}) + go func() { + defer close(done) + r.run() + }() + + upstreamProtocol := pgproto3.NewBackend(upstream, upstream) + upstreamProtocol.Send(&pgproto3.ReadyForQuery{TxStatus: status.value}) + require.NoError(t, upstreamProtocol.Flush()) + + clientProtocol := pgproto3.NewFrontend(client, client) + msg, err := clientProtocol.Receive() + require.NoError(t, err) + ready, ok := msg.(*pgproto3.ReadyForQuery) + require.True(t, ok) + require.Equal(t, status.value, ready.TxStatus) + + require.NoError(t, protocol.send(clientProtocol)) + + msg, err = clientProtocol.Receive() + require.NoError(t, err) + _, ok = msg.(*pgproto3.ErrorResponse) + require.True(t, ok) + if protocol.sync { + clientProtocol.Send(&pgproto3.Sync{}) + require.NoError(t, clientProtocol.Flush()) + } + + msg, err = clientProtocol.Receive() + require.NoError(t, err) + ready, ok = msg.(*pgproto3.ReadyForQuery) + require.True(t, ok) + require.Equal(t, status.value, ready.TxStatus) + + require.NoError(t, client.Close()) + clientClosed = true + select { + case <-done: + case <-time.After(time.Second): + require.FailNow(t, "relay did not stop") + } + require.NoError(t, upstream.Close()) + upstreamClosed = true + }) + } + } +} diff --git a/internal/postgres/session.go b/internal/postgres/session.go index 0f87a88..151292c 100644 --- a/internal/postgres/session.go +++ b/internal/postgres/session.go @@ -14,6 +14,11 @@ import ( "github.com/jackc/pgx/v5/pgproto3" ) +// maxFrontendMessageBodyLen caps every client message after the startup packet. +// pgproto3 otherwise accepts bodies up to the protocol's multi-gigabyte length +// field and allocates the declared size before decoding the message. +const maxFrontendMessageBodyLen = 16 << 20 + // runSession owns a single client connection from accept through close. It // performs the proxy-side handshake under a deadline, selects a route from the // startup database name, opens an upstream connection through pgconn, issues @@ -24,7 +29,7 @@ func runSession(ctx context.Context, clientConn net.Conn, listener *Listener, lo _ = clientConn.SetDeadline(time.Now().Add(handshakeTimeout)) - backend := pgproto3.NewBackend(clientConn, clientConn) + backend := newClientBackend(clientConn) startup, err := receiveStartup(clientConn, backend) if err != nil { @@ -111,6 +116,12 @@ func runSession(ctx context.Context, clientConn net.Conn, listener *Listener, lo relay.run() } +func newClientBackend(conn net.Conn) *pgproto3.Backend { + backend := pgproto3.NewBackend(conn, conn) + backend.SetMaxBodyLen(maxFrontendMessageBodyLen) + return backend +} + // receiveStartup handles the optional SSLRequest / GSSEncRequest preludes and // returns the StartupMessage that follows. Returns (nil, nil) when the client // sent a CancelRequest (handled by closing the connection) since the proxy @@ -267,9 +278,8 @@ func completeClientHandshake(backend *pgproto3.Backend, hj *pgconn.HijackedConn) // relay drives the bidirectional message pump between client and upstream // after the role has been set on the upstream session. // -// Two goroutines do socket I/O: c2s reads client messages and rejects -// role-changing statements (and DO blocks) before forwarding everything else; -// s2c is a pure passthrough. +// Two goroutines do socket I/O: c2s reads client messages and applies the +// statement and protocol policy before forwarding; s2c is a pure passthrough. // // Writes to the client serialize through clientWriteMu since both goroutines // may write — c2s when synthesizing a reject, s2c when forwarding the normal @@ -285,6 +295,10 @@ type relay struct { logger *slog.Logger clientWriteMu sync.Mutex + // txStatus is the last ReadyForQuery status received from upstream. It is + // protected by clientWriteMu so synthetic ReadyForQuery messages preserve + // the upstream transaction state. + txStatus byte // skipExtended, when true, drops client Bind/Describe/Execute/Close // messages until a Sync is observed. Set when a Parse is rejected: per the @@ -304,6 +318,7 @@ func newRelay(clientConn, upstreamConn net.Conn, backend *pgproto3.Backend, fron frontend: frontend, upstream: upstream, logger: logger, + txStatus: 'I', } } @@ -327,8 +342,8 @@ func (r *relay) run() { <-done } -// clientToServer reads frontend messages from the client and forwards them -// upstream, rejecting only role-changing statements and DO blocks. +// clientToServer reads frontend messages from the client and forwards those +// permitted by the statement and protocol policy upstream. func (r *relay) clientToServer() { for { msg, err := r.backend.Receive() @@ -370,13 +385,23 @@ func (r *relay) clientToServer() { return } + case *pgproto3.FunctionCall: + if r.skipExtended { + continue + } + // FunctionCall invokes a function by OID without carrying SQL text, + // so the proxy cannot apply its statement policy. The protocol is a + // legacy fast path; reject it rather than forwarding an opaque call. + r.writeReject(RejectFunctionCall, true) + continue + case *pgproto3.Sync: if r.skipExtended { // We rejected an earlier Parse in this extended-query batch. // Synthesize a ReadyForQuery for the client and consume the // Sync without forwarding upstream. r.skipExtended = false - r.writeClient(&pgproto3.ReadyForQuery{TxStatus: 'I'}) + r.writeReadyForQuery() continue } r.frontend.Send(m) @@ -406,17 +431,29 @@ func (r *relay) writeReject(reason RejectReason, withReadyForQuery bool) { r.backend.Send(rejectError(reason)) if withReadyForQuery { - r.backend.Send(&pgproto3.ReadyForQuery{TxStatus: 'I'}) + r.backend.Send(&pgproto3.ReadyForQuery{TxStatus: r.txStatus}) } _ = r.backend.Flush() } +// writeReadyForQuery writes a synthetic ReadyForQuery carrying the most recent +// transaction status observed from upstream. +func (r *relay) writeReadyForQuery() { + r.clientWriteMu.Lock() + defer r.clientWriteMu.Unlock() + r.backend.Send(&pgproto3.ReadyForQuery{TxStatus: r.txStatus}) + _ = r.backend.Flush() +} + // writeClient writes one or more BackendMessages to the client under the // shared write mutex. func (r *relay) writeClient(msgs ...pgproto3.BackendMessage) { r.clientWriteMu.Lock() defer r.clientWriteMu.Unlock() for _, m := range msgs { + if ready, ok := m.(*pgproto3.ReadyForQuery); ok { + r.txStatus = ready.TxStatus + } r.backend.Send(m) } _ = r.backend.Flush() @@ -435,7 +472,7 @@ func rejectError(reason RejectReason) *pgproto3.ErrorResponse { return &pgproto3.ErrorResponse{ Severity: "ERROR", Code: "42501", - Message: "blocked by iron-proxy policy: this session setting is managed by the proxy; clients may not SET / RESET / set_config it", + Message: "blocked by iron-proxy policy: this session setting is managed by the proxy; clients may not SET or RESET it", } case RejectResetAll: return &pgproto3.ErrorResponse{ @@ -449,13 +486,43 @@ func rejectError(reason RejectReason) *pgproto3.ErrorResponse { Code: "42501", Message: "blocked by iron-proxy policy: DISCARD ALL would reset the proxy-managed role and session settings", } + case RejectSetConfig: + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "42501", + Message: "blocked by iron-proxy policy: set_config is not supported because session settings are managed by the proxy", + } + case RejectSettingsCatalogWrite: + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "42501", + Message: "blocked by iron-proxy policy: writing to pg_settings, or defining a view or rule over it, is not supported because session settings are managed by the proxy", + } + case RejectCallbackDefinition: + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "0A000", + Message: "blocked by iron-proxy policy: definitions that bind functions as callbacks (aggregates, operators, types, casts, and similar) are not supported", + } + case RejectFunctionCall: + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "0A000", + Message: "blocked by iron-proxy policy: the legacy FunctionCall protocol is not supported", + } + case RejectUninspectableRoutine: + return &pgproto3.ErrorResponse{ + Severity: "ERROR", + Code: "0A000", + Message: "blocked by iron-proxy policy: function and procedure definitions must use inspectable SQL or PL/pgSQL without dynamic SQL", + } case RejectClientRoleChange: fallthrough default: return &pgproto3.ErrorResponse{ Severity: "ERROR", Code: "42501", - Message: "blocked by iron-proxy policy: role is managed by the proxy; clients may not issue SET ROLE / SET SESSION AUTHORIZATION / RESET ROLE / set_config('role',...) / set_config('session_authorization',...)", + Message: "blocked by iron-proxy policy: role is managed by the proxy; clients may not issue SET ROLE / SET SESSION AUTHORIZATION / RESET ROLE", } } } diff --git a/internal/postgres/sql.go b/internal/postgres/sql.go index c6ebbd2..17a57bc 100644 --- a/internal/postgres/sql.go +++ b/internal/postgres/sql.go @@ -1,13 +1,14 @@ package postgres import ( + "encoding/json" "strings" // The wasilibs package is a pure-Go (wazero-based) drop-in for pg_query_go's // Parse function, sparing us from a CGO build dependency. AST node types // still come from the pganalyze package; wasilibs re-uses them. - pgparse "github.com/wasilibs/go-pgquery" pg_query "github.com/pganalyze/pg_query_go/v6" + pgparse "github.com/wasilibs/go-pgquery" "google.golang.org/protobuf/reflect/protoreflect" lru "github.com/hashicorp/golang-lru/v2" @@ -49,8 +50,7 @@ const ( // OpOther is any statement not matched by a more specific case. OpOther OpKind = iota // OpSetRole is `SET ROLE ` or anything anywhere in the AST that - // changes the `role` GUC — including SET LOCAL, SET SESSION, - // and function-call bypasses like `set_config('role', ...)`. + // changes the `role` GUC through SET, including SET LOCAL and SET SESSION. OpSetRole // OpSetSessionAuthorization is the equivalent for `session_authorization`. OpSetSessionAuthorization @@ -69,6 +69,28 @@ const ( // upstream so Postgres produces the canonical syntax error; the proxy // doesn't try to second-guess. OpParseError + // OpSetConfig is any call to set_config, regardless of schema qualification + // or whether its target setting is a string literal. Dynamic targets cannot + // be proven safe at Parse time, so the policy rejects the function wholesale. + OpSetConfig + // OpUninspectableRoutine is a CREATE FUNCTION / CREATE PROCEDURE statement + // whose body cannot be inspected. Dynamic PL/pgSQL and unsupported languages + // can hide role or GUC changes, so the policy rejects their definitions. + OpUninspectableRoutine + // OpSettingsCatalogWrite is an UPDATE or INSERT targeting pg_settings, or a + // view or rule definition referencing it. pg_settings' update rule calls + // set_config with the row's name, and an auto-updatable view over it + // forwards updates the same way, so these reach any listed setting — + // including pinned built-in ones — without a set_config call in the SQL. + OpSettingsCatalogWrite + // OpCallbackDefinition is a statement that binds existing functions by + // name into catalog metadata that PostgreSQL later invokes implicitly: + // CREATE AGGREGATE (SFUNC, FINALFUNC, ...), CREATE OPERATOR, CREATE TYPE, + // CREATE CAST WITH FUNCTION, operator class support functions, and + // similar. Such definitions create executable aliases (e.g. an aggregate + // whose SFUNC is pg_catalog.set_config) that the FuncCall-based + // classifier cannot see at call time, so the policy rejects them. + OpCallbackDefinition ) // GUC names whose mutation we treat as a role change for policy purposes. @@ -78,13 +100,6 @@ var roleGUCs = map[string]OpKind{ "session_authorization": OpSetSessionAuthorization, } -// dangerous set_config / current_setting target GUCs. We don't reject reads -// (current_setting), only writes (set_config). -var roleGUCsForSetConfig = map[string]OpKind{ - "role": OpSetRole, - "session_authorization": OpSetSessionAuthorization, -} - // classifyCache memoizes Classify results so repeated queries (the common // case in ORM workloads where the same parameterized SQL fires hundreds of // times per connection) don't re-parse on the hot path. Bounded so a @@ -135,7 +150,7 @@ func classifyUncached(sql string) Op { if root == nil { continue } - m := classifyStmt(root) + m := classifyStmt(root, rawStatementSQL(sql, s)) if op.Kind == OpOther { op.Kind = m.kind } @@ -157,9 +172,9 @@ type mutations struct { } // classifyStmt classifies a single statement's root node. -func classifyStmt(root *pg_query.Node) mutations { +func classifyStmt(root *pg_query.Node, statementSQL string) mutations { // Top-level shape gives us a fast path for the common DDL/DML cases. - switch root.Node.(type) { + switch n := root.Node.(type) { case *pg_query.Node_TransactionStmt: // BEGIN / COMMIT / ROLLBACK / SAVEPOINT etc. — no GUC mutation. return mutations{kind: OpOther} @@ -167,10 +182,209 @@ func classifyStmt(root *pg_query.Node) mutations { // The plpgsql body is opaque to the SQL AST walker; we can't see GUC // writes inside it, so the statement is rejected wholesale via Kind. return mutations{kind: OpDoBlock} + case *pg_query.Node_CreateFunctionStmt: + return classifyRoutineDefinition(root, n.CreateFunctionStmt, statementSQL) } return scanMutations(root) } +func rawStatementSQL(sql string, stmt *pg_query.RawStmt) string { + start := int(stmt.GetStmtLocation()) + if start < 0 || start > len(sql) { + return sql + } + end := len(sql) + if stmt.GetStmtLen() > 0 { + end = start + int(stmt.GetStmtLen()) + if end > len(sql) { + return sql + } + } + return sql[start:end] +} + +// classifyRoutineDefinition inspects CREATE FUNCTION and CREATE PROCEDURE +// bodies submitted by clients. SQL-standard bodies are already represented as +// AST nodes under SqlBody. String-form LANGUAGE sql bodies need a second parse. +// Other languages are rejected because their bodies are opaque to the SQL AST. +func classifyRoutineDefinition(root *pg_query.Node, stmt *pg_query.CreateFunctionStmt, statementSQL string) mutations { + m := scanMutations(root) + if m.kind != OpOther { + return m + } + + language, ok := routineStringOption(stmt.GetOptions(), "language") + if !ok { + m.kind = OpUninspectableRoutine + return m + } + if strings.EqualFold(language, "plpgsql") { + return mergeMutations(m, classifyPLpgSQL(statementSQL)) + } + if !strings.EqualFold(language, "sql") { + m.kind = OpUninspectableRoutine + return m + } + + if stmt.GetSqlBody() != nil { + return m + } + + body, ok := routineStringOption(stmt.GetOptions(), "as") + if !ok { + m.kind = OpUninspectableRoutine + return m + } + bodyOp := Classify(body) + if bodyOp.Kind == OpParseError || bodyOp.Kind == OpEmpty { + m.kind = OpUninspectableRoutine + return m + } + return mergeMutations(m, mutationsFromOp(bodyOp)) +} + +// classifyPLpgSQL parses a PL/pgSQL definition, recursively classifies every +// static SQL statement and expression, and rejects dynamic execution. Dynamic +// SQL cannot be resolved at Parse time, even when its current text is a literal. +func classifyPLpgSQL(statementSQL string) mutations { + parsed, err := pgparse.ParsePlPgSqlToJSON(statementSQL) + if err != nil { + return mutations{kind: OpUninspectableRoutine} + } + var tree any + if err := json.Unmarshal([]byte(parsed), &tree); err != nil { + return mutations{kind: OpUninspectableRoutine} + } + m := mutations{kind: OpOther} + if !scanPLpgSQL(tree, &m) { + m.kind = OpUninspectableRoutine + } + return m +} + +func scanPLpgSQL(value any, m *mutations) bool { + switch value := value.(type) { + case []any: + for _, item := range value { + if !scanPLpgSQL(item, m) { + return false + } + } + case map[string]any: + for key, item := range value { + switch key { + case "PLpgSQL_stmt_dynexecute", "PLpgSQL_stmt_dynfors", "dynquery": + return false + case "PLpgSQL_expr": + expr, ok := item.(map[string]any) + if !ok { + return false + } + op, ok := classifyPLpgSQLExpression(expr) + if !ok { + return false + } + *m = mergeMutations(*m, mutationsFromOp(op)) + default: + if !scanPLpgSQL(item, m) { + return false + } + } + } + } + return true +} + +func classifyPLpgSQLExpression(expr map[string]any) (Op, bool) { + query, ok := expr["query"].(string) + if !ok || strings.TrimSpace(query) == "" { + return Op{}, false + } + parseMode, ok := expr["parseMode"].(float64) + if !ok { + parseMode = 0 + } + + switch { + case parseMode == 0: + // A complete SQL statement such as SELECT, PERFORM's synthesized + // SELECT, CALL, or SET. + case parseMode == 2: + query = "SELECT " + query + case parseMode >= 3: + _, rhs, ok := strings.Cut(query, ":=") + if !ok || strings.TrimSpace(rhs) == "" { + return Op{}, false + } + query = "SELECT " + rhs + default: + // Type-name parse modes cannot execute SQL or mutate a setting. + return Op{Kind: OpOther}, true + } + + op := Classify(query) + if op.Kind == OpParseError || op.Kind == OpEmpty { + return Op{}, false + } + return op, true +} + +// routineStringOption returns a single string value from a CREATE FUNCTION / +// CREATE PROCEDURE option. LANGUAGE stores a String directly; AS stores its +// body in a one-item List. Native functions use two AS strings and are not +// considered inspectable. +func routineStringOption(options []*pg_query.Node, name string) (string, bool) { + for _, option := range options { + defNode, ok := option.Node.(*pg_query.Node_DefElem) + if !ok || !strings.EqualFold(defNode.DefElem.GetDefname(), name) { + continue + } + if value, ok := nodeString(defNode.DefElem.GetArg()); ok { + return value, true + } + arg := defNode.DefElem.GetArg() + if arg == nil { + return "", false + } + listNode, ok := arg.Node.(*pg_query.Node_List) + if !ok || len(listNode.List.GetItems()) != 1 { + return "", false + } + return nodeString(listNode.List.GetItems()[0]) + } + return "", false +} + +func nodeString(node *pg_query.Node) (string, bool) { + if node == nil { + return "", false + } + strNode, ok := node.Node.(*pg_query.Node_String_) + if !ok { + return "", false + } + return strNode.String_.GetSval(), true +} + +func mutationsFromOp(op Op) mutations { + return mutations{ + kind: op.Kind, + setGUCs: op.SetGUCs, + resetAll: op.ResetAll, + discard: op.Discard, + } +} + +func mergeMutations(dst, src mutations) mutations { + if dst.kind == OpOther { + dst.kind = src.kind + } + dst.setGUCs = append(dst.setGUCs, src.setGUCs...) + dst.resetAll = dst.resetAll || src.resetAll + dst.discard = dst.discard || src.discard + return dst +} + // scanMutations walks the AST rooted at node and reports every GUC-affecting // construct it finds: SET / RESET / RESET ALL, DISCARD ALL, and set_config(...) // calls, wherever they're nested (CTEs, subqueries, PREPARE bodies, function @@ -203,11 +417,39 @@ func scanMutations(node *pg_query.Node) mutations { } case *pg_query.VariableShowStmt: // SHOW is read-only; ignore. + case *pg_query.UpdateStmt: + if isPgSettings(n.GetRelation()) && m.kind == 0 { + m.kind = OpSettingsCatalogWrite + } + case *pg_query.InsertStmt: + if isPgSettings(n.GetRelation()) && m.kind == 0 { + m.kind = OpSettingsCatalogWrite + } + case *pg_query.ViewStmt, *pg_query.RuleStmt: + if referencesPgSettings(msg) && m.kind == 0 { + m.kind = OpSettingsCatalogWrite + } + case *pg_query.DefineStmt: + if definesCallbacks(n.GetKind()) && m.kind == 0 { + m.kind = OpCallbackDefinition + } + case *pg_query.CreateRangeStmt, *pg_query.CreateCastStmt, + *pg_query.CreateOpClassStmt, *pg_query.AlterOpFamilyStmt, + *pg_query.AlterOperatorStmt, *pg_query.AlterTypeStmt, + *pg_query.CreateConversionStmt, *pg_query.CreatePLangStmt, + *pg_query.CreateFdwStmt, *pg_query.AlterFdwStmt, + *pg_query.CreateAmStmt, *pg_query.CreateTransformStmt, + *pg_query.CreateEventTrigStmt: + if m.kind == 0 { + m.kind = OpCallbackDefinition + } case *pg_query.FuncCall: - if name, kind := setConfigTarget(n); name != "" { - m.setGUCs = append(m.setGUCs, name) - if kind != 0 && m.kind == 0 { - m.kind = kind + if name, ok := setConfigTarget(n); ok { + if name != "" { + m.setGUCs = append(m.setGUCs, name) + } + if m.kind == 0 { + m.kind = OpSetConfig } } } @@ -216,6 +458,40 @@ func scanMutations(node *pg_query.Node) mutations { return m } +// definesCallbacks reports whether a DefineStmt of the given kind can bind +// functions by name. Collations and text search dictionaries and +// configurations reference no functions directly; every other kind +// (aggregates, operators, types, text search parsers and templates) does. +func definesCallbacks(kind pg_query.ObjectType) bool { + switch kind { + case pg_query.ObjectType_OBJECT_COLLATION, + pg_query.ObjectType_OBJECT_TSDICTIONARY, + pg_query.ObjectType_OBJECT_TSCONFIGURATION: + return false + } + return true +} + +// isPgSettings reports whether rv names pg_settings. Any schema qualifier is +// matched: a user relation of that name is a harmless false positive, while +// schema-matching would have to track search_path. +func isPgSettings(rv *pg_query.RangeVar) bool { + return rv != nil && rv.GetRelname() == "pg_settings" +} + +// referencesPgSettings reports whether any relation reference in the tree +// rooted at m names pg_settings. +func referencesPgSettings(m protoreflect.Message) bool { + found := false + walkProto(m, func(msg protoreflect.Message) bool { + if rv, ok := msg.Interface().(*pg_query.RangeVar); ok && isPgSettings(rv) { + found = true + } + return !found + }) + return found +} + // roleKindFor returns the role-policy OpKind for a SET/RESET of the named GUC, // or 0 when the GUC is not role-affecting. kind distinguishes a write // (OpSetRole) from a reset (OpResetRole). @@ -233,46 +509,42 @@ func roleKindFor(name string, kind pg_query.VariableSetKind) OpKind { return base } -// setConfigTarget inspects fc and, when it is a call to set_config (or -// pg_catalog.set_config) whose first argument is a string-literal GUC name, -// returns that lowercased name and the role-policy OpKind for it (0 when the -// target is not role-affecting). Returns ("", 0) when fc is not such a call. -func setConfigTarget(fc *pg_query.FuncCall) (name string, kind OpKind) { +// setConfigTarget inspects fc and reports whether it is a call to set_config, +// including schema-qualified forms. When the first argument is a string +// literal, name is its lowercased value; dynamic targets return an empty name +// with ok=true so the policy can still reject the call. +func setConfigTarget(fc *pg_query.FuncCall) (name string, ok bool) { names := fc.GetFuncname() if len(names) == 0 { - return "", 0 + return "", false } // Funcname is a list of String nodes: ["set_config"] for unqualified // or ["pg_catalog", "set_config"] for schema-qualified. We accept any // schema qualifier and trust that user-defined set_config functions // are also suspicious — better a rare false positive than a bypass. last := names[len(names)-1] - lastStr, ok := last.Node.(*pg_query.Node_String_) - if !ok { - return "", 0 + lastStr, isString := last.Node.(*pg_query.Node_String_) + if !isString { + return "", false } if !strings.EqualFold(lastStr.String_.GetSval(), "set_config") { - return "", 0 + return "", false } args := fc.GetArgs() if len(args) < 1 { - return "", 0 + return "", true } - param, ok := stringConst(args[0]) - if !ok { - return "", 0 + param, isConst := stringConst(args[0]) + if !isConst { + return "", true } - lower := strings.ToLower(param) - // roleGUCsForSetConfig returns the zero OpKind (0) for non-role names, which - // is exactly the "not role-affecting" signal callers expect. - return lower, roleGUCsForSetConfig[lower] + return strings.ToLower(param), true } // stringConst returns the string value of node if it's an A_Const with a // string value, else ("", false). set_config's first argument is the GUC -// name; we only flag calls where it's a literal we can read at parse time. -// `set_config(my_var, ...)` where my_var is a variable evaluates at run -// time and is invisible to us — accepted limitation. +// name. Literal targets are retained for mutation metadata; non-literal +// targets are still classified as OpSetConfig and rejected by policy. func stringConst(node *pg_query.Node) (string, bool) { if node == nil { return "", false diff --git a/internal/postgres/sql_test.go b/internal/postgres/sql_test.go index 4d28265..355e894 100644 --- a/internal/postgres/sql_test.go +++ b/internal/postgres/sql_test.go @@ -52,35 +52,162 @@ func TestClassify(t *testing.T) { {name: "multi select then select allowed", sql: "SELECT 1; SELECT 2", want: OpOther}, {name: "multi benign then set role", sql: "SELECT 1; SET ROLE tenant", want: OpSetRole}, {name: "multi benign then do block", sql: "SELECT 1; DO $$ BEGIN END $$", want: OpDoBlock}, - {name: "multi benign then set_config role", sql: "SELECT 1; SELECT set_config('role', 'admin', false)", want: OpSetRole}, + {name: "multi benign then set_config role", sql: "SELECT 1; SELECT set_config('role', 'admin', false)", want: OpSetConfig}, {name: "multi non-role sets allowed", sql: "SET search_path = public; SELECT 1", want: OpOther}, // Trailing semicolon alone is *not* multi (pg_query collapses it). {name: "trailing semicolon not multi", sql: "SET ROLE tenant;", want: OpSetRole}, // set_config function-call bypass attempts - {name: "set_config role top level", sql: "SELECT set_config('role', 'admin', false)", want: OpSetRole}, - {name: "set_config role local flag", sql: "SELECT set_config('role', 'admin', true)", want: OpSetRole}, - {name: "pg_catalog set_config role", sql: "SELECT pg_catalog.set_config('role', 'admin', false)", want: OpSetRole}, - {name: "set_config session_authorization", sql: "SELECT set_config('session_authorization', 'admin', false)", want: OpSetSessionAuthorization}, - {name: "set_config case insensitive name", sql: "SELECT set_config('ROLE', 'admin', false)", want: OpSetRole}, - {name: "set_config nested in select", sql: "SELECT 1, set_config('role', 'admin', false), 2", want: OpSetRole}, - {name: "set_config in subquery", sql: "SELECT * FROM (SELECT set_config('role', 'admin', false)) s", want: OpSetRole}, - {name: "set_config in cte", sql: "WITH x AS (SELECT set_config('role', 'admin', false)) SELECT * FROM x", want: OpSetRole}, - {name: "set_config in where clause", sql: "SELECT 1 WHERE set_config('role', 'admin', false) = 'admin'", want: OpSetRole}, - {name: "set_config inside insert", sql: "INSERT INTO t SELECT set_config('role', 'admin', false)", want: OpSetRole}, - - // set_config for safe parameters — allowed. - {name: "set_config search_path allowed", sql: "SELECT set_config('search_path', 'public', false)", want: OpOther}, + {name: "set_config role top level", sql: "SELECT set_config('role', 'admin', false)", want: OpSetConfig}, + {name: "set_config role local flag", sql: "SELECT set_config('role', 'admin', true)", want: OpSetConfig}, + {name: "pg_catalog set_config role", sql: "SELECT pg_catalog.set_config('role', 'admin', false)", want: OpSetConfig}, + {name: "set_config session_authorization", sql: "SELECT set_config('session_authorization', 'admin', false)", want: OpSetConfig}, + {name: "set_config case insensitive name", sql: "SELECT set_config('ROLE', 'admin', false)", want: OpSetConfig}, + {name: "set_config nested in select", sql: "SELECT 1, set_config('role', 'admin', false), 2", want: OpSetConfig}, + {name: "set_config in subquery", sql: "SELECT * FROM (SELECT set_config('role', 'admin', false)) s", want: OpSetConfig}, + {name: "set_config in cte", sql: "WITH x AS (SELECT set_config('role', 'admin', false)) SELECT * FROM x", want: OpSetConfig}, + {name: "set_config in where clause", sql: "SELECT 1 WHERE set_config('role', 'admin', false) = 'admin'", want: OpSetConfig}, + {name: "set_config inside insert", sql: "INSERT INTO t SELECT set_config('role', 'admin', false)", want: OpSetConfig}, + {name: "set_config non-role setting", sql: "SELECT set_config('search_path', 'public', false)", want: OpSetConfig}, + {name: "set_config parameter target", sql: "SELECT set_config($1, $2, false)", want: OpSetConfig}, + {name: "set_config cast target", sql: "SELECT set_config('role'::text, 'admin', false)", want: OpSetConfig}, + {name: "set_config computed target", sql: "SELECT set_config(concat('ro', 'le'), 'admin', false)", want: OpSetConfig}, {name: "current_setting role is read only", sql: "SELECT current_setting('role')", want: OpOther}, // PREPARE wrapping a SELECT that calls set_config — caught by the // walker through the nested SelectStmt. - {name: "prepare wrapping set_config caught", sql: "PREPARE p AS SELECT set_config('role', 'admin', false)", want: OpSetRole}, + {name: "prepare wrapping set_config caught", sql: "PREPARE p AS SELECT set_config('role', 'admin', false)", want: OpSetConfig}, + + // pg_settings writes reach set_config through its update rule; an + // auto-updatable view or a rule over it forwards writes the same way. + {name: "update pg_settings", sql: "UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", want: OpSettingsCatalogWrite}, + {name: "update qualified pg_settings", sql: "UPDATE pg_catalog.pg_settings SET setting = '0' WHERE name = 'statement_timeout'", want: OpSettingsCatalogWrite}, + {name: "update pg_settings in cte", sql: "WITH x AS (UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout') SELECT 1", want: OpSettingsCatalogWrite}, + {name: "prepare update pg_settings", sql: "PREPARE p AS UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", want: OpSettingsCatalogWrite}, + {name: "insert pg_settings", sql: "INSERT INTO pg_settings (name, setting) VALUES ('statement_timeout', '0')", want: OpSettingsCatalogWrite}, + {name: "view over pg_settings", sql: "CREATE TEMP VIEW v AS SELECT * FROM pg_settings", want: OpSettingsCatalogWrite}, + {name: "view over pg_settings subquery", sql: "CREATE VIEW v AS SELECT * FROM (SELECT name, setting FROM pg_catalog.pg_settings) s", want: OpSettingsCatalogWrite}, + {name: "rule updating pg_settings", sql: "CREATE RULE r AS ON INSERT TO t DO ALSO UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", want: OpSettingsCatalogWrite}, + {name: "multi benign then update pg_settings", sql: "SELECT 1; UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'", want: OpSettingsCatalogWrite}, + {name: "select pg_settings is other", sql: "SELECT setting FROM pg_settings WHERE name = 'statement_timeout'", want: OpOther}, + {name: "update reading pg_settings is other", sql: "UPDATE t SET x = (SELECT setting FROM pg_settings WHERE name = 'work_mem')", want: OpOther}, + {name: "view not over pg_settings is other", sql: "CREATE VIEW v AS SELECT * FROM t", want: OpOther}, + + // Definitions that bind existing functions as implicitly invoked + // callbacks create executable aliases the FuncCall check cannot see. + {name: "aggregate with set_config sfunc", sql: "CREATE AGGREGATE pg_temp.a(text, boolean) (SFUNC = pg_catalog.set_config, STYPE = text)", want: OpCallbackDefinition}, + {name: "aggregate with benign sfunc", sql: "CREATE AGGREGATE a(integer) (SFUNC = int4pl, STYPE = integer)", want: OpCallbackDefinition}, + {name: "operator definition", sql: "CREATE OPERATOR === (LEFTARG = text, RIGHTARG = text, FUNCTION = texteq)", want: OpCallbackDefinition}, + {name: "base type definition", sql: "CREATE TYPE t (INPUT = t_in, OUTPUT = t_out)", want: OpCallbackDefinition}, + {name: "range type definition", sql: "CREATE TYPE r AS RANGE (SUBTYPE = integer, SUBTYPE_DIFF = f)", want: OpCallbackDefinition}, + {name: "cast with function", sql: "CREATE CAST (text AS t) WITH FUNCTION f(text)", want: OpCallbackDefinition}, + {name: "operator class support function", sql: "CREATE OPERATOR CLASS c FOR TYPE t USING btree AS FUNCTION 1 f(t, t)", want: OpCallbackDefinition}, + {name: "alter operator family add function", sql: "ALTER OPERATOR FAMILY fam USING btree ADD FUNCTION 1 f(t, t)", want: OpCallbackDefinition}, + {name: "alter operator restrict", sql: "ALTER OPERATOR === (text, text) SET (RESTRICT = f)", want: OpCallbackDefinition}, + {name: "alter type set callbacks", sql: "ALTER TYPE t SET (SEND = f)", want: OpCallbackDefinition}, + {name: "conversion definition", sql: "CREATE CONVERSION c FOR 'UTF8' TO 'LATIN1' FROM f", want: OpCallbackDefinition}, + {name: "multi benign then aggregate", sql: "SELECT 1; CREATE AGGREGATE a(text, boolean) (SFUNC = set_config, STYPE = text)", want: OpCallbackDefinition}, + {name: "composite type is other", sql: "CREATE TYPE t AS (a integer, b text)", want: OpOther}, + {name: "enum type is other", sql: "CREATE TYPE e AS ENUM ('a', 'b')", want: OpOther}, + {name: "collation is other", sql: "CREATE COLLATION c (provider = icu, locale = 'und')", want: OpOther}, // DO blocks — rejected regardless of contents. {name: "do block empty", sql: "DO $$ BEGIN END $$", want: OpDoBlock}, {name: "do block with set role", sql: "DO $$ BEGIN EXECUTE 'SET ROLE admin'; END $$", want: OpDoBlock}, + // SQL function and procedure bodies are parsed recursively. Other + // languages remain opaque and are rejected fail-closed. + { + name: "benign sql function", + sql: "CREATE FUNCTION f() RETURNS integer LANGUAGE sql AS $$ SELECT 1 $$", + want: OpOther, + }, + { + name: "sql function calls set_config", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql AS $$ SELECT set_config('centaur.slack_user', 'U999', false) $$", + want: OpSetConfig, + }, + { + name: "sql function changes role", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql AS $$ SET ROLE none; SELECT current_role::text $$", + want: OpSetRole, + }, + { + name: "sql procedure resets all", + sql: "CREATE PROCEDURE p() LANGUAGE sql AS $$ RESET ALL $$", + want: OpOther, + }, + { + name: "sql standard body calls set_config", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql RETURN set_config('centaur.slack_user', 'U999', false)", + want: OpSetConfig, + }, + { + name: "sql function delegates to existing function", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE sql AS $$ SELECT existing_wrapper() $$", + want: OpOther, + }, + { + name: "benign plpgsql function", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN 'ok'; END $$", + want: OpOther, + }, + { + name: "plpgsql return calls set_config", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN set_config('centaur.slack_user', 'U999', false); END $$", + want: OpSetConfig, + }, + { + name: "plpgsql assignment calls set_config", + sql: "CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ DECLARE x text; BEGIN x := set_config('centaur.slack_user', 'U999', false); RETURN x; END $$", + want: OpSetConfig, + }, + { + name: "plpgsql perform calls set_config", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN PERFORM set_config('centaur.slack_user', 'U999', false); END $$", + want: OpSetConfig, + }, + { + name: "sql function updates pg_settings", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE sql AS $$ UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout' $$", + want: OpSettingsCatalogWrite, + }, + { + name: "plpgsql function updates pg_settings", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN UPDATE pg_settings SET setting = '0' WHERE name = 'statement_timeout'; END $$", + want: OpSettingsCatalogWrite, + }, + { + name: "plpgsql procedure sets pinned guc", + sql: "CREATE PROCEDURE p() LANGUAGE plpgsql AS $$ BEGIN SET centaur.slack_user = 'U999'; END $$", + want: OpOther, + }, + { + name: "plpgsql dynamic execute is uninspectable", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN EXECUTE 'SET centaur.slack_user = ''U999'''; END $$", + want: OpUninspectableRoutine, + }, + { + name: "plpgsql dynamic cursor is uninspectable", + sql: "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ DECLARE c refcursor; BEGIN OPEN c FOR EXECUTE 'SELECT 1'; END $$", + want: OpUninspectableRoutine, + }, + { + name: "plpgsql routine after another statement uses its own source", + sql: "SELECT 1; CREATE FUNCTION f() RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN set_config('centaur.slack_user', 'U999', false); END $$", + want: OpSetConfig, + }, + { + name: "native function is uninspectable", + sql: "CREATE FUNCTION f(integer) RETURNS integer AS 'module', 'symbol' LANGUAGE C STRICT", + want: OpUninspectableRoutine, + }, + { + name: "invalid sql body is uninspectable", + sql: "CREATE FUNCTION f() RETURNS integer LANGUAGE sql AS $$ this is not sql $$", + want: OpUninspectableRoutine, + }, + // Parse errors — forwarded. {name: "syntax error", sql: "INSERT FROM WHERE", want: OpParseError}, } @@ -114,6 +241,21 @@ func TestClassifyMutationFacts(t *testing.T) { {name: "reset all flagged", sql: "RESET ALL", resetAll: true}, {name: "discard all flagged", sql: "DISCARD ALL", discard: true}, {name: "discard plans not flagged", sql: "DISCARD PLANS"}, + { + name: "sql procedure body reset all", + sql: "CREATE PROCEDURE p() LANGUAGE sql AS $$ RESET ALL $$", + resetAll: true, + }, + { + name: "sql function set clause collected", + sql: "CREATE FUNCTION f() RETURNS integer LANGUAGE sql SET centaur.slack_user = 'U999' AS $$ SELECT 1 $$", + setGUCs: []string{"centaur.slack_user"}, + }, + { + name: "plpgsql procedure body set collected", + sql: "CREATE PROCEDURE p() LANGUAGE plpgsql AS $$ BEGIN SET centaur.slack_user = 'U999'; END $$", + setGUCs: []string{"centaur.slack_user"}, + }, {name: "select writes nothing", sql: "SELECT 1"}, {name: "current_setting writes nothing", sql: "SELECT current_setting('centaur.slack_channel_id')"}, } diff --git a/internal/proxy/integration_test.go b/internal/proxy/integration_test.go index 956521c..bd2bfba 100644 --- a/internal/proxy/integration_test.go +++ b/internal/proxy/integration_test.go @@ -5,6 +5,7 @@ import ( "context" "crypto/tls" "crypto/x509" + "encoding/json" "fmt" "io" "log/slog" @@ -12,16 +13,332 @@ import ( "net/http" "net/http/httptest" "os" + "strings" "testing" "time" "github.com/stretchr/testify/require" + "golang.org/x/net/http2" "github.com/ironsh/iron-proxy/internal/certcache" + "github.com/ironsh/iron-proxy/internal/responseretry" "github.com/ironsh/iron-proxy/internal/transform" "github.com/ironsh/iron-proxy/internal/transform/allowlist" ) +func newResponseRetryTestProxy( + t *testing.T, + authorizer *httptest.Server, + status int, + transforms []transform.Transformer, + maxRequestBodyBytes int64, +) *Proxy { + t.Helper() + handler, err := responseretry.New(responseretry.Options{ + AuthorizeEndpoint: authorizer.URL + "/authorize", + CompleteEndpoint: authorizer.URL + "/complete", + Token: "proxy-token", + SandboxID: "sandbox-1", + Statuses: []int{status}, + CompletionHeaders: []string{"Payment-Receipt"}, + Client: authorizer.Client(), + }) + require.NoError(t, err) + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + pipeline := transform.NewPipeline(transforms, transform.BodyLimits{ + MaxRequestBodyBytes: maxRequestBodyBytes, + MaxResponseBodyBytes: 1 << 20, + }, logger) + return New(Options{ + Pipeline: transform.NewPipelineHolder(pipeline), + Logger: logger, + ResponseRetryHandler: handler, + }) +} + +func TestIntegration_ResponseHandlerReplaysExactTransformedRequestOnce(t *testing.T) { + const originalBody = "original request body" + const transformedBody = "transformed request body" + const chargeTraceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" + calls := 0 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + gotBody, err := io.ReadAll(r.Body) + require.NoError(t, err) + require.Equal(t, transformedBody, string(gotBody)) + require.Equal(t, "transformed", r.Header.Get("X-Transformed")) + if r.Header.Get("X-Retry-Token") == "retry-token" { + require.Equal(t, chargeTraceparent, r.Header.Get("Traceparent")) + _, err := w.Write([]byte("paid")) + require.NoError(t, err) + return + } + w.Header().Set("X-Retry-Challenge", "challenge") + w.WriteHeader(http.StatusConflict) + })) + defer upstream.Close() + + completions := 0 + authorizer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, "Bearer proxy-token", r.Header.Get("Authorization")) + if r.URL.Path == "/complete" { + completions++ + var payload responseretry.CompletionRequest + require.NoError(t, json.NewDecoder(r.Body).Decode(&payload)) + require.Equal(t, chargeTraceparent, payload.Traceparent) + w.WriteHeader(http.StatusOK) + return + } + w.Header().Set("Content-Type", "application/json") + _, err := fmt.Fprintf(w, `{"retry":true,"attempt_id":"8ace71a1-4e12-47e5-9df4-f2f660db6a82","traceparent":%q,"headers":{"X-Retry-Token":"retry-token"}}`, chargeTraceparent) + require.NoError(t, err) + })) + defer authorizer.Close() + p := newResponseRetryTestProxy( + t, + authorizer, + http.StatusConflict, + []transform.Transformer{&replacerTransform{ + reqBody: []byte(transformedBody), + reqHeaders: http.Header{"X-Transformed": {"transformed"}}, + }}, + 1<<20, + ) + req := httptest.NewRequest(http.MethodPost, upstream.URL+"/paid", strings.NewReader(originalBody)) + recorder := httptest.NewRecorder() + + p.handleDirectHTTP(recorder, req) + + require.Equal(t, http.StatusOK, recorder.Code) + require.Equal(t, "paid", recorder.Body.String()) + require.Equal(t, 2, calls) + require.Equal(t, 1, completions) +} + +func TestIntegration_ResponseHandlerReturnsOriginalChallengeForOversizedRequest(t *testing.T) { + const body = "request is larger than the replay limit" + upstreamCalls := 0 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + upstreamCalls++ + gotBody, err := io.ReadAll(r.Body) + require.NoError(t, err) + require.Equal(t, body, string(gotBody)) + w.Header().Set("Www-Authenticate", "Payment challenge") + w.WriteHeader(http.StatusPaymentRequired) + _, _ = w.Write([]byte("original challenge")) + })) + defer upstream.Close() + + authorizeCalls := 0 + authorizer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + authorizeCalls++ + var payload responseretry.DecisionRequest + require.NoError(t, json.NewDecoder(r.Body).Decode(&payload)) + require.Equal(t, "http", payload.Scheme) + require.False(t, payload.Replayable) + _, _ = w.Write([]byte(`{"retry":false,"headers":{}}`)) + })) + defer authorizer.Close() + p := newResponseRetryTestProxy(t, authorizer, http.StatusPaymentRequired, nil, 8) + req := httptest.NewRequest(http.MethodPost, upstream.URL+"/paid", strings.NewReader(body)) + recorder := httptest.NewRecorder() + + p.handleDirectHTTP(recorder, req) + + require.Equal(t, http.StatusPaymentRequired, recorder.Code) + require.Equal(t, "original challenge", recorder.Body.String()) + require.Equal(t, 1, upstreamCalls) + require.Equal(t, 1, authorizeCalls) +} + +func TestIntegration_ResponseHandlerBypassesStreamingRequests(t *testing.T) { + cases := []struct { + name string + contentType string + unknownSize bool + }{ + {name: "gRPC", contentType: "application/grpc+proto"}, + {name: "unknown length", contentType: "application/octet-stream", unknownSize: true}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + const body = "streamed request body" + upstreamCalls := 0 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + upstreamCalls++ + gotBody, err := io.ReadAll(r.Body) + require.NoError(t, err) + require.Equal(t, body, string(gotBody)) + w.WriteHeader(http.StatusPaymentRequired) + _, err = io.WriteString(w, "original challenge") + require.NoError(t, err) + })) + defer upstream.Close() + + authorizeCalls := 0 + authorizer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + authorizeCalls++ + http.Error(w, "unexpected", http.StatusInternalServerError) + })) + defer authorizer.Close() + p := newResponseRetryTestProxy(t, authorizer, http.StatusPaymentRequired, nil, 1<<20) + var requestBody io.Reader = strings.NewReader(body) + if tc.unknownSize { + requestBody = struct{ io.Reader }{Reader: requestBody} + } + req := httptest.NewRequest(http.MethodPost, upstream.URL+"/paid", requestBody) + req.Header.Set("Content-Type", tc.contentType) + if tc.unknownSize { + require.Equal(t, int64(-1), req.ContentLength) + } + recorder := httptest.NewRecorder() + + p.handleDirectHTTP(recorder, req) + + require.Equal(t, http.StatusPaymentRequired, recorder.Code) + require.Equal(t, "original challenge", recorder.Body.String()) + require.Equal(t, 1, upstreamCalls) + require.Zero(t, authorizeCalls) + }) + } +} + +func TestIntegration_ResponseHandlerFailureReturnsOriginalChallenge(t *testing.T) { + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusPaymentRequired) + _, _ = w.Write([]byte("original challenge")) + })) + defer upstream.Close() + authorizer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + http.Error(w, "unavailable", http.StatusServiceUnavailable) + })) + defer authorizer.Close() + p := newResponseRetryTestProxy(t, authorizer, http.StatusPaymentRequired, nil, 1<<20) + req := httptest.NewRequest(http.MethodGet, upstream.URL+"/paid", nil) + recorder := httptest.NewRecorder() + + p.handleDirectHTTP(recorder, req) + + require.Equal(t, http.StatusPaymentRequired, recorder.Code) + require.Equal(t, "original challenge", recorder.Body.String()) +} + +func TestIntegration_ResponseHandlerNeverRetriesAReplay(t *testing.T) { + upstreamCalls := 0 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + upstreamCalls++ + w.WriteHeader(http.StatusPaymentRequired) + _, err := fmt.Fprintf(w, "challenge %d", upstreamCalls) + require.NoError(t, err) + })) + defer upstream.Close() + + authorizeCalls := 0 + completionCalls := 0 + authorizer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/authorize": + authorizeCalls++ + _, err := io.WriteString(w, `{"retry":true,"attempt_id":"8ace71a1-4e12-47e5-9df4-f2f660db6a82","headers":{"X-Retry-Token":"retry-token"}}`) + require.NoError(t, err) + case "/complete": + completionCalls++ + w.WriteHeader(http.StatusNoContent) + default: + http.NotFound(w, r) + } + })) + defer authorizer.Close() + p := newResponseRetryTestProxy(t, authorizer, http.StatusPaymentRequired, nil, 1<<20) + req := httptest.NewRequest(http.MethodGet, upstream.URL+"/paid", nil) + recorder := httptest.NewRecorder() + + p.handleDirectHTTP(recorder, req) + + require.Equal(t, http.StatusPaymentRequired, recorder.Code) + require.Equal(t, "challenge 2", recorder.Body.String()) + require.Equal(t, 2, upstreamCalls) + require.Equal(t, 1, authorizeCalls) + require.Equal(t, 1, completionCalls) +} + +func TestIntegration_ResponseHandlerReportsReplayTransportFailure(t *testing.T) { + upstreamCalls := 0 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + upstreamCalls++ + if upstreamCalls == 1 { + w.WriteHeader(http.StatusPaymentRequired) + return + } + conn, _, err := w.(http.Hijacker).Hijack() + require.NoError(t, err) + require.NoError(t, conn.Close()) + })) + defer upstream.Close() + + completionCalls := 0 + authorizer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/complete" { + completionCalls++ + var payload responseretry.CompletionRequest + require.NoError(t, json.NewDecoder(r.Body).Decode(&payload)) + require.Nil(t, payload.ReplayStatus) + require.Equal(t, "upstream_transport_error", payload.TransportError) + w.WriteHeader(http.StatusNoContent) + return + } + _, err := io.WriteString(w, `{"retry":true,"attempt_id":"8ace71a1-4e12-47e5-9df4-f2f660db6a82","headers":{"X-Retry-Token":"retry-token"}}`) + require.NoError(t, err) + })) + defer authorizer.Close() + p := newResponseRetryTestProxy(t, authorizer, http.StatusPaymentRequired, nil, 1<<20) + req := httptest.NewRequest(http.MethodGet, upstream.URL+"/paid", nil) + recorder := httptest.NewRecorder() + + p.handleDirectHTTP(recorder, req) + + require.Equal(t, http.StatusBadGateway, recorder.Code) + require.Equal(t, 2, upstreamCalls) + require.Equal(t, 1, completionCalls) +} + +func TestIntegration_ResponseHandlerCompletionFailureDoesNotHideReplay(t *testing.T) { + upstreamCalls := 0 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + upstreamCalls++ + if upstreamCalls == 1 { + w.WriteHeader(http.StatusPaymentRequired) + return + } + _, err := io.WriteString(w, "paid") + require.NoError(t, err) + })) + defer upstream.Close() + + completionCalls := 0 + authorizer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/complete" { + completionCalls++ + http.Error(w, "unavailable", http.StatusServiceUnavailable) + return + } + _, err := io.WriteString(w, `{"retry":true,"attempt_id":"8ace71a1-4e12-47e5-9df4-f2f660db6a82","headers":{"X-Retry-Token":"retry-token"}}`) + require.NoError(t, err) + })) + defer authorizer.Close() + p := newResponseRetryTestProxy(t, authorizer, http.StatusPaymentRequired, nil, 1<<20) + req := httptest.NewRequest(http.MethodGet, upstream.URL+"/paid", nil) + recorder := httptest.NewRecorder() + + p.handleDirectHTTP(recorder, req) + + require.Equal(t, http.StatusOK, recorder.Code) + require.Equal(t, "paid", recorder.Body.String()) + require.Equal(t, 2, upstreamCalls) + require.Equal(t, 2, completionCalls) +} + // integrationCA bundles the test CA certificate, cert cache, and trust pool. type integrationCA struct { certCache *certcache.Cache @@ -494,3 +811,179 @@ func TestIntegration_SOCKS5(t *testing.T) { }) } +// TestIntegration_CONNECT_HTTP2 verifies a gRPC-style HTTP/2 client can tunnel +// through the MITM proxy: ALPN negotiates h2 on both the client->proxy and +// proxy->upstream hops, and upstream trailers (where gRPC carries grpc-status) +// reach the client. This is the regression test for HTTP/2/gRPC MITM support. +func TestIntegration_CONNECT_HTTP2(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test in short mode") + } + + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) + + // HTTP/2 upstream that records its negotiated protocol and sends a trailer + // after the body, mirroring how gRPC delivers grpc-status. + upstream := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Trailer", "X-Test-Trailer") + w.Header().Set("X-Upstream-Proto", r.Proto) + w.WriteHeader(http.StatusOK) + _, _ = fmt.Fprint(w, "h2 integration response") + w.Header().Set("X-Test-Trailer", "trailer-value") + })) + upstream.EnableHTTP2 = true + upstream.StartTLS() + defer upstream.Close() + upstreamAddr := upstream.Listener.Addr().String() + + const allowedHost = "allowed.example.com" + p, tunnelAddr, caPool := startTunnelIntegrationProxy(t, []string{allowedHost}, logger) + + // Route all dials to the h2 upstream. ForceAttemptHTTP2 mirrors the + // production buildTransport change so the proxy negotiates h2 upstream. + p.transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + ForceAttemptHTTP2: true, + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, network, upstreamAddr) + }, + } + + // HTTP/2 client that CONNECT-tunnels through the proxy and trusts the MITM CA. + client := &http.Client{ + Transport: &http2.Transport{ + TLSClientConfig: &tls.Config{ + RootCAs: caPool, + ServerName: allowedHost, + NextProtos: []string{"h2"}, + }, + DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) { + conn, err := net.DialTimeout("tcp", tunnelAddr, 5*time.Second) + if err != nil { + return nil, err + } + if _, err := fmt.Fprintf(conn, "CONNECT %s:443 HTTP/1.1\r\nHost: %s:443\r\n\r\n", allowedHost, allowedHost); err != nil { + _ = conn.Close() + return nil, err + } + resp, err := http.ReadResponse(bufio.NewReader(conn), nil) + if err != nil { + _ = conn.Close() + return nil, err + } + if resp.StatusCode != http.StatusOK { + _ = conn.Close() + return nil, fmt.Errorf("CONNECT failed: %d", resp.StatusCode) + } + tlsConn := tls.Client(conn, cfg) + if err := tlsConn.HandshakeContext(ctx); err != nil { + _ = conn.Close() + return nil, err + } + return tlsConn, nil + }, + }, + } + defer client.CloseIdleConnections() + + resp, err := client.Get(fmt.Sprintf("https://%s/test", allowedHost)) + require.NoError(t, err) + defer func() { _ = resp.Body.Close() }() + + require.Equal(t, 2, resp.ProtoMajor, "client<->proxy hop must be HTTP/2") + require.Equal(t, http.StatusOK, resp.StatusCode) + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + require.Equal(t, "h2 integration response", string(body)) + require.Equal(t, "HTTP/2.0", resp.Header.Get("X-Upstream-Proto"), "proxy<->upstream hop must be HTTP/2") + // The trailer must survive proxying — this is where gRPC carries grpc-status. + require.Equal(t, "trailer-value", resp.Trailer.Get("X-Test-Trailer")) +} + +// startDirectHTTPSProxy starts a proxy serving its direct HTTPS (MITM) listener +// (not the CONNECT tunnel) and routes all upstream dials to upstreamAddr. +// Returns the proxy, the HTTPS listener address, and the CA pool to trust. +func startDirectHTTPSProxy(t *testing.T, allowedHosts []string, upstreamAddr string, logger *slog.Logger) (*Proxy, string, *x509.CertPool) { + t.Helper() + + ca := newIntegrationCA(t) + al, err := allowlist.New(allowedHosts, nil) + require.NoError(t, err) + pipeline := transform.NewPipeline([]transform.Transformer{al}, transform.BodyLimits{}, logger) + holder := transform.NewPipelineHolder(pipeline) + + p := New(Options{ + HTTPAddr: "127.0.0.1:0", + HTTPSAddr: "127.0.0.1:0", + CertCache: ca.certCache, + Pipeline: holder, + Logger: logger, + }) + + httpsLn, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + tlsLn := tls.NewListener(httpsLn, p.httpsServer.TLSConfig) + httpsAddr := httpsLn.Addr().String() + go func() { _ = p.httpsServer.Serve(tlsLn) }() + t.Cleanup(func() { _ = p.httpsServer.Close() }) + + p.transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + ForceAttemptHTTP2: true, + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, network, upstreamAddr) + }, + } + return p, httpsAddr, ca.caPool +} + +// trailerUpstream returns an HTTP/2 test server that sends a body followed by a +// trailer, mirroring how gRPC delivers grpc-status. +func trailerUpstream(t *testing.T) *httptest.Server { + t.Helper() + s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Trailer", "X-Test-Trailer") + w.WriteHeader(http.StatusOK) + _, _ = fmt.Fprint(w, "trailer body") + w.Header().Set("X-Test-Trailer", "trailer-value") + })) + s.EnableHTTP2 = true + s.StartTLS() + return s +} + +// TestIntegration_DirectHTTPS_HTTP2 verifies the direct HTTPS MITM listener +// (not the CONNECT tunnel) negotiates h2 and forwards response trailers. +func TestIntegration_DirectHTTPS_HTTP2(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test in short mode") + } + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) + + upstream := trailerUpstream(t) + defer upstream.Close() + + const fakeHost = "direct-h2.example.com" + _, httpsAddr, caPool := startDirectHTTPSProxy(t, []string{fakeHost}, upstream.Listener.Addr().String(), logger) + + client := &http.Client{ + Transport: &http2.Transport{ + TLSClientConfig: &tls.Config{RootCAs: caPool, ServerName: fakeHost, NextProtos: []string{"h2"}}, + DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) { + return (&tls.Dialer{Config: cfg}).DialContext(ctx, "tcp", httpsAddr) + }, + }, + } + defer client.CloseIdleConnections() + + resp, err := client.Get("https://" + fakeHost + "/test") + require.NoError(t, err) + defer func() { _ = resp.Body.Close() }() + + require.Equal(t, 2, resp.ProtoMajor, "direct HTTPS listener must serve HTTP/2") + require.Equal(t, http.StatusOK, resp.StatusCode) + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + require.Equal(t, "trailer body", string(body)) + require.Equal(t, "trailer-value", resp.Trailer.Get("X-Test-Trailer")) +} diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index 6bcec6c..463a748 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -5,6 +5,7 @@ package proxy import ( + "bytes" "context" "crypto/tls" "errors" @@ -16,14 +17,16 @@ import ( "net/url" "strconv" "strings" - "sync" "time" "github.com/ironsh/iron-proxy/internal/certcache" "github.com/ironsh/iron-proxy/internal/config" "github.com/ironsh/iron-proxy/internal/dnsguard" "github.com/ironsh/iron-proxy/internal/mcp" + "github.com/ironsh/iron-proxy/internal/mcpgateway" + "github.com/ironsh/iron-proxy/internal/responseretry" "github.com/ironsh/iron-proxy/internal/transform" + "golang.org/x/net/http2" ) // Proxy is the HTTP/HTTPS proxy server. When Mode is TLSModeMITM, the HTTPS @@ -31,21 +34,23 @@ import ( // listener peeks the SNI from the ClientHello and TCP-passthroughs to the // upstream without terminating TLS. type Proxy struct { - httpServer *http.Server - httpsServer *http.Server - httpsAddr string - tlsMode string - tlsListener net.Listener - tunnelAddr string - tunnelListener net.Listener - tunnelDone chan struct{} - certCache *certcache.Cache - pipeline *transform.PipelineHolder - transport *http.Transport - resolver *net.Resolver - guard *dnsguard.Guard - mcpPolicy *mcp.PolicyHolder - logger *slog.Logger + httpServer *http.Server + httpsServer *http.Server + httpsAddr string + tlsMode string + tlsListener net.Listener + tunnelAddr string + tunnelListener net.Listener + tunnelDone chan struct{} + certCache *certcache.Cache + pipeline *transform.PipelineHolder + transport *http.Transport + resolver *net.Resolver + guard *dnsguard.Guard + mcpPolicy *mcp.PolicyHolder + mcpGateway *mcpgateway.Holder + responseRetryHandler *responseretry.Handler + logger *slog.Logger // shutdownCtx is canceled by Shutdown to unblock in-flight TCP-passthrough // connections that would otherwise sit on blocking Reads. @@ -72,7 +77,11 @@ type Options struct { Resolver *net.Resolver Guard *dnsguard.Guard // nil is treated as an empty (no-op) guard MCPPolicy *mcp.PolicyHolder // optional MCP-aware policy interceptor; nil disables MCP handling - Logger *slog.Logger + MCPGateway *mcpgateway.Holder + // ResponseRetryHandler may add headers and replay the exact transformed + // request once after selected upstream response statuses. + ResponseRetryHandler *responseretry.Handler + Logger *slog.Logger // UpstreamResponseHeaderTimeout overrides the upstream HTTP transport's // ResponseHeaderTimeout. Zero falls back to // config.DefaultUpstreamResponseHeaderTimeout. @@ -101,20 +110,22 @@ func New(opts Options) *Proxy { guard, _ = dnsguard.New(nil) } p := &Proxy{ - ready: opts.Ready, - httpsAddr: opts.HTTPSAddr, - tlsMode: opts.TLSMode, - tunnelAddr: opts.TunnelAddr, - tunnelDone: make(chan struct{}), - certCache: opts.CertCache, - pipeline: opts.Pipeline, - transport: buildTransport(opts.Resolver, guard, opts.UpstreamResponseHeaderTimeout, opts.UpstreamProxy), - resolver: opts.Resolver, - guard: guard, - mcpPolicy: opts.MCPPolicy, - logger: opts.Logger, - shutdownCtx: shutdownCtx, - shutdownCancel: shutdownCancel, + ready: opts.Ready, + httpsAddr: opts.HTTPSAddr, + tlsMode: opts.TLSMode, + tunnelAddr: opts.TunnelAddr, + tunnelDone: make(chan struct{}), + certCache: opts.CertCache, + pipeline: opts.Pipeline, + transport: buildTransport(opts.Resolver, guard, opts.UpstreamResponseHeaderTimeout, opts.UpstreamProxy), + resolver: opts.Resolver, + guard: guard, + mcpPolicy: opts.MCPPolicy, + mcpGateway: opts.MCPGateway, + responseRetryHandler: opts.ResponseRetryHandler, + logger: opts.Logger, + shutdownCtx: shutdownCtx, + shutdownCancel: shutdownCancel, } p.httpServer = &http.Server{ @@ -129,6 +140,9 @@ func New(opts Options) *Proxy { GetCertificate: p.getCertificate, }, } + // Enable HTTP/2 on the HTTPS listener (adds h2 to the TLS ALPN and serves + // negotiated conns through the existing handler); zero config never errors. + _ = http2.ConfigureServer(p.httpsServer, &http2.Server{}) return p } @@ -421,18 +435,49 @@ func (p *Proxy) handleHTTP(w http.ResponseWriter, r *http.Request, tunnelInfo *t return } + upstreamScheme := scheme + upstreamHost := host + upstreamPath := r.URL.Path + upstreamRawPath := r.URL.RawPath + upstreamURL := "" + + if mcpServer != nil { + gateway := p.mcpGateway.Load() + if route := gateway.Match(r); route != nil { + applied, err := route.Apply(r.Context(), r) + if err != nil { + if markIfClientCancel(r, err, result) { + return + } + result.Action = transform.ActionContinue + result.StatusCode = http.StatusBadGateway + result.Err = err + http.Error(w, "bad gateway", http.StatusBadGateway) + return + } + upstreamURL = applied.RequestURL + mcpTrace.SetGateway(map[string]any{ + "route": applied.Name, + "upstream": applied.Upstream, + "credentials_injected": applied.InjectedCredentialIDs, + }) + } + } + // Build upstream request. Use r.URL (which transforms may have modified) // rather than r.RequestURI (which is immutable). Preserve RawPath so // percent-encoded reserved characters like %2F survive to the upstream — // some APIs (e.g. GCS object names) treat decoded vs encoded slashes as // distinct path segments. - upstreamURL := (&url.URL{ - Scheme: scheme, - Host: host, - Path: r.URL.Path, - RawPath: r.URL.RawPath, - RawQuery: r.URL.RawQuery, - }).String() + if upstreamURL == "" { + upstreamURL = (&url.URL{ + Scheme: upstreamScheme, + Host: upstreamHost, + Path: upstreamPath, + RawPath: upstreamRawPath, + RawQuery: r.URL.RawQuery, + }).String() + } reqBody := transform.RequireBufferedBody(r.Body) // Check Len() before StreamingReader(), which clears the original reader. @@ -456,6 +501,27 @@ func (p *Proxy) handleHTTP(w http.ResponseWriter, r *http.Request, tunnelInfo *t upstreamReq.ContentLength = r.ContentLength } + var replayBody []byte + replayable := false + responseRetryEnabled := p.responseRetryHandler != nil && responseRetryEligible(upstreamReq) + if responseRetryEnabled { + upstreamReq.Body, replayBody, replayable, err = prepareReplayBody( + upstreamReq.Body, + upstreamReq.ContentLength, + bodyLimits.MaxRequestBodyBytes, + ) + if err != nil { + result.Action = transform.ActionContinue + result.StatusCode = http.StatusBadGateway + result.Err = err + http.Error(w, "bad gateway", http.StatusBadGateway) + return + } + if replayable { + upstreamReq.ContentLength = int64(len(replayBody)) + } + } + resp, err := p.doUpstream(upstreamReq) if err != nil { if markIfClientCancel(r, err, result) { @@ -469,6 +535,65 @@ func (p *Proxy) handleHTTP(w http.ResponseWriter, r *http.Request, tunnelInfo *t } defer resp.Body.Close() + if responseRetryEnabled { + chargeStarted := time.Now() + decision, replay, retryErr := p.responseRetryHandler.Decide(r.Context(), upstreamReq, resp, replayable) + if retryErr != nil { + p.logger.Warn("response retry authorization failed", slog.String("error", retryErr.Error())) + } + if replay { + _ = resp.Body.Close() // The 402 body is never returned after authorization. + replayReq := upstreamReq.Clone(r.Context()) + replayReq.Body = io.NopCloser(bytes.NewReader(replayBody)) + replayReq.ContentLength = int64(len(replayBody)) + replayReq.Header = upstreamReq.Header.Clone() + for name, values := range decision.Headers { + replayReq.Header.Del(name) + for _, value := range values { + replayReq.Header.Add(name, value) + } + } + if decision.Traceparent != "" { + replayReq.Header.Set("Traceparent", decision.Traceparent) + } + replayStarted := time.Now() + resp, err = p.doUpstream(replayReq) + completionTraceparent := decision.Traceparent + if completionTraceparent == "" { + completionTraceparent = upstreamReq.Header.Get("Traceparent") + } + if err != nil { + p.completeResponseRetry( + r.Context(), + decision.AttemptID, + nil, + "upstream_transport_error", + completionTraceparent, + time.Since(replayStarted), + time.Since(chargeStarted), + ) + if markIfClientCancel(r, err, result) { + return + } + result.Action = transform.ActionContinue + result.StatusCode = http.StatusBadGateway + result.Err = err + http.Error(w, "bad gateway", http.StatusBadGateway) + return + } + defer resp.Body.Close() + p.completeResponseRetry( + r.Context(), + decision.AttemptID, + resp, + "", + completionTraceparent, + time.Since(replayStarted), + time.Since(chargeStarted), + ) + } + } + // Wrap response body for lazy buffering by transforms. resp.Body = transform.NewBufferedBody(resp.Body, bodyLimits.MaxResponseBodyBytes) @@ -511,6 +636,50 @@ func (p *Proxy) handleHTTP(w http.ResponseWriter, r *http.Request, tunnelInfo *t p.writeResponse(w, finalResp) } +type multiReadCloser struct { + io.Reader + io.Closer +} + +func prepareReplayBody(body io.ReadCloser, contentLength, limit int64) (io.ReadCloser, []byte, bool, error) { + if limit <= 0 || contentLength > limit { + return body, nil, false, nil + } + replayBody, err := io.ReadAll(io.LimitReader(body, limit+1)) + if err != nil { + if closeErr := body.Close(); closeErr != nil { + return nil, nil, false, errors.Join(err, closeErr) + } + return nil, nil, false, err + } + if int64(len(replayBody)) > limit { + return &multiReadCloser{ + Reader: io.MultiReader(bytes.NewReader(replayBody), body), + Closer: body, + }, nil, false, nil + } + if err := body.Close(); err != nil { + return nil, nil, false, err + } + return io.NopCloser(bytes.NewReader(replayBody)), replayBody, true, nil +} + +func responseRetryEligible(req *http.Request) bool { + if req.ContentLength < 0 || isWebSocketUpgrade(req) { + return false + } + contentType := strings.ToLower(strings.TrimSpace(strings.SplitN(req.Header.Get("Content-Type"), ";", 2)[0])) + return !strings.HasPrefix(contentType, "application/grpc") +} + +func (p *Proxy) completeResponseRetry(ctx context.Context, attemptID string, resp *http.Response, transportError, traceparent string, replayDuration, chargeDuration time.Duration) { + ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second) + defer cancel() + if err := p.responseRetryHandler.Complete(ctx, attemptID, resp, transportError, traceparent, replayDuration, chargeDuration); err != nil { + p.logger.Warn("response retry completion failed", slog.String("error", err.Error())) + } +} + // isWebSocketUpgrade detects a WebSocket upgrade request. Connection is // parsed as comma-separated tokens with an exact case-insensitive "upgrade" // match so a value like "notupgrade" does not satisfy the check. @@ -652,35 +821,10 @@ func (p *Proxy) handleWebSocket(w http.ResponseWriter, r *http.Request, scheme, return } - // Proxy bidirectionally - var wg sync.WaitGroup - wg.Add(2) - - go func() { - defer wg.Done() - if _, err := io.Copy(upstreamConn, clientBuf); err != nil { - p.logger.Debug("websocket client->upstream copy error", slog.String("error", err.Error())) - } - // Signal upstream we're done writing - if tc, ok := upstreamConn.(*net.TCPConn); ok { - tc.CloseWrite() - } - }() - - go func() { - defer wg.Done() - if _, err := io.Copy(clientConn, upstreamConn); err != nil { - p.logger.Debug("websocket upstream->client copy error", slog.String("error", err.Error())) - } - // Signal client we're done writing - if tc, ok := clientConn.(*net.TCPConn); ok { - tc.CloseWrite() - } - }() - - wg.Wait() - clientConn.Close() - upstreamConn.Close() + // Relay until either side closes or the proxy shuts down. The hijacked + // bufio.Reader may hold bytes already read from the client, so wrap the + // conn to drain it first. + proxyBidi(p.shutdownCtx, newPeekedConn(clientConn, clientBuf.Reader), upstreamConn, p.logger, "websocket") p.logger.Debug("websocket connection closed", slog.String("host", host)) } @@ -727,6 +871,8 @@ func (p *Proxy) streamSSE(w http.ResponseWriter, resp *http.Response) { func (p *Proxy) writeResponse(w http.ResponseWriter, resp *http.Response) { copyHeaders(w.Header(), resp.Header) + // Forward upstream trailers (e.g. gRPC status) after the body. + defer writeTrailers(w, resp) if buf, ok := resp.Body.(*transform.BufferedBody); ok { // If a transform buffered the response body, set Content-Length // from the buffered data. Otherwise preserve the upstream header @@ -774,8 +920,11 @@ func buildTransport(resolver *net.Resolver, guard *dnsguard.Guard, responseHeade TLSClientConfig: &tls.Config{ MinVersion: tls.VersionTLS12, }, - Proxy: proxyFunc, - DialContext: dialer.DialContext, + Proxy: proxyFunc, + DialContext: dialer.DialContext, + // A custom DialContext disables net/http's automatic HTTP/2; opt back in + // so gRPC upstreams negotiate h2 over the same (dnsguard-controlled) dialer. + ForceAttemptHTTP2: true, MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, @@ -818,3 +967,13 @@ func copyHeaders(dst, src http.Header) { } } } + +// writeTrailers forwards upstream response trailers (e.g. gRPC status) to the +// client after the body, via the TrailerPrefix convention. +func writeTrailers(w http.ResponseWriter, resp *http.Response) { + for k, vs := range resp.Trailer { + for _, v := range vs { + w.Header().Add(http.TrailerPrefix+k, v) + } + } +} diff --git a/internal/proxy/proxy_test.go b/internal/proxy/proxy_test.go index 31151a2..975ccef 100644 --- a/internal/proxy/proxy_test.go +++ b/internal/proxy/proxy_test.go @@ -24,8 +24,12 @@ import ( "time" "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" "github.com/ironsh/iron-proxy/internal/certcache" + "github.com/ironsh/iron-proxy/internal/hostmatch" + "github.com/ironsh/iron-proxy/internal/mcp" + "github.com/ironsh/iron-proxy/internal/mcpgateway" "github.com/ironsh/iron-proxy/internal/transform" ) @@ -65,8 +69,9 @@ func startProxy(t *testing.T) (*Proxy, string, string, *x509.CertPool) { // replacerTransform replaces request and response bodies with fixed-size padding. type replacerTransform struct { - reqBody []byte - respBody []byte + reqBody []byte + reqHeaders http.Header + respBody []byte } func (r *replacerTransform) Name() string { return "replacer" } @@ -80,6 +85,7 @@ func (r *replacerTransform) TransformRequest(_ context.Context, _ *transform.Tra req.Body = transform.NewBufferedBodyFromBytes(r.reqBody) req.ContentLength = int64(len(r.reqBody)) } + copyHeaders(req.Header, r.reqHeaders) return &transform.TransformResult{Action: transform.ActionContinue}, nil } @@ -134,6 +140,34 @@ func startProxyWithTransforms(t *testing.T, transforms []transform.Transformer) return p, httpAddr, httpsAddr, pool } +func startHTTPProxyWithMCP(t *testing.T, policy *mcp.Policy, gateway *mcpgateway.Gateway) (*Proxy, string) { + t.Helper() + pipeline := transform.NewPipeline(nil, transform.BodyLimits{}, testLogger()) + p := New(Options{ + HTTPAddr: "127.0.0.1:0", + Pipeline: transform.NewPipelineHolder(pipeline), + MCPPolicy: mcp.NewPolicyHolder(policy), + MCPGateway: mcpgateway.NewHolder(gateway), + Logger: testLogger(), + }) + + ln, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + go func() { _ = p.httpServer.Serve(ln) }() + t.Cleanup(func() { _ = p.httpServer.Close() }) + return p, ln.Addr().String() +} + +func mustYAMLNode(t *testing.T, src string) yaml.Node { + t.Helper() + var doc yaml.Node + err := yaml.Unmarshal([]byte(src), &doc) + require.NoError(t, err) + require.Equal(t, yaml.DocumentNode, doc.Kind) + require.NotEmpty(t, doc.Content) + return *doc.Content[0] +} + func TestHTTPProxy(t *testing.T) { // Start an upstream HTTP server upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -188,6 +222,116 @@ func TestHTTPProxy_PostBody(t *testing.T) { require.Equal(t, "echo: request body", string(body)) } +func TestMCPGatewayRoutesAllowedCallWithCredential(t *testing.T) { + t.Setenv("MCP_TOKEN", "real-token") + + reached := make(chan struct{}, 1) + var wantHost string + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + reached <- struct{}{} + require.Equal(t, "Bearer real-token", r.Header.Get("Authorization")) + require.Equal(t, "/upstream", r.URL.Path) + require.Equal(t, wantHost, r.Host) + body, err := io.ReadAll(r.Body) + require.NoError(t, err) + require.Contains(t, string(body), `"tools/call"`) + w.Header().Set("Content-Type", "application/json") + _, err = io.WriteString(w, `{"jsonrpc":"2.0","id":1,"result":{"ok":true}}`) + require.NoError(t, err) + })) + defer upstream.Close() + + upstreamURL, err := url.Parse(upstream.URL) + require.NoError(t, err) + wantHost = upstreamURL.Host + + policy, err := mcp.Compile(mcp.Config{ + Servers: []mcp.ServerConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local", Paths: []string{"/mcp"}}}, + Tools: []mcp.ToolConfig{{Name: "search_repositories"}}, + }}, + }) + require.NoError(t, err) + gateway, err := mcpgateway.Compile(mcpgateway.Config{ + Routes: []mcpgateway.RouteConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local", Paths: []string{"/mcp"}}}, + Upstream: upstreamURL.String() + "/upstream", + Credentials: []mcpgateway.CredentialConfig{{ + Source: mustYAMLNode(t, `type: env +var: MCP_TOKEN +`), + Inject: mcpgateway.InjectConfig{ + Header: "Authorization", + Formatter: "Bearer {{ .Value }}", + }, + }}, + }}, + }) + require.NoError(t, err) + _, proxyAddr := startHTTPProxyWithMCP(t, policy, gateway) + + req, err := http.NewRequest(http.MethodPost, "http://"+proxyAddr+"/mcp", strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"search_repositories","arguments":{}}}`)) + require.NoError(t, err) + req.Host = "github.mcp.local" + req.Header.Set("Content-Type", "application/json") + + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + require.Equal(t, http.StatusOK, resp.StatusCode) + + select { + case <-reached: + case <-time.After(5 * time.Second): + t.Fatal("gateway upstream was not reached") + } +} + +func TestMCPGatewayDeniedToolDoesNotReachUpstream(t *testing.T) { + var reached atomic.Int32 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + reached.Add(1) + w.WriteHeader(http.StatusOK) + })) + defer upstream.Close() + + upstreamURL, err := url.Parse(upstream.URL) + require.NoError(t, err) + policy, err := mcp.Compile(mcp.Config{ + Servers: []mcp.ServerConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local", Paths: []string{"/mcp"}}}, + Tools: []mcp.ToolConfig{{Name: "search_repositories"}}, + }}, + }) + require.NoError(t, err) + gateway, err := mcpgateway.Compile(mcpgateway.Config{ + Routes: []mcpgateway.RouteConfig{{ + Name: "github", + Rules: []hostmatch.RuleConfig{{Host: "github.mcp.local", Paths: []string{"/mcp"}}}, + Upstream: upstreamURL.String(), + }}, + }) + require.NoError(t, err) + _, proxyAddr := startHTTPProxyWithMCP(t, policy, gateway) + + req, err := http.NewRequest(http.MethodPost, "http://"+proxyAddr+"/mcp", strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"delete_repository","arguments":{}}}`)) + require.NoError(t, err) + req.Host = "github.mcp.local" + req.Header.Set("Content-Type", "application/json") + + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + require.Equal(t, http.StatusOK, resp.StatusCode) + require.Contains(t, string(body), "blocked by iron-proxy policy") + require.Equal(t, int32(0), reached.Load()) +} + func TestHTTPSProxy(t *testing.T) { // Start an upstream HTTPS server upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -834,6 +978,91 @@ func TestContainsDotSegments(t *testing.T) { } } +func TestPrepareReplayBodyBoundaries(t *testing.T) { + cases := []struct { + name string + body string + contentLength int64 + limit int64 + wantReplay bool + }{ + {name: "below limit", body: "1234", contentLength: 4, limit: 5, wantReplay: true}, + {name: "at limit", body: "12345", contentLength: 5, limit: 5, wantReplay: true}, + {name: "known over limit", body: "123456", contentLength: 6, limit: 5, wantReplay: false}, + {name: "unknown at limit", body: "12345", contentLength: -1, limit: 5, wantReplay: true}, + {name: "unknown over limit", body: "123456", contentLength: -1, limit: 5, wantReplay: false}, + {name: "disabled", body: "12345", contentLength: 5, limit: 0, wantReplay: false}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + prepared, replayBody, replayable, err := prepareReplayBody( + io.NopCloser(strings.NewReader(tc.body)), + tc.contentLength, + tc.limit, + ) + require.NoError(t, err) + require.Equal(t, tc.wantReplay, replayable) + if tc.wantReplay { + require.Equal(t, []byte(tc.body), replayBody) + } else { + require.Nil(t, replayBody) + } + got, err := io.ReadAll(prepared) + require.NoError(t, err) + require.NoError(t, prepared.Close()) + require.Equal(t, tc.body, string(got)) + }) + } +} + +func TestResponseRetryEligible(t *testing.T) { + cases := []struct { + name string + method string + contentType string + contentLength int64 + connection string + upgrade string + want bool + }{ + {name: "ordinary GET", method: http.MethodGet, contentLength: 0, want: true}, + {name: "ordinary HEAD", method: http.MethodHead, contentLength: 0, want: true}, + {name: "ordinary known body", method: http.MethodPost, contentType: "application/json", contentLength: 4, want: true}, + {name: "known body with content type parameters", method: http.MethodPost, contentType: "application/json; charset=utf-8", contentLength: 4, want: true}, + {name: "known body without content type", method: http.MethodPost, contentLength: 4, want: true}, + {name: "unknown length with content type", method: http.MethodPost, contentType: "application/json", contentLength: -1, want: false}, + {name: "unknown length without content type", method: http.MethodPost, contentLength: -1, want: false}, + {name: "gRPC", method: http.MethodPost, contentType: "application/grpc", contentLength: 4, want: false}, + {name: "gRPC proto", method: http.MethodPost, contentType: "application/grpc+proto", contentLength: 4, want: false}, + {name: "gRPC JSON", method: http.MethodPost, contentType: "application/grpc+json", contentLength: 4, want: false}, + {name: "gRPC web", method: http.MethodPost, contentType: "application/grpc-web", contentLength: 4, want: false}, + {name: "gRPC web text", method: http.MethodPost, contentType: "application/grpc-web-text", contentLength: 4, want: false}, + {name: "gRPC web mixed case and parameters", method: http.MethodPost, contentType: " Application/GRPC-Web+Proto ; charset=utf-8", contentLength: 4, want: false}, + {name: "gRPC prefix is conservative", method: http.MethodPost, contentType: "application/grpcish", contentLength: 4, want: false}, + {name: "WebSocket", method: http.MethodGet, contentLength: 0, connection: "Upgrade", upgrade: "websocket", want: false}, + {name: "WebSocket mixed case and connection tokens", method: http.MethodGet, contentLength: 0, connection: "keep-alive, UpGrAdE", upgrade: "WebSocket", want: false}, + {name: "upgrade without connection header", method: http.MethodGet, contentLength: 0, upgrade: "websocket", want: true}, + {name: "connection upgrade without protocol", method: http.MethodGet, contentLength: 0, connection: "Upgrade", want: true}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var body io.Reader + if tc.contentLength != 0 { + body = strings.NewReader("body") + } + req := httptest.NewRequest(tc.method, "https://service.example/", body) + req.ContentLength = tc.contentLength + req.Header.Set("Content-Type", tc.contentType) + req.Header.Set("Connection", tc.connection) + req.Header.Set("Upgrade", tc.upgrade) + + require.Equal(t, tc.want, responseRetryEligible(req)) + }) + } +} + func TestHTTPProxy_FailsClosedUntilReady(t *testing.T) { upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) diff --git a/internal/proxy/sni_passthrough.go b/internal/proxy/sni_passthrough.go index 9beeb44..850e958 100644 --- a/internal/proxy/sni_passthrough.go +++ b/internal/proxy/sni_passthrough.go @@ -2,6 +2,7 @@ package proxy import ( "context" + "errors" "fmt" "io" "log/slog" @@ -135,14 +136,14 @@ func (p *Proxy) serveSNIPassthrough(clientConn net.Conn) error { result.StatusCode = http.StatusOK // Proxy bidirectionally until either side closes or the proxy shuts down. - proxyBidi(p.shutdownCtx, clientConn, upstream, p.logger) + proxyBidi(p.shutdownCtx, clientConn, upstream, p.logger, "sni passthrough") return nil } // proxyBidi copies bytes between two connections in both directions. When // either direction ends (EOF, error, or ctx cancellation), both connections -// are closed so the other direction unblocks. -func proxyBidi(ctx context.Context, a, b net.Conn, logger *slog.Logger) { +// are closed so the other direction unblocks. label names the relay in logs. +func proxyBidi(ctx context.Context, a, b net.Conn, logger *slog.Logger, label string) { ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -152,22 +153,28 @@ func proxyBidi(ctx context.Context, a, b net.Conn, logger *slog.Logger) { _ = b.Close() }() + // The direction that loses the race sees ErrClosed when the winner closes + // its conn; that is the normal teardown, not a copy failure. + logCopyErr := func(dir string, err error) { + if err != nil && !errors.Is(err, net.ErrClosed) { + logger.Debug(label+" "+dir+" copy error", slog.String("error", err.Error())) + } + } + var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() - if _, err := io.Copy(b, a); err != nil { - logger.Debug("sni passthrough a->b copy error", slog.String("error", err.Error())) - } + _, err := io.Copy(b, a) + logCopyErr("a->b", err) cancel() }() go func() { defer wg.Done() - if _, err := io.Copy(a, b); err != nil { - logger.Debug("sni passthrough b->a copy error", slog.String("error", err.Error())) - } + _, err := io.Copy(a, b) + logCopyErr("b->a", err) cancel() }() diff --git a/internal/proxy/tunnel.go b/internal/proxy/tunnel.go index 3bd87d4..4bc52a7 100644 --- a/internal/proxy/tunnel.go +++ b/internal/proxy/tunnel.go @@ -5,6 +5,7 @@ import ( "context" "crypto/tls" "encoding/binary" + "errors" "fmt" "io" "log/slog" @@ -15,9 +16,12 @@ import ( "slices" "strconv" "sync" + "sync/atomic" + "time" "github.com/ironsh/iron-proxy/internal/config" "github.com/ironsh/iron-proxy/internal/transform" + "golang.org/x/net/http2" ) // listenTunnel starts the CONNECT/SOCKS5 tunnel listener. @@ -44,7 +48,7 @@ func (p *Proxy) listenTunnel() error { } } -// handleTunnel peeks at the first byte to dispatch to CONNECT or SOCKS5. +// handleTunnel peeks at the first byte to dispatch to HTTP proxy or SOCKS5. // This is the single logging point for tunnel connection errors. func (p *Proxy) handleTunnel(conn net.Conn) { br := bufio.NewReader(conn) @@ -63,28 +67,32 @@ func (p *Proxy) handleTunnel(conn net.Conn) { return } - // Otherwise assume HTTP CONNECT - if err := p.handleCONNECT(conn, br); err != nil { - p.logger.Debug("tunnel connect error", slog.String("error", err.Error())) + // Otherwise assume HTTP proxy traffic. This supports both CONNECT and + // absolute-form HTTP requests on the same explicit proxy port. + if err := p.serveTunnelProxyHTTP(newPeekedConn(conn, br)); err != nil { + p.logger.Debug("tunnel http error", slog.String("error", err.Error())) } } -// handleCONNECT handles HTTP CONNECT tunnel requests. -func (p *Proxy) handleCONNECT(conn net.Conn, br *bufio.Reader) error { - defer conn.Close() - - req, err := http.ReadRequest(br) - if err != nil { - return fmt.Errorf("read request: %w", err) - } - - if req.Method != http.MethodConnect { - if _, err := fmt.Fprintf(conn, "HTTP/1.1 405 Method Not Allowed\r\n\r\n"); err != nil { - return fmt.Errorf("write 405: %w", err) +// serveTunnelProxyHTTP serves HTTP proxy requests on the tunnel listener. +// CONNECT establishes a tunnel; all other methods are forwarded through the +// normal HTTP proxy path. +func (p *Proxy) serveTunnelProxyHTTP(conn net.Conn) error { + return serveOneHTTPConn(conn, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodConnect { + p.handleTunnelCONNECT(w, r) + return } - return nil - } + if r.URL.Scheme != "http" { + http.Error(w, "unsupported proxy request scheme", http.StatusBadRequest) + return + } + p.handleDirectHTTP(w, r) + })) +} +// handleTunnelCONNECT handles HTTP CONNECT tunnel requests. +func (p *Proxy) handleTunnelCONNECT(w http.ResponseWriter, req *http.Request) { host := req.Host if _, _, err := net.SplitHostPort(host); err != nil { host = net.JoinHostPort(host, "443") @@ -92,30 +100,42 @@ func (p *Proxy) handleCONNECT(conn net.Conn, br *bufio.Reader) error { p.logger.Debug("tunnel CONNECT", slog.String("target", host)) - ok, rejectResp, tunnelInfo := p.tunnelTransformCheck(conn.RemoteAddr().String(), host, req.Header) + ok, rejectResp, tunnelInfo := p.tunnelTransformCheck(req.RemoteAddr, host, req.Header) if !ok { + w.Header().Set("Connection", "close") if rejectResp == nil { - rejectResp = &http.Response{ - StatusCode: http.StatusForbidden, - Proto: "HTTP/1.1", - ProtoMajor: 1, - ProtoMinor: 1, - Header: http.Header{}, - Body: http.NoBody, - } + http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) + return } - if err := rejectResp.Write(conn); err != nil { - return fmt.Errorf("write rejection: %w", err) - } - return nil + p.writeResponse(w, rejectResp) + return } - // Send 200 to signal tunnel established - if _, err := fmt.Fprintf(conn, "HTTP/1.1 200 Connection Established\r\n\r\n"); err != nil { - return fmt.Errorf("write 200: %w", err) + hj, ok := w.(http.Hijacker) + if !ok { + http.Error(w, "hijacking not supported", http.StatusInternalServerError) + return + } + conn, rw, err := hj.Hijack() + if err != nil { + p.logger.Warn("tunnel hijack error", slog.String("error", err.Error())) + return + } + defer conn.Close() + + // Send 200 to signal tunnel established. + if _, err := rw.WriteString("HTTP/1.1 200 Connection Established\r\n\r\n"); err != nil { + p.logger.Warn("tunnel write 200 error", slog.String("error", err.Error())) + return + } + if err := rw.Flush(); err != nil { + p.logger.Warn("tunnel flush 200 error", slog.String("error", err.Error())) + return } - return p.serveTunnel(conn, host, tunnelInfo) + if err := p.serveTunnelWithReader(conn, rw.Reader, host, tunnelInfo); err != nil { + p.logger.Debug("tunnel connect error", slog.String("error", err.Error())) + } } // handleSOCKS5 handles SOCKS5 tunnel requests. @@ -325,6 +345,10 @@ func (p *Proxy) tunnelTransformCheck(remoteAddr, target string, connectHeaders h // plain HTTP is served directly through handleHTTP. Anything else is rejected. func (p *Proxy) serveTunnel(clientConn net.Conn, target string, tunnelInfo *transform.TunnelInfo) error { br := bufio.NewReader(clientConn) + return p.serveTunnelWithReader(clientConn, br, target, tunnelInfo) +} + +func (p *Proxy) serveTunnelWithReader(clientConn net.Conn, br *bufio.Reader, target string, tunnelInfo *transform.TunnelInfo) error { first, err := br.Peek(1) if err != nil { return fmt.Errorf("peek client protocol: %w", err) @@ -357,6 +381,7 @@ func (p *Proxy) serveTunnelTLS(clientConn net.Conn, target string, tunnelInfo *t tlsConn := tls.Server(clientConn, &tls.Config{ GetCertificate: p.getCertificate, + NextProtos: []string{"h2", "http/1.1"}, // offer HTTP/2 to tunnelled clients }) defer func() { _ = tlsConn.Close() }() @@ -364,26 +389,55 @@ func (p *Proxy) serveTunnelTLS(clientConn net.Conn, target string, tunnelInfo *t return fmt.Errorf("TLS handshake for %s: %w", target, err) } - ln := newOneConnListener(tlsConn) - srv := &http.Server{ - Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - p.handleHTTP(w, r, tunnelInfo) - }), - } - return srv.Serve(ln) + return serveOneHTTPConn(tlsConn, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + p.handleHTTP(w, r, tunnelInfo) + })) } // serveTunnelHTTP serves plain HTTP requests through the normal handleHTTP handler. func (p *Proxy) serveTunnelHTTP(clientConn net.Conn, target string, tunnelInfo *transform.TunnelInfo) error { defer clientConn.Close() - ln := newOneConnListener(clientConn) + return serveOneHTTPConn(clientConn, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + p.handleHTTP(w, r, tunnelInfo) + })) +} + +func serveOneHTTPConn(conn net.Conn, handler http.Handler) error { + ln := newOneConnListener(conn) + var hijacked atomic.Bool + var handlers sync.WaitGroup srv := &http.Server{ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - p.handleHTTP(w, r, tunnelInfo) + handlers.Add(1) + defer handlers.Done() + handler.ServeHTTP(w, r) }), + ReadHeaderTimeout: 10 * time.Second, + IdleTimeout: 10 * time.Second, + ConnState: func(_ net.Conn, state http.ConnState) { + if state == http.StateHijacked { + hijacked.Store(true) + } + if state == http.StateClosed || state == http.StateHijacked { + ln.closeDone() + } + }, } - return srv.Serve(ln) + // Serve HTTP/2 when a tunnelled conn negotiates it; zero config never errors. + _ = http2.ConfigureServer(srv, &http2.Server{}) + err := srv.Serve(ln) + // A hijacking handler (WebSocket relay, CONNECT tunnel) owns the + // connection until it returns. Serve comes back as soon as the hijack + // happens, so wait for the handler before returning: callers defer + // Close on the connection and must not run it mid-relay. + if hijacked.Load() { + handlers.Wait() + } + if errors.Is(err, net.ErrClosed) { + return nil + } + return err } func cloneTunnelInfo(info *transform.TunnelInfo) *transform.TunnelInfo { @@ -434,12 +488,16 @@ func (l *oneConnListener) Accept() (net.Conn, error) { } func (l *oneConnListener) Close() error { + l.closeDone() + return nil +} + +func (l *oneConnListener) closeDone() { select { case <-l.done: default: close(l.done) } - return nil } func (l *oneConnListener) Addr() net.Addr { diff --git a/internal/proxy/tunnel_test.go b/internal/proxy/tunnel_test.go index b70c903..04bab51 100644 --- a/internal/proxy/tunnel_test.go +++ b/internal/proxy/tunnel_test.go @@ -11,6 +11,7 @@ import ( "net" "net/http" "net/http/httptest" + "net/url" "testing" "time" @@ -167,26 +168,72 @@ func TestTunnel_CONNECT_HTTPS_MITM(t *testing.T) { defer resp2.Body.Close() require.Equal(t, http.StatusOK, resp2.StatusCode) + require.False(t, resp2.Close) body, err := io.ReadAll(resp2.Body) require.NoError(t, err) require.Equal(t, "hello from tls tunnel", string(body)) + + req2, err := http.NewRequest("GET", fmt.Sprintf("https://%s/again", fakeHost), nil) + require.NoError(t, err) + + err = req2.Write(tlsConn) + require.NoError(t, err) + + resp3, err := http.ReadResponse(tlsBr, req2) + require.NoError(t, err) + defer resp3.Body.Close() + + require.Equal(t, http.StatusOK, resp3.StatusCode) + body, err = io.ReadAll(resp3.Body) + require.NoError(t, err) + require.Equal(t, "hello from tls tunnel", string(body)) } -func TestTunnel_CONNECT_MethodNotAllowed(t *testing.T) { +func TestTunnel_HTTPProxyAbsoluteForm(t *testing.T) { + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, "/test?x=1", r.RequestURI) + w.Header().Set("X-Tunnel-HTTP", "true") + w.WriteHeader(http.StatusOK) + _, _ = fmt.Fprint(w, "hello from http proxy") + })) + defer upstream.Close() + + _, tunnelAddr, _ := startTunnelProxy(t, nil) + + proxyURL, err := url.Parse("http://" + tunnelAddr) + require.NoError(t, err) + transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)} + defer transport.CloseIdleConnections() + + client := &http.Client{Transport: transport} + resp, err := client.Get(upstream.URL + "/test?x=1") + require.NoError(t, err) + defer resp.Body.Close() + + require.Equal(t, http.StatusOK, resp.StatusCode) + require.Equal(t, "true", resp.Header.Get("X-Tunnel-HTTP")) + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + require.Equal(t, "hello from http proxy", string(body)) +} + +func TestTunnel_HTTPProxyRejectsAbsoluteFormHTTPS(t *testing.T) { _, tunnelAddr, _ := startTunnelProxy(t, nil) conn, err := net.DialTimeout("tcp", tunnelAddr, 5*time.Second) require.NoError(t, err) defer conn.Close() - _, err = fmt.Fprintf(conn, "GET /test HTTP/1.1\r\nHost: example.com\r\n\r\n") + _, err = fmt.Fprintf(conn, "GET https://example.com/test HTTP/1.1\r\nHost: example.com\r\n\r\n") require.NoError(t, err) br := bufio.NewReader(conn) resp, err := http.ReadResponse(br, nil) require.NoError(t, err) - require.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) + defer resp.Body.Close() + + require.Equal(t, http.StatusBadRequest, resp.StatusCode) } func TestTunnel_SOCKS5_HTTP(t *testing.T) { @@ -348,6 +395,14 @@ func TestTunnel_TransformReject(t *testing.T) { resp, err := http.ReadResponse(br, nil) require.NoError(t, err) require.Equal(t, http.StatusForbidden, resp.StatusCode) + _, err = io.ReadAll(resp.Body) + require.NoError(t, err) + require.NoError(t, resp.Body.Close()) + + err = conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond)) + require.NoError(t, err) + _, err = br.Peek(1) + require.ErrorIs(t, err, io.EOF) } func TestTunnel_CONNECTHeadersAndRejectResponse(t *testing.T) { @@ -522,3 +577,342 @@ func (t *tunnelInfoTransform) TransformRequest(_ context.Context, tctx *transfor func (t *tunnelInfoTransform) TransformResponse(_ context.Context, _ *transform.TransformContext, _ *http.Request, _ *http.Response) (*transform.TransformResult, error) { return &transform.TransformResult{Action: transform.ActionContinue}, nil } + +// dialCONNECT dials the tunnel listener, issues CONNECT for target, and +// returns the connection plus a reader positioned after the 200 reply. +func dialCONNECT(t *testing.T, tunnelAddr, target string) (net.Conn, *bufio.Reader) { + t.Helper() + + conn, err := net.DialTimeout("tcp", tunnelAddr, 5*time.Second) + require.NoError(t, err) + t.Cleanup(func() { _ = conn.Close() }) + + _, err = fmt.Fprintf(conn, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", target, target) + require.NoError(t, err) + + br := bufio.NewReader(conn) + resp, err := http.ReadResponse(br, nil) + require.NoError(t, err) + _ = resp.Body.Close() + require.Equal(t, http.StatusOK, resp.StatusCode) + return conn, br +} + +// dialSOCKS5 dials the tunnel listener and completes a no-auth SOCKS5 +// CONNECT to target, which must be an IPv4 host:port. +func dialSOCKS5(t *testing.T, tunnelAddr, target string) (net.Conn, *bufio.Reader) { + t.Helper() + + conn, err := net.DialTimeout("tcp", tunnelAddr, 5*time.Second) + require.NoError(t, err) + t.Cleanup(func() { _ = conn.Close() }) + + targetHost, targetPortStr, err := net.SplitHostPort(target) + require.NoError(t, err) + ip := net.ParseIP(targetHost).To4() + require.NotNil(t, ip) + var port uint16 + _, err = fmt.Sscanf(targetPortStr, "%d", &port) + require.NoError(t, err) + + // Auth negotiation: version 5, 1 method (no auth) + _, err = conn.Write([]byte{0x05, 0x01, 0x00}) + require.NoError(t, err) + authResp := make([]byte, 2) + _, err = io.ReadFull(conn, authResp) + require.NoError(t, err) + require.Equal(t, []byte{0x05, 0x00}, authResp) + + connectReq := []byte{0x05, 0x01, 0x00, 0x01} // ver, cmd=connect, rsv, atyp=IPv4 + connectReq = append(connectReq, ip...) + connectReq = binary.BigEndian.AppendUint16(connectReq, port) + _, err = conn.Write(connectReq) + require.NoError(t, err) + + connectResp := make([]byte, 10) + _, err = io.ReadFull(conn, connectResp) + require.NoError(t, err) + require.Equal(t, byte(0x05), connectResp[0]) // version + require.Equal(t, byte(0x00), connectResp[1]) // success + + return conn, bufio.NewReader(conn) +} + +// startWebSocketUpstream starts a raw TCP server that accepts a single +// connection, answers the upgrade request with 101, then hands the +// connection to serve. The connection is closed when serve returns. +func startWebSocketUpstream(t *testing.T, serve func(conn net.Conn, br *bufio.Reader)) string { + t.Helper() + + ln, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { _ = ln.Close() }) + + go func() { + conn, err := ln.Accept() + if err != nil { + return + } + defer conn.Close() + + br := bufio.NewReader(conn) + if _, err := http.ReadRequest(br); err != nil { + return + } + _, err = io.WriteString(conn, "HTTP/1.1 101 Switching Protocols\r\n"+ + "Upgrade: websocket\r\nConnection: Upgrade\r\n\r\n") + if err != nil { + return + } + serve(conn, br) + }() + + return ln.Addr().String() +} + +// startWebSocketEchoUpstream is startWebSocketUpstream with an echo loop. +func startWebSocketEchoUpstream(t *testing.T) string { + t.Helper() + return startWebSocketUpstream(t, func(conn net.Conn, br *bufio.Reader) { + _, _ = io.Copy(conn, br) + }) +} + +// wsUpgrade sends a WebSocket upgrade request for target over conn and +// verifies the 101 response, leaving br positioned at the relayed stream. +func wsUpgrade(t *testing.T, conn net.Conn, br *bufio.Reader, target string) { + t.Helper() + + _, err := fmt.Fprintf(conn, "GET /ws HTTP/1.1\r\n"+ + "Host: %s\r\n"+ + "Upgrade: websocket\r\n"+ + "Connection: Upgrade\r\n"+ + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ + "Sec-WebSocket-Version: 13\r\n\r\n", + target) + require.NoError(t, err) + + require.NoError(t, conn.SetReadDeadline(time.Now().Add(5*time.Second))) + resp, err := http.ReadResponse(br, nil) + require.NoError(t, err) + require.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode) +} + +// wsUpgradeAndEcho upgrades via wsUpgrade, then checks that payload bytes +// echo back. +func wsUpgradeAndEcho(t *testing.T, conn net.Conn, br *bufio.Reader, target string) { + t.Helper() + + wsUpgrade(t, conn, br, target) + + _, err := conn.Write([]byte("hello websocket")) + require.NoError(t, err) + + buf := make([]byte, 4096) + require.NoError(t, conn.SetReadDeadline(time.Now().Add(5*time.Second))) + n, err := br.Read(buf) + require.NoError(t, err) + require.Equal(t, "hello websocket", string(buf[:n])) +} + +func TestTunnel_CONNECT_WebSocket(t *testing.T) { + target := startWebSocketEchoUpstream(t) + + _, tunnelAddr, _ := startTunnelProxy(t, nil) + + conn, err := net.DialTimeout("tcp", tunnelAddr, 5*time.Second) + require.NoError(t, err) + defer conn.Close() + + _, err = fmt.Fprintf(conn, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", target, target) + require.NoError(t, err) + + br := bufio.NewReader(conn) + resp, err := http.ReadResponse(br, nil) + require.NoError(t, err) + require.Equal(t, http.StatusOK, resp.StatusCode) + + wsUpgradeAndEcho(t, conn, br, target) +} + +func TestTunnel_SOCKS5_WebSocket(t *testing.T) { + target := startWebSocketEchoUpstream(t) + + _, tunnelAddr, _ := startTunnelProxy(t, nil) + + conn, err := net.DialTimeout("tcp", tunnelAddr, 5*time.Second) + require.NoError(t, err) + defer conn.Close() + + targetHost, targetPortStr, err := net.SplitHostPort(target) + require.NoError(t, err) + + // SOCKS5 auth negotiation: version 5, 1 method (no auth) + _, err = conn.Write([]byte{0x05, 0x01, 0x00}) + require.NoError(t, err) + + authResp := make([]byte, 2) + _, err = io.ReadFull(conn, authResp) + require.NoError(t, err) + require.Equal(t, []byte{0x05, 0x00}, authResp) + + // SOCKS5 connect request: IPv4 + ip := net.ParseIP(targetHost).To4() + require.NotNil(t, ip) + + var port uint16 + _, err = fmt.Sscanf(targetPortStr, "%d", &port) + require.NoError(t, err) + + connectReq := []byte{0x05, 0x01, 0x00, 0x01} // ver, cmd=connect, rsv, atyp=IPv4 + connectReq = append(connectReq, ip...) + portBuf := make([]byte, 2) + binary.BigEndian.PutUint16(portBuf, port) + connectReq = append(connectReq, portBuf...) + + _, err = conn.Write(connectReq) + require.NoError(t, err) + + connectResp := make([]byte, 10) + _, err = io.ReadFull(conn, connectResp) + require.NoError(t, err) + require.Equal(t, byte(0x05), connectResp[0]) // version + require.Equal(t, byte(0x00), connectResp[1]) // success + + wsUpgradeAndEcho(t, conn, bufio.NewReader(conn), target) +} + +// wsEntryPaths are the ways a client reaches the WebSocket relay with a +// plaintext client connection. Each opens a connection ready for the +// upgrade request to target. The TLS-terminated paths (HTTPS listener, +// CONNECT MITM) are absent: they dial upstream with wss verified against +// system roots, which a test upstream cannot satisfy. +var wsEntryPaths = []struct { + name string + open func(t *testing.T, target string) (net.Conn, *bufio.Reader) +}{ + {"http listener", func(t *testing.T, _ string) (net.Conn, *bufio.Reader) { + _, httpAddr, _, _ := startProxy(t) + conn, err := net.DialTimeout("tcp", httpAddr, 5*time.Second) + require.NoError(t, err) + t.Cleanup(func() { _ = conn.Close() }) + return conn, bufio.NewReader(conn) + }}, + {"connect tunnel", func(t *testing.T, target string) (net.Conn, *bufio.Reader) { + _, tunnelAddr, _ := startTunnelProxy(t, nil) + return dialCONNECT(t, tunnelAddr, target) + }}, + {"socks5 tunnel", func(t *testing.T, target string) (net.Conn, *bufio.Reader) { + _, tunnelAddr, _ := startTunnelProxy(t, nil) + return dialSOCKS5(t, tunnelAddr, target) + }}, +} + +// The relay must close the client when the upstream hangs up; the +// pre-fix code only did so for bare *net.TCPConn clients, so tunnelled +// clients hung until they gave up. +func TestWebSocket_UpstreamClosePropagatesToClient(t *testing.T) { + for _, path := range wsEntryPaths { + t.Run(path.name, func(t *testing.T) { + // Upstream hangs up right after the handshake. + target := startWebSocketUpstream(t, func(net.Conn, *bufio.Reader) {}) + conn, br := path.open(t, target) + wsUpgrade(t, conn, br, target) + + require.NoError(t, conn.SetReadDeadline(time.Now().Add(5*time.Second))) + _, err := br.ReadByte() + require.ErrorIs(t, err, io.EOF) + }) + } +} + +// The relay must close the upstream when the client hangs up, or every +// abandoned client leaks an upstream socket. +func TestWebSocket_ClientClosePropagatesToUpstream(t *testing.T) { + for _, path := range wsEntryPaths { + t.Run(path.name, func(t *testing.T) { + upstreamRead := make(chan error, 1) + target := startWebSocketUpstream(t, func(conn net.Conn, br *bufio.Reader) { + _ = conn.SetReadDeadline(time.Now().Add(5 * time.Second)) + _, err := br.ReadByte() + upstreamRead <- err + }) + conn, br := path.open(t, target) + wsUpgrade(t, conn, br, target) + + require.NoError(t, conn.Close()) + select { + case err := <-upstreamRead: + require.ErrorIs(t, err, io.EOF) + case <-time.After(5 * time.Second): + t.Fatal("upstream did not observe the client close") + } + }) + } +} + +// TestServeOneHTTPConn_HijackWaitsForHandler pins the contract that lets +// hijacking handlers (WebSocket relays, CONNECT tunnels) survive their +// callers' deferred conn.Close(): serveOneHTTPConn must not return until a +// hijacking handler has finished with the connection. This covers the TLS +// MITM branch too, which cannot be exercised end-to-end because +// handleWebSocket verifies upstream certificates against system roots. +func TestServeOneHTTPConn_HijackWaitsForHandler(t *testing.T) { + clientConn, serverConn := net.Pipe() + defer clientConn.Close() + + release := make(chan struct{}) + serveReturned := make(chan struct{}) + + go func() { + defer close(serveReturned) + err := serveOneHTTPConn(serverConn, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Background HTTP handler: report failures via t.Error. + hj, ok := w.(http.Hijacker) + if !ok { + t.Error("hijacking not supported") + return + } + conn, rw, err := hj.Hijack() + if err != nil { + t.Errorf("hijack: %v", err) + return + } + defer conn.Close() + if _, err := rw.WriteString("HTTP/1.1 101 Switching Protocols\r\n\r\n"); err != nil { + t.Errorf("write 101: %v", err) + return + } + if err := rw.Flush(); err != nil { + t.Errorf("flush 101: %v", err) + return + } + <-release + })) + if err != nil { + t.Errorf("serveOneHTTPConn: %v", err) + } + }() + + _, err := clientConn.Write([]byte("GET /ws HTTP/1.1\r\nHost: example.com\r\n\r\n")) + require.NoError(t, err) + + // Read the 101 status line so the hijack has definitely happened. + br := bufio.NewReader(clientConn) + statusLine, err := br.ReadString('\n') + require.NoError(t, err) + require.Contains(t, statusLine, "101") + + select { + case <-serveReturned: + t.Fatal("serveOneHTTPConn returned while the hijacking handler was still running") + case <-time.After(100 * time.Millisecond): + } + + close(release) + select { + case <-serveReturned: + case <-time.After(5 * time.Second): + t.Fatal("serveOneHTTPConn did not return after the hijacking handler finished") + } +} diff --git a/internal/responseretry/handler.go b/internal/responseretry/handler.go new file mode 100644 index 0000000..5ec03ca --- /dev/null +++ b/internal/responseretry/handler.go @@ -0,0 +1,534 @@ +// Package responseretry implements a protocol-neutral, externally decided +// response retry hook. +package responseretry + +import ( + "bytes" + "context" + "crypto/tls" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "net" + "net/http" + "net/netip" + "net/url" + "strconv" + "strings" + "syscall" + "time" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "golang.org/x/net/http/httpguts" +) + +var forbiddenRetryHeaders = map[string]struct{}{ + "Connection": {}, + "Content-Length": {}, + "Host": {}, + "Keep-Alive": {}, + "Proxy-Authorization": {}, + "Proxy-Authenticate": {}, + "Proxy-Connection": {}, + "Te": {}, + "Trailer": {}, + "Transfer-Encoding": {}, + "Upgrade": {}, +} + +const defaultClientTimeout = 10 * time.Second + +var ( + privateNetworks = []netip.Prefix{ + netip.MustParsePrefix("10.0.0.0/8"), + netip.MustParsePrefix("172.16.0.0/12"), + netip.MustParsePrefix("192.168.0.0/16"), + netip.MustParsePrefix("fc00::/7"), + } +) + +// Handler asks a trusted service whether a bounded upstream response should +// be retried and reports the result of the one permitted replay. +type Handler struct { + authorizeEndpoint *url.URL + completeEndpoint *url.URL + token string + sandboxID string + statuses map[int]struct{} + completionHeaders map[string]struct{} + client *http.Client +} + +// Options configures a response retry handler. +type Options struct { + AuthorizeEndpoint string + CompleteEndpoint string + Token string + SandboxID string + Statuses []int + AllowHTTP bool + CompletionHeaders []string + AllowCIDRs []string + Resolver *net.Resolver + Guard *dnsguard.Guard + ClientTimeout time.Duration + Client *http.Client +} + +// DecisionRequest describes the completed request and response. Request and +// response bodies are deliberately excluded. +type DecisionRequest struct { + Scheme string `json:"scheme"` + Host string `json:"host"` + Method string `json:"method"` + Path string `json:"path"` + Replayable bool `json:"replayable"` + Status int `json:"status"` + ResponseHeaders map[string][]string `json:"response_headers"` + SandboxID string `json:"sandbox_id"` + Traceparent string `json:"traceparent,omitempty"` +} + +// DecisionResponse authorizes at most one replay with additional headers. +type DecisionResponse struct { + Retry bool `json:"retry"` + Headers map[string]string `json:"headers"` + AttemptID string `json:"attempt_id"` + Traceparent string `json:"traceparent,omitempty"` +} + +// CompletionRequest reports the replay result without any request or response +// body and includes only explicitly selected response headers. +type CompletionRequest struct { + AttemptID string `json:"attempt_id"` + ReplayStatus *int `json:"replay_status"` + ResponseHeaders map[string][]string `json:"response_headers"` + TransportError string `json:"transport_error,omitempty"` + Traceparent string `json:"traceparent,omitempty"` + ReplayDurationMS int64 `json:"replay_duration_ms,omitempty"` + ChargeDurationMS int64 `json:"charge_duration_ms,omitempty"` +} + +// Decision contains the validated result of an authorization call. +type Decision struct { + Headers http.Header + AttemptID string + Traceparent string +} + +// New creates a Handler for the configured response status codes. +func New(opts Options) (*Handler, error) { + authorizeURL, err := parseEndpoint(opts.AuthorizeEndpoint, opts.AllowHTTP) + if err != nil { + return nil, fmt.Errorf("authorize endpoint: %w", err) + } + completeURL, err := parseEndpoint(opts.CompleteEndpoint, opts.AllowHTTP) + if err != nil { + return nil, fmt.Errorf("complete endpoint: %w", err) + } + if opts.Token == "" { + return nil, fmt.Errorf("response retry handler token is required") + } + if opts.SandboxID == "" { + return nil, fmt.Errorf("response retry handler sandbox identity is required") + } + statusSet := make(map[int]struct{}, len(opts.Statuses)) + for _, status := range opts.Statuses { + if status < 100 || status > 599 { + return nil, fmt.Errorf("invalid response retry status %s", strconv.Itoa(status)) + } + statusSet[status] = struct{}{} + } + if len(statusSet) == 0 { + return nil, fmt.Errorf("at least one response retry status is required") + } + allowCIDRs, err := parsePrivateCIDRs(opts.AllowCIDRs) + if err != nil { + return nil, fmt.Errorf("response retry handler allow CIDRs: %w", err) + } + completionHeaders := make(map[string]struct{}, len(opts.CompletionHeaders)) + for _, name := range opts.CompletionHeaders { + if !httpguts.ValidHeaderFieldName(name) { + return nil, fmt.Errorf("invalid completion response header %q", name) + } + completionHeaders[http.CanonicalHeaderKey(name)] = struct{}{} + } + return &Handler{ + authorizeEndpoint: authorizeURL, + completeEndpoint: completeURL, + token: opts.Token, + sandboxID: opts.SandboxID, + statuses: statusSet, + completionHeaders: completionHeaders, + client: hardenedClient( + opts.Client, + opts.Resolver, + opts.Guard, + opts.ClientTimeout, + allowCIDRs, + []*url.URL{authorizeURL, completeURL}, + ), + }, nil +} + +// Decide returns authorization metadata for one replay. The caller passes +// replayable=false when the exact request body cannot be replayed safely. +func (h *Handler) Decide(ctx context.Context, req *http.Request, resp *http.Response, replayable bool) (*Decision, bool, error) { + if _, ok := h.statuses[resp.StatusCode]; !ok { + return nil, false, nil + } + path := req.URL.EscapedPath() + if req.URL.RawQuery != "" { + path += "?" + req.URL.RawQuery + } + payload, err := json.Marshal(DecisionRequest{ + Scheme: req.URL.Scheme, + Host: req.URL.Host, + Method: req.Method, + Path: path, + Replayable: replayable, + Status: resp.StatusCode, + ResponseHeaders: resp.Header, + SandboxID: h.sandboxID, + Traceparent: req.Header.Get("Traceparent"), + }) + if err != nil { + return nil, false, fmt.Errorf("encode response retry decision request: %w", err) + } + decisionReq, err := h.newRequest(ctx, h.authorizeEndpoint, payload) + if err != nil { + return nil, false, fmt.Errorf("create response retry decision request: %w", err) + } + decisionResp, err := h.client.Do(decisionReq) + if err != nil { + return nil, false, fmt.Errorf("request response retry decision: %w", err) + } + defer decisionResp.Body.Close() + if decisionResp.StatusCode != http.StatusOK { + _, _ = io.Copy(io.Discard, io.LimitReader(decisionResp.Body, 64<<10)) + return nil, false, fmt.Errorf("response retry handler rejected request with status %d", decisionResp.StatusCode) + } + var decision DecisionResponse + if err := json.NewDecoder(io.LimitReader(decisionResp.Body, 64<<10)).Decode(&decision); err != nil { + return nil, false, fmt.Errorf("decode response retry decision: %w", err) + } + if !decision.Retry { + return nil, false, nil + } + if !replayable { + return nil, false, fmt.Errorf("response retry handler authorized a non-replayable request") + } + if decision.AttemptID == "" { + return nil, false, fmt.Errorf("response retry handler omitted attempt id") + } + if decision.Traceparent != "" && !validTraceparent(decision.Traceparent) { + return nil, false, fmt.Errorf("response retry handler returned invalid traceparent") + } + headers := make(http.Header, len(decision.Headers)) + for name, value := range decision.Headers { + if !httpguts.ValidHeaderFieldName(name) { + return nil, false, fmt.Errorf("response retry handler returned an invalid header name") + } + if !httpguts.ValidHeaderFieldValue(value) { + return nil, false, fmt.Errorf("response retry handler returned an invalid value for header %s", name) + } + canonical := http.CanonicalHeaderKey(name) + if _, forbidden := forbiddenRetryHeaders[canonical]; forbidden { + return nil, false, fmt.Errorf("response retry handler returned forbidden header %s", canonical) + } + if _, duplicate := headers[canonical]; duplicate { + return nil, false, fmt.Errorf("response retry handler returned duplicate header %s", canonical) + } + headers.Set(canonical, value) + } + return &Decision{ + Headers: headers, + AttemptID: decision.AttemptID, + Traceparent: decision.Traceparent, + }, true, nil +} + +// Complete reports the replay outcome. It is idempotent at the handler. +func (h *Handler) Complete(ctx context.Context, attemptID string, resp *http.Response, transportError, traceparent string, replayDuration, chargeDuration time.Duration) error { + var status *int + headers := make(http.Header) + if resp != nil { + value := resp.StatusCode + status = &value + headers = selectedHeaders(resp.Header, h.completionHeaders) + } + payload, err := json.Marshal(CompletionRequest{ + AttemptID: attemptID, + ReplayStatus: status, + ResponseHeaders: headers, + TransportError: transportError, + Traceparent: traceparent, + ReplayDurationMS: replayDuration.Milliseconds(), + ChargeDurationMS: chargeDuration.Milliseconds(), + }) + if err != nil { + return fmt.Errorf("encode response retry completion request: %w", err) + } + var lastErr error + for attempt := 0; attempt < 2; attempt++ { + req, err := h.newRequest(ctx, h.completeEndpoint, payload) + if err != nil { + return fmt.Errorf("create response retry completion request: %w", err) + } + completionResp, err := h.client.Do(req) + if err != nil { + lastErr = fmt.Errorf("request response retry completion: %w", err) + continue + } + _, copyErr := io.Copy(io.Discard, io.LimitReader(completionResp.Body, 64<<10)) + closeErr := completionResp.Body.Close() + if copyErr != nil { + return fmt.Errorf("read response retry completion: %w", copyErr) + } + if closeErr != nil { + return fmt.Errorf("close response retry completion: %w", closeErr) + } + if completionResp.StatusCode >= 200 && completionResp.StatusCode < 300 { + return nil + } + lastErr = fmt.Errorf("response retry completion returned status %d", completionResp.StatusCode) + if completionResp.StatusCode < 500 { + break + } + } + return lastErr +} + +func (h *Handler) newRequest(ctx context.Context, endpoint *url.URL, payload []byte) (*http.Request, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), bytes.NewReader(payload)) + if err != nil { + return nil, err + } + req.Header.Set("Authorization", "Bearer "+h.token) + req.Header.Set("Content-Type", "application/json") + return req, nil +} + +func parseEndpoint(endpoint string, allowHTTP bool) (*url.URL, error) { + u, err := url.Parse(endpoint) + if err != nil || !u.IsAbs() || u.Host == "" || u.User != nil { + return nil, fmt.Errorf("response retry handler URL must be absolute without credentials") + } + if u.Fragment != "" { + return nil, fmt.Errorf("response retry handler URL must not contain a fragment") + } + httpAllowed := u.Scheme == "http" && (allowHTTP || isLoopback(u.Hostname())) + if u.Scheme != "https" && !httpAllowed { + return nil, fmt.Errorf("response retry handler URL must use HTTPS unless HTTP is explicitly allowed") + } + return u, nil +} + +func hardenedClient( + client *http.Client, + resolver *net.Resolver, + guard *dnsguard.Guard, + timeout time.Duration, + allowCIDRs []netip.Prefix, + endpoints []*url.URL, +) *http.Client { + if client == nil { + client = &http.Client{Transport: newHandlerTransport(resolver, guard, allowCIDRs, endpoints)} + } + result := *client + if result.Timeout <= 0 { + result.Timeout = timeout + if result.Timeout <= 0 { + result.Timeout = defaultClientTimeout + } + } + result.CheckRedirect = func(_ *http.Request, _ []*http.Request) error { + return http.ErrUseLastResponse + } + return &result +} + +type handlerTransport struct { + guarded http.RoundTripper + loopback http.RoundTripper + allowedEndpoints map[string]struct{} +} + +func newHandlerTransport( + resolver *net.Resolver, + guard *dnsguard.Guard, + allowCIDRs []netip.Prefix, + endpoints []*url.URL, +) http.RoundTripper { + guardedDialer := &net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + Resolver: resolver, + Control: handlerDialControl(guard, allowCIDRs), + } + loopbackDialer := &net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + Control: loopbackOnlyDialControl, + } + allowedEndpoints := make(map[string]struct{}, len(endpoints)) + for _, endpoint := range endpoints { + allowedEndpoints[handlerEndpointKey(endpoint)] = struct{}{} + } + return &handlerTransport{ + guarded: newHTTPTransport(guardedDialer), + loopback: newHTTPTransport(loopbackDialer), + allowedEndpoints: allowedEndpoints, + } +} + +func (t *handlerTransport) RoundTrip(req *http.Request) (*http.Response, error) { + if _, allowed := t.allowedEndpoints[handlerEndpointKey(req.URL)]; !allowed || req.Method != http.MethodPost { + return nil, fmt.Errorf("response retry transport rejected unconfigured request") + } + if isLoopback(req.URL.Hostname()) { + return t.loopback.RoundTrip(req) + } + return t.guarded.RoundTrip(req) +} + +func handlerEndpointKey(endpoint *url.URL) string { + port := endpoint.Port() + if port == "" { + if strings.EqualFold(endpoint.Scheme, "https") { + port = "443" + } else { + port = "80" + } + } + path := endpoint.EscapedPath() + if path == "" { + path = "/" + } + return strings.ToLower(endpoint.Scheme) + "\x00" + + net.JoinHostPort(strings.ToLower(endpoint.Hostname()), port) + "\x00" + + path + "\x00" + endpoint.RawQuery +} + +func handlerDialControl(guard *dnsguard.Guard, allowCIDRs []netip.Prefix) func(string, string, syscall.RawConn) error { + return func(network string, address string, connection syscall.RawConn) error { + host, _, err := net.SplitHostPort(address) + if err != nil { + host = address + } + addr, err := netip.ParseAddr(host) + if err == nil { + addr = addr.Unmap() + if guard.IsDenied(addr) && prefixContains(allowCIDRs, addr) { + return nil + } + } + return guard.DialControl(network, address, connection) + } +} + +func parsePrivateCIDRs(cidrs []string) ([]netip.Prefix, error) { + prefixes := make([]netip.Prefix, 0, len(cidrs)) + for _, raw := range cidrs { + value := strings.TrimSpace(raw) + if value == "" || !strings.Contains(value, "/") { + return nil, fmt.Errorf("invalid CIDR %q: must use CIDR notation", raw) + } + prefix, err := netip.ParsePrefix(value) + if err != nil { + return nil, fmt.Errorf("invalid CIDR %q: %w", raw, err) + } + prefix = prefix.Masked() + if !prefixWithin(prefix, privateNetworks) { + return nil, fmt.Errorf("CIDR %q must be within a private address range", raw) + } + if dnsguard.ContainsMetadataAddress(prefix) { + return nil, fmt.Errorf("CIDR %q includes a metadata address", raw) + } + prefixes = append(prefixes, prefix) + } + return prefixes, nil +} + +func prefixWithin(prefix netip.Prefix, networks []netip.Prefix) bool { + for _, network := range networks { + if prefix.Bits() >= network.Bits() && network.Contains(prefix.Addr()) { + return true + } + } + return false +} + +func prefixContains(prefixes []netip.Prefix, addr netip.Addr) bool { + for _, prefix := range prefixes { + if prefix.Contains(addr) { + return true + } + } + return false +} + +func newHTTPTransport(dialer *net.Dialer) *http.Transport { + return &http.Transport{ + TLSClientConfig: &tls.Config{ + MinVersion: tls.VersionTLS12, + }, + DialContext: dialer.DialContext, + ForceAttemptHTTP2: true, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + } +} + +func loopbackOnlyDialControl(_ string, address string, _ syscall.RawConn) error { + host, _, err := net.SplitHostPort(address) + if err != nil { + host = address + } + addr, err := netip.ParseAddr(host) + if err != nil || !addr.Unmap().IsLoopback() { + return fmt.Errorf("response retry loopback endpoint resolved to non-loopback address %q", host) + } + return nil +} + +func selectedHeaders(headers http.Header, allowed map[string]struct{}) http.Header { + result := make(http.Header) + for name := range allowed { + if values := headers.Values(name); len(values) > 0 { + result[name] = append([]string(nil), values...) + } + } + return result +} + +func isLoopback(host string) bool { + if strings.EqualFold(host, "localhost") { + return true + } + address, err := netip.ParseAddr(host) + return err == nil && address.Unmap().IsLoopback() +} + +func validTraceparent(value string) bool { + parts := strings.Split(value, "-") + if len(parts) != 4 || parts[0] != "00" || len(parts[1]) != 32 || len(parts[2]) != 16 || len(parts[3]) != 2 { + return false + } + traceID, traceErr := hex.DecodeString(parts[1]) + spanID, spanErr := hex.DecodeString(parts[2]) + _, flagsErr := hex.DecodeString(parts[3]) + return traceErr == nil && spanErr == nil && flagsErr == nil && + !allZero(traceID) && !allZero(spanID) +} + +func allZero(value []byte) bool { + for _, current := range value { + if current != 0 { + return false + } + } + return true +} diff --git a/internal/responseretry/handler_test.go b/internal/responseretry/handler_test.go new file mode 100644 index 0000000..6dbe640 --- /dev/null +++ b/internal/responseretry/handler_test.go @@ -0,0 +1,721 @@ +package responseretry + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "net/http" + "net/http/httptest" + "net/netip" + "net/url" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/stretchr/testify/require" +) + +const testAttemptID = "8ace71a1-4e12-47e5-9df4-f2f660db6a82" + +type roundTripFunc func(*http.Request) (*http.Response, error) + +func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { + return f(req) +} + +func testOptions(endpoint string, client *http.Client) Options { + return Options{ + AuthorizeEndpoint: endpoint, + CompleteEndpoint: endpoint, + Token: "proxy-token", + SandboxID: "sandbox-1", + Statuses: []int{http.StatusPaymentRequired}, + CompletionHeaders: []string{"Payment-Receipt"}, + Client: client, + } +} + +func TestHandlerAuthorizeAndComplete(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, "Bearer proxy-token", r.Header.Get("Authorization")) + var request DecisionRequest + require.NoError(t, json.NewDecoder(r.Body).Decode(&request)) + require.Equal(t, "https", request.Scheme) + require.Equal(t, "service.example", request.Host) + require.Equal(t, "/v1/search?q=one", request.Path) + require.Equal(t, "sandbox-1", request.SandboxID) + require.Equal(t, "00-trace-span-01", request.Traceparent) + require.True(t, request.Replayable) + w.Header().Set("Content-Type", "application/json") + _, err := w.Write([]byte(`{"retry":true,"attempt_id":"` + testAttemptID + `","traceparent":"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01","headers":{"Authorization":"credential"}}`)) + require.NoError(t, err) + }) + mux.HandleFunc("/complete", func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, "Bearer proxy-token", r.Header.Get("Authorization")) + var request CompletionRequest + require.NoError(t, json.NewDecoder(r.Body).Decode(&request)) + require.Equal(t, testAttemptID, request.AttemptID) + require.NotNil(t, request.ReplayStatus) + require.Equal(t, http.StatusOK, *request.ReplayStatus) + require.Equal(t, []string{"receipt"}, request.ResponseHeaders["Payment-Receipt"]) + require.Empty(t, request.ResponseHeaders["Set-Cookie"]) + require.Equal(t, "00-trace-span-01", request.Traceparent) + require.EqualValues(t, 25, request.ReplayDurationMS) + require.EqualValues(t, 50, request.ChargeDurationMS) + w.WriteHeader(http.StatusOK) + }) + server := httptest.NewServer(mux) + defer server.Close() + opts := testOptions(server.URL+"/authorize", server.Client()) + opts.CompleteEndpoint = server.URL + "/complete" + handler, err := New(opts) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/v1/search?q=one", nil) + require.NoError(t, err) + req.Header.Set("Traceparent", "00-trace-span-01") + resp := &http.Response{ + StatusCode: http.StatusPaymentRequired, + Header: http.Header{"Www-Authenticate": {"Payment challenge"}}, + } + + decision, retry, err := handler.Decide(context.Background(), req, resp, true) + + require.NoError(t, err) + require.True(t, retry) + require.Equal(t, "credential", decision.Headers.Get("Authorization")) + require.Equal(t, testAttemptID, decision.AttemptID) + require.Equal(t, "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", decision.Traceparent) + + replayResp := &http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{ + "Payment-Receipt": {"receipt"}, + "Set-Cookie": {"secret"}, + }, + } + require.NoError(t, handler.Complete( + context.Background(), + decision.AttemptID, + replayResp, + "", + "00-trace-span-01", + 25*time.Millisecond, + 50*time.Millisecond, + )) +} + +func TestHandlerReportsExactUpstreamScheme(t *testing.T) { + cases := []struct { + name string + target string + want string + }{ + {name: "HTTPS", target: "https://service.example/paid", want: "https"}, + {name: "HTTP", target: "http://service.example/paid", want: "http"}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var request DecisionRequest + require.NoError(t, json.NewDecoder(r.Body).Decode(&request)) + require.Equal(t, tc.want, request.Scheme) + _, err := w.Write([]byte(`{"retry":false,"headers":{}}`)) + require.NoError(t, err) + })) + defer server.Close() + handler, err := New(testOptions(server.URL, server.Client())) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, tc.target, nil) + require.NoError(t, err) + + decision, retry, err := handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusPaymentRequired}, true) + + require.NoError(t, err) + require.False(t, retry) + require.Nil(t, decision) + }) + } +} + +func TestHandlerSkipsUnconfiguredStatus(t *testing.T) { + opts := testOptions("http://127.0.0.1/authorize", nil) + opts.CompleteEndpoint = "http://127.0.0.1/complete" + handler, err := New(opts) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/", nil) + require.NoError(t, err) + + decision, retry, err := handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusTooManyRequests}, true) + + require.NoError(t, err) + require.False(t, retry) + require.Nil(t, decision) +} + +func TestHandlerRejectsForbiddenReplayHeader(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, err := w.Write([]byte(`{"retry":true,"attempt_id":"` + testAttemptID + `","headers":{"Host":"other.example"}}`)) + require.NoError(t, err) + })) + defer server.Close() + handler, err := New(testOptions(server.URL, server.Client())) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/", nil) + require.NoError(t, err) + + _, _, err = handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusPaymentRequired}, true) + + require.ErrorContains(t, err, "forbidden header Host") +} + +func TestHandlerRejectsAuthorizedNonReplayableRequest(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, err := w.Write([]byte(`{"retry":true,"attempt_id":"` + testAttemptID + `","headers":{"Authorization":"credential"}}`)) + require.NoError(t, err) + })) + defer server.Close() + handler, err := New(testOptions(server.URL, server.Client())) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/", nil) + require.NoError(t, err) + + _, _, err = handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusPaymentRequired}, false) + + require.ErrorContains(t, err, "non-replayable") +} + +func TestHandlerRejectsInvalidReturnedTraceparent(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, err := w.Write([]byte(`{"retry":true,"attempt_id":"` + testAttemptID + `","traceparent":"invalid","headers":{"Authorization":"credential"}}`)) + require.NoError(t, err) + })) + defer server.Close() + handler, err := New(testOptions(server.URL, server.Client())) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/", nil) + require.NoError(t, err) + + _, _, err = handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusPaymentRequired}, true) + + require.ErrorContains(t, err, "invalid traceparent") +} + +func TestNewValidatesConfiguration(t *testing.T) { + cases := []struct { + name string + mutate func(*Options) + want string + }{ + { + name: "plaintext non-loopback", + mutate: func(opts *Options) { + opts.AuthorizeEndpoint = "http://127.attacker.example/authorize" + }, + want: "must use HTTPS", + }, + { + name: "URL credentials", + mutate: func(opts *Options) { + opts.AuthorizeEndpoint = "https://user:password@handler.example/authorize" + }, + want: "without credentials", + }, + { + name: "URL fragment", + mutate: func(opts *Options) { + opts.AuthorizeEndpoint = "https://handler.example/authorize#fragment" + }, + want: "must not contain a fragment", + }, + { + name: "missing token", + mutate: func(opts *Options) { + opts.Token = "" + }, + want: "token is required", + }, + { + name: "missing sandbox", + mutate: func(opts *Options) { + opts.SandboxID = "" + }, + want: "sandbox identity is required", + }, + { + name: "missing statuses", + mutate: func(opts *Options) { + opts.Statuses = nil + }, + want: "at least one", + }, + { + name: "invalid status", + mutate: func(opts *Options) { + opts.Statuses = []int{99} + }, + want: "invalid response retry status", + }, + { + name: "invalid completion header", + mutate: func(opts *Options) { + opts.CompletionHeaders = []string{"Bad Header"} + }, + want: "invalid completion response header", + }, + { + name: "allow address without prefix", + mutate: func(opts *Options) { + opts.AllowCIDRs = []string{"10.43.0.1"} + }, + want: "must use CIDR notation", + }, + { + name: "malformed allow CIDR", + mutate: func(opts *Options) { + opts.AllowCIDRs = []string{"10.43.0.0/99"} + }, + want: "invalid CIDR", + }, + { + name: "public allow CIDR", + mutate: func(opts *Options) { + opts.AllowCIDRs = []string{"203.0.113.0/24"} + }, + want: "must be within a private address range", + }, + { + name: "link-local allow CIDR", + mutate: func(opts *Options) { + opts.AllowCIDRs = []string{"169.254.0.0/16"} + }, + want: "must be within a private address range", + }, + { + name: "AWS IPv6 metadata allow CIDR", + mutate: func(opts *Options) { + opts.AllowCIDRs = []string{"fd00::/8"} + }, + want: "includes a metadata address", + }, + { + name: "GCP IPv6 metadata allow CIDR", + mutate: func(opts *Options) { + opts.AllowCIDRs = []string{"fd20:ce::/64"} + }, + want: "includes a metadata address", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + opts := testOptions("https://handler.example/authorize", nil) + opts.CompleteEndpoint = "https://handler.example/complete" + tc.mutate(&opts) + _, err := New(opts) + require.ErrorContains(t, err, tc.want) + }) + } +} + +func TestNewAllowsOnlyActualLoopbackHTTP(t *testing.T) { + for _, endpoint := range []string{ + "http://localhost/authorize", + "http://127.0.0.1/authorize", + "http://127.255.255.255/authorize", + "http://[::1]/authorize", + "http://[::ffff:127.0.0.1]/authorize", + } { + t.Run(endpoint, func(t *testing.T) { + opts := testOptions(endpoint, nil) + opts.CompleteEndpoint = endpoint + _, err := New(opts) + require.NoError(t, err) + }) + } +} + +func TestHandlerRejectsRedirectsWithoutForwardingAuthorization(t *testing.T) { + var redirectedCalls atomic.Int64 + target := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + redirectedCalls.Add(1) + require.Empty(t, r.Header.Get("Authorization")) + })) + defer target.Close() + redirector := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Location", target.URL) + w.WriteHeader(http.StatusFound) + })) + defer redirector.Close() + + handler, err := New(testOptions(redirector.URL, redirector.Client())) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/", nil) + require.NoError(t, err) + + _, _, err = handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusPaymentRequired}, true) + + require.ErrorContains(t, err, "status 302") + require.Zero(t, redirectedCalls.Load()) +} + +func TestHandlerValidatesAllReturnedHeaders(t *testing.T) { + cases := []struct { + name string + headers map[string]string + want string + }{ + {name: "invalid name", headers: map[string]string{"Bad Header": "value"}, want: "invalid header name"}, + {name: "invalid value", headers: map[string]string{"X-Test": "bad\nvalue"}, want: "invalid value"}, + {name: "duplicate canonical name", headers: map[string]string{"X-Test": "one", "x-test": "two"}, want: "duplicate header"}, + } + for name := range forbiddenRetryHeaders { + cases = append(cases, struct { + name string + headers map[string]string + want string + }{name: "forbidden " + name, headers: map[string]string{name: "value"}, want: "forbidden header"}) + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + payload, err := json.Marshal(DecisionResponse{ + Retry: true, + AttemptID: testAttemptID, + Headers: tc.headers, + }) + require.NoError(t, err) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, writeErr := w.Write(payload) + require.NoError(t, writeErr) + })) + defer server.Close() + handler, err := New(testOptions(server.URL, server.Client())) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/", nil) + require.NoError(t, err) + + _, _, err = handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusPaymentRequired}, true) + + require.ErrorContains(t, err, tc.want) + }) + } +} + +func TestHandlerDecisionFailureModes(t *testing.T) { + cases := []struct { + name string + status int + body string + wantRetry bool + wantErr string + }{ + {name: "declined", status: http.StatusOK, body: `{"retry":false}`, wantRetry: false}, + {name: "handler rejection", status: http.StatusUnauthorized, body: `{}`, wantErr: "status 401"}, + {name: "malformed JSON", status: http.StatusOK, body: `{`, wantErr: "decode response retry decision"}, + {name: "missing attempt", status: http.StatusOK, body: `{"retry":true}`, wantErr: "omitted attempt id"}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(tc.status) + _, err := io.WriteString(w, tc.body) + require.NoError(t, err) + })) + defer server.Close() + handler, err := New(testOptions(server.URL, server.Client())) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/", nil) + require.NoError(t, err) + + decision, retry, err := handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusPaymentRequired}, true) + + require.Equal(t, tc.wantRetry, retry) + if tc.wantErr == "" { + require.NoError(t, err) + require.Nil(t, decision) + } else { + require.ErrorContains(t, err, tc.wantErr) + } + }) + } +} + +func TestHandlerCompleteRetriesServerFailureAndSelectsConfiguredHeaders(t *testing.T) { + var calls atomic.Int64 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + call := calls.Add(1) + var request CompletionRequest + require.NoError(t, json.NewDecoder(r.Body).Decode(&request)) + require.Equal(t, []string{"selected"}, request.ResponseHeaders["X-Selected"]) + require.Empty(t, request.ResponseHeaders["Payment-Receipt"]) + if call == 1 { + http.Error(w, "retry", http.StatusServiceUnavailable) + return + } + w.WriteHeader(http.StatusNoContent) + })) + defer server.Close() + opts := testOptions(server.URL, server.Client()) + opts.CompletionHeaders = []string{"X-Selected"} + handler, err := New(opts) + require.NoError(t, err) + + err = handler.Complete( + context.Background(), + testAttemptID, + &http.Response{StatusCode: http.StatusOK, Header: http.Header{ + "X-Selected": {"selected"}, + "Payment-Receipt": {"not-selected"}, + }}, + "", + "", + 0, + 0, + ) + + require.NoError(t, err) + require.EqualValues(t, 2, calls.Load()) +} + +func TestHandlerCompleteDoesNotRetryClientFailure(t *testing.T) { + var calls atomic.Int64 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + calls.Add(1) + http.Error(w, "invalid", http.StatusBadRequest) + })) + defer server.Close() + handler, err := New(testOptions(server.URL, server.Client())) + require.NoError(t, err) + + err = handler.Complete(context.Background(), testAttemptID, nil, "upstream_transport_error", "", 0, 0) + + require.ErrorContains(t, err, "status 400") + require.EqualValues(t, 1, calls.Load()) +} + +func TestHardenedClientDoesNotMutateInputClient(t *testing.T) { + originalRedirect := func(_ *http.Request, _ []*http.Request) error { return fmt.Errorf("original") } + client := &http.Client{CheckRedirect: originalRedirect} + hardened := hardenedClient(client, nil, nil, 0, nil, nil) + + require.NotSame(t, client, hardened) + require.Zero(t, client.Timeout) + require.Equal(t, defaultClientTimeout, hardened.Timeout) + require.ErrorContains(t, client.CheckRedirect(nil, nil), "original") + require.ErrorIs(t, hardened.CheckRedirect(nil, nil), http.ErrUseLastResponse) +} + +func TestHandlerClientUsesConfiguredResolver(t *testing.T) { + resolverErr := errors.New("configured resolver used") + resolver := &net.Resolver{ + PreferGo: true, + Dial: func(context.Context, string, string) (net.Conn, error) { + return nil, resolverErr + }, + } + opts := testOptions("http://handler.invalid/authorize", nil) + opts.AllowHTTP = true + opts.Resolver = resolver + handler, err := New(opts) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/", nil) + require.NoError(t, err) + + _, _, err = handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusPaymentRequired}, true) + + require.ErrorContains(t, err, resolverErr.Error()) +} + +func TestHandlerClientEnforcesUpstreamDenyGuard(t *testing.T) { + guard, err := dnsguard.New([]string{"169.254.169.254/32"}) + require.NoError(t, err) + opts := testOptions("http://169.254.169.254/authorize", nil) + opts.AllowHTTP = true + opts.Guard = guard + handler, err := New(opts) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/", nil) + require.NoError(t, err) + + _, _, err = handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusPaymentRequired}, true) + + require.Error(t, err) + require.True(t, dnsguard.IsDenyError(err)) +} + +func TestHandlerClientAllowsExplicitLoopbackDespiteGuard(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, err := io.WriteString(w, `{"retry":false}`) + require.NoError(t, err) + })) + defer server.Close() + guard, err := dnsguard.New([]string{"127.0.0.0/8", "::1/128"}) + require.NoError(t, err) + opts := testOptions(server.URL, nil) + opts.Guard = guard + handler, err := New(opts) + require.NoError(t, err) + req, err := http.NewRequest(http.MethodGet, "https://service.example/", nil) + require.NoError(t, err) + + decision, retry, err := handler.Decide(context.Background(), req, &http.Response{StatusCode: http.StatusPaymentRequired}, true) + + require.NoError(t, err) + require.False(t, retry) + require.Nil(t, decision) +} + +func TestParsePrivateCIDRs(t *testing.T) { + cases := []struct { + name string + value string + want netip.Prefix + }{ + {name: "ten", value: "10.43.0.1/16", want: netip.MustParsePrefix("10.43.0.0/16")}, + {name: "172", value: "172.20.0.0/16", want: netip.MustParsePrefix("172.20.0.0/16")}, + {name: "192", value: "192.168.10.0/24", want: netip.MustParsePrefix("192.168.10.0/24")}, + {name: "IPv6 ULA", value: "fd12:3456::/48", want: netip.MustParsePrefix("fd12:3456::/48")}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + prefixes, err := parsePrivateCIDRs([]string{tc.value}) + require.NoError(t, err) + require.Equal(t, []netip.Prefix{tc.want}, prefixes) + }) + } +} + +func TestHandlerDialControlAllowsOnlyConfiguredPrivateCIDRs(t *testing.T) { + guard, err := dnsguard.New([]string{ + "10.42.0.0/16", + "10.43.0.0/16", + "169.254.0.0/16", + "203.0.113.0/24", + "fd00::/8", + }) + require.NoError(t, err) + allowCIDRs, err := parsePrivateCIDRs([]string{"10.43.0.0/16", "fd12:3456::/48"}) + require.NoError(t, err) + control := handlerDialControl(guard, allowCIDRs) + + cases := []struct { + name string + address string + allowed bool + }{ + {name: "configured service CIDR", address: "10.43.5.10:8090", allowed: true}, + {name: "mapped configured service CIDR", address: "[::ffff:10.43.5.10]:8090", allowed: true}, + {name: "configured IPv6 ULA", address: "[fd12:3456::10]:8090", allowed: true}, + {name: "ordinary allowed address", address: "8.8.8.8:443", allowed: true}, + {name: "malformed address deferred to dialer", address: "not-an-address", allowed: true}, + {name: "different private CIDR", address: "10.42.5.10:8090", allowed: false}, + {name: "IPv4 metadata", address: "169.254.169.254:80", allowed: false}, + {name: "IPv6 metadata", address: "[fd00:ec2::254]:80", allowed: false}, + {name: "denied public CIDR", address: "203.0.113.10:443", allowed: false}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + err := control("tcp", tc.address, nil) + if tc.allowed { + require.NoError(t, err) + } else { + require.Error(t, err) + require.True(t, dnsguard.IsDenyError(err)) + } + }) + } + + err = guard.DialControl("tcp", "10.43.5.10:8090", nil) + require.Error(t, err) + require.True(t, dnsguard.IsDenyError(err), "the handler exception must not modify the shared guard") +} + +func TestHandlerTransportAllowsOnlyConfiguredEndpoints(t *testing.T) { + authorizeEndpoint, err := url.Parse("https://handler.internal/authorize?realm=payments") + require.NoError(t, err) + completeEndpoint, err := url.Parse("https://handler.internal/complete") + require.NoError(t, err) + + cases := []struct { + name string + method string + request string + allowed bool + }{ + {name: "authorize", request: "https://handler.internal/authorize?realm=payments", allowed: true}, + {name: "complete", request: "https://handler.internal/complete", allowed: true}, + {name: "explicit default port", request: "https://handler.internal:443/complete", allowed: true}, + {name: "different authority", request: "https://other.internal/authorize?realm=payments", allowed: false}, + {name: "different port", request: "https://handler.internal:8443/complete", allowed: false}, + {name: "downgraded scheme", request: "http://handler.internal/complete", allowed: false}, + {name: "different path", request: "https://handler.internal/admin", allowed: false}, + {name: "missing query", request: "https://handler.internal/authorize", allowed: false}, + {name: "different query", request: "https://handler.internal/authorize?realm=other", allowed: false}, + {name: "different method", method: http.MethodGet, request: "https://handler.internal/complete", allowed: false}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var calls atomic.Int64 + roundTripper := roundTripFunc(func(*http.Request) (*http.Response, error) { + calls.Add(1) + return &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader("")), + }, nil + }) + transport := &handlerTransport{ + guarded: roundTripper, + loopback: roundTripper, + allowedEndpoints: map[string]struct{}{ + handlerEndpointKey(authorizeEndpoint): {}, + handlerEndpointKey(completeEndpoint): {}, + }, + } + method := tc.method + if method == "" { + method = http.MethodPost + } + req, err := http.NewRequest(method, tc.request, nil) + require.NoError(t, err) + + resp, err := transport.RoundTrip(req) + if tc.allowed { + require.NoError(t, err) + require.NotNil(t, resp) + require.NoError(t, resp.Body.Close()) + require.EqualValues(t, 1, calls.Load()) + } else { + require.ErrorContains(t, err, "unconfigured request") + require.Nil(t, resp) + require.Zero(t, calls.Load()) + } + }) + } +} + +func TestSelectedHeadersIsCaseInsensitive(t *testing.T) { + headers := make(http.Header) + headers.Add("x-receipt", "one") + headers.Add("X-Receipt", "two") + headers.Add("Set-Cookie", "secret") + + selected := selectedHeaders(headers, map[string]struct{}{"X-Receipt": {}}) + + require.Equal(t, []string{"one", "two"}, selected.Values("X-Receipt")) + require.Empty(t, selected.Values("Set-Cookie")) +} + +func TestValidTraceparentRejectsAllZeroFlagsShape(t *testing.T) { + require.False(t, validTraceparent(strings.Repeat("0", 55))) +} diff --git a/internal/sshproxy/README.md b/internal/sshproxy/README.md new file mode 100644 index 0000000..08740af --- /dev/null +++ b/internal/sshproxy/README.md @@ -0,0 +1,55 @@ +# Iron SSH listener + +SSH runs as an optional listener inside the main `iron-proxy` process. Build and +test it with the normal repository commands; no separate executable or module +is needed. + +Configure a top-level `ssh:` block in the regular YAML file, or set +`IRON_SSH_CONFIG_JSON` to the equivalent JSON object for managed deployments. +SSH is disabled when configuration is absent. Pass `--ssh` to require a configured +listener at startup. Listener settings apply at startup. + +The SSH configuration contains `listen`, `host_key` (an existing Iron +secret source), `client_public_key` (registered by the trusted control plane), +`authorize_url`, and `token_env`. The environment variable named by `token_env` +is a proxy-only bearer token. Configuration and host keys must never be mounted +in the sandbox. Upstream dials honor the proxy-wide `proxy.upstream_deny_cidrs`. +Connections, authorization decisions, and session requests emit the standard +audit entries (JSON and OTEL) with `mode: ssh`. + +The authorizer receives `{route, public_key}` and an optional `destination` for +per-call targets. A client names these using SSH username +`ee:`; the proxy decodes this to route `ee` and passes +the URL to the authorizer. This is only a request: the authorizer must enforce +deployment-owned host/path policy and must never infer a user from this input. +It must derive the principal from +the proxy token and return a grant containing `id`, `principal`, `generation`, +`destination` (`tcp://host:port` or a full `wss://host/path`), `username`, +`host_key` (authorized-key format), `private_key` (Iron secret source), and +`expires_at` (RFC3339, at most 15 minutes away). It must deny ambiguous or +unauthorized routes with HTTP 401 or 403; other non-200 statuses are audited as +authorizer errors. Responses are limited to 1 MiB; errors never expose their +bodies or credential values. Redirects are not followed. + +The client must prove possession of the paired key before authorization or +any upstream dial. Exec and SFTP channels are supported; forwarding is blocked. +Global requests are refused locally, and environment/end-of-write requests are +discarded. Other channel types and requests are refused. Grants normally require a pinned SSH host key and verified WSS TLS. +An authorizer may explicitly return `tls_identity: true` with an empty +`host_key` for deployment-allowed WSS gateways. This authenticates the exact +destination using the TLS certificate and hostname, allowing the SSH key inside +that authenticated tunnel to rotate. It is rejected for plain TCP or combined +with a host pin. TLS verification, redirect rejection, IP guards, disposable +client keys, and per-request authorization remain mandatory. WSS implements +the wstunnel v11 default unmasked framing and routing handshake with a fixed remote target `127.0.0.1:22`. +The JWT is protocol metadata, not an authentication credential. + +The lease is renewed every five seconds. Expiry, authorization failure, or a +change to any grant field except expiry closes the active connection. Authorizer +failures fail closed. Both peers are probed with keepalives every 15 seconds, +and a peer that misses three intervals closes the connection. Reconnection never retries a remote command automatically. +A terminated channel does not guarantee detached remote descendants have exited. + +Run `go test -race ./internal/sshproxy`. No SSH functionality is enabled merely +by installing this binary; the embedding platform must supply the paired key, +authenticated authorizer, destination grants, and lifecycle wiring. diff --git a/internal/sshproxy/config.go b/internal/sshproxy/config.go new file mode 100644 index 0000000..4567e6e --- /dev/null +++ b/internal/sshproxy/config.go @@ -0,0 +1,143 @@ +// Package sshproxy implements an SSH credential boundary within iron-proxy. +package sshproxy + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "log/slog" + "net" + "net/http" + "os" + "time" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/ironsh/iron-proxy/internal/transform" + "github.com/ironsh/iron-proxy/internal/transform/secrets" + "golang.org/x/crypto/ssh" + "gopkg.in/yaml.v3" +) + +// Grant is delivered only over the authenticated proxy control channel. +type Grant struct { + ID string `json:"id" yaml:"id"` + Principal string `json:"principal" yaml:"principal"` + Destination string `json:"destination" yaml:"destination"` + Generation string `json:"generation" yaml:"generation"` + Username string `json:"username" yaml:"username"` + HostKey string `json:"host_key" yaml:"host_key"` + TLSIdentity bool `json:"tls_identity,omitempty" yaml:"tls_identity"` + PrivateKey yaml.Node `json:"-" yaml:"private_key"` + ExpiresAt time.Time `json:"expires_at" yaml:"expires_at"` +} + +// Authorization identifies a sandbox key and a configured route, never a user. +type Authorization struct { + Route string `json:"route"` + PublicKey string `json:"public_key"` + Destination string `json:"destination,omitempty"` +} + +// errAccessDenied marks policy rejections, as opposed to operational errors. +var errAccessDenied = errors.New("SSH access denied") + +// Authorizer is called again during active connections; errors revoke access. +type Authorizer func(context.Context, Authorization) (*Grant, error) + +// HTTPAuthorizer uses a proxy-only bearer token and does not follow redirects. +func HTTPAuthorizer(endpoint, token string) Authorizer { + client := &http.Client{Timeout: 5 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} + return func(ctx context.Context, a Authorization) (*Grant, error) { + body, err := json.Marshal(a) + if err != nil { + return nil, errors.New("authorization encoding failed") + } + req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body)) + if err != nil { + return nil, errors.New("authorization URL invalid") + } + req.Header.Set("Authorization", "Bearer "+token) + req.Header.Set("Content-Type", "application/json") + resp, err := client.Do(req) + if err != nil { + return nil, errors.New("authorization unavailable") + } + defer resp.Body.Close() + switch resp.StatusCode { + case http.StatusOK: + case http.StatusUnauthorized, http.StatusForbidden: + return nil, errAccessDenied + default: + // Outages are operational errors, not policy denials. + return nil, fmt.Errorf("authorization unavailable: status %d", resp.StatusCode) + } + // JSON is decoded as YAML so source nodes retain the existing resolver contract. + data, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) + if err != nil { + return nil, errors.New("authorization response unavailable") + } + var g Grant + if yaml.Unmarshal(data, &g) != nil { + return nil, errors.New("invalid authorization response") + } + return &g, nil + } +} + +func signer(ctx context.Context, node yaml.Node, logger *slog.Logger) (ssh.Signer, error) { + source, err := secrets.BuildSource(node, logger) + if err != nil { + return nil, errors.New("SSH credential source invalid") + } + value, err := source.Get(ctx) + if err != nil { + return nil, errors.New("SSH credential unavailable") + } + key, err := ssh.ParsePrivateKey([]byte(value)) + if err != nil { + return nil, errors.New("SSH credential invalid") + } + return key, nil +} + +// LoadFromNode prepares the optional SSH listener before other services start. +// An absent block disables SSH. The real key and bearer token stay in the proxy. +// Upstream dials use the proxy-wide guard and audit events the shared sinks. +func LoadFromNode(ctx context.Context, node yaml.Node, guard *dnsguard.Guard, audit transform.AuditFunc, logger *slog.Logger) (*Server, net.Listener, error) { + if node.Kind == 0 || node.Tag == "!!null" { + return nil, nil, nil + } + var cfg struct { + Listen string `yaml:"listen"` + HostKey yaml.Node `yaml:"host_key"` + ClientPublicKey string `yaml:"client_public_key"` + AuthorizeURL string `yaml:"authorize_url"` + TokenEnv string `yaml:"token_env"` + } + if node.Decode(&cfg) != nil || cfg.Listen == "" || cfg.AuthorizeURL == "" { + return nil, nil, errors.New("SSH listen address and authorization URL required") + } + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + host, err := ParseHostKey(ctx, cfg.HostKey, logger) + if err != nil { + return nil, nil, err + } + key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(cfg.ClientPublicKey)) + if err != nil { + return nil, nil, errors.New("sandbox SSH public key invalid") + } + token := os.Getenv(cfg.TokenEnv) + if token == "" { + return nil, nil, errors.New("SSH proxy token unavailable") + } + listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", cfg.Listen) + if err != nil { + return nil, nil, errors.New("SSH listener unavailable") + } + server := &Server{HostKey: host, ClientPublicKey: key, Authorize: HTTPAuthorizer(cfg.AuthorizeURL, token), Guard: guard, Audit: audit, Logger: logger} + return server, listener, nil +} diff --git a/internal/sshproxy/config_test.go b/internal/sshproxy/config_test.go new file mode 100644 index 0000000..92a3aa2 --- /dev/null +++ b/internal/sshproxy/config_test.go @@ -0,0 +1,58 @@ +package sshproxy + +import ( + "context" + "encoding/json" + "io" + "log/slog" + "testing" + "time" + + "github.com/stretchr/testify/require" + "golang.org/x/crypto/ssh" + "gopkg.in/yaml.v3" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/ironsh/iron-proxy/internal/transform" +) + +func noAudit(*transform.PipelineResult) {} + +func TestOptionalListenerLifecycle(t *testing.T) { + logger := slog.New(slog.NewJSONHandler(io.Discard, nil)) + server, listener, err := LoadFromNode(context.Background(), yaml.Node{}, &dnsguard.Guard{}, noAudit, logger) + require.NoError(t, err) + require.Nil(t, server) + require.Nil(t, listener) + + _, private := key(t) + client, _ := key(t) + t.Setenv("SSH_STARTUP_TEST_TOKEN", "synthetic-token") + data, err := json.Marshal(map[string]any{ + "listen": "127.0.0.1:0", "host_key": map[string]string{"type": "control_plane", "value": string(private)}, + "client_public_key": string(ssh.MarshalAuthorizedKey(client.PublicKey())), "authorize_url": "http://127.0.0.1/authorize", "token_env": "SSH_STARTUP_TEST_TOKEN", + }) + require.NoError(t, err) + var node yaml.Node + require.NoError(t, yaml.Unmarshal(data, &node)) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + server, listener, err = LoadFromNode(ctx, node, &dnsguard.Guard{}, noAudit, logger) + require.NoError(t, err) + defer listener.Close() + done := make(chan error, 1) + go func() { done <- server.Serve(ctx, listener) }() + cancel() + select { + case err := <-done: + require.NoError(t, err) + case <-time.After(time.Second): + t.Fatal("listener did not stop with the proxy context") + } + + t.Setenv("SSH_STARTUP_TEST_TOKEN", "") + server, listener, err = LoadFromNode(context.Background(), node, &dnsguard.Guard{}, noAudit, logger) + require.ErrorContains(t, err, "token unavailable") + require.Nil(t, server) + require.Nil(t, listener) +} diff --git a/internal/sshproxy/lifecycle_test.go b/internal/sshproxy/lifecycle_test.go new file mode 100644 index 0000000..88aba00 --- /dev/null +++ b/internal/sshproxy/lifecycle_test.go @@ -0,0 +1,123 @@ +package sshproxy + +import ( + "context" + "io" + "log/slog" + "net" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/require" + "golang.org/x/crypto/ssh" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/ironsh/iron-proxy/internal/transform" +) + +func startHandshake(t *testing.T, server *Server, clientKey ssh.Signer) (context.CancelFunc, <-chan struct{}) { + t.Helper() + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + client, err := net.Dial("tcp", listener.Addr().String()) + require.NoError(t, err) + downstream, err := listener.Accept() + require.NoError(t, err) + require.NoError(t, listener.Close()) + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(func() { cancel(); _ = client.Close(); _ = downstream.Close() }) + done := make(chan struct{}) + go func() { defer close(done); server.handle(ctx, downstream) }() + go func() { + // These fixtures intentionally fail or cancel before authentication completes. + conn, _, _, err := ssh.NewClientConn(client, "proxy", &ssh.ClientConfig{User: "ee", Auth: []ssh.AuthMethod{ssh.PublicKeys(clientKey)}, HostKeyCallback: ssh.FixedHostKey(server.HostKey.PublicKey())}) + if err == nil { + _ = conn.Close() + } + }() + return cancel, done +} + +func TestHandshakeDeadlineIncludesWSSNegotiation(t *testing.T) { + for _, stage := range []string{"TLS", "HTTP upgrade"} { + t.Run(stage, func(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + backendHost, private := key(t) + g := grant(t, "unused:22", backendHost, private) + reached := make(chan struct{}) + server := &Server{HostKey: proxyHost, ClientPublicKey: clientKey.PublicKey(), Authorize: func(context.Context, Authorization) (*Grant, error) { return g, nil }, Guard: &dnsguard.Guard{}, Audit: func(*transform.PipelineResult) {}, Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)), HandshakeTimeout: 500 * time.Millisecond} + if stage == "TLS" { + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { _ = listener.Close() }) + go func() { + conn, err := listener.Accept() + if err != nil { + return + } + defer conn.Close() + close(reached) + _, _ = io.Copy(io.Discard, conn) + }() + g.Destination = "wss://" + listener.Addr().String() + "/events/events" + } else { + release := make(chan struct{}) + peer := httptest.NewTLSServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { close(reached); <-release })) + t.Cleanup(func() { close(release); peer.Close() }) + server.TLSRootCAs = peer.Client().Transport.(*http.Transport).TLSClientConfig.RootCAs + g.Destination = "wss" + strings.TrimPrefix(peer.URL, "https") + "/events/events" + } + _, done := startHandshake(t, server, clientKey) + select { + case <-reached: + case <-time.After(2 * time.Second): + t.Fatal("handshake never reached stalled peer") + } + select { + case <-done: + case <-time.After(2 * time.Second): + t.Fatal("WSS negotiation outlived the handshake deadline") + } + }) + } +} + +func TestCancellationClosesEstablishedUpstreamDuringSSHHandshake(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + backendHost, private := key(t) + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { _ = listener.Close() }) + reached, closed := make(chan struct{}), make(chan struct{}) + go func() { + conn, err := listener.Accept() + if err != nil { + return + } + defer conn.Close() + close(reached) + _, _ = io.Copy(io.Discard, conn) + close(closed) + }() + g := grant(t, listener.Addr().String(), backendHost, private) + server := &Server{HostKey: proxyHost, ClientPublicKey: clientKey.PublicKey(), Authorize: func(context.Context, Authorization) (*Grant, error) { return g, nil }, Guard: &dnsguard.Guard{}, Audit: func(*transform.PipelineResult) {}, Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)), HandshakeTimeout: time.Minute} + cancel, done := startHandshake(t, server, clientKey) + select { + case <-reached: + case <-time.After(2 * time.Second): + t.Fatal("upstream was not dialed") + } + cancel() + for _, event := range []<-chan struct{}{closed, done} { + select { + case <-event: + case <-time.After(2 * time.Second): + t.Fatal("cancellation did not close the established upstream and handler") + } + } +} diff --git a/internal/sshproxy/policy.go b/internal/sshproxy/policy.go new file mode 100644 index 0000000..d47c42f --- /dev/null +++ b/internal/sshproxy/policy.go @@ -0,0 +1,42 @@ +package sshproxy + +import ( + "errors" + + "golang.org/x/crypto/ssh" +) + +var errCapabilityDenied = errors.New("SSH capability denied") + +type requestDecision int + +const ( + forwardRequest requestDecision = iota + // dropRequest refuses client bookkeeping without auditing it. + dropRequest + refuseRequest +) + +// sessionRequestPolicy is the default-deny allowlist for session channel +// requests. It returns the decision and the audited detail. +func sessionRequestPolicy(typ string, payload []byte) (requestDecision, string) { + switch typ { + case "exec": + var p struct{ Command string } + if ssh.Unmarshal(payload, &p) != nil { + return refuseRequest, "" + } + return forwardRequest, p.Command + case "subsystem", "signal": + var p struct{ Name string } + if ssh.Unmarshal(payload, &p) != nil || (typ == "subsystem" && p.Name != "sftp") { + return refuseRequest, p.Name + } + return forwardRequest, p.Name + case "env", "eow@openssh.com": + // OpenSSH sends these routinely; environment injection stays blocked. + return dropRequest, "" + default: + return refuseRequest, "" + } +} diff --git a/internal/sshproxy/policy_test.go b/internal/sshproxy/policy_test.go new file mode 100644 index 0000000..e69aaf6 --- /dev/null +++ b/internal/sshproxy/policy_test.go @@ -0,0 +1,37 @@ +package sshproxy + +import ( + "testing" + + "github.com/stretchr/testify/require" + "golang.org/x/crypto/ssh" +) + +func TestSessionRequestPolicy(t *testing.T) { + name := func(s string) []byte { return ssh.Marshal(struct{ Name string }{s}) } + cases := []struct { + typ string + payload []byte + decision requestDecision + detail string + }{ + {"exec", ssh.Marshal(struct{ Command string }{"ls -la"}), forwardRequest, "ls -la"}, + {"exec", append(ssh.Marshal(struct{ Command string }{"ls"}), 0), refuseRequest, ""}, + {"subsystem", name("sftp"), forwardRequest, "sftp"}, + {"subsystem", name("netconf"), refuseRequest, "netconf"}, + {"signal", name("TERM"), forwardRequest, "TERM"}, + {"env", nil, dropRequest, ""}, + {"eow@openssh.com", nil, dropRequest, ""}, + {"shell", nil, refuseRequest, ""}, + {"pty-req", nil, refuseRequest, ""}, + {"x11-req", nil, refuseRequest, ""}, + {"auth-agent-req@openssh.com", nil, refuseRequest, ""}, + } + for _, tc := range cases { + t.Run(tc.typ+"/"+tc.detail, func(t *testing.T) { + decision, detail := sessionRequestPolicy(tc.typ, tc.payload) + require.Equal(t, tc.decision, decision) + require.Equal(t, tc.detail, detail) + }) + } +} diff --git a/internal/sshproxy/server.go b/internal/sshproxy/server.go new file mode 100644 index 0000000..d78045f --- /dev/null +++ b/internal/sshproxy/server.go @@ -0,0 +1,451 @@ +package sshproxy + +import ( + "bytes" + "context" + "crypto/x509" + "encoding/base64" + "errors" + "fmt" + "io" + "log/slog" + "net" + "net/url" + "reflect" + "strings" + "sync" + "time" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/ironsh/iron-proxy/internal/transform" + "golang.org/x/crypto/ssh" + "gopkg.in/yaml.v3" +) + +// Server accepts only its paired sandbox's disposable public key. +type Server struct { + HostKey ssh.Signer + ClientPublicKey ssh.PublicKey + Authorize Authorizer + Guard *dnsguard.Guard + Audit transform.AuditFunc + TLSRootCAs *x509.CertPool + Logger *slog.Logger + RenewalInterval time.Duration + HandshakeTimeout time.Duration + // KeepaliveInterval paces liveness probes to both peers; 0 means 15s. + KeepaliveInterval time.Duration +} + +// session is one authorized upstream connection. +type session struct { + auth Authorization + grant *Grant + conn ssh.Conn + chans <-chan ssh.NewChannel + reqs <-chan *ssh.Request +} + +func (s *Server) Serve(parent context.Context, listener net.Listener) error { + if s.HostKey == nil || s.ClientPublicKey == nil || s.Authorize == nil || s.Guard == nil || s.Audit == nil || s.Logger == nil { + return errors.New("incomplete SSH configuration") + } + ctx, cancel := context.WithCancel(parent) + var wg sync.WaitGroup + defer wg.Wait() + defer cancel() + stop := context.AfterFunc(ctx, func() { _ = listener.Close() }) + defer stop() + for { + conn, err := listener.Accept() + if err != nil { + if ctx.Err() != nil { + return nil + } + return err + } + wg.Add(1) + go func() { defer wg.Done(); s.handle(ctx, conn) }() + } +} + +func (s *Server) handle(parent context.Context, conn net.Conn) { + ctx, cancel := context.WithCancel(parent) + defer cancel() + defer conn.Close() + stop := context.AfterFunc(ctx, func() { _ = conn.Close() }) + defer stop() + started := time.Now() + remote := conn.RemoteAddr().String() + timeout := s.HandshakeTimeout + if timeout == 0 { + timeout = 15 * time.Second + } + handshakeCtx, handshakeCancel := context.WithTimeout(ctx, timeout) + defer handshakeCancel() + handshakeDeadline, _ := handshakeCtx.Deadline() + if conn.SetDeadline(handshakeDeadline) != nil { + return + } + var sess *session + defer func() { + if sess != nil { + _ = sess.conn.Close() + } + }() + attempted := false + config := &ssh.ServerConfig{ + // Also runs for unsigned key queries, so it must only compare keys. + PublicKeyCallback: func(_ ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { + if !bytes.Equal(key.Marshal(), s.ClientPublicKey.Marshal()) { + return nil, errAccessDenied + } + return nil, nil + }, + // Runs only after the client proves possession of the paired key. + VerifiedPublicKeyCallback: func(meta ssh.ConnMetadata, key ssh.PublicKey, _ *ssh.Permissions, _ string) (*ssh.Permissions, error) { + attempted = true + attemptStarted := time.Now() + next, err := s.connect(handshakeCtx, meta.User(), key, handshakeDeadline) + s.audit(attemptStarted, remote, next, "auth", next.auth.Route, "ssh_authorization", err) + if err != nil { + return nil, err + } + sess = next + return nil, nil + }, + } + config.AddHostKey(s.HostKey) + downstream, chans, reqs, err := ssh.NewServerConn(conn, config) + if err != nil { + if !attempted { + s.audit(started, remote, nil, "auth", "", "ssh_authorization", fmt.Errorf("%w: %v", errAccessDenied, err)) + } + return + } + defer func() { _ = downstream.Close() }() // teardown; the connection is usually already closed + if conn.SetDeadline(time.Time{}) != nil { + return + } + handshakeCancel() + stopUpstream := context.AfterFunc(ctx, func() { _ = sess.conn.Close() }) + defer stopUpstream() + + var wg sync.WaitGroup + run := func(f func()) { + wg.Add(1) + go func() { defer wg.Done(); f() }() + } + // Wait's error only says why upstream closed; either way the session ends. + run(func() { _ = sess.conn.Wait(); cancel() }) + run(func() { s.renew(ctx, cancel, sess.auth, sess.grant) }) + run(func() { s.keepalive(ctx, cancel, downstream) }) + run(func() { s.keepalive(ctx, cancel, sess.conn) }) + // Global requests (forwarding, keepalives) are refused in both directions. + run(func() { ssh.DiscardRequests(reqs) }) + run(func() { ssh.DiscardRequests(sess.reqs) }) + run(func() { + for nc := range sess.chans { + _ = nc.Reject(ssh.Prohibited, "upstream channels are not permitted") // peer is being refused; a failed send changes nothing + } + }) + for nc := range chans { + if nc.ChannelType() != "session" { + s.audit(time.Now(), remote, sess, nc.ChannelType(), "", "ssh_policy", errCapabilityDenied) + _ = nc.Reject(ssh.Prohibited, "only session channels are permitted") // as above + continue + } + run(func() { s.bridge(nc, sess, remote) }) + } + cancel() + _ = sess.conn.Close() + wg.Wait() +} + +// bridge pairs one downstream session channel with a new upstream one. +func (s *Server) bridge(nc ssh.NewChannel, sess *session, remote string) { + up, upReqs, err := sess.conn.OpenChannel("session", nc.ExtraData()) + if err != nil { + _ = nc.Reject(ssh.ConnectionFailed, "upstream session unavailable") // refusal is best effort + return + } + down, downReqs, err := nc.Accept() + if err != nil { + // Keep upstream requests from stalling the connection until it closes. + go ssh.DiscardRequests(upReqs) + _ = up.Close() + return + } + // Upstream EOF is relayed once both streams drain, and the downstream + // channel closes only after exit-status requests are delivered. + var output sync.WaitGroup + output.Add(2) + go func() { defer output.Done(); _, _ = io.Copy(down, up) }() + go func() { defer output.Done(); _, _ = io.Copy(down.Stderr(), up.Stderr()) }() + requests := make(chan struct{}) + go func() { + defer close(requests) + for req := range upReqs { + ok, _ := down.SendRequest(req.Type, req.WantReply, req.Payload) // a send failure means the client is gone; report false + _ = req.Reply(ok, nil) // upstream channel may already be closed + } + }() + closed := make(chan struct{}) + go func() { + defer close(closed) + output.Wait() + _ = down.CloseWrite() // fails only if the channel is already closed + <-requests + _ = down.Close() + }() + input := make(chan struct{}) + go func() { defer close(input); _, _ = io.Copy(up, down); _ = up.CloseWrite() }() + + for req := range downReqs { + started := time.Now() + decision, detail := sessionRequestPolicy(req.Type, req.Payload) + if decision != forwardRequest { + if decision == refuseRequest { + s.audit(started, remote, sess, req.Type, detail, "ssh_policy", errCapabilityDenied) + } + _ = req.Reply(false, nil) // client may already be gone + continue + } + ok, err := up.SendRequest(req.Type, req.WantReply, req.Payload) + s.audit(started, remote, sess, req.Type, detail, "ssh_policy", err) + _ = req.Reply(ok, nil) // client may already be gone + } + _ = up.Close() + <-closed + <-input +} + +// connect authorizes the route and logs in upstream with the granted key. +// The returned session is never nil so failures can still be audited. +func (s *Server) connect(ctx context.Context, user string, key ssh.PublicKey, deadline time.Time) (*session, error) { + sess := &session{} + route, destination, err := parseRoute(user) + if err != nil { + return sess, fmt.Errorf("%w: %v", errAccessDenied, err) + } + sess.auth = Authorization{Route: route, Destination: destination, PublicKey: string(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key)))} + grant, err := s.Authorize(ctx, sess.auth) + if err != nil { + return sess, err + } + if err = validateGrant(grant); err != nil { + return sess, err + } + sess.grant = grant + credential, err := signer(ctx, grant.PrivateKey, s.Logger) + if err != nil { + return sess, err + } + var expected ssh.PublicKey + var algorithms []string + if !grant.TLSIdentity { + expected, _, _, _, err = ssh.ParseAuthorizedKey([]byte(grant.HostKey)) + if err != nil { + return sess, errors.New("invalid upstream host identity") + } + algorithms = []string{expected.Type()} + if expected.Type() == ssh.KeyAlgoRSA { + algorithms = []string{ssh.KeyAlgoRSASHA512, ssh.KeyAlgoRSASHA256} + } + } + netConn, err := transport(ctx, grant.Destination, s.Guard, s.TLSRootCAs) + if err != nil { + return sess, fmt.Errorf("SSH upstream unavailable: %w", err) + } + if err = netConn.SetDeadline(deadline); err != nil { + _ = netConn.Close() + return sess, err + } + config := &ssh.ClientConfig{ + User: grant.Username, + Auth: []ssh.AuthMethod{ssh.PublicKeys(credential)}, + HostKeyAlgorithms: algorithms, + HostKeyCallback: func(_ string, _ net.Addr, actual ssh.PublicKey) error { + // TLS-identity grants are WSS-only (validated before dialing). The + // transport authenticates the exact URL's server certificate with no + // redirects or insecure TLS option; SSH remains inside that tunnel. + if expected != nil && !bytes.Equal(expected.Marshal(), actual.Marshal()) { + return errors.New("SSH host identity mismatch") + } + return nil + }, + } + // NewClientConn ignores ctx; shutdown or revocation must still interrupt it. + stop := context.AfterFunc(ctx, func() { _ = netConn.Close() }) + conn, chans, reqs, err := ssh.NewClientConn(netConn, grant.Destination, config) + if !stop() { + err = errors.Join(err, ctx.Err()) + } + if err != nil { + _ = netConn.Close() + return sess, fmt.Errorf("SSH upstream login failed: %w", err) + } + if err = netConn.SetDeadline(time.Time{}); err != nil { + _ = conn.Close() + return sess, err + } + sess.conn, sess.chans, sess.reqs = conn, chans, reqs + return sess, nil +} + +// audit emits one SSH event through the proxy's shared audit sinks. +func (s *Server) audit(started time.Time, remote string, sess *session, method, path, name string, err error) { + trace := transform.TransformTrace{Name: name, Duration: time.Since(started), Annotations: map[string]any{}} + result := &transform.PipelineResult{ + Method: method, + Path: path, + RemoteAddr: remote, + Mode: transform.ModeSSH, + StartedAt: started, + Duration: time.Since(started), + } + if sess != nil { + if sess.auth.Route != "" { + trace.Annotations["route"] = sess.auth.Route + } + if g := sess.grant; g != nil { + trace.Annotations["principal"] = g.Principal + trace.Annotations["grant_id"] = g.ID + if u, err := url.Parse(g.Destination); err == nil { + result.Host = u.Host + } + } + } + switch { + case errors.Is(err, errAccessDenied) || errors.Is(err, errCapabilityDenied): + result.Action, trace.Action = transform.ActionReject, transform.ActionReject + trace.Annotations["reason"] = err.Error() + case err != nil: + result.Err, trace.Err = err, err + } + result.RequestTransforms = []transform.TransformTrace{trace} + s.Audit(result) +} + +func validateGrant(g *Grant) error { + if g == nil || g.ID == "" || g.Principal == "" || g.Generation == "" || g.Username == "" || g.Destination == "" || !g.ExpiresAt.After(time.Now()) || time.Until(g.ExpiresAt) > 15*time.Minute { + return errors.New("invalid SSH grant") + } + if g.TLSIdentity { + u, err := url.Parse(g.Destination) + if err != nil || u.Scheme != "wss" || g.HostKey != "" { + return errors.New("invalid TLS identity grant") + } + } else if g.HostKey == "" { + return errors.New("SSH host key required") + } + return nil +} + +// A client may name a destination, but only the authorizer can approve it. +// The encoded value carries no credentials and is bounded before decoding. +func parseRoute(value string) (string, string, error) { + if strings.HasPrefix(value, "ee:") { + if len(value) > 4096 { + return "", "", errors.New("invalid SSH target") + } + data, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(value, "ee:")) + if err != nil || len(data) == 0 || len(data) > 2048 { + return "", "", errors.New("invalid SSH target") + } + return "ee", string(data), nil + } + if len(value) == 0 || len(value) > 128 { + return "", "", errors.New("invalid SSH route") + } + return value, "", nil +} + +// keepalive ends the session when conn stops answering global requests, so a +// silently dropped peer cannot hold the other side open. Any reply counts. +func (s *Server) keepalive(ctx context.Context, cancel context.CancelFunc, conn ssh.Conn) { + interval := s.KeepaliveInterval + if interval == 0 { + interval = 15 * time.Second + } + ticker := time.NewTicker(interval) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + } + reply := make(chan error, 1) + go func() { + _, _, err := conn.SendRequest("keepalive@openssh.com", true, nil) + reply <- err + }() + // Like OpenSSH's default ServerAliveCountMax, allow three intervals. + timer := time.NewTimer(3 * interval) + select { + case err := <-reply: + timer.Stop() + if err != nil { + cancel() + return + } + case <-timer.C: + s.Logger.Warn("SSH peer stopped responding", slog.String("remote_addr", conn.RemoteAddr().String())) + cancel() + return + case <-ctx.Done(): + timer.Stop() + return + } + } +} + +func (s *Server) renew(ctx context.Context, cancel context.CancelFunc, a Authorization, g *Grant) { + interval := s.RenewalInterval + if interval == 0 { + interval = 5 * time.Second + } + ticker := time.NewTicker(interval) + defer ticker.Stop() + expiry := time.AfterFunc(time.Until(g.ExpiresAt), cancel) + defer expiry.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + next, err := s.Authorize(ctx, a) + if err != nil || validateGrant(next) != nil || !sameGrant(g, next) { + if ctx.Err() == nil { + s.Logger.Warn("SSH lease revoked", slog.String("grant_id", g.ID), slog.String("principal", g.Principal)) + } + cancel() + return + } + expiry.Reset(time.Until(next.ExpiresAt)) + } + } +} +func sameGrant(a, b *Grant) bool { + if a == nil || b == nil { + return false + } + x, y := *a, *b + x.ExpiresAt = time.Time{} + y.ExpiresAt = time.Time{} + x.PrivateKey, y.PrivateKey = yaml.Node{}, yaml.Node{} + if !reflect.DeepEqual(x, y) { + return false + } + var sourceA, sourceB any + if a.PrivateKey.Decode(&sourceA) != nil || b.PrivateKey.Decode(&sourceB) != nil { + return false + } + return reflect.DeepEqual(sourceA, sourceB) +} + +// ParseHostKey resolves a proxy-only credential source. +func ParseHostKey(ctx context.Context, source yaml.Node, logger *slog.Logger) (ssh.Signer, error) { + return signer(ctx, source, logger) +} diff --git a/internal/sshproxy/server_test.go b/internal/sshproxy/server_test.go new file mode 100644 index 0000000..7734544 --- /dev/null +++ b/internal/sshproxy/server_test.go @@ -0,0 +1,765 @@ +package sshproxy + +import ( + "bytes" + "context" + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "crypto/x509" + "encoding/base64" + "encoding/json" + "encoding/pem" + "errors" + "io" + "log/slog" + "net" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/gobwas/ws" + "github.com/pkg/sftp" + "github.com/stretchr/testify/require" + "golang.org/x/crypto/ssh" + "gopkg.in/yaml.v3" + + "github.com/ironsh/iron-proxy/internal/dnsguard" + "github.com/ironsh/iron-proxy/internal/transform" +) + +func key(t *testing.T) (ssh.Signer, []byte) { + t.Helper() + _, private, err := ed25519.GenerateKey(rand.Reader) + require.NoError(t, err) + signer, err := ssh.NewSignerFromKey(private) + require.NoError(t, err) + block, err := ssh.MarshalPrivateKey(private, "") + require.NoError(t, err) + return signer, pem.EncodeToMemory(block) +} + +// testBackend is a real SSH server that accepts only userKey as user "dev". +// exec replies "ok:" with exit status 7; "hold" blocks on stdin, +// "eof-then-hold" sends output and EOF then blocks, and "stream-output" +// writes 12 MiB. The sftp subsystem serves a temp directory. +func testBackend(t *testing.T, host, userKey ssh.Signer) string { + t.Helper() + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, listener.Close()) }) + root := t.TempDir() + alternatePrivate, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + require.NoError(t, err) + alternateHost, err := ssh.NewSignerFromKey(alternatePrivate) + require.NoError(t, err) + go func() { + for { + conn, err := listener.Accept() + if err != nil { + return + } + go func() { + defer conn.Close() + cfg := &ssh.ServerConfig{PublicKeyCallback: func(meta ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { + if meta.User() != "dev" || !bytes.Equal(key.Marshal(), userKey.PublicKey().Marshal()) { + return nil, errors.New("wrong upstream identity") + } + return nil, nil + }} + // A second host key ensures the proxy negotiates the pinned one. + cfg.AddHostKey(alternateHost) + cfg.AddHostKey(host) + server, channels, requests, err := ssh.NewServerConn(conn, cfg) + if err != nil { + return // some tests provoke handshake failures on purpose + } + defer func() { _ = server.Close() }() + go ssh.DiscardRequests(requests) + for channel := range channels { + if channel.ChannelType() != "session" { + _ = channel.Reject(ssh.UnknownChannelType, "session required") + continue + } + stream, reqs, err := channel.Accept() + if err != nil { + return + } + go serveBackendSession(stream, reqs, root) + } + }() + } + }() + return listener.Addr().String() +} + +// Errors are ignored below: this is the test peer, and failures surface as +// assertion failures in the client. +func serveBackendSession(stream ssh.Channel, reqs <-chan *ssh.Request, root string) { + defer func() { _ = stream.Close() }() + for req := range reqs { + switch req.Type { + case "exec": + var cmd struct{ Command string } + if ssh.Unmarshal(req.Payload, &cmd) != nil || req.Reply(true, nil) != nil { + return + } + if cmd.Command == "hold" { + _, _ = io.Copy(io.Discard, stream) + return + } + if cmd.Command == "eof-then-hold" { + _, _ = stream.Write([]byte("partial")) + _ = stream.CloseWrite() + _, _ = io.Copy(io.Discard, stream) + return + } + output := []byte("ok:" + cmd.Command) + if cmd.Command == "stream-output" { + output = bytes.Repeat([]byte("output"), 1<<21) + } + if _, err := stream.Write(output); err != nil { + return + } + _, _ = stream.SendRequest("exit-status", false, ssh.Marshal(struct{ Status uint32 }{7})) + return + case "subsystem": + var sub struct{ Name string } + if ssh.Unmarshal(req.Payload, &sub) != nil || sub.Name != "sftp" { + _ = req.Reply(false, nil) + return + } + if req.Reply(true, nil) != nil { + return + } + server, err := sftp.NewServer(stream, sftp.WithServerWorkingDirectory(root)) + if err != nil { + return + } + _ = server.Serve() + _ = server.Close() + return + default: + _ = req.Reply(false, nil) + } + } +} + +func grant(t *testing.T, address string, host ssh.Signer, private []byte) *Grant { + t.Helper() + var source yaml.Node + require.NoError(t, yaml.Unmarshal([]byte("type: control_plane\nvalue: "+string(mustJSON(t, string(private)))), &source)) + return &Grant{ID: "grant-one", Principal: "user-a", Generation: "one", Destination: "tcp://" + address, Username: "dev", HostKey: strings.TrimSpace(string(ssh.MarshalAuthorizedKey(host.PublicKey()))), PrivateKey: source, ExpiresAt: time.Now().Add(time.Minute)} +} + +func mustJSON(t *testing.T, v any) []byte { + t.Helper() + data, err := json.Marshal(v) + require.NoError(t, err) + return data +} + +type auditLog struct { + mu sync.Mutex + events []*transform.PipelineResult +} + +func (l *auditLog) record(r *transform.PipelineResult) { + l.mu.Lock() + defer l.mu.Unlock() + l.events = append(l.events, r) +} + +func (l *auditLog) all() []*transform.PipelineResult { + l.mu.Lock() + defer l.mu.Unlock() + return append([]*transform.PipelineResult(nil), l.events...) +} + +// recordAuth returns g for every request and records what was asked. +type recordAuth struct { + mu sync.Mutex + g *Grant + calls []Authorization +} + +func (r *recordAuth) authorize(_ context.Context, a Authorization) (*Grant, error) { + r.mu.Lock() + defer r.mu.Unlock() + r.calls = append(r.calls, a) + return r.g, nil +} + +func (r *recordAuth) requests() []Authorization { + r.mu.Lock() + defer r.mu.Unlock() + return append([]Authorization(nil), r.calls...) +} + +// serveProxy runs server on a loopback listener. Unset Guard, Audit, and +// Logger default to an empty guard (the backends are on loopback), a +// recording audit sink, and a discarding logger. +func serveProxy(t *testing.T, server *Server) (string, *auditLog) { + t.Helper() + audits := &auditLog{} + if server.Guard == nil { + server.Guard = &dnsguard.Guard{} + } + if server.Audit == nil { + server.Audit = audits.record + } + if server.Logger == nil { + server.Logger = slog.New(slog.NewJSONHandler(io.Discard, nil)) + } + if server.RenewalInterval == 0 { + server.RenewalInterval = 20 * time.Millisecond + } + ctx, cancel := context.WithCancel(context.Background()) + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + done := make(chan error, 1) + go func() { done <- server.Serve(ctx, listener) }() + t.Cleanup(func() { cancel(); require.NoError(t, <-done) }) + return listener.Addr().String(), audits +} + +func testProxy(t *testing.T, clientKey, hostKey ssh.Signer, auth Authorizer, roots *x509.CertPool) (string, *auditLog) { + t.Helper() + return serveProxy(t, &Server{HostKey: hostKey, ClientPublicKey: clientKey.PublicKey(), Authorize: auth, TLSRootCAs: roots}) +} + +func connect(address string, key, host ssh.Signer) (*ssh.Client, error) { + return ssh.Dial("tcp", address, &ssh.ClientConfig{User: "ee", Auth: []ssh.AuthMethod{ssh.PublicKeys(key)}, HostKeyCallback: ssh.FixedHostKey(host.PublicKey()), Timeout: time.Second}) +} + +// backendFixture is a backend plus a grant that reaches it. +func backendFixture(t *testing.T) *Grant { + t.Helper() + backendHost, _ := key(t) + upstreamKey, private := key(t) + return grant(t, testBackend(t, backendHost, upstreamKey), backendHost, private) +} + +func TestExecSFTPAndKeyIsolation(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + backendHost, _ := key(t) + realKey, private := key(t) + target := testBackend(t, backendHost, realKey) + auth := &recordAuth{g: grant(t, target, backendHost, private)} + address, _ := testProxy(t, clientKey, proxyHost, auth.authorize, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + output, err := session.Output("test-command") + var exit *ssh.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, 7, exit.ExitStatus()) + require.Equal(t, "ok:test-command", string(output)) + files, err := sftp.NewClient(client) + require.NoError(t, err) + defer func() { _ = files.Close() }() + remote, err := files.Create("fixture.txt") + require.NoError(t, err) + _, err = remote.Write([]byte("edited")) + require.NoError(t, err) + require.NoError(t, remote.Close()) + remote, err = files.Open("fixture.txt") + require.NoError(t, err) + data, err := io.ReadAll(remote) + require.NoError(t, err) + require.Equal(t, "edited", string(data)) + require.NoError(t, remote.Close()) + require.Len(t, auth.requests(), 1) + require.Equal(t, "ee", auth.requests()[0].Route) + + wrong, _ := key(t) + _, err = connect(address, wrong, proxyHost) + require.Error(t, err) + _, err = connect(address, clientKey, backendHost) + require.Error(t, err, "client must pin the proxy's host key, not the backend's") +} + +func TestRefusalsAreAuditedAndKeepConnection(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + target := strings.TrimPrefix(g.Destination, "tcp://") + auth := &recordAuth{g: g} + address, audits := testProxy(t, clientKey, proxyHost, auth.authorize, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + + refused, err := client.NewSession() + require.NoError(t, err) + require.Error(t, refused.RequestPty("xterm", 24, 80, nil)) + require.NoError(t, refused.Close()) + _, err = client.Dial("tcp", "example.com:80") + require.Error(t, err) + + session, err := client.NewSession() + require.NoError(t, err) + out, err := session.Output("git-upload-pack 'repo'") + var exit *ssh.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, "ok:git-upload-pack 'repo'", string(out)) + + events := audits.all() + methods := make([]string, len(events)) + for i, e := range events { + methods[i] = e.Method + require.Equal(t, transform.ModeSSH, e.Mode) + require.Equal(t, target, e.Host) + require.Len(t, e.RequestTransforms, 1) + require.Equal(t, "user-a", e.RequestTransforms[0].Annotations["principal"]) + require.Equal(t, "grant-one", e.RequestTransforms[0].Annotations["grant_id"]) + } + require.Equal(t, []string{"auth", "pty-req", "direct-tcpip", "exec"}, methods) + require.Equal(t, transform.ActionContinue, events[0].Action) + require.Equal(t, "ssh_authorization", events[0].RequestTransforms[0].Name) + for _, e := range events[1:3] { + require.Equal(t, transform.ActionReject, e.Action) + require.Equal(t, "ssh_policy", e.RequestTransforms[0].Name) + require.Equal(t, transform.ActionReject, e.RequestTransforms[0].Action) + } + require.Equal(t, transform.ActionContinue, events[3].Action) + require.Equal(t, "git-upload-pack 'repo'", events[3].Path) +} + +// publicOnlySigner offers a public key without holding its private key, as +// anyone who has seen the sandbox's public key could. +type publicOnlySigner struct{ key ssh.PublicKey } + +func (s publicOnlySigner) PublicKey() ssh.PublicKey { return s.key } +func (publicOnlySigner) Sign(io.Reader, []byte) (*ssh.Signature, error) { + return nil, errors.New("no private key") +} + +func TestAuthorizesOnlyVerifiedSandboxKey(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + other, _ := key(t) + cases := []struct { + name string + signer ssh.Signer + }{ + {"unpaired key", other}, + {"paired public key without proof", publicOnlySigner{clientKey.PublicKey()}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + auth := &recordAuth{g: backendFixture(t)} + address, audits := testProxy(t, clientKey, proxyHost, auth.authorize, nil) + _, err := connect(address, tc.signer, proxyHost) + require.Error(t, err) + + require.Eventually(t, func() bool { return len(audits.all()) == 1 }, 5*time.Second, 10*time.Millisecond) + event := audits.all()[0] + require.Equal(t, "auth", event.Method) + require.Equal(t, transform.ActionReject, event.Action) + require.Equal(t, "ssh_authorization", event.RequestTransforms[0].Name) + require.Empty(t, auth.requests()) + }) + } +} + +func TestUpstreamDialHonorsProxyDenyCIDRs(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + guard, err := dnsguard.New(dnsguard.DefaultDenyCIDRs) + require.NoError(t, err) + auth := &recordAuth{g: backendFixture(t)} + address, audits := serveProxy(t, &Server{HostKey: proxyHost, ClientPublicKey: clientKey.PublicKey(), Authorize: auth.authorize, Guard: guard}) + _, err = connect(address, clientKey, proxyHost) + require.Error(t, err) + + events := audits.all() + require.Len(t, events, 1) + require.Equal(t, "auth", events[0].Method) + require.ErrorContains(t, events[0].Err, "upstream_deny_cidrs") + require.Len(t, auth.requests(), 1) +} + +func TestWrongUpstreamHostKeyIsRejected(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + backendHost, _ := key(t) + realKey, private := key(t) + wrongHost, _ := key(t) + g := grant(t, testBackend(t, backendHost, realKey), wrongHost, private) + address, audits := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { return g, nil }, nil) + _, err := connect(address, clientKey, proxyHost) + require.Error(t, err) + require.ErrorContains(t, audits.all()[0].Err, "SSH host identity mismatch") +} + +func TestRevocationClosesActiveConnection(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + var denied atomic.Bool + address, _ := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { + if denied.Load() { + return nil, errAccessDenied + } + return g, nil + }, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + require.NoError(t, session.Start("hold")) + denied.Store(true) + done := make(chan error, 1) + go func() { done <- session.Wait() }() + select { + case err := <-done: + require.Error(t, err) + case <-time.After(2 * time.Second): + t.Fatal("revoked connection stayed open") + } +} + +func TestWstunnelV11Handshake(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + backendHost, _ := key(t) + realKey, private := key(t) + target := testBackend(t, backendHost, realKey) + wsServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/events/events" || !validTunnelProtocols(r.Header.Get("Sec-WebSocket-Protocol")) { + http.NotFound(w, r) + return + } + upgrader := ws.HTTPUpgrader{Protocol: func(value string) bool { return value == "v1" }} + conn, buffered, _, err := upgrader.Upgrade(r, w) + if err != nil { + return + } + stream := &websocketConn{Conn: conn, reader: buffered} + defer func() { _ = stream.Close() }() + upstream, err := net.Dial("tcp", target) + if err != nil { + return + } + defer upstream.Close() + go func() { _, _ = io.Copy(upstream, stream); _ = upstream.Close() }() + _, _ = io.Copy(stream, upstream) + })) + defer wsServer.Close() + roots := x509.NewCertPool() + roots.AddCert(wsServer.Certificate()) + g := grant(t, target, backendHost, private) + g.Destination = "wss" + strings.TrimPrefix(wsServer.URL, "https") + "/events/events" + address, _ := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { return g, nil }, roots) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + output, err := session.Output("websocket") + var exit *ssh.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, 7, exit.ExitStatus()) + require.Equal(t, "ok:websocket", string(output)) + + // Dynamic destinations use the WSS gateway's verified certificate instead + // of a per-pod SSH pin. The authorizer still approves each exact target. + tlsGrant := *g + tlsGrant.HostKey = "" + tlsGrant.TLSIdentity = true + route := "ee:" + base64.RawURLEncoding.EncodeToString([]byte(g.Destination)) + for _, test := range []struct { + name string + roots *x509.CertPool + allowed bool + }{ + {"verified TLS", roots, true}, {"untrusted TLS", x509.NewCertPool(), false}, + } { + t.Run(test.name, func(t *testing.T) { + auth := &recordAuth{g: &tlsGrant} + address, _ := testProxy(t, clientKey, proxyHost, auth.authorize, test.roots) + client, err := ssh.Dial("tcp", address, &ssh.ClientConfig{User: route, Auth: []ssh.AuthMethod{ssh.PublicKeys(clientKey)}, HostKeyCallback: ssh.FixedHostKey(proxyHost.PublicKey()), Timeout: time.Second}) + require.Len(t, auth.requests(), 1) + require.Equal(t, Authorization{Route: "ee", Destination: g.Destination, PublicKey: strings.TrimSpace(string(ssh.MarshalAuthorizedKey(clientKey.PublicKey())))}, auth.requests()[0]) + if !test.allowed { + require.Error(t, err) + return + } + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + output, err := session.Output("dynamic") + var exit *ssh.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, 7, exit.ExitStatus()) + require.Equal(t, "ok:dynamic", string(output)) + }) + } +} + +// validTunnelProtocols checks the wstunnel v11 routing token targets 127.0.0.1:22. +func validTunnelProtocols(header string) bool { + protocols := strings.Split(header, ",") + if len(protocols) != 2 { + return false + } + parts := strings.Split(strings.TrimPrefix(strings.TrimSpace(protocols[1]), "authorization.bearer."), ".") + if len(parts) != 3 { + return false + } + claims, err := base64.RawURLEncoding.DecodeString(parts[1]) + if err != nil { + return false + } + var decoded map[string]any + return json.Unmarshal(claims, &decoded) == nil && decoded["r"] == "127.0.0.1" && decoded["rp"] == float64(22) +} + +func TestParseRouteRejectsMalformedTargets(t *testing.T) { + for _, value := range []string{"", "ee:", "ee:not*base64", "ee:" + strings.Repeat("a", 4096), strings.Repeat("a", 129)} { + _, _, err := parseRoute(value) + require.Error(t, err, value) + } + route, destination, err := parseRoute("ee:" + base64.RawURLEncoding.EncodeToString([]byte("wss://example.com/x"))) + require.NoError(t, err) + require.Equal(t, "ee", route) + require.Equal(t, "wss://example.com/x", destination) +} + +func TestValidateGrant(t *testing.T) { + _, private := key(t) + host, _ := key(t) + cases := []struct { + name string + mutate func(*Grant) + valid bool + }{ + {"pinned TCP", func(*Grant) {}, true}, + {"expired", func(g *Grant) { g.ExpiresAt = time.Now().Add(-time.Second) }, false}, + {"lease too long", func(g *Grant) { g.ExpiresAt = time.Now().Add(16 * time.Minute) }, false}, + {"missing host key", func(g *Grant) { g.HostKey = "" }, false}, + {"TLS identity over TCP", func(g *Grant) { g.TLSIdentity, g.HostKey = true, "" }, false}, + {"TLS identity over WSS", func(g *Grant) { g.TLSIdentity, g.HostKey, g.Destination = true, "", "wss://example.com/events" }, true}, + {"TLS identity with host pin", func(g *Grant) { g.TLSIdentity, g.Destination = true, "wss://example.com/events" }, false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + g := grant(t, "example.com:22", host, private) + tc.mutate(g) + if tc.valid { + require.NoError(t, validateGrant(g)) + } else { + require.Error(t, validateGrant(g)) + } + }) + } +} + +func TestSameGrant(t *testing.T) { + decode := func(expiry, generation, credential string) *Grant { + t.Helper() + var g Grant + require.NoError(t, yaml.Unmarshal([]byte(`{"id":"grant","generation":"`+generation+`","expires_at":"`+expiry+`","private_key":{"type":"control_plane","value":"`+credential+`"}}`), &g)) + return &g + } + a := decode("2099-01-01T00:00:00.123Z", "one", "same-key") + b := decode("2099-01-01T00:00:00.123456Z", "one", "same-key") + require.NotEqual(t, a.PrivateKey.Column, b.PrivateKey.Column, "fixture must shift YAML source positions") + require.True(t, sameGrant(a, b), "expiry formatting must not revoke an unchanged credential") + require.NoError(t, yaml.Unmarshal([]byte("value: 'same-key' # harmless comment\ntype: control_plane\n"), &b.PrivateKey)) + require.True(t, sameGrant(a, b), "mapping order, style and comments do not change the credential") + require.False(t, sameGrant(a, decode("2099-01-01T00:00:00Z", "one", "different-key")), "credential changes must revoke") + require.False(t, sameGrant(a, decode("2099-01-01T00:00:00Z", "two", "same-key")), "generation changes must revoke") + b.PrivateKey = yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: "invalid"} + require.False(t, sameGrant(a, b), "invalid sources must fail closed") +} + +func TestLeaseExpiryInterruptsBlockedRenewal(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + server := &Server{RenewalInterval: time.Millisecond, Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)), Authorize: func(ctx context.Context, _ Authorization) (*Grant, error) { <-ctx.Done(); return nil, ctx.Err() }} + done := make(chan struct{}) + go func() { + defer close(done) + server.renew(ctx, cancel, Authorization{}, &Grant{ExpiresAt: time.Now().Add(30 * time.Millisecond)}) + }() + select { + case <-done: + require.ErrorIs(t, ctx.Err(), context.Canceled) + case <-time.After(time.Second): + t.Fatal("blocked renewal outlived lease") + } +} + +func TestHTTPAuthorizationUsesProxyTokenAndDoesNotLeakErrors(t *testing.T) { + cases := []struct { + status int + denied bool + }{ + {http.StatusUnauthorized, true}, + {http.StatusForbidden, true}, + {http.StatusServiceUnavailable, false}, + {http.StatusNotFound, false}, + } + for _, tc := range cases { + t.Run(http.StatusText(tc.status), func(t *testing.T) { + var gotAuth atomic.Value + controller := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gotAuth.Store(r.Header.Get("Authorization")) + http.Error(w, "test-upstream-secret", tc.status) + })) + defer controller.Close() + _, err := HTTPAuthorizer(controller.URL, "test-only-token")(context.Background(), Authorization{Route: "ee"}) + require.Error(t, err) + require.Equal(t, tc.denied, errors.Is(err, errAccessDenied)) + require.NotContains(t, err.Error(), "test-upstream-secret") + require.Equal(t, "Bearer test-only-token", gotAuth.Load()) + }) + } +} + +func TestUpstreamEOFReachesClientBeforeClose(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + address, _ := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { return g, nil }, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + defer func() { _ = session.Close() }() + stdout, err := session.StdoutPipe() + require.NoError(t, err) + // Hold stdin open so the backend keeps the channel open after its EOF. + stdin, err := session.StdinPipe() + require.NoError(t, err) + defer func() { _ = stdin.Close() }() + require.NoError(t, session.Start("eof-then-hold")) + read := make(chan []byte, 1) + go func() { + data, _ := io.ReadAll(stdout) // the assertion below covers the content + read <- data + }() + select { + case data := <-read: + require.Equal(t, "partial", string(data)) + case <-time.After(2 * time.Second): + t.Fatal("upstream EOF was not relayed while the channel stayed open") + } +} + +// deafConn stops delivering inbound bytes once deaf is set, like a peer whose +// network silently dropped. It still reports EOF when the proxy hangs up. +type deafConn struct { + net.Conn + deaf atomic.Bool +} + +func (c *deafConn) Read(p []byte) (int, error) { + n, err := c.Conn.Read(p) + for c.deaf.Load() && err == nil { + n, err = c.Conn.Read(p) + } + return n, err +} + +func TestUnresponsiveClientIsDisconnected(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + address, _ := serveProxy(t, &Server{HostKey: proxyHost, ClientPublicKey: clientKey.PublicKey(), Authorize: func(context.Context, Authorization) (*Grant, error) { return g, nil }, KeepaliveInterval: 20 * time.Millisecond}) + raw, err := net.Dial("tcp", address) + require.NoError(t, err) + conn := &deafConn{Conn: raw} + c, chans, reqs, err := ssh.NewClientConn(conn, address, &ssh.ClientConfig{User: "ee", Auth: []ssh.AuthMethod{ssh.PublicKeys(clientKey)}, HostKeyCallback: ssh.FixedHostKey(proxyHost.PublicKey()), Timeout: time.Second}) + require.NoError(t, err) + client := ssh.NewClient(c, chans, reqs) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + require.NoError(t, session.Start("hold")) + + conn.deaf.Store(true) + done := make(chan error, 1) + go func() { done <- client.Wait() }() + select { + case err := <-done: + require.Error(t, err) + case <-time.After(2 * time.Second): + t.Fatal("proxy kept a silent client's session open") + } +} + +func TestOpenSSHBookkeepingDoesNotDisconnect(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + address, _ := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { return g, nil }, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + for _, request := range []string{"keepalive@openssh.com", "tcpip-forward"} { + ok, _, err := client.SendRequest(request, true, nil) + require.NoError(t, err) + require.False(t, ok, "global requests must be refused locally") + } + channel, requests, err := client.OpenChannel("session", nil) + require.NoError(t, err) + defer func() { _ = channel.Close() }() + go ssh.DiscardRequests(requests) + for _, request := range []string{"env", "eow@openssh.com"} { + _, err := channel.SendRequest(request, false, nil) + require.NoError(t, err) + } + ok, err := channel.SendRequest("exec", true, ssh.Marshal(struct{ Command string }{"after-bookkeeping"})) + require.NoError(t, err) + require.True(t, ok) + output, err := io.ReadAll(channel) + require.NoError(t, err) + require.Equal(t, "ok:after-bookkeeping", string(output)) +} + +func TestKeepalivesDuringServerOutput(t *testing.T) { + clientKey, _ := key(t) + proxyHost, _ := key(t) + g := backendFixture(t) + address, _ := testProxy(t, clientKey, proxyHost, func(context.Context, Authorization) (*Grant, error) { return g, nil }, nil) + client, err := connect(address, clientKey, proxyHost) + require.NoError(t, err) + defer func() { _ = client.Close() }() + session, err := client.NewSession() + require.NoError(t, err) + defer func() { _ = session.Close() }() + output, err := session.StdoutPipe() + require.NoError(t, err) + require.NoError(t, session.Start("stream-output")) + keepalives := make(chan error, 1) + go func() { + for range 200 { + if _, _, err := client.SendRequest("keepalive@openssh.com", true, nil); err != nil { + keepalives <- err + return + } + } + keepalives <- nil + }() + data, err := io.ReadAll(output) + require.NoError(t, err) + require.NoError(t, <-keepalives) + var exit *ssh.ExitError + require.ErrorAs(t, session.Wait(), &exit) + require.Equal(t, 7, exit.ExitStatus()) + require.Equal(t, bytes.Repeat([]byte("output"), 1<<21), data) +} diff --git a/internal/sshproxy/transport.go b/internal/sshproxy/transport.go new file mode 100644 index 0000000..cdbde92 --- /dev/null +++ b/internal/sshproxy/transport.go @@ -0,0 +1,90 @@ +package sshproxy + +import ( + "bytes" + "context" + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "crypto/tls" + "crypto/x509" + "encoding/base64" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "net/url" + "time" + + "github.com/gobwas/ws" + "github.com/ironsh/iron-proxy/internal/dnsguard" +) + +func transport(ctx context.Context, target string, guard *dnsguard.Guard, roots *x509.CertPool) (net.Conn, error) { + // Bound TLS and HTTP upgrade negotiation too, not only the TCP dial. + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + u, err := url.Parse(target) + if err != nil || u.User != nil || u.Host == "" || u.RawQuery != "" || u.Fragment != "" { + return nil, errors.New("invalid SSH destination") + } + dialer := &net.Dialer{Timeout: 10 * time.Second, Control: guard.DialControl} + switch u.Scheme { + case "tcp": + if u.Path != "" { + return nil, errors.New("invalid TCP destination") + } + return dialer.DialContext(ctx, "tcp", u.Host) + case "wss": + // wstunnel v11 carries the fixed loopback SSH target in a JWT subprotocol. + token, err := tunnelToken() + if err != nil { + return nil, err + } + + client := ws.Dialer{NetDial: dialer.DialContext, TLSConfig: &tls.Config{RootCAs: roots, MinVersion: tls.VersionTLS12}, Timeout: 10 * time.Second, Protocols: []string{"v1", "authorization.bearer." + token}} + conn, buffered, handshake, err := client.Dial(ctx, target) + if err != nil { + return nil, fmt.Errorf("SSH WebSocket connection failed: %w", err) + } + if handshake.Protocol != "v1" { + _ = conn.Close() + return nil, errors.New("unsupported SSH WebSocket protocol") + } + stream := &websocketConn{Conn: conn, reader: conn} + if buffered != nil { + prefix := make([]byte, buffered.Buffered()) + if _, err := io.ReadFull(buffered, prefix); err != nil { + _ = conn.Close() + return nil, err + } + ws.PutReader(buffered) + stream.reader = io.MultiReader(bytes.NewReader(prefix), conn) + } + return stream, nil + + default: + return nil, errors.New("SSH transport must be tcp or wss") + } +} + +func tunnelToken() (string, error) { + nonce := make([]byte, 16) + if _, err := rand.Read(nonce); err != nil { + return "", errors.New("tunnel nonce unavailable") + } + payload, err := json.Marshal(map[string]any{"id": hex.EncodeToString(nonce), "p": map[string]any{"Tcp": map[string]any{"proxy_protocol": false}}, "r": "127.0.0.1", "rp": 22}) + if err != nil { + return "", err + } + enc := base64.RawURLEncoding + text := enc.EncodeToString([]byte(`{"alg":"HS256","typ":"JWT"}`)) + "." + enc.EncodeToString(payload) + // wstunnel treats this JWT as routing metadata, not as authentication. + mac := hmac.New(sha256.New, nonce) + if _, err := mac.Write([]byte(text)); err != nil { + return "", err + } + return text + "." + enc.EncodeToString(mac.Sum(nil)), nil +} diff --git a/internal/sshproxy/websocket.go b/internal/sshproxy/websocket.go new file mode 100644 index 0000000..602e8b5 --- /dev/null +++ b/internal/sshproxy/websocket.go @@ -0,0 +1,86 @@ +package sshproxy + +import ( + "errors" + "io" + "net" + "sync" + + "github.com/gobwas/ws" + "github.com/gobwas/ws/wsutil" +) + +// websocketConn matches wstunnel v11's default unmasked framing on both legs. +// TLS still authenticates and encrypts the external transport. +type websocketConn struct { + net.Conn + reader io.Reader + remaining int64 + fragmented bool + writeMu sync.Mutex +} + +func (c *websocketConn) Write(data []byte) (int, error) { + c.writeMu.Lock() + defer c.writeMu.Unlock() + if err := wsutil.WriteServerBinary(c.Conn, data); err != nil { + return 0, err + } + return len(data), nil +} + +func (c *websocketConn) Read(data []byte) (int, error) { + if len(data) == 0 { + return 0, nil + } + for c.remaining == 0 { + h, err := ws.ReadHeader(c.reader) + if err != nil { + return 0, err + } + if h.Masked || h.Rsv != 0 || h.Length < 0 || h.Length > 1<<20 { + return 0, errors.New("invalid wstunnel frame") + } + switch h.OpCode { + case ws.OpBinary: + if c.fragmented { + return 0, errors.New("invalid wstunnel fragmentation") + } + c.fragmented = !h.Fin + c.remaining = h.Length + case ws.OpContinuation: + if !c.fragmented { + return 0, errors.New("unexpected wstunnel continuation") + } + c.fragmented = !h.Fin + c.remaining = h.Length + case ws.OpPing, ws.OpPong, ws.OpClose: + if !h.Fin || h.Length > 125 { + return 0, errors.New("invalid wstunnel control frame") + } + payload := make([]byte, int(h.Length)) + if _, err = io.ReadFull(c.reader, payload); err != nil { + return 0, err + } + if h.OpCode == ws.OpClose { + return 0, io.EOF + } + if h.OpCode == ws.OpPing { + c.writeMu.Lock() + err = wsutil.WriteServerMessage(c.Conn, ws.OpPong, payload) + c.writeMu.Unlock() + if err != nil { + return 0, err + } + } + default: + return 0, errors.New("unsupported wstunnel frame") + } + } + if int64(len(data)) > c.remaining { + data = data[:int(c.remaining)] + } + n, err := c.reader.Read(data) + c.remaining -= int64(n) + return n, err +} diff --git a/internal/sshproxy/wstunnel_integration_test.go b/internal/sshproxy/wstunnel_integration_test.go new file mode 100644 index 0000000..a177cfb --- /dev/null +++ b/internal/sshproxy/wstunnel_integration_test.go @@ -0,0 +1,42 @@ +package sshproxy + +import ( + "bufio" + "bytes" + "context" + "crypto/x509" + "io" + "os" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/ironsh/iron-proxy/internal/dnsguard" +) + +func TestWstunnelIntegration(t *testing.T) { + target := os.Getenv("IRON_SSH_WSTUNNEL_TEST_URL") + caPath := os.Getenv("IRON_SSH_WSTUNNEL_TEST_CA") + if target == "" || caPath == "" { + t.Skip("set IRON_SSH_WSTUNNEL_TEST_URL and IRON_SSH_WSTUNNEL_TEST_CA for a local OpenSSH/wstunnel fixture") + } + cert, err := os.ReadFile(caPath) + require.NoError(t, err) + roots := x509.NewCertPool() + require.True(t, roots.AppendCertsFromPEM(cert)) + conn, err := transport(context.Background(), target, &dnsguard.Guard{}, roots) + require.NoError(t, err) + defer conn.Close() + require.NoError(t, conn.SetDeadline(time.Now().Add(3*time.Second))) + _, err = conn.Write([]byte("SSH-2.0-iron-test\r\n")) + require.NoError(t, err) + reader := bufio.NewReaderSize(conn, 256) + banner, err := reader.ReadSlice('\n') + require.NoError(t, err) + require.True(t, bytes.HasPrefix(banner, []byte("SSH-2.0-"))) + // Wait for the packet header even when the banner spans multiple reads. + var packetLength [4]byte + _, err = io.ReadFull(reader, packetLength[:]) + require.NoError(t, err) +} diff --git a/internal/transform/audit.go b/internal/transform/audit.go index a8e7165..e512835 100644 --- a/internal/transform/audit.go +++ b/internal/transform/audit.go @@ -84,6 +84,9 @@ func NewAuditLogger(logger *slog.Logger) AuditFunc { if msgs := result.MCP.MCPMessages(); len(msgs) > 0 { mcpAttrs = append(mcpAttrs, slog.Any("messages", msgs)) } + if gateway := result.MCP.MCPGateway(); len(gateway) > 0 { + mcpAttrs = append(mcpAttrs, slog.Any("gateway", gateway)) + } attrs = append(attrs, slog.Group("mcp", mcpAttrs...)) } if result.BodyCapture != nil && result.BodyCapture.RequestBody() != "" { diff --git a/internal/transform/gcpauth/gcpauth.go b/internal/transform/gcpauth/gcpauth.go index 579a71b..4e9837d 100644 --- a/internal/transform/gcpauth/gcpauth.go +++ b/internal/transform/gcpauth/gcpauth.go @@ -40,6 +40,7 @@ import ( "github.com/ironsh/iron-proxy/internal/hostmatch" "github.com/ironsh/iron-proxy/internal/transform" + "github.com/ironsh/iron-proxy/internal/transform/gcpjwt" "github.com/ironsh/iron-proxy/internal/transform/secrets" ) @@ -258,7 +259,7 @@ func isTokenEndpoint(req *http.Request) bool { } switch host { case "oauth2.googleapis.com": - return path == "/token" + return path == "/token" && !isIDTokenJWTBearerRequest(req) case "metadata.google.internal", "169.254.169.254": return strings.HasPrefix(path, "/computeMetadata/v1/instance/service-accounts/") && strings.HasSuffix(path, "/token") @@ -266,6 +267,11 @@ func isTokenEndpoint(req *http.Request) bool { return false } +func isIDTokenJWTBearerRequest(req *http.Request) bool { + _, ok := gcpjwt.JWTBearerTargetAudience(req) + return ok +} + func stubTokenResponse(req *http.Request) *http.Response { body := transform.NewBufferedBodyFromBytes(stubTokenJSON) return &http.Response{ diff --git a/internal/transform/gcpauth/gcpauth_test.go b/internal/transform/gcpauth/gcpauth_test.go index 6e463a2..c0388bc 100644 --- a/internal/transform/gcpauth/gcpauth_test.go +++ b/internal/transform/gcpauth/gcpauth_test.go @@ -13,6 +13,7 @@ import ( "log/slog" "net/http" "net/http/httptest" + "net/url" "os" "path/filepath" "strings" @@ -23,7 +24,9 @@ import ( "golang.org/x/oauth2" "gopkg.in/yaml.v3" + "github.com/ironsh/iron-proxy/internal/hostmatch" "github.com/ironsh/iron-proxy/internal/transform" + "github.com/ironsh/iron-proxy/internal/transform/gcpjwt" "github.com/ironsh/iron-proxy/internal/transform/secrets" ) @@ -402,6 +405,91 @@ rules: require.Equal(t, int64(0), calls.Load(), "real GCP token endpoint must not be hit when stubbing") } +func TestGCPAuth_StubsOAuth2TokenEndpointWithBufferedJWTBody(t *testing.T) { + srv, calls := fakeTokenServer(t, "minted-token", 3600) + dir := t.TempDir() + path := filepath.Join(dir, "sa.json") + require.NoError(t, os.WriteFile(path, testKeyfileJSON(t, srv.URL, "sa@p.iam.gserviceaccount.com"), 0o600)) + + cfg := config{ + KeyfilePath: path, + Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"}, + Rules: []hostmatch.RuleConfig{ + {Host: "bigquery.googleapis.com"}, + }, + } + g, err := newFromConfig(cfg, slog.Default(), os.ReadFile, staticBuilder(&staticSource{}), errTokenSourceBuilder) + require.NoError(t, err) + + form := url.Values{} + form.Set("grant_type", gcpjwt.JWTBearerGrantType) + form.Set("assertion", unsignedAssertion(t, map[string]any{ + "iss": "stub@p.iam.gserviceaccount.com", + "aud": "https://oauth2.googleapis.com/token", + "scope": "https://www.googleapis.com/auth/cloud-platform", + })) + body := form.Encode() + req, err := http.NewRequest(http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(body)) + require.NoError(t, err) + req.Body = transform.NewBufferedBody(req.Body, 0) + + p := transform.NewPipeline([]transform.Transformer{g}, transform.BodyLimits{}, slog.Default()) + var traces []transform.TransformTrace + resp, err := p.ProcessRequest(context.Background(), newContext(), req, &traces) + require.NoError(t, err) + require.NotNil(t, resp) + require.Equal(t, http.StatusOK, resp.StatusCode) + require.Len(t, traces, 1) + require.Equal(t, transform.ActionStub, traces[0].Action) + require.IsType(t, &transform.BufferedBody{}, req.Body) + + restored, err := io.ReadAll(req.Body) + require.NoError(t, err) + require.Equal(t, body, string(restored)) + require.Equal(t, int64(0), calls.Load(), "real GCP token endpoint must not be hit when stubbing") +} + +func TestGCPAuth_DoesNotStubIDTokenJWTBearer(t *testing.T) { + srv, calls := fakeTokenServer(t, "minted-token", 3600) + dir := t.TempDir() + path := filepath.Join(dir, "sa.json") + require.NoError(t, os.WriteFile(path, testKeyfileJSON(t, srv.URL, "sa@p.iam.gserviceaccount.com"), 0o600)) + + cfgYAML := ` +keyfile_path: ` + path + ` +scopes: ["https://www.googleapis.com/auth/cloud-platform"] +rules: + - host: "bigquery.googleapis.com" +` + var c config + node := yamlFromString(t, cfgYAML) + require.NoError(t, node.Decode(&c)) + g, err := newFromConfig(c, slog.Default(), os.ReadFile, staticBuilder(&staticSource{}), errTokenSourceBuilder) + require.NoError(t, err) + + form := url.Values{} + form.Set("grant_type", gcpjwt.JWTBearerGrantType) + form.Set("assertion", unsignedAssertion(t, map[string]any{ + "iss": "stub@p.iam.gserviceaccount.com", + "aud": "https://oauth2.googleapis.com/token", + "target_audience": "https://service.run.app", + })) + body := form.Encode() + req, err := http.NewRequest(http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(body)) + require.NoError(t, err) + tctx := newContext() + res, err := g.TransformRequest(context.Background(), tctx, req) + require.NoError(t, err) + require.Equal(t, transform.ActionContinue, res.Action) + require.Empty(t, tctx.DrainAnnotations()) + + restored, err := io.ReadAll(req.Body) + require.NoError(t, err) + require.Equal(t, body, string(restored)) + require.Empty(t, req.Header.Get("Authorization")) + require.Equal(t, int64(0), calls.Load(), "gcp_auth must not stub or mint for ID-token JWT bearer requests") +} + func TestGCPAuth_StubsMetadataServerToken(t *testing.T) { srv, _ := fakeTokenServer(t, "minted-token", 3600) dir := t.TempDir() @@ -505,6 +593,16 @@ func decodeJWTClaims(t *testing.T, assertion string) map[string]any { return claims } +func unsignedAssertion(t *testing.T, claims map[string]any) string { + t.Helper() + header, err := json.Marshal(map[string]any{"alg": "RS256", "typ": "JWT"}) + require.NoError(t, err) + payload, err := json.Marshal(claims) + require.NoError(t, err) + return base64.RawURLEncoding.EncodeToString(header) + "." + + base64.RawURLEncoding.EncodeToString(payload) + ".sig" +} + // TestGCPAuth_Subject verifies that the subject field drives domain-wide // delegation: the minted assertion's sub claim impersonates the configured // Workspace user, and is absent when no subject is set. diff --git a/internal/transform/gcpidtoken/gcpidtoken.go b/internal/transform/gcpidtoken/gcpidtoken.go new file mode 100644 index 0000000..53c2336 --- /dev/null +++ b/internal/transform/gcpidtoken/gcpidtoken.go @@ -0,0 +1,384 @@ +// Package gcpidtoken implements a transform that mints Google-signed OIDC +// ID tokens and injects them as Bearer headers on matching requests. +// +// This is intended for private Cloud Run, Cloud Run functions, IAP, API +// Gateway, and other Google-backed targets that authenticate with an ID token +// whose aud claim names the receiving service. Unlike gcp_auth, this transform +// is audience-based and does not use OAuth scopes. +// +// Credentials come from exactly one service-account JSON source: +// - keyfile_path: a service account JSON keyfile loaded from disk. +// - keyfile: a service account JSON keyfile loaded from any registered +// secret source (env, aws_sm, aws_ssm, 1password, 1password_connect). +// +// Like all header-injecting transforms, this requires MITM mode; sni-only +// mode has no way to rewrite headers. +package gcpidtoken + +import ( + "context" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "log/slog" + "net/http" + "os" + "strings" + "sync" + "time" + + "golang.org/x/oauth2" + "golang.org/x/oauth2/google" + "gopkg.in/yaml.v3" + + "github.com/ironsh/iron-proxy/internal/hostmatch" + "github.com/ironsh/iron-proxy/internal/transform" + "github.com/ironsh/iron-proxy/internal/transform/gcpjwt" + "github.com/ironsh/iron-proxy/internal/transform/secrets" +) + +const ( + defaultHeader = "Authorization" + serverlessHeader = "X-Serverless-Authorization" + stubAudience = "iron-proxy-stub-audience" + stubIDTokenLifetimeSeconds = 3600 +) + +func init() { + transform.Register("gcp_id_token", factory) +} + +type config struct { + KeyfilePath string `yaml:"keyfile_path,omitempty"` + Keyfile yaml.Node `yaml:"keyfile,omitempty"` + Audience string `yaml:"audience"` + Header string `yaml:"header,omitempty"` + Rules []hostmatch.RuleConfig `yaml:"rules"` +} + +// GCPIDToken is the transform. +type GCPIDToken struct { + rules []hostmatch.Rule + audience string + header string + tsLoader tokenSourceLoader + logger *slog.Logger + + mu sync.Mutex + tokenSource oauth2.TokenSource + principal string +} + +// tokenSourceLoader returns a fresh token source for one transform instance, +// plus a best-effort principal identifier for audit annotations. +type tokenSourceLoader func(ctx context.Context) (oauth2.TokenSource, string, error) + +// sourceBuilder is the signature of secrets.BuildSource. Pulled out so tests +// can inject a stub instead of constructing real source backends. +type sourceBuilder func(yaml.Node, *slog.Logger) (secrets.Source, error) + +func factory(cfg yaml.Node, logger *slog.Logger) (transform.Transformer, error) { + var c config + if err := cfg.Decode(&c); err != nil { + return nil, fmt.Errorf("parsing gcp_id_token config: %w", err) + } + return newFromConfig(c, logger, os.ReadFile, secrets.BuildSource) +} + +func newFromConfig(c config, logger *slog.Logger, readFile func(string) ([]byte, error), buildSource sourceBuilder) (*GCPIDToken, error) { + hasPath := c.KeyfilePath != "" + hasNested := c.Keyfile.Kind != 0 + if hasPath == hasNested { + return nil, fmt.Errorf("gcp_id_token: requires exactly one of \"keyfile_path\" or \"keyfile\"") + } + if c.Audience == "" { + return nil, fmt.Errorf("gcp_id_token: \"audience\" is required") + } + rules, err := hostmatch.CompileRules(c.Rules, "gcp_id_token") + if err != nil { + return nil, err + } + if len(rules) == 0 { + return nil, fmt.Errorf("gcp_id_token: at least one entry in \"rules\" is required") + } + header, err := normalizeHeader(c.Header) + if err != nil { + return nil, err + } + + var tsLoader tokenSourceLoader + switch { + case hasPath: + path := c.KeyfilePath + audience := c.Audience + tsLoader = func(ctx context.Context) (oauth2.TokenSource, string, error) { + data, err := readFile(path) + if err != nil { + return nil, "", fmt.Errorf("reading GCP service account keyfile %q: %w", path, err) + } + return tokenSourceFromKeyfile(ctx, data, audience) + } + case hasNested: + nested, err := buildSource(c.Keyfile, logger) + if err != nil { + return nil, fmt.Errorf("gcp_id_token: building keyfile source: %w", err) + } + audience := c.Audience + tsLoader = func(ctx context.Context) (oauth2.TokenSource, string, error) { + v, err := nested.Get(ctx) + if err != nil { + return nil, "", fmt.Errorf("loading GCP service account keyfile from %q: %w", nested.Name(), err) + } + return tokenSourceFromKeyfile(ctx, []byte(v), audience) + } + } + + return &GCPIDToken{ + rules: rules, + audience: c.Audience, + header: header, + tsLoader: tsLoader, + logger: logger, + }, nil +} + +func normalizeHeader(raw string) (string, error) { + switch strings.ToLower(strings.TrimSpace(raw)) { + case "", "authorization": + return defaultHeader, nil + case "x-serverless-authorization": + return serverlessHeader, nil + default: + return "", fmt.Errorf("gcp_id_token: \"header\" must be \"authorization\" or \"x-serverless-authorization\"") + } +} + +// tokenSourceFromKeyfile parses a service-account JSON keyfile and returns a +// token source that exchanges a signed JWT assertion for a Google-signed ID +// token whose aud claim is audience. +func tokenSourceFromKeyfile(ctx context.Context, keyJSON []byte, audience string) (oauth2.TokenSource, string, error) { + cfg, err := google.JWTConfigFromJSON(keyJSON) + if err != nil { + return nil, "", fmt.Errorf("parsing GCP service account keyfile: %w", err) + } + cfg.PrivateClaims = map[string]any{"target_audience": audience} + cfg.UseIDToken = true + + var meta struct { + ClientEmail string `json:"client_email"` + } + email := "" + if err := json.Unmarshal(keyJSON, &meta); err == nil { + email = meta.ClientEmail + } + return cfg.TokenSource(ctx), email, nil +} + +func (g *GCPIDToken) Name() string { return "gcp_id_token" } + +func (g *GCPIDToken) TransformRequest(ctx context.Context, tctx *transform.TransformContext, req *http.Request) (*transform.TransformResult, error) { + if audience, kind, ok := stubRequest(req); ok { + tok, err := fakeIDToken(audience) + if err != nil { + return nil, err + } + tctx.Annotate("stubbed", kind) + if audience != "" { + tctx.Annotate("audience", audience) + } + return &transform.TransformResult{ + Action: transform.ActionStub, + Response: stubResponse(req, kind, tok), + }, nil + } + + if !hostmatch.MatchAnyRule(g.rules, req) { + return &transform.TransformResult{Action: transform.ActionContinue}, nil + } + + tok, err := g.mintToken(ctx) + if err != nil { + reason := g.logMintFailure(err) + tctx.Annotate("error", reason) + tctx.Annotate("rejected", "token_unavailable") + return &transform.TransformResult{Action: transform.ActionReject}, nil + } + + req.Header.Set(g.header, "Bearer "+tok) + if g.principal != "" { + tctx.Annotate("service_account", g.principal) + } + tctx.Annotate("audience", g.audience) + tctx.Annotate("injected", []string{"header:" + g.header}) + return &transform.TransformResult{Action: transform.ActionContinue}, nil +} + +func (g *GCPIDToken) TransformResponse(context.Context, *transform.TransformContext, *http.Request, *http.Response) (*transform.TransformResult, error) { + return &transform.TransformResult{Action: transform.ActionContinue}, nil +} + +func (g *GCPIDToken) mintToken(ctx context.Context) (string, error) { + ts, err := g.loadTokenSource(ctx) + if err != nil { + return "", err + } + tok, err := ts.Token() + if err != nil { + return "", fmt.Errorf("minting GCP ID token: %w", err) + } + if tok.AccessToken == "" { + return "", fmt.Errorf("GCP ID token source returned empty token") + } + return tok.AccessToken, nil +} + +func (g *GCPIDToken) loadTokenSource(ctx context.Context) (oauth2.TokenSource, error) { + g.mu.Lock() + defer g.mu.Unlock() + if g.tokenSource != nil { + return g.tokenSource, nil + } + ts, principal, err := g.tsLoader(ctx) + if err != nil { + return nil, err + } + g.tokenSource = ts + g.principal = principal + return g.tokenSource, nil +} + +func (g *GCPIDToken) logMintFailure(err error) string { + reason := classifyMintError(err) + if g.logger != nil { + g.logger.Warn("gcp_id_token mint failed", "reason", reason, "error", err) + } + return reason +} + +func classifyMintError(err error) string { + var re *oauth2.RetrieveError + if errors.As(err, &re) { + switch { + case re.ErrorCode != "": + return re.ErrorCode + case re.Response != nil: + return fmt.Sprintf("token_endpoint_http_%d", re.Response.StatusCode) + default: + return "token_endpoint_error" + } + } + return "mint_failed" +} + +func stubRequest(req *http.Request) (audience string, kind string, ok bool) { + if audience, ok := metadataIdentityAudience(req); ok { + return audience, "gcp_metadata_identity_endpoint", true + } + if audience, ok := jwtBearerIDTokenAudience(req); ok { + return audience, "gcp_oauth2_id_token_endpoint", true + } + return "", "", false +} + +func metadataIdentityAudience(req *http.Request) (string, bool) { + host := hostmatch.StripPort(req.Host) + if host != "metadata.google.internal" && host != "169.254.169.254" { + return "", false + } + path := "" + if req.URL != nil { + path = req.URL.Path + } + if !strings.HasPrefix(path, "/computeMetadata/v1/instance/service-accounts/") || !strings.HasSuffix(path, "/identity") { + return "", false + } + audience := "" + if req.URL != nil { + audience = req.URL.Query().Get("audience") + } + if audience == "" { + return "", false + } + return audience, true +} + +func jwtBearerIDTokenAudience(req *http.Request) (string, bool) { + host := hostmatch.StripPort(req.Host) + path := "" + if req.URL != nil { + path = req.URL.Path + } + if host != "oauth2.googleapis.com" || path != "/token" { + return "", false + } + return gcpjwt.JWTBearerTargetAudience(req) +} + +func fakeIDToken(audience string) (string, error) { + if audience == "" { + audience = stubAudience + } + now := time.Now() + header := map[string]any{ + "alg": "RS256", + "typ": "JWT", + } + claims := map[string]any{ + "iss": "https://accounts.google.com", + "aud": audience, + "azp": "iron-proxy-stub", + "sub": "iron-proxy-stub", + "email": "stub@iron-proxy.local", + "email_verified": true, + "iat": now.Unix(), + "exp": now.Add(stubIDTokenLifetimeSeconds * time.Second).Unix(), + } + h, err := encodeJWTPart(header) + if err != nil { + return "", err + } + c, err := encodeJWTPart(claims) + if err != nil { + return "", err + } + sig := base64.RawURLEncoding.EncodeToString([]byte("stub-signature")) + return h + "." + c + "." + sig, nil +} + +func encodeJWTPart(v any) (string, error) { + b, err := json.Marshal(v) + if err != nil { + return "", fmt.Errorf("encoding stub ID token: %w", err) + } + return base64.RawURLEncoding.EncodeToString(b), nil +} + +func stubResponse(req *http.Request, kind, token string) *http.Response { + if kind == "gcp_metadata_identity_endpoint" { + body := []byte(token) + return &http.Response{ + StatusCode: http.StatusOK, + Status: "200 OK", + Proto: "HTTP/1.1", + ProtoMajor: 1, + ProtoMinor: 1, + Header: http.Header{"Content-Type": {"text/plain"}}, + Body: transform.NewBufferedBodyFromBytes(body), + ContentLength: int64(len(body)), + Request: req, + } + } + body := []byte(`{"id_token":"` + token + `","expires_in":3600,"token_type":"Bearer"}`) + return &http.Response{ + StatusCode: http.StatusOK, + Status: "200 OK", + Proto: "HTTP/1.1", + ProtoMajor: 1, + ProtoMinor: 1, + Header: http.Header{"Content-Type": {"application/json"}}, + Body: transform.NewBufferedBodyFromBytes(body), + ContentLength: int64(len(body)), + Request: req, + } +} diff --git a/internal/transform/gcpidtoken/gcpidtoken_test.go b/internal/transform/gcpidtoken/gcpidtoken_test.go new file mode 100644 index 0000000..c0b4ec2 --- /dev/null +++ b/internal/transform/gcpidtoken/gcpidtoken_test.go @@ -0,0 +1,514 @@ +package gcpidtoken + +import ( + "context" + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/base64" + "encoding/json" + "encoding/pem" + "fmt" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "net/url" + "os" + "path/filepath" + "strings" + "sync/atomic" + "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" + + "github.com/ironsh/iron-proxy/internal/hostmatch" + "github.com/ironsh/iron-proxy/internal/transform" + "github.com/ironsh/iron-proxy/internal/transform/gcpjwt" + "github.com/ironsh/iron-proxy/internal/transform/secrets" +) + +type staticSource struct { + name string + value string + err error + calls atomic.Int64 +} + +func (s *staticSource) Name() string { return s.name } +func (s *staticSource) Get(context.Context) (string, error) { + s.calls.Add(1) + return s.value, s.err +} + +func staticBuilder(src secrets.Source) sourceBuilder { + return func(yaml.Node, *slog.Logger) (secrets.Source, error) { return src, nil } +} + +func failingBuilder(err error) sourceBuilder { + return func(yaml.Node, *slog.Logger) (secrets.Source, error) { return nil, err } +} + +func testKeyfileJSON(t *testing.T, tokenURL, email string) []byte { + t.Helper() + key, err := rsa.GenerateKey(rand.Reader, 2048) + require.NoError(t, err) + der, err := x509.MarshalPKCS8PrivateKey(key) + require.NoError(t, err) + pemBytes := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der}) + keyfile := map[string]string{ + "type": "service_account", + "project_id": "test-project", + "private_key": string(pemBytes), + "client_email": email, + "token_uri": tokenURL, + } + data, err := json.Marshal(keyfile) + require.NoError(t, err) + return data +} + +func fakeIDTokenServer(t *testing.T, accessToken string) (*httptest.Server, *atomic.Int64, *atomic.Value) { + t.Helper() + var calls atomic.Int64 + var assertion atomic.Value + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls.Add(1) + require.NoError(t, r.ParseForm()) + assertion.Store(r.PostForm.Get("assertion")) + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "id_token": accessToken, + "token_type": "Bearer", + "expires_in": stubIDTokenLifetimeSeconds, + }) + })) + t.Cleanup(srv.Close) + return srv, &calls, &assertion +} + +func yamlFromString(t *testing.T, src string) yaml.Node { + t.Helper() + var node yaml.Node + require.NoError(t, yaml.Unmarshal([]byte(src), &node)) + return *node.Content[0] +} + +func newRequest(t *testing.T, host string) *http.Request { + t.Helper() + r, err := http.NewRequest(http.MethodGet, "https://"+host+"/v1/resource", nil) + require.NoError(t, err) + return r +} + +func newContext() *transform.TransformContext { + return &transform.TransformContext{Mode: transform.ModeMITM} +} + +func decodeJWTClaims(t *testing.T, token string) map[string]any { + t.Helper() + parts := strings.Split(token, ".") + require.Len(t, parts, 3, "token is not a JWT") + raw, err := base64.RawURLEncoding.DecodeString(parts[1]) + require.NoError(t, err) + var claims map[string]any + require.NoError(t, json.Unmarshal(raw, &claims)) + return claims +} + +func unsignedAssertion(t *testing.T, claims map[string]any) string { + t.Helper() + header, err := encodeJWTPart(map[string]any{"alg": "RS256", "typ": "JWT"}) + require.NoError(t, err) + body, err := encodeJWTPart(claims) + require.NoError(t, err) + return header + "." + body + ".sig" +} + +func TestGCPIDToken_Validation(t *testing.T) { + cases := []struct { + name string + yaml string + wantError string + }{ + { + name: "missing both keyfile sources", + yaml: ` +audience: https://service.run.app +rules: + - host: service.run.app +`, + wantError: "exactly one", + }, + { + name: "both keyfile sources set", + yaml: ` +keyfile_path: /tmp/k.json +keyfile: + type: env + var: X +audience: https://service.run.app +rules: + - host: service.run.app +`, + wantError: "exactly one", + }, + { + name: "missing audience", + yaml: ` +keyfile_path: /tmp/k.json +rules: + - host: service.run.app +`, + wantError: "audience", + }, + { + name: "missing rules", + yaml: ` +keyfile_path: /tmp/k.json +audience: https://service.run.app +`, + wantError: "rules", + }, + { + name: "invalid header", + yaml: ` +keyfile_path: /tmp/k.json +audience: https://service.run.app +header: X-Other +rules: + - host: service.run.app +`, + wantError: "header", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var c config + node := yamlFromString(t, tc.yaml) + require.NoError(t, node.Decode(&c)) + _, err := newFromConfig(c, slog.Default(), os.ReadFile, staticBuilder(&staticSource{})) + require.ErrorContains(t, err, tc.wantError) + }) + } +} + +func TestGCPIDToken_InjectsBearerFromKeyfilePath(t *testing.T) { + wantIDToken, err := fakeIDToken("https://service.run.app") + require.NoError(t, err) + srv, calls, assertion := fakeIDTokenServer(t, wantIDToken) + path := filepath.Join(t.TempDir(), "sa.json") + require.NoError(t, os.WriteFile(path, testKeyfileJSON(t, srv.URL, "sa@p.iam.gserviceaccount.com"), 0o600)) + + cfg := config{ + KeyfilePath: path, + Audience: "https://service.run.app", + Rules: []hostmatch.RuleConfig{hostmatchRuleConfig(t, "service.run.app")}, + } + g, err := newFromConfig(cfg, slog.Default(), os.ReadFile, staticBuilder(&staticSource{})) + require.NoError(t, err) + + req := newRequest(t, "service.run.app") + tctx := newContext() + res, err := g.TransformRequest(context.Background(), tctx, req) + require.NoError(t, err) + require.Equal(t, transform.ActionContinue, res.Action) + require.Equal(t, "Bearer "+wantIDToken, req.Header.Get("Authorization")) + require.Equal(t, int64(1), calls.Load()) + + claims := decodeJWTClaims(t, assertion.Load().(string)) + require.Equal(t, "sa@p.iam.gserviceaccount.com", claims["iss"]) + require.Equal(t, srv.URL, claims["aud"]) + require.Equal(t, "https://service.run.app", claims["target_audience"]) + + annotations := tctx.DrainAnnotations() + require.Equal(t, "sa@p.iam.gserviceaccount.com", annotations["service_account"]) + require.Equal(t, "https://service.run.app", annotations["audience"]) +} + +func TestGCPIDToken_InjectsServerlessHeader(t *testing.T) { + wantIDToken, err := fakeIDToken("https://service.run.app") + require.NoError(t, err) + srv, _, _ := fakeIDTokenServer(t, wantIDToken) + path := filepath.Join(t.TempDir(), "sa.json") + require.NoError(t, os.WriteFile(path, testKeyfileJSON(t, srv.URL, "sa@p.iam.gserviceaccount.com"), 0o600)) + + cfg := config{ + KeyfilePath: path, + Audience: "https://service.run.app", + Header: "x-serverless-authorization", + Rules: []hostmatch.RuleConfig{hostmatchRuleConfig(t, "service.run.app")}, + } + g, err := newFromConfig(cfg, slog.Default(), os.ReadFile, staticBuilder(&staticSource{})) + require.NoError(t, err) + + req := newRequest(t, "service.run.app") + res, err := g.TransformRequest(context.Background(), newContext(), req) + require.NoError(t, err) + require.Equal(t, transform.ActionContinue, res.Action) + require.Empty(t, req.Header.Get("Authorization")) + require.Equal(t, "Bearer "+wantIDToken, req.Header.Get("X-Serverless-Authorization")) +} + +func TestGCPIDToken_InjectsBearerFromNestedSource(t *testing.T) { + wantIDToken, err := fakeIDToken("https://nested.run.app") + require.NoError(t, err) + srv, _ := fakeIDTokenServerNoAssertion(t, wantIDToken) + keyJSON := testKeyfileJSON(t, srv.URL, "nested@p.iam.gserviceaccount.com") + nested := &staticSource{name: "op://vault/gcp-sa/credential", value: string(keyJSON)} + + cfg := config{ + Keyfile: yamlFromString(t, "type: 1password_connect\nsecret_ref: \"op://vault/gcp-sa/credential\"\n"), + Audience: "https://nested.run.app", + Rules: []hostmatch.RuleConfig{hostmatchRuleConfig(t, "nested.run.app")}, + } + g, err := newFromConfig(cfg, slog.Default(), os.ReadFile, staticBuilder(nested)) + require.NoError(t, err) + + req := newRequest(t, "nested.run.app") + res, err := g.TransformRequest(context.Background(), newContext(), req) + require.NoError(t, err) + require.Equal(t, transform.ActionContinue, res.Action) + require.Equal(t, "Bearer "+wantIDToken, req.Header.Get("Authorization")) + require.Equal(t, int64(1), nested.calls.Load()) +} + +func TestGCPIDToken_HostRulesRestrictInjection(t *testing.T) { + wantIDToken, err := fakeIDToken("https://service.run.app") + require.NoError(t, err) + srv, calls, _ := fakeIDTokenServer(t, wantIDToken) + path := filepath.Join(t.TempDir(), "sa.json") + require.NoError(t, os.WriteFile(path, testKeyfileJSON(t, srv.URL, "sa@p.iam.gserviceaccount.com"), 0o600)) + + cfg := config{ + KeyfilePath: path, + Audience: "https://service.run.app", + Rules: []hostmatch.RuleConfig{hostmatchRuleConfig(t, "service.run.app")}, + } + g, err := newFromConfig(cfg, slog.Default(), os.ReadFile, staticBuilder(&staticSource{})) + require.NoError(t, err) + + req := newRequest(t, "service.run.app") + _, err = g.TransformRequest(context.Background(), newContext(), req) + require.NoError(t, err) + require.Equal(t, "Bearer "+wantIDToken, req.Header.Get("Authorization")) + + other := newRequest(t, "api.openai.com") + _, err = g.TransformRequest(context.Background(), newContext(), other) + require.NoError(t, err) + require.Empty(t, other.Header.Get("Authorization")) + require.Equal(t, int64(1), calls.Load()) +} + +func TestGCPIDToken_CachesTokenAcrossRequests(t *testing.T) { + wantIDToken, err := fakeIDToken("https://service.run.app") + require.NoError(t, err) + srv, calls, _ := fakeIDTokenServer(t, wantIDToken) + path := filepath.Join(t.TempDir(), "sa.json") + require.NoError(t, os.WriteFile(path, testKeyfileJSON(t, srv.URL, "sa@p.iam.gserviceaccount.com"), 0o600)) + + cfg := config{ + KeyfilePath: path, + Audience: "https://service.run.app", + Rules: []hostmatch.RuleConfig{hostmatchRuleConfig(t, "service.run.app")}, + } + g, err := newFromConfig(cfg, slog.Default(), os.ReadFile, staticBuilder(&staticSource{})) + require.NoError(t, err) + + for i := 0; i < 5; i++ { + req := newRequest(t, "service.run.app") + _, err := g.TransformRequest(context.Background(), newContext(), req) + require.NoError(t, err) + require.Equal(t, "Bearer "+wantIDToken, req.Header.Get("Authorization")) + } + require.Equal(t, int64(1), calls.Load()) +} + +func TestGCPIDToken_KeyfileMissing_Rejects(t *testing.T) { + cfg := config{ + KeyfilePath: "/does/not/exist.json", + Audience: "https://service.run.app", + Rules: []hostmatch.RuleConfig{hostmatchRuleConfig(t, "service.run.app")}, + } + g, err := newFromConfig(cfg, slog.Default(), os.ReadFile, staticBuilder(&staticSource{})) + require.NoError(t, err) + + req := newRequest(t, "service.run.app") + tctx := newContext() + res, err := g.TransformRequest(context.Background(), tctx, req) + require.NoError(t, err) + require.Equal(t, transform.ActionReject, res.Action) + require.Empty(t, req.Header.Get("Authorization")) + require.Equal(t, "mint_failed", tctx.DrainAnnotations()["error"]) +} + +func TestGCPIDToken_NestedSourceBuildError(t *testing.T) { + cfg := config{ + Keyfile: yamlFromString(t, "type: 1password_connect\nsecret_ref: \"op://vault/missing\"\n"), + Audience: "https://service.run.app", + Rules: []hostmatch.RuleConfig{hostmatchRuleConfig(t, "service.run.app")}, + } + _, err := newFromConfig(cfg, slog.Default(), os.ReadFile, failingBuilder(fmt.Errorf("not configured"))) + require.ErrorContains(t, err, "building keyfile source") + require.ErrorContains(t, err, "not configured") +} + +func TestGCPIDToken_StubsOAuth2IDTokenEndpoint(t *testing.T) { + form := url.Values{} + form.Set("grant_type", gcpjwt.JWTBearerGrantType) + form.Set("assertion", unsignedAssertion(t, map[string]any{ + "iss": "stub@p.iam.gserviceaccount.com", + "aud": "https://oauth2.googleapis.com/token", + "target_audience": "https://service.run.app", + })) + req, err := http.NewRequest(http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(form.Encode())) + require.NoError(t, err) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + g := newStubOnlyTransform(t) + tctx := newContext() + res, err := g.TransformRequest(context.Background(), tctx, req) + require.NoError(t, err) + require.Equal(t, transform.ActionStub, res.Action) + require.Equal(t, "gcp_oauth2_id_token_endpoint", tctx.DrainAnnotations()["stubbed"]) + + var out struct { + IDToken string `json:"id_token"` + } + require.NoError(t, json.NewDecoder(res.Response.Body).Decode(&out)) + claims := decodeJWTClaims(t, out.IDToken) + require.Equal(t, "https://service.run.app", claims["aud"]) +} + +func TestGCPIDToken_DoesNotStubAccessTokenJWTBearer(t *testing.T) { + form := url.Values{} + form.Set("grant_type", gcpjwt.JWTBearerGrantType) + form.Set("assertion", unsignedAssertion(t, map[string]any{ + "iss": "stub@p.iam.gserviceaccount.com", + "aud": "https://oauth2.googleapis.com/token", + "scope": "https://www.googleapis.com/auth/cloud-platform", + })) + body := form.Encode() + req, err := http.NewRequest(http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(body)) + require.NoError(t, err) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + g := newStubOnlyTransform(t) + res, err := g.TransformRequest(context.Background(), newContext(), req) + require.NoError(t, err) + require.Equal(t, transform.ActionContinue, res.Action) + restored, err := io.ReadAll(req.Body) + require.NoError(t, err) + require.Equal(t, body, string(restored)) +} + +func TestGCPIDToken_DoesNotInspectOversizedTokenEndpointBody(t *testing.T) { + body := strings.Repeat("x", gcpjwt.MaxTokenRequestBodyBytes+1) + req, err := http.NewRequest(http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(body)) + require.NoError(t, err) + + g := newStubOnlyTransform(t) + res, err := g.TransformRequest(context.Background(), newContext(), req) + require.NoError(t, err) + require.Equal(t, transform.ActionContinue, res.Action) + restored, err := io.ReadAll(req.Body) + require.NoError(t, err) + require.Equal(t, body, string(restored)) +} + +func TestGCPIDToken_StubsMetadataIdentityEndpoint(t *testing.T) { + req, err := http.NewRequest(http.MethodGet, "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=https%3A%2F%2Fservice.run.app", nil) + require.NoError(t, err) + + g := newStubOnlyTransform(t) + tctx := newContext() + res, err := g.TransformRequest(context.Background(), tctx, req) + require.NoError(t, err) + require.Equal(t, transform.ActionStub, res.Action) + require.Equal(t, "gcp_metadata_identity_endpoint", tctx.DrainAnnotations()["stubbed"]) + body, err := io.ReadAll(res.Response.Body) + require.NoError(t, err) + claims := decodeJWTClaims(t, string(body)) + require.Equal(t, "https://service.run.app", claims["aud"]) +} + +func TestGCPIDToken_DoesNotStubMetadataIdentityWithoutAudience(t *testing.T) { + req, err := http.NewRequest(http.MethodGet, "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity", nil) + require.NoError(t, err) + + g := newStubOnlyTransform(t) + res, err := g.TransformRequest(context.Background(), newContext(), req) + require.NoError(t, err) + require.Equal(t, transform.ActionContinue, res.Action) +} + +func TestGCPIDToken_MintFailureRejectsWithSanitizedReason(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, err := io.Copy(io.Discard, r.Body) + require.NoError(t, err) + http.Error(w, "upstream token endpoint failed", http.StatusInternalServerError) + })) + t.Cleanup(srv.Close) + path := filepath.Join(t.TempDir(), "sa.json") + require.NoError(t, os.WriteFile(path, testKeyfileJSON(t, srv.URL, "sa@p.iam.gserviceaccount.com"), 0o600)) + + cfg := config{ + KeyfilePath: path, + Audience: "https://service.run.app", + Rules: []hostmatch.RuleConfig{hostmatchRuleConfig(t, "service.run.app")}, + } + g, err := newFromConfig(cfg, slog.Default(), os.ReadFile, staticBuilder(&staticSource{})) + require.NoError(t, err) + + req := newRequest(t, "service.run.app") + tctx := newContext() + res, err := g.TransformRequest(context.Background(), tctx, req) + require.NoError(t, err) + require.Equal(t, transform.ActionReject, res.Action) + require.Equal(t, "token_endpoint_http_500", tctx.DrainAnnotations()["error"]) +} + +func newStubOnlyTransform(t *testing.T) *GCPIDToken { + t.Helper() + wantIDToken, err := fakeIDToken("https://service.run.app") + require.NoError(t, err) + srv, _ := fakeIDTokenServerNoAssertion(t, wantIDToken) + path := filepath.Join(t.TempDir(), "sa.json") + require.NoError(t, os.WriteFile(path, testKeyfileJSON(t, srv.URL, "sa@p.iam.gserviceaccount.com"), 0o600)) + cfg := config{ + KeyfilePath: path, + Audience: "https://service.run.app", + Rules: []hostmatch.RuleConfig{hostmatchRuleConfig(t, "service.run.app")}, + } + g, err := newFromConfig(cfg, slog.Default(), os.ReadFile, staticBuilder(&staticSource{})) + require.NoError(t, err) + return g +} + +func fakeIDTokenServerNoAssertion(t *testing.T, accessToken string) (*httptest.Server, *atomic.Int64) { + t.Helper() + var calls atomic.Int64 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls.Add(1) + _, err := io.Copy(io.Discard, r.Body) + require.NoError(t, err) + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "id_token": accessToken, + "token_type": "Bearer", + "expires_in": stubIDTokenLifetimeSeconds, + }) + })) + t.Cleanup(srv.Close) + return srv, &calls +} + +func hostmatchRuleConfig(t *testing.T, host string) hostmatch.RuleConfig { + t.Helper() + var rules struct { + Rules []hostmatch.RuleConfig `yaml:"rules"` + } + require.NoError(t, yaml.Unmarshal([]byte("rules:\n - host: "+host+"\n"), &rules)) + return rules.Rules[0] +} diff --git a/internal/transform/gcpjwt/gcpjwt.go b/internal/transform/gcpjwt/gcpjwt.go new file mode 100644 index 0000000..2d1d728 --- /dev/null +++ b/internal/transform/gcpjwt/gcpjwt.go @@ -0,0 +1,104 @@ +// Package gcpjwt contains small helpers shared by GCP auth transforms for +// recognizing Google JWT-bearer token exchange requests. +package gcpjwt + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "io" + "net/http" + "net/url" + "strings" + + "github.com/ironsh/iron-proxy/internal/transform" +) + +const ( + // JWTBearerGrantType is the OAuth2 grant type used by Google service + // account keyfiles when exchanging a signed JWT assertion. + JWTBearerGrantType = "urn:ietf:params:oauth:grant-type:jwt-bearer" + + // MaxTokenRequestBodyBytes caps token endpoint body inspection. Requests + // above this size are left for the normal request path. + MaxTokenRequestBodyBytes = 1 << 20 +) + +// JWTBearerTargetAudience returns the target_audience claim from a JWT-bearer +// token request body. The request body is restored before returning. +func JWTBearerTargetAudience(req *http.Request) (string, bool) { + body, ok := tokenRequestBody(req) + if !ok { + return "", false + } + values, err := url.ParseQuery(string(body)) + if err != nil { + return "", false + } + if values.Get("grant_type") != JWTBearerGrantType { + return "", false + } + return TargetAudienceFromAssertion(values.Get("assertion")) +} + +// TargetAudienceFromAssertion extracts the target_audience private claim from +// an unsigned inspection of a JWT assertion. +func TargetAudienceFromAssertion(assertion string) (string, bool) { + parts := strings.Split(assertion, ".") + if len(parts) != 3 { + return "", false + } + raw, err := base64.RawURLEncoding.DecodeString(parts[1]) + if err != nil { + return "", false + } + var claims struct { + TargetAudience string `json:"target_audience"` + } + if err := json.Unmarshal(raw, &claims); err != nil { + return "", false + } + return claims.TargetAudience, claims.TargetAudience != "" +} + +func tokenRequestBody(req *http.Request) ([]byte, bool) { + if req.Body == nil { + return nil, false + } + if req.ContentLength < 0 || req.ContentLength > MaxTokenRequestBodyBytes { + return nil, false + } + + buffered, isBuffered := req.Body.(*transform.BufferedBody) + body, err := io.ReadAll(io.LimitReader(req.Body, MaxTokenRequestBodyBytes+1)) + if err != nil || len(body) > MaxTokenRequestBodyBytes { + if isBuffered { + buffered.Reset() + } else { + req.Body = &replayReadCloser{ + Reader: io.MultiReader(bytes.NewReader(body), req.Body), + closer: req.Body, + } + } + return nil, false + } + closeErr := req.Body.Close() + if isBuffered { + buffered.Reset() + } else { + req.Body = io.NopCloser(bytes.NewReader(body)) + } + if closeErr != nil { + return nil, false + } + return body, true +} + +type replayReadCloser struct { + io.Reader + closer io.Closer +} + +func (r *replayReadCloser) Close() error { + return r.closer.Close() +} diff --git a/internal/transform/gcpjwt/gcpjwt_test.go b/internal/transform/gcpjwt/gcpjwt_test.go new file mode 100644 index 0000000..b4c79ad --- /dev/null +++ b/internal/transform/gcpjwt/gcpjwt_test.go @@ -0,0 +1,103 @@ +package gcpjwt + +import ( + "encoding/base64" + "encoding/json" + "io" + "net/http" + "net/url" + "strings" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/ironsh/iron-proxy/internal/transform" +) + +func TestJWTBearerTargetAudience(t *testing.T) { + form := url.Values{} + form.Set("grant_type", JWTBearerGrantType) + form.Set("assertion", unsignedAssertion(t, map[string]any{ + "target_audience": "https://service.run.app", + })) + body := form.Encode() + req, err := http.NewRequest(http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(body)) + require.NoError(t, err) + + audience, ok := JWTBearerTargetAudience(req) + require.True(t, ok) + require.Equal(t, "https://service.run.app", audience) + + restored, err := io.ReadAll(req.Body) + require.NoError(t, err) + require.Equal(t, body, string(restored)) +} + +func TestJWTBearerTargetAudiencePreservesBufferedBody(t *testing.T) { + form := url.Values{} + form.Set("grant_type", JWTBearerGrantType) + form.Set("assertion", unsignedAssertion(t, map[string]any{ + "target_audience": "https://service.run.app", + })) + body := form.Encode() + req, err := http.NewRequest(http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(body)) + require.NoError(t, err) + req.Body = transform.NewBufferedBody(req.Body, 0) + + audience, ok := JWTBearerTargetAudience(req) + require.True(t, ok) + require.Equal(t, "https://service.run.app", audience) + require.IsType(t, &transform.BufferedBody{}, req.Body) + + restored, err := io.ReadAll(req.Body) + require.NoError(t, err) + require.Equal(t, body, string(restored)) +} + +func TestJWTBearerTargetAudienceRestoresOversizedBody(t *testing.T) { + body := strings.Repeat("x", MaxTokenRequestBodyBytes+1) + req, err := http.NewRequest(http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(body)) + require.NoError(t, err) + + audience, ok := JWTBearerTargetAudience(req) + require.False(t, ok) + require.Empty(t, audience) + + restored, err := io.ReadAll(req.Body) + require.NoError(t, err) + require.Equal(t, body, string(restored)) +} + +func TestJWTBearerTargetAudiencePreservesBufferedOversizedBody(t *testing.T) { + body := strings.Repeat("x", MaxTokenRequestBodyBytes+1) + req, err := http.NewRequest(http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(body)) + require.NoError(t, err) + req.Body = transform.NewBufferedBody(req.Body, 0) + + audience, ok := JWTBearerTargetAudience(req) + require.False(t, ok) + require.Empty(t, audience) + require.IsType(t, &transform.BufferedBody{}, req.Body) + + restored, err := io.ReadAll(req.Body) + require.NoError(t, err) + require.Equal(t, body, string(restored)) +} + +func TestTargetAudienceFromAssertionRejectsMissingAudience(t *testing.T) { + audience, ok := TargetAudienceFromAssertion(unsignedAssertion(t, map[string]any{ + "scope": "https://www.googleapis.com/auth/cloud-platform", + })) + require.False(t, ok) + require.Empty(t, audience) +} + +func unsignedAssertion(t *testing.T, claims map[string]any) string { + t.Helper() + header, err := json.Marshal(map[string]any{"alg": "RS256", "typ": "JWT"}) + require.NoError(t, err) + payload, err := json.Marshal(claims) + require.NoError(t, err) + return base64.RawURLEncoding.EncodeToString(header) + "." + + base64.RawURLEncoding.EncodeToString(payload) + ".sig" +} diff --git a/internal/transform/otelaudit.go b/internal/transform/otelaudit.go index e0b7999..e4f37b1 100644 --- a/internal/transform/otelaudit.go +++ b/internal/transform/otelaudit.go @@ -95,6 +95,9 @@ func NewOTELAuditFunc(provider *sdklog.LoggerProvider) AuditFunc { } mcpKVs = append(mcpKVs, log.KeyValue{Key: "messages", Value: log.SliceValue(vals...)}) } + if gateway := result.MCP.MCPGateway(); len(gateway) > 0 { + mcpKVs = append(mcpKVs, log.KeyValue{Key: "gateway", Value: toLogValue(gateway)}) + } attrs = append(attrs, log.KeyValue{Key: "mcp", Value: log.MapValue(mcpKVs...)}) } if result.BodyCapture != nil && result.BodyCapture.RequestBody() != "" { diff --git a/internal/transform/secrets/op_resolver.go b/internal/transform/secrets/op_resolver.go index fe197bb..c9aedf2 100644 --- a/internal/transform/secrets/op_resolver.go +++ b/internal/transform/secrets/op_resolver.go @@ -36,21 +36,27 @@ type opConfig struct { FailureTTL string `yaml:"failure_ttl,omitempty"` } +// opClients is shared by every 1Password source in the process. Each secrets +// transform, credential source, and config reload builds its own registry, but +// they must not create SDK clients concurrently: the SDK lazily loads its Wasm +// core without synchronization, so concurrent first calls each compile a +// separate copy (~150 MB of RSS apiece). +var opClients = &opClientCache{ + getenv: os.Getenv, + newClient: func(ctx context.Context, token string) (opClient, error) { + c, err := onepassword.NewClient(ctx, + onepassword.WithServiceAccountToken(token), + onepassword.WithIntegrationInfo("iron-proxy", version.Version), + ) + if err != nil { + return nil, err + } + return opSDKClient{c: c}, nil + }, +} + func newOPBuilder(logger *slog.Logger) *opBuilder { - cache := &opClientCache{ - getenv: os.Getenv, - newClient: func(ctx context.Context, token string) (opClient, error) { - c, err := onepassword.NewClient(ctx, - onepassword.WithServiceAccountToken(token), - onepassword.WithIntegrationInfo("iron-proxy", version.Version), - ) - if err != nil { - return nil, err - } - return opSDKClient{c: c}, nil - }, - } - return &opBuilder{clientFor: cache.get, logger: logger} + return &opBuilder{clientFor: opClients.get, logger: logger} } func (r *opBuilder) Build(raw yaml.Node) (secretSource, error) { @@ -86,7 +92,8 @@ func (r *opBuilder) fetchSecret(ctx context.Context, cfg opConfig) (string, erro // opClientCache lazily constructs a single SDK client. The 1P SDK loads a // Wasm module on NewClient, so reusing the client across multiple secret -// entries is significantly cheaper than creating one per entry. +// entries is significantly cheaper than creating one per entry. The mutex also +// serializes NewClient calls, which the SDK requires (see opClients). type opClientCache struct { mu sync.Mutex client opClient diff --git a/internal/transform/secrets/op_resolver_test.go b/internal/transform/secrets/op_resolver_test.go index 8b8c23b..43a5704 100644 --- a/internal/transform/secrets/op_resolver_test.go +++ b/internal/transform/secrets/op_resolver_test.go @@ -4,7 +4,10 @@ import ( "context" "fmt" "log/slog" + "sync" + "sync/atomic" "testing" + "time" "github.com/stretchr/testify/require" ) @@ -207,3 +210,43 @@ func TestOPClientCache_PropagatesNewClientError(t *testing.T) { require.Error(t, err) require.Contains(t, err.Error(), "sdk init failed") } + +func TestOPSources_ShareOneClientAcrossRegistries(t *testing.T) { + // Separate transforms each build their own registry. Concurrent first + // fetches across them must still create only one SDK client. + var calls atomic.Int32 + saved := opClients + opClients = &opClientCache{ + getenv: func(string) string { return "tok" }, + newClient: func(_ context.Context, _ string) (opClient, error) { + calls.Add(1) + time.Sleep(10 * time.Millisecond) // widen the window for overlapping creation + return staticOPClient("v", nil), nil + }, + } + t.Cleanup(func() { opClients = saved }) + + var sources []Source + for _, ref := range []string{"op://v/slack/token", "op://v/openai/key", "op://v/centaur/token"} { + src, err := BuildSource(yamlNode(t, map[string]string{"type": "1password", "secret_ref": ref}), slog.Default()) + require.NoError(t, err) + sources = append(sources, src) + } + + var wg sync.WaitGroup + errs := make(chan error, len(sources)) + for _, src := range sources { + wg.Add(1) + go func() { + defer wg.Done() + _, err := src.Get(context.Background()) + errs <- err + }() + } + wg.Wait() + close(errs) + for err := range errs { + require.NoError(t, err) + } + require.Equal(t, int32(1), calls.Load()) +} diff --git a/internal/transform/secrets/secrets.go b/internal/transform/secrets/secrets.go index 7d1ad2e..9828044 100644 --- a/internal/transform/secrets/secrets.go +++ b/internal/transform/secrets/secrets.go @@ -148,6 +148,7 @@ func defaultRegistry(logger *slog.Logger) sourceBuilderRegistry { "control_plane": newControlPlaneBuilder(logger), "aws_sm": newAWSSMBuilder(logger), "aws_ssm": newAWSSSMBuilder(logger), + "vault_kv": newVaultKVBuilder(logger), "1password": newOPBuilder(logger), "1password_connect": newOPConnectBuilder(logger), } diff --git a/internal/transform/secrets/vault_kv_resolver.go b/internal/transform/secrets/vault_kv_resolver.go new file mode 100644 index 0000000..930b3c3 --- /dev/null +++ b/internal/transform/secrets/vault_kv_resolver.go @@ -0,0 +1,142 @@ +package secrets + +import ( + "context" + "encoding/json" + "fmt" + "log/slog" + "strings" + "sync" + + vaultapi "github.com/hashicorp/vault/api" + "gopkg.in/yaml.v3" +) + +// vaultKVClient is the subset of Vault used by vaultKVBuilder, narrowed for +// testability and to keep KV version handling out of the source builder. +type vaultKVClient interface { + ReadKV(ctx context.Context, mount, secretPath string, version int) (map[string]any, error) +} + +// vaultKVBuilder reads static secrets from Vault KV v1 or v2. Authentication, +// address, namespace, and TLS configuration use the standard VAULT_* +// environment variables understood by the official Vault API client. +type vaultKVBuilder struct { + clientFor func(context.Context) (vaultKVClient, error) + logger *slog.Logger +} + +type vaultKVConfig struct { + Type string `yaml:"type"` + Mount string `yaml:"mount"` + Path string `yaml:"path"` + KVVersion int `yaml:"kv_version,omitempty"` + TTL string `yaml:"ttl,omitempty"` + FailureTTL string `yaml:"failure_ttl,omitempty"` +} + +func (cfg vaultKVConfig) version() int { + if cfg.KVVersion == 0 { + return 2 + } + return cfg.KVVersion +} + +func newVaultKVBuilder(logger *slog.Logger) *vaultKVBuilder { + cache := &vaultKVClientCache{ + newClient: func() (vaultKVClient, error) { + client, err := vaultapi.NewClient(nil) + if err != nil { + return nil, err + } + return vaultAPIKVClient{client: client}, nil + }, + } + return &vaultKVBuilder{clientFor: cache.get, logger: logger} +} + +func (r *vaultKVBuilder) Build(raw yaml.Node) (secretSource, error) { + var cfg vaultKVConfig + if err := raw.Decode(&cfg); err != nil { + return nil, fmt.Errorf("parsing vault_kv source config: %w", err) + } + if cfg.Mount == "" { + return nil, fmt.Errorf("vault_kv source requires \"mount\" field") + } + if cfg.Path == "" { + return nil, fmt.Errorf("vault_kv source requires \"path\" field") + } + if version := cfg.version(); version != 1 && version != 2 { + return nil, fmt.Errorf("vault_kv source kv_version must be 1 or 2, got %d", cfg.KVVersion) + } + + secretPath := cfg.Mount + "/" + cfg.Path + name := "vault_kv:" + secretPath + return buildLazySource(name, cfg.TTL, cfg.FailureTTL, r.logger, func(ctx context.Context) (string, error) { + client, err := r.clientFor(ctx) + if err != nil { + return "", fmt.Errorf("creating Vault client: %w", err) + } + data, err := client.ReadKV(ctx, cfg.Mount, cfg.Path, cfg.version()) + if err != nil { + return "", fmt.Errorf("reading Vault KV secret %q: %w", secretPath, err) + } + if len(data) == 0 { + return "", fmt.Errorf("vault KV secret %q resolved without data", secretPath) + } + value, err := json.Marshal(data) + if err != nil { + return "", fmt.Errorf("encoding Vault KV secret %q as JSON: %w", secretPath, err) + } + return string(value), nil + }) +} + +// vaultKVClientCache lazily constructs one Vault client per builder. A builder +// is shared by all vault_kv entries in a transform pipeline, so the client's +// HTTP transport and connection pool are reused safely across entries. +type vaultKVClientCache struct { + mu sync.Mutex + client vaultKVClient + newClient func() (vaultKVClient, error) +} + +func (c *vaultKVClientCache) get(_ context.Context) (vaultKVClient, error) { + c.mu.Lock() + defer c.mu.Unlock() + if c.client != nil { + return c.client, nil + } + client, err := c.newClient() + if err != nil { + return nil, err + } + c.client = client + return c.client, nil +} + +type vaultAPIKVClient struct { + client *vaultapi.Client +} + +func (c vaultAPIKVClient) ReadKV(ctx context.Context, mount, secretPath string, version int) (map[string]any, error) { + var ( + secret *vaultapi.KVSecret + err error + ) + if version == 1 { + secret, err = c.client.KVv1(mount).Get(ctx, secretPath) + } else { + secret, err = c.client.KVv2(mount).Get(ctx, secretPath) + } + if err != nil { + return nil, err + } + if secret.Data == nil { + if secret.Raw != nil && len(secret.Raw.Warnings) > 0 { + return nil, fmt.Errorf("secret resolved without data: %s", strings.Join(secret.Raw.Warnings, "; ")) + } + return nil, fmt.Errorf("secret resolved without data") + } + return secret.Data, nil +} diff --git a/internal/transform/secrets/vault_kv_resolver_test.go b/internal/transform/secrets/vault_kv_resolver_test.go new file mode 100644 index 0000000..3b6de0b --- /dev/null +++ b/internal/transform/secrets/vault_kv_resolver_test.go @@ -0,0 +1,323 @@ +package secrets + +import ( + "context" + "fmt" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + + vaultapi "github.com/hashicorp/vault/api" + "github.com/stretchr/testify/require" +) + +type mockVaultKVClient struct { + read func(ctx context.Context, mount, secretPath string, version int) (map[string]any, error) +} + +func (m mockVaultKVClient) ReadKV(ctx context.Context, mount, secretPath string, version int) (map[string]any, error) { + return m.read(ctx, mount, secretPath, version) +} + +func staticVaultKVClient(data map[string]any, err error) vaultKVClient { + return &mockVaultKVClient{read: func(context.Context, string, string, int) (map[string]any, error) { + return data, err + }} +} + +func newTestVaultKVBuilder(client vaultKVClient) *vaultKVBuilder { + return &vaultKVBuilder{ + clientFor: func(context.Context) (vaultKVClient, error) { return client, nil }, + logger: slog.Default(), + } +} + +func TestVaultKVBuilder_ReadsKVVersions(t *testing.T) { + cases := []struct { + name string + configured int + wantVersion int + }{ + {name: "defaults to KV v2", configured: 0, wantVersion: 2}, + {name: "KV v1", configured: 1, wantVersion: 1}, + {name: "KV v2", configured: 2, wantVersion: 2}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + client := mockVaultKVClient{read: func(_ context.Context, mount, secretPath string, version int) (map[string]any, error) { + require.Equal(t, "secret", mount) + require.Equal(t, "services/openai", secretPath) + require.Equal(t, tc.wantVersion, version) + return map[string]any{"api_key": "real-value", "region": "us-east-1"}, nil + }} + builder := newTestVaultKVBuilder(client) + node := yamlNode(t, map[string]any{ + "type": "vault_kv", + "mount": "secret", + "path": "services/openai", + "kv_version": tc.configured, + }) + + source, err := builder.Build(node) + require.NoError(t, err) + require.Equal(t, "vault_kv:secret/services/openai", source.Name()) + + value, err := source.Get(context.Background()) + require.NoError(t, err) + require.JSONEq(t, `{"api_key":"real-value","region":"us-east-1"}`, value) + }) + } +} + +func TestVaultKVBuilder_JSONKey(t *testing.T) { + builder := newTestVaultKVBuilder(staticVaultKVClient(map[string]any{ + "api_key": "real-value", + "ignored": "other-value", + }, nil)) + node := yamlNode(t, map[string]any{ + "type": "vault_kv", + "mount": "secret", + "path": "services/openai", + "json_key": "api_key", + }) + + source, err := resolveSource(sourceBuilderRegistry{"vault_kv": builder}, node) + require.NoError(t, err) + value, err := source.Get(context.Background()) + require.NoError(t, err) + require.Equal(t, "real-value", value) +} + +func TestVaultKVBuilder_ConfigErrors(t *testing.T) { + cases := []struct { + name string + input map[string]any + errMsg string + }{ + { + name: "missing mount", + input: map[string]any{"type": "vault_kv", "path": "services/openai"}, + errMsg: `requires "mount" field`, + }, + { + name: "missing path", + input: map[string]any{"type": "vault_kv", "mount": "secret"}, + errMsg: `requires "path" field`, + }, + { + name: "unsupported KV version", + input: map[string]any{"type": "vault_kv", "mount": "secret", "path": "app", "kv_version": 3}, + errMsg: "kv_version must be 1 or 2", + }, + { + name: "invalid TTL", + input: map[string]any{"type": "vault_kv", "mount": "secret", "path": "app", "ttl": "later"}, + errMsg: "parsing ttl", + }, + { + name: "invalid failure TTL", + input: map[string]any{"type": "vault_kv", "mount": "secret", "path": "app", "failure_ttl": "later"}, + errMsg: "parsing failure_ttl", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + builder := newTestVaultKVBuilder(staticVaultKVClient(nil, nil)) + _, err := builder.Build(yamlNode(t, tc.input)) + require.Error(t, err) + require.ErrorContains(t, err, tc.errMsg) + }) + } +} + +func TestVaultKVBuilder_FetchErrors(t *testing.T) { + cases := []struct { + name string + clientFor func(context.Context) (vaultKVClient, error) + errMsg string + }{ + { + name: "client creation", + clientFor: func(context.Context) (vaultKVClient, error) { + return nil, fmt.Errorf("bad Vault environment") + }, + errMsg: "creating Vault client: bad Vault environment", + }, + { + name: "read failure", + clientFor: func(context.Context) (vaultKVClient, error) { + return staticVaultKVClient(nil, fmt.Errorf("permission denied")), nil + }, + errMsg: "permission denied", + }, + { + name: "missing data", + clientFor: func(context.Context) (vaultKVClient, error) { + return staticVaultKVClient(nil, nil), nil + }, + errMsg: "resolved without data", + }, + { + name: "unencodable data", + clientFor: func(context.Context) (vaultKVClient, error) { + return staticVaultKVClient(map[string]any{"bad": make(chan int)}, nil), nil + }, + errMsg: "encoding Vault KV secret", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + builder := &vaultKVBuilder{clientFor: tc.clientFor, logger: slog.Default()} + source, err := builder.Build(yamlNode(t, map[string]any{ + "type": "vault_kv", "mount": "secret", "path": "app", + })) + require.NoError(t, err) + _, err = source.Get(context.Background()) + require.Error(t, err) + require.ErrorContains(t, err, tc.errMsg) + }) + } +} + +func TestVaultKVBuilder_BuildIsLazy(t *testing.T) { + var calls atomic.Int64 + builder := &vaultKVBuilder{ + clientFor: func(context.Context) (vaultKVClient, error) { + calls.Add(1) + return staticVaultKVClient(map[string]any{"value": "secret"}, nil), nil + }, + logger: slog.Default(), + } + source, err := builder.Build(yamlNode(t, map[string]any{ + "type": "vault_kv", "mount": "secret", "path": "app", + })) + require.NoError(t, err) + require.Equal(t, int64(0), calls.Load()) + + _, err = source.Get(context.Background()) + require.NoError(t, err) + require.Equal(t, int64(1), calls.Load()) +} + +func TestVaultKVClientCache_ReusesClient(t *testing.T) { + var calls atomic.Int64 + want := staticVaultKVClient(map[string]any{"value": "secret"}, nil) + cache := &vaultKVClientCache{newClient: func() (vaultKVClient, error) { + calls.Add(1) + return want, nil + }} + + first, err := cache.get(context.Background()) + require.NoError(t, err) + second, err := cache.get(context.Background()) + require.NoError(t, err) + require.Same(t, want, first) + require.Same(t, want, second) + require.Equal(t, int64(1), calls.Load()) +} + +func TestVaultAPIKVClient_IncludesWarningsWhenDataMissing(t *testing.T) { + const warning = "Invalid path for a versioned K/V secrets engine" + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusNotFound) + // The in-memory server and response writer are torn down with the test; + // the client call below reports any response read failure. + _, _ = io.WriteString(w, `{"warnings":["`+warning+`"]}`) + })) + t.Cleanup(server.Close) + + client, err := vaultapi.NewClient(&vaultapi.Config{ + Address: server.URL, + HttpClient: server.Client(), + }) + require.NoError(t, err) + adapter := vaultAPIKVClient{client: client} + + cases := []struct { + name string + version int + }{ + {name: "KV v1", version: 1}, + {name: "KV v2", version: 2}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + _, err := adapter.ReadKV(context.Background(), "secret", "services/openai", tc.version) + require.Error(t, err) + require.ErrorContains(t, err, "secret resolved without data") + require.ErrorContains(t, err, warning) + }) + } +} + +func TestVaultKVBuilder_OfficialClientEnvironment(t *testing.T) { + type requestRecord struct { + path string + token string + namespace string + } + requests := make(chan requestRecord, 2) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests <- requestRecord{ + path: r.URL.Path, + token: r.Header.Get("X-Vault-Token"), + namespace: r.Header.Get("X-Vault-Namespace"), + } + w.Header().Set("Content-Type", "application/json") + // The test server and response writer are in-memory; a write failure is + // irrelevant because the client call below reports it as a read error. + if r.URL.Path == "/v1/secret/legacy/openai" { + _, _ = io.WriteString(w, `{"data":{"api_key":"kv1-value"}}`) + return + } + _, _ = io.WriteString(w, `{"data":{"data":{"api_key":"kv2-value"},"metadata":{}}}`) + })) + t.Cleanup(server.Close) + + // Clear endpoint overrides that may be present in a developer's shell, then + // set the standard environment consumed by vault/api.NewClient. + t.Setenv(vaultapi.EnvVaultAgentAddr, "") + t.Setenv(vaultapi.EnvVaultProxyAddr, "") + t.Setenv(vaultapi.EnvVaultCACert, "") + t.Setenv(vaultapi.EnvVaultCAPath, "") + t.Setenv(vaultapi.EnvVaultClientCert, "") + t.Setenv(vaultapi.EnvVaultClientKey, "") + t.Setenv(vaultapi.EnvVaultAddress, server.URL) + t.Setenv(vaultapi.EnvVaultToken, "test-token") + t.Setenv(vaultapi.EnvVaultNamespace, "engineering") + + cases := []struct { + name string + path string + version int + wantValue string + wantAPIPath string + }{ + {name: "KV v1", path: "legacy/openai", version: 1, wantValue: "kv1-value", wantAPIPath: "/v1/secret/legacy/openai"}, + {name: "KV v2", path: "services/openai", version: 2, wantValue: "kv2-value", wantAPIPath: "/v1/secret/data/services/openai"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + node := yamlNode(t, map[string]any{ + "type": "vault_kv", "mount": "secret", "path": tc.path, "kv_version": tc.version, "json_key": "api_key", + }) + source, err := resolveSource(defaultRegistry(slog.Default()), node) + require.NoError(t, err) + value, err := source.Get(context.Background()) + require.NoError(t, err) + require.Equal(t, tc.wantValue, value) + + record := <-requests + require.Equal(t, tc.wantAPIPath, record.path) + require.Equal(t, "test-token", record.token) + require.Equal(t, "engineering", record.namespace) + }) + } +} diff --git a/internal/transform/transform.go b/internal/transform/transform.go index 62505ab..ea48a2f 100644 --- a/internal/transform/transform.go +++ b/internal/transform/transform.go @@ -59,6 +59,10 @@ const ( // from a peeked TLS ClientHello SNI. Method, path, headers, and body are // all empty; transforms that depend on them cannot function. ModeSNIOnly + + // ModeSSH means the event came from the SSH listener. Method carries the + // SSH operation and Path its detail (route, command, or subsystem). + ModeSSH ) // String returns the canonical string form of the mode. @@ -68,6 +72,8 @@ func (m Mode) String() string { return "mitm" case ModeSNIOnly: return "sni-only" + case ModeSSH: + return "ssh" default: return "unknown" } @@ -175,6 +181,9 @@ type MCPAudit interface { // MCPMessages returns the audit messages in observed order. Each entry // is a flat key/value map suitable for JSON encoding (string-keyed). MCPMessages() []map[string]any + // MCPGateway returns gateway route metadata, or nil when no gateway route + // was applied. + MCPGateway() map[string]any } // BodyCapture is the read-only view of a body_capture transform's per-request diff --git a/iron-proxy.example.yaml b/iron-proxy.example.yaml index 3f42e25..3140e30 100644 --- a/iron-proxy.example.yaml +++ b/iron-proxy.example.yaml @@ -53,6 +53,8 @@ proxy: # Set an explicit list to override the defaults. Use an empty list # (upstream_deny_cidrs: []) if you intentionally need to allow IMDS or # loopback traffic to upstreams. + # The equivalent environment override is a comma-separated list: + # IRON_PROXY_UPSTREAM_DENY_CIDRS="169.254.169.254/32,127.0.0.0/8" # upstream_deny_cidrs: # - "169.254.169.254/32" # - "127.0.0.0/8" @@ -311,6 +313,42 @@ transforms: # rules: # - host: "*.googleapis.com" + # GCP OIDC ID token authentication. Mints a Google-signed ID token with + # the configured audience and sets Authorization: Bearer on matching + # requests. This is the token shape required by private Cloud Run services, + # Cloud Run functions, IAP, API Gateway, and other audience-authenticated + # Google targets. Tokens are cached and refreshed automatically before + # expiry. Requires MITM mode (sni-only mode cannot rewrite headers). On + # token-minting failure the request is rejected (403). + # + # The service account JSON can come from disk via keyfile_path, or from any + # configured secret source (env, aws_sm, aws_ssm, 1password, + # 1password_connect) via a nested keyfile block. Use exactly one. + # + # Cloud Run checks the aud claim against the receiving service URL or a + # configured custom audience. For Cloud Run, custom domains are not valid + # audience values. + # - name: gcp_id_token + # config: + # keyfile_path: "/etc/iron-proxy/cloud-run-caller.json" + # audience: "https://my-service-abc123-uc.a.run.app" + # # Optional. Use x-serverless-authorization when the upstream app needs + # # the Authorization header for its own application auth. + # header: authorization + # rules: + # - host: "my-service-abc123-uc.a.run.app" + # + # Same thing, service account JSON sourced from 1Password Connect: + # - name: gcp_id_token + # config: + # keyfile: + # type: 1password_connect + # secret_ref: "op://Engineering/Cloud-Run-Caller/credential" + # audience: "https://my-service-abc123-uc.a.run.app" + # header: x-serverless-authorization + # rules: + # - host: "my-service-abc123-uc.a.run.app" + # OAuth2 token injection. Mints short-lived OAuth2 access tokens and sets # Authorization: Bearer on matching requests. Each entry under tokens names # a grant type, its credential fields, and the hosts it applies to. Token @@ -661,8 +699,13 @@ transforms: # inside CTEs, subqueries, function arguments, etc.: # - SET ROLE / RESET ROLE / SET SESSION AUTHORIZATION / RESET SESSION # AUTHORIZATION — direct GUC mutation. -# - SELECT set_config('role', ...), SELECT pg_catalog.set_config(...) and -# the same for 'session_authorization' — function-call bypasses. +# - All calls to set_config, including schema-qualified and parameterized +# forms. Dynamic targets cannot be proven safe when the query is parsed. +# - The legacy wire-level FunctionCall protocol, which invokes functions by +# OID without carrying SQL text the proxy can inspect. +# - Function and procedure definitions whose bodies cannot be inspected. +# LANGUAGE sql and static LANGUAGE plpgsql bodies are parsed recursively; +# dynamic PL/pgSQL and unsupported languages are rejected. # - DO $$ ... $$ blocks — the plpgsql body is opaque to the SQL parser, so # we reject rather than risk an embedded role change. # - Multi-statement Simple Queries — kept as a single-statement guard. @@ -670,11 +713,11 @@ transforms: # Cannot be caught at the wire (defend at the database with role/grant config): # - User-defined SECURITY DEFINER functions that elevate internally. # - Dynamic SQL via plpgsql EXECUTE with a non-literal argument. -# - Functions that wrap set_config under a different name. +# - Calls to pre-existing functions that wrap set_config under another name. # # Limitations: client→proxy TLS is not supported (SSLRequest is refused); -# CancelRequest is not forwarded; one shared client credential per route -# (no per-user). +# CancelRequest is not forwarded; frontend messages are limited to 16 MiB; +# one shared client credential per route (no per-user). # # The proxy runs a single postgres listener: the postgres key is one object # with a bind address and a list of upstreams. A single listen address serves @@ -706,11 +749,12 @@ transforms: # # client issues runs as this role on the upstream database. # role: tenant_role # # Optional session variables (GUCs) the proxy SETs at session start, -# # before the SET ROLE. The proxy also pins these: a client may not SET, -# # RESET, or set_config them afterwards (nor RESET ALL / DISCARD ALL), so -# # a setting used as a security boundary (e.g. an RLS key) can't be -# # overridden. Names must be bare or dotted GUC identifiers; role and -# # session_authorization are reserved (use role above). +# # before the SET ROLE. The proxy also pins these: a client may not SET +# # or RESET them afterwards (nor RESET ALL / DISCARD ALL), so a setting +# # used as a security boundary (e.g. an RLS key) can't be overridden. +# # All set_config calls are blocked. Names must be bare or dotted GUC +# # identifiers; role and session_authorization are reserved (use role +# # above). # settings: # - name: app.tenant_id # value: centaur @@ -734,6 +778,11 @@ transforms: # # Env var that holds the bearer token. Defaults to IRON_MANAGEMENT_API_KEY. # api_key_env: "IRON_MANAGEMENT_API_KEY" +# Optional managed-mode control-plane settings. +# control_plane: +# # Base interval between sync polls. The poller adds jitter between polls. +# poll_interval: "10s" + # Logging log: level: "info" # debug, info, warn, error