Species: a monthly build of the packs, signed where the key is (#1036)

* Species: a monthly build of the packs, signed where the key is

The build host builds the packs unsigned (inat-build.sh, a systemd timer), and the machine with the signing key
signs the hashes and the manifest from manifest.json alone (inat-publish.sh), so the packs never leave the build host
and the key never goes to it. build_inat_regions.py --no-sign now still writes each pack's sha256, and --sign signs
such a build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Species: sign only a well-formed build manifest, written again where the key is

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Species: inat-publish.sh passes the bucket to the upload, so a try stays out of the live packs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Species: the monthly build publishes nothing older than what is published

The version is the observations' publication date; a build of the same (or older) observations would replace the
published packs with an older version, and the prune would delete the newer ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-30 15:05:22 -07:00 • committed by GitHub
parent e61d87483a
commit 04ddc7c63c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 378 additions and 6 deletions

View file

@ -1,5 +1,6 @@
import csv
import gzip
import hashlib
import io
import json
import sqlite3
@ -363,6 +364,91 @@ def test_write_manifest(test_directory, west_pack):
sign_manifest.assert_called_once_with(str(manifest_path))
def test_write_manifest_unsigned(test_directory, west_pack):
"""Without signing, the manifest still has each pack's sha256, for `sign_build` to sign elsewhere."""
db_path, _ = west_pack
with mock.patch.object(build, 'write_meta4') as write_meta4, \
mock.patch.object(build, 'sign_manifest') as sign_manifest:
manifest_path = build.write_manifest(test_directory, '20261123', 'https://cdn.example/inat',
{'us-west': db_path}, sign=False)
url = 'https://cdn.example/inat/20261123/inat-us-west-20261123.species.db'
assert json.loads(manifest_path.read_text())['sha256'] == {url: hashlib.sha256(db_path.read_bytes()).hexdigest()}
write_meta4.assert_not_called()
sign_manifest.assert_not_called()
CDN = 'https://cdn.example/inat'
URL = f'{CDN}/20261123/inat-us-west-20261123.species.db'
def unsigned_build(output_dir, **changes):
"""The manifest of an unsigned build, without the packs (they stay on the build host)."""
manifest = dict(version='20261123', license='CC BY-NC 4.0', attribution='...', regions={'us-west': URL},
sizes={'us-west': 1234}, sha256={URL: 'a' * 64})
manifest.update(changes)
output_dir.mkdir(parents=True, exist_ok=True)
(output_dir / 'manifest.json').write_text(json.dumps(manifest))
def test_sign_build(test_directory):
"""Signs an unsigned build's hashes (a meta4 per pack) and its manifest, without the packs."""
output_dir = test_directory / 'out'
unsigned_build(output_dir, extra='not signed')
old_signature = output_dir / 'manifest.json.sig'
old_signature.write_text('old')
with mock.patch.object(build, 'sign_hash', return_value='SIGNATURE') as sign_hash, \
mock.patch.object(build, 'sign_manifest', side_effect=lambda path: assert_not_exists(old_signature)) \
as sign_manifest:
build.sign_build(output_dir, CDN)
sign_hash.assert_called_once_with('a' * 64)
sign_manifest.assert_called_once_with(str(output_dir / 'manifest.json'))
meta4 = (output_dir / '20261123' / 'inat-us-west-20261123.species.db.meta4').read_text()
assert 'a' * 64 in meta4 and URL in meta4 and '<size>1234</size>' in meta4 and 'SIGNATURE' in meta4
# The manifest that is signed is written here, from what was checked: nothing else is carried over.
manifest = json.loads((output_dir / 'manifest.json').read_text())
assert 'extra' not in manifest
assert manifest['attribution'] == build.ATTRIBUTION
assert manifest['regions'] == {'us-west': URL} and manifest['sha256'] == {URL: 'a' * 64}
@pytest.mark.parametrize('changes', [
dict(version='../../etc'),
dict(version=20261123),
dict(regions={'atlantis': URL}),
dict(regions={'us-west': 'https://elsewhere.example/inat-us-west-20261123.species.db'}),
dict(regions={'us-west': f'{CDN}/20261123/../../evil.species.db'}),
dict(regions={}),
dict(sha256={URL: None}),
dict(sha256={URL: 'not a hash'}),
dict(sizes={'us-west': 0}),
dict(sizes={'us-west': '1234'}),
])
def test_sign_build_refuses(test_directory, changes):
"""Only what a build writes is signed: a manifest from a compromised or broken build host is refused."""
output_dir = test_directory / 'out'
unsigned_build(output_dir, **changes)
with mock.patch.object(build, 'sign_hash') as sign_hash, \
mock.patch.object(build, 'sign_manifest') as sign_manifest, pytest.raises(ValueError):
build.sign_build(output_dir, CDN)
sign_hash.assert_not_called()
sign_manifest.assert_not_called()
assert not list(test_directory.rglob('*.meta4'))
def test_main_sign(test_directory):
"""--sign signs an unsigned build; it needs no work directory."""
with mock.patch('sys.argv', ['build_inat_regions.py', '--sign', str(test_directory), '--cdn-base', CDN]), \
mock.patch.object(build, 'sign_build') as sign_build:
build.main()
sign_build.assert_called_once_with(test_directory, CDN)
def test_main_needs_directories():
with mock.patch('sys.argv', ['build_inat_regions.py']), pytest.raises(SystemExit):
build.main()
def test_main(test_directory, dwca_path, taxa_path, taxonomy_path):
"""The whole build, offline: given sources, two regions, no signing."""
work_dir, output_dir = test_directory / 'work', test_directory / 'out'
@ -396,6 +482,18 @@ def test_main(test_directory, dwca_path, taxa_path, taxonomy_path):
assert db.execute('SELECT COUNT(*) FROM species').fetchone() == (4,)
@pytest.mark.parametrize('newer_than,built', [('20261122', True), ('20261123', False), ('20261129', False)])
def test_main_newer_than(test_directory, dwca_path, taxa_path, taxonomy_path, newer_than, built):
"""--newer-than: observations no newer than those published build nothing, so nothing older is published."""
output_dir = test_directory / 'out'
argv = ['build_inat_regions.py', str(test_directory / 'work'), str(output_dir), '--dwca', str(dwca_path), '--taxa',
str(taxa_path), '--taxonomy', str(taxonomy_path), '--regions', 'us-west', '--no-sign',
'--newer-than', newer_than]
with mock.patch('sys.argv', argv), mock.patch.object(build, 'query_wikidata', return_value=[]):
build.main()
assert (output_dir / 'manifest.json').is_file() == built
@pytest.mark.parametrize('extra', [['--version', '2026-11-28'], ['--reuse-split']])
def test_main_invalid(test_directory, dwca_path, taxa_path, taxonomy_path, extra):
"""A version must be YYYYMMDD; --reuse-split needs the work files of a last build."""