feat(work): stage authenticated remote approvals with scoped execution fencing

This commit is contained in:
ex_tianzy
2026-11-17 11:43:33 +08:00
parent d2881b82b0
commit c80c6a0d27
19 changed files with 1586 additions and 25 deletions
+87
View File
@@ -672,3 +672,90 @@ def test_prepare_update_cards_are_opt_in_and_rollback_keeps_parent_environment(s
assert current['services']['connector']['environment']['HN_WORK_TASK_CARDS_ENABLED']=='true'
assert rollback['services']['connector']['environment']==old['services']['connector']['environment']
assert (output/'credential-keyring.json').read_bytes()==(root/'credential-keyring.json').read_bytes()
def approval_update(scenario,**changes):
parent=prepared(scenario);root=scenario['output'];image='sha256:'+'a'*64
for name in ('connector','router'):scenario['roles'][name]['image']=image
deploy.write_json(root/'candidate.json',{'image':image,'tag':parent['candidate_image']})
env=scenario['base']['services']['connector']['environment']
scenario['base']['services']['longconn']['environment'].update(
HN_FEISHU_APP_ID=env['HN_FEISHU_APP_ID'],HN_INTERNAL_SECRET=env['HN_INTERNAL_SECRET'],HN_FEISHU_EVENT_SOURCE='longconn')
identity=root.parent/'callback-private.json'
identity.write_text(json.dumps({'tenant_key':'tenant_test','verification_token':'synthetic-verify-token'}))
identity.chmod(0o600)
args=argparse.Namespace(plan=str(root),source_root=str(ROOT),output=str(root.parent/'approvals'),
enable_remote_approvals=True,callback_identity=str(identity))
for key,value in changes.items():setattr(args,key,value)
return args
def test_approval_update_preserves_runtime_ancestry_and_original_longconn_rollback(scenario):
args=approval_update(scenario);deploy.prepare_update(args)
root=Path(args.output);plan=deploy.load_plan(root)
candidate=json.loads((root/'compose.broker.json').read_text());rollback=json.loads((root/'compose.rollback.json').read_text())
assert deploy.targets(plan)==('connector','router','longconn')
assert plan['longconn_base_image']==scenario['roles']['longconn']['image']
assert candidate['services']['longconn']['image']!=candidate['services']['connector']['image']
assert candidate['services']['longconn']['environment']['HN_CREDENTIAL_MODE']=='broker'
assert rollback['services']['longconn']['image']==scenario['roles']['longconn']['image']
assert rollback['services']['longconn']['environment']['HN_CREDENTIAL_MODE']=='legacy'
assert rollback['services']['longconn']['environment']['HN_WORK_REMOTE_APPROVALS_ENABLED']=='false'
assert (root/'credential-keyring.json').read_bytes()==(Path(args.plan)/'credential-keyring.json').read_bytes()
def test_approval_runtime_checks_three_roles_and_sdk_without_network(scenario):
args=approval_update(scenario);deploy.prepare_update(args);root=Path(args.output);plan=deploy.load_plan(root)
scenario['calls'].clear();deploy.runtime_check(root,plan)
probes=[c for c in scenario['calls']if c[:2]==['docker','run']]
assert len(probes)==3
assert all('--network'in c and c[c.index('--network')+1]=='none'for c in probes)
assert any(plan['longconn_candidate_image']in c and 'register_p2_card_action_trigger'in c[-1]for c in probes)
assert not any('synthetic-verify-token'in part for c in probes for part in c)
def test_approval_activation_targets_original_three_services_only(scenario):
args=approval_update(scenario);deploy.prepare_update(args);root=Path(args.output);plan=deploy.load_plan(root)
image='sha256:'+'a'*64
deploy.write_json(root/'candidate.json',{'image':image,'tag':plan['candidate_image']})
deploy.write_json(root/'candidate-longconn.json',{'image':image,'tag':plan['longconn_candidate_image']})
scenario['calls'].clear();deploy.activate(root,plan,True)
up=[c for c in scenario['calls']if 'up'in c]
assert len(up)==1 and up[0][-3:]==['connector','router','longconn']
assert '--no-deps'in up[0]and '--pull'in up[0]
def test_approval_overlay_rejects_longconn_command_or_network_change(scenario):
args=approval_update(scenario);deploy.prepare_update(args);root=Path(args.output);plan=deploy.load_plan(root)
base=deploy.run(deploy.compose(plan)+['config','--format','json'],True)
merged=deploy.run(deploy.compose(plan,root/'compose.broker.json')+['config','--format','json'],True)
merged['services']['longconn']['entrypoint']=['sh','-c','unreviewed']
with pytest.raises(deploy.DeployError,match='unrelated longconn'):
deploy.validate_overlay(base,merged,plan['candidate_image'],approval_plan=plan)
def test_approval_update_rejects_different_app_or_missing_identity(scenario):
args=approval_update(scenario)
scenario['base']['services']['longconn']['environment']['HN_FEISHU_APP_ID']='other'
with pytest.raises(deploy.DeployError,match='does not match'):deploy.prepare_update(args)
assert not Path(args.output).exists()
def test_approval_build_reuses_distinct_longconn_base_and_saves_both_receipts(scenario):
args=approval_update(scenario);deploy.prepare_update(args);root=Path(args.output);plan=deploy.load_plan(root)
scenario['calls'].clear();deploy.build(root,plan)
commands=[c for c in scenario['calls']if c[:2]==['docker','build']]
assert len(commands)==2
assert 'BASE_IMAGE='+scenario['roles']['longconn']['image']in commands[1]
assert plan['longconn_candidate_image']in commands[1]
assert all('--network'in c and c[c.index('--network')+1]=='none'for c in commands)
assert (root/'candidate.json').is_file()and(root/'candidate-longconn.json').is_file()
def test_approval_cli_rollback_restores_original_longconn_too(scenario,monkeypatch):
args=approval_update(scenario);deploy.prepare_update(args);root=Path(args.output)
monkeypatch.setattr(deploy.sys,'argv',['broker_deploy.py','rollback','--plan',str(root),'--stop-broker-use'])
scenario['calls'].clear();deploy.main()
commands=[c for c in scenario['calls']if 'up'in c]
assert len(commands)==1 and commands[0][-3:]==['connector','router','longconn']
assert str(root/'compose.rollback.json')in commands[0]