feat(drm): license a plugin DRM at the licence step and over --remote with the client's plugin device

This commit is contained in:
imSp4rky
2026-12-03 15:51:42 -07:00
parent f1f91a7d5c
commit 0536056ed6
24 changed files with 803 additions and 136 deletions
+6 -4
View File
@@ -205,8 +205,10 @@ def test_a_served_service_gets_a_device_by_system_only_where_the_server_licenses
else:
launch_session("EXAMPLE", "t", {}, licensor=licensor)
cdm_for = seen["ctx"].obj.cdm_for
before = list(loads)
assert (cdm_for is not None) == served
if cdm_for:
cdm_for("example")
assert loads[-1] == ("EXAMPLE", None, "example")
gives = cdm_for("example") if cdm_for else None
assert (loads[len(before) :] == [("EXAMPLE", None, "example")]) == served
if not served:
assert gives is None
+101
View File
@@ -0,0 +1,101 @@
"""A plugin DRM licenses at the licence step of content-key resolution, like a CDM track: vaults first, then a device."""
from __future__ import annotations
from pathlib import Path
from types import SimpleNamespace
from typing import Any
from uuid import UUID
import click
import pytest
from tests.core.key_resolver import MemoryVault, key_table, resolver, vaults_of
from tests.drm_plugins import ExampleDevice
from unshackle.core.drm import CustomDRM
from unshackle.core.tracks import Video
from unshackle.core.tracks.track import Track
pytestmark = pytest.mark.unit
KID = UUID(hex="0123456789abcdef0123456789abcdef")
KEY = "5a" * 16
@pytest.fixture
def device(tmp_path: Path) -> ExampleDevice:
path = tmp_path / "EXAMPLE_DEVICE.exd"
path.write_bytes(bytes.fromhex(KEY))
return ExampleDevice(path)
def plugin_track(data: dict[str, Any] | None = None) -> Video:
drm = CustomDRM("example", data={"ticket": KID.hex} if data is None else data)
return Video(
id_="vid",
url="https://example.test/v.m3u8",
language="en",
descriptor=Track.Descriptor.HLS,
height=1080,
drm=[drm],
)
def run_licence(track: Video, vault: MemoryVault, device: Any, loads: list, **flags: Any) -> CustomDRM:
"""Run the licence step of ``track`` with ``vault``; ``loads`` records each device the run loads."""
def cdm_for(system: str, quality: int | None = None) -> Any:
loads.append((system, quality))
return device
keys = resolver(vaults_of(vault), cdm_for=cdm_for, **flags)
drm = track.drm[0]
keys.licence_for(track, SimpleNamespace(tracks=SimpleNamespace(videos=[track])), key_table())(drm)
keys.wait_vault_writes()
return drm
def test_the_drm_reads_its_kids_from_the_plugin_data_before_any_licence(example_plugins: None) -> None:
assert plugin_track().drm[0].kids == [KID]
def test_a_vault_key_stands_in_for_the_licence_and_loads_no_device(example_plugins: None, device: Any) -> None:
loads: list = []
drm = run_licence(plugin_track(), MemoryVault({KID: KEY}), device, loads)
assert drm.content_keys == {KID: KEY} and loads == []
def test_a_vault_miss_licenses_with_the_plugin_device_and_stores_the_key(example_plugins: None, device: Any) -> None:
loads: list = []
vault = MemoryVault()
drm = run_licence(plugin_track(), vault, device, loads)
assert drm.content_keys == {KID: KEY} and loads == [("example", None)]
assert vault.asked == [KID] and vault.added == {KID: KEY}
def test_vaults_only_stops_a_miss_before_the_device(example_plugins: None, device: Any) -> None:
loads: list = []
with pytest.raises(click.ClickException, match=f"^No Vault has a Key for {KID.hex} and --vaults-only was used$"):
run_licence(plugin_track(), MemoryVault(), device, loads, vaults_only=True)
assert loads == []
def test_cdm_only_licenses_with_the_device_and_asks_no_vault(example_plugins: None, device: Any) -> None:
loads: list = []
vault = MemoryVault({KID: "00" * 16})
drm = run_licence(plugin_track(), vault, device, loads, cdm_only=True)
assert drm.content_keys == {KID: KEY} and vault.asked == [] and loads == [("example", None)]
def test_a_run_without_a_plugin_device_stops_the_track_with_one_line(example_plugins: None) -> None:
with pytest.raises(click.ClickException, match="^No EXAMPLE device to license track vid$"):
run_licence(plugin_track(), MemoryVault(), None, [])
def test_a_drm_whose_data_names_no_kid_stops_the_track_with_one_line(example_plugins: None, device: Any) -> None:
with pytest.raises(click.ClickException, match="^The EXAMPLE DRM of track vid names no KID$"):
run_licence(plugin_track({}), MemoryVault(), device, [])
+12 -4
View File
@@ -1074,8 +1074,10 @@ def every_drm() -> list:
def local_plugin_drm() -> CustomDRM:
"""A plugin DRM that a device licensed: its content key and its ticket never leave the machine."""
drm = CustomDRM("example", kids=[VIDEO_KID], display_id="EXAMPLE-1", data={"ticket": "server only"})
"""A plugin DRM that a device licensed: its ticket crosses as licence data, its content key and its other data never do."""
drm = CustomDRM(
"example", kids=[VIDEO_KID], display_id="EXAMPLE-1", data={"ticket": VIDEO_KID.hex, "secret": "server only"}
)
drm.content_keys[VIDEO_KID] = VIDEO_KEY
return drm
@@ -1085,7 +1087,13 @@ def test_rest_drm_of_each_system(example_plugins: None) -> None:
assert serialize_drm(drm) == [
*REST_VIDEO["drm"],
{"type": "other", "kids": [], "label": "OTHER", "data": {"key": HELD_KEY.hex()}},
{"type": "example", "kids": [VIDEO_KID.hex], "label": "EXAMPLE", "display_id": "EXAMPLE-1"},
{
"type": "example",
"kids": [VIDEO_KID.hex],
"label": "EXAMPLE",
"display_id": "EXAMPLE-1",
"data": {"ticket": VIDEO_KID.hex},
},
{
"type": "clearkeyctr",
"key": HELD_KEY.hex(),
@@ -1129,7 +1137,7 @@ def test_reconstruct_rest_drm(example_plugins: None) -> None:
"label": "EXAMPLE",
"kids": [VIDEO_KID.hex],
"display_id": "EXAMPLE-1",
"data": {},
"data": {"ticket": VIDEO_KID.hex},
},
{"class": "ClearKeyCTR", "kids": [], "content_keys": {}},
]