Skip to content

fix(installer): provision project credential key during upgrades - #1316

Merged
crispinhu27 merged 1 commit into
mainfrom
feature/project-credential-key-upgrade
Nov 25, 2026
Merged

crispinhu27 merged 1 commit into
mainfrom
feature/project-credential-key-upgrade

Conversation

@crispinhu27

Copy link
Copy Markdown
Collaborator

What changed

#1295 introduced the encryption key for project credentials. An existing Compose installation upgraded with ./start.sh pull could start without a usable key and return HTTP 503 when saving a project credential.

  • Initialize AGENTFLOW_PROJECT_CREDENTIAL_KEY under the Compose upgrade lock before starting the platform. Fill a missing or blank setting in .env and reuse a configured key on subsequent upgrades.
  • Discard an empty exported override so Compose can load the persisted key. Reuse a nonempty supplied key when configuring an installation for the first time.

Scope

  • Public adapter / deployment
  • Installer / CLI

Compatibility

The official Compose install and upgrade paths share the key initializer. Custom deployments supply a stable key to their platform processes through their own configuration.

Validation

  • python3 scripts/test-install.py -q in Linux passed 68 tests. The two Bash 3.2 tests skipped there passed on macOS.
  • Upgraded a disposable v0.4.2 Compose deployment with real MySQL. Verified the generated key reached the platform despite an empty exported override. Created a project credential, recreated the platform on a second upgrade, and verified the stored credential could still be decrypted.
  • bash -n start.sh, bash -n scripts/configure.sh, git diff --check, and python3 scripts/check-public.py passed.

Safety

  • Only synthetic test credentials were used in a disposable deployment.
  • The existing key and unrelated deployment settings are preserved.

@Robert0622 Robert0622 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

评审 dd0f3b7(基于 main 7bebfa5)。代码看过,也在部署环境里实测了升级路径,没有发现阻塞问题,通过。

代码

  • start.sh pull:configure.sh --project-credential-key 放在 lock_compose_upgrade 之后、compose pull/up 之前执行,补齐密钥和启动平台都在同一把升级锁里。之后导出的空值会被 unset,Compose 能从 .env 读到密钥。
  • configure.sh:--project-credential-key 模式只处理密钥,不会顺带补 LOOPFORGE_DATA_DIR、AGENTFLOW_CREDENTIAL_KEYS。按最后一条匹配判断,这和 dotenv 后写覆盖前写的规则一致。""、'' 和行尾 # 注释 都算空值,注释会保留;非空值不改。.env 不存在时直接退出并提示先安装。
  • save_existing_env:先写同目录临时文件,再 os.replace 替换。文件权限沿用原来的;.env 是符号链接时写到链接目标,链接本身保留;按 newline='' 读写,CRLF 不会被改掉。

部署实测(本机部署:MySQL 8.4.7 全新库,平台用本 PR 代码构建。容器里没有 Docker,所以 docker 换成了替身:环境变量插值交给真实的 docker-compose v2.40.3 config 计算,up 时用算出来的 platform 环境启动真实平台进程,exec mysql 指向本机 MySQL。凭据都是合成的。)

  1. 在 main 上复现问题:准备一份 #1295 之前的 .env(没有密钥),执行 ./start.sh pull。退出码为 0,但 .env 里没有密钥,平台进程拿到的 AGENTFLOW_PROJECT_CREDENTIAL_KEY 为空,新建项目凭据返回 503「请为平台配置 AGENTFLOW_PROJECT_CREDENTIAL_KEY」。
  2. 用本 PR 升级同一套部署,同时导出空的 AGENTFLOW_PROJECT_CREDENTIAL_KEY=:输出 Configured AGENTFLOW_PROJECT_CREDENTIAL_KEY in existing .env。新增的值能解码出 32 字节,.env 只多了这一行,权限仍是 600。平台进程里的密钥和 .env 一致,新建项目凭据返回 201。
  3. 再升级两次(不导出这个变量):都提示 Keeping existing .env,.env 的 sha256 没有变化,平台进程重建。用不带 value 的 PUT 改名(需要解密原来的密文),返回 200。对照组:把 .env 换成另一把合法密钥再执行 pull,脚本保留了这把换上的密钥,同样的 PUT 返回 400「无法解密项目凭据」。换回原密钥后再执行 pull,又能正常解密。由此可以确认:前面的 200 确实是解密成功,已有的非空密钥也不会被覆盖。
  4. 边界情况:
    • CRLF 文件、export AGENTFLOW_PROJECT_CREDENTIAL_KEY="" # project key、权限 640:补上了密钥,CRLF、export 前缀、注释和权限都保留,平台进程里的密钥与 .env 一致。
    • .env 里没有密钥、外部导出了非空密钥:这把密钥原样写进 .env,平台用的就是它。下一次 pull 不导出变量,平台仍然用同一把密钥。
    • .env 是符号链接:写入了链接目标,链接保留。

测试

  • python3 scripts/test-install.py -q:本 PR 70 个测试通过(跳过 2 个,都是 Bash 3.2 用例),main 69 个通过。新增的 test_start_pull_configures_project_credential_key_once 拿到 main 的脚本上跑,missing、empty、quoted empty、provided 四个子用例失败,说明这个测试能抓住这个问题。
  • bash -n start.sh、bash -n scripts/configure.sh、git diff --check、python3 scripts/check-public.py 通过。CI 全绿。

P3(不阻塞):外部导出空值的情况只在 pull 里处理了。用 docker-compose 验证过:导出 AGENTFLOW_PROJECT_CREDENTIAL_KEY= 后,即使 .env 里有密钥,config 算出来的值仍为空。所以带着这样的空导出执行 ./start.sh、dev 或 release(都走 install.sh)重建平台,还是会拿到空密钥。可以考虑把这行 unset 挪到 compose() 或 install.sh 里,这样所有入口都能覆盖到。

@crispinhu27
crispinhu27 merged commit 2ad5659 into main Nov 25, 2026
16 checks passed
@crispinhu27
crispinhu27 deleted the feature/project-credential-key-upgrade branch November 25, 2026 02:26
Robert0622 pushed a commit that referenced this pull request Nov 25, 2026
Bring in #1316, #1319 and #1318. Conflicts in orchestration and
workspace docs, run admission, the MCP run tool, the LOOP designer
prompt, catalog and sharing comments, the Web flow, issue and run
views, index.html and the sandbox selection Web test are resolved by
taking main's behaviour and applying this branch's LOOP terminology.

Rename the user-visible SOP copy that #1319 added outside the
conflicts (OpenAPI, run admission errors, the runtime sandbox picker
and its assistant prompt) with the same rules.

Co-Authored-By: Claude Code <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants