native_openviking_config.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. """Redacted native configuration, draft probes and reversible .env updates.
  2. Connection values remain Hermes-native. No server is installed or started here.
  3. """
  4. import hashlib
  5. import base64
  6. import json
  7. import os
  8. import re
  9. import tempfile
  10. import uuid
  11. from pathlib import Path
  12. from urllib.parse import urlsplit
  13. from plugins.memory.openviking import (
  14. _VikingClient, _load_hermes_openviking_config, _profile_openviking_env,
  15. _resolve_connection_settings, _probe_openviking_identity, _write_env_vars,
  16. _ovcli_values_for,
  17. )
  18. class ConfigurationError(RuntimeError):
  19. def __init__(self, detail, status=400):
  20. super().__init__(detail)
  21. self.status = status
  22. def _revision(home):
  23. files = [Path(home) / '.env', Path(home) / 'config.yaml']
  24. env = _profile_openviking_env(str(home)) or {}
  25. config = _load_hermes_openviking_config(str(home), env=env)
  26. try: linked = _ovcli_values_for(config, env=env)
  27. except Exception: linked = {}
  28. parts = [p.read_bytes() if p.exists() else b'<missing>' for p in files]
  29. parts.append(json.dumps(linked, sort_keys=True).encode())
  30. return hashlib.sha256(b'\0'.join(parts)).hexdigest()
  31. def _resolved(home):
  32. env = _profile_openviking_env(str(home)) or {}
  33. config = _load_hermes_openviking_config(str(home), env=env)
  34. try:
  35. settings = _resolve_connection_settings(config, env=env, hermes_home=str(home))
  36. except Exception:
  37. # A malformed saved endpoint must still be editable while offline.
  38. try: linked = _ovcli_values_for(config, env=env)
  39. except Exception: linked = {}
  40. settings = {k: env.get('OPENVIKING_' + k.upper(), linked.get(k) or config.get(k) or '') for k in ('endpoint', 'api_key', 'account', 'user')}
  41. settings['api_key'] = env.get('OPENVIKING_API_KEY', linked.get('api_key') or '')
  42. settings['endpoint'] = settings['endpoint'] or 'http://127.0.0.1:1933'
  43. settings = {**settings, 'agent': ''}
  44. source = 'managed' if config.get('deployment') == 'quick_local' else 'profile' if any(k in env for k in ('OPENVIKING_ENDPOINT', 'OPENVIKING_API_KEY', 'OPENVIKING_ACCOUNT', 'OPENVIKING_USER')) else 'linked' if config.get('use_ovcli_config') else 'profile' if any(config.get(k) for k in ('endpoint', 'account', 'user')) else 'default'
  45. # Use the upstream constant too, so a renamed deployment cannot become editable.
  46. from plugins.memory.openviking import quick_local
  47. if config.get('deployment') == quick_local.DEPLOYMENT:
  48. source = 'managed'
  49. return settings, config, source
  50. def read_configuration(home):
  51. settings, _, source = _resolved(home)
  52. return {'endpoint': settings['endpoint'], 'account': settings.get('account') or 'default',
  53. 'user': settings.get('user') or 'default', 'keyConfigured': bool(settings.get('api_key')),
  54. 'source': source, 'readOnly': source == 'managed', 'revision': _revision(home), 'configHome': str(home)}
  55. def candidate(home, values=None):
  56. settings, config, source = _resolved(home)
  57. if values is not None:
  58. if not isinstance(values, dict) or any(k not in ('endpoint', 'account', 'user', 'apiKey', 'clearApiKey', 'revision') for k in values):
  59. raise ConfigurationError('原生连接配置字段无效')
  60. if source == 'managed':
  61. raise ConfigurationError('Quick Local 连接由 Hermes 管理,请在 Hermes 中修改', 409)
  62. if values.get('revision') is not None and values['revision'] != _revision(home):
  63. raise ConfigurationError('Hermes 配置已更新,请重新读取后保存', 409)
  64. for key in ('endpoint', 'account', 'user'):
  65. if key in values:
  66. if not isinstance(values[key], str):
  67. raise ConfigurationError('原生连接字段必须是文本')
  68. settings[key] = values[key].strip()
  69. if 'clearApiKey' in values and not isinstance(values['clearApiKey'], bool):
  70. raise ConfigurationError('清除密钥选项无效')
  71. if 'apiKey' in values and not isinstance(values['apiKey'], str):
  72. raise ConfigurationError('API Key 必须是文本')
  73. key = values.get('apiKey', '').strip()
  74. if key and values.get('clearApiKey'):
  75. raise ConfigurationError('不能同时填写和清除 API Key')
  76. if key or values.get('clearApiKey'):
  77. settings['api_key'] = key
  78. endpoint = settings.get('endpoint', '').strip()
  79. try:
  80. parsed = urlsplit(endpoint)
  81. valid = parsed.scheme in ('http', 'https') and parsed.hostname and not parsed.username and not parsed.password and not parsed.query and not parsed.fragment
  82. if not valid or any(c.isspace() for c in endpoint):
  83. raise ValueError()
  84. parsed.port
  85. except ValueError:
  86. raise ConfigurationError('请填写有效的 HTTP / HTTPS 服务地址,不要包含凭据、查询参数或片段') from None
  87. settings['endpoint'] = endpoint.rstrip('/')
  88. for key in ('account', 'user'):
  89. settings[key] = settings.get(key) or 'default'
  90. if not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9_.@-]{0,127}', settings[key]):
  91. raise ConfigurationError('Account 和 User 仅支持 1–128 位字母、数字及 _ . @ -')
  92. secret = settings.get('api_key') or ''
  93. if len(secret) > 4096 or any(ord(c) < 32 or ord(c) == 127 for c in secret):
  94. raise ConfigurationError('API Key 格式无效,请使用单行密钥')
  95. return {'settings': settings, 'providerConfig': config}
  96. def _error_detail(error, stage):
  97. status = getattr(error, 'status_code', None) or getattr(getattr(error, 'response', None), 'status_code', None)
  98. if isinstance(error, ConfigurationError):
  99. return str(error)
  100. if status in (401, 403):
  101. return f'凭据无效或无访问权限(HTTP {status}),请检查 API Key 和身份设置'
  102. if status:
  103. return f'{stage}返回 HTTP {status},请检查服务地址、访问代理与服务版本'
  104. if 'timeout' in type(error).__name__.lower():
  105. return f'{stage}超时,请检查网络与服务地址'
  106. if isinstance(error, (ConnectionError, OSError)) or 'connect' in type(error).__name__.lower():
  107. return f'无法连接服务,请检查地址、端口和网络({stage})'
  108. return f'{stage}失败,请检查原生配置;服务端原始错误已隐藏以保护凭据'
  109. def probe(value):
  110. steps = []
  111. stage, name = '服务健康检查', 'health'
  112. try:
  113. s = value['settings']
  114. client = _VikingClient(s['endpoint'], s.get('api_key', ''), account=s['account'], user=s['user'], agent='', extra_headers=s.get('extra_headers'))
  115. kind, health = _probe_openviking_identity(client)
  116. if kind != 'modern':
  117. raise ConfigurationError('服务未返回健康的 OpenViking 标识,请检查服务地址与版本', 422)
  118. steps.append({'name': name, 'ok': True, 'detail': '已连接 OpenViking 服务'})
  119. stage, name = '服务版本检查', 'version'
  120. version = re.match(r'^v?(\d+)\.(\d+)\.(\d+)', str(health.get('version', '')))
  121. if not version or tuple(map(int, version.groups())) < (0, 4, 17):
  122. found = '.'.join(version.groups()) if version else '未知'
  123. raise ConfigurationError(f'peer 模式需要 OpenViking 0.4.17 或更新服务,当前版本:{found}', 422)
  124. steps.append({'name': name, 'ok': True, 'detail': '当前版本:' + '.'.join(version.groups())})
  125. stage, name = '凭据与身份检查', 'identity'
  126. status = client.get('/api/v1/system/status', timeout=5).get('result', {})
  127. user = str(status.get('user') or '').strip()
  128. if not user:
  129. raise ConfigurationError('服务未确认当前 user,请检查 API Key 或本地 / root key 身份设置', 422)
  130. steps.append({'name': name, 'ok': True, 'detail': f'当前 user:{user}'})
  131. stage, name = 'peer 命名空间检查', 'peer'
  132. try:
  133. client.get('/api/v1/fs/ls', params={'uri': f'viking://user/{user}/peers'}, timeout=5)
  134. except Exception as error:
  135. if getattr(error, 'status_code', None) != 404:
  136. raise
  137. steps.append({'name': name, 'ok': True, 'detail': 'peer 路径可用;空空间尚未创建时无需回填'})
  138. return {'ok': True, 'steps': steps, 'identity': {'user': user, 'account': str(status.get('account') or s['account'])}}
  139. except Exception as error:
  140. steps.append({'name': name, 'ok': False, 'detail': _error_detail(error, stage)})
  141. return {'ok': False, 'steps': steps}
  142. def test_configuration(home, values=None):
  143. try:
  144. return probe(candidate(home, values))
  145. except Exception as error:
  146. return {'ok': False, 'steps': [{'name': 'native', 'ok': False, 'detail': _error_detail(error, '原生配置读取')}]}
  147. def _atomic_write(path, content):
  148. descriptor, staged = tempfile.mkstemp(prefix='.openviking-', dir=path.parent)
  149. try:
  150. with os.fdopen(descriptor, 'wb') as stream:
  151. stream.write(content); stream.flush(); os.fsync(stream.fileno())
  152. os.chmod(staged, 0o600)
  153. os.replace(staged, path)
  154. finally:
  155. if os.path.exists(staged): os.unlink(staged)
  156. _transactions = {}
  157. def _journal(state, token):
  158. folder = state['home'] / '.openviking-config-transactions'
  159. folder.mkdir(mode=0o700, exist_ok=True)
  160. os.chmod(folder, 0o700)
  161. return folder / (token + '.json')
  162. def _save_transaction(state, token):
  163. data = {k: state[k] for k in ('revision', 'committed')}
  164. data['before'] = None if state['before'] is None else base64.b64encode(state['before']).decode()
  165. data['after'] = base64.b64encode(state['after']).decode()
  166. _atomic_write(_journal(state, token), json.dumps(data).encode())
  167. def prepare(home, values):
  168. home = Path(home)
  169. if not isinstance(values, dict): raise ConfigurationError('原生连接配置必须是对象')
  170. initial_revision = _revision(home)
  171. value = candidate(home, values)
  172. result = probe(value)
  173. if not result['ok']:
  174. raise ConfigurationError(result['steps'][-1]['detail'], 422)
  175. path = home / '.env'
  176. before = path.read_bytes() if path.exists() else None
  177. revision = _revision(home)
  178. if initial_revision != revision or values.get('revision') is not None and values['revision'] != revision:
  179. raise ConfigurationError('Hermes 配置已更新,请重新读取后保存', 409)
  180. home.mkdir(parents=True, exist_ok=True, mode=0o700)
  181. fd, temp = tempfile.mkstemp(prefix='.openviking-draft-', dir=home)
  182. os.close(fd)
  183. try:
  184. if before is not None: Path(temp).write_bytes(before)
  185. s = value['settings']
  186. writes = {'OPENVIKING_ENDPOINT': s['endpoint'], 'OPENVIKING_ACCOUNT': s['account'], 'OPENVIKING_USER': s['user']}
  187. if values.get('apiKey', '').strip() or values.get('clearApiKey'):
  188. writes['OPENVIKING_API_KEY'] = s.get('api_key') or ''
  189. _write_env_vars(Path(temp), writes)
  190. after = Path(temp).read_bytes()
  191. finally:
  192. os.unlink(temp)
  193. token = uuid.uuid4().hex
  194. _transactions[token] = {'home': home, 'before': before, 'after': after, 'revision': revision, 'committed': False}
  195. _save_transaction(_transactions[token], token)
  196. return {'token': token, 'test': result}
  197. def transaction(token, action, home=None):
  198. if action not in ('commit', 'rollback', 'finish', 'retain'):
  199. raise ConfigurationError('配置保存事务操作无效')
  200. if not isinstance(token, str) or not re.fullmatch(r'[0-9a-f]{32}', token):
  201. raise ConfigurationError('配置保存事务标识无效', 409)
  202. state = _transactions.get(token)
  203. if state is None and home is not None:
  204. journal = Path(home) / '.openviking-config-transactions' / (token + '.json')
  205. if journal.exists():
  206. data = json.loads(journal.read_bytes())
  207. state = {**data, 'home':Path(home), 'before':None if data['before'] is None else base64.b64decode(data['before']), 'after':base64.b64decode(data['after'])}
  208. _transactions[token] = state
  209. if state is None: raise ConfigurationError('配置保存事务已失效,请重试', 409)
  210. path = state['home'] / '.env'
  211. if action == 'commit':
  212. if _revision(state['home']) != state['revision']:
  213. raise ConfigurationError('Hermes 配置已更新,请重新读取后保存', 409)
  214. _atomic_write(path, state['after']); state['committed'] = True
  215. _save_transaction(state, token)
  216. return read_configuration(state['home'])
  217. if action == 'rollback' and (state['committed'] or path.exists() and path.read_bytes() == state['after']):
  218. if not path.exists() or path.read_bytes() != state['after']:
  219. raise ConfigurationError('Hermes 配置已被其他操作修改,未覆盖其内容', 409)
  220. if state['before'] is None: path.unlink()
  221. else: _atomic_write(path, state['before'])
  222. state['committed'] = False
  223. _save_transaction(state, token)
  224. if action == 'finish':
  225. _journal(state, token).unlink(missing_ok=True)
  226. _transactions.pop(token, None)
  227. return {'ok': True}