feat(deploy): add guarded active broker permission profile updates

This commit is contained in:
ex_tianzy
2026-11-06 10:15:23 +08:00
parent 8d0c82385b
commit 2e1d88b3fa
4 changed files with 197 additions and 1 deletions
+65
View File
@@ -551,3 +551,68 @@ def test_prepare_update_rejects_partial_switch_and_existing_output(scenario):
scenario['roles']['router']['image']=image
args.output=str(root)
with pytest.raises(deploy.DeployError,match='new absolute'):deploy.prepare_update(args)
def permission_scenario(scenario,monkeypatch):
plan=prepared(scenario)
image='sha256:'+'a'*64
deploy.write_json(scenario['output']/'candidate.json',{'image':image,'tag':plan['candidate_image']})
for name in ('connector','router'):
scenario['roles'][name]['image']=image
scenario['roles'][name]['state']={'status':'running','health':'healthy'}
profiles=scenario['profiles'];profiles.write_text(json.dumps({'product-v1':['read','wiki:wiki']}))
original=deploy.run
def scoped_run(args,*a,**kw):
if args[:2]==['docker','inspect'] and args[-1]=='{{json .Config.Env}}':
overlay=json.loads((scenario['output']/'compose.broker.json').read_text())
return ['HN_CREDENTIAL_PERMISSION_PROFILES='+overlay['services']['connector']['environment']['HN_CREDENTIAL_PERMISSION_PROFILES']]
return original(args,*a,**kw)
monkeypatch.setattr(deploy,'run',scoped_run)
return plan
def test_permission_review_is_read_only_and_redacted(scenario,monkeypatch,capsys):
plan=permission_scenario(scenario,monkeypatch)
before=(scenario['output']/'compose.broker.json').read_bytes()
deploy.update_permissions(scenario['output'],plan,scenario['profiles'])
assert (scenario['output']/'compose.broker.json').read_bytes()==before
assert not any('up' in c for c in scenario['calls'])
text=capsys.readouterr().out
assert 'wiki:wiki' in text and 'synthetic-app-secret' not in text
def test_permission_apply_changes_only_connector_scope_and_preserves_keyring(scenario,monkeypatch):
plan=permission_scenario(scenario,monkeypatch)
key=(scenario['output']/'credential-keyring.json').read_bytes()
deploy.update_permissions(scenario['output'],plan,scenario['profiles'],apply=True)
overlay=json.loads((scenario['output']/'compose.broker.json').read_text())
assert json.loads(overlay['services']['connector']['environment']['HN_CREDENTIAL_PERMISSION_PROFILES'])=={'product-v1':['read','wiki:wiki']}
updated=deploy.load_plan(scenario['output'])
assert updated['prepared_hashes']['compose.broker.json']==deploy.digest(scenario['output']/'compose.broker.json')
assert (scenario['output']/'credential-keyring.json').read_bytes()==key
calls=[c for c in scenario['calls'] if 'up' in c]
assert len(calls)==1 and calls[0][-1]=='connector' and '--no-deps' in calls[0]
before=len(scenario['calls'])
deploy.update_permissions(scenario['output'],updated,scenario['profiles'],apply=True)
assert not any('up' in c for c in scenario['calls'][before:])
def test_permission_update_rejects_scope_removal(scenario,monkeypatch):
plan=permission_scenario(scenario,monkeypatch)
scenario['profiles'].write_text(json.dumps({'product-v1':['wiki:wiki']}))
with pytest.raises(deploy.DeployError,match='additive'):deploy.update_permissions(scenario['output'],plan,scenario['profiles'],True)
assert not any('up' in c for c in scenario['calls'])
def test_permission_failed_restart_restores_overlay_and_plan(scenario,monkeypatch):
plan=permission_scenario(scenario,monkeypatch)
before={name:(scenario['output']/name).read_bytes() for name in ('compose.broker.json','plan.json')}
original=deploy.run;calls=[0]
def fail_once(args,*a,**kw):
if 'up' in args:
calls[0]+=1
if calls[0]==1:raise deploy.DeployError('synthetic restart failure')
return original(args,*a,**kw)
monkeypatch.setattr(deploy,'run',fail_once)
with pytest.raises(deploy.DeployError,match='restored'):deploy.update_permissions(scenario['output'],plan,scenario['profiles'],True)
assert calls[0]==2
for name,value in before.items():assert json.loads((scenario['output']/name).read_text())==json.loads(value)