Feat/approval foundation local 20261117 #2
@@ -67,3 +67,11 @@ sudo python3 deployable/scripts/broker_deploy.py state \
|
||||
身份JSON、plan.json、compose.broker.json含私有信息,只保存在ECS受限目录,勿发聊天或提交Git。tenant_key由脚本自动核实;Verification Token确实缺失时才由本人在平台本地核对,不需要把账号密码交给助手。真实PostgreSQL/SDK回调/手机UI尚待本次环境验收,不能凭本地测试数宣称上线。
|
||||
|
||||
2026-11-17现场修正:c80c6a0的旧准备脚本曾要求用户输入tenant_key,用户未知该运维字段而中止,未生成候选/切换服务。修复后移除tenant输入;12项自动发现测试通过,覆盖只读加密SQL、有效授权用户匹配、过期/403/重定向/缺字段拒绝、应用/环境匹配,以及不输出身份正文或令牌。私有化user_info文档已确认tenant_key字段:[获取用户信息](https://open.w.cnooc.com.cn/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/user_info/get)。测试不代表现场已成功取得tenant_key。
|
||||
|
||||
现场3132c07自动发现返回通用失败,尚无证据判定是授权过期或平台网络。新增--diagnose只读入口,不要求--output、不写身份文件、不询问Verification Token、不重启容器。失败仅输出TENANT_DIAGNOSTIC的固定stage、计数、HTTP/平台数字错误码及白名单异常类;不输出数据库地址、账号、token、异常正文或平台用户信息。15项针对性测试通过。
|
||||
|
||||
```bash
|
||||
sudo python3 deployable/scripts/approval_identity.py --diagnose
|
||||
```
|
||||
|
||||
阶段为container_execution说明容器内探测未完成;credentials_query/database_connect指数据库读取阶段;matched_bindings为0表示没有匹配当前应用/环境的未撤销绑定授权;expired_access表示已有access过期但没有尝试refresh;platform_rejected/http_status/platform_code表示平台拒绝;missing_tenant/user_mismatch分别是返回缺字段/用户身份不匹配。只有拿到诊断事实后才决定修复,不因通用失败要求用户解绑、重建身份或猜填租户。
|
||||
|
||||
@@ -19,6 +19,9 @@ from relay.config import Settings
|
||||
from relay.credential_store import CredentialStore,CredentialKeyring
|
||||
from relay.store import Postgres
|
||||
|
||||
diag={'stage':'settings','matched_bindings':0,'expired_access':0,'credential_errors':0,
|
||||
'user_mismatch':0,'missing_tenant':0,'platform_rejected':0,'network_errors':0}
|
||||
|
||||
def discover():
|
||||
s=Settings(role='connector')
|
||||
target=urlsplit(s.feishu_api_base)
|
||||
@@ -27,6 +30,7 @@ def discover():
|
||||
raise ValueError()
|
||||
target.port
|
||||
if s.credential_mode!='broker' or not s.credential_keyring_file:raise ValueError()
|
||||
diag['stage']='database_connect'
|
||||
if s.database_url:
|
||||
db=Postgres(s.database_url)
|
||||
db.execute('SET default_transaction_read_only TO on')
|
||||
@@ -35,48 +39,96 @@ def discover():
|
||||
db=sqlite3.connect((s.data_dir/'metadata.sqlite').resolve().as_uri()+'?mode=ro',uri=True,timeout=2)
|
||||
db.row_factory=sqlite3.Row
|
||||
try:
|
||||
diag['stage']='credentials_query'
|
||||
rows=db.execute('''SELECT c.principal_id FROM work_credentials_v2 c
|
||||
JOIN devices d ON d.owner=c.principal_id
|
||||
WHERE c.environment=? AND c.app_id=? AND d.revoked=0
|
||||
ORDER BY c.updated_at DESC LIMIT 3''',(s.environment,s.feishu_app_id)).fetchall()
|
||||
diag['matched_bindings']=len(rows)
|
||||
# Reuse the existing getter WITHOUT running its schema constructor.
|
||||
diag['stage']='keyring'
|
||||
credentials=CredentialStore.__new__(CredentialStore)
|
||||
credentials.db=db;credentials.keyring=CredentialKeyring.from_file(s.credential_keyring_file)
|
||||
with httpx.Client(base_url=s.feishu_api_base.rstrip('/'),trust_env=False,
|
||||
follow_redirects=False,timeout=httpx.Timeout(3,connect=2))as client:
|
||||
for row in rows:
|
||||
try:
|
||||
diag['stage']='credential_read'
|
||||
value=credentials.get(s.environment,s.feishu_app_id,row['principal_id'])
|
||||
if not value or value.access_expires_at<=time.time()+10:continue
|
||||
if not value or value.access_expires_at<=time.time()+10:
|
||||
diag['expired_access']+=1
|
||||
continue
|
||||
diag['stage']='user_info'
|
||||
with client.stream('GET','/open-apis/authen/v1/user_info',
|
||||
headers={'Authorization':'Bearer '+value.access_token})as response:
|
||||
if response.status_code!=200:continue
|
||||
diag['http_status']=response.status_code
|
||||
if response.status_code!=200:
|
||||
diag['platform_rejected']+=1
|
||||
continue
|
||||
body=bytearray()
|
||||
for chunk in response.iter_bytes():
|
||||
body.extend(chunk)
|
||||
if len(body)>65536:raise ValueError()
|
||||
data=json.loads(body)
|
||||
info=data.get('data',{})
|
||||
if data.get('code')!=0 or not isinstance(info,dict):continue
|
||||
if info.get('open_id')!=row['principal_id']:continue
|
||||
if type(data.get('code'))is int:diag['platform_code']=data['code']
|
||||
if data.get('code')!=0 or not isinstance(info,dict):
|
||||
diag['platform_rejected']+=1
|
||||
continue
|
||||
if info.get('open_id')!=row['principal_id']:
|
||||
diag['user_mismatch']+=1
|
||||
continue
|
||||
tenant=info.get('tenant_key')
|
||||
if not isinstance(tenant,str)or not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',tenant):continue
|
||||
if not isinstance(tenant,str)or not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',tenant):
|
||||
diag['missing_tenant']+=1
|
||||
continue
|
||||
return {'status':'verified','tenant_key':tenant,'app_id':s.feishu_app_id,'environment':s.environment}
|
||||
except Exception:continue
|
||||
except httpx.HTTPError as error:
|
||||
diag['network_errors']+=1
|
||||
diag['error_class']=type(error).__name__
|
||||
continue
|
||||
except Exception as error:
|
||||
diag['credential_errors']+=1
|
||||
diag['error_class']=type(error).__name__
|
||||
continue
|
||||
raise ValueError()
|
||||
finally:db.close()
|
||||
|
||||
try:print(json.dumps(discover()))
|
||||
except Exception:print(json.dumps({'status':'tenant_discovery_unavailable'}))
|
||||
except Exception as error:
|
||||
if not any(diag.get(k)for k in ('expired_access','credential_errors','user_mismatch','missing_tenant','platform_rejected','network_errors')):
|
||||
diag['error_class']=type(error).__name__
|
||||
print(json.dumps({'status':'tenant_discovery_unavailable','diagnostic':diag}))
|
||||
"""
|
||||
|
||||
|
||||
def safe_diagnostic(value):
|
||||
if not isinstance(value,dict):return {}
|
||||
result={}
|
||||
if value.get('stage')in {'settings','database_connect','credentials_query','keyring','credential_read','user_info'}:
|
||||
result['stage']=value['stage']
|
||||
if value.get('error_class')in {'ValidationError','OperationalError','ProgrammingError','UndefinedTable',
|
||||
'UndefinedColumn','CredentialError','PermissionError','FileNotFoundError','ReadOnlySqlTransaction',
|
||||
'ConnectError','ConnectTimeout','ReadTimeout','SSLError','ValueError','TypeError'}:
|
||||
result['error_class']=value['error_class']
|
||||
for key in ('matched_bindings','expired_access','credential_errors','user_mismatch','missing_tenant',
|
||||
'platform_rejected','network_errors','http_status','platform_code'):
|
||||
number=value.get(key)
|
||||
if type(number)is int and 0<=number<=2147483647:result[key]=number
|
||||
return result
|
||||
|
||||
|
||||
def discover_tenant(container,env):
|
||||
result=subprocess.run(['docker','exec','-i',container,'python','-'],input=DISCOVERY_CODE,
|
||||
stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True,timeout=25)
|
||||
try:
|
||||
if result.returncode or len(result.stdout)>4096:raise ValueError()
|
||||
if result.returncode or len(result.stdout)>4096:
|
||||
print('TENANT_DIAGNOSTIC '+json.dumps({'stage':'container_execution','exit_code':result.returncode}))
|
||||
raise ValueError()
|
||||
value=json.loads(result.stdout)
|
||||
if isinstance(value,dict)and value.get('status')=='tenant_discovery_unavailable':
|
||||
print('TENANT_DIAGNOSTIC '+json.dumps(safe_diagnostic(value.get('diagnostic'))))
|
||||
raise ValueError()
|
||||
if (set(value)!={'status','tenant_key','app_id','environment'}or value['status']!='verified'
|
||||
or value['app_id']!=env.get('HN_FEISHU_APP_ID')
|
||||
or value['environment']!=env.get('HN_ENVIRONMENT','production')
|
||||
@@ -90,10 +142,11 @@ def discover_tenant(container,env):
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser()
|
||||
parser.add_argument('--output',required=True)
|
||||
parser.add_argument('--output')
|
||||
parser.add_argument('--diagnose',action='store_true',help='Read-only discovery check; no output file or token prompt.')
|
||||
parser.add_argument('--container',default='hainuo-relay-connector-1')
|
||||
args=parser.parse_args();path=Path(args.output)
|
||||
if not path.is_absolute()or path.exists()or path.is_symlink():
|
||||
args=parser.parse_args();path=Path(args.output)if args.output else None
|
||||
if not args.diagnose and(path is None or not path.is_absolute()or path.exists()or path.is_symlink()):
|
||||
raise ValueError('output must be a new absolute file')
|
||||
# Keep existing fields in process; never print docker Env or token values.
|
||||
result=subprocess.run(['docker','inspect',args.container,'--format','{{json .Config.Env}}'],
|
||||
@@ -101,6 +154,10 @@ def main():
|
||||
if result.returncode:raise ValueError('cannot inspect current connector')
|
||||
env=dict(item.split('=',1)for item in json.loads(result.stdout)if '='in item)
|
||||
tenant=discover_tenant(args.container,env)
|
||||
if args.diagnose:
|
||||
print('TENANT_DIAGNOSTIC '+json.dumps({'status':'verified',
|
||||
'verification_token_configured':bool(env.get('HN_FEISHU_VERIFICATION_TOKEN'))}))
|
||||
return
|
||||
configured=env.get('HN_FEISHU_TENANT_KEY')
|
||||
if configured and configured!=tenant:
|
||||
raise ValueError('configured tenant does not match verified platform identity')
|
||||
|
||||
@@ -41,7 +41,13 @@ async def test_in_container_lookup_is_readonly_and_never_refreshes(env,monkeypat
|
||||
output=capsys.readouterr().out;result=json.loads(output)
|
||||
if failure is None:
|
||||
assert result=={'status':'verified','tenant_key':'tenant_verified','app_id':'cli_test','environment':'test'}
|
||||
else:assert result=={'status':'tenant_discovery_unavailable'}
|
||||
else:
|
||||
assert result['status']=='tenant_discovery_unavailable'
|
||||
diagnostic=result['diagnostic']
|
||||
if failure=='expired':assert diagnostic['expired_access']==1
|
||||
elif failure=='wrong_user':assert diagnostic['user_mismatch']==1
|
||||
elif failure=='missing_tenant':assert diagnostic['missing_tenant']==1
|
||||
else:assert diagnostic['platform_rejected']==1
|
||||
assert len(calls)==(0 if failure=='expired'else 1)
|
||||
assert '\n'.join(env['store'].db.iterdump())==before
|
||||
assert env['platform'].refreshes==0
|
||||
@@ -72,3 +78,29 @@ def test_prepare_never_asks_user_for_tenant_or_prints_verification_token(tmp_pat
|
||||
identity.main()
|
||||
assert json.loads(output.read_text())=={'tenant_key':'tenant_verified','verification_token':'synthetic-private-verify'}
|
||||
assert 'synthetic-private-verify'not in capsys.readouterr().out
|
||||
|
||||
|
||||
def test_diagnostic_whitelist_drops_secrets_and_untrusted_strings():
|
||||
assert identity.safe_diagnostic({'stage':'user_info','http_status':403,'platform_code':99991679,
|
||||
'email':'private@example.test','access_token':'private-secret','error_class':'private-secret',
|
||||
'matched_bindings':True,'expired_access':-1})=={'stage':'user_info','http_status':403,'platform_code':99991679}
|
||||
|
||||
|
||||
def test_diagnose_does_not_prompt_or_write(tmp_path,monkeypatch,capsys):
|
||||
monkeypatch.setattr(sys,'argv',['approval_identity.py','--diagnose'])
|
||||
monkeypatch.setattr(identity.subprocess,'run',lambda *a,**kw:SimpleNamespace(returncode=0,stdout='[]'))
|
||||
monkeypatch.setattr(identity,'discover_tenant',lambda *a:'tenant_verified')
|
||||
def forbidden(*a):raise AssertionError('Diagnostic must not prompt')
|
||||
monkeypatch.setattr(identity.getpass,'getpass',forbidden)
|
||||
identity.main()
|
||||
assert '"status": "verified"'in capsys.readouterr().out
|
||||
assert not list(tmp_path.iterdir())
|
||||
|
||||
|
||||
def test_failed_discovery_reports_only_safe_stage_counts(monkeypatch,capsys):
|
||||
body={'status':'tenant_discovery_unavailable','diagnostic':{'stage':'user_info',
|
||||
'expired_access':2,'network_errors':0,'access_token':'do-not-print','name':'do-not-print'}}
|
||||
monkeypatch.setattr(identity.subprocess,'run',lambda *a,**kw:SimpleNamespace(returncode=0,stdout=json.dumps(body)))
|
||||
with pytest.raises(ValueError):identity.discover_tenant('connector',{})
|
||||
output=capsys.readouterr().out
|
||||
assert '"expired_access": 2'in output and 'do-not-print'not in output
|
||||
|
||||
Reference in New Issue
Block a user