diff --git a/CHANGELOG.md b/CHANGELOG.md index 28e2f21a..c6fb9785 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - **`qumo abr` transcodes broadcasts into an adaptive-bitrate ladder (`internal/abr`).** A publisher opts a broadcast in by announcing it beneath `/.abr/` (`/.abr/live/cam1` for `/live/cam1`), a redirect to it (`internal/optin`, shared with HLS); `qumo abr` then publishes `/live/cam1` + `ABR_SUFFIX` (`.abr`) with the source tracks as they are plus one H.264 rendition per `ABR_LADDER` rung shorter than the source (default `720:2500,360:800`; `ABR_FFMPEG`, `ABR_CODEC`, `ABR_PRESET`), all in one MSF `altGroup` with groups aligned to the source's, so a player can switch at any group boundary. Sources: H.264 (AVCC or Annex B) or VP9, without B-frames. A key holds an opt-in only when its key-set entry lists it (`"optIns": ["hls", "abr"]`, `qumo auth keygen -opt-ins`); the relay refuses other opt-in tokens and revokes a removed one within 30 s. `RELAY_SIGNING_KEY` lets `qumo abr` sign its own relay credential. See `docs/site/content/en/docs/cli/abr.md`. +- **RTMP and RTSP ingest opt streams into HLS and ABR on their publishers' behalf (`internal/ingest`, `INGEST_OPT_IN`).** A pushed or pulled stream can't announce an opt-in itself, so `qumo rtmp`, `qumo rtsp-push` and `qumo rtsp` announce it beside the broadcast (`/.abr/live/cam` for `/live/cam`, the redirect of `internal/optin`) for each egress `INGEST_OPT_IN` lists (`hls`, `abr`; none by default). With auth off every stream is opted in; with auth on, only when a token the publisher presents may publish the opt-in, so the key set's `optIns` decide, as for an MoQ publisher. A push now carries several tokens, each as a `?jwt=` (OBS: Stream Key `cam?jwt=BROADCAST&jwt=OPTIN`, or on the RTSP URL): the broadcast is taken on the first that may publish it, and each opt-in on the first that may publish the opt-in. A refused opt-in leaves the stream on air without it, and an opt-in is withdrawn when its token expires. A push to a path beneath an opt-in directory (`/.abr/live/cam`) is now refused, since the ingest announces opt-ins there. Through a relay with `RELAY_SIGNING_KEY`, the ingest presents a ticket for each opt-in directory beside its credential (`upstream.Config.OptIns`, publishing beneath `/.abr/live/` for `RELAY_PUBLISH=/live/`), retried for as long as the session lasts while the relay refuses it (`relaycred.Hold`). - **The playground opts a broadcast into ABR, and plays ABR broadcasts adaptively (`playground`).** The publish board's ABR box announces the broadcast's opt-in beneath `/.abr/` beside it, as the HLS scenario does beneath `/.hls/`. A broadcast whose video tracks share an alternate group is played adaptively: the viewer switches renditions at group boundaries, stepping down when frames fall behind their media time to what the rate measured since then carries, and up one rendition after a calm stretch. A step up keeps the old rendition until its last group before the switch has arrived; a step down drops it at once. The subscribe board picks a rendition by hand or automatically, and shows the rendition playing, the receive rate and the lag. - **`PEERS` names are resolved again while the relay runs, so peers behind a name join and leave without a restart; an entry without a port is an SRV name (`_moqt._udp.hubs.example.com`) (`internal/relay`).** A `PEERS` entry that is neither `host:port` nor a name without a port now stops the relay at startup, and a host that doesn't resolve is retried instead of being dialed by name. - **The relay re-reads its certificates while it runs (`internal/relay`).** About once a minute it reads `CERT_FILE`/`KEY_FILE`, `PEER_CERT_FILE`/`PEER_KEY_FILE` and `CA_FILE` and compares them with what it holds; what changed is taken from the next TLS handshake on, so cert-manager, a Vault or step-ca agent, or any job that writes the files needs no restart. A public certificate is taken once it parses and matches its key; a peer certificate and the CA together, once they pass the startup checks (issued by the CA, the peering name, valid now). Anything else is logged, once per distinct error rather than at every read, and the certificates in use are kept; renewal tools should replace the files atomically (write, then rename). Sessions already established are untouched. The CA is now checked at each handshake on both sides: as the dialed relay (`GetConfigForClient`) and as the dialer (`VerifyConnection` against the current CA, with the same checks crypto/tls made). The relay's key-set and usage requests are signed with the renewed peer certificate (`reqsign.Signer.Rotate`), and `qumo dns` re-reads its `PEER_CERT_FILE`/`PEER_KEY_FILE` on the same one-minute cadence and signs its feed reads with a renewed pair (`internal/certreload`, shared by both). New metrics: `qumo_relay_cert_reloads_total{cert,result}` and `qumo_relay_cert_not_after_seconds{cert}`, and `qumo_dns_cert_reloads_total{cert,result}` and `qumo_dns_cert_not_after_seconds{cert}`; alert on the `not_after` gauges. A CA is rotated by putting the new one beside the old in `CA_FILE` first ([TLS & mTLS](docs/site/content/en/docs/deployment/tls.md#certificate-renewal)). diff --git a/docs/site/content/en/docs/cli/abr.md b/docs/site/content/en/docs/cli/abr.md index a379ec8a..d98a20c4 100644 --- a/docs/site/content/en/docs/cli/abr.md +++ b/docs/site/content/en/docs/cli/abr.md @@ -50,6 +50,16 @@ one key. The app needs no new key when its tenant pays; the relay refuses the token until the key set lists `abr`. `qumo auth keygen -opt-ins hls,abr` writes the member for a key set kept by hand. +### RTMP and RTSP + +A stream pushed over RTMP or RTSP, or pulled from a camera, can't announce its +opt-in, so the ingest announces it on the publisher's behalf: set +`INGEST_OPT_IN=abr` on [`qumo rtmp`]({{< relref "rtmp#opting-streams-into-hls-and-abr" >}}), +`qumo rtsp-push` or `qumo rtsp`. With auth on, the publisher presents the +opt-in's token as a second `?jwt=` (OBS: Stream Key +`cam?jwt=BROADCAST&jwt=OPTIN`), and the ingest checks it against the key set, +so an unpaid tenant's stream plays without renditions. + It dials the relay as an ordinary client: it subscribes to the source like any viewer and publishes the ABR broadcast like any publisher. The relay itself never decodes media. diff --git a/docs/site/content/en/docs/cli/hls.md b/docs/site/content/en/docs/cli/hls.md index ec3007bd..2bb583dc 100644 --- a/docs/site/content/en/docs/cli/hls.md +++ b/docs/site/content/en/docs/cli/hls.md @@ -89,6 +89,10 @@ token of its own: qumo auth token -key signing-key.jwk -root /.hls/acme/room/42/ -publish /cam ``` +A stream pushed over RTMP or RTSP is opted in by the ingest on its publisher's +behalf, with `INGEST_OPT_IN=hls` and the same token as a second `?jwt=` on the +stream key: see [rtmp → Opting streams into HLS and ABR]({{< relref "rtmp#opting-streams-into-hls-and-abr" >}}). + ## Viewers By default the egress serves every opted-in broadcast to anyone. With diff --git a/docs/site/content/en/docs/cli/rtmp.md b/docs/site/content/en/docs/cli/rtmp.md index 8923d7fa..34fbc5f6 100644 --- a/docs/site/content/en/docs/cli/rtmp.md +++ b/docs/site/content/en/docs/cli/rtmp.md @@ -50,6 +50,35 @@ $ QUMO_AUTH_KEYS=keys.json qumo rtmp # OBS: Server rtmp://host:1935/live, Stream Key cam?jwt=$(qumo auth token -root /live/ -publish /cam) ``` +## Opting streams into HLS and ABR + +A pushed stream can't announce an opt-in for [`qumo hls`]({{< relref "hls" >}}) +or [`qumo abr`]({{< relref "abr" >}}) itself, as an MoQ publisher does, so the +server announces it on the publisher's behalf: `/.abr/live/cam` beside +`/live/cam`, a redirect to the broadcast. `INGEST_OPT_IN` lists the egresses +it offers (`hls`, `abr`, comma-separated; none by default). + +With auth off, every stream is opted into each of them. With auth on, a +stream is opted into one only when a token it presents may publish the +opt-in: a second `?jwt=` on the stream key, signed for the opt-in's path +(`-root /.abr/live/ -publish /cam`). The relay's key set decides, as for an +MoQ publisher: a tenant's key signs it only while its entry lists the opt-in +(`"optIns": ["abr"]`, see [abr → Paying for the opt-in]({{< relref "abr#paying-for-the-opt-in" >}})). +A refused opt-in token leaves the stream on air without the opt-in, and the +opt-in is withdrawn when its token expires. A push to an opt-in's path itself +(`/.abr/live/cam`) is refused: the server keeps those paths for its opt-ins. + +```console +$ QUMO_AUTH_KEYS=keys.json INGEST_OPT_IN=abr qumo rtmp +# OBS: Stream Key cam?jwt=$(qumo auth token -root /live/ -publish /cam)&jwt=$(qumo auth token -root /.abr/live/ -publish /cam) +``` + +Through a relay with `RELAY_SIGNING_KEY`, the server also presents a ticket +for each egress in `INGEST_OPT_IN` (publishing beneath `/.abr/live/` for +`RELAY_PUBLISH=/live/`), so the relay takes the opt-ins: the key must hold the +opt-ins in the relay's key set, as a key for the whole namespace (`-prefix /`) +does. + ## Publishing through a relay With `RELAY_URL` set, the server publishes what it ingests through that relay, @@ -69,6 +98,7 @@ $ RELAY_URL=moqt://relay:4433 RELAY_SIGNING_KEY=ingest-key.jwk RELAY_PUBLISH=/li | `RTMP_SERVE_ADDR` | `:4433`; none with `RELAY_URL` | MoQT listen address for subscribers to dial directly. | | `QUMO_AUTH_KEYS` | (unset) | The key set a publisher's credential is verified against, as the relay reads it (a file or URL). Unset accepts every publish. | | `QUMO_AUTH_KEYS_CACHE` / `QUMO_RELAY_TOKEN` | (unset) | A file a downloaded key set is kept in, and the bearer token sent to a key-set URL. | +| `INGEST_OPT_IN` | (unset) | The egresses (`hls`, `abr`) a stream is opted into on its publisher's behalf, comma-separated: with auth on, those a second `?jwt=` may publish the opt-in of. | | `RELAY_URL` | (unset) | The relay to publish through, dialed as a client (`https://…` or `moqt://…`); it may carry a credential as `?jwt=`. | | `RELAY_SIGNING_KEY`, `RELAY_PUBLISH` | (unset) | Set together: a signing key (`qumo auth keygen`) the server signs a fresh relay credential with on every dial and renews on the session, granting publishing beneath the directory `RELAY_PUBLISH`. A push outside it is refused. | | `RELAY_CA_FILE` / `RELAY_TLS_INSECURE` | (unset) / `false` | Trust this PEM as the relay's root, or skip verification for a self-signed dev relay. | diff --git a/docs/site/content/en/docs/cli/rtsp-push.md b/docs/site/content/en/docs/cli/rtsp-push.md index fc2389da..0e626e10 100644 --- a/docs/site/content/en/docs/cli/rtsp-push.md +++ b/docs/site/content/en/docs/cli/rtsp-push.md @@ -49,6 +49,35 @@ $ QUMO_AUTH_KEYS=keys.json qumo rtsp-push $ ffmpeg -re -i in.mp4 -c copy -f rtsp "rtsp://host:8554/live/cam?jwt=$(qumo auth token -root /live/ -publish /cam)" ``` +## Opting streams into HLS and ABR + +A pushed stream can't announce an opt-in for [`qumo hls`]({{< relref "hls" >}}) +or [`qumo abr`]({{< relref "abr" >}}) itself, as an MoQ publisher does, so the +server announces it on the publisher's behalf: `/.abr/live/cam` beside +`/live/cam`, a redirect to the broadcast. `INGEST_OPT_IN` lists the egresses +it offers (`hls`, `abr`, comma-separated; none by default). + +With auth off, every stream is opted into each of them. With auth on, a +stream is opted into one only when a token it presents may publish the +opt-in: a second `?jwt=` on the URL, signed for the opt-in's path +(`-root /.abr/live/ -publish /cam`). The relay's key set decides, as for an +MoQ publisher: a tenant's key signs it only while its entry lists the opt-in +(`"optIns": ["abr"]`, see [abr → Paying for the opt-in]({{< relref "abr#paying-for-the-opt-in" >}})). +A refused opt-in token leaves the stream on air without the opt-in, and the +opt-in is withdrawn when its token expires. A push to an opt-in's path itself +(`/.abr/live/cam`) is refused: the server keeps those paths for its opt-ins. + +```console +$ QUMO_AUTH_KEYS=keys.json INGEST_OPT_IN=abr qumo rtsp-push +$ ffmpeg -re -i in.mp4 -c copy -f rtsp "rtsp://host:8554/live/cam?jwt=$(qumo auth token -root /live/ -publish /cam)&jwt=$(qumo auth token -root /.abr/live/ -publish /cam)" +``` + +Through a relay with `RELAY_SIGNING_KEY`, the server also presents a ticket +for each egress in `INGEST_OPT_IN` (publishing beneath `/.abr/live/` for +`RELAY_PUBLISH=/live/`), so the relay takes the opt-ins: the key must hold the +opt-ins in the relay's key set, as a key for the whole namespace (`-prefix /`) +does. + ## Publishing through a relay With `RELAY_URL` set, the server publishes what it ingests through that relay, @@ -68,6 +97,7 @@ $ RELAY_URL=moqt://relay:4433 RELAY_SIGNING_KEY=ingest-key.jwk RELAY_PUBLISH=/li | `RTSP_SERVE_ADDR` | `:4433`; none with `RELAY_URL` | MoQT listen address for subscribers to dial directly. | | `QUMO_AUTH_KEYS` | (unset) | The key set a publisher's credential is verified against, as the relay reads it (a file or URL). Unset accepts every publish. | | `QUMO_AUTH_KEYS_CACHE` / `QUMO_RELAY_TOKEN` | (unset) | A file a downloaded key set is kept in, and the bearer token sent to a key-set URL. | +| `INGEST_OPT_IN` | (unset) | The egresses (`hls`, `abr`) a stream is opted into on its publisher's behalf, comma-separated: with auth on, those a second `?jwt=` may publish the opt-in of. | | `RELAY_URL` | (unset) | The relay to publish through, dialed as a client (`https://…` or `moqt://…`); it may carry a credential as `?jwt=`. | | `RELAY_SIGNING_KEY`, `RELAY_PUBLISH` | (unset) | Set together: a signing key (`qumo auth keygen`) the server signs a fresh relay credential with on every dial and renews on the session, granting publishing beneath the directory `RELAY_PUBLISH`. A push outside it is refused. | | `RELAY_CA_FILE` / `RELAY_TLS_INSECURE` | (unset) / `false` | Trust this PEM as the relay's root, or skip verification for a self-signed dev relay. | diff --git a/docs/site/content/en/docs/cli/rtsp.md b/docs/site/content/en/docs/cli/rtsp.md index 8f280d52..b726c034 100644 --- a/docs/site/content/en/docs/cli/rtsp.md +++ b/docs/site/content/en/docs/cli/rtsp.md @@ -55,6 +55,7 @@ $ RELAY_URL=moqt://relay:4433 RELAY_SIGNING_KEY=ingest-key.jwk RELAY_PUBLISH=/li | Variable | Default | Description | |---|---|---| | `RTSP_SERVE_ADDR` | `:4433`; none with `RELAY_URL` | MoQT listen address for subscribers to dial directly. | +| `INGEST_OPT_IN` | (unset) | The egresses (`hls`, `abr`) the broadcast is opted into, comma-separated, for as long as the pull runs: `/.abr/live/camera` beside `/live/camera`. Through a relay with `RELAY_SIGNING_KEY`, the command presents a ticket for each beside its credential, which the relay grants when the key holds the opt-in. | | `RELAY_URL` | (unset) | The relay to publish through, dialed as a client (`https://…` or `moqt://…`); it may carry a credential as `?jwt=`. | | `RELAY_SIGNING_KEY`, `RELAY_PUBLISH` | (unset) | Set together: a signing key (`qumo auth keygen`) the server signs a fresh relay credential with on every dial and renews on the session, granting publishing beneath the directory `RELAY_PUBLISH`. A broadcast path outside it stops the command at startup. | | `RELAY_CA_FILE` / `RELAY_TLS_INSECURE` | (unset) / `false` | Trust this PEM as the relay's root, or skip verification for a self-signed dev relay. | diff --git a/internal/auth/cmd.go b/internal/auth/cmd.go index 9aae297c..82a33117 100644 --- a/internal/auth/cmd.go +++ b/internal/auth/cmd.go @@ -74,17 +74,6 @@ func prefixLabel(prefix string, optIns []token.OptIn) string { return label + " or its opt-ins " + strings.Join(mirrors, ", ") } -// parseOptIns reads a comma-separated list of opt-ins: "" is none. -func parseOptIns(s string) []token.OptIn { - var out []token.OptIn - for part := range strings.SplitSeq(s, ",") { - if part = strings.TrimSpace(part); part != "" { - out = append(out, token.OptIn(part)) - } - } - return out -} - // runKeygen writes a new signing key pair: the private key, for the app that // signs tokens, and a key set holding its public key, for the relay. The key // and the two paths it will be written to are shown before any file is; on a @@ -109,15 +98,13 @@ func runKeygen(args []string, out, info io.Writer, in io.Reader) error { return err } pub := key.Public() - pub.OptIns = parseOptIns(*optIns) + if pub.OptIns, err = token.ParseOptIns(*optIns); err != nil { + return fmt.Errorf("-opt-ins: %w", err) + } set, err := token.MarshalKeySet(pub) if err != nil { return err } - // MarshalKeySet checked them: what the key set holds is what is shown. - if keys, err := token.ParseKeySet(set); err == nil { - pub = keys[pub.ID] - } // The key and where it lies, before any file does: the key's prefix is // the one setting a re-issue won't give back, and a bare prefix makes // the key the tenant of everything. diff --git a/internal/funnel/cmd.go b/internal/funnel/cmd.go index 4fca7a9d..a4be47d3 100644 --- a/internal/funnel/cmd.go +++ b/internal/funnel/cmd.go @@ -110,7 +110,7 @@ func Run(_ []string) error { } quicconfig.LogEnvSet("funnel") - up, err := upstream.FromEnv("funnel", quicConfig) + up, err := upstream.FromEnv("funnel", quicConfig, nil) if err != nil { return fmt.Errorf("funnel: %w", err) } diff --git a/internal/ingest/auth.go b/internal/ingest/auth.go index d20a57df..239fbcb2 100644 --- a/internal/ingest/auth.go +++ b/internal/ingest/auth.go @@ -2,43 +2,116 @@ package ingest import ( "context" + "errors" + "fmt" + "log/slog" "net/url" "strings" "time" "github.com/qumo-dev/gomoqt/moqt" + + "github.com/qumo-dev/qumo/internal/optin" + "github.com/qumo-dev/qumo/token" ) // PublishAuthorizer decides whether a pushed stream may publish the broadcast -// at path with credential, the ?jwt= the publisher presented ("" for none), +// at path with credential, a ?jwt= the publisher presented ("" for none), // and returns when that permission lapses: the stream is cut then. The zero // time never lapses. An error refuses the stream. +// +// A stream's opt-ins (see [optIn]) are asked for as well, with path the +// opt-in's, such as /.abr/live/cam for /live/cam. type PublishAuthorizer func(credential string, path moqt.BroadcastPath) (lapses time.Time, err error) -// splitCredential splits a name such as "cam?jwt=TOKEN" into the name and the -// credential its query carries as jwt, "" for none. -func splitCredential(s string) (name, credential string) { +// splitCredential splits a name such as "cam?jwt=A&jwt=B" into the name and +// the credentials its query carries as jwt, in order, none for none. +func splitCredential(s string) (name string, credentials []string) { name, rawQuery, _ := strings.Cut(s, "?") query, err := url.ParseQuery(rawQuery) if err != nil { // A query that doesn't parse carries no credential: the publish is // then checked as one without. - return name, "" + return name, nil } - return name, query.Get("jwt") + return name, query["jwt"] } // rtmpTarget returns the broadcast an RTMP publish names, /app/streamKey, and -// the credential it carries: ?jwt= on the stream key (OBS's "Stream Key" -// field), or else on the app (rtmp://host/live?jwt=… puts it there). Neither -// query is part of the path. -func rtmpTarget(app, streamKey string) (moqt.BroadcastPath, string) { - appName, appCredential := splitCredential(app) - key, credential := splitCredential(streamKey) - if credential == "" { - credential = appCredential +// the credentials it carries: each ?jwt= on the stream key (OBS's "Stream +// Key" field), then each on the app (rtmp://host/live?jwt=… puts it there). +// Neither query is part of the path. +func rtmpTarget(app, streamKey string) (moqt.BroadcastPath, []string) { + appName, appCredentials := splitCredential(app) + key, credentials := splitCredential(streamKey) + return moqt.BroadcastPath("/" + appName + "/" + key), append(credentials, appCredentials...) +} + +// errOptInPath refuses a push to a path beneath an opt-in directory, such as +// /.abr/live/cam: the server announces opt-ins there on publishers' behalf, +// and media there would answer an egress that expects a redirect. +var errOptInPath = errors.New("the path lies beneath an opt-in directory, where the server opts streams in: push to the broadcast's own path") + +// optInDirectory returns the opt-in directory path lies beneath, such as +// /.abr/ for /.abr/live/cam, and false when it lies beneath none. +func optInDirectory(path moqt.BroadcastPath) (string, bool) { + for _, o := range token.OptIns() { + if dir := o.Directory(); strings.HasPrefix(string(path), dir) { + return dir, true + } + } + return "", false +} + +// checkPushPath refuses a push to path when the server keeps it for opt-ins +// (errOptInPath). +func checkPushPath(path moqt.BroadcastPath) error { + if _, ok := optInDirectory(path); ok { + return fmt.Errorf("%s: %w", path, errOptInPath) + } + return nil +} + +// authorizeAny asks authorize whether path may be published with one of +// credentials, in order, and returns the first permission granted. Without +// credentials it asks for a publish that presented none. When none is +// granted it returns the first refusal. +func authorizeAny(authorize PublishAuthorizer, credentials []string, path moqt.BroadcastPath) (time.Time, error) { + if len(credentials) == 0 { + return authorize("", path) + } + var first error + for _, credential := range credentials { + lapses, err := authorize(credential, path) + if err == nil { + return lapses, nil + } + if first == nil { + first = err + } + } + return time.Time{}, first +} + +// optIn opts sess's broadcast into each of optIns, the egresses the server +// offers, that a publisher with credentials may publish the opt-in of: +// authorize is asked for the opt-in's path, beneath the egress's directory, +// and a nil authorize accepts every one. Each opt-in is withdrawn when its +// permission lapses or sess closes; a refused one leaves the broadcast as it +// is. +func optIn(sess *Session, authorize PublishAuthorizer, credentials []string, optIns []token.OptIn) { + for _, o := range optIns { + path := optin.Path(o.Directory(), sess.path) + var lapses time.Time + if authorize != nil { + var err error + if lapses, err = authorizeAny(authorize, credentials, path); err != nil { + slog.Info("ingest opt-in refused", "broadcast_path", sess.path, "opt_in", o, "error", err) + continue + } + } + sess.OptIn(o, lapses) } - return moqt.BroadcastPath("/" + appName + "/" + key), credential } // untilLapse returns ctx ended at lapses as well, or ctx alone for the zero diff --git a/internal/ingest/auth_test.go b/internal/ingest/auth_test.go index 751086ba..96759acb 100644 --- a/internal/ingest/auth_test.go +++ b/internal/ingest/auth_test.go @@ -18,26 +18,90 @@ import ( func TestRTMPTarget(t *testing.T) { tests := map[string]struct { - app, streamKey string - wantPath moqt.BroadcastPath - wantCredential string + app, streamKey string + wantPath moqt.BroadcastPath + wantCredentials []string }{ - "no credential": {app: "live", streamKey: "cam", wantPath: "/live/cam"}, - "on the stream key": {app: "live", streamKey: "cam?jwt=k", wantPath: "/live/cam", wantCredential: "k"}, - "on the app": {app: "live?jwt=a", streamKey: "cam", wantPath: "/live/cam", wantCredential: "a"}, - "on both, the stream key's": {app: "live?jwt=a", streamKey: "cam?jwt=k", wantPath: "/live/cam", wantCredential: "k"}, - "beside other parameters": {app: "live", streamKey: "cam?x=1&jwt=k", wantPath: "/live/cam", wantCredential: "k"}, - "a query that doesn't parse": {app: "live", streamKey: "cam?jwt=%zz", wantPath: "/live/cam"}, - "an empty stream key": {app: "live", streamKey: "", wantPath: "/live/"}, - "a query and no stream name": {app: "live", streamKey: "?jwt=k", wantPath: "/live/", wantCredential: "k"}, - "a nested app keeps its path": {app: "acme/room", streamKey: "cam?jwt=k", wantPath: "/acme/room/cam", wantCredential: "k"}, + "no credential": {app: "live", streamKey: "cam", wantPath: "/live/cam"}, + "on the stream key": {app: "live", streamKey: "cam?jwt=k", wantPath: "/live/cam", wantCredentials: []string{"k"}}, + "on the app": {app: "live?jwt=a", streamKey: "cam", wantPath: "/live/cam", wantCredentials: []string{"a"}}, + "on both, the stream key's first": {app: "live?jwt=a", streamKey: "cam?jwt=k", wantPath: "/live/cam", wantCredentials: []string{"k", "a"}}, + "several, in order": {app: "live", streamKey: "cam?jwt=k&jwt=abr", wantPath: "/live/cam", wantCredentials: []string{"k", "abr"}}, + "beside other parameters": {app: "live", streamKey: "cam?x=1&jwt=k", wantPath: "/live/cam", wantCredentials: []string{"k"}}, + "a query that doesn't parse": {app: "live", streamKey: "cam?jwt=%zz", wantPath: "/live/cam"}, + "an empty stream key": {app: "live", streamKey: "", wantPath: "/live/"}, + "a query and no stream name": {app: "live", streamKey: "?jwt=k", wantPath: "/live/", wantCredentials: []string{"k"}}, + "a nested app keeps its path": {app: "acme/room", streamKey: "cam?jwt=k", wantPath: "/acme/room/cam", wantCredentials: []string{"k"}}, } for name, tt := range tests { t.Run(name, func(t *testing.T) { - path, credential := rtmpTarget(tt.app, tt.streamKey) + path, credentials := rtmpTarget(tt.app, tt.streamKey) assert.Equal(t, tt.wantPath, path) - assert.Equal(t, tt.wantCredential, credential) + assert.Equal(t, tt.wantCredentials, credentials) + }) + } +} + +func TestCheckPushPath(t *testing.T) { + for path, opt := range map[moqt.BroadcastPath]bool{ + "/live/cam": false, + "/.abr/live/cam": true, + "/.hls/live/cam": true, + "/.hlsx/cam": false, + // Beneath the directory, though it names no broadcast. + "/.abr/live/../cam": true, + } { + err := checkPushPath(path) + if opt { + assert.ErrorIs(t, err, errOptInPath, path) + } else { + assert.NoError(t, err, path) + } + } +} + +func TestAuthorizeAny(t *testing.T) { + errFirst, errSecond := errors.New("first"), errors.New("second") + lapses := time.Now().Add(time.Minute) + tests := map[string]struct { + credentials []string + answers map[string]error // by credential; absent is granted + wantErr error + wantAsked []string + }{ + "none asks for a publish without one": {wantAsked: []string{""}}, + "the first granted": {credentials: []string{"a", "b"}, wantAsked: []string{"a"}}, + "a later one granted": { + credentials: []string{"a", "b"}, answers: map[string]error{"a": errFirst}, wantAsked: []string{"a", "b"}, + }, + "none granted, the first refusal": { + credentials: []string{"a", "b"}, answers: map[string]error{"a": errFirst, "b": errSecond}, + wantErr: errFirst, wantAsked: []string{"a", "b"}, + }, + } + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + fake := &fakePublishAuthorizer{answer: func(ask publishAsk) (time.Time, error) { + if err := tt.answers[ask.credential]; err != nil { + return time.Time{}, err + } + return lapses, nil + }} + + got, err := authorizeAny(fake.authorize, tt.credentials, "/live/cam") + + var asked []string + for _, q := range fake.questions() { + asked = append(asked, q.credential) + } + assert.Equal(t, tt.wantAsked, asked) + if tt.wantErr != nil { + assert.ErrorIs(t, err, tt.wantErr) + return + } + require.NoError(t, err) + assert.Equal(t, lapses, got) }) } } @@ -93,6 +157,9 @@ func TestPublishAuthorizer(t *testing.T) { }, "auth off, beneath RELAY_PUBLISH": {beneath: "/live/", path: "/live/cam"}, "auth off, outside RELAY_PUBLISH": {beneath: "/live/", path: "/other/cam", wantErr: errOutsidePublish}, + // Cleaned, /.abr/other/../live/cam would pass RELAY_PUBLISH as + // /live/cam, while the relay grants and routes it beneath /.abr/other/. + "auth off, an opt-in with a .. segment": {beneath: "/live/", path: "/.abr/other/../live/cam", wantErr: errOptInNamesNone}, "outside RELAY_PUBLISH, before the credential": { verifier: v, beneath: "/other/", credential: sign(key, publishLive), path: "/live/cam", wantErr: errOutsidePublish, }, @@ -124,6 +191,52 @@ func TestPublishAuthorizer(t *testing.T) { } } +// TestPublishAuthorizer_OptIn verifies an opt-in's path is checked as its +// broadcast's against RELAY_PUBLISH, and granted only to a token whose key +// holds the opt-in in the key set, as a control plane sells it: a tenant's +// key whose entry lists "abr" signs for /.abr/live/, one without does not. +func TestPublishAuthorizer_OptIn(t *testing.T) { + newVerifier := func(optIns ...token.OptIn) (*auth.Verifier, token.SigningKey) { + key, err := token.GenerateKey("/live/") + require.NoError(t, err) + public := key.Public() + public.OptIns = optIns + set, err := token.MarshalKeySet(public) + require.NoError(t, err) + keys := filepath.Join(t.TempDir(), "keys.json") + require.NoError(t, os.WriteFile(keys, set, 0o600)) + v, err := auth.NewVerifier(auth.VerifierConfig{Keys: keys}) + require.NoError(t, err) + return v, key + } + paid, paidKey := newVerifier(token.OptInABR) + unpaid, unpaidKey := newVerifier() + optInGrant := token.Grant{Root: "/.abr/live/", Publish: []string{"/cam"}} + sign := func(k token.SigningKey) string { + credential, err := token.Sign(k, optInGrant, time.Minute) + require.NoError(t, err) + return credential + } + + t.Run("a key that holds the opt-in", func(t *testing.T) { + lapses, err := publishAuthorizer(paid, "/live/")(sign(paidKey), "/.abr/live/cam") + require.NoError(t, err) + assert.WithinDuration(t, time.Now().Add(time.Minute), lapses, 5*time.Second) + }) + t.Run("a key that does not", func(t *testing.T) { + _, err := publishAuthorizer(unpaid, "/live/")(sign(unpaidKey), "/.abr/live/cam") + assert.Error(t, err) + }) + t.Run("auth off: beneath RELAY_PUBLISH as its broadcast", func(t *testing.T) { + _, err := publishAuthorizer(nil, "/live/")("", "/.abr/live/cam") + assert.NoError(t, err) + }) + t.Run("auth off: its broadcast outside RELAY_PUBLISH", func(t *testing.T) { + _, err := publishAuthorizer(nil, "/live/")("", "/.abr/other/cam") + assert.ErrorIs(t, err, errOutsidePublish) + }) +} + // TestPublishAuthorizer_Off verifies that without a key set or RELAY_PUBLISH // there is no check at all. func TestPublishAuthorizer_Off(t *testing.T) { diff --git a/internal/ingest/cmd.go b/internal/ingest/cmd.go index 96dac87c..80de060b 100644 --- a/internal/ingest/cmd.go +++ b/internal/ingest/cmd.go @@ -19,6 +19,7 @@ import ( "github.com/qumo-dev/qumo/internal/auth" "github.com/qumo-dev/qumo/internal/cors" "github.com/qumo-dev/qumo/internal/envconfig" + "github.com/qumo-dev/qumo/internal/optin" "github.com/qumo-dev/qumo/internal/quicconfig" "github.com/qumo-dev/qumo/internal/upstream" "github.com/qumo-dev/qumo/token" @@ -41,6 +42,7 @@ const ( // QUMO_AUTH_KEYS - the key set a publisher's credential (?jwt= on the stream key) is verified against, as the relay reads it (default: none, every publish is accepted) // QUMO_AUTH_KEYS_CACHE - a file a downloaded key set is kept in between runs // QUMO_RELAY_TOKEN - bearer token sent to a key-set URL +// INGEST_OPT_IN - the egresses (hls, abr) a stream is opted into on its publisher's behalf, comma-separated: with auth on, those a second ?jwt= on the stream key may publish the opt-in of (/.abr/live/cam for /live/cam); with it off, every stream's (default: none) // RELAY_URL, RELAY_SIGNING_KEY, RELAY_PUBLISH, RELAY_CA_FILE, RELAY_TLS_INSECURE // - the relay to publish through (upstream.FromEnv lists them) // CERT_FILE - TLS certificate file for the MoQT listener (default: "certs/server.crt") @@ -49,8 +51,8 @@ const ( // QUIC_* - QUIC transport settings (quicconfig.FromEnv lists them) func RunRTMP(_ []string) error { return runPush("rtmp", "RTMP_INGEST_ADDR", defaultRTMPIngestAddr, "RTMP_SERVE_ADDR", - func(addr string, mux *moqt.TrackMux, authorize PublishAuthorizer) pushServer { - return NewRTMPServer(RTMPConfig{Addr: addr, TrackMux: mux, Authorize: authorize}) + func(addr string, mux *moqt.TrackMux, authorize PublishAuthorizer, optIns []token.OptIn) pushServer { + return NewRTMPServer(RTMPConfig{Addr: addr, TrackMux: mux, Authorize: authorize, OptIns: optIns}) }) } @@ -63,8 +65,8 @@ func RunRTMP(_ []string) error { // on the URL it pushes to. func RunRTSP(_ []string) error { return runPush("rtsp", "RTSP_INGEST_ADDR", defaultRTSPIngestAddr, "RTSP_SERVE_ADDR", - func(addr string, mux *moqt.TrackMux, authorize PublishAuthorizer) pushServer { - return NewRTSPServer(RTSPConfig{Addr: addr, TrackMux: mux, Authorize: authorize}) + func(addr string, mux *moqt.TrackMux, authorize PublishAuthorizer, optIns []token.OptIn) pushServer { + return NewRTSPServer(RTSPConfig{Addr: addr, TrackMux: mux, Authorize: authorize, OptIns: optIns}) }) } @@ -82,7 +84,7 @@ var ( // runPush runs the push ingest server newServer makes, named name, until // SIGINT or SIGTERM, with its broadcasts published as originFromEnv says. func runPush(name, ingestEnv, defaultIngestAddr, serveEnv string, - newServer func(addr string, mux *moqt.TrackMux, authorize PublishAuthorizer) pushServer) error { + newServer func(addr string, mux *moqt.TrackMux, authorize PublishAuthorizer, optIns []token.OptIn) pushServer) error { ingestAddr := envconfig.String(ingestEnv, defaultIngestAddr) quicConfig, err := quicconfig.FromEnv(nil) if err != nil { @@ -102,7 +104,7 @@ func runPush(name, ingestEnv, defaultIngestAddr, serveEnv string, return err } trackMux := moqt.NewTrackMux(0) - srv := newServer(ingestAddr, trackMux, authorize) + srv := newServer(ingestAddr, trackMux, authorize, out.optIns) log.Println(" Ingest :", ingestAddr) out.logBanner() @@ -138,13 +140,19 @@ type origin struct { certFile, keyFile string allowedOrigins []string quic *quic.Config + // optIns are the egresses streams are opted into (INGEST_OPT_IN). + optIns []token.OptIn } // originFromEnv reads where the ingest command name publishes: through the // relay upstream.FromEnv reads, and on a MoQT listener at serveEnv, which // defaults to ":4433" without a relay and to none with one. func originFromEnv(name, serveEnv string, quicConfig *quic.Config) (*origin, error) { - up, err := upstream.FromEnv(name, quicConfig) + optIns, err := token.ParseOptIns(envconfig.String("INGEST_OPT_IN", "")) + if err != nil { + return nil, fmt.Errorf("%s: INGEST_OPT_IN: %w", name, err) + } + up, err := upstream.FromEnv(name, quicConfig, optIns) if err != nil { return nil, fmt.Errorf("%s: %w", name, err) } @@ -161,6 +169,7 @@ func originFromEnv(name, serveEnv string, quicConfig *quic.Config) (*origin, err keyFile: envconfig.String("KEY_FILE", "certs/server.key"), allowedOrigins: cors.LoadAllowed(), quic: quicConfig, + optIns: optIns, }, nil } @@ -172,6 +181,11 @@ func (o *origin) logBanner() { serve = "none" } log.Println(" Serve :", serve) + optIns := "none" + if len(o.optIns) > 0 { + optIns = fmt.Sprint(o.optIns) + } + log.Println(" Opt-ins :", optIns) } // serve publishes trackMux's broadcasts until ctx ends: through the relay, and @@ -255,6 +269,10 @@ func publishAuthorizerFromEnv(ctx context.Context, name, beneath string) (Publis // server: its path lies outside RELAY_PUBLISH. var errOutsidePublish = errors.New("the path lies outside RELAY_PUBLISH, beneath which the relay takes this server's broadcasts") +// errOptInNamesNone refuses an opt-in path with a "." or ".." segment, which +// names no broadcast (optin.Broadcast). +var errOptInNamesNone = errors.New("an opt-in path with a . or .. segment names no broadcast") + // errNoCredential refuses a push that presented no credential while auth is // on. var errNoCredential = errors.New("no credential: present a token that may publish the broadcast as ?jwt=") @@ -263,13 +281,25 @@ var errNoCredential = errors.New("no credential: present a token that may publis // beneath, a directory the relay takes the server's broadcasts beneath ("" // for anywhere), and, with v, its credential must let it publish there; the // push is then cut when the credential expires. It returns nil, accepting -// every push, for neither. +// every push, for neither. An opt-in's path, such as /.abr/live/cam, lies +// beneath beneath when its broadcast's does; whether a credential may publish +// it is v's, which grants it only to a key that holds the opt-in. func publishAuthorizer(v *auth.Verifier, beneath string) PublishAuthorizer { if v == nil && beneath == "" { return nil } return func(credential string, path moqt.BroadcastPath) (time.Time, error) { - if beneath != "" && !strings.HasPrefix(string(path), beneath) { + broadcast := path + if dir, ok := optInDirectory(path); ok { + b, named := optin.Broadcast(string(path), dir) + if !named { + // Cleaned, it could name a broadcast outside the + // directory the relay grants its exact path in. + return time.Time{}, fmt.Errorf("%s: %w", path, errOptInNamesNone) + } + broadcast = moqt.BroadcastPath(b) + } + if beneath != "" && !strings.HasPrefix(string(broadcast), beneath) { return time.Time{}, fmt.Errorf("%s: %w (%s)", path, errOutsidePublish, beneath) } if v == nil { diff --git a/internal/ingest/fake_publish_authorizer_test.go b/internal/ingest/fake_publish_authorizer_test.go index 9bb88f62..396e8e0f 100644 --- a/internal/ingest/fake_publish_authorizer_test.go +++ b/internal/ingest/fake_publish_authorizer_test.go @@ -14,12 +14,13 @@ type publishAsk struct { path moqt.BroadcastPath } -// fakePublishAuthorizer answers every publish with lapses and err, and records -// what it was asked. The servers ask from their connection goroutines, so the -// record is guarded. +// fakePublishAuthorizer answers every publish with lapses and err, or with +// answer when it is set, and records what it was asked. The servers ask from +// their connection goroutines, so the record is guarded. type fakePublishAuthorizer struct { lapses time.Time err error + answer func(publishAsk) (time.Time, error) mu sync.Mutex asked []publishAsk @@ -30,7 +31,11 @@ var _ PublishAuthorizer = (*fakePublishAuthorizer)(nil).authorize func (f *fakePublishAuthorizer) authorize(credential string, path moqt.BroadcastPath) (time.Time, error) { f.mu.Lock() defer f.mu.Unlock() - f.asked = append(f.asked, publishAsk{credential: credential, path: path}) + ask := publishAsk{credential: credential, path: path} + f.asked = append(f.asked, ask) + if f.answer != nil { + return f.answer(ask) + } return f.lapses, f.err } diff --git a/internal/ingest/rtmp.go b/internal/ingest/rtmp.go index 502103d1..b8f9a1db 100644 --- a/internal/ingest/rtmp.go +++ b/internal/ingest/rtmp.go @@ -10,6 +10,7 @@ import ( "github.com/qumo-dev/gomoqt/moqt" "github.com/qumo-dev/qumo/internal/rtmp" + "github.com/qumo-dev/qumo/token" ) // RTMPConfig holds configuration for an [RTMPServer]. @@ -21,10 +22,16 @@ type RTMPConfig struct { TrackMux *moqt.TrackMux // Authorize, when set, decides whether each publish may go ahead, from - // the ?jwt= its stream key (or app) carries; a refused one is answered - // NetStream.Publish.Denied. The stream is cut when the permission - // lapses. Nil accepts every publish. + // the ?jwt= its stream key (or app) carries, or any of several; a + // refused one is answered NetStream.Publish.Denied. The stream is cut + // when the permission lapses. Nil accepts every publish. Authorize PublishAuthorizer + + // OptIns are the egresses each stream is opted into, on the publisher's + // behalf, when one of its credentials may publish the opt-in (Authorize + // is asked for the opt-in's path, such as /.abr/live/cam): a publisher + // presents a second ?jwt= for it. Nil opts none in. + OptIns []token.OptIn } // RTMPServer accepts RTMP publish connections and bridges each stream to @@ -144,17 +151,20 @@ func (s *RTMPServer) handleConn(ctx context.Context, conn *rtmp.Conn) { defer conn.Close() var ( - path moqt.BroadcastPath - lapses time.Time + path moqt.BroadcastPath + credentials []string + lapses time.Time ) mr, err := conn.AcceptStreamFunc(func(app, streamKey string) error { - var credential string - path, credential = rtmpTarget(app, streamKey) + path, credentials = rtmpTarget(app, streamKey) + if err := checkPushPath(path); err != nil { + return err + } if s.config.Authorize == nil { return nil } var err error - lapses, err = s.config.Authorize(credential, path) + lapses, err = authorizeAny(s.config.Authorize, credentials, path) return err }) if err != nil { @@ -187,6 +197,7 @@ func (s *RTMPServer) handleConn(ctx context.Context, conn *rtmp.Conn) { return } defer sess.Close() + optIn(sess, s.config.Authorize, credentials, s.config.OptIns) slog.Info("RTMP ingest started", "remote", conn.RemoteAddr(), diff --git a/internal/ingest/rtmp_server_test.go b/internal/ingest/rtmp_server_test.go index 4579caa0..398f542f 100644 --- a/internal/ingest/rtmp_server_test.go +++ b/internal/ingest/rtmp_server_test.go @@ -12,15 +12,17 @@ import ( "github.com/stretchr/testify/require" "github.com/qumo-dev/qumo/internal/rtmp" + "github.com/qumo-dev/qumo/token" ) // startRTMPServer stands up an RTMPServer checking publishes with authorize -// (nil: none) on an ephemeral loopback port, and returns its TrackMux and the -// bound address once it is listening. It is torn down on test cleanup. -func startRTMPServer(t *testing.T, authorize PublishAuthorizer) (*moqt.TrackMux, net.Addr) { +// (nil: none) and opting streams into optIns, on an ephemeral loopback port, +// and returns its TrackMux and the bound address once it is listening. It is +// torn down on test cleanup. +func startRTMPServer(t *testing.T, authorize PublishAuthorizer, optIns ...token.OptIn) (*moqt.TrackMux, net.Addr) { t.Helper() mux := moqt.NewTrackMux(0) - server := NewRTMPServer(RTMPConfig{Addr: "127.0.0.1:0", TrackMux: mux, Authorize: authorize}) + server := NewRTMPServer(RTMPConfig{Addr: "127.0.0.1:0", TrackMux: mux, Authorize: authorize, OptIns: optIns}) ctx, cancel := context.WithCancel(context.Background()) errCh := make(chan error, 1) go func() { errCh <- server.ListenAndServe(ctx) }() @@ -95,3 +97,59 @@ func TestRTMPServer_Authorize_CutsAtLapse(t *testing.T) { require.Eventually(t, func() bool { return published(mux, "/live/cam") }, 3*time.Second, 10*time.Millisecond) assert.Eventually(t, func() bool { return !published(mux, "/live/cam") }, 3*time.Second, 20*time.Millisecond) } + +// TestRTMPServer_OptIns verifies a stream is opted into each egress the +// server offers that one of its credentials may publish the opt-in of: a +// second ?jwt= on the stream key, which the broadcast's own does not grant. +func TestRTMPServer_OptIns(t *testing.T) { + errNoGrant := errors.New("no grant") + // tok publishes the broadcast; abr publishes its ABR opt-in alone. + grants := map[publishAsk]bool{ + {credential: "tok", path: "/live/cam"}: true, + {credential: "abr", path: "/.abr/live/cam"}: true, + } + tests := map[string]struct { + authorize bool + streamKey string + wantABR bool + }{ + "a second credential that may publish the opt-in": {authorize: true, streamKey: "cam?jwt=tok&jwt=abr", wantABR: true}, + "the broadcast's credential alone": {authorize: true, streamKey: "cam?jwt=tok"}, + "auth off opts every stream in": {streamKey: "cam", wantABR: true}, + } + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + var authorize PublishAuthorizer + if tt.authorize { + fake := &fakePublishAuthorizer{answer: func(ask publishAsk) (time.Time, error) { + if grants[ask] { + return time.Time{}, nil + } + return time.Time{}, errNoGrant + }} + authorize = fake.authorize + } + mux, addr := startRTMPServer(t, authorize, token.OptInABR) + + publishRTMP(t, addr, "live", tt.streamKey) + + require.Eventually(t, func() bool { return published(mux, "/live/cam") }, 3*time.Second, 10*time.Millisecond) + if tt.wantABR { + assert.Eventually(t, func() bool { return published(mux, "/.abr/live/cam") }, 3*time.Second, 10*time.Millisecond) + } else { + assert.Never(t, func() bool { return published(mux, "/.abr/live/cam") }, 300*time.Millisecond, 50*time.Millisecond) + } + assert.False(t, published(mux, "/.hls/live/cam"), "an egress the server does not offer") + }) + } +} + +// TestRTMPServer_RefusesOptInPaths verifies a push can't name an opt-in's +// path itself: the server announces opt-ins there. +func TestRTMPServer_RefusesOptInPaths(t *testing.T) { + mux, addr := startRTMPServer(t, nil, token.OptInABR) + + publishRTMP(t, addr, ".abr", "live/cam") + + assert.Never(t, func() bool { return published(mux, "/.abr/live/cam") }, 300*time.Millisecond, 50*time.Millisecond) +} diff --git a/internal/ingest/rtsp.go b/internal/ingest/rtsp.go index 6ca6062d..2cdeab21 100644 --- a/internal/ingest/rtsp.go +++ b/internal/ingest/rtsp.go @@ -14,6 +14,7 @@ import ( "github.com/qumo-dev/gomoqt/moqt" "github.com/qumo-dev/qumo/internal/rtsp" + "github.com/qumo-dev/qumo/token" ) // RTSPConfig holds configuration for an [RTSPServer]. @@ -25,10 +26,14 @@ type RTSPConfig struct { TrackMux *moqt.TrackMux // Authorize, when set, decides whether each ANNOUNCE may publish, from - // the ?jwt= its URL carries; a refused one is answered 401. The - // connection is cut when the permission lapses. Nil accepts every - // ANNOUNCE. + // the ?jwt= its URL carries, or any of several; a refused one is + // answered 401. The connection is cut when the permission lapses. Nil + // accepts every ANNOUNCE. Authorize PublishAuthorizer + + // OptIns are the egresses each stream is opted into, as + // [RTMPConfig.OptIns] are. + OptIns []token.OptIn } // RTSPServer accepts RTSP push connections and bridges each stream to @@ -187,8 +192,14 @@ func (s *RTSPServer) handleConn(ctx context.Context, conn *rtsp.Conn) { if path == "" { path = "/live/stream" } + if err := checkPushPath(path); err != nil { + slog.Warn("RTSP publish refused", "remote", conn.RemoteAddr(), "broadcast_path", path, "error", err) + resp.StatusCode = rtsp.StatusBadRequest + break + } + credentials := req.URL.Query()["jwt"] if s.config.Authorize != nil { - lapses, err := s.config.Authorize(req.URL.Query().Get("jwt"), path) + lapses, err := authorizeAny(s.config.Authorize, credentials, path) if err != nil { slog.Warn("RTSP publish refused", "remote", conn.RemoteAddr(), @@ -223,7 +234,9 @@ func (s *RTSPServer) handleConn(ctx context.Context, conn *rtsp.Conn) { if err != nil { slog.Error("failed to create ingest session", "error", err) resp.StatusCode = rtsp.StatusInternalServerError + break } + optIn(sess, s.config.Authorize, credentials, s.config.OptIns) case rtsp.MethodSetup: if sdp == nil || sess == nil { diff --git a/internal/ingest/rtsp_pull.go b/internal/ingest/rtsp_pull.go index 40cb4984..f281e6f8 100644 --- a/internal/ingest/rtsp_pull.go +++ b/internal/ingest/rtsp_pull.go @@ -17,6 +17,7 @@ import ( "github.com/qumo-dev/qumo/internal/quicconfig" "github.com/qumo-dev/qumo/internal/rtsp" "github.com/qumo-dev/qumo/internal/upstream" + "github.com/qumo-dev/qumo/token" ) // RunRTSPPull connects to an RTSP source (e.g. an IP camera), pulls the stream @@ -30,6 +31,7 @@ import ( // args[0] RTSP source URL (rtsp://[user:pass@]host/path) // args[1] (opt) broadcast path (default /live/camera) // RTSP_SERVE_ADDR MoQT listen address for subscribers to dial directly (default :4433 without RELAY_URL, none with it) +// INGEST_OPT_IN the egresses (hls, abr) the broadcast is opted into, comma-separated (default none) // RELAY_URL, RELAY_SIGNING_KEY, RELAY_PUBLISH, RELAY_CA_FILE, RELAY_TLS_INSECURE // the relay to publish through (upstream.FromEnv lists them) // CERT_FILE TLS certificate (default certs/server.crt) @@ -53,6 +55,9 @@ func RunRTSPPull(args []string) error { if err != nil { return err } + if err := checkPushPath(moqt.BroadcastPath(path)); err != nil { + return fmt.Errorf("rtsp: %w", err) + } if beneath := out.up.PublishBeneath(); beneath != "" && !strings.HasPrefix(path, beneath) { return fmt.Errorf("rtsp: %s: %w (%s)", path, errOutsidePublish, beneath) } @@ -69,6 +74,7 @@ func RunRTSPPull(args []string) error { AllowedOrigins: out.allowedOrigins, QUIC: quicConfig, Upstream: out.up, + OptIns: out.optIns, }) if err != nil { return err @@ -92,6 +98,9 @@ type PullConfig struct { QUIC *quic.Config // Upstream, when set, publishes the broadcast through a relay as well. Upstream *upstream.Upstream + // OptIns are the egresses the broadcast is opted into for as long as the + // pull runs (see [Session.OptIn]). + OptIns []token.OptIn } // PullHandle is a running RTSP pull ingest. Close stops the pull + MoQT server; @@ -158,6 +167,9 @@ func PullAndServe(parentCtx context.Context, cfg PullConfig) (*PullHandle, error cancel() return nil, fmt.Errorf("rtsp pull session: %w", err) } + for _, o := range cfg.OptIns { + sess.OptIn(o, time.Time{}) + } var moqSrv *moqt.Server if cfg.ServeAddr != "" { diff --git a/internal/ingest/rtsp_test.go b/internal/ingest/rtsp_test.go index 8db04d98..da1f4499 100644 --- a/internal/ingest/rtsp_test.go +++ b/internal/ingest/rtsp_test.go @@ -13,6 +13,7 @@ import ( "github.com/qumo-dev/gomoqt/moqt" "github.com/qumo-dev/qumo/internal/rtsp" + "github.com/qumo-dev/qumo/token" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -26,12 +27,13 @@ const testH264SDP = "v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\ns=No Name\r\nc=IN IP4 12 // none) on an ephemeral loopback port and returns the bound address once it is // listening. The server (and its // ListenAndServe goroutine) are torn down on test cleanup. -func startRTSPServer(t *testing.T, authorize PublishAuthorizer) (*RTSPServer, net.Addr) { +func startRTSPServer(t *testing.T, authorize PublishAuthorizer, optIns ...token.OptIn) (*RTSPServer, net.Addr) { t.Helper() server := NewRTSPServer(RTSPConfig{ Addr: "127.0.0.1:0", TrackMux: moqt.NewTrackMux(0), Authorize: authorize, + OptIns: optIns, }) ctx, cancel := context.WithCancel(context.Background()) errCh := make(chan error, 1) @@ -235,6 +237,38 @@ func TestRTSPServer_Authorize(t *testing.T) { } } +// TestRTSPServer_OptIns verifies an ANNOUNCE is opted into an egress with a +// second ?jwt= on its URL that may publish the opt-in, asked for every +// credential in order. +func TestRTSPServer_OptIns(t *testing.T) { + authorize := &fakePublishAuthorizer{answer: func(ask publishAsk) (time.Time, error) { + if ask.path == "/.abr/live/cam" && ask.credential != "abr" { + return time.Time{}, errors.New("no grant") + } + return time.Time{}, nil + }} + server, addr := startRTSPServer(t, authorize.authorize, token.OptInABR) + rc := dialRTSP(t, addr) + + require.Equal(t, rtsp.StatusOK, announce(t, rc, addr, "/live/cam?jwt=tok&jwt=abr").StatusCode) + + assert.Equal(t, []publishAsk{ + {credential: "tok", path: "/live/cam"}, + {credential: "tok", path: "/.abr/live/cam"}, + {credential: "abr", path: "/.abr/live/cam"}, + }, authorize.questions()) + assert.Eventually(t, func() bool { return published(server.config.TrackMux, "/.abr/live/cam") }, 3*time.Second, 10*time.Millisecond) +} + +// TestRTSPServer_RefusesOptInPaths verifies an ANNOUNCE can't name an +// opt-in's path itself. +func TestRTSPServer_RefusesOptInPaths(t *testing.T) { + _, addr := startRTSPServer(t, nil, token.OptInABR) + rc := dialRTSP(t, addr) + + assert.Equal(t, rtsp.StatusBadRequest, announce(t, rc, addr, "/.abr/live/cam").StatusCode) +} + // TestRTSPServer_Authorize_CutsAtLapse verifies a push is cut when its // permission lapses. func TestRTSPServer_Authorize_CutsAtLapse(t *testing.T) { diff --git a/internal/ingest/session.go b/internal/ingest/session.go index 5e1d9f56..670724cc 100644 --- a/internal/ingest/session.go +++ b/internal/ingest/session.go @@ -25,8 +25,12 @@ import ( "context" "fmt" "log/slog" + "time" "github.com/qumo-dev/gomoqt/moqt" + + "github.com/qumo-dev/qumo/internal/optin" + "github.com/qumo-dev/qumo/token" ) // Session manages the MoQT announcement and fan-out for a single ingested @@ -37,10 +41,13 @@ import ( // A Session is safe for concurrent use; however, callers should typically // push video and audio from a single goroutine per track. type Session struct { - path moqt.BroadcastPath - handler *ingestHandler - endAnn func() - cancel context.CancelFunc + path moqt.BroadcastPath + trackMux *moqt.TrackMux + handler *ingestHandler + endAnn func() + // ctx ends when the session closes, and with it every opt-in. + ctx context.Context + cancel context.CancelFunc } // NewSession creates a new ingest session that announces path on trackMux. @@ -66,13 +73,36 @@ func NewSession(trackMux *moqt.TrackMux, path moqt.BroadcastPath) (*Session, err slog.Info("ingest session started", "broadcast_path", path) return &Session{ - path: path, - handler: h, - endAnn: endAnn, - cancel: cancel, + path: path, + trackMux: trackMux, + handler: h, + endAnn: endAnn, + ctx: ctx, + cancel: cancel, }, nil } +// OptIn opts the session's broadcast into the egress o names, as a MoQT +// publisher does (see package optin): it announces the broadcast's path +// beneath o's directory, redirecting every subscription to the broadcast, on +// the session's TrackMux. The opt-in is withdrawn at lapses, or when the +// session closes; the zero time never lapses. +func (s *Session) OptIn(o token.OptIn, lapses time.Time) { + ctx, cancel := s.ctx, context.CancelFunc(func() {}) + if !lapses.IsZero() { + ctx, cancel = context.WithDeadline(s.ctx, lapses) + } + path := optin.Path(o.Directory(), s.path) + // The announcement ends with ctx. + ann, _ := moqt.NewAnnouncement(ctx, path) + context.AfterFunc(ctx, func() { + cancel() + slog.Info("ingest opt-in withdrawn", "broadcast_path", s.path, "opt_in", o) + }) + s.trackMux.Announce(ann, optin.Handler(s.path)) + slog.Info("ingest opted in", "broadcast_path", s.path, "opt_in", o, "opt_in_path", path) +} + // RegisterVideo registers (or replaces) the video track in the MSF catalog // using codec configuration extracted from a sequence header. Subscribers // can discover the track via the catalog and receive video frames pushed diff --git a/internal/ingest/session_test.go b/internal/ingest/session_test.go index df0ce56b..ba4b3a3b 100644 --- a/internal/ingest/session_test.go +++ b/internal/ingest/session_test.go @@ -2,10 +2,13 @@ package ingest import ( "testing" + "time" "github.com/qumo-dev/gomoqt/moqt" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/qumo-dev/qumo/token" ) func TestNewSession_RejectsInvalidPath(t *testing.T) { @@ -31,3 +34,26 @@ func TestNewSession_AcceptsValidPath(t *testing.T) { require.NoError(t, err) defer sess.Close() } + +// TestSession_OptIn verifies an opt-in is announced beside the broadcast, +// redirecting to it, and withdrawn at its lapse or when the session closes, +// without the broadcast. +func TestSession_OptIn(t *testing.T) { + mux := moqt.NewTrackMux(0) + sess, err := NewSession(mux, "/live/cam") + require.NoError(t, err) + + sess.OptIn(token.OptInABR, time.Now().Add(200*time.Millisecond)) + sess.OptIn(token.OptInHLS, time.Time{}) + + assert.True(t, published(mux, "/.abr/live/cam")) + assert.True(t, published(mux, "/.hls/live/cam")) + assert.Eventually(t, func() bool { return !published(mux, "/.abr/live/cam") }, 3*time.Second, 20*time.Millisecond, + "withdrawn at its lapse") + assert.True(t, published(mux, "/live/cam"), "the broadcast plays on") + assert.True(t, published(mux, "/.hls/live/cam"), "an opt-in that never lapses stays") + + sess.Close() + assert.Eventually(t, func() bool { return !published(mux, "/.hls/live/cam") }, 3*time.Second, 20*time.Millisecond, + "withdrawn with the session") +} diff --git a/internal/integration/abr_auth_test.go b/internal/integration/abr_auth_test.go index 6c19b738..7f96ed81 100644 --- a/internal/integration/abr_auth_test.go +++ b/internal/integration/abr_auth_test.go @@ -5,6 +5,8 @@ package integration import ( "context" "errors" + "fmt" + "net" "os" "path/filepath" "testing" @@ -17,8 +19,11 @@ import ( "github.com/qumo-dev/qumo/internal/abr" "github.com/qumo-dev/qumo/internal/auth" + "github.com/qumo-dev/qumo/internal/ffpub" + "github.com/qumo-dev/qumo/internal/ingest" "github.com/qumo-dev/qumo/internal/optin" "github.com/qumo-dev/qumo/internal/relay" + "github.com/qumo-dev/qumo/internal/upstream" "github.com/qumo-dev/qumo/token" ) @@ -133,3 +138,141 @@ func runPaidOptIn(t *testing.T, optIns []token.OptIn, paid bool) { require.Eventually(t, routed(srv, dst), 10*time.Second, 50*time.Millisecond, "the transcoder, on its own credential, did not publish the ABR broadcast") } + +// TestABR_IngestOptsIn runs the opt-in through RTMP ingest, which opts a +// stream in on its publisher's behalf (INGEST_OPT_IN): the publisher puts a +// second token, for the opt-in, on its stream key, and the ingest announces +// the opt-in beside the broadcast when its key holds the opt-in in the key +// set. The ingest publishes through a relay that verifies tokens, presenting +// a ticket for the opt-ins beside its own credential (upstream.Config.OptIns), +// and the transcoder publishes the ABR broadcast. Unpaid, the opt-in token is +// refused at the ingest, and the broadcast plays without renditions. +func TestABR_IngestOptsIn(t *testing.T) { + if !ffpub.Available() { + t.Skip("ffmpeg not on PATH") + } + tests := map[string]struct { + optIns []token.OptIn // the key set's optIns for the tenant's key + paid bool + }{ + "unpaid: the key set lists no ABR": {optIns: []token.OptIn{token.OptInHLS}}, + "paid: the key set lists ABR": {optIns: []token.OptIn{token.OptInABR}, paid: true}, + } + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + runIngestOptIn(t, tt.optIns, tt.paid) + }) + } +} + +func runIngestOptIn(t *testing.T, optIns []token.OptIn, paid bool) { + const ( + src = moqt.BroadcastPath("/live/ingested") + dst = moqt.BroadcastPath("/live/ingested.abr") + ) + optInPath := optin.Path(token.ABRDirectory, src) + newKey := func(prefix string) token.SigningKey { + k, err := token.GenerateKey(prefix) + require.NoError(t, err) + return k + } + tenant := newKey("/live/") // the tenant's key; its app signs the publisher's tokens + operator := newKey("/") // the operator's, for the ingest's and the transcoder's own credentials + tenantPub := tenant.Public() + tenantPub.OptIns = optIns + verifier := verifierFor(t, tenantPub, operator.Public()) + addr, srv := startAuthRelay(t, verifier.Authorize, nil, func(s *relay.Server) { s.Tickets = verifier }) + sign := func(key token.SigningKey, g token.Grant) string { + tok, err := token.Sign(key, g, time.Minute) + require.NoError(t, err) + return tok + } + + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + + // The ingest: it checks each publish against the key set as the relay + // does, and publishes through the relay with the operator's key. + authorize := func(credential string, path moqt.BroadcastPath) (time.Time, error) { + g, err := verifier.Check(credential, token.ActionPublish, path) + if err != nil { + return time.Time{}, err + } + if !g.Allows(token.ActionPublish, path) { + return time.Time{}, fmt.Errorf("may not publish %s", path) + } + return g.Expires(), nil + } + ingestMux := moqt.NewTrackMux(0) + rtmpLn, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + rtmpAddr := rtmpLn.Addr().String() + require.NoError(t, rtmpLn.Close()) + go func() { + _ = ingest.NewRTMPServer(ingest.RTMPConfig{ + Addr: rtmpAddr, TrackMux: ingestMux, Authorize: authorize, OptIns: []token.OptIn{token.OptInABR}, + }).ListenAndServe(ctx) + }() + require.Eventually(t, func() bool { + c, err := net.DialTimeout("tcp", rtmpAddr, 200*time.Millisecond) + if err == nil { + _ = c.Close() + } + return err == nil + }, 5*time.Second, 50*time.Millisecond) + rawKey, err := operator.MarshalJWK() + require.NoError(t, err) + keyFile := filepath.Join(t.TempDir(), "signing-key.jwk") + require.NoError(t, os.WriteFile(keyFile, rawKey, 0o600)) + upCtx, stopUp := context.WithCancel(ctx) + upDone := make(chan error, 1) + go func() { + // Native QUIC: see TestABR_PaidOptIn. + upDone <- upstream.Publish(upCtx, upstream.Config{ + Name: "rtmp", URL: nativeURL(addr), Insecure: true, + SigningKeyFile: keyFile, Publish: "/live/", OptIns: []token.OptIn{token.OptInABR}, + }, ingestMux) + }() + + // The publisher: two tokens on its stream key, for the broadcast and + // for its opt-in, both signed with the tenant's key. + streamKey := fmt.Sprintf("ingested?jwt=%s&jwt=%s", + sign(tenant, token.Grant{Root: "/live/", Publish: []string{"/ingested"}}), + sign(tenant, token.Grant{Root: "/.abr/live/", Publish: []string{"/ingested"}})) + pub := ffpub.New(ffpub.Config{ + URL: fmt.Sprintf("rtmp://%s/live/%s", rtmpAddr, streamKey), + GOP: 30, Width: 640, Height: 480, Framerate: 30, + }) + pubCtx, stopPub := context.WithCancel(ctx) + require.NoError(t, pub.Start(pubCtx)) + t.Cleanup(func() { + stopPub() // ffmpeg publishes until its context ends + _ = pub.Wait() + stopUp() + <-upDone + }) + require.Eventually(t, routed(srv, src), 10*time.Second, 50*time.Millisecond) + + // The transcoder, with the credential qumo abr signs for itself. + abrMux := moqt.NewTrackMux(0) + abrSess := dialOver(t, nativeURL(addr)+"/?jwt="+sign(operator, abr.RelayGrant()), nil, abrMux) + tc := &abr.Transcoder{Suffix: ".abr", Config: abr.Config{ + Ladder: []abr.Rung{{Height: 240, Bitrate: 400_000}}, + Encoder: abr.EncoderConfig{FFmpeg: "ffmpeg", Codec: "libx264", Preset: "ultrafast"}, + }} + go func() { _ = tc.Serve(ctx, abrSess, abrMux) }() + + if !paid { + assert.Never(t, func() bool { return routed(srv, optInPath)() || routed(srv, dst)() }, + 3*time.Second, 50*time.Millisecond, "an opt-in was announced without ABR paid for") + ann, _ := ingestMux.TrackHandler(optInPath) + assert.Nil(t, ann, "the ingest refused the opt-in token") + return + } + require.Eventually(t, routed(srv, optInPath), 10*time.Second, 50*time.Millisecond, + "the relay did not route the ingest's opt-in: its ticket was not granted") + require.Eventually(t, routed(srv, dst), 15*time.Second, 50*time.Millisecond, + "the transcoder did not publish the ABR broadcast") + viewer := dialOver(t, nativeURL(addr)+"/?jwt="+sign(operator, token.Grant{Root: "/live/", Subscribe: []string{"/"}}), nil, moqt.NewTrackMux(0)) + waitCatalog(t, viewer, dst, []string{"video", "video-240p"}) +} diff --git a/internal/relaycred/relaycred.go b/internal/relaycred/relaycred.go index e46497e5..d504ec9a 100644 --- a/internal/relaycred/relaycred.go +++ b/internal/relaycred/relaycred.go @@ -77,20 +77,59 @@ type credential interface { // nothing. A relay that verifies no credentials (NOT_SUPPORTED) lets the // session's grant last for good, and Keep stops. func Keep(sess *moqt.Session, key token.SigningKey, g token.Grant, lapses time.Time) { - keep(sess.Context(), func(ctx context.Context, tok []byte) (credential, error) { + keep(sess.Context(), slog.Default(), renewGrant, presentOn(sess), func() { + // not actionable: the session is ending either way. + _ = sess.CloseWithError(moqt.UnauthorizedSessionErrorCode, "relay credential lapsed") + }, func() (string, time.Time, error) { return Sign(key, g) }, lapses) +} + +// Hold adds g to sess's grant until sess ends: it presents a credential +// granting g, signed with key, on sess's Auth Stream as a ticket, and a fresh +// one each time the ticket in force is two thirds through its lifetime, as +// Keep does. A ticket that can't be signed or is not granted is tried again +// every retryInterval, and sess is kept: what it connected with stays in +// force, so a relay that grants g later (a key set that comes to hold an +// opt-in) takes the ticket then. A relay that verifies no credentials +// (NOT_SUPPORTED) needs no ticket, and Hold stops. +func Hold(sess *moqt.Session, key token.SigningKey, g token.Grant) { + // No grant in force yet (a zero lapses): the first ticket is presented + // at once. + keep(sess.Context(), slog.Default(), renewTicket, presentOn(sess), nil, func() (string, time.Time, error) { return Sign(key, g) }, time.Time{}) +} + +// presentOn presents a credential on sess's Auth Stream. It returns a nil +// credential with an error: a nil *moqt.Credential returned as is would be a +// credential interface that is not nil. +func presentOn(sess *moqt.Session) func(context.Context, []byte) (credential, error) { + return func(ctx context.Context, tok []byte) (credential, error) { c, err := sess.Auth(ctx, tok) if err != nil { return nil, err } return c, nil - }, func() { - // not actionable: the session is ending either way. - _ = sess.CloseWithError(moqt.UnauthorizedSessionErrorCode, "relay credential lapsed") - }, func() (string, time.Time, error) { return Sign(key, g) }, lapses) + } } -// keep is Keep, presenting with present and closing the session with end. -func keep(ctx context.Context, present func(context.Context, []byte) (credential, error), end func(), +// renewal is what keep keeps in force, which sets what it does with a +// credential not granted. +type renewal int + +const ( + // renewGrant renews the grant a session connected with (Keep): a + // credential not granted is retried, warning each time, until the + // grant in force would lapse first; then keep ends. + renewGrant renewal = iota + // renewTicket renews a ticket added to a session's grant (Hold): a + // credential not granted is retried for as long as the session lasts, + // warning once, and noting when one is granted again. + renewTicket +) + +// keep is Keep and Hold, as r says: it presents credentials signed with sign +// through present, logging to log, until ctx ends. The first is presented two +// thirds of the way to lapses. end, when not nil, is called when keep ends +// because the grant lapses (renewGrant only); a nil end does nothing then. +func keep(ctx context.Context, log *slog.Logger, r renewal, present func(context.Context, []byte) (credential, error), end func(), sign func() (string, time.Time, error), lapses time.Time) { var held credential defer func() { @@ -99,6 +138,7 @@ func keep(ctx context.Context, present func(context.Context, []byte) (credential } }() wait := time.Duration(float64(time.Until(lapses)) * renewAt) + failing := false for { select { case <-ctx.Done(): @@ -111,20 +151,36 @@ func keep(ctx context.Context, present func(context.Context, []byte) (credential return } if notSupported(err) { - slog.Info("relay credential not renewed: the relay verifies none, and the session's grant does not lapse") + log.Info("relay credential not renewed: the relay verifies none, and the session's grant does not lapse") return } + if r == renewTicket { + // A ticket refused stays refused until the relay's key set + // changes: say so once, not every retry. + if !failing { + log.Warn("relay ticket not granted, retrying", "error", err) + } + failing = true + wait = retryInterval + continue + } // A retry signs, then may wait presentTimeout for an answer: the // session must not outlive its grant meanwhile. if time.Until(lapses) <= retryInterval+presentTimeout { - slog.Error("relay credential lapsing: closing the session to dial again", "error", err) - end() + log.Error("relay credential lapsing: closing the session to dial again", "error", err) + if end != nil { + end() + } return } - slog.Warn("relay credential not renewed, retrying", "error", err, "lapses_in", time.Until(lapses).Round(time.Second)) + log.Warn("relay credential not renewed, retrying", "error", err, "lapses_in", time.Until(lapses).Round(time.Second)) wait = retryInterval continue } + if failing { + log.Info("relay ticket granted", "root", next.Grant().Root) + failing = false + } if held != nil { _ = held.Close() // not actionable: the fresh grant is in force } diff --git a/internal/relaycred/relaycred_test.go b/internal/relaycred/relaycred_test.go index 88ceb12c..ea00d6ba 100644 --- a/internal/relaycred/relaycred_test.go +++ b/internal/relaycred/relaycred_test.go @@ -1,7 +1,10 @@ package relaycred import ( + "bytes" "context" + "log/slog" + "strings" "sync" "testing" "testing/synctest" @@ -68,7 +71,7 @@ func TestKeep_Renews(t *testing.T) { done := make(chan struct{}) go func() { defer close(done) - keep(ctx, relay.present, func() { ended = true }, sign, start.Add(time.Hour)) + keep(ctx, discard, renewGrant, relay.present, func() { ended = true }, sign, start.Add(time.Hour)) }() time.Sleep(time.Hour) @@ -92,7 +95,7 @@ func TestKeep_ClosesBeforeTheGrantLapses(t *testing.T) { var endedAt time.Time start := time.Now() - keep(t.Context(), relay.present, func() { endedAt = time.Now() }, sign, start.Add(2*time.Minute)) + keep(t.Context(), discard, renewGrant, relay.present, func() { endedAt = time.Now() }, sign, start.Add(2*time.Minute)) assert.Equal(t, []time.Time{start.Add(80 * time.Second), start.Add(90 * time.Second), start.Add(100 * time.Second)}, relay.presented, "first at 2/3 of the grant, then every retryInterval") @@ -113,7 +116,7 @@ func TestKeep_StopsWhenNotSupported(t *testing.T) { ended := false start := time.Now() - keep(t.Context(), relay.present, func() { ended = true }, sign, start.Add(time.Hour)) + keep(t.Context(), discard, renewGrant, relay.present, func() { ended = true }, sign, start.Add(time.Hour)) assert.Equal(t, []time.Time{start.Add(40 * time.Minute)}, relay.presented) assert.False(t, ended) @@ -132,7 +135,7 @@ func TestKeep_RecoversBeforeTheLapse(t *testing.T) { done := make(chan struct{}) go func() { defer close(done) - keep(ctx, relay.present, func() { ended = true }, sign, start.Add(time.Hour)) + keep(ctx, discard, renewGrant, relay.present, func() { ended = true }, sign, start.Add(time.Hour)) }() time.Sleep(40*time.Minute + time.Second) @@ -147,6 +150,82 @@ func TestKeep_RecoversBeforeTheLapse(t *testing.T) { }) } +// discard drops keep's logs, for tests that do not look at them. +var discard = slog.New(slog.DiscardHandler) + +// A ticket (renewTicket, no grant in force yet) is presented at once, and one +// not granted is presented again every retryInterval for as long as the +// session lasts, until the relay grants it: a key set that comes to hold an +// opt-in. That is so whether or not keep has an end: an end is only cleanup, +// never what makes keep retry. It warns once, not every retry, and notes the +// grant. +func TestKeep_TicketRetriesUntilGranted(t *testing.T) { + for name, withEnd := range map[string]bool{"no end": false, "an end": true} { + t.Run(name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + relay := &fakeRelay{refusing: errRefused, maxAge: time.Hour} + var logs bytes.Buffer + ended := false + var end func() + if withEnd { + end = func() { ended = true } + } + start := time.Now() + done := make(chan struct{}) + go func() { + defer close(done) + keep(ctx, slog.New(slog.NewTextHandler(&logs, nil)), renewTicket, relay.present, end, sign, time.Time{}) + }() + + time.Sleep(25 * time.Second) + relay.refuse(nil) + time.Sleep(retryInterval) + synctest.Wait() + cancel() + <-done + + assert.Equal(t, []time.Time{start, start.Add(10 * time.Second), start.Add(20 * time.Second), start.Add(30 * time.Second)}, + relay.presented, "at once, then every retryInterval") + require.Len(t, relay.granted, 1) + assert.True(t, relay.granted[0].closed, "withdrawn with the session") + assert.False(t, ended, "a ticket never ends the session") + assert.Equal(t, 1, strings.Count(logs.String(), "relay ticket not granted, retrying"), "warned once") + assert.Equal(t, 1, strings.Count(logs.String(), "relay ticket granted")) + assert.NotContains(t, logs.String(), "level=ERROR") + }) + }) + } +} + +// A grant (renewGrant) not renewed is retried, warning each time, until it +// would lapse first: then keep stops renewing, calling end when there is one. +// A nil end is no cleanup, not retrying for good. +func TestKeep_GrantStopsAtTheLapse(t *testing.T) { + for name, withEnd := range map[string]bool{"no end": false, "an end": true} { + t.Run(name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + relay := &fakeRelay{refusing: errRefused} + var logs bytes.Buffer + ended := false + var end func() + if withEnd { + end = func() { ended = true } + } + start := time.Now() + + keep(t.Context(), slog.New(slog.NewTextHandler(&logs, nil)), renewGrant, relay.present, end, sign, start.Add(2*time.Minute)) + + assert.Equal(t, []time.Time{start.Add(80 * time.Second), start.Add(90 * time.Second), start.Add(100 * time.Second)}, + relay.presented, "first at 2/3 of the grant, then every retryInterval, then no more") + assert.Equal(t, withEnd, ended) + assert.Equal(t, 2, strings.Count(logs.String(), "relay credential not renewed, retrying"), "warned each retry") + assert.Equal(t, 1, strings.Count(logs.String(), "relay credential lapsing")) + }) + }) + } +} + // Sign's lapse is the token's lifetime from before it was signed. func TestSign(t *testing.T) { key, err := token.GenerateKey("/acme/") diff --git a/internal/upstream/upstream.go b/internal/upstream/upstream.go index cffbeb19..4a375bd2 100644 --- a/internal/upstream/upstream.go +++ b/internal/upstream/upstream.go @@ -10,6 +10,7 @@ import ( "fmt" "log/slog" "net/url" + "strings" "time" "github.com/quic-go/quic-go" @@ -42,6 +43,10 @@ type Upstream struct { // and a fresh one on the session before its grant lapses. key *token.SigningKey publish string + // optIns are the opt-ins the publisher announces beside its broadcasts: + // with key, each session holds a ticket for publishing beneath publish's + // mirror beneath each one's directory. + optIns []token.OptIn } // Config is how a publisher reaches the relay it publishes through. @@ -64,20 +69,29 @@ type Config struct { // for longer than its lifetime needs a signing key. SigningKeyFile string Publish string + // OptIns are the opt-ins the publisher announces beside its broadcasts + // (see package optin), such as /.abr/room/cam beside /room/cam. With + // SigningKeyFile, each session presents a ticket granting publishing + // beneath Publish's mirror beneath each one's directory (/.abr/room/), and + // keeps it in force; the relay grants it when the key's entry in its key + // set holds the opt-in. Without one, the credential in URL must grant + // them. + OptIns []token.OptIn // QUIC is the QUIC configuration of the session, whether moqt:// or // https://; nil is quic-go's defaults. QUIC *quic.Config } // FromEnv reads the relay the publisher name publishes through from the -// environment, or returns nil when RELAY_URL is unset: +// environment, or returns nil when RELAY_URL is unset. optIns are the opt-ins +// it announces beside its broadcasts (Config.OptIns): // // RELAY_URL - the relay to publish through, dialed as a client, e.g. "https://relay:4433" or "moqt://relay:4433"; it may carry a credential as ?jwt= // RELAY_SIGNING_KEY - a signing key (qumo auth keygen) to sign a fresh relay credential with on every dial, replacing ?jwt=, and again on the session before its grant lapses // RELAY_PUBLISH - the directory that credential grants publishing beneath, e.g. "/room/"; set with RELAY_SIGNING_KEY // RELAY_CA_FILE - PEM cert to trust as the relay's root (default: the system roots) // RELAY_TLS_INSECURE - skip relay TLS verification, for a self-signed dev relay (default: "false") -func FromEnv(name string, quicConfig *quic.Config) (*Upstream, error) { +func FromEnv(name string, quicConfig *quic.Config, optIns []token.OptIn) (*Upstream, error) { relayURL := envconfig.String("RELAY_URL", "") if relayURL == "" { return nil, nil @@ -89,6 +103,7 @@ func FromEnv(name string, quicConfig *quic.Config) (*Upstream, error) { Insecure: envconfig.String("RELAY_TLS_INSECURE", "false") == "true", SigningKeyFile: envconfig.String("RELAY_SIGNING_KEY", ""), Publish: envconfig.String("RELAY_PUBLISH", ""), + OptIns: optIns, QUIC: quicConfig, }) } @@ -119,7 +134,7 @@ func newUpstream(cfg Config) (*Upstream, error) { if err := tlsclient.Apply(tc, cfg.CAFile, cfg.Insecure); err != nil { return nil, fmt.Errorf("relay TLS: %w", err) } - u := &Upstream{name: cfg.Name, url: cfg.URL, tls: tc, quic: cfg.QUIC} + u := &Upstream{name: cfg.Name, url: cfg.URL, tls: tc, quic: cfg.QUIC, optIns: cfg.OptIns} switch { case cfg.SigningKeyFile == "" && cfg.Publish == "": case cfg.SigningKeyFile == "" || cfg.Publish == "": @@ -138,6 +153,9 @@ func newUpstream(cfg Config) (*Upstream, error) { if _, err := token.Sign(key, publishGrant(publish), token.MaxLifetime); err != nil { return nil, fmt.Errorf("RELAY_PUBLISH: %w", err) } + // Its opt-in grants need no check of their own: Sign signs for + // every opt-in of a directory it signs for, and leaves which ones + // the key holds to the relay. u.key, u.publish = &key, publish } return u, nil @@ -159,6 +177,12 @@ func publishGrant(dir string) token.Grant { return token.Grant{Root: dir, Publish: []string{"/"}} } +// optInGrant grants publishing every opt-in o of a broadcast beneath dir, a +// directory: publishing beneath dir's mirror beneath o's directory. +func optInGrant(o token.OptIn, dir string) token.Grant { + return publishGrant(o.Directory() + strings.TrimPrefix(dir, "/")) +} + // Run keeps a session to the relay open until ctx ends, dialing again after // each one ends, with a signed credential fresh on every dial and renewed on // the session before its grant lapses. @@ -170,6 +194,9 @@ func (u *Upstream) Run(ctx context.Context, mux *moqt.TrackMux) { slog.Info("publishing through the relay", "publisher", u.name) if u.key != nil { go relaycred.Keep(sess, *u.key, publishGrant(u.publish), lapses) + for _, o := range u.optIns { + go relaycred.Hold(sess, *u.key, optInGrant(o, u.publish)) + } } select { case <-ctx.Done(): diff --git a/internal/upstream/upstream_test.go b/internal/upstream/upstream_test.go index 16828c28..cd9e7f47 100644 --- a/internal/upstream/upstream_test.go +++ b/internal/upstream/upstream_test.go @@ -35,6 +35,7 @@ func TestNewUpstream(t *testing.T) { }{ "a URL alone": {cfg: Config{URL: "https://relay:4433/?jwt=abc"}}, "a signing key": {cfg: Config{URL: "moqt://relay:4433", SigningKeyFile: keyFile, Publish: "room"}}, + "a tenant's key with opt-ins": {cfg: Config{URL: "moqt://relay:4433", SigningKeyFile: roomKeyFile, Publish: "room", OptIns: []token.OptIn{token.OptInHLS, token.OptInABR}}}, "an unparsable URL": {cfg: Config{URL: "https://relay:port"}, wantErr: "RELAY_URL"}, "a key without a path": {cfg: Config{URL: "https://relay", SigningKeyFile: keyFile}, wantErr: "set together"}, "a path without a key": {cfg: Config{URL: "https://relay", Publish: "room"}, wantErr: "set together"}, @@ -57,6 +58,14 @@ func TestNewUpstream(t *testing.T) { } } +func TestOptInGrant(t *testing.T) { + g := optInGrant(token.OptInABR, "/acme/room/") + assert.Equal(t, "/.abr/acme/room/", g.Root) + assert.True(t, g.Allows(token.ActionPublish, "/.abr/acme/room/cam")) + assert.False(t, g.Allows(token.ActionPublish, "/acme/room/cam"), "the broadcast is the connection credential's") + assert.False(t, g.Allows(token.ActionPublish, "/.hls/acme/room/cam"), "another opt-in") +} + func TestUpstream_Target(t *testing.T) { t.Run("the URL as given", func(t *testing.T) { u, err := newUpstream(Config{URL: "https://relay:4433/?jwt=given"}) diff --git a/token/path.go b/token/path.go index cdcc6408..eac26d27 100644 --- a/token/path.go +++ b/token/path.go @@ -93,6 +93,19 @@ func OptIns() []OptIn { return []OptIn{OptInHLS, OptInABR} } +// ParseOptIns reads a comma-separated list of opt-ins, such as "hls,abr", in +// the canonical order OptIns returns them, each once. "" is none, nil. It +// refuses an unknown opt-in. +func ParseOptIns(s string) ([]OptIn, error) { + var optIns []OptIn + for part := range strings.SplitSeq(s, ",") { + if part = strings.TrimSpace(part); part != "" { + optIns = append(optIns, OptIn(part)) + } + } + return normalizeOptIns(optIns) +} + // within reports whether root, a MoQ directory, lies at or beneath prefix, // another one, or at or beneath prefix's mirror under the directory of one of // optIns. Both are in Directory's form, so a plain prefix comparison holds at