47 lines
3.4 KiB
Python
47 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Verify and install an explicitly published help directory; never contacts the app."""
|
|
import argparse,hashlib,json,os,shutil,tempfile,grp
|
|
from pathlib import Path,PurePosixPath
|
|
|
|
def consume(source,store,channel,expected,audience="public",runtime_group=None):
|
|
source,store=Path(source).resolve(),Path(store).resolve()
|
|
manifest=(source/'manifest.json').read_bytes()
|
|
if hashlib.sha256(manifest).hexdigest()!=expected:raise ValueError('Approved manifest digest differs')
|
|
m=json.loads(manifest)
|
|
if m['format']!=2 or m['channel']!=channel or m.get('audience')!=audience:raise ValueError('Wrong publication format, channel or audience')
|
|
if not m['files'] or 'articles.json' not in m['files']:raise ValueError('Missing article inventory')
|
|
private_group=grp.getgrnam(runtime_group or 'plotdirector').gr_gid if audience=='application' else None
|
|
store.mkdir(parents=True,exist_ok=True)
|
|
if private_group is not None:
|
|
os.chown(store,-1,private_group);store.chmod(0o750)
|
|
version=store/expected
|
|
if version.exists():raise ValueError('Publication already installed; select its retained directory explicitly')
|
|
with tempfile.TemporaryDirectory(prefix='.incoming-',dir=store) as temp:
|
|
temp=Path(temp)
|
|
for name,digest in m['files'].items():
|
|
path=PurePosixPath(name)
|
|
if path.is_absolute() or '..' in path.parts or '\\' in name:raise ValueError('Unsafe package member')
|
|
src=source/name
|
|
if src.is_symlink() or not src.is_file() or not src.resolve().is_relative_to(source):raise ValueError('Unsafe publication file')
|
|
data=src.read_bytes()
|
|
if hashlib.sha256(data).hexdigest()!=digest:raise ValueError('Publication checksum mismatch')
|
|
out=temp/name;out.parent.mkdir(parents=True,exist_ok=True);out.write_bytes(data)
|
|
articles=json.loads((temp/'articles.json').read_text())
|
|
urls=[a['CategorySlug']+'/'+a['Slug'] for a in articles]
|
|
allowed={'public'} if audience=='public' else {'public','administrator'}
|
|
if len(urls)!=len(set(urls)) or any(a.get('Visibility') not in allowed for a in articles):raise ValueError('Restricted or unclassified article in publication')
|
|
(temp/'manifest.json').write_bytes(manifest)
|
|
for file in temp.rglob('*'):
|
|
if private_group is not None:os.chown(file,-1,private_group)
|
|
file.chmod((0o750 if file.is_dir() else 0o640) if private_group is not None else (0o755 if file.is_dir() else 0o444))
|
|
if private_group is not None:os.chown(temp,-1,private_group)
|
|
temp.chmod(0o750 if private_group is not None else 0o755)
|
|
os.rename(temp,version)
|
|
# TemporaryDirectory cleanup tolerates the directory being atomically moved.
|
|
link=store/('.current-'+expected)
|
|
link.symlink_to(version)
|
|
os.replace(link,store/'current')
|
|
print(json.dumps({'channel':channel,'audience':audience,'version':m['version'],'manifestSha256':expected,'articles':len(articles)}))
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser();p.add_argument('source');p.add_argument('--store',required=True);p.add_argument('--channel',choices=['Development','Production'],required=True);p.add_argument('--manifest-sha256',required=True);p.add_argument('--audience',choices=['public','application'],default='public');p.add_argument('--runtime-group');a=p.parse_args();consume(a.source,a.store,a.channel,a.manifest_sha256,a.audience,a.runtime_group)
|