fix(stage): verify existing database alias for bootstrap runtime
Stage author bootstrap (dedicated repair) / bootstrap (push) Failing after 38s
Stage author bootstrap (dedicated repair) / bootstrap (push) Failing after 38s
This commit is contained in:
1 parent
46da1c5339
commit
2408bafc49
3 files changed
+54
-4
No files matched your search
@@ -16,6 +16,18 @@ op = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(op)
|
||||
|
||||
|
||||
def validate_runtime_url(value, database_info):
|
||||
from urllib.parse import urlparse
|
||||
url = urlparse(value)
|
||||
aliases = {a for n in database_info.get('NetworkSettings', {}).get('Networks', {}).values()
|
||||
for a in (n.get('Aliases') or [])}
|
||||
if (url.scheme not in ('postgres', 'postgresql')
|
||||
or url.hostname not in ({op.STAGE, 'db_stage'} & (aliases | {op.STAGE}))
|
||||
or url.port not in (None, 5432) or url.path != '/caninodb_stage'
|
||||
or url.fragment or url.query not in ('', 'schema=public')):
|
||||
raise ValueError('Existing Stage runtime DB identity mismatch')
|
||||
|
||||
|
||||
def run(env=os.environ, call=subprocess.run):
|
||||
sha = env.get('GITHUB_SHA', '')
|
||||
refs = {'refs/heads/ops/author-bootstrap-stage-rehearse': 'rehearse',
|
||||
@@ -47,10 +59,9 @@ def run(env=os.environ, call=subprocess.run):
|
||||
if info.get('Name') != '/' + op.BACKEND or not info.get('State', {}).get('Running'):
|
||||
raise ValueError('Existing Stage runtime unavailable')
|
||||
values = dict(x.split('=', 1) for x in info['Config']['Env'])
|
||||
from urllib.parse import urlparse
|
||||
url = urlparse(values.get('DATABASE_URL', ''))
|
||||
if url.hostname != op.STAGE or url.path != '/caninodb_stage' or url.query or url.fragment:
|
||||
raise ValueError('Existing Stage runtime DB identity mismatch')
|
||||
database_info = json.loads(command(['docker', 'inspect', op.STAGE], capture_output=True).stdout)[0]
|
||||
op.check_identity(database_info)
|
||||
validate_runtime_url(values.get('DATABASE_URL', ''), database_info)
|
||||
if any('\n' in v or '\r' in v for v in values.values()): raise ValueError('Multiline env refused')
|
||||
live_env = evidence / 'stage.env'
|
||||
op.private_write(live_env, '\n'.join(k + '=' + v for k, v in values.items()).encode())
|
||||
|
||||
@@ -30,6 +30,13 @@ class WorkflowTests(unittest.TestCase):
|
||||
if args[:2] == ['docker','build']:
|
||||
if build_fail: raise subprocess.CalledProcessError(1,args)
|
||||
Path(args[args.index('--iidfile')+1]).write_text('sha256:'+'b'*64)
|
||||
if args[:3] == ['docker','inspect', ci.op.STAGE]:
|
||||
return subprocess.CompletedProcess(args,0,stdout=json.dumps([{
|
||||
'Name':'/'+ci.op.STAGE, 'Image':ci.op.POSTGRES_IMAGE, 'Id':'c'*64,
|
||||
'State':{'Running':True}, 'Config':{'Env':['POSTGRES_DB=caninodb_stage']},
|
||||
'Mounts':[{'Destination':'/var/lib/postgresql/data','RW':True,'Type':'volume'}],
|
||||
'NetworkSettings':{'Networks':{'stage':{'Aliases':['canino_db_stage','db_stage']}}}
|
||||
}]).encode())
|
||||
if args[:2] == ['docker','inspect']:
|
||||
return subprocess.CompletedProcess(args,0,stdout=json.dumps([{
|
||||
'Name':'/canino_backend_stage','State':{'Running':True},
|
||||
@@ -51,6 +58,23 @@ class WorkflowTests(unittest.TestCase):
|
||||
self.assertEqual((root/'evidence/clone.env').stat().st_mode & 0o777,0o600)
|
||||
return calls
|
||||
|
||||
def test_stage_runtime_url_identity(self):
|
||||
network = {'NetworkSettings': {'Networks': {'stage': {'Aliases': ['canino_db_stage', 'db_stage']}}}}
|
||||
ci.validate_runtime_url('postgresql://u:p@db_stage:5432/caninodb_stage?schema=public', network)
|
||||
ci.validate_runtime_url('postgresql://u:p@canino_db_stage:5432/caninodb_stage', network)
|
||||
for url in ['postgresql://u:p@db_stage/caninodb_stage?'+q for q in
|
||||
('&&','schema=public&','%73chema=public','schema=%70ublic')] + [
|
||||
'postgresql://u:p@db_prod/caninodb_stage?schema=public',
|
||||
'postgresql://u:p@db_stage/caninodb_prod?schema=public',
|
||||
'postgresql://u:p@db_stage/caninodb_stage?schema=private',
|
||||
'postgresql://u:p@db_stage/caninodb_stage?schema=public&schema=public',
|
||||
'postgresql://u:p@db_stage/caninodb_stage?sslmode=disable',
|
||||
'postgresql://u:p@db_stage:5433/caninodb_stage',
|
||||
'postgresql://u:p@db_stage/caninodb_stage#fragment']:
|
||||
with self.assertRaises(ValueError): ci.validate_runtime_url(url, network)
|
||||
with self.assertRaises(ValueError):
|
||||
ci.validate_runtime_url('postgresql://u:p@db_stage/caninodb_stage', {})
|
||||
|
||||
def test_real_ci_driver_rehearse(self): self.exercise('refs/heads/ops/author-bootstrap-stage-rehearse')
|
||||
def test_real_ci_driver_apply(self): self.exercise('refs/heads/ops/author-bootstrap-stage-apply')
|
||||
def test_missing_token_still_builds(self):
|
||||
|
||||
Reference in new issue
Block a user