From 2e1d88b3fa200f17c8e948453c0ac7a410b7c091 Mon Sep 17 00:00:00 2001 From: ex_tianzy Date: Fri, 6 Nov 2026 10:15:23 +0800 Subject: [PATCH] feat(deploy): add guarded active broker permission profile updates --- deployable/EXISTING_ECS_BROKER_DEPLOY.md | 24 ++++++ .../work-product-profiles.expanded.json | 28 +++++++ deployable/scripts/broker_deploy.py | 81 ++++++++++++++++++- deployable/tests/test_broker_deploy.py | 65 +++++++++++++++ 4 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 deployable/examples/work-product-profiles.expanded.json diff --git a/deployable/EXISTING_ECS_BROKER_DEPLOY.md b/deployable/EXISTING_ECS_BROKER_DEPLOY.md index 240e4d8..b5d9821 100644 --- a/deployable/EXISTING_ECS_BROKER_DEPLOY.md +++ b/deployable/EXISTING_ECS_BROKER_DEPLOY.md @@ -140,3 +140,27 @@ sudo python3 deployable/scripts/broker_deploy.py rollback \ 回退仅对connector/router执行up,不操作数据库卷或恢复陈旧refresh。脚本验证原配置和非目标服务未漂移后使用实际旧镜像;原longconn/签发镜像单独保留。**容器回退不等于用户授权无损回退**:服务端已轮换的refresh不能交回旧PC继续刷新,旧客户端需要协调模式/重新授权。保留集中凭据数据库及密钥,不用旧数据库备份覆盖新的平台轮换状态。 activate失败不会自动猜测执行回滚,停止后按此流程处置;如果配置、密钥或运行版本漂移导致check拒绝,应先审阅差异,不用强制选项绕过。 + +## 已运行broker的权限配置更新 + +用于飞书开发者后台完成权限开通/版本发布后,同步ECS实际申请的用户scope。不是重新部署代码,不重建镜像,不生成密钥,不更换客户端模式。 + +先准备产品确认的JSON,保留原profile名称和已有scope。示例只展示新增云盘、wiki、任务读写;其他用户业务权限按开发者后台实际开通的内容加入。 + +```json +{"current-config-v1":["calendar:calendar","calendar:calendar:readonly","drive:drive","wiki:wiki","task:task:write"]} +``` + +对现场最新已激活的计划操作,先预览: + +```bash +cd /home/cnooc/ecs-relay-service-staging-http +git pull --ff-only +sudo python3 deployable/scripts/broker_deploy.py update-permissions \ + --plan /home/cnooc/hainuo-broker-update-20261105-recovery \ + --profiles /home/cnooc/work-product-profiles.expanded.json +``` + +核对预览只列出已开通的scope后,用相同命令加`--apply`。脚本校验活动容器、计划完整性、已有scope不被移除,备份overlay和plan并同步校验记录,只recreate connector;相同配置重跑不重启。镜像、router、长连接、数据库卷、密钥和绑定保持,connector重建期间请求可能短暂中断;失败恢复原配置并尝试重建旧connector,失败状态明确报告。不输出解析后的环境或密钥。 + +完成后必须发起**新的授权事务**:旧授权页面/已创建事务的scope不会随环境变更自动扩展。确认授权URL的scope已包含新增项、实际token返回granted_scopes包含目标范围,再以真实业务code和数据验收。不要手改授权URL绕过ECS产品权限Profile。新权限要求可能使旧凭据暂时需要重新授权,使用测试用户完成验证。 diff --git a/deployable/examples/work-product-profiles.expanded.json b/deployable/examples/work-product-profiles.expanded.json new file mode 100644 index 0000000..e9eb484 --- /dev/null +++ b/deployable/examples/work-product-profiles.expanded.json @@ -0,0 +1,28 @@ +{ + "current-config-v1": [ + "auth:user.id:read", + "bitable:app", + "calendar:calendar", + "calendar:calendar:readonly", + "contact:contact.base:readonly", + "contact:department.base:readonly", + "contact:department.organize:readonly", + "contact:user.base:readonly", + "contact:user.department:readonly", + "contact:user.employee:readonly", + "contact:user.employee_number:read", + "contact:user:search", + "docx:document", + "docx:document.block:convert", + "drive:drive", + "drive:file:readonly", + "im:chat.members:read", + "im:chat:readonly", + "im:message", + "task:task:write", + "task:tasklist:write", + "vc:meeting", + "vc:reserve", + "wiki:wiki" + ] +} diff --git a/deployable/scripts/broker_deploy.py b/deployable/scripts/broker_deploy.py index 4c245a4..f516217 100644 --- a/deployable/scripts/broker_deploy.py +++ b/deployable/scripts/broker_deploy.py @@ -612,6 +612,79 @@ def activate(root,plan,ready): print("ACTIVATED: connector/router only. Verify real OAuth, v2 client, unbind and task routing.") +def container_permission_profiles(container): + values=run(['docker','inspect',container['id'],'--format','{{json .Config.Env}}'],True) + env=dict(value.split('=',1) for value in values if '=' in value) + try: + value=json.loads(env['HN_CREDENTIAL_PERMISSION_PROFILES']) + if not isinstance(value,dict):raise ValueError() + return value + except (KeyError,ValueError,TypeError): + raise DeployError('cannot verify active connector product scopes') from None + + +def update_permissions(root,plan,profiles_path,apply=False): + """Change approved scopes on the active broker, without changing images/keys.""" + profiles=profiles_from(Path(profiles_path)) + check(root,plan,baseline=False) + built=json.loads((root/'candidate.json').read_text(encoding='utf-8')) + current=service(plan['project'],'connector') + if current['image']!=built['image'] or current.get('state',{}).get('health')!='healthy': + raise DeployError('permission update requires the active healthy candidate connector') + overlay_path=root/'compose.broker.json' + overlay=json.loads(overlay_path.read_text(encoding='utf-8')) + old=json.loads(overlay['services']['connector']['environment']['HN_CREDENTIAL_PERMISSION_PROFILES']) + if container_permission_profiles(current)!=old: + raise DeployError('active connector scopes differ from selected plan; no changes made') + if set(old)!=set(profiles) or any(not set(scopes)<=set(profiles[name]) for name,scopes in old.items()): + raise DeployError('permission update is additive only; preserve all existing profiles and scopes') + additions={name:sorted(set(scopes)-set(old[name])) for name,scopes in profiles.items()} + print('Product scope additions: '+json.dumps(additions,sort_keys=True)) + if not any(additions.values()): + print('PERMISSIONS UNCHANGED: no files or containers changed') + return + if not apply: + print('PERMISSIONS REVIEW: no files or containers changed; use --apply after platform approval/publication') + return + backup=root/('permission-backup-'+secrets.token_hex(8)) + backup.mkdir(mode=0o700) + for name in ('compose.broker.json','plan.json'): + shutil.copyfile(str(root/name),str(backup/name));os.chmod(str(backup/name),0o600) + next_overlay=json.loads(json.dumps(overlay)) + next_overlay['services']['connector']['environment']['HN_CREDENTIAL_PERMISSION_PROFILES']=json.dumps(profiles,separators=(',',':')) + next_plan=json.loads(json.dumps(plan)) + touched=False + try: + replace_json(overlay_path,next_overlay);touched=True + next_plan['prepared_hashes']['compose.broker.json']=digest(overlay_path) + replace_json(root/'plan.json',next_plan) + check(root,next_plan,baseline=False) + # Validate what Compose will actually pass, with secrets kept in-process. + resolved=run(compose(next_plan,overlay_path)+['config','--format','json'],True) + value=resolved['services']['connector']['environment'].get('HN_CREDENTIAL_PERMISSION_PROFILES','') + if json.loads(value)!=profiles:raise DeployError('resolved product scopes do not match approved input') + print('PERMISSIONS APPLY: recreate connector only; same image, credentials and storage',flush=True) + command=compose(next_plan,overlay_path)+['up','-d','--no-deps','--no-build','--pull','never', + '--wait','--wait-timeout','180','connector'] + run(command) + after=service(plan['project'],'connector') + if after['image']!=current['image'] or after.get('state',{}).get('health')!='healthy': + raise DeployError('connector did not retain its reviewed healthy image') + if container_permission_profiles(after)!=profiles: + raise DeployError('running connector did not load the approved product scopes') + except Exception: + if touched: + for name in ('compose.broker.json','plan.json'): + replace_json(root/name,json.loads((backup/name).read_text(encoding='utf-8'))) + try: + run(compose(plan,overlay_path)+['up','-d','--no-deps','--no-build','--pull','never', + '--wait','--wait-timeout','180','connector']) + except Exception: + raise DeployError('permission update failed; files restored but connector rollback unconfirmed; run state') from None + raise DeployError('permission update failed; original configuration restored; no credential keys changed') from None + print('PERMISSIONS UPDATED: connector only; backup='+str(backup)+'; obtain a NEW authorization transaction next') + + def main(): parser = argparse.ArgumentParser(description=__doc__) sub = parser.add_subparsers(dest="action",required=True) @@ -628,6 +701,10 @@ def main(): update.add_argument("--plan",required=True) update.add_argument("--source-root",default=str(Path(__file__).resolve().parents[1])) update.add_argument("--output",required=True) + permissions=sub.add_parser('update-permissions') + permissions.add_argument('--plan',required=True) + permissions.add_argument('--profiles',required=True) + permissions.add_argument('--apply',action='store_true') for name in ("repair-router","state","diagnose","check","build","activate","rollback"): cmd = sub.add_parser(name) cmd.add_argument("--plan",required=True) @@ -648,7 +725,9 @@ def main(): return root = Path(args.plan).resolve() plan = load_plan(root) - if args.action == "repair-router": + if args.action == 'update-permissions': + update_permissions(root,plan,args.profiles,args.apply) + elif args.action == "repair-router": repair_router(root,plan) elif args.action == "state": deployment_state(root,plan) diff --git a/deployable/tests/test_broker_deploy.py b/deployable/tests/test_broker_deploy.py index e5f4e90..abe4a68 100644 --- a/deployable/tests/test_broker_deploy.py +++ b/deployable/tests/test_broker_deploy.py @@ -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)