2026-03-29 18:58:31 -06:00
|
|
|
import json
|
2026-11-13 15:41:43 -07:00
|
|
|
import re
|
2026-03-29 18:58:31 -06:00
|
|
|
import tempfile
|
2022-03-08 13:26:03 -07:00
|
|
|
from pathlib import Path
|
2026-03-29 18:58:31 -06:00
|
|
|
from typing import List, Dict
|
2022-03-08 13:26:03 -07:00
|
|
|
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
from modules.map import pmtiles
|
2026-10-27 15:50:09 -06:00
|
|
|
from modules.map.catalog import MAP_REGIONS, MAP_REGIONS_BY_NAME, MANIFEST_URL
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
from modules.map.config import get_maps_config
|
2026-03-29 18:58:31 -06:00
|
|
|
from wrolpi.common import get_media_directory, walk, logger, get_wrolpi_config, aiohttp_get, verify_gpg_signature
|
|
|
|
|
from wrolpi.downloader import DownloadFrequency, Download, save_downloads_config, download_manager
|
2022-03-08 13:26:03 -07:00
|
|
|
|
|
|
|
|
logger = logger.getChild(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_map_directory() -> Path:
|
2024-08-06 18:13:21 -06:00
|
|
|
map_directory = get_media_directory() / get_wrolpi_config().map_destination
|
2022-03-31 07:31:15 -06:00
|
|
|
if not map_directory.is_dir():
|
2024-03-09 16:27:34 -07:00
|
|
|
map_directory.mkdir(parents=True)
|
2022-03-31 07:31:15 -06:00
|
|
|
return map_directory
|
2022-03-08 13:26:03 -07:00
|
|
|
|
|
|
|
|
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
def iter_map_files(suffix: str) -> List[Path]:
|
|
|
|
|
"""The files in the map directory (at any depth) whose name ends with `suffix`, sorted.
|
|
|
|
|
|
|
|
|
|
Hidden directories are skipped; an unfinished download waits in one under its final name."""
|
|
|
|
|
map_directory = get_map_directory()
|
|
|
|
|
paths = []
|
|
|
|
|
for path in walk(map_directory):
|
|
|
|
|
if not path.name.endswith(suffix) or not path.is_file():
|
|
|
|
|
continue
|
|
|
|
|
if any(i.startswith('.') for i in path.relative_to(map_directory).parts):
|
|
|
|
|
continue
|
|
|
|
|
paths.append(path)
|
|
|
|
|
return sorted(paths)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_map_path(path: Path) -> str:
|
|
|
|
|
"""The name of a map file in the API and the maps config: its path relative to the map directory."""
|
|
|
|
|
return path.relative_to(get_map_directory()).as_posix()
|
|
|
|
|
|
|
|
|
|
|
2026-11-13 15:41:43 -07:00
|
|
|
def find_sidecar_style(path: Path) -> Path | None:
|
|
|
|
|
"""The MapLibre style a person put beside a PMTiles file to say how it is drawn: `<stem>.style.json`, else the
|
|
|
|
|
`style.json` shared by the files of its directory."""
|
|
|
|
|
for candidate in (path.with_suffix('.style.json'), path.parent / 'style.json'):
|
|
|
|
|
if candidate.is_file():
|
|
|
|
|
return candidate
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_sidecar_sprite(path: Path, extension: str = 'json', high_resolution: bool = False) -> Path | None:
|
|
|
|
|
"""The sprite sheet (the icons of a sidecar style) beside a PMTiles file: `<stem>.sprite.json` and
|
|
|
|
|
`<stem>.sprite.png`, else the `sprite.json` and `sprite.png` shared by the files of its directory.
|
|
|
|
|
|
|
|
|
|
A sheet may also come at double resolution (`sprite@2x.json`, `sprite@2x.png`). The sheet of the other
|
|
|
|
|
resolution is returned when the one asked for does not exist; a sprite file says its own pixel ratio."""
|
|
|
|
|
for base in (path.with_suffix('.sprite'), path.parent / 'sprite'):
|
|
|
|
|
resolutions = [i for i in ('', '@2x')
|
|
|
|
|
if base.with_name(f'{base.name}{i}.json').is_file()
|
|
|
|
|
and base.with_name(f'{base.name}{i}.png').is_file()]
|
|
|
|
|
if resolutions:
|
|
|
|
|
wanted = '@2x' if high_resolution else ''
|
|
|
|
|
resolution = wanted if wanted in resolutions else resolutions[0]
|
|
|
|
|
return base.with_name(f'{base.name}{resolution}.{extension}')
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SPRITE_NAME = re.compile(r'^(?P<filename>.+\.pmtiles)(?P<high_resolution>@2x)?\.(?P<extension>json|png)$')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_sidecar_sprite_file(name: str) -> Path | None:
|
|
|
|
|
"""The sprite file MapLibre asks for as `<map file>[@2x].(json|png)`, if that map file has a sprite.
|
|
|
|
|
|
|
|
|
|
Raises ValueError if the name is not such a name, or leads outside of the map directory."""
|
|
|
|
|
match = SPRITE_NAME.match(name)
|
|
|
|
|
if not match:
|
|
|
|
|
raise ValueError(f'Invalid sprite name: {name}')
|
|
|
|
|
path = _resolve_pmtiles_file(match.group('filename'))
|
|
|
|
|
if not path:
|
|
|
|
|
return None
|
|
|
|
|
return find_sidecar_sprite(path, match.group('extension'), bool(match.group('high_resolution')))
|
|
|
|
|
|
|
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
def get_pmtiles_files() -> List[dict]:
|
|
|
|
|
"""Find all .pmtiles files in the map directory and return their metadata."""
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
maps_config = get_maps_config()
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
files = []
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
for path in iter_map_files('.pmtiles'):
|
|
|
|
|
stat = path.stat()
|
|
|
|
|
map_path = get_map_path(path)
|
2026-11-13 15:41:43 -07:00
|
|
|
style = find_sidecar_style(path)
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
files.append(dict(
|
|
|
|
|
name=path.name,
|
|
|
|
|
path=str(path.relative_to(get_media_directory())),
|
|
|
|
|
map_path=map_path,
|
|
|
|
|
size=stat.st_size,
|
|
|
|
|
mtime=stat.st_mtime,
|
|
|
|
|
has_search_index=path.with_suffix('.search.db').is_file(),
|
|
|
|
|
visible=not maps_config.is_ignored(map_path),
|
2026-11-13 15:41:43 -07:00
|
|
|
style_path=str(style.relative_to(get_media_directory())) if style else None,
|
The standalone map viewer draws overlays, as the map does (#966)
The standalone viewer (port 8084) drew basemaps only: a map file which is
not a basemap was skipped. It now offers each overlay in its layer panel,
hidden until turned on, with a checkbox per layer, draws it with the style
beside it when there is one, and shows a feature's attributes on a click.
The overlay logic is the map viewer's own. It moved to dependency-free
modules (app/src/map-shared) which a browser can load as they are; the
standalone viewer, which has no build step, loads copies of them from
modules/map/static/js. A test fails while a copy differs from its module
(`npm run sync:map-shared` makes the copies). What differs between the
viewers is passed in: where a map file and its style are fetched from, and
whether a layer can be checked before MapLibre is given it.
The standalone viewer cannot check a layer first, so it adds an overlay's
layers to the map one at a time once the style has loaded: MapLibre refuses a
whole style over one layer it cannot draw, but only that layer when it is
added by itself.
The API also names a style by its path within the map directory, which is
what the standalone viewer is served.
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-13 17:27:38 -07:00
|
|
|
# The standalone viewer is served the map directory, not the media directory.
|
|
|
|
|
style_map_path=get_map_path(style) if style else None,
|
2026-11-13 15:41:43 -07:00
|
|
|
has_sprite=find_sidecar_sprite(path) is not None,
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
**pmtiles.describe(path),
|
|
|
|
|
))
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
return files
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2022-03-31 07:31:15 -06:00
|
|
|
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
def _resolve_pmtiles_file(filename: str) -> Path | None:
|
|
|
|
|
"""The PMTiles file in the map directory that `filename` (relative to the map directory) names, if it exists.
|
2026-04-11 20:09:30 -06:00
|
|
|
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
Raises ValueError if the name leads outside of the map directory."""
|
|
|
|
|
map_directory = get_map_directory().resolve()
|
2026-03-29 18:58:31 -06:00
|
|
|
# Prevent path traversal.
|
|
|
|
|
path = (map_directory / filename).resolve()
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
if not path.is_relative_to(map_directory):
|
2026-03-29 18:58:31 -06:00
|
|
|
raise ValueError(f'Invalid filename: {filename}')
|
2022-03-31 07:31:15 -06:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
if path.is_file() and path.suffix == '.pmtiles':
|
The map draws any PMTiles file as an overlay, in any sub-directory (#962)
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically.
Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config.
PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
2026-11-13 15:11:01 -07:00
|
|
|
return path
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_pmtiles_file(filename: str) -> bool:
|
|
|
|
|
"""Delete a PMTiles file from the map directory. Returns True if the file was deleted.
|
|
|
|
|
|
|
|
|
|
Also deletes the companion .search.db file if it exists."""
|
|
|
|
|
path = _resolve_pmtiles_file(filename)
|
|
|
|
|
if not path:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
map_path = path.relative_to(get_map_directory().resolve()).as_posix()
|
|
|
|
|
path.unlink()
|
|
|
|
|
logger.warning(f'Deleted PMTiles file: {path}')
|
|
|
|
|
# Also delete companion search index.
|
|
|
|
|
search_db = path.with_suffix('.search.db')
|
|
|
|
|
if search_db.is_file():
|
|
|
|
|
search_db.unlink()
|
|
|
|
|
logger.warning(f'Deleted search index: {search_db}')
|
|
|
|
|
# A file that is gone is not ignored; a later file of the same name starts visible.
|
|
|
|
|
get_maps_config().set_ignored(map_path, False)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_pmtiles_file_visible(filename: str, visible: bool) -> bool:
|
|
|
|
|
"""Show a PMTiles file on the map, or ignore it. Returns False if there is no such file."""
|
|
|
|
|
path = _resolve_pmtiles_file(filename)
|
|
|
|
|
if not path:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
map_path = path.relative_to(get_map_directory().resolve()).as_posix()
|
|
|
|
|
get_maps_config().set_ignored(map_path, not visible)
|
|
|
|
|
return True
|
2022-03-31 07:31:15 -06:00
|
|
|
|
|
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
def get_map_catalog() -> List[dict]:
|
|
|
|
|
"""Return the list of available map regions."""
|
|
|
|
|
return MAP_REGIONS.copy()
|
2022-03-08 13:26:03 -07:00
|
|
|
|
|
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
def _get_map_download(session: Session) -> Download | None:
|
|
|
|
|
"""Find the single recurring map download, if it exists."""
|
|
|
|
|
return session.query(Download).filter_by(url=MANIFEST_URL, downloader='map_catalog').one_or_none()
|
2022-03-08 13:26:03 -07:00
|
|
|
|
|
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
def _get_or_create_map_download(session: Session) -> Download:
|
|
|
|
|
"""Get or create the single recurring map download."""
|
|
|
|
|
download = _get_map_download(session)
|
|
|
|
|
if download:
|
|
|
|
|
return download
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
download = download_manager.get_or_create_download(session, MANIFEST_URL, reset_attempts=True,
|
|
|
|
|
override_skip=True)
|
|
|
|
|
download.downloader = 'map_catalog'
|
|
|
|
|
download.frequency = DownloadFrequency.days180
|
|
|
|
|
download.settings = {'regions': []}
|
|
|
|
|
download.location = '/map/manage'
|
|
|
|
|
download.attempts = 0
|
|
|
|
|
session.flush([download])
|
|
|
|
|
return download
|
2022-10-22 14:40:08 -06:00
|
|
|
|
2022-10-27 20:22:11 -06:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
def get_map_subscriptions(session: Session) -> List[dict]:
|
|
|
|
|
"""Return the list of subscribed regions."""
|
|
|
|
|
regions = []
|
|
|
|
|
download = _get_map_download(session)
|
|
|
|
|
if download and download.settings:
|
|
|
|
|
regions.extend(download.settings.get('regions', []))
|
|
|
|
|
return regions
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2022-03-31 07:31:15 -06:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
async def subscribe(session: Session, name: str, region: str):
|
|
|
|
|
"""Subscribe to a map region."""
|
|
|
|
|
if name not in MAP_REGIONS_BY_NAME:
|
|
|
|
|
raise ValueError(f'{name!r} is not a valid map region name')
|
|
|
|
|
|
|
|
|
|
region_info = MAP_REGIONS_BY_NAME[name]
|
|
|
|
|
if region != region_info['region']:
|
|
|
|
|
raise ValueError(f'Region {region!r} does not match name {name!r}')
|
|
|
|
|
|
|
|
|
|
download = _get_or_create_map_download(session)
|
|
|
|
|
regions = list(download.settings.get('regions', []))
|
|
|
|
|
|
|
|
|
|
# Don't add duplicates.
|
|
|
|
|
if any(r['region'] == region for r in regions):
|
|
|
|
|
return
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
entry = {'region': region}
|
|
|
|
|
if region_info.get('terrain'):
|
|
|
|
|
entry['terrain'] = True
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
regions.append(entry)
|
|
|
|
|
download.settings = {**download.settings, 'regions': regions}
|
|
|
|
|
download.renew(reset_attempts=True)
|
|
|
|
|
session.commit()
|
|
|
|
|
save_downloads_config.activate_switch()
|
2023-08-17 08:27:49 -06:00
|
|
|
|
|
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
async def unsubscribe(session: Session, region: str):
|
|
|
|
|
"""Remove a region from the subscriptions."""
|
|
|
|
|
download = _get_map_download(session)
|
|
|
|
|
if not download:
|
|
|
|
|
raise ValueError(f'No map subscriptions exist')
|
2024-03-06 17:41:04 -07:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
regions = [r for r in download.settings.get('regions', []) if r['region'] != region]
|
|
|
|
|
if regions:
|
|
|
|
|
download.settings = {**download.settings, 'regions': regions}
|
|
|
|
|
else:
|
|
|
|
|
# No more subscriptions — delete the download.
|
|
|
|
|
session.delete(download)
|
|
|
|
|
session.commit()
|
|
|
|
|
save_downloads_config.activate_switch()
|
2024-03-06 17:41:04 -07:00
|
|
|
|
|
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
async def fetch_manifest(url: str = None) -> dict:
|
|
|
|
|
"""Fetch the map manifest JSON from the CDN and verify its GPG signature."""
|
|
|
|
|
url = url or MANIFEST_URL
|
|
|
|
|
sig_url = f'{url}.sig'
|
2024-03-06 17:41:04 -07:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
# Fetch manifest.
|
|
|
|
|
async with aiohttp_get(url, timeout=30) as response:
|
|
|
|
|
if response.status != 200:
|
|
|
|
|
raise RuntimeError(f'Failed to fetch manifest: HTTP {response.status}')
|
|
|
|
|
manifest_bytes = await response.content.read()
|
2024-03-06 17:41:04 -07:00
|
|
|
|
2026-03-29 18:58:31 -06:00
|
|
|
# Fetch detached signature.
|
|
|
|
|
try:
|
|
|
|
|
async with aiohttp_get(sig_url, timeout=30) as response:
|
|
|
|
|
if response.status != 200:
|
|
|
|
|
raise RuntimeError(f'Failed to fetch manifest signature: HTTP {response.status}')
|
|
|
|
|
signature_bytes = await response.content.read()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning(f'Could not fetch manifest signature from {sig_url}: {e}')
|
|
|
|
|
raise RuntimeError(f'Manifest signature not available: {e}')
|
|
|
|
|
|
|
|
|
|
# Write to temp files and verify signature against on-disk files.
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
manifest_path = Path(tmpdir) / 'manifest.json'
|
|
|
|
|
sig_path = Path(tmpdir) / 'manifest.json.sig'
|
|
|
|
|
manifest_path.write_bytes(manifest_bytes)
|
|
|
|
|
sig_path.write_bytes(signature_bytes)
|
|
|
|
|
|
|
|
|
|
if not await verify_gpg_signature(manifest_path, sig_path):
|
|
|
|
|
raise RuntimeError('Manifest GPG signature verification failed — file may be tampered with')
|
|
|
|
|
|
|
|
|
|
logger.info('Manifest GPG signature verified successfully')
|
|
|
|
|
return json.loads(manifest_bytes)
|