40 lines
2.0 KiB
Python
40 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Prepare a private callback identity file without printing the token."""
|
|
import argparse
|
|
import getpass
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser()
|
|
parser.add_argument('--output',required=True)
|
|
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():
|
|
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}}'],
|
|
stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)
|
|
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=env.get('HN_FEISHU_TENANT_KEY')or input('请输入本测试应用所属租户 tenant_key(不是 App ID):').strip()
|
|
token=env.get('HN_FEISHU_VERIFICATION_TOKEN')or getpass.getpass('请输入应用“事件与回调”中的 Verification Token(输入不显示):')
|
|
if (not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',tenant)or not 8<=len(token)<=512
|
|
or any(ord(c)<33 or ord(c)>126 for c in token)):
|
|
raise ValueError('invalid callback identity; no values displayed')
|
|
fd=os.open(str(path),os.O_WRONLY|os.O_CREAT|os.O_EXCL,0o600)
|
|
with os.fdopen(fd,'w',encoding='utf-8')as stream:
|
|
json.dump({'tenant_key':tenant,'verification_token':token},stream)
|
|
stream.write('\n')
|
|
print('Callback identity prepared privately; no services changed. Token must match the platform application.')
|
|
|
|
|
|
if __name__=='__main__':
|
|
try:main()
|
|
except (ValueError,OSError,EOFError,KeyboardInterrupt):
|
|
raise SystemExit('STOP: callback identity preparation incomplete; values withheld')from None
|