57 lines
2.6 KiB
Python
57 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""The navigation server reads the same verified snapshot used by its pull job."""
|
|
import json
|
|
import os
|
|
import sys
|
|
import time
|
|
from urllib.parse import urlencode, urlsplit
|
|
from public_client import read, current, validate_manifest
|
|
|
|
CONFIG={'project':'fuc','environment':'production','instance':'navigation','providers':['fuc']}
|
|
|
|
def snapshot(path):
|
|
state=read(path)
|
|
if not current(state): raise ValueError('snapshot_missing_or_expired')
|
|
m=state['manifest']
|
|
watermark=read(str(path)+'.watermark',{})
|
|
if watermark.get('version')!=m.get('version') or not state.get('digest') or watermark.get('digest')!=state['digest']: raise ValueError('snapshot_rollback')
|
|
if any(m.get(k)!=CONFIG[k] for k in ('project','environment','instance')): raise ValueError('wrong_scope')
|
|
validate_manifest(m,CONFIG)
|
|
return state
|
|
|
|
def entries(path):
|
|
state=snapshot(path)
|
|
values=[]
|
|
for entry in state['manifest']['providers']['fuc']['entries']:
|
|
if entry['role']=='navigation': continue
|
|
entry=dict(entry)
|
|
entry['display_name']=entry['hostname'].encode('ascii').decode('idna')
|
|
if entry.get('valid_until'):
|
|
from datetime import datetime, timezone
|
|
if datetime.fromisoformat(entry['valid_until']).replace(tzinfo=timezone.utc).timestamp()<=time.time():
|
|
entry.update(eligible=False,reason='evidence_expired')
|
|
# Manual navigation and advertising have different audiences. A reachability
|
|
# warning suppresses automatic probes, but does not revoke a public link.
|
|
entry['selectable'] = entry.get('reason') in {None, 'requires_proxy', 'suspected_blocked', 'blocked', 'unknown', 'evidence_expired', 'service_unknown', 'service_unhealthy'}
|
|
entry['probe_enabled'] = entry['selectable'] and entry['eligible']
|
|
values.append(entry)
|
|
return state['manifest']['version'],values
|
|
|
|
def destination(path, code, invite=''):
|
|
_,values=entries(path)
|
|
item=next((e for e in values if e['code']==code and e['selectable']),None)
|
|
if not item: return None
|
|
import re
|
|
query='?'+urlencode({'code':invite}) if re.fullmatch('[A-Za-z0-9_-]{1,128}',invite) else ''
|
|
return item['url'].rstrip('/')+'/#/register'+query
|
|
|
|
def catalog(path):
|
|
return {e['code']:e['hostname'] for e in entries(path)[1]}
|
|
|
|
if __name__=='__main__':
|
|
try:
|
|
state=snapshot(sys.argv[1]); version=state['manifest']['version']
|
|
if version!=int(os.environ['DOMAINHUB_EXPECT_VERSION']): raise ValueError('wrong_version')
|
|
print(json.dumps({'version':version,'consumer':'fuc-navigation'}))
|
|
except Exception: sys.exit(1)
|