diff --git a/docs/backend-waiting.md b/docs/backend-waiting.md new file mode 100644 index 0000000..d8d86d8 --- /dev/null +++ b/docs/backend-waiting.md @@ -0,0 +1,66 @@ +# Waiting for sleeping backends + +A tsjwt Gateway can hold authorized requests until the configured upstream is +connectable. The tailnet listener and WhoIs identity lookup remain inside tsjwt. +Waiting is disabled by default. Enable it only after the platform has a working +external demand metric and autoscaler for every backend of the Gateway: + +```yaml +metadata: + annotations: + tsjwt.dev/backend-wait-timeout: 60s +``` + +The controller validates a positive duration of at most two minutes and passes +it to the data plane as `-backend-wait-timeout`. Standalone data planes accept +that flag and `-backend-wait-max-pending` (default 128 per upstream). + +Requests are authorized before they register demand. Each request waits at most +the configured timeout, or until its caller disconnects. Concurrent waiters +share a readiness probe per upstream. The default probe opens a TCP connection +to the configured upstream; for Kubernetes Services this relies on normal +ready-endpoint publication. Do not use publishNotReadyAddresses or an upstream +proxy that accepts connections before the target app is ready. Core consumers +can inject a stronger readiness probe using WaitConfig.Ready. + +On readiness, the proxy reauthorizes the caller and mints a fresh assertion, +then forwards the original request once. The tenant selected on arrival stays +pinned through the wait, even if the caller's default changes; loss of access +to that tenant returns 403 rather than falling back to another tenant. It does not retry application requests +or replay POST bodies after forwarding. Failure or queue saturation returns 503 +with Retry-After. Waiting does not read/buffer the body; memory used for waiting +requests is bounded by request limits rather than body size. Active requests, +including upgraded connections, remain demand until proxying finishes. + +The gate exports the following on `/metrics` on the existing internal operations +listener (the listener serving JWKS and health), respecting its TLS setting: + +- `tsjwt_backend_pending_requests{upstream="..."}` +- `tsjwt_backend_active_requests{upstream="..."}` +- `tsjwt_backend_last_activity_seconds{upstream="..."}` + +These are process-local metrics keyed by configured upstream URL, shared across +route reloads. The platform must map ExternalName bridges and alternate ingress +paths to a stable backend identity and sum pending/active demand across them. +Retain a nonzero recommendation for the chosen idle window using last activity. +Metrics exist after the first authorized request, remain explicit zero when idle, +and are absent after restart until demand is seen again. Missing/stale metrics +must fail safe and never be converted to idle zero. Backends are retained for +explicit zero publication; a process admits at most 256 unique upstreams over +its lifetime, after which new ones return 503 until restarted. Labels never +contain caller-supplied hosts or paths. + +This feature does not scale applications, create HPAs, discover dependency +workloads, or install a metrics adapter. In particular it cannot wake an app +without the platform consuming its demand metric. Keep gates, identity services, +metrics infrastructure and databases running. Check every ingress path before +enabling suspension. Keep HPA as the only replica writer. + +Release the library first, then bump the deployment repository's pinned source +and enable the annotation for a disposable pilot through its normal CI/CD path. +After deployment validate the controller/dataplane rollout, Gateway conditions, +JWKS and unauthorized responses, then the pilot's idle → zero → held request → +ready → successful response lifecycle. Inspect demand metrics and HPA events. +Rollback through CI/CD: restore the workload's autoscaler minimum to one first, +wait for readiness, then disable waiting or revert the release. Never disable +the waiting gate while workloads remain asleep. diff --git a/gateway/cmd/tsjwt-dataplane/main.go b/gateway/cmd/tsjwt-dataplane/main.go index ca47681..9e544b2 100644 --- a/gateway/cmd/tsjwt-dataplane/main.go +++ b/gateway/cmd/tsjwt-dataplane/main.go @@ -47,28 +47,33 @@ func main() { func run() error { var ( - gatewayRef = flag.String("gateway", "", "the Gateway this serves, as namespace/name (required)") - routesPath = flag.String("routes", "/etc/tsjwt/routes.json", "the rendered route table, normally a mounted ConfigMap") - routeCache = flag.String("route-cache", "", "where to keep the last table that parsed, so a restart during an outage still has routes; defaults to /routes.cache.json") - hostname = flag.String("hostname", "", "tailnet hostname; defaults to the Gateway name") - stateDir = flag.String("state-dir", "/var/lib/tsjwt", "directory for tailnet node state") - issuer = flag.String("issuer", "", "iss claim; defaults to https://") - tenants = flag.String("tenants", "", "path to the tenant policy JSON file (required)") - listen = flag.String("listen", ":443", "tailnet address to listen on") - jwksListen = flag.String("jwks-listen", "0.0.0.0:9100", "ordinary address for the key set and health") - header = flag.String("header", tsjwt.DefaultHeader, "header the assertion is injected into") - bearer = flag.Bool("bearer", false, `write the assertion as "Bearer "`) - capName = flag.String("cap", string(tsnetid.CapGroups), "tailnet capability carrying the caller's groups") - ttl = flag.Duration("ttl", signer.DefaultTTL, "token lifetime") - rotate = flag.Duration("rotate", 24*time.Hour, "signing key rotation interval") - overlap = flag.Duration("overlap", time.Hour, "how long a retired key still verifies") - resync = flag.Duration("resync", 5*time.Second, "how often to re-read routes") - keyTags = flag.String("key-tags", "", "comma-separated tags for an auth key minted at startup") - shareCM = flag.String("share-keys", "", "publish verification keys to this ConfigMap") - plaintext = flag.Bool("plaintext", false, "serve HTTP instead of HTTPS") - jwksTLS = flag.Bool("jwks-tls", false, "serve the key set over HTTPS, using this node's own certificate") + waitTimeout = flag.Duration("backend-wait-timeout", 0, "hold authorized requests for backend readiness; zero disables waiting") + waitPending = flag.Int("backend-wait-max-pending", 128, "maximum waiting requests per upstream") + gatewayRef = flag.String("gateway", "", "the Gateway this serves, as namespace/name (required)") + routesPath = flag.String("routes", "/etc/tsjwt/routes.json", "the rendered route table, normally a mounted ConfigMap") + routeCache = flag.String("route-cache", "", "where to keep the last table that parsed, so a restart during an outage still has routes; defaults to /routes.cache.json") + hostname = flag.String("hostname", "", "tailnet hostname; defaults to the Gateway name") + stateDir = flag.String("state-dir", "/var/lib/tsjwt", "directory for tailnet node state") + issuer = flag.String("issuer", "", "iss claim; defaults to https://") + tenants = flag.String("tenants", "", "path to the tenant policy JSON file (required)") + listen = flag.String("listen", ":443", "tailnet address to listen on") + jwksListen = flag.String("jwks-listen", "0.0.0.0:9100", "ordinary address for the key set and health") + header = flag.String("header", tsjwt.DefaultHeader, "header the assertion is injected into") + bearer = flag.Bool("bearer", false, `write the assertion as "Bearer "`) + capName = flag.String("cap", string(tsnetid.CapGroups), "tailnet capability carrying the caller's groups") + ttl = flag.Duration("ttl", signer.DefaultTTL, "token lifetime") + rotate = flag.Duration("rotate", 24*time.Hour, "signing key rotation interval") + overlap = flag.Duration("overlap", time.Hour, "how long a retired key still verifies") + resync = flag.Duration("resync", 5*time.Second, "how often to re-read routes") + keyTags = flag.String("key-tags", "", "comma-separated tags for an auth key minted at startup") + shareCM = flag.String("share-keys", "", "publish verification keys to this ConfigMap") + plaintext = flag.Bool("plaintext", false, "serve HTTP instead of HTTPS") + jwksTLS = flag.Bool("jwks-tls", false, "serve the key set over HTTPS, using this node's own certificate") ) flag.Parse() + if *waitTimeout < 0 || *waitTimeout > 2*time.Minute || *waitPending <= 0 { + return errors.New("backend waiting requires a timeout between 0 and 2m and a positive pending limit") + } ns, name, ok := strings.Cut(*gatewayRef, "/") if !ok || ns == "" || name == "" { @@ -184,8 +189,20 @@ func run() error { // The route table starts empty. That is not an error state: a Gateway // with no routes yet answers 503, which tells a caller to retry rather // than that the path is wrong. + var gate *proxy.WaitGate + var requestGate proxy.RequestGate + if *waitTimeout < 0 { + return errors.New("-backend-wait-timeout must not be negative") + } + if *waitTimeout > 0 { + gate, err = proxy.NewWaitGate(proxy.WaitConfig{Timeout: *waitTimeout, Interval: 250 * time.Millisecond, MaxPending: *waitPending, MaxBackends: 256}) + if err != nil { + return err + } + requestGate = gate + } dyn, err := proxy.NewDynamic(proxy.Config{ - Signer: sg, Header: *header, Bearer: *bearer, + Signer: sg, Header: *header, Bearer: *bearer, Gate: requestGate, }, nil) if err != nil { return err @@ -286,6 +303,9 @@ func run() error { }) // Fixed, small payloads (JWKS and a status line): bound the whole // request and response, not just the header, against a slow client. + if gate != nil { + ops.Handle("/metrics", gate) + } opsSrv := &http.Server{ Addr: *jwksListen, Handler: ops, diff --git a/gateway/go.mod b/gateway/go.mod index 07114d7..f6ff3bb 100644 --- a/gateway/go.mod +++ b/gateway/go.mod @@ -58,7 +58,7 @@ require ( github.com/huin/goupnp v1.3.0 // indirect github.com/jsimonetti/rtnetlink v1.4.1 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/compress v1.19.1 // indirect + github.com/klauspost/compress v1.20.2 // indirect github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42 // indirect github.com/mdlayher/socket v0.5.0 // indirect github.com/mitchellh/go-ps v1.0.0 // indirect @@ -86,14 +86,14 @@ require ( go.yaml.in/yaml/v3 v3.0.4 // indirect go4.org/mem v0.0.0-20240501181205-ae6ca9944745 // indirect go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect - golang.org/x/crypto v0.54.0 // indirect + golang.org/x/crypto v0.57.0 // indirect golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f // indirect - golang.org/x/net v0.57.0 // indirect + golang.org/x/net v0.60.0 // indirect golang.org/x/oauth2 v0.36.0 // indirect - golang.org/x/sync v0.22.0 // indirect - golang.org/x/sys v0.47.0 // indirect - golang.org/x/term v0.45.0 // indirect - golang.org/x/text v0.40.0 // indirect + golang.org/x/sync v0.23.0 // indirect + golang.org/x/sys v0.48.0 // indirect + golang.org/x/term v0.46.0 // indirect + golang.org/x/text v0.42.0 // indirect golang.org/x/time v0.15.0 // indirect golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect golang.zx2c4.com/wireguard/windows v0.5.3 // indirect diff --git a/gateway/go.sum b/gateway/go.sum index 79cbfe5..bd33128 100644 --- a/gateway/go.sum +++ b/gateway/go.sum @@ -180,6 +180,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk= github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/klauspost/compress v1.20.2 h1:v5tiZvm1KGLyg5d5oXdXe3lsSPrbEUiKoXGia//I7Qo= +github.com/klauspost/compress v1.20.2/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= github.com/kortschak/wol v0.0.0-20200729010619-da482cc4850a h1:+RR6SqnTkDLWyICxS1xpjCi/3dhyV+TgZwA6Ww3KncQ= github.com/kortschak/wol v0.0.0-20200729010619-da482cc4850a/go.mod h1:YTtCCM3ryyfiu4F7t8HQ1mxvp1UBdWM2r6Xa+nGWvDk= github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= @@ -295,6 +297,8 @@ go4.org/netipx v0.0.0-20231129151722-fdeea329fbba h1:0b9z3AuHCjxk0x/opv64kcgZLBs go4.org/netipx v0.0.0-20231129151722-fdeea329fbba/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y= golang.org/x/crypto v0.54.0 h1:YLIA59K4fiNzHzjnZt2tUJQjQtUWfWbeHBqKtk3eScw= golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk= +golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= +golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f h1:W3F4c+6OLc6H2lb//N1q4WpJkhzJCK5J6kUi1NTVXfM= golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f/go.mod h1:J1xhfL/vlindoeF/aINzNzt2Bket5bjo9sdOYzOsU80= golang.org/x/exp/typeparams v0.0.0-20240314144324-c7f7c6466f7f h1:phY1HzDcf18Aq9A8KkmRtY9WvOFIxN8wgfvy6Zm1DV8= @@ -305,18 +309,25 @@ golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ= golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0= golang.org/x/net v0.57.0 h1:K5+3DljvIuDG9/Jv9rvyMywYNFCQ9RSUY6OOTTkT+tE= golang.org/x/net v0.57.0/go.mod h1:KpXc8iv+r3XplLAG/f7Jsf9RPszJzdR0f58q9vGOuEU= +golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU= +golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= 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.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 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/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= +golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= 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/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= 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/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc= golang.org/x/text v0.40.0 h1:Ub2Z6/xjgF1WrYQz2nuITOEegKFtiIy+rieRJ5lHZKs= golang.org/x/text v0.40.0/go.mod h1:hpnzDAfGV753zIKo+wk3u1bVKCGPbrnF7+7LBF/UHVY= +golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U= golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno= golang.org/x/tools v0.47.0 h1:7Kn5x/d1svx/PzryTsqeoZN4TZwqeH5pGWjefhLi/1Q= diff --git a/gateway/provision.go b/gateway/provision.go index ca1358c..4b6b604 100644 --- a/gateway/provision.go +++ b/gateway/provision.go @@ -3,6 +3,7 @@ package gateway import ( "fmt" "strings" + "time" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -191,6 +192,11 @@ func DataPlaneServiceAccount(gw *gwapi.Gateway) *corev1.ServiceAccount { // So it is per Gateway, and defaults off. const JWKSOverTLSAnnotation = "tsjwt.dev/jwks-tls" +// BackendWaitAnnotation opts one Gateway into bounded backend waiting. The +// value is a positive Go duration of at most two minutes. This enables demand +// metrics but never creates an autoscaler or changes application replicas. +const BackendWaitAnnotation = "tsjwt.dev/backend-wait-timeout" + // KeysRole lets one Gateway's data planes publish their verification keys, // and nothing else. // @@ -257,6 +263,13 @@ func StatefulSet(gw *gwapi.Gateway, cfg Config, tenantsConfigMap string) (*appsv if cfg.Tag == "" { return nil, fmt.Errorf("gateway: Config.Tag is required") } + wait := gw.Annotations[BackendWaitAnnotation] + if wait != "" { + d, err := time.ParseDuration(wait) + if err != nil || d <= 0 || d > 2*time.Minute { + return nil, fmt.Errorf("gateway: %s must be a positive duration no greater than 2m", BackendWaitAnnotation) + } + } n := names{gw} replicas := int32(1) @@ -284,6 +297,9 @@ func StatefulSet(gw *gwapi.Gateway, cfg Config, tenantsConfigMap string) (*appsv "-share-keys=" + n.keys(), } + if wait != "" { + args = append(args, "-backend-wait-timeout="+wait) + } env := []corev1.EnvVar{ {Name: "POD_NAME", ValueFrom: &corev1.EnvVarSource{ FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.name"}}}, diff --git a/gateway/wait_test.go b/gateway/wait_test.go new file mode 100644 index 0000000..52d8fa4 --- /dev/null +++ b/gateway/wait_test.go @@ -0,0 +1,37 @@ +package gateway_test + +import ( + "github.com/bitcomplete/tsjwt/gateway" + "strings" + "testing" +) + +func TestBackendWaitIsOptInAndValidated(t *testing.T) { + for _, tc := range []struct { + value string + valid bool + }{ + {"", true}, {"60s", true}, {"2m", true}, {"0s", false}, {"-1s", false}, {"3m", false}, {"true", false}, + } { + t.Run(tc.value, func(t *testing.T) { + gw := gatewayWithClass("edge", "infra", "tsjwt") + gw.Annotations = map[string]string{gateway.BackendWaitAnnotation: tc.value} + sts, err := gateway.StatefulSet(gw, gateway.Config{Image: "img", CredentialSecret: "creds", Tag: "tag:example"}, "tenants") + if (err == nil) != tc.valid { + t.Fatalf("error %v, valid %v", err, tc.valid) + } + if err != nil { + return + } + found := "" + for _, arg := range sts.Spec.Template.Spec.Containers[0].Args { + if strings.HasPrefix(arg, "-backend-wait-timeout=") { + found = strings.TrimPrefix(arg, "-backend-wait-timeout=") + } + } + if found != tc.value { + t.Fatalf("wait arg %q, want %q", found, tc.value) + } + }) + } +} diff --git a/go.work.sum b/go.work.sum index d23ddd4..3b0a5fb 100644 --- a/go.work.sum +++ b/go.work.sum @@ -859,6 +859,7 @@ github.com/goccy/go-yaml v1.12.0/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHv github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/godbus/dbus v0.0.0-20151105175453-c7fdd8b5cd55/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw= github.com/godbus/dbus v0.0.0-20180201030542-885f9cc04c9c/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw= +github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e h1:BWhy2j3IXJhjCbC68FptL43tDKIq8FladmaTs3Xs7Z8= github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -1473,6 +1474,7 @@ github.com/ryanrolds/sqlclosecheck v0.5.1/go.mod h1:2g3dUjoS6AL4huFdv6wn55WpLIDj github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4= +github.com/safchain/ethtool v0.4.0 h1:vq1i2HCjshJNywOXFZ1BpwIjyeFR/kvNdHiRzqSElDI= github.com/safchain/ethtool v0.4.0/go.mod h1:XLLnZmy4OCRTkksP/UiMjij96YmIsBfmBQcs7H6tA48= github.com/sagikazarmark/crypt v0.17.0/go.mod h1:SMtHTvdmsZMuY/bpZoqokSoChIrcJ/epOxZN58PbZDg= github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= @@ -1880,6 +1882,7 @@ golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w= golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU= golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w= golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU= +golang.org/x/mod v0.39.0/go.mod h1:bvIbwjQ0HUFFf5AKukeeYQG4ZBUG9yxQbR9aEweIwYY= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -1964,6 +1967,7 @@ golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw= golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs= golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww= golang.org/x/net v0.56.0/go.mod h1:D3Ku6r+V6JROoZK144D2XfMHFcMq/0zSfLelVTCFKec= +golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -2140,6 +2144,7 @@ golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= golang.org/x/telemetry v0.0.0-20250908211612-aef8a434d053/go.mod h1:+nZKN+XVh4LCiA9DV3ywrzN4gumyCnKjau3NGb9SGoE= golang.org/x/telemetry v0.0.0-20260625142307-59b4966ccb57/go.mod h1:3AWMyWHS+caVoiEXpiq6+tzKA40J4vQT3MYr80ZtQpc= +golang.org/x/telemetry v0.0.0-20260811182544-a038080d80e5/go.mod h1:LVehoXe41cL5SCVQilsV7Gg6BNG+Js6P9PhSbYTIUkQ= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/proxy/proxy.go b/proxy/proxy.go index 0e33a66..17b9fa8 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -63,6 +63,9 @@ type Config struct { // a gateway; see newTransport for why the default is not suitable. Transport http.RoundTripper + // Gate optionally holds authorized requests until the backend is ready. + Gate RequestGate + // MaxIdleConnsPerHost bounds kept-alive connections to each backend. // Zero uses a gateway-appropriate default. Ignored when Transport is // set. @@ -187,6 +190,29 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { p.refuse(w, r, err) return } + if p.cfg.Gate != nil { + release, waitErr := p.cfg.Gate.Acquire(r.Context(), route) + if waitErr != nil { + p.log.Warn("backend wait failed", "route", route.Name, "err", waitErr) + w.Header().Set("Retry-After", "5") + http.Error(w, "backend unavailable", http.StatusServiceUnavailable) + return + } + defer release() + if r.Context().Err() != nil { + return + } + // Reauthorize and mint only the assertion forwarded to the app after + // waiting. No token minted before a cold start leaves this process. + // Preserve the tenant selected on arrival, including a default. + // A policy change must revoke this request or retain its tenant, + // never redirect a pending mutation into another tenant. + tok, claims, err = p.cfg.Signer.Mint(r.RemoteAddr, route.Audience, claims.Tenant) + if err != nil { + p.refuse(w, r, err) + return + } + } if p.cfg.Bearer { r.Header.Set(p.cfg.Header, "Bearer "+tok) } else { diff --git a/proxy/wait.go b/proxy/wait.go new file mode 100644 index 0000000..6fa0192 --- /dev/null +++ b/proxy/wait.go @@ -0,0 +1,183 @@ +package proxy + +import ( + "context" + "fmt" + "net" + "net/http" + "net/url" + "strconv" + "strings" + "sync" + "time" +) + +// RequestGate holds an authorized request until its backend is available. +// Acquire must publish demand before waiting. The returned release function +// tracks demand through the entire response, including upgraded connections. +// On failure Acquire must clean up its own demand. +type RequestGate interface { + Acquire(context.Context, Route) (release func(), err error) +} + +// WaitConfig configures a bounded gate. It does not change replica counts. +type WaitConfig struct { + Timeout time.Duration + Interval time.Duration + MaxPending int + MaxBackends int + // Ready checks a trusted, configured upstream without sending the user's + // request. Nil checks TCP connectivity to the upstream Service. Kubernetes + // Services must publish only ready endpoints; custom deployments can supply + // an application readiness check instead. + Ready func(context.Context, *url.URL) error +} + +type demand struct { + pending, active int + lastActivity time.Time + checking bool + changed chan struct{} + cancel context.CancelFunc +} + +// WaitGate shares readiness checks per upstream and exports demand in +// Prometheus text format. Share one instance across dynamic route reloads. +// Entries are retained to export explicit idle zeros; MaxBackends bounds +// cardinality and new backends fail closed once the limit is reached. +type WaitGate struct { + cfg WaitConfig + mu sync.Mutex + backends map[string]*demand +} + +func NewWaitGate(cfg WaitConfig) (*WaitGate, error) { + if cfg.Timeout <= 0 || cfg.Interval <= 0 || cfg.MaxPending <= 0 || cfg.MaxBackends <= 0 { + return nil, fmt.Errorf("wait gate: timeout, interval and limits must be positive") + } + if cfg.Ready == nil { + cfg.Ready = tcpReady + } + return &WaitGate{cfg: cfg, backends: make(map[string]*demand)}, nil +} + +func tcpReady(ctx context.Context, u *url.URL) error { + port := u.Port() + if port == "" { + if u.Scheme == "https" { + port = "443" + } else { + port = "80" + } + } + c, err := (&net.Dialer{Timeout: time.Second}).DialContext(ctx, "tcp", net.JoinHostPort(u.Hostname(), port)) + if err == nil { + c.Close() + } + return err +} + +func (g *WaitGate) Acquire(ctx context.Context, r Route) (func(), error) { + ctx, cancel := context.WithTimeout(ctx, g.cfg.Timeout) + defer cancel() + if err := ctx.Err(); err != nil { + return nil, err + } + key := r.Upstream.String() + g.mu.Lock() + d := g.backends[key] + if d == nil { + if len(g.backends) >= g.cfg.MaxBackends { + g.mu.Unlock() + return nil, fmt.Errorf("wait gate: backend limit") + } + d = &demand{} + g.backends[key] = d + } + if d.pending >= g.cfg.MaxPending { + g.mu.Unlock() + return nil, fmt.Errorf("wait gate: pending limit") + } + d.pending++ + d.lastActivity = time.Now() + if !d.checking { + probeCtx, stop := context.WithCancel(context.Background()) + d.checking, d.changed, d.cancel = true, make(chan struct{}), stop + go g.check(probeCtx, r.Upstream, d, d.changed) + } + ready := d.changed + g.mu.Unlock() + var err error + select { + case <-ctx.Done(): + err = ctx.Err() + case <-ready: + err = ctx.Err() + } + g.mu.Lock() + d.pending-- + if d.pending == 0 && d.checking { + d.cancel() + d.checking = false + } + if err == nil { + d.active++ + } + d.lastActivity = time.Now() + g.mu.Unlock() + if err != nil { + return nil, err + } + var once sync.Once + return func() { + once.Do(func() { + g.mu.Lock() + d.active-- + d.lastActivity = time.Now() + g.mu.Unlock() + }) + }, nil +} + +func (g *WaitGate) check(ctx context.Context, u *url.URL, d *demand, ready chan struct{}) { + for { + probeCtx, cancel := context.WithTimeout(ctx, time.Second) + err := g.cfg.Ready(probeCtx, u) + cancel() + if err == nil { + g.mu.Lock() + // A cancelled generation must never signal a newer batch of requests. + if d.checking && d.changed == ready { + close(ready) + d.checking = false + d.cancel() + } + g.mu.Unlock() + return + } + timer := time.NewTimer(g.cfg.Interval) + select { + case <-ctx.Done(): + timer.Stop() + return + case <-timer.C: + } + } +} + +// ServeHTTP exposes only configured upstream labels, never caller-controlled +// paths or host headers. Scrape on the internal operations listener. +func (g *WaitGate) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8") + var out strings.Builder + g.mu.Lock() + for key, d := range g.backends { + label := strconv.Quote(key) + fmt.Fprintf(&out, "tsjwt_backend_pending_requests{upstream=%s} %d\n", label, d.pending) + fmt.Fprintf(&out, "tsjwt_backend_active_requests{upstream=%s} %d\n", label, d.active) + fmt.Fprintf(&out, "tsjwt_backend_last_activity_seconds{upstream=%s} %d\n", label, d.lastActivity.Unix()) + } + g.mu.Unlock() + // Never hold the demand lock while writing to a potentially slow scraper. + fmt.Fprint(w, out.String()) +} diff --git a/proxy/wait_test.go b/proxy/wait_test.go new file mode 100644 index 0000000..a7bc307 --- /dev/null +++ b/proxy/wait_test.go @@ -0,0 +1,343 @@ +package proxy_test + +import ( + "context" + "errors" + "io" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/bitcomplete/tsjwt" + "github.com/bitcomplete/tsjwt/keys" + "github.com/bitcomplete/tsjwt/proxy" + "github.com/bitcomplete/tsjwt/signer" + "github.com/bitcomplete/tsjwt/verifier" +) + +func waitGate(t *testing.T, ready func(context.Context, *url.URL) error, max int) *proxy.WaitGate { + t.Helper() + g, err := proxy.NewWaitGate(proxy.WaitConfig{Timeout: time.Second, Interval: time.Millisecond, MaxPending: max, MaxBackends: 2, Ready: ready}) + if err != nil { + t.Fatal(err) + } + return g +} +func gateMetrics(g *proxy.WaitGate) string { + w := httptest.NewRecorder() + g.ServeHTTP(w, httptest.NewRequest("GET", "/metrics", nil)) + return w.Body.String() +} +func awaitMetric(t *testing.T, g *proxy.WaitGate, want string) { + t.Helper() + deadline := time.Now().Add(3 * time.Second) + for time.Now().Before(deadline) { + if strings.Contains(gateMetrics(g), want) { + return + } + time.Sleep(time.Millisecond) + } + t.Fatalf("missing metric %q: %s", want, gateMetrics(g)) +} +func TestWaitGateBoundsAndCancelsPending(t *testing.T) { + started := make(chan struct{}, 1) + g := waitGate(t, func(ctx context.Context, _ *url.URL) error { + select { + case started <- struct{}{}: + default: + } + <-ctx.Done() + return ctx.Err() + }, 1) + u, _ := url.Parse("http://app.test") + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan error, 1) + go func() { _, err := g.Acquire(ctx, proxy.Route{Upstream: u}); done <- err }() + <-started + if _, err := g.Acquire(context.Background(), proxy.Route{Upstream: u}); err == nil { + t.Fatal("pending limit not enforced") + } + cancel() + if err := <-done; !errors.Is(err, context.Canceled) { + t.Fatalf("cancel: %v", err) + } + awaitMetric(t, g, "tsjwt_backend_pending_requests{upstream=\"http://app.test\"} 0") +} +func TestWaitGateTracksActiveUntilReleased(t *testing.T) { + g := waitGate(t, func(context.Context, *url.URL) error { return nil }, 2) + u, _ := url.Parse("http://app.test") + release, err := g.Acquire(context.Background(), proxy.Route{Upstream: u}) + if err != nil { + t.Fatal(err) + } + awaitMetric(t, g, "tsjwt_backend_active_requests{upstream=\"http://app.test\"} 1") + release() + release() + awaitMetric(t, g, "tsjwt_backend_active_requests{upstream=\"http://app.test\"} 0") +} +func TestProxyWaitsThenDeliversPOSTOnce(t *testing.T) { + var calls atomic.Int32 + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls.Add(1); b, _ := io.ReadAll(r.Body); w.Write(b) })) + defer backend.Close() + ready := make(chan struct{}) + g := waitGate(t, func(ctx context.Context, _ *url.URL) error { + select { + case <-ready: + return nil + case <-ctx.Done(): + return ctx.Err() + } + }, 8) + u, _ := url.Parse(backend.URL) + p, err := proxy.New(proxy.Config{Upstream: u, Audience: "app", Signer: dynamicSigner(t), Gate: g}) + if err != nil { + t.Fatal(err) + } + done := make(chan *httptest.ResponseRecorder, 1) + go func() { + w := httptest.NewRecorder() + p.ServeHTTP(w, httptest.NewRequest("POST", "/", strings.NewReader("one body"))) + done <- w + }() + awaitMetric(t, g, "tsjwt_backend_pending_requests{upstream="+`"`+backend.URL+`"} 1`) + if calls.Load() != 0 { + t.Fatal("request forwarded before readiness") + } + close(ready) + w := <-done + if w.Code != 200 || w.Body.String() != "one body" || calls.Load() != 1 { + t.Fatalf("response %d %s, calls %d", w.Code, w.Body.String(), calls.Load()) + } + awaitMetric(t, g, "tsjwt_backend_active_requests{upstream="+`"`+backend.URL+`"} 0`) +} +func TestUnauthorizedRequestNeverRegistersDemand(t *testing.T) { + g := waitGate(t, func(context.Context, *url.URL) error { t.Error("unauthorized request probed backend"); return nil }, 1) + u, _ := url.Parse("http://app.test") + p, err := proxy.New(proxy.Config{Upstream: u, Audience: "app", Signer: dynamicSigner(t), Gate: g, TenantFrom: func(*http.Request) string { return "forbidden" }}) + if err != nil { + t.Fatal(err) + } + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + r.Header.Set(tsjwt.DefaultHeader, "forged") + p.ServeHTTP(w, r) + if w.Code != 403 { + t.Fatalf("status %d", w.Code) + } + if got := gateMetrics(g); got != "" { + t.Fatalf("unauthorized demand: %s", got) + } + if r.Header.Get(tsjwt.DefaultHeader) != "" { + t.Fatal("assertion not stripped") + } +} +func TestWaitGateDeadlineReturns503WithoutForwarding(t *testing.T) { + g := waitGate(t, func(ctx context.Context, _ *url.URL) error { <-ctx.Done(); return ctx.Err() }, 1) + u, _ := url.Parse("http://app.test") + p, err := proxy.New(proxy.Config{Upstream: u, Audience: "app", Signer: dynamicSigner(t), Gate: g}) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) + defer cancel() + w := httptest.NewRecorder() + p.ServeHTTP(w, httptest.NewRequest("POST", "/", nil).WithContext(ctx)) + if w.Code != 503 || w.Header().Get("Retry-After") == "" { + t.Fatalf("response %d %v", w.Code, w.Header()) + } + awaitMetric(t, g, "tsjwt_backend_pending_requests{upstream=\"http://app.test\"} 0") +} + +func TestWaitGateSharesReadinessCheck(t *testing.T) { + var probes atomic.Int32 + ready := make(chan struct{}) + g := waitGate(t, func(ctx context.Context, _ *url.URL) error { + probes.Add(1) + select { + case <-ready: + return nil + case <-ctx.Done(): + return ctx.Err() + } + }, 32) + u, _ := url.Parse("http://app.test") + results := make(chan error, 32) + for i := 0; i < 32; i++ { + go func() { + release, err := g.Acquire(context.Background(), proxy.Route{Upstream: u}) + if err == nil { + release() + } + results <- err + }() + } + awaitMetric(t, g, "tsjwt_backend_pending_requests{upstream=\"http://app.test\"} 32") + if probes.Load() != 1 { + t.Fatalf("readiness probes %d, want one", probes.Load()) + } + close(ready) + for i := 0; i < 32; i++ { + if err := <-results; err != nil { + t.Fatal(err) + } + } + awaitMetric(t, g, "tsjwt_backend_active_requests{upstream=\"http://app.test\"} 0") +} + +func TestWaitGateBoundsMetricCardinality(t *testing.T) { + g := waitGate(t, func(context.Context, *url.URL) error { return nil }, 1) + for _, host := range []string{"one.test", "two.test", "three.test"} { + u, _ := url.Parse("http://" + host) + release, err := g.Acquire(context.Background(), proxy.Route{Upstream: u}) + if host == "three.test" { + if err == nil { + release() + t.Fatal("backend limit not enforced") + } + } else { + if err != nil { + t.Fatal(err) + } + release() + } + } + if strings.Contains(gateMetrics(g), "three.test") { + t.Fatal("unbounded metrics") + } +} + +type gateFunc func(context.Context, proxy.Route) (func(), error) + +func (f gateFunc) Acquire(ctx context.Context, r proxy.Route) (func(), error) { return f(ctx, r) } + +func TestProxyReauthorizesAfterWaiting(t *testing.T) { + identity := &FakeIdentitySource{identity: tsjwt.Identity{Subject: "user"}} + set, _ := keys.NewSet() + sg, _ := signer.New(signer.Config{Issuer: "https://test", Keys: set, Identities: identity, Tenants: tsjwt.StaticGrant(tsjwt.Grant{Tenants: []tsjwt.Tenant{{ID: "acme", Roles: []string{"admin"}}}, Default: "acme"})}) + released := false + u, _ := url.Parse("http://app.test") + p, err := proxy.New(proxy.Config{Upstream: u, Audience: "app", Signer: sg, Gate: gateFunc(func(context.Context, proxy.Route) (func(), error) { + identity.err = tsjwt.ErrNoIdentity + return func() { released = true }, nil + })}) + if err != nil { + t.Fatal(err) + } + w := httptest.NewRecorder() + p.ServeHTTP(w, httptest.NewRequest("GET", "/", nil)) + if w.Code != 401 || !released { + t.Fatalf("status %d released %v", w.Code, released) + } +} + +func TestProxyMintsFreshAssertionAfterColdStart(t *testing.T) { + now := time.Now().Truncate(time.Second) + set, _ := keys.NewSet() + sg, _ := signer.New(signer.Config{Issuer: "https://test", Keys: set, Identities: &FakeIdentitySource{identity: tsjwt.Identity{Subject: "user"}}, Tenants: tsjwt.StaticGrant(tsjwt.Grant{Tenants: []tsjwt.Tenant{{ID: "acme", Roles: []string{"admin"}}}, Default: "acme"}), Now: func() time.Time { return now }}) + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + v, _ := verifier.New(verifier.Config{Issuer: "https://test", Audience: "app", Keys: verifier.LocalKeys{Set: set}, Now: func() time.Time { return now }}) + claims, err := v.Verify(r.Context(), r.Header.Get(tsjwt.DefaultHeader)) + if err != nil || claims.IssuedAt != now.Unix() { + t.Errorf("stale assertion: %v, issued %d", err, claims.IssuedAt) + } + })) + defer backend.Close() + u, _ := url.Parse(backend.URL) + p, err := proxy.New(proxy.Config{Upstream: u, Audience: "app", Signer: sg, Gate: gateFunc(func(context.Context, proxy.Route) (func(), error) { + now = now.Add(6 * time.Minute) + return func() {}, nil + })}) + if err != nil { + t.Fatal(err) + } + w := httptest.NewRecorder() + p.ServeHTTP(w, httptest.NewRequest("GET", "/", nil)) + if w.Code != 200 { + t.Fatalf("response %d", w.Code) + } +} + +func TestProxyCountsDemandUntilStreamingResponseEnds(t *testing.T) { + started := make(chan struct{}) + finish := make(chan struct{}) + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + w.(http.Flusher).Flush() + close(started) + <-finish + w.Write([]byte("finished")) + })) + defer backend.Close() + g := waitGate(t, func(context.Context, *url.URL) error { return nil }, 1) + u, _ := url.Parse(backend.URL) + p, err := proxy.New(proxy.Config{Upstream: u, Audience: "app", Signer: dynamicSigner(t), Gate: g}) + if err != nil { + t.Fatal(err) + } + done := make(chan struct{}) + go func() { defer close(done); p.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil)) }() + <-started + metrics := gateMetrics(g) + close(finish) + <-done + if !strings.Contains(metrics, "tsjwt_backend_active_requests{upstream="+`"`+backend.URL+`"} 1`) { + t.Fatalf("stream not counted: %s", metrics) + } + awaitMetric(t, g, "tsjwt_backend_active_requests{upstream="+`"`+backend.URL+`"} 0`) +} + +type changingTenantResolver struct{ grant tsjwt.Grant } + +func (r *changingTenantResolver) Resolve(tsjwt.Identity) (tsjwt.Grant, error) { return r.grant, nil } + +func TestWaitingRequestPreservesSelectedTenant(t *testing.T) { + for _, revoke := range []bool{false, true} { + t.Run(map[bool]string{false: "default changes", true: "original access revoked"}[revoke], func(t *testing.T) { + tenants := []tsjwt.Tenant{{ID: "original", Roles: []string{"admin"}}, {ID: "other", Roles: []string{"admin"}}} + resolver := &changingTenantResolver{grant: tsjwt.Grant{Tenants: tenants, Default: "original"}} + set, _ := keys.NewSet() + sg, err := signer.New(signer.Config{Issuer: "https://test", Keys: set, Identities: &FakeIdentitySource{identity: tsjwt.Identity{Subject: "user"}}, Tenants: resolver}) + if err != nil { + t.Fatal(err) + } + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + v, _ := verifier.New(verifier.Config{Issuer: "https://test", Audience: "app", Keys: verifier.LocalKeys{Set: set}}) + claims, err := v.Verify(r.Context(), r.Header.Get(tsjwt.DefaultHeader)) + if err != nil { + t.Error(err) + return + } + io.WriteString(w, claims.Tenant) + })) + defer backend.Close() + u, _ := url.Parse(backend.URL) + released := false + p, err := proxy.New(proxy.Config{Upstream: u, Audience: "app", Signer: sg, Gate: gateFunc(func(context.Context, proxy.Route) (func(), error) { + resolver.grant.Default = "other" + if revoke { + resolver.grant.Tenants = tenants[1:] + } + return func() { released = true }, nil + })}) + if err != nil { + t.Fatal(err) + } + w := httptest.NewRecorder() + p.ServeHTTP(w, httptest.NewRequest("POST", "/", strings.NewReader("tenant-scoped mutation"))) + if revoke { + if w.Code != 403 { + t.Fatalf("lost original tenant access: got %d %s", w.Code, w.Body.String()) + } + } else if w.Code != 200 || w.Body.String() != "original" { + t.Fatalf("request switched tenant: %d %s", w.Code, w.Body.String()) + } + if !released { + t.Fatal("demand not released") + } + }) + } +} diff --git a/tsnetid/go.mod b/tsnetid/go.mod index 05801f2..42add5e 100644 --- a/tsnetid/go.mod +++ b/tsnetid/go.mod @@ -1,6 +1,6 @@ module github.com/bitcomplete/tsjwt/tsnetid -go 1.25 +go 1.26.0 require ( github.com/bitcomplete/tsjwt v0.0.0 @@ -43,7 +43,7 @@ require ( github.com/illarion/gonotify/v3 v3.0.2 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/jsimonetti/rtnetlink v1.4.0 // indirect - github.com/klauspost/compress v1.17.11 // indirect + github.com/klauspost/compress v1.20.2 // indirect github.com/mdlayher/genetlink v1.3.2 // indirect github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42 // indirect github.com/mdlayher/sdnotify v1.0.0 // indirect @@ -64,16 +64,16 @@ require ( github.com/x448/float16 v0.8.4 // indirect go4.org/mem v0.0.0-20240501181205-ae6ca9944745 // indirect go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect - golang.org/x/crypto v0.38.0 // indirect + golang.org/x/crypto v0.57.0 // indirect golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac // indirect - golang.org/x/mod v0.24.0 // indirect - golang.org/x/net v0.40.0 // indirect - golang.org/x/sync v0.14.0 // indirect - golang.org/x/sys v0.33.0 // indirect - golang.org/x/term v0.32.0 // indirect - golang.org/x/text v0.25.0 // indirect + golang.org/x/mod v0.41.0 // indirect + golang.org/x/net v0.60.0 // indirect + golang.org/x/sync v0.23.0 // indirect + golang.org/x/sys v0.48.0 // indirect + golang.org/x/term v0.46.0 // indirect + golang.org/x/text v0.42.0 // indirect golang.org/x/time v0.11.0 // indirect - golang.org/x/tools v0.33.0 // indirect + golang.org/x/tools v0.49.0 // indirect golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect golang.zx2c4.com/wireguard/windows v0.5.3 // indirect gvisor.dev/gvisor v0.0.0-20250205023644-9414b50a5633 // indirect diff --git a/tsnetid/go.sum b/tsnetid/go.sum index f75000b..120c592 100644 --- a/tsnetid/go.sum +++ b/tsnetid/go.sum @@ -103,6 +103,8 @@ github.com/jsimonetti/rtnetlink v1.4.0 h1:Z1BF0fRgcETPEa0Kt0MRk3yV5+kF1FWTni6KUF github.com/jsimonetti/rtnetlink v1.4.0/go.mod h1:5W1jDvWdnthFJ7fxYX1GMK07BUpI4oskfOqvPteYS6E= github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= +github.com/klauspost/compress v1.20.2 h1:v5tiZvm1KGLyg5d5oXdXe3lsSPrbEUiKoXGia//I7Qo= +github.com/klauspost/compress v1.20.2/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= github.com/kortschak/wol v0.0.0-20200729010619-da482cc4850a h1:+RR6SqnTkDLWyICxS1xpjCi/3dhyV+TgZwA6Ww3KncQ= github.com/kortschak/wol v0.0.0-20200729010619-da482cc4850a/go.mod h1:YTtCCM3ryyfiu4F7t8HQ1mxvp1UBdWM2r6Xa+nGWvDk= github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= @@ -184,6 +186,7 @@ go4.org/netipx v0.0.0-20231129151722-fdeea329fbba h1:0b9z3AuHCjxk0x/opv64kcgZLBs go4.org/netipx v0.0.0-20231129151722-fdeea329fbba/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y= golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8= golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw= +golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac h1:l5+whBCLH3iH2ZNHYLbAe58bo7yrN4mVcnkHDYz5vvs= golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac/go.mod h1:hH+7mtFmImwwcMvScyxUhjuVHR3HGaDPMn9rMSUUbxo= golang.org/x/exp/typeparams v0.0.0-20240314144324-c7f7c6466f7f h1:phY1HzDcf18Aq9A8KkmRtY9WvOFIxN8wgfvy6Zm1DV8= @@ -192,11 +195,16 @@ golang.org/x/image v0.27.0 h1:C8gA4oWU/tKkdCfYT6T2u4faJu3MeNS5O8UPWlPF61w= golang.org/x/image v0.27.0/go.mod h1:xbdrClrAUway1MUTEZDq9mz/UpRwYAkFFNUslZtcB+g= golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= +golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= +golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY= golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds= +golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU= +golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ= golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -204,14 +212,22 @@ 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.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= +golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg= golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ= +golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE= +golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc= golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4= golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA= +golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI= +golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0= golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc= golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI= +golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI= +golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo= golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 h1:B82qJJgjvYKsXS9jeunTOisW56dUokqW/FOteYJJ/yg= golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2/go.mod h1:deeaetjYA+DHMHg+sMSMI58GrEteJUUzzw7en6TJQcI= golang.zx2c4.com/wireguard/windows v0.5.3 h1:On6j2Rpn3OEMXqBq00QEDC7bWSZrPIHKIus8eIuExIE=