180 lines
8.1 KiB
Python
180 lines
8.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Publish the reviewed nearby-post-media API hotfix with automatic rollback."""
|
|
import base64
|
|
import fcntl
|
|
import hashlib
|
|
import hmac
|
|
import json
|
|
import os
|
|
import pwd
|
|
import shutil
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, '/www/server/xingyu-im/ops')
|
|
from baota_publish_test_users import BT, DOMAIN, WEB, database, digest, env_values, extract, fetch, health, query, restart
|
|
|
|
OLD = 'b7eeb926d063855e4271815872ba8c90e226554a3824d48e0995479a671e546e'
|
|
BASE = Path('/www/server/xingyu-im/releases/avatars-20260831-180014')
|
|
ALLOWED = {'backend/internal/app/social.go'}
|
|
TARGET_PUBLIC_ID = 'TESTCN000100'
|
|
|
|
|
|
def source_hashes(archive):
|
|
with zipfile.ZipFile(str(archive)) as package:
|
|
result = {}
|
|
for item in package.infolist():
|
|
if item.is_dir():
|
|
continue
|
|
name = item.filename.replace('\\', '/')
|
|
assert name not in result and not name.startswith('/') and '..' not in Path(name).parts
|
|
result[name] = hashlib.sha256(package.read(item)).hexdigest()
|
|
return result
|
|
|
|
|
|
def running(expected):
|
|
pids = []
|
|
for proc in Path('/proc').iterdir():
|
|
if not proc.name.isdigit():
|
|
continue
|
|
try:
|
|
if os.readlink(str(proc / 'exe')) == str(BT / 'xingyu-api'):
|
|
assert digest(proc / 'exe') == expected
|
|
pids.append(int(proc.name))
|
|
except (FileNotFoundError, PermissionError):
|
|
pass
|
|
assert len(pids) == 1, 'expected one running project executable'
|
|
return pids
|
|
|
|
|
|
def replace(source, stamp):
|
|
temporary = BT / ('xingyu-api.nearby-post-media-' + stamp)
|
|
assert not temporary.exists()
|
|
shutil.copy2(str(source), str(temporary))
|
|
www = pwd.getpwnam('www')
|
|
os.chown(str(temporary), www.pw_uid, www.pw_gid)
|
|
os.chmod(str(temporary), 0o750)
|
|
os.replace(str(temporary), str(BT / 'xingyu-api'))
|
|
|
|
|
|
def encode_json(value):
|
|
raw = json.dumps(value, separators=(',', ':'), ensure_ascii=False).encode()
|
|
return base64.urlsafe_b64encode(raw).rstrip(b'=').decode()
|
|
|
|
|
|
def user_token(secret, user_id, nickname, version):
|
|
now = int(time.time())
|
|
header = encode_json({'alg': 'HS256', 'typ': 'JWT'})
|
|
payload = encode_json({'role': 'user', 'name': nickname, 'ver': version, 'sub': str(user_id), 'iat': now, 'exp': now + 300})
|
|
signing_input = (header + '.' + payload).encode()
|
|
signature = base64.urlsafe_b64encode(hmac.new(secret.encode(), signing_input, hashlib.sha256).digest()).rstrip(b'=').decode()
|
|
return header + '.' + payload + '.' + signature
|
|
|
|
|
|
def verify_nearby_response(conn, values):
|
|
target = query(conn, "SELECT post.id,COUNT(media.id) FROM posts post JOIN users user ON user.id=post.user_id LEFT JOIN post_media media ON media.post_id=post.id WHERE user.public_id=%s AND post.status=1 AND post.moderation_status=1 AND post.deleted_at IS NULL GROUP BY post.id ORDER BY post.created_at DESC,post.id DESC LIMIT 1", (TARGET_PUBLIC_ID,))
|
|
assert target and target[0][1] == 2, 'expected the reviewed two-image post'
|
|
viewer = query(conn, "SELECT user.id,profile.nickname,security.token_version FROM users user JOIN user_profiles profile ON profile.user_id=user.id JOIN user_security_controls security ON security.user_id=user.id WHERE user.public_id<>%s AND user.status=1 AND user.deleted_at IS NULL ORDER BY user.id LIMIT 1", (TARGET_PUBLIC_ID,))[0]
|
|
token = user_token(values['IM_JWT_SECRET'], viewer[0], viewer[1], viewer[2])
|
|
found = None
|
|
for page in (1, 2, 3):
|
|
request = urllib.request.Request(
|
|
DOMAIN + '/api/v1/nearby/users?page=' + str(page) + '&pageSize=50',
|
|
headers={'Authorization': 'Bearer ' + token, 'Accept': 'application/json'},
|
|
)
|
|
with urllib.request.urlopen(request, timeout=30) as response:
|
|
assert response.status == 200
|
|
payload = json.loads(response.read())
|
|
for item in payload['data']['items']:
|
|
if item.get('publicId') == TARGET_PUBLIC_ID:
|
|
found = item
|
|
break
|
|
if found:
|
|
break
|
|
assert found, 'target user was not returned by nearby API'
|
|
latest = found.get('latestPost') or {}
|
|
assert latest.get('id') == target[0][0]
|
|
assert len(latest.get('media') or []) == 2
|
|
assert latest.get('mediaThumbnails') == latest.get('media')
|
|
return {'publicId': TARGET_PUBLIC_ID, 'postId': latest['id'], 'mediaCount': len(latest['media'])}
|
|
|
|
|
|
def publish(archive, expected):
|
|
assert os.geteuid() == 0
|
|
archive = Path(archive).resolve()
|
|
assert archive.parent == Path('/tmp') and digest(archive) == expected
|
|
lock = open('/www/server/xingyu-im/ops/application-release.lock', 'a')
|
|
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
assert digest(BT / 'xingyu-api') == OLD, 'production changed; re-audit required'
|
|
running(OLD)
|
|
health()
|
|
stamp = time.strftime('%Y%m%d-%H%M%S')
|
|
stage = Path('/www/server/xingyu-im/releases/nearby-post-media-' + stamp)
|
|
extract(archive, stage)
|
|
os.chmod(str(stage), 0o700)
|
|
old_source = source_hashes(BASE / 'backend-source.zip')
|
|
new_source = source_hashes(stage / 'backend-source.zip')
|
|
changed = {name for name in set(old_source) | set(new_source) if old_source.get(name) != new_source.get(name)}
|
|
assert changed == ALLOWED, 'unreviewed source changes: ' + repr(changed)
|
|
new_hash = digest(stage / 'xingyu-api')
|
|
assert (stage / 'xingyu-api').read_bytes()[:4] == b'\x7fELF' and new_hash != OLD
|
|
manifest = json.loads((stage / 'manifest.json').read_text())
|
|
for name in ['xingyu-api', 'backend-source.zip']:
|
|
assert digest(stage / name) == manifest['files'][name]
|
|
values = env_values()
|
|
conn, _ = database(values)
|
|
try:
|
|
before_migrations = dict(query(conn, 'SELECT version,checksum FROM schema_migrations'))
|
|
for name, sha in before_migrations.items():
|
|
assert new_source['backend/migrations/' + name] == sha
|
|
assert len(before_migrations) == len([name for name in new_source if name.startswith('backend/migrations/')])
|
|
frontend = {name: str((WEB / name).resolve()) for name in ['admin', 'app']}
|
|
backup = Path('/www/backup/xingyu-nearby-post-media-' + stamp)
|
|
backup.mkdir(mode=0o700)
|
|
shutil.copy2(str(BT / 'xingyu-api'), str(backup / 'xingyu-api-original'))
|
|
state = {
|
|
'backup': str(backup), 'stage': str(stage), 'previousBackendSha256': OLD,
|
|
'backendSha256': new_hash, 'archiveSha256': expected,
|
|
'changedSourceFiles': sorted(changed), 'frontendUnchanged': frontend,
|
|
'databaseMigrationsApplied': [], 'businessDataWrites': False,
|
|
}
|
|
(backup / 'before.json').write_text(json.dumps(state, indent=2))
|
|
print('BACKUP_VERIFIED=' + str(backup), flush=True)
|
|
changed_binary = False
|
|
try:
|
|
replace(stage / 'xingyu-api', stamp)
|
|
changed_binary = True
|
|
restart()
|
|
state['runningPids'] = running(new_hash)
|
|
assert digest(BT / 'xingyu-api') == new_hash
|
|
assert dict(query(conn, 'SELECT version,checksum FROM schema_migrations')) == before_migrations
|
|
assert {name: str((WEB / name).resolve()) for name in frontend} == frontend
|
|
for path in ['/api/v1/nearby/users', '/admin/v1/users', '/ws']:
|
|
try:
|
|
fetch(path)
|
|
except urllib.error.HTTPError as error:
|
|
assert error.code == 401
|
|
else:
|
|
raise RuntimeError('private endpoint became public')
|
|
state['nearbyPostMedia'] = verify_nearby_response(conn, values)
|
|
state['healthAndAuthChecks'] = True
|
|
(backup / 'result.json').write_text(json.dumps(state, indent=2))
|
|
print(json.dumps(state), flush=True)
|
|
except Exception:
|
|
if changed_binary:
|
|
replace(backup / 'xingyu-api-original', stamp + '-rollback')
|
|
restart()
|
|
running(OLD)
|
|
(backup / 'rolled-back.txt').write_text('Original binary restored. No database restore performed.\n')
|
|
raise
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
publish(*sys.argv[1:])
|