Compare commits
30 Commits
secrets-mc
...
secrets-mc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6fb457734 | ||
| df701f21b9 | |||
| c3c536200e | |||
| 7909f7102d | |||
| 87a29af82d | |||
| 1b11f7e976 | |||
| 08e81363c9 | |||
|
|
beade4503d | ||
|
|
409fd78a35 | ||
|
|
f7afd7f819 | ||
|
|
719bdd7e08 | ||
|
|
1e597559a2 | ||
|
|
e3ca43ca3f | ||
|
|
0b57605103 | ||
|
|
8b191937cd | ||
|
|
11c936a5b8 | ||
|
|
b6349dd1c8 | ||
|
|
f720983328 | ||
|
|
7bd0603dc6 | ||
|
|
17a95bea5b | ||
|
|
a42db62702 | ||
|
|
2edb970cba | ||
|
|
17f8ac0dbc | ||
|
|
259fbe10a6 | ||
|
|
c815fb4cc8 | ||
|
|
90cd1eca15 | ||
|
|
da007348ea | ||
|
|
f2344b7543 | ||
|
|
ee028d45c3 | ||
|
|
a44c8ebf08 |
@@ -7,7 +7,6 @@ on:
|
|||||||
- 'crates/**'
|
- 'crates/**'
|
||||||
- 'Cargo.toml'
|
- 'Cargo.toml'
|
||||||
- 'Cargo.lock'
|
- 'Cargo.lock'
|
||||||
# systemd / 部署模板变更也应跑构建(产物无变时可快速跳过 check)
|
|
||||||
- 'deploy/**'
|
- 'deploy/**'
|
||||||
- '.gitea/workflows/**'
|
- '.gitea/workflows/**'
|
||||||
|
|
||||||
@@ -25,225 +24,162 @@ env:
|
|||||||
CARGO_NET_RETRY: 10
|
CARGO_NET_RETRY: 10
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
RUST_BACKTRACE: short
|
RUST_BACKTRACE: short
|
||||||
|
MUSL_TARGET: x86_64-unknown-linux-musl
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
version:
|
ci:
|
||||||
name: 版本 & Release
|
name: 检查 / 构建 / 发版
|
||||||
runs-on: debian
|
runs-on: debian
|
||||||
|
timeout-minutes: 40
|
||||||
outputs:
|
outputs:
|
||||||
version: ${{ steps.ver.outputs.version }}
|
|
||||||
tag: ${{ steps.ver.outputs.tag }}
|
tag: ${{ steps.ver.outputs.tag }}
|
||||||
tag_exists: ${{ steps.ver.outputs.tag_exists }}
|
version: ${{ steps.ver.outputs.version }}
|
||||||
release_id: ${{ steps.release.outputs.release_id }}
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
|
# ── 版本解析 ────────────────────────────────────────────────────────
|
||||||
- name: 解析版本
|
- name: 解析版本
|
||||||
id: ver
|
id: ver
|
||||||
run: |
|
run: |
|
||||||
version=$(grep -m1 '^version' crates/secrets-mcp/Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
|
version=$(grep -m1 '^version' crates/secrets-mcp/Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
|
||||||
tag="secrets-mcp-${version}"
|
tag="secrets-mcp-${version}"
|
||||||
previous_tag=$(git tag --list 'secrets-mcp-*' --sort=-v:refname | awk -v tag="$tag" '$0 != tag { print; exit }')
|
|
||||||
|
|
||||||
echo "version=${version}" >> "$GITHUB_OUTPUT"
|
echo "version=${version}" >> "$GITHUB_OUTPUT"
|
||||||
echo "tag=${tag}" >> "$GITHUB_OUTPUT"
|
echo "tag=${tag}" >> "$GITHUB_OUTPUT"
|
||||||
echo "previous_tag=${previous_tag}" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
if git rev-parse "refs/tags/${tag}" >/dev/null 2>&1; then
|
if git rev-parse "refs/tags/${tag}" >/dev/null 2>&1; then
|
||||||
|
echo "⚠ 版本 ${tag} 已存在,将覆盖重新发版。"
|
||||||
echo "tag_exists=true" >> "$GITHUB_OUTPUT"
|
echo "tag_exists=true" >> "$GITHUB_OUTPUT"
|
||||||
echo "版本 ${tag} 已存在"
|
|
||||||
else
|
else
|
||||||
echo "tag_exists=false" >> "$GITHUB_OUTPUT"
|
|
||||||
echo "将创建新版本 ${tag}"
|
echo "将创建新版本 ${tag}"
|
||||||
|
echo "tag_exists=false" >> "$GITHUB_OUTPUT"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: 严格拦截重复版本
|
# ── Rust 工具链 ──────────────────────────────────────────────────────
|
||||||
if: steps.ver.outputs.tag_exists == 'true'
|
- name: 安装 Rust 与 musl 工具链
|
||||||
run: |
|
run: |
|
||||||
echo "错误: 版本 ${{ steps.ver.outputs.tag }} 已存在,禁止重复发版。"
|
sudo apt-get update -qq
|
||||||
echo "请先 bump crates/secrets-mcp/Cargo.toml 中的 version,并执行 cargo build 同步 Cargo.lock。"
|
sudo apt-get install -y -qq pkg-config musl-tools binutils jq
|
||||||
exit 1
|
if ! command -v rustup >/dev/null 2>&1; then
|
||||||
|
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain "${RUST_TOOLCHAIN}"
|
||||||
|
echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"
|
||||||
|
fi
|
||||||
|
source "$HOME/.cargo/env" 2>/dev/null || true
|
||||||
|
rustup toolchain install "${RUST_TOOLCHAIN}" --profile minimal \
|
||||||
|
--component rustfmt --component clippy
|
||||||
|
rustup default "${RUST_TOOLCHAIN}"
|
||||||
|
rustup target add "${MUSL_TARGET}" --toolchain "${RUST_TOOLCHAIN}"
|
||||||
|
rustc -V && cargo -V
|
||||||
|
|
||||||
|
- name: 缓存 Cargo
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.cargo/registry/index
|
||||||
|
~/.cargo/registry/cache
|
||||||
|
~/.cargo/git/db
|
||||||
|
target
|
||||||
|
key: cargo-${{ env.MUSL_TARGET }}-${{ env.RUST_TOOLCHAIN }}-${{ hashFiles('Cargo.lock') }}
|
||||||
|
restore-keys: |
|
||||||
|
cargo-${{ env.MUSL_TARGET }}-${{ env.RUST_TOOLCHAIN }}-
|
||||||
|
cargo-${{ env.MUSL_TARGET }}-
|
||||||
|
|
||||||
|
# ── 质量检查(先于构建,失败即止)──────────────────────────────────
|
||||||
|
- name: fmt
|
||||||
|
run: cargo fmt -- --check
|
||||||
|
|
||||||
|
- name: clippy
|
||||||
|
run: cargo clippy --locked -- -D warnings
|
||||||
|
|
||||||
|
- name: test
|
||||||
|
run: cargo test --locked
|
||||||
|
|
||||||
|
# ── 构建(质量检查通过后才执行)────────────────────────────────────
|
||||||
|
- name: 构建 secrets-mcp (musl)
|
||||||
|
run: |
|
||||||
|
cargo build --release --locked --target "${MUSL_TARGET}" -p secrets-mcp
|
||||||
|
strip "target/${MUSL_TARGET}/release/${MCP_BINARY}"
|
||||||
|
|
||||||
|
- name: 上传构建产物
|
||||||
|
uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: ${{ env.MCP_BINARY }}-linux-musl
|
||||||
|
path: target/${{ env.MUSL_TARGET }}/release/${{ env.MCP_BINARY }}
|
||||||
|
retention-days: 3
|
||||||
|
|
||||||
|
# ── 创建 / 覆盖 Tag(构建成功后才打)───────────────────────────────
|
||||||
- name: 创建 Tag
|
- name: 创建 Tag
|
||||||
if: steps.ver.outputs.tag_exists == 'false'
|
|
||||||
run: |
|
run: |
|
||||||
git config user.name "github-actions[bot]"
|
git config user.name "github-actions[bot]"
|
||||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
git tag -a "${{ steps.ver.outputs.tag }}" -m "Release ${{ steps.ver.outputs.tag }}"
|
tag="${{ steps.ver.outputs.tag }}"
|
||||||
git push origin "${{ steps.ver.outputs.tag }}"
|
if [ "${{ steps.ver.outputs.tag_exists }}" = "true" ]; then
|
||||||
|
git tag -d "$tag" 2>/dev/null || true
|
||||||
|
git push origin ":refs/tags/$tag" 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
git tag -a "$tag" -m "Release $tag"
|
||||||
|
git push origin "$tag"
|
||||||
|
|
||||||
- name: 解析或创建 Release
|
# ── Release(可选,需配置 RELEASE_TOKEN)───────────────────────────
|
||||||
id: release
|
- name: Upsert Release
|
||||||
|
if: env.RELEASE_TOKEN != ''
|
||||||
env:
|
env:
|
||||||
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
||||||
run: |
|
run: |
|
||||||
if [ -z "$RELEASE_TOKEN" ]; then
|
|
||||||
echo "release_id=" >> "$GITHUB_OUTPUT"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
command -v jq >/dev/null 2>&1 || (sudo apt-get update -qq && sudo apt-get install -y -qq jq)
|
|
||||||
|
|
||||||
tag="${{ steps.ver.outputs.tag }}"
|
tag="${{ steps.ver.outputs.tag }}"
|
||||||
version="${{ steps.ver.outputs.version }}"
|
version="${{ steps.ver.outputs.version }}"
|
||||||
release_api="${{ github.server_url }}/api/v1/repos/${{ github.repository }}/releases"
|
api="${{ github.server_url }}/api/v1/repos/${{ github.repository }}/releases"
|
||||||
|
auth="Authorization: token $RELEASE_TOKEN"
|
||||||
|
|
||||||
http_code=$(curl -sS -o /tmp/release.json -w '%{http_code}' \
|
previous_tag=$(git tag --list 'secrets-mcp-*' --sort=-v:refname | awk -v t="$tag" '$0 != t { print; exit }')
|
||||||
-H "Authorization: token $RELEASE_TOKEN" \
|
|
||||||
"${release_api}/tags/${tag}")
|
|
||||||
|
|
||||||
if [ "$http_code" = "200" ]; then
|
|
||||||
release_id=$(jq -r '.id // empty' /tmp/release.json)
|
|
||||||
if [ -n "$release_id" ]; then
|
|
||||||
echo "已找到现有 Release: ${release_id}"
|
|
||||||
echo "release_id=${release_id}" >> "$GITHUB_OUTPUT"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
previous_tag="${{ steps.ver.outputs.previous_tag }}"
|
|
||||||
if [ -n "$previous_tag" ]; then
|
if [ -n "$previous_tag" ]; then
|
||||||
changes=$(git log --pretty=format:'- %s (%h)' "${previous_tag}..HEAD")
|
changes=$(git log --pretty=format:'- %s (%h)' "${previous_tag}..HEAD")
|
||||||
else
|
else
|
||||||
changes=$(git log --pretty=format:'- %s (%h)')
|
changes=$(git log --pretty=format:'- %s (%h)')
|
||||||
fi
|
fi
|
||||||
[ -z "$changes" ] && changes="- 首次发布"
|
[ -z "$changes" ] && changes="- 首次发布"
|
||||||
|
|
||||||
body=$(printf '## 变更日志\n\n%s' "$changes")
|
body=$(printf '## 变更日志\n\n%s' "$changes")
|
||||||
|
|
||||||
payload=$(jq -n \
|
# Upsert: 存在 → PATCH + 清旧 assets;不存在 → POST
|
||||||
--arg tag "$tag" \
|
release_id=$(curl -sS -H "$auth" "${api}/tags/${tag}" 2>/dev/null | jq -r '.id // empty')
|
||||||
--arg name "secrets-mcp ${version}" \
|
|
||||||
--arg body "$body" \
|
|
||||||
'{tag_name: $tag, name: $name, body: $body, draft: true}')
|
|
||||||
|
|
||||||
http_code=$(curl -sS -o /tmp/create-release.json -w '%{http_code}' \
|
|
||||||
-H "Authorization: token $RELEASE_TOKEN" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
-X POST "$release_api" \
|
|
||||||
-d "$payload")
|
|
||||||
|
|
||||||
if [ "$http_code" = "201" ] || [ "$http_code" = "200" ]; then
|
|
||||||
release_id=$(jq -r '.id // empty' /tmp/create-release.json)
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$release_id" ]; then
|
if [ -n "$release_id" ]; then
|
||||||
echo "已创建草稿 Release: ${release_id}"
|
curl -sS -o /dev/null -H "$auth" -H "Content-Type: application/json" \
|
||||||
echo "release_id=${release_id}" >> "$GITHUB_OUTPUT"
|
-X PATCH "${api}/${release_id}" \
|
||||||
|
-d "$(jq -n --arg n "secrets-mcp ${version}" --arg b "$body" '{name:$n,body:$b,draft:false}')"
|
||||||
|
curl -sS -H "$auth" "${api}/${release_id}/assets" | \
|
||||||
|
jq -r '.[].id' | xargs -I{} curl -sS -o /dev/null -H "$auth" -X DELETE "${api}/${release_id}/assets/{}"
|
||||||
|
echo "已更新 Release ${release_id}"
|
||||||
else
|
else
|
||||||
echo "⚠ 创建 Release 失败 (HTTP ${http_code}),跳过产物上传"
|
release_id=$(curl -fsS -H "$auth" -H "Content-Type: application/json" \
|
||||||
cat /tmp/create-release.json 2>/dev/null || true
|
-X POST "$api" \
|
||||||
echo "release_id=" >> "$GITHUB_OUTPUT"
|
-d "$(jq -n --arg t "$tag" --arg n "secrets-mcp ${version}" --arg b "$body" \
|
||||||
|
'{tag_name:$t,name:$n,body:$b,draft:false}')" | jq -r '.id')
|
||||||
|
echo "已创建 Release ${release_id}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
check:
|
bin="target/${MUSL_TARGET}/release/${MCP_BINARY}"
|
||||||
name: 质量检查 (fmt / clippy / test)
|
archive="${MCP_BINARY}-${tag}-x86_64-linux-musl.tar.gz"
|
||||||
needs: [version]
|
|
||||||
runs-on: debian
|
|
||||||
timeout-minutes: 15
|
|
||||||
steps:
|
|
||||||
- name: 安装 Rust
|
|
||||||
run: |
|
|
||||||
if ! command -v rustup >/dev/null 2>&1; then
|
|
||||||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain "${RUST_TOOLCHAIN}"
|
|
||||||
echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"
|
|
||||||
fi
|
|
||||||
source "$HOME/.cargo/env" 2>/dev/null || true
|
|
||||||
rustup toolchain install "${RUST_TOOLCHAIN}" --profile minimal --component rustfmt --component clippy
|
|
||||||
rustup default "${RUST_TOOLCHAIN}"
|
|
||||||
rustc -V
|
|
||||||
cargo -V
|
|
||||||
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: 缓存 Cargo
|
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
~/.cargo/registry/index
|
|
||||||
~/.cargo/registry/cache
|
|
||||||
~/.cargo/git/db
|
|
||||||
key: cargo-check-${{ env.RUST_TOOLCHAIN }}-${{ hashFiles('Cargo.lock') }}
|
|
||||||
restore-keys: |
|
|
||||||
cargo-check-${{ env.RUST_TOOLCHAIN }}-
|
|
||||||
cargo-check-
|
|
||||||
|
|
||||||
- run: cargo fmt -- --check
|
|
||||||
- run: cargo clippy --locked -- -D warnings
|
|
||||||
- run: cargo test --locked
|
|
||||||
|
|
||||||
build-linux:
|
|
||||||
name: Build Linux (secrets-mcp, musl)
|
|
||||||
needs: [version, check]
|
|
||||||
runs-on: debian
|
|
||||||
timeout-minutes: 25
|
|
||||||
steps:
|
|
||||||
- name: 安装依赖
|
|
||||||
run: |
|
|
||||||
sudo apt-get update
|
|
||||||
sudo apt-get install -y pkg-config musl-tools binutils curl
|
|
||||||
if ! command -v rustup >/dev/null 2>&1; then
|
|
||||||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain "${RUST_TOOLCHAIN}"
|
|
||||||
echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"
|
|
||||||
fi
|
|
||||||
source "$HOME/.cargo/env" 2>/dev/null || true
|
|
||||||
rustup toolchain install "${RUST_TOOLCHAIN}" --profile minimal
|
|
||||||
rustup default "${RUST_TOOLCHAIN}"
|
|
||||||
rustup target add x86_64-unknown-linux-musl --toolchain "${RUST_TOOLCHAIN}"
|
|
||||||
rustc -V
|
|
||||||
cargo -V
|
|
||||||
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: 缓存 Cargo
|
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
~/.cargo/registry/index
|
|
||||||
~/.cargo/registry/cache
|
|
||||||
~/.cargo/git/db
|
|
||||||
key: cargo-x86_64-unknown-linux-musl-${{ env.RUST_TOOLCHAIN }}-${{ hashFiles('Cargo.lock') }}
|
|
||||||
restore-keys: |
|
|
||||||
cargo-x86_64-unknown-linux-musl-${{ env.RUST_TOOLCHAIN }}-
|
|
||||||
cargo-x86_64-unknown-linux-musl-
|
|
||||||
|
|
||||||
- name: 构建 secrets-mcp (musl)
|
|
||||||
run: |
|
|
||||||
cargo build --release --locked --target x86_64-unknown-linux-musl -p secrets-mcp
|
|
||||||
strip target/x86_64-unknown-linux-musl/release/${{ env.MCP_BINARY }}
|
|
||||||
|
|
||||||
- name: 上传 Release 产物
|
|
||||||
if: needs.version.outputs.release_id != ''
|
|
||||||
env:
|
|
||||||
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
|
||||||
run: |
|
|
||||||
[ -z "$RELEASE_TOKEN" ] && exit 0
|
|
||||||
tag="${{ needs.version.outputs.tag }}"
|
|
||||||
bin="target/x86_64-unknown-linux-musl/release/${{ env.MCP_BINARY }}"
|
|
||||||
archive="${{ env.MCP_BINARY }}-${tag}-x86_64-linux-musl.tar.gz"
|
|
||||||
tar -czf "$archive" -C "$(dirname "$bin")" "$(basename "$bin")"
|
tar -czf "$archive" -C "$(dirname "$bin")" "$(basename "$bin")"
|
||||||
sha256sum "$archive" > "${archive}.sha256"
|
sha256sum "$archive" > "${archive}.sha256"
|
||||||
release_url="${{ github.server_url }}/api/v1/repos/${{ github.repository }}/releases/${{ needs.version.outputs.release_id }}/assets"
|
curl -fsS -H "$auth" -F "attachment=@${archive}" "${api}/${release_id}/assets"
|
||||||
curl -fsS -H "Authorization: token $RELEASE_TOKEN" \
|
curl -fsS -H "$auth" -F "attachment=@${archive}.sha256" "${api}/${release_id}/assets"
|
||||||
-F "attachment=@${archive}" "$release_url"
|
echo "Release ${tag} 已发布"
|
||||||
curl -fsS -H "Authorization: token $RELEASE_TOKEN" \
|
|
||||||
-F "attachment=@${archive}.sha256" "$release_url"
|
|
||||||
|
|
||||||
|
# ── 飞书汇总通知 ─────────────────────────────────────────────────────
|
||||||
- name: 飞书通知
|
- name: 飞书通知
|
||||||
if: always()
|
if: always()
|
||||||
env:
|
env:
|
||||||
WEBHOOK_URL: ${{ vars.WEBHOOK_URL }}
|
WEBHOOK_URL: ${{ vars.WEBHOOK_URL }}
|
||||||
run: |
|
run: |
|
||||||
[ -z "$WEBHOOK_URL" ] && exit 0
|
[ -z "$WEBHOOK_URL" ] && exit 0
|
||||||
command -v jq >/dev/null 2>&1 || (sudo apt-get update -qq && sudo apt-get install -y -qq jq)
|
tag="${{ steps.ver.outputs.tag }}"
|
||||||
tag="${{ needs.version.outputs.tag }}"
|
commit="${{ github.event.head_commit.message }}"
|
||||||
commit=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "N/A")
|
[ -z "$commit" ] && commit="${{ github.sha }}"
|
||||||
url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_number }}"
|
url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_number }}"
|
||||||
result="${{ job.status }}"
|
result="${{ job.status }}"
|
||||||
if [ "$result" = "success" ]; then icon="✅"; else icon="❌"; fi
|
if [ "$result" = "success" ]; then icon="✅"; else icon="❌"; fi
|
||||||
msg="secrets-mcp linux 构建${icon}
|
msg="secrets-mcp 构建&发版 ${icon}
|
||||||
版本:${tag}
|
版本:${tag}
|
||||||
提交:${commit}
|
提交:${commit}
|
||||||
作者:${{ github.actor }}
|
作者:${{ github.actor }}
|
||||||
@@ -251,50 +187,21 @@ jobs:
|
|||||||
payload=$(jq -n --arg text "$msg" '{msg_type: "text", content: {text: $text}}')
|
payload=$(jq -n --arg text "$msg" '{msg_type: "text", content: {text: $text}}')
|
||||||
curl -sS -H "Content-Type: application/json" -X POST -d "$payload" "$WEBHOOK_URL"
|
curl -sS -H "Content-Type: application/json" -X POST -d "$payload" "$WEBHOOK_URL"
|
||||||
|
|
||||||
deploy-mcp:
|
deploy:
|
||||||
name: 部署 secrets-mcp
|
name: 部署 secrets-mcp
|
||||||
needs: [version, build-linux]
|
needs: [ci]
|
||||||
# 部署目标由仓库 Actions 配置:vars.DEPLOY_HOST / vars.DEPLOY_USER;私钥 secrets.DEPLOY_SSH_KEY(PEM 原文,勿 base64)
|
if: |
|
||||||
# (可用 scripts/setup-gitea-actions.sh 或 Gitea API 写入,勿写进本文件)
|
github.ref == 'refs/heads/main' ||
|
||||||
# Google OAuth / SERVER_MASTER_KEY / SECRETS_DATABASE_URL 等勿写入 CI,请在 ECS 上
|
github.ref == 'refs/heads/feat/mcp' ||
|
||||||
# /opt/secrets-mcp/.env 配置(见 deploy/.env.example)。
|
github.ref == 'refs/heads/mcp'
|
||||||
# 若仓库 main 仍为纯 CLI、仅 feat/mcp 含本 workflow,请去掉条件里的 main,避免误部署。
|
|
||||||
if: needs.build-linux.result == 'success' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/feat/mcp' || github.ref == 'refs/heads/mcp')
|
|
||||||
runs-on: debian
|
runs-on: debian
|
||||||
timeout-minutes: 10
|
timeout-minutes: 10
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- name: 下载构建产物
|
||||||
|
uses: actions/download-artifact@v3
|
||||||
- name: 安装 Rust
|
|
||||||
run: |
|
|
||||||
if ! command -v rustup >/dev/null 2>&1; then
|
|
||||||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain "${RUST_TOOLCHAIN}"
|
|
||||||
echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"
|
|
||||||
fi
|
|
||||||
source "$HOME/.cargo/env" 2>/dev/null || true
|
|
||||||
sudo apt-get update -qq && sudo apt-get install -y -qq pkg-config musl-tools
|
|
||||||
rustup toolchain install "${RUST_TOOLCHAIN}" --profile minimal
|
|
||||||
rustup default "${RUST_TOOLCHAIN}"
|
|
||||||
rustup target add x86_64-unknown-linux-musl --toolchain "${RUST_TOOLCHAIN}"
|
|
||||||
rustc -V
|
|
||||||
cargo -V
|
|
||||||
|
|
||||||
- name: 缓存 Cargo
|
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
with:
|
||||||
path: |
|
name: ${{ env.MCP_BINARY }}-linux-musl
|
||||||
~/.cargo/registry/index
|
path: /tmp/artifact
|
||||||
~/.cargo/registry/cache
|
|
||||||
~/.cargo/git/db
|
|
||||||
key: cargo-x86_64-unknown-linux-musl-${{ env.RUST_TOOLCHAIN }}-${{ hashFiles('Cargo.lock') }}
|
|
||||||
restore-keys: |
|
|
||||||
cargo-x86_64-unknown-linux-musl-${{ env.RUST_TOOLCHAIN }}-
|
|
||||||
cargo-x86_64-unknown-linux-musl-
|
|
||||||
|
|
||||||
- name: 构建 secrets-mcp
|
|
||||||
run: |
|
|
||||||
cargo build --release --locked --target x86_64-unknown-linux-musl -p secrets-mcp
|
|
||||||
strip target/x86_64-unknown-linux-musl/release/${{ env.MCP_BINARY }}
|
|
||||||
|
|
||||||
- name: 部署到阿里云 ECS
|
- name: 部署到阿里云 ECS
|
||||||
env:
|
env:
|
||||||
@@ -303,16 +210,15 @@ jobs:
|
|||||||
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
||||||
run: |
|
run: |
|
||||||
if [ -z "$DEPLOY_HOST" ] || [ -z "$DEPLOY_USER" ] || [ -z "$DEPLOY_SSH_KEY" ]; then
|
if [ -z "$DEPLOY_HOST" ] || [ -z "$DEPLOY_USER" ] || [ -z "$DEPLOY_SSH_KEY" ]; then
|
||||||
echo "部署跳过:请在仓库 Actions 中配置 vars.DEPLOY_HOST、vars.DEPLOY_USER 与 secrets.DEPLOY_SSH_KEY"
|
echo "部署跳过:请配置 vars.DEPLOY_HOST、vars.DEPLOY_USER 与 secrets.DEPLOY_SSH_KEY"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "$DEPLOY_SSH_KEY" > /tmp/deploy_key
|
echo "$DEPLOY_SSH_KEY" > /tmp/deploy_key
|
||||||
chmod 600 /tmp/deploy_key
|
chmod 600 /tmp/deploy_key
|
||||||
|
|
||||||
SCP="scp -i /tmp/deploy_key -o StrictHostKeyChecking=no"
|
scp -i /tmp/deploy_key -o StrictHostKeyChecking=no \
|
||||||
|
"/tmp/artifact/${MCP_BINARY}" \
|
||||||
$SCP target/x86_64-unknown-linux-musl/release/${{ env.MCP_BINARY }} \
|
|
||||||
"${DEPLOY_USER}@${DEPLOY_HOST}:/tmp/secrets-mcp.new"
|
"${DEPLOY_USER}@${DEPLOY_HOST}:/tmp/secrets-mcp.new"
|
||||||
|
|
||||||
ssh -i /tmp/deploy_key -o StrictHostKeyChecking=no "${DEPLOY_USER}@${DEPLOY_HOST}" "
|
ssh -i /tmp/deploy_key -o StrictHostKeyChecking=no "${DEPLOY_USER}@${DEPLOY_HOST}" "
|
||||||
@@ -322,7 +228,6 @@ jobs:
|
|||||||
sleep 2
|
sleep 2
|
||||||
sudo systemctl is-active secrets-mcp && echo '服务启动成功' || (sudo journalctl -u secrets-mcp -n 20 && exit 1)
|
sudo systemctl is-active secrets-mcp && echo '服务启动成功' || (sudo journalctl -u secrets-mcp -n 20 && exit 1)
|
||||||
"
|
"
|
||||||
|
|
||||||
rm -f /tmp/deploy_key
|
rm -f /tmp/deploy_key
|
||||||
|
|
||||||
- name: 飞书通知
|
- name: 飞书通知
|
||||||
@@ -331,94 +236,13 @@ jobs:
|
|||||||
WEBHOOK_URL: ${{ vars.WEBHOOK_URL }}
|
WEBHOOK_URL: ${{ vars.WEBHOOK_URL }}
|
||||||
run: |
|
run: |
|
||||||
[ -z "$WEBHOOK_URL" ] && exit 0
|
[ -z "$WEBHOOK_URL" ] && exit 0
|
||||||
command -v jq >/dev/null 2>&1 || (sudo apt-get update -qq && sudo apt-get install -y -qq jq)
|
tag="${{ needs.ci.outputs.tag }}"
|
||||||
tag="${{ needs.version.outputs.tag }}"
|
|
||||||
commit=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "N/A")
|
|
||||||
url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_number }}"
|
url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_number }}"
|
||||||
result="${{ job.status }}"
|
result="${{ job.status }}"
|
||||||
if [ "$result" = "success" ]; then icon="✅"; else icon="❌"; fi
|
if [ "$result" = "success" ]; then icon="✅"; else icon="❌"; fi
|
||||||
msg="secrets-mcp 部署${icon}
|
msg="secrets-mcp 部署 ${icon}
|
||||||
版本:${tag}
|
版本:${tag}
|
||||||
提交:${commit}
|
|
||||||
作者:${{ github.actor }}
|
作者:${{ github.actor }}
|
||||||
详情:${url}"
|
详情:${url}"
|
||||||
payload=$(jq -n --arg text "$msg" '{msg_type: "text", content: {text: $text}}')
|
payload=$(jq -n --arg text "$msg" '{msg_type: "text", content: {text: $text}}')
|
||||||
curl -sS -H "Content-Type: application/json" -X POST -d "$payload" "$WEBHOOK_URL"
|
curl -sS -H "Content-Type: application/json" -X POST -d "$payload" "$WEBHOOK_URL"
|
||||||
|
|
||||||
publish-release:
|
|
||||||
name: 发布草稿 Release
|
|
||||||
needs: [version, build-linux]
|
|
||||||
if: always() && needs.version.outputs.release_id != ''
|
|
||||||
runs-on: debian
|
|
||||||
timeout-minutes: 5
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: 发布草稿
|
|
||||||
env:
|
|
||||||
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
|
||||||
run: |
|
|
||||||
[ -z "$RELEASE_TOKEN" ] && exit 0
|
|
||||||
|
|
||||||
linux_r="${{ needs.build-linux.result }}"
|
|
||||||
if [ "$linux_r" != "success" ]; then
|
|
||||||
echo "linux 构建未成功,保留草稿 Release"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
release_api="${{ github.server_url }}/api/v1/repos/${{ github.repository }}/releases/${{ needs.version.outputs.release_id }}"
|
|
||||||
http_code=$(curl -sS -o /tmp/publish-release.json -w '%{http_code}' \
|
|
||||||
-H "Authorization: token $RELEASE_TOKEN" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
-X PATCH "$release_api" \
|
|
||||||
-d '{"draft":false}')
|
|
||||||
|
|
||||||
if [ "$http_code" != "200" ]; then
|
|
||||||
echo "发布草稿 Release 失败 (HTTP ${http_code})"
|
|
||||||
cat /tmp/publish-release.json 2>/dev/null || true
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "Release 已发布"
|
|
||||||
|
|
||||||
- name: 飞书汇总通知
|
|
||||||
if: always()
|
|
||||||
env:
|
|
||||||
WEBHOOK_URL: ${{ vars.WEBHOOK_URL }}
|
|
||||||
run: |
|
|
||||||
[ -z "$WEBHOOK_URL" ] && exit 0
|
|
||||||
command -v jq >/dev/null 2>&1 || (sudo apt-get update -qq && sudo apt-get install -y -qq jq)
|
|
||||||
|
|
||||||
tag="${{ needs.version.outputs.tag }}"
|
|
||||||
tag_exists="${{ needs.version.outputs.tag_exists }}"
|
|
||||||
commit="${{ github.event.head_commit.message }}"
|
|
||||||
[ -z "$commit" ] && commit="${{ github.sha }}"
|
|
||||||
url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_number }}"
|
|
||||||
|
|
||||||
linux_r="${{ needs.build-linux.result }}"
|
|
||||||
publish_r="${{ job.status }}"
|
|
||||||
|
|
||||||
icon() { case "$1" in success) echo "✅";; skipped) echo "⏭";; *) echo "❌";; esac; }
|
|
||||||
|
|
||||||
if [ "$linux_r" = "success" ] && [ "$publish_r" = "success" ]; then
|
|
||||||
status="发布成功 ✅"
|
|
||||||
elif [ "$linux_r" != "success" ]; then
|
|
||||||
status="构建失败 ❌"
|
|
||||||
else
|
|
||||||
status="发布失败 ❌"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$tag_exists" = "false" ]; then
|
|
||||||
version_line="🆕 新版本 ${tag}"
|
|
||||||
else
|
|
||||||
version_line="🔄 重复构建 ${tag}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
msg="secrets-mcp ${status}
|
|
||||||
${version_line}
|
|
||||||
linux $(icon "$linux_r") | Release $(icon "$publish_r")
|
|
||||||
提交:${commit}
|
|
||||||
作者:${{ github.actor }}
|
|
||||||
详情:${url}"
|
|
||||||
|
|
||||||
payload=$(jq -n --arg text "$msg" '{msg_type: "text", content: {text: $text}}')
|
|
||||||
curl -sS -H "Content-Type: application/json" -X POST -d "$payload" "$WEBHOOK_URL"
|
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@
|
|||||||
# Google OAuth 下载的 JSON 凭据文件
|
# Google OAuth 下载的 JSON 凭据文件
|
||||||
client_secret_*.apps.googleusercontent.com.json
|
client_secret_*.apps.googleusercontent.com.json
|
||||||
*.pem
|
*.pem
|
||||||
|
tmp/
|
||||||
64
AGENTS.md
64
AGENTS.md
@@ -2,12 +2,13 @@
|
|||||||
|
|
||||||
本仓库为 **MCP SaaS**:`secrets-core`(业务与持久化)+ `secrets-mcp`(Streamable HTTP MCP、Web、OAuth、API Key)。对外入口见 `crates/secrets-mcp`。
|
本仓库为 **MCP SaaS**:`secrets-core`(业务与持久化)+ `secrets-mcp`(Streamable HTTP MCP、Web、OAuth、API Key)。对外入口见 `crates/secrets-mcp`。
|
||||||
|
|
||||||
## 提交 / 发版硬规则(优先于下文)
|
## 提交 / 推送硬规则(优先于下文)
|
||||||
|
|
||||||
|
**每次提交和推送前必须执行以下检查,无论是否明确「发版」:**
|
||||||
|
|
||||||
1. 涉及 `crates/**`、根目录 `Cargo.toml`/`Cargo.lock`、`secrets-mcp` 行为变更的提交,默认视为**需要发版**,除非明确说明「本次不发版」。
|
1. 涉及 `crates/**`、根目录 `Cargo.toml`/`Cargo.lock`、`secrets-mcp` 行为变更的提交,默认视为**需要发版**,除非明确说明「本次不发版」。
|
||||||
2. 发版前检查 `crates/secrets-mcp/Cargo.toml` 的 `version`,再查 tag:`git tag -l 'secrets-mcp-*'`。
|
2. 提交前检查 `crates/secrets-mcp/Cargo.toml` 的 `version`,再查 tag:`git tag -l 'secrets-mcp-*'`。若当前版本对应 tag 已存在且有代码变更,**必须 bump 版本号**并 `cargo build` 同步 `Cargo.lock`。
|
||||||
3. 若当前版本对应 tag 已存在,须先 bump `version`,再 `cargo build` 同步 `Cargo.lock` 后提交。
|
3. 提交前运行 `./scripts/release-check.sh`(版本/tag + `fmt` + `clippy --locked` + `test --locked`)。若脚本不存在或不可用,至少运行 `cargo fmt -- --check && cargo clippy --locked -- -D warnings && cargo test --locked`。
|
||||||
4. 提交前优先运行 `./scripts/release-check.sh`(版本/tag + `fmt` + `clippy --locked` + `test --locked`)。
|
|
||||||
|
|
||||||
## 项目结构
|
## 项目结构
|
||||||
|
|
||||||
@@ -28,7 +29,8 @@ secrets/
|
|||||||
|
|
||||||
- **建议库名**:`secrets-mcp`(专用实例,与历史库名区分)。
|
- **建议库名**:`secrets-mcp`(专用实例,与历史库名区分)。
|
||||||
- **连接**:环境变量 **`SECRETS_DATABASE_URL`**(本分支无本地配置文件路径)。
|
- **连接**:环境变量 **`SECRETS_DATABASE_URL`**(本分支无本地配置文件路径)。
|
||||||
- **表**:`entries`(含 `user_id`)、`secrets`、`entries_history`、`secrets_history`、`audit_log`、`users`、`oauth_accounts`、`api_keys`,首次连接 **auto-migrate**。
|
- **表**:`entries`(含 `user_id`)、`secrets`、`entries_history`、`secrets_history`、`audit_log`、`users`、`oauth_accounts`,首次连接 **auto-migrate**(`secrets-core` 的 `migrate`)。
|
||||||
|
- **Web 会话**:与上项 **同一数据库 URL**;`secrets-mcp` 启动时对 tower-sessions 的 PostgreSQL 存储 **auto-migrate**(会话表与业务表共存于该实例,无需第二套连接串)。
|
||||||
|
|
||||||
### 表结构(摘录)
|
### 表结构(摘录)
|
||||||
|
|
||||||
@@ -36,15 +38,18 @@ secrets/
|
|||||||
entries (
|
entries (
|
||||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||||
user_id UUID, -- 多租户:NULL=遗留行;非空=归属用户
|
user_id UUID, -- 多租户:NULL=遗留行;非空=归属用户
|
||||||
namespace VARCHAR(64) NOT NULL,
|
folder VARCHAR(128) NOT NULL DEFAULT '',
|
||||||
kind VARCHAR(64) NOT NULL,
|
type VARCHAR(64) NOT NULL DEFAULT '',
|
||||||
name VARCHAR(256) NOT NULL,
|
name VARCHAR(256) NOT NULL,
|
||||||
|
notes TEXT NOT NULL DEFAULT '',
|
||||||
tags TEXT[] NOT NULL DEFAULT '{}',
|
tags TEXT[] NOT NULL DEFAULT '{}',
|
||||||
metadata JSONB NOT NULL DEFAULT '{}',
|
metadata JSONB NOT NULL DEFAULT '{}',
|
||||||
version BIGINT NOT NULL DEFAULT 1,
|
version BIGINT NOT NULL DEFAULT 1,
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
)
|
)
|
||||||
|
-- 唯一:UNIQUE(user_id, folder, name) WHERE user_id IS NOT NULL;
|
||||||
|
-- UNIQUE(folder, name) WHERE user_id IS NULL(单租户遗留)
|
||||||
```
|
```
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
@@ -60,7 +65,7 @@ secrets (
|
|||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
### users / oauth_accounts / api_keys
|
### users / oauth_accounts
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
users (
|
users (
|
||||||
@@ -71,6 +76,7 @@ users (
|
|||||||
key_salt BYTEA, -- PBKDF2 salt(32B),首次设置密码短语时写入
|
key_salt BYTEA, -- PBKDF2 salt(32B),首次设置密码短语时写入
|
||||||
key_check BYTEA, -- 派生密钥加密已知常量,用于验证密码短语
|
key_check BYTEA, -- 派生密钥加密已知常量,用于验证密码短语
|
||||||
key_params JSONB, -- 算法参数,如 {"alg":"pbkdf2-sha256","iterations":600000}
|
key_params JSONB, -- 算法参数,如 {"alg":"pbkdf2-sha256","iterations":600000}
|
||||||
|
api_key TEXT UNIQUE, -- MCP Bearer token(当前实现为明文存储)
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
)
|
)
|
||||||
@@ -80,32 +86,31 @@ oauth_accounts (
|
|||||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
provider VARCHAR(32) NOT NULL,
|
provider VARCHAR(32) NOT NULL,
|
||||||
provider_id VARCHAR(256) NOT NULL,
|
provider_id VARCHAR(256) NOT NULL,
|
||||||
...
|
email VARCHAR(256),
|
||||||
|
name VARCHAR(256),
|
||||||
|
avatar_url TEXT,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
UNIQUE(provider, provider_id)
|
UNIQUE(provider, provider_id)
|
||||||
)
|
)
|
||||||
|
-- 另有唯一索引 UNIQUE(user_id, provider)(迁移中 idx_oauth_accounts_user_provider):同一用户每种 provider 至多一条关联。
|
||||||
api_keys (
|
|
||||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
|
||||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
||||||
name VARCHAR(256) NOT NULL,
|
|
||||||
key_hash VARCHAR(64) NOT NULL UNIQUE,
|
|
||||||
key_prefix VARCHAR(12) NOT NULL,
|
|
||||||
last_used_at TIMESTAMPTZ,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
||||||
)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### audit_log / history
|
### audit_log / history
|
||||||
|
|
||||||
与迁移脚本一致:`audit_log`、`entries_history`、`secrets_history` 用于审计与时间旅行恢复;字段定义见 `crates/secrets-core/src/db.rs` 内 `migrate` SQL。
|
与迁移脚本一致:`audit_log`、`entries_history`、`secrets_history` 用于审计与时间旅行恢复;字段定义见 `crates/secrets-core/src/db.rs` 内 `migrate` SQL。`audit_log` 含可选 **`user_id`**(多租户下标识操作者;可空以兼容遗留数据)。`audit_log` 中普通业务事件使用 **`folder` / `type` / `name`** 对应 entry 坐标;登录类事件固定使用 **`folder='auth'`**,此时 `type`/`name` 表示认证目标而非 entry 身份。
|
||||||
|
|
||||||
|
### MCP 消歧(AI 调用)
|
||||||
|
|
||||||
|
按 `name` 定位条目的工具(`get` / `update` / 单条 `delete` / `history` / `rollback`):若该用户下仅一条匹配则直接执行;若多条(同 `name`、不同 `folder`)则返回错误并提示补全 `folder`。`secrets_delete` 的 `dry_run=true` 与真实删除使用相同消歧规则。
|
||||||
|
|
||||||
### 字段职责
|
### 字段职责
|
||||||
|
|
||||||
| 字段 | 含义 | 示例 |
|
| 字段 | 含义 | 示例 |
|
||||||
|------|------|------|
|
|------|------|------|
|
||||||
| `namespace` | 隔离空间 | `refining` |
|
| `folder` | 隔离空间(参与唯一键) | `refining` |
|
||||||
| `kind` | 记录类型 | `server`, `service`, `key` |
|
| `type` | 软分类(不参与唯一键) | `server`, `service`, `key`, `person` |
|
||||||
| `name` | 标识名 | `gitea`, `i-example0…` |
|
| `name` | 标识名 | `gitea`, `aliyun` |
|
||||||
|
| `notes` | 非敏感说明 | 自由文本 |
|
||||||
| `tags` | 标签 | `["aliyun","prod"]` |
|
| `tags` | 标签 | `["aliyun","prod"]` |
|
||||||
| `metadata` | 明文描述 | `ip`、`url`、`key_ref` |
|
| `metadata` | 明文描述 | `ip`、`url`、`key_ref` |
|
||||||
| `secrets.field_name` | 加密字段名(明文) | `token`, `ssh_key` |
|
| `secrets.field_name` | 加密字段名(明文) | `token`, `ssh_key` |
|
||||||
@@ -113,14 +118,14 @@ api_keys (
|
|||||||
|
|
||||||
### PEM 共享(`key_ref`)
|
### PEM 共享(`key_ref`)
|
||||||
|
|
||||||
将共享 PEM 存为 `kind=key` 的 entry;其它记录在 `metadata.key_ref` 指向该 key 的 `name`。更新 key 记录后,引用方通过服务层解析合并逻辑即可使用新密钥(实现见 `secrets_core::service`)。
|
建议将共享 PEM 存为 **`type=key`** 的 entry;其它记录在 `metadata.key_ref` 指向目标 entry 的 `name`(支持 `folder/name` 格式消歧)。删除被引用 key 时,服务会自动迁移为单副本 + 重定向(复制到首个引用方,其余引用方改指向新 owner);解析逻辑见 `secrets_core::service::env_map`。
|
||||||
|
|
||||||
## 代码规范
|
## 代码规范
|
||||||
|
|
||||||
- 错误:业务层 `anyhow::Result`,避免生产路径 `unwrap()`。
|
- 错误:业务层 `anyhow::Result`,避免生产路径 `unwrap()`。
|
||||||
- 异步:`tokio` + `sqlx` async。
|
- 异步:`tokio` + `sqlx` async。
|
||||||
- SQL:`sqlx::query` / `query_as` 参数绑定;动态 WHERE 仍须用占位符绑定。
|
- SQL:`sqlx::query` / `query_as` 参数绑定;动态 WHERE 仍须用占位符绑定。
|
||||||
- 日志:运维用 `tracing`;面向用户的 Web 响应走 axum handler。
|
- 日志:运维用 `tracing`;面向用户的 Web 响应走 axum handler。tracing 字段风格:变量名即字段名时用简写(`%var`、`?var`、`var`),否则用显式形式(`field = %expr`)。
|
||||||
- 审计:写操作成功后尽量 `audit::log_tx`;失败可 `warn`,不掩盖主错误。
|
- 审计:写操作成功后尽量 `audit::log_tx`;失败可 `warn`,不掩盖主错误。
|
||||||
- 加密:密钥由用户密码短语通过 **PBKDF2-SHA256(600k 次)** 在客户端派生,服务端只存 `key_salt`/`key_check`/`key_params`,不持有原始密钥。Web 客户端在浏览器本地完成加解密;MCP 客户端通过 `X-Encryption-Key` 请求头传递密钥,服务端临时解密后返回明文。
|
- 加密:密钥由用户密码短语通过 **PBKDF2-SHA256(600k 次)** 在客户端派生,服务端只存 `key_salt`/`key_check`/`key_params`,不持有原始密钥。Web 客户端在浏览器本地完成加解密;MCP 客户端通过 `X-Encryption-Key` 请求头传递密钥,服务端临时解密后返回明文。
|
||||||
- MCP:tools 参数与 JSON Schema(`schemars`)保持同步,鉴权以请求扩展中的用户上下文为准。
|
- MCP:tools 参数与 JSON Schema(`schemars`)保持同步,鉴权以请求扩展中的用户上下文为准。
|
||||||
@@ -148,10 +153,10 @@ git tag -l 'secrets-mcp-*'
|
|||||||
|
|
||||||
## CI/CD
|
## CI/CD
|
||||||
|
|
||||||
- **触发**:任意分支 `push`,且路径含 `crates/**`、`deploy/**`、根目录 `Cargo.toml`、`Cargo.lock`(见 `.gitea/workflows/secrets.yml`)。
|
- **触发**:任意分支 `push`,且路径含 `crates/**`、`deploy/**`、根目录 `Cargo.toml`、`Cargo.lock`、`.gitea/workflows/**`(见 `.gitea/workflows/secrets.yml`)。
|
||||||
- **版本与 tag**:从 `crates/secrets-mcp/Cargo.toml` 读版本;若远程已存在同名 `secrets-mcp-<version>` tag,**工作流失败**(须先 bump 版本并 `cargo build` 同步 `Cargo.lock`);否则由 CI 创建并推送该 tag。
|
- **版本与 tag**:从 `crates/secrets-mcp/Cargo.toml` 读版本;构建成功后打 `secrets-mcp-<version>`:若远端已存在同名 tag,CI 会先删后于**当前提交**重建并推送(覆盖式发版)。
|
||||||
- **质量与构建**:`fmt` / `clippy --locked` / `test --locked` → `x86_64-unknown-linux-musl` 发布构建 `secrets-mcp`。
|
- **质量与构建**:`fmt` / `clippy --locked` / `test --locked` → `x86_64-unknown-linux-musl` 发布构建 `secrets-mcp`。
|
||||||
- **Release(可选)**:`secrets.RELEASE_TOKEN`(Gitea PAT)用于创建草稿 Release、上传 `tar.gz` + `.sha256`、构建成功后发布;未配置则跳过 API Release,仅 tag + 构建。
|
- **Release(可选)**:`secrets.RELEASE_TOKEN`(Gitea PAT)用于通过 API **创建或更新**该 tag 的 Release(非 draft)、上传 `tar.gz` + `.sha256`;未配置则跳过 API Release,仅 tag + 构建。
|
||||||
- **部署(可选)**:仅 `main`、`feat/mcp`、`mcp` 分支在构建成功时跑 `deploy-mcp`;需 `vars.DEPLOY_HOST`、`vars.DEPLOY_USER`、`secrets.DEPLOY_SSH_KEY`。勿把 OAuth/DB 等写进 workflow,用 `deploy/.env.example` 在目标机配置。
|
- **部署(可选)**:仅 `main`、`feat/mcp`、`mcp` 分支在构建成功时跑 `deploy-mcp`;需 `vars.DEPLOY_HOST`、`vars.DEPLOY_USER`、`secrets.DEPLOY_SSH_KEY`。勿把 OAuth/DB 等写进 workflow,用 `deploy/.env.example` 在目标机配置。
|
||||||
- **Secrets 写法**:Actions **secrets 须为原始值**(PEM、PAT 明文),**勿** base64;否则 SSH/Release 会失败。**勿**在 CI 中保存 `GOOGLE_CLIENT_SECRET`、DB 密码。
|
- **Secrets 写法**:Actions **secrets 须为原始值**(PEM、PAT 明文),**勿** base64;否则 SSH/Release 会失败。**勿**在 CI 中保存 `GOOGLE_CLIENT_SECRET`、DB 密码。
|
||||||
- **通知**:`vars.WEBHOOK_URL`(可选,飞书)。
|
- **通知**:`vars.WEBHOOK_URL`(可选,飞书)。
|
||||||
@@ -162,9 +167,8 @@ git tag -l 'secrets-mcp-*'
|
|||||||
|------|------|
|
|------|------|
|
||||||
| `SECRETS_DATABASE_URL` | **必填**。PostgreSQL URL。 |
|
| `SECRETS_DATABASE_URL` | **必填**。PostgreSQL URL。 |
|
||||||
| `BASE_URL` | 对外基址;OAuth 回调 `${BASE_URL}/auth/google/callback`。 |
|
| `BASE_URL` | 对外基址;OAuth 回调 `${BASE_URL}/auth/google/callback`。 |
|
||||||
| `SECRETS_MCP_BIND` | 监听地址,默认 `0.0.0.0:9315`。 |
|
| `SECRETS_MCP_BIND` | 监听地址,默认 `127.0.0.1:9315`(容器/远程直接暴露时需改为 `0.0.0.0:9315`)。 |
|
||||||
| `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET` | 可选;仅运行时配置。 |
|
| `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET` | 可选;仅运行时配置。 |
|
||||||
| `RUST_LOG` | 如 `secrets_mcp=debug`。 |
|
| `RUST_LOG` | 如 `secrets_mcp=debug`。 |
|
||||||
| `USER` | 若写入审计 `actor`,由运行环境提供。 |
|
|
||||||
|
|
||||||
> `SERVER_MASTER_KEY` 已不再需要。新架构下密钥由用户密码短语在客户端派生,服务端不持有。
|
> `SERVER_MASTER_KEY` 已不再需要。新架构下密钥由用户密码短语在客户端派生,服务端不持有。
|
||||||
|
|||||||
40
Cargo.lock
generated
40
Cargo.lock
generated
@@ -1809,6 +1809,25 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rmp"
|
||||||
|
version = "0.8.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4ba8be72d372b2c9b35542551678538b562e7cf86c3315773cae48dfbfe7790c"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rmp-serde"
|
||||||
|
version = "1.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "72f81bee8c8ef9b577d1681a70ebbc962c232461e397b22c208c43c04b67a155"
|
||||||
|
dependencies = [
|
||||||
|
"rmp",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rsa"
|
name = "rsa"
|
||||||
version = "0.9.10"
|
version = "0.9.10"
|
||||||
@@ -1949,7 +1968,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "secrets-mcp"
|
name = "secrets-mcp"
|
||||||
version = "0.1.5"
|
version = "0.3.8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"askama",
|
"askama",
|
||||||
@@ -1967,10 +1986,12 @@ dependencies = [
|
|||||||
"serde_json",
|
"serde_json",
|
||||||
"sha2",
|
"sha2",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
|
"time",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower",
|
"tower",
|
||||||
"tower-http",
|
"tower-http",
|
||||||
"tower-sessions",
|
"tower-sessions",
|
||||||
|
"tower-sessions-sqlx-store-chrono",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"urlencoding",
|
"urlencoding",
|
||||||
@@ -2700,6 +2721,7 @@ dependencies = [
|
|||||||
"tower",
|
"tower",
|
||||||
"tower-layer",
|
"tower-layer",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2765,6 +2787,22 @@ dependencies = [
|
|||||||
"tower-sessions-core",
|
"tower-sessions-core",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower-sessions-sqlx-store-chrono"
|
||||||
|
version = "0.14.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b295c8fc08db03246e92773c5e10119b72db6bc4240112135bebb0e49670804f"
|
||||||
|
dependencies = [
|
||||||
|
"async-trait",
|
||||||
|
"axum",
|
||||||
|
"chrono",
|
||||||
|
"rmp-serde",
|
||||||
|
"sqlx",
|
||||||
|
"thiserror",
|
||||||
|
"time",
|
||||||
|
"tower-sessions-core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tracing"
|
name = "tracing"
|
||||||
version = "0.1.44"
|
version = "0.1.44"
|
||||||
|
|||||||
62
README.md
62
README.md
@@ -17,17 +17,47 @@ cargo build --release -p secrets-mcp
|
|||||||
|
|
||||||
| 变量 | 说明 |
|
| 变量 | 说明 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| `SECRETS_DATABASE_URL` | **必填**。PostgreSQL 连接串(建议专用库,如 `secrets-mcp`)。 |
|
| `SECRETS_DATABASE_URL` | **必填**。PostgreSQL 连接串(推荐使用域名,例如 `db.refining.ltd`,避免直连 IP)。 |
|
||||||
|
| `SECRETS_DATABASE_SSL_MODE` | 可选但强烈建议生产必填。推荐 `verify-full`(至少 `verify-ca`),避免回退到弱 TLS 模式。 |
|
||||||
|
| `SECRETS_DATABASE_SSL_ROOT_CERT` | 可选。私有 CA 或自签链路时指定 CA 根证书路径(如 `/etc/secrets/pg-ca.crt`)。 |
|
||||||
|
| `SECRETS_ENV` | 可选。设为 `prod` / `production` 时会拒绝弱 PostgreSQL TLS 模式(`prefer`、`disable`、`allow`、`require`)。 |
|
||||||
| `BASE_URL` | 对外访问基址;OAuth 回调为 `{BASE_URL}/auth/google/callback`。默认 `http://localhost:9315`。 |
|
| `BASE_URL` | 对外访问基址;OAuth 回调为 `{BASE_URL}/auth/google/callback`。默认 `http://localhost:9315`。 |
|
||||||
| `SECRETS_MCP_BIND` | 监听地址,默认 `0.0.0.0:9315`。反代时常为 `127.0.0.1:9315`。 |
|
| `SECRETS_MCP_BIND` | 监听地址,默认 `127.0.0.1:9315`。容器内或直接对外暴露端口时请改为 `0.0.0.0:9315`;反代时常为 `127.0.0.1:9315`。 |
|
||||||
| `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET` | 可选;不配置则无 Google 登录入口。运行时从环境读取,勿写入 CI、勿打入二进制。 |
|
| `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET` | 可选;不配置则无 Google 登录入口。运行时从环境读取,勿写入 CI、勿打入二进制。 |
|
||||||
|
| `RUST_LOG` | 可选;日志级别,如 `secrets_mcp=debug`。 |
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cargo run -p secrets-mcp
|
cargo run -p secrets-mcp
|
||||||
```
|
```
|
||||||
|
|
||||||
|
生产推荐示例(PostgreSQL TLS):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
SECRETS_DATABASE_URL=postgres://postgres:***@db.refining.ltd:5432/secrets-mcp
|
||||||
|
SECRETS_DATABASE_SSL_MODE=verify-full
|
||||||
|
SECRETS_DATABASE_SSL_ROOT_CERT=/etc/secrets/pg-ca.crt
|
||||||
|
SECRETS_ENV=production
|
||||||
|
```
|
||||||
|
|
||||||
- **Web**:`BASE_URL`(登录、Dashboard、设置密码短语、创建 API Key)。
|
- **Web**:`BASE_URL`(登录、Dashboard、设置密码短语、创建 API Key)。
|
||||||
- **MCP**:Streamable HTTP 基址 `{BASE_URL}/mcp`,需 `Authorization: Bearer <api_key>` + `X-Encryption-Key: <hex>` 请求头。
|
- **MCP**:Streamable HTTP 基址 `{BASE_URL}/mcp`,需 `Authorization: Bearer <api_key>` + `X-Encryption-Key: <hex>` 请求头(读密文工具须带密钥)。
|
||||||
|
|
||||||
|
## PostgreSQL TLS 加固
|
||||||
|
|
||||||
|
- 推荐将数据库域名单独设置为 `db.refining.ltd`,服务域名保持 `secrets.refining.app`。
|
||||||
|
- 数据库证书建议使用可校验链路(如 Let's Encrypt 或私有 CA),并保证证书 `SAN` 包含 `db.refining.ltd`。
|
||||||
|
- PostgreSQL 侧建议使用 `hostssl` 规则限制应用来源(如 `47.238.146.244/32`),逐步移除公网明文 `host` 访问。
|
||||||
|
- 应用端推荐 `SECRETS_DATABASE_SSL_MODE=verify-full`;仅在过渡阶段可临时用 `verify-ca`。
|
||||||
|
- 可执行运维步骤见 [`deploy/postgres-tls-hardening.md`](deploy/postgres-tls-hardening.md)。
|
||||||
|
|
||||||
|
## MCP 与 AI 工作流(v0.3+)
|
||||||
|
|
||||||
|
条目在逻辑上以 **`(folder, name)`** 在用户内唯一(数据库唯一索引:`user_id + folder + name`)。同名可在不同 folder 下各存一条(例如 `refining/aliyun` 与 `ricnsmart/aliyun`)。
|
||||||
|
|
||||||
|
- **`secrets_search`**:发现条目(可按 query / folder / type / name 过滤);不要求加密头。
|
||||||
|
- **`secrets_get` / `secrets_update` / `secrets_delete`(按 name)/ `secrets_history` / `secrets_rollback`**:仅 `name` 且全局唯一则直接命中;若多条同名,返回消歧错误,需在参数中补 **`folder`**。
|
||||||
|
- **`secrets_delete`**:`dry_run=true` 时与真实删除相同的消歧规则——唯一则预览一条,多条则报错并要求 `folder`。
|
||||||
|
- **共享 key 自动迁移删除**:删除仍被 `metadata.key_ref` 引用的 key 条目时,系统会自动迁移:把密文复制到首个引用方,并将其余引用方的 `key_ref` 重定向到新 owner,然后继续删除。
|
||||||
|
|
||||||
## 加密架构(混合 E2EE)
|
## 加密架构(混合 E2EE)
|
||||||
|
|
||||||
@@ -77,7 +107,7 @@ flowchart LR
|
|||||||
### 敏感数据传输
|
### 敏感数据传输
|
||||||
|
|
||||||
- **OAuth `client_secret`** 只存服务端环境变量,不发给浏览器
|
- **OAuth `client_secret`** 只存服务端环境变量,不发给浏览器
|
||||||
- **API Key** 创建时原始 key 仅展示一次,库中只存 SHA-256 哈希
|
- **API Key** 当前存放在 `users.api_key`,Dashboard 会明文展示并可重置
|
||||||
- **X-Encryption-Key** 随 MCP 请求经 TLS 传输,服务端仅在请求处理期间持有(不持久化)
|
- **X-Encryption-Key** 随 MCP 请求经 TLS 传输,服务端仅在请求处理期间持有(不持久化)
|
||||||
- **生产环境必须走 HTTPS/TLS**
|
- **生产环境必须走 HTTPS/TLS**
|
||||||
|
|
||||||
@@ -121,13 +151,14 @@ flowchart LR
|
|||||||
|
|
||||||
## 数据模型
|
## 数据模型
|
||||||
|
|
||||||
主表 **`entries`**(`namespace`、`kind`、`name`、`tags`、`metadata`,多租户时带 `user_id`)+ 子表 **`secrets`**(每行一个加密字段:`field_name`、`encrypted`)。另有 `entries_history`、`secrets_history`、`audit_log`,以及 **`users`**(含 `key_salt`、`key_check`、`key_params`)、**`oauth_accounts`**、**`api_keys`**。首次连库自动迁移建表。
|
主表 **`entries`**(`folder`、`type`、`name`、`notes`、`tags`、`metadata`,多租户时带 `user_id`)+ 子表 **`secrets`**(每行一个加密字段:`field_name`、`encrypted`)。**唯一性**:`UNIQUE(user_id, folder, name)`(`user_id` 为空时为遗留行唯一 `(folder, name)`)。另有 `entries_history`、`secrets_history`、`audit_log`,以及 **`users`**(含 `key_salt`、`key_check`、`key_params`、`api_key`)、**`oauth_accounts`**。首次连库自动迁移建表(`secrets-core` 的 `migrate`);已有库可对照 [`scripts/migrate-v0.3.0.sql`](scripts/migrate-v0.3.0.sql) 做列重命名与索引重建。**Web 登录会话**(tower-sessions)使用同一 `SECRETS_DATABASE_URL`,进程启动时对会话存储执行迁移(见 `secrets-mcp` 中 `PostgresStore::migrate`),无需额外环境变量。
|
||||||
|
|
||||||
| 位置 | 字段 | 说明 |
|
| 位置 | 字段 | 说明 |
|
||||||
|------|------|------|
|
|------|------|------|
|
||||||
| entries | namespace | 一级隔离,如 `refining`、`ricnsmart` |
|
| entries | folder | 组织/隔离空间,如 `refining`、`ricnsmart`;参与唯一键 |
|
||||||
| entries | kind | `server`、`service`、`key` 等(可扩展) |
|
| entries | type | 软分类,如 `server`、`service`、`key`、`person`(可扩展,不参与唯一键) |
|
||||||
| entries | name | 人类可读标识 |
|
| entries | name | 人类可读标识;与 `folder` 一起在用户内唯一 |
|
||||||
|
| entries | notes | 非敏感说明文本 |
|
||||||
| entries | metadata | 明文 JSON(ip、url、`key_ref` 等) |
|
| entries | metadata | 明文 JSON(ip、url、`key_ref` 等) |
|
||||||
| secrets | field_name | 明文字段名,便于 schema 展示 |
|
| secrets | field_name | 明文字段名,便于 schema 展示 |
|
||||||
| secrets | encrypted | AES-GCM 密文(含 nonce) |
|
| secrets | encrypted | AES-GCM 密文(含 nonce) |
|
||||||
@@ -137,14 +168,16 @@ flowchart LR
|
|||||||
|
|
||||||
### PEM 共享(`key_ref`)
|
### PEM 共享(`key_ref`)
|
||||||
|
|
||||||
同一 PEM 可被多条 `server` 记录引用:将 PEM 存为 `kind=key` 的 entry,在服务器条目的 `metadata.key_ref` 中写 key 的名称;轮换时只更新 key 对应记录即可。
|
同一 PEM 可被多条 `server` 等记录引用:建议将 PEM 存为 **`type=key`** 的 entry,在其它条目的 `metadata.key_ref` 中写目标 entry 的 `name`(支持 `folder/name` 格式消歧);轮换时只更新该目标记录即可。
|
||||||
|
删除共享 key 时,系统会自动迁移引用:将密文复制到首个引用方(单副本),其余引用方的 `key_ref` 自动重定向到该新 owner,再删除原 key 记录。
|
||||||
|
|
||||||
## 审计日志
|
## 审计日志
|
||||||
|
|
||||||
`add`、`update`、`delete` 等写操作写入 **`audit_log`**(操作类型、对象、摘要,不含 secret 明文)。
|
`add`、`update`、`delete` 等写操作写入 **`audit_log`**(操作类型、对象、摘要,不含 secret 明文)。多租户场景下可写 **`user_id`**(可空,兼容遗留行)。
|
||||||
|
业务条目事件使用 **`folder` / `type` / `name`**;登录类事件使用 **`folder='auth'`**,此时 `type`/`name` 表示认证目标(例如 `oauth` / `google`),不表示某条 secrets entry。
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT action, namespace, kind, name, actor, detail, created_at
|
SELECT action, folder, type, name, detail, user_id, created_at
|
||||||
FROM audit_log
|
FROM audit_log
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
LIMIT 20;
|
LIMIT 20;
|
||||||
@@ -157,6 +190,7 @@ Cargo.toml
|
|||||||
crates/secrets-core/ # db / crypto / models / audit / service
|
crates/secrets-core/ # db / crypto / models / audit / service
|
||||||
crates/secrets-mcp/ # MCP HTTP、Web、OAuth、API Key
|
crates/secrets-mcp/ # MCP HTTP、Web、OAuth、API Key
|
||||||
scripts/
|
scripts/
|
||||||
|
migrate-v0.3.0.sql # 可选:手动 SQL 迁移(namespace/kind → folder/type、唯一键含 folder)
|
||||||
deploy/ # systemd、.env 示例
|
deploy/ # systemd、.env 示例
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -164,9 +198,9 @@ deploy/ # systemd、.env 示例
|
|||||||
|
|
||||||
见 [`.gitea/workflows/secrets.yml`](.gitea/workflows/secrets.yml)。
|
见 [`.gitea/workflows/secrets.yml`](.gitea/workflows/secrets.yml)。
|
||||||
|
|
||||||
- **触发**:任意分支 `push`,且变更路径包含 `crates/**`、`deploy/**`、根目录 `Cargo.toml` / `Cargo.lock`。
|
- **触发**:任意分支 `push`,且变更路径包含 `crates/**`、`deploy/**`、根目录 `Cargo.toml` / `Cargo.lock`、`.gitea/workflows/**`。
|
||||||
- **流水线**:解析 `crates/secrets-mcp/Cargo.toml` 版本 → **若 `secrets-mcp-<version>` 的 tag 已存在则整次运行失败**(避免重复发版)→ 否则自动打 tag → `cargo fmt` / `clippy --locked` / `test --locked` → 交叉编译 `x86_64-unknown-linux-musl` 的 `secrets-mcp`。
|
- **流水线**:解析 `crates/secrets-mcp/Cargo.toml` 版本 → `cargo fmt` / `clippy --locked` / `test --locked` → 交叉编译 `x86_64-unknown-linux-musl` 的 `secrets-mcp` → 构建成功后打 tag `secrets-mcp-<version>`(若远端已存在同名 tag,会先删除再于**当前提交**重建并推送,覆盖式发版)。
|
||||||
- **Release(可选)**:配置仓库 Secret `RELEASE_TOKEN`(Gitea PAT,明文勿 base64)时,会通过 API 创建**草稿** Release、在 Linux 构建成功后上传 `tar.gz` 与 `.sha256`,再自动将草稿**正式发布**;未配置则跳过创建 Release 与产物上传,仅保留 tag 与构建结果。
|
- **Release(可选)**:配置仓库 Secret `RELEASE_TOKEN`(Gitea PAT,明文勿 base64)时,会通过 API **创建或更新**已指向该 tag 的 Release(非 draft)、上传 `tar.gz` 与 `.sha256`;未配置则跳过 API Release,仅 tag + 构建结果。
|
||||||
- **部署(可选)**:仅在 `main`、`feat/mcp` 或 `mcp` 分支且构建成功时,若已配置 `vars.DEPLOY_HOST`、`vars.DEPLOY_USER` 与 `secrets.DEPLOY_SSH_KEY`,则 `deploy-mcp` 通过 SCP/SSH 更新目标机二进制并 `systemctl restart secrets-mcp`。
|
- **部署(可选)**:仅在 `main`、`feat/mcp` 或 `mcp` 分支且构建成功时,若已配置 `vars.DEPLOY_HOST`、`vars.DEPLOY_USER` 与 `secrets.DEPLOY_SSH_KEY`,则 `deploy-mcp` 通过 SCP/SSH 更新目标机二进制并 `systemctl restart secrets-mcp`。
|
||||||
- **通知(可选)**:`vars.WEBHOOK_URL` 为飞书 Webhook 时,构建/部署/发布节点会推送简要状态。
|
- **通知(可选)**:`vars.WEBHOOK_URL` 为飞书 Webhook 时,构建/部署/发布节点会推送简要状态。
|
||||||
|
|
||||||
|
|||||||
@@ -1,37 +1,88 @@
|
|||||||
use serde_json::Value;
|
use serde_json::{Value, json};
|
||||||
use sqlx::{Postgres, Transaction};
|
use sqlx::{PgPool, Postgres, Transaction};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
/// Return the current OS user as the audit actor (falls back to empty string).
|
pub const ACTION_LOGIN: &str = "login";
|
||||||
pub fn current_actor() -> String {
|
pub const FOLDER_AUTH: &str = "auth";
|
||||||
std::env::var("USER").unwrap_or_default()
|
|
||||||
|
fn login_detail(provider: &str, client_ip: Option<&str>, user_agent: Option<&str>) -> Value {
|
||||||
|
json!({
|
||||||
|
"provider": provider,
|
||||||
|
"client_ip": client_ip,
|
||||||
|
"user_agent": user_agent,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write a login audit entry without requiring an explicit transaction.
|
||||||
|
pub async fn log_login(
|
||||||
|
pool: &PgPool,
|
||||||
|
entry_type: &str,
|
||||||
|
provider: &str,
|
||||||
|
user_id: Uuid,
|
||||||
|
client_ip: Option<&str>,
|
||||||
|
user_agent: Option<&str>,
|
||||||
|
) {
|
||||||
|
let detail = login_detail(provider, client_ip, user_agent);
|
||||||
|
let result: Result<_, sqlx::Error> = sqlx::query(
|
||||||
|
"INSERT INTO audit_log (user_id, action, folder, type, name, detail) \
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)",
|
||||||
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(ACTION_LOGIN)
|
||||||
|
.bind(FOLDER_AUTH)
|
||||||
|
.bind(entry_type)
|
||||||
|
.bind(provider)
|
||||||
|
.bind(&detail)
|
||||||
|
.execute(pool)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
if let Err(e) = result {
|
||||||
|
tracing::warn!(error = %e, entry_type, provider, "failed to write login audit log");
|
||||||
|
} else {
|
||||||
|
tracing::debug!(entry_type, provider, ?user_id, "login audit logged");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write an audit entry within an existing transaction.
|
/// Write an audit entry within an existing transaction.
|
||||||
pub async fn log_tx(
|
pub async fn log_tx(
|
||||||
tx: &mut Transaction<'_, Postgres>,
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
user_id: Option<Uuid>,
|
||||||
action: &str,
|
action: &str,
|
||||||
namespace: &str,
|
folder: &str,
|
||||||
kind: &str,
|
entry_type: &str,
|
||||||
name: &str,
|
name: &str,
|
||||||
detail: Value,
|
detail: Value,
|
||||||
) {
|
) {
|
||||||
let actor = current_actor();
|
|
||||||
let result: Result<_, sqlx::Error> = sqlx::query(
|
let result: Result<_, sqlx::Error> = sqlx::query(
|
||||||
"INSERT INTO audit_log (action, namespace, kind, name, detail, actor) \
|
"INSERT INTO audit_log (user_id, action, folder, type, name, detail) \
|
||||||
VALUES ($1, $2, $3, $4, $5, $6)",
|
VALUES ($1, $2, $3, $4, $5, $6)",
|
||||||
)
|
)
|
||||||
|
.bind(user_id)
|
||||||
.bind(action)
|
.bind(action)
|
||||||
.bind(namespace)
|
.bind(folder)
|
||||||
.bind(kind)
|
.bind(entry_type)
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.bind(&detail)
|
.bind(&detail)
|
||||||
.bind(&actor)
|
|
||||||
.execute(&mut **tx)
|
.execute(&mut **tx)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
tracing::warn!(error = %e, "failed to write audit log");
|
tracing::warn!(error = %e, "failed to write audit log");
|
||||||
} else {
|
} else {
|
||||||
tracing::debug!(action, namespace, kind, name, actor, "audit logged");
|
tracing::debug!(action, folder, entry_type, name, "audit logged");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn login_detail_includes_expected_fields() {
|
||||||
|
let detail = login_detail("google", Some("127.0.0.1"), Some("Mozilla/5.0"));
|
||||||
|
|
||||||
|
assert_eq!(detail["provider"], "google");
|
||||||
|
assert_eq!(detail["client_ip"], "127.0.0.1");
|
||||||
|
assert_eq!(detail["user_agent"], "Mozilla/5.0");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,15 @@
|
|||||||
use anyhow::Result;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use sqlx::postgres::PgSslMode;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct DatabaseConfig {
|
||||||
|
pub url: String,
|
||||||
|
pub ssl_mode: Option<PgSslMode>,
|
||||||
|
pub ssl_root_cert: Option<PathBuf>,
|
||||||
|
pub enforce_strict_tls: bool,
|
||||||
|
}
|
||||||
|
|
||||||
/// Resolve database URL from environment.
|
/// Resolve database URL from environment.
|
||||||
/// Priority: `SECRETS_DATABASE_URL` env var → error.
|
/// Priority: `SECRETS_DATABASE_URL` env var → error.
|
||||||
@@ -18,3 +29,54 @@ pub fn resolve_db_url(override_url: &str) -> Result<String> {
|
|||||||
Example: SECRETS_DATABASE_URL=postgres://user:pass@host:port/dbname"
|
Example: SECRETS_DATABASE_URL=postgres://user:pass@host:port/dbname"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn env_var_non_empty(name: &str) -> Option<String> {
|
||||||
|
std::env::var(name)
|
||||||
|
.ok()
|
||||||
|
.filter(|value| !value.trim().is_empty())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_ssl_mode_from_env() -> Result<Option<PgSslMode>> {
|
||||||
|
let Some(mode) = env_var_non_empty("SECRETS_DATABASE_SSL_MODE") else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
|
let parsed = mode.parse::<PgSslMode>().with_context(|| {
|
||||||
|
format!(
|
||||||
|
"Invalid SECRETS_DATABASE_SSL_MODE='{mode}'. Use one of: disable, allow, prefer, require, verify-ca, verify-full."
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
Ok(Some(parsed))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_ssl_root_cert_from_env() -> Result<Option<PathBuf>> {
|
||||||
|
let Some(path) = env_var_non_empty("SECRETS_DATABASE_SSL_ROOT_CERT") else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
let path = PathBuf::from(path);
|
||||||
|
if !path.exists() {
|
||||||
|
anyhow::bail!(
|
||||||
|
"SECRETS_DATABASE_SSL_ROOT_CERT points to a missing file: {}",
|
||||||
|
path.display()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(Some(path))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_production_env() -> bool {
|
||||||
|
matches!(
|
||||||
|
env_var_non_empty("SECRETS_ENV")
|
||||||
|
.as_deref()
|
||||||
|
.map(|value| value.to_ascii_lowercase()),
|
||||||
|
Some(value) if value == "prod" || value == "production"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resolve_db_config(override_url: &str) -> Result<DatabaseConfig> {
|
||||||
|
Ok(DatabaseConfig {
|
||||||
|
url: resolve_db_url(override_url)?,
|
||||||
|
ssl_mode: parse_ssl_mode_from_env()?,
|
||||||
|
ssl_root_cert: resolve_ssl_root_cert_from_env()?,
|
||||||
|
enforce_strict_tls: is_production_env(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -55,35 +55,6 @@ pub fn decrypt_json(master_key: &[u8; 32], data: &[u8]) -> Result<Value> {
|
|||||||
serde_json::from_slice(&bytes).context("deserialize decrypted JSON")
|
serde_json::from_slice(&bytes).context("deserialize decrypted JSON")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Per-user key management (DEPRECATED — kept only for migration) ───────────
|
|
||||||
|
|
||||||
/// Generate a new random 32-byte per-user encryption key.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn generate_user_key() -> [u8; 32] {
|
|
||||||
use aes_gcm::aead::rand_core::RngCore;
|
|
||||||
let mut key = [0u8; 32];
|
|
||||||
OsRng.fill_bytes(&mut key);
|
|
||||||
key
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Wrap a per-user key with the server master key using AES-256-GCM.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn wrap_user_key(server_master_key: &[u8; 32], user_key: &[u8; 32]) -> Result<Vec<u8>> {
|
|
||||||
encrypt(server_master_key, user_key.as_ref())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Unwrap a per-user key using the server master key.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn unwrap_user_key(server_master_key: &[u8; 32], wrapped: &[u8]) -> Result<[u8; 32]> {
|
|
||||||
let bytes = decrypt(server_master_key, wrapped)?;
|
|
||||||
if bytes.len() != 32 {
|
|
||||||
bail!("unwrapped user key has unexpected length {}", bytes.len());
|
|
||||||
}
|
|
||||||
let mut key = [0u8; 32];
|
|
||||||
key.copy_from_slice(&bytes);
|
|
||||||
Ok(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Client-supplied key extraction ──────────────────────────────────────────
|
// ─── Client-supplied key extraction ──────────────────────────────────────────
|
||||||
|
|
||||||
/// Parse a 64-char hex string (from X-Encryption-Key header) into a 32-byte key.
|
/// Parse a 64-char hex string (from X-Encryption-Key header) into a 32-byte key.
|
||||||
@@ -100,33 +71,6 @@ pub fn extract_key_from_hex(hex_str: &str) -> Result<[u8; 32]> {
|
|||||||
Ok(key)
|
Ok(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Server master key ────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
/// Load the server master key from `SERVER_MASTER_KEY` environment variable (64 hex chars).
|
|
||||||
pub fn load_master_key_auto() -> Result<[u8; 32]> {
|
|
||||||
let hex_str = std::env::var("SERVER_MASTER_KEY").map_err(|_| {
|
|
||||||
anyhow::anyhow!(
|
|
||||||
"SERVER_MASTER_KEY is not set. \
|
|
||||||
Generate one with: openssl rand -hex 32"
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
if hex_str.is_empty() {
|
|
||||||
bail!("SERVER_MASTER_KEY is set but empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
let bytes = hex::decode_hex(hex_str.trim())?;
|
|
||||||
if bytes.len() != 32 {
|
|
||||||
bail!(
|
|
||||||
"SERVER_MASTER_KEY must be 64 hex chars (32 bytes), got {} bytes",
|
|
||||||
bytes.len()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
let mut key = [0u8; 32];
|
|
||||||
key.copy_from_slice(&bytes);
|
|
||||||
Ok(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Public hex helpers ───────────────────────────────────────────────────────
|
// ─── Public hex helpers ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
pub mod hex {
|
pub mod hex {
|
||||||
@@ -186,22 +130,4 @@ mod tests {
|
|||||||
let dec = decrypt_json(&key, &enc).unwrap();
|
let dec = decrypt_json(&key, &enc).unwrap();
|
||||||
assert_eq!(dec, value);
|
assert_eq!(dec, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn user_key_wrap_unwrap_roundtrip() {
|
|
||||||
let server_key = [0xABu8; 32];
|
|
||||||
let user_key = [0xCDu8; 32];
|
|
||||||
let wrapped = wrap_user_key(&server_key, &user_key).unwrap();
|
|
||||||
let unwrapped = unwrap_user_key(&server_key, &wrapped).unwrap();
|
|
||||||
assert_eq!(unwrapped, user_key);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn user_key_wrap_wrong_server_key_fails() {
|
|
||||||
let server_key1 = [0xABu8; 32];
|
|
||||||
let server_key2 = [0xEFu8; 32];
|
|
||||||
let user_key = [0xCDu8; 32];
|
|
||||||
let wrapped = wrap_user_key(&server_key1, &user_key).unwrap();
|
|
||||||
assert!(unwrap_user_key(&server_key2, &wrapped).is_err());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,45 @@
|
|||||||
use anyhow::Result;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::{PgConnectOptions, PgPoolOptions, PgSslMode};
|
||||||
|
|
||||||
use crate::audit::current_actor;
|
use crate::config::DatabaseConfig;
|
||||||
|
|
||||||
pub async fn create_pool(database_url: &str) -> Result<PgPool> {
|
fn build_connect_options(config: &DatabaseConfig) -> Result<PgConnectOptions> {
|
||||||
|
let mut options = PgConnectOptions::from_str(&config.url)
|
||||||
|
.with_context(|| "failed to parse SECRETS_DATABASE_URL".to_string())?;
|
||||||
|
|
||||||
|
if let Some(mode) = config.ssl_mode {
|
||||||
|
options = options.ssl_mode(mode);
|
||||||
|
}
|
||||||
|
if let Some(path) = &config.ssl_root_cert {
|
||||||
|
options = options.ssl_root_cert(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.enforce_strict_tls
|
||||||
|
&& !matches!(
|
||||||
|
options.get_ssl_mode(),
|
||||||
|
PgSslMode::VerifyCa | PgSslMode::VerifyFull
|
||||||
|
)
|
||||||
|
{
|
||||||
|
anyhow::bail!(
|
||||||
|
"Refusing to start in production with weak PostgreSQL TLS mode. \
|
||||||
|
Set SECRETS_DATABASE_SSL_MODE=verify-ca or verify-full."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_pool(config: &DatabaseConfig) -> Result<PgPool> {
|
||||||
tracing::debug!("connecting to database");
|
tracing::debug!("connecting to database");
|
||||||
|
let connect_options = build_connect_options(config)?;
|
||||||
let pool = PgPoolOptions::new()
|
let pool = PgPoolOptions::new()
|
||||||
.max_connections(10)
|
.max_connections(10)
|
||||||
.acquire_timeout(std::time::Duration::from_secs(5))
|
.acquire_timeout(std::time::Duration::from_secs(5))
|
||||||
.connect(database_url)
|
.connect_with(connect_options)
|
||||||
.await?;
|
.await?;
|
||||||
tracing::debug!("database connection established");
|
tracing::debug!("database connection established");
|
||||||
Ok(pool)
|
Ok(pool)
|
||||||
@@ -24,9 +53,10 @@ pub async fn migrate(pool: &PgPool) -> Result<()> {
|
|||||||
CREATE TABLE IF NOT EXISTS entries (
|
CREATE TABLE IF NOT EXISTS entries (
|
||||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||||
user_id UUID,
|
user_id UUID,
|
||||||
namespace VARCHAR(64) NOT NULL,
|
folder VARCHAR(128) NOT NULL DEFAULT '',
|
||||||
kind VARCHAR(64) NOT NULL,
|
type VARCHAR(64) NOT NULL DEFAULT '',
|
||||||
name VARCHAR(256) NOT NULL,
|
name VARCHAR(256) NOT NULL,
|
||||||
|
notes TEXT NOT NULL DEFAULT '',
|
||||||
tags TEXT[] NOT NULL DEFAULT '{}',
|
tags TEXT[] NOT NULL DEFAULT '{}',
|
||||||
metadata JSONB NOT NULL DEFAULT '{}',
|
metadata JSONB NOT NULL DEFAULT '{}',
|
||||||
version BIGINT NOT NULL DEFAULT 1,
|
version BIGINT NOT NULL DEFAULT 1,
|
||||||
@@ -36,92 +66,108 @@ pub async fn migrate(pool: &PgPool) -> Result<()> {
|
|||||||
|
|
||||||
-- Legacy unique constraint without user_id (single-user mode)
|
-- Legacy unique constraint without user_id (single-user mode)
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_entries_unique_legacy
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_entries_unique_legacy
|
||||||
ON entries(namespace, kind, name)
|
ON entries(folder, name)
|
||||||
WHERE user_id IS NULL;
|
WHERE user_id IS NULL;
|
||||||
|
|
||||||
-- Multi-user unique constraint
|
-- Multi-user unique constraint
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_entries_unique_user
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_entries_unique_user
|
||||||
ON entries(user_id, namespace, kind, name)
|
ON entries(user_id, folder, name)
|
||||||
WHERE user_id IS NOT NULL;
|
WHERE user_id IS NOT NULL;
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_entries_namespace ON entries(namespace);
|
CREATE INDEX IF NOT EXISTS idx_entries_folder ON entries(folder) WHERE folder <> '';
|
||||||
CREATE INDEX IF NOT EXISTS idx_entries_kind ON entries(kind);
|
CREATE INDEX IF NOT EXISTS idx_entries_type ON entries(type) WHERE type <> '';
|
||||||
CREATE INDEX IF NOT EXISTS idx_entries_user_id ON entries(user_id) WHERE user_id IS NOT NULL;
|
CREATE INDEX IF NOT EXISTS idx_entries_user_id ON entries(user_id) WHERE user_id IS NOT NULL;
|
||||||
CREATE INDEX IF NOT EXISTS idx_entries_tags ON entries USING GIN(tags);
|
CREATE INDEX IF NOT EXISTS idx_entries_tags ON entries USING GIN(tags);
|
||||||
CREATE INDEX IF NOT EXISTS idx_entries_metadata ON entries USING GIN(metadata jsonb_path_ops);
|
CREATE INDEX IF NOT EXISTS idx_entries_metadata ON entries USING GIN(metadata jsonb_path_ops);
|
||||||
|
|
||||||
-- ── secrets: one row per encrypted field ─────────────────────────────────
|
-- ── secrets: one row per encrypted field ─────────────────────────────────
|
||||||
CREATE TABLE IF NOT EXISTS secrets (
|
CREATE TABLE IF NOT EXISTS secrets (
|
||||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||||
entry_id UUID NOT NULL REFERENCES entries(id) ON DELETE CASCADE,
|
user_id UUID,
|
||||||
field_name VARCHAR(256) NOT NULL,
|
name VARCHAR(256) NOT NULL,
|
||||||
|
type VARCHAR(64) NOT NULL DEFAULT 'text',
|
||||||
encrypted BYTEA NOT NULL DEFAULT '\x',
|
encrypted BYTEA NOT NULL DEFAULT '\x',
|
||||||
version BIGINT NOT NULL DEFAULT 1,
|
version BIGINT NOT NULL DEFAULT 1,
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
UNIQUE(entry_id, field_name)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_secrets_entry_id ON secrets(entry_id);
|
CREATE INDEX IF NOT EXISTS idx_secrets_user_id ON secrets(user_id) WHERE user_id IS NOT NULL;
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_secrets_unique_user_name
|
||||||
|
ON secrets(user_id, name) WHERE user_id IS NOT NULL;
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_secrets_name ON secrets(name);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_secrets_type ON secrets(type);
|
||||||
|
|
||||||
|
-- ── entry_secrets: N:N relation ────────────────────────────────────────────
|
||||||
|
CREATE TABLE IF NOT EXISTS entry_secrets (
|
||||||
|
entry_id UUID NOT NULL REFERENCES entries(id) ON DELETE CASCADE,
|
||||||
|
secret_id UUID NOT NULL REFERENCES secrets(id) ON DELETE CASCADE,
|
||||||
|
sort_order INT NOT NULL DEFAULT 0,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
PRIMARY KEY(entry_id, secret_id)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_entry_secrets_secret_id ON entry_secrets(secret_id);
|
||||||
|
|
||||||
-- ── audit_log: append-only operation log ─────────────────────────────────
|
-- ── audit_log: append-only operation log ─────────────────────────────────
|
||||||
CREATE TABLE IF NOT EXISTS audit_log (
|
CREATE TABLE IF NOT EXISTS audit_log (
|
||||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||||
|
user_id UUID,
|
||||||
action VARCHAR(32) NOT NULL,
|
action VARCHAR(32) NOT NULL,
|
||||||
namespace VARCHAR(64) NOT NULL,
|
folder VARCHAR(128) NOT NULL DEFAULT '',
|
||||||
kind VARCHAR(64) NOT NULL,
|
type VARCHAR(64) NOT NULL DEFAULT '',
|
||||||
name VARCHAR(256) NOT NULL,
|
name VARCHAR(256) NOT NULL,
|
||||||
detail JSONB NOT NULL DEFAULT '{}',
|
detail JSONB NOT NULL DEFAULT '{}',
|
||||||
actor VARCHAR(128) NOT NULL DEFAULT '',
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_audit_log_created ON audit_log(created_at DESC);
|
CREATE INDEX IF NOT EXISTS idx_audit_log_created ON audit_log(created_at DESC);
|
||||||
CREATE INDEX IF NOT EXISTS idx_audit_log_ns_kind ON audit_log(namespace, kind);
|
CREATE INDEX IF NOT EXISTS idx_audit_log_folder_type ON audit_log(folder, type);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_audit_log_user_id ON audit_log(user_id) WHERE user_id IS NOT NULL;
|
||||||
|
|
||||||
-- ── entries_history ───────────────────────────────────────────────────────
|
-- ── entries_history ───────────────────────────────────────────────────────
|
||||||
CREATE TABLE IF NOT EXISTS entries_history (
|
CREATE TABLE IF NOT EXISTS entries_history (
|
||||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||||
entry_id UUID NOT NULL,
|
entry_id UUID NOT NULL,
|
||||||
namespace VARCHAR(64) NOT NULL,
|
folder VARCHAR(128) NOT NULL DEFAULT '',
|
||||||
kind VARCHAR(64) NOT NULL,
|
type VARCHAR(64) NOT NULL DEFAULT '',
|
||||||
name VARCHAR(256) NOT NULL,
|
name VARCHAR(256) NOT NULL,
|
||||||
version BIGINT NOT NULL,
|
version BIGINT NOT NULL,
|
||||||
action VARCHAR(16) NOT NULL,
|
action VARCHAR(16) NOT NULL,
|
||||||
tags TEXT[] NOT NULL DEFAULT '{}',
|
tags TEXT[] NOT NULL DEFAULT '{}',
|
||||||
metadata JSONB NOT NULL DEFAULT '{}',
|
metadata JSONB NOT NULL DEFAULT '{}',
|
||||||
actor VARCHAR(128) NOT NULL DEFAULT '',
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_entries_history_entry_id
|
CREATE INDEX IF NOT EXISTS idx_entries_history_entry_id
|
||||||
ON entries_history(entry_id, version DESC);
|
ON entries_history(entry_id, version DESC);
|
||||||
CREATE INDEX IF NOT EXISTS idx_entries_history_ns_kind_name
|
CREATE INDEX IF NOT EXISTS idx_entries_history_folder_type_name
|
||||||
ON entries_history(namespace, kind, name, version DESC);
|
ON entries_history(folder, type, name, version DESC);
|
||||||
|
|
||||||
-- Backfill: add user_id to entries_history for multi-tenant isolation
|
-- Backfill: add user_id to entries_history for multi-tenant isolation
|
||||||
ALTER TABLE entries_history ADD COLUMN IF NOT EXISTS user_id UUID;
|
ALTER TABLE entries_history ADD COLUMN IF NOT EXISTS user_id UUID;
|
||||||
CREATE INDEX IF NOT EXISTS idx_entries_history_user_id
|
CREATE INDEX IF NOT EXISTS idx_entries_history_user_id
|
||||||
ON entries_history(user_id) WHERE user_id IS NOT NULL;
|
ON entries_history(user_id) WHERE user_id IS NOT NULL;
|
||||||
|
ALTER TABLE entries_history DROP COLUMN IF EXISTS actor;
|
||||||
|
|
||||||
|
-- Backfill: add notes to entries if not present (fresh installs already have it)
|
||||||
|
ALTER TABLE entries ADD COLUMN IF NOT EXISTS notes TEXT NOT NULL DEFAULT '';
|
||||||
|
|
||||||
-- ── secrets_history: field-level snapshot ────────────────────────────────
|
-- ── secrets_history: field-level snapshot ────────────────────────────────
|
||||||
CREATE TABLE IF NOT EXISTS secrets_history (
|
CREATE TABLE IF NOT EXISTS secrets_history (
|
||||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||||
entry_id UUID NOT NULL,
|
|
||||||
secret_id UUID NOT NULL,
|
secret_id UUID NOT NULL,
|
||||||
entry_version BIGINT NOT NULL,
|
name VARCHAR(256) NOT NULL,
|
||||||
field_name VARCHAR(256) NOT NULL,
|
|
||||||
encrypted BYTEA NOT NULL DEFAULT '\x',
|
encrypted BYTEA NOT NULL DEFAULT '\x',
|
||||||
action VARCHAR(16) NOT NULL,
|
action VARCHAR(16) NOT NULL,
|
||||||
actor VARCHAR(128) NOT NULL DEFAULT '',
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_secrets_history_entry_id
|
|
||||||
ON secrets_history(entry_id, entry_version DESC);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_secrets_history_secret_id
|
CREATE INDEX IF NOT EXISTS idx_secrets_history_secret_id
|
||||||
ON secrets_history(secret_id);
|
ON secrets_history(secret_id);
|
||||||
|
|
||||||
|
-- Drop redundant actor column (derivable via entries_history JOIN)
|
||||||
|
ALTER TABLE secrets_history DROP COLUMN IF EXISTS actor;
|
||||||
|
|
||||||
-- ── users ─────────────────────────────────────────────────────────────────
|
-- ── users ─────────────────────────────────────────────────────────────────
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||||
@@ -152,21 +198,294 @@ pub async fn migrate(pool: &PgPool) -> Result<()> {
|
|||||||
CREATE INDEX IF NOT EXISTS idx_oauth_accounts_user ON oauth_accounts(user_id);
|
CREATE INDEX IF NOT EXISTS idx_oauth_accounts_user ON oauth_accounts(user_id);
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_oauth_accounts_user_provider
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_oauth_accounts_user_provider
|
||||||
ON oauth_accounts(user_id, provider);
|
ON oauth_accounts(user_id, provider);
|
||||||
|
|
||||||
|
-- FK: user_id columns -> users(id) (nullable = legacy rows; ON DELETE SET NULL)
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint WHERE conname = 'fk_entries_user_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE entries
|
||||||
|
ADD CONSTRAINT fk_entries_user_id
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint WHERE conname = 'fk_entries_history_user_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE entries_history
|
||||||
|
ADD CONSTRAINT fk_entries_history_user_id
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint WHERE conname = 'fk_secrets_user_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE secrets
|
||||||
|
ADD CONSTRAINT fk_secrets_user_id
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint WHERE conname = 'fk_audit_log_user_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE audit_log
|
||||||
|
ADD CONSTRAINT fk_audit_log_user_id
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
migrate_schema(pool).await?;
|
||||||
|
restore_plaintext_api_keys(pool).await?;
|
||||||
|
|
||||||
tracing::debug!("migrations complete");
|
tracing::debug!("migrations complete");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Idempotent schema migration: rename namespace→folder, kind→type in existing databases.
|
||||||
|
async fn migrate_schema(pool: &PgPool) -> Result<()> {
|
||||||
|
sqlx::raw_sql(
|
||||||
|
r#"
|
||||||
|
-- ── entries: rename namespace→folder, kind→type ──────────────────────────
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries' AND column_name = 'namespace'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE entries RENAME COLUMN namespace TO folder;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries' AND column_name = 'kind'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE entries RENAME COLUMN kind TO type;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- ── audit_log: rename namespace→folder, kind→type ────────────────────────
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'audit_log' AND column_name = 'namespace'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE audit_log RENAME COLUMN namespace TO folder;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'audit_log' AND column_name = 'kind'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE audit_log RENAME COLUMN kind TO type;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- ── entries_history: rename namespace→folder, kind→type ──────────────────
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries_history' AND column_name = 'namespace'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE entries_history RENAME COLUMN namespace TO folder;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries_history' AND column_name = 'kind'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE entries_history RENAME COLUMN kind TO type;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- ── Set empty defaults for new folder/type columns ────────────────────────
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries' AND column_name = 'folder'
|
||||||
|
) THEN
|
||||||
|
UPDATE entries SET folder = '' WHERE folder IS NULL;
|
||||||
|
ALTER TABLE entries ALTER COLUMN folder SET NOT NULL;
|
||||||
|
ALTER TABLE entries ALTER COLUMN folder SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries' AND column_name = 'type'
|
||||||
|
) THEN
|
||||||
|
UPDATE entries SET type = '' WHERE type IS NULL;
|
||||||
|
ALTER TABLE entries ALTER COLUMN type SET NOT NULL;
|
||||||
|
ALTER TABLE entries ALTER COLUMN type SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'audit_log' AND column_name = 'folder'
|
||||||
|
) THEN
|
||||||
|
UPDATE audit_log SET folder = '' WHERE folder IS NULL;
|
||||||
|
ALTER TABLE audit_log ALTER COLUMN folder SET NOT NULL;
|
||||||
|
ALTER TABLE audit_log ALTER COLUMN folder SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'audit_log' AND column_name = 'type'
|
||||||
|
) THEN
|
||||||
|
UPDATE audit_log SET type = '' WHERE type IS NULL;
|
||||||
|
ALTER TABLE audit_log ALTER COLUMN type SET NOT NULL;
|
||||||
|
ALTER TABLE audit_log ALTER COLUMN type SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries_history' AND column_name = 'folder'
|
||||||
|
) THEN
|
||||||
|
UPDATE entries_history SET folder = '' WHERE folder IS NULL;
|
||||||
|
ALTER TABLE entries_history ALTER COLUMN folder SET NOT NULL;
|
||||||
|
ALTER TABLE entries_history ALTER COLUMN folder SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries_history' AND column_name = 'type'
|
||||||
|
) THEN
|
||||||
|
UPDATE entries_history SET type = '' WHERE type IS NULL;
|
||||||
|
ALTER TABLE entries_history ALTER COLUMN type SET NOT NULL;
|
||||||
|
ALTER TABLE entries_history ALTER COLUMN type SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- ── Rebuild unique indexes on entries: folder is now part of the key ────────
|
||||||
|
-- (user_id, folder, name) allows same name in different folders.
|
||||||
|
DROP INDEX IF EXISTS idx_entries_unique_legacy;
|
||||||
|
DROP INDEX IF EXISTS idx_entries_unique_user;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_entries_unique_legacy
|
||||||
|
ON entries(folder, name)
|
||||||
|
WHERE user_id IS NULL;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_entries_unique_user
|
||||||
|
ON entries(user_id, folder, name)
|
||||||
|
WHERE user_id IS NOT NULL;
|
||||||
|
|
||||||
|
-- ── Replace old namespace/kind indexes ────────────────────────────────────
|
||||||
|
DROP INDEX IF EXISTS idx_entries_namespace;
|
||||||
|
DROP INDEX IF EXISTS idx_entries_kind;
|
||||||
|
DROP INDEX IF EXISTS idx_audit_log_ns_kind;
|
||||||
|
DROP INDEX IF EXISTS idx_entries_history_ns_kind_name;
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_entries_folder
|
||||||
|
ON entries(folder) WHERE folder <> '';
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_entries_type
|
||||||
|
ON entries(type) WHERE type <> '';
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_audit_log_folder_type
|
||||||
|
ON audit_log(folder, type);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_entries_history_folder_type_name
|
||||||
|
ON entries_history(folder, type, name, version DESC);
|
||||||
|
|
||||||
|
-- ── Drop legacy actor columns ─────────────────────────────────────────────
|
||||||
|
ALTER TABLE secrets_history DROP COLUMN IF EXISTS actor;
|
||||||
|
ALTER TABLE audit_log DROP COLUMN IF EXISTS actor;
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn restore_plaintext_api_keys(pool: &PgPool) -> Result<()> {
|
||||||
|
let has_users_api_key: bool = sqlx::query_scalar(
|
||||||
|
"SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema = 'public'
|
||||||
|
AND table_name = 'users'
|
||||||
|
AND column_name = 'api_key'
|
||||||
|
)",
|
||||||
|
)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if !has_users_api_key {
|
||||||
|
sqlx::query("ALTER TABLE users ADD COLUMN api_key TEXT")
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
sqlx::query("CREATE UNIQUE INDEX IF NOT EXISTS idx_users_api_key ON users(api_key) WHERE api_key IS NOT NULL")
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let has_api_keys_table: bool = sqlx::query_scalar(
|
||||||
|
"SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.tables
|
||||||
|
WHERE table_schema = 'public'
|
||||||
|
AND table_name = 'api_keys'
|
||||||
|
)",
|
||||||
|
)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if !has_api_keys_table {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow)]
|
||||||
|
struct UserWithoutKey {
|
||||||
|
id: uuid::Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
let users_without_key: Vec<UserWithoutKey> =
|
||||||
|
sqlx::query_as("SELECT DISTINCT user_id AS id FROM api_keys WHERE user_id NOT IN (SELECT id FROM users WHERE api_key IS NOT NULL)")
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
for user in users_without_key {
|
||||||
|
let new_key = crate::service::api_key::generate_api_key();
|
||||||
|
sqlx::query("UPDATE users SET api_key = $1 WHERE id = $2")
|
||||||
|
.bind(&new_key)
|
||||||
|
.bind(user.id)
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlx::query("DROP TABLE IF EXISTS api_keys")
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// ── Entry-level history snapshot ─────────────────────────────────────────────
|
// ── Entry-level history snapshot ─────────────────────────────────────────────
|
||||||
|
|
||||||
pub struct EntrySnapshotParams<'a> {
|
pub struct EntrySnapshotParams<'a> {
|
||||||
pub entry_id: uuid::Uuid,
|
pub entry_id: uuid::Uuid,
|
||||||
pub user_id: Option<uuid::Uuid>,
|
pub user_id: Option<uuid::Uuid>,
|
||||||
pub namespace: &'a str,
|
pub folder: &'a str,
|
||||||
pub kind: &'a str,
|
pub entry_type: &'a str,
|
||||||
pub name: &'a str,
|
pub name: &'a str,
|
||||||
pub version: i64,
|
pub version: i64,
|
||||||
pub action: &'a str,
|
pub action: &'a str,
|
||||||
@@ -178,21 +497,19 @@ pub async fn snapshot_entry_history(
|
|||||||
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||||
p: EntrySnapshotParams<'_>,
|
p: EntrySnapshotParams<'_>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let actor = current_actor();
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"INSERT INTO entries_history \
|
"INSERT INTO entries_history \
|
||||||
(entry_id, namespace, kind, name, version, action, tags, metadata, actor, user_id) \
|
(entry_id, folder, type, name, version, action, tags, metadata, user_id) \
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
||||||
)
|
)
|
||||||
.bind(p.entry_id)
|
.bind(p.entry_id)
|
||||||
.bind(p.namespace)
|
.bind(p.folder)
|
||||||
.bind(p.kind)
|
.bind(p.entry_type)
|
||||||
.bind(p.name)
|
.bind(p.name)
|
||||||
.bind(p.version)
|
.bind(p.version)
|
||||||
.bind(p.action)
|
.bind(p.action)
|
||||||
.bind(p.tags)
|
.bind(p.tags)
|
||||||
.bind(p.metadata)
|
.bind(p.metadata)
|
||||||
.bind(&actor)
|
|
||||||
.bind(p.user_id)
|
.bind(p.user_id)
|
||||||
.execute(&mut **tx)
|
.execute(&mut **tx)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -202,10 +519,8 @@ pub async fn snapshot_entry_history(
|
|||||||
// ── Secret field-level history snapshot ──────────────────────────────────────
|
// ── Secret field-level history snapshot ──────────────────────────────────────
|
||||||
|
|
||||||
pub struct SecretSnapshotParams<'a> {
|
pub struct SecretSnapshotParams<'a> {
|
||||||
pub entry_id: uuid::Uuid,
|
|
||||||
pub secret_id: uuid::Uuid,
|
pub secret_id: uuid::Uuid,
|
||||||
pub entry_version: i64,
|
pub name: &'a str,
|
||||||
pub field_name: &'a str,
|
|
||||||
pub encrypted: &'a [u8],
|
pub encrypted: &'a [u8],
|
||||||
pub action: &'a str,
|
pub action: &'a str,
|
||||||
}
|
}
|
||||||
@@ -214,19 +529,15 @@ pub async fn snapshot_secret_history(
|
|||||||
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||||
p: SecretSnapshotParams<'_>,
|
p: SecretSnapshotParams<'_>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let actor = current_actor();
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"INSERT INTO secrets_history \
|
"INSERT INTO secrets_history \
|
||||||
(entry_id, secret_id, entry_version, field_name, encrypted, action, actor) \
|
(secret_id, name, encrypted, action) \
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7)",
|
VALUES ($1, $2, $3, $4)",
|
||||||
)
|
)
|
||||||
.bind(p.entry_id)
|
|
||||||
.bind(p.secret_id)
|
.bind(p.secret_id)
|
||||||
.bind(p.entry_version)
|
.bind(p.name)
|
||||||
.bind(p.field_name)
|
|
||||||
.bind(p.encrypted)
|
.bind(p.encrypted)
|
||||||
.bind(p.action)
|
.bind(p.action)
|
||||||
.bind(&actor)
|
|
||||||
.execute(&mut **tx)
|
.execute(&mut **tx)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -4,14 +4,18 @@ use serde_json::Value;
|
|||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
/// A top-level entry (server, service, key, …).
|
/// A top-level entry (server, service, key, person, …).
|
||||||
/// Sensitive fields are stored separately in `secrets`.
|
/// Sensitive fields are stored separately in `secrets`.
|
||||||
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
|
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
|
||||||
pub struct Entry {
|
pub struct Entry {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub namespace: String,
|
pub user_id: Option<Uuid>,
|
||||||
pub kind: String,
|
pub folder: String,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
pub entry_type: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub notes: String,
|
||||||
pub tags: Vec<String>,
|
pub tags: Vec<String>,
|
||||||
pub metadata: Value,
|
pub metadata: Value,
|
||||||
pub version: i64,
|
pub version: i64,
|
||||||
@@ -23,8 +27,11 @@ pub struct Entry {
|
|||||||
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
|
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
|
||||||
pub struct SecretField {
|
pub struct SecretField {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub entry_id: Uuid,
|
pub user_id: Option<Uuid>,
|
||||||
pub field_name: String,
|
pub name: String,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
pub secret_type: String,
|
||||||
/// AES-256-GCM ciphertext: nonce(12B) || ciphertext+tag
|
/// AES-256-GCM ciphertext: nonce(12B) || ciphertext+tag
|
||||||
pub encrypted: Vec<u8>,
|
pub encrypted: Vec<u8>,
|
||||||
pub version: i64,
|
pub version: i64,
|
||||||
@@ -39,15 +46,47 @@ pub struct SecretField {
|
|||||||
pub struct EntryRow {
|
pub struct EntryRow {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub version: i64,
|
pub version: i64,
|
||||||
|
pub folder: String,
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
pub entry_type: String,
|
||||||
pub tags: Vec<String>,
|
pub tags: Vec<String>,
|
||||||
pub metadata: Value,
|
pub metadata: Value,
|
||||||
|
pub notes: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Entry row including `name` (used for id-scoped web / service updates).
|
||||||
|
#[derive(Debug, sqlx::FromRow)]
|
||||||
|
pub struct EntryWriteRow {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub version: i64,
|
||||||
|
pub folder: String,
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
pub entry_type: String,
|
||||||
|
pub name: String,
|
||||||
|
pub tags: Vec<String>,
|
||||||
|
pub metadata: Value,
|
||||||
|
pub notes: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&EntryWriteRow> for EntryRow {
|
||||||
|
fn from(r: &EntryWriteRow) -> Self {
|
||||||
|
EntryRow {
|
||||||
|
id: r.id,
|
||||||
|
version: r.version,
|
||||||
|
folder: r.folder.clone(),
|
||||||
|
entry_type: r.entry_type.clone(),
|
||||||
|
tags: r.tags.clone(),
|
||||||
|
metadata: r.metadata.clone(),
|
||||||
|
notes: r.notes.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Minimal secret field row fetched before snapshots or cascade deletes.
|
/// Minimal secret field row fetched before snapshots or cascade deletes.
|
||||||
#[derive(Debug, sqlx::FromRow)]
|
#[derive(Debug, sqlx::FromRow)]
|
||||||
pub struct SecretFieldRow {
|
pub struct SecretFieldRow {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub field_name: String,
|
pub name: String,
|
||||||
pub encrypted: Vec<u8>,
|
pub encrypted: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,10 +166,14 @@ pub struct ExportData {
|
|||||||
/// A single entry with decrypted secrets for export/import.
|
/// A single entry with decrypted secrets for export/import.
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct ExportEntry {
|
pub struct ExportEntry {
|
||||||
pub namespace: String,
|
|
||||||
pub kind: String,
|
|
||||||
pub name: String,
|
pub name: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
pub folder: String,
|
||||||
|
#[serde(default, rename = "type")]
|
||||||
|
pub entry_type: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub notes: String,
|
||||||
|
#[serde(default)]
|
||||||
pub tags: Vec<String>,
|
pub tags: Vec<String>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub metadata: Value,
|
pub metadata: Value,
|
||||||
@@ -174,6 +217,21 @@ pub struct OauthAccount {
|
|||||||
pub created_at: DateTime<Utc>,
|
pub created_at: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A single audit log row, optionally scoped to a business user.
|
||||||
|
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
|
||||||
|
pub struct AuditLogEntry {
|
||||||
|
pub id: i64,
|
||||||
|
pub user_id: Option<Uuid>,
|
||||||
|
pub action: String,
|
||||||
|
pub folder: String,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
pub entry_type: String,
|
||||||
|
pub name: String,
|
||||||
|
pub detail: Value,
|
||||||
|
pub created_at: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
// ── TOML ↔ JSON value conversion ──────────────────────────────────────────────
|
// ── TOML ↔ JSON value conversion ──────────────────────────────────────────────
|
||||||
|
|
||||||
/// Convert a serde_json Value to a toml Value.
|
/// Convert a serde_json Value to a toml Value.
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use serde_json::{Map, Value};
|
use serde_json::{Map, Value};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
use std::collections::{BTreeSet, HashSet};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@@ -159,21 +160,24 @@ pub fn flatten_json_fields(prefix: &str, value: &Value) -> Vec<(String, Value)>
|
|||||||
|
|
||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
pub struct AddResult {
|
pub struct AddResult {
|
||||||
pub namespace: String,
|
|
||||||
pub kind: String,
|
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub folder: String,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
pub entry_type: String,
|
||||||
pub tags: Vec<String>,
|
pub tags: Vec<String>,
|
||||||
pub meta_keys: Vec<String>,
|
pub meta_keys: Vec<String>,
|
||||||
pub secret_keys: Vec<String>,
|
pub secret_keys: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AddParams<'a> {
|
pub struct AddParams<'a> {
|
||||||
pub namespace: &'a str,
|
|
||||||
pub kind: &'a str,
|
|
||||||
pub name: &'a str,
|
pub name: &'a str,
|
||||||
|
pub folder: &'a str,
|
||||||
|
pub entry_type: &'a str,
|
||||||
|
pub notes: &'a str,
|
||||||
pub tags: &'a [String],
|
pub tags: &'a [String],
|
||||||
pub meta_entries: &'a [String],
|
pub meta_entries: &'a [String],
|
||||||
pub secret_entries: &'a [String],
|
pub secret_entries: &'a [String],
|
||||||
|
pub link_secret_names: &'a [String],
|
||||||
/// Optional user_id for multi-user isolation (None = single-user CLI mode)
|
/// Optional user_id for multi-user isolation (None = single-user CLI mode)
|
||||||
pub user_id: Option<Uuid>,
|
pub user_id: Option<Uuid>,
|
||||||
}
|
}
|
||||||
@@ -183,28 +187,31 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
let secret_json = build_json(params.secret_entries)?;
|
let secret_json = build_json(params.secret_entries)?;
|
||||||
let meta_keys = collect_key_paths(params.meta_entries)?;
|
let meta_keys = collect_key_paths(params.meta_entries)?;
|
||||||
let secret_keys = collect_key_paths(params.secret_entries)?;
|
let secret_keys = collect_key_paths(params.secret_entries)?;
|
||||||
|
let flat_fields = flatten_json_fields("", &secret_json);
|
||||||
|
let new_secret_names: BTreeSet<String> =
|
||||||
|
flat_fields.iter().map(|(name, _)| name.clone()).collect();
|
||||||
|
let link_secret_names =
|
||||||
|
validate_link_secret_names(params.link_secret_names, &new_secret_names)?;
|
||||||
|
|
||||||
let mut tx = pool.begin().await?;
|
let mut tx = pool.begin().await?;
|
||||||
|
|
||||||
// Fetch existing entry (user-scoped or global depending on user_id)
|
// Fetch existing entry by (user_id, folder, name) — the natural unique key
|
||||||
let existing: Option<EntryRow> = if let Some(uid) = params.user_id {
|
let existing: Option<EntryRow> = if let Some(uid) = params.user_id {
|
||||||
sqlx::query_as(
|
sqlx::query_as(
|
||||||
"SELECT id, version, tags, metadata FROM entries \
|
"SELECT id, version, folder, type, tags, metadata, notes FROM entries \
|
||||||
WHERE user_id = $1 AND namespace = $2 AND kind = $3 AND name = $4",
|
WHERE user_id = $1 AND folder = $2 AND name = $3",
|
||||||
)
|
)
|
||||||
.bind(uid)
|
.bind(uid)
|
||||||
.bind(params.namespace)
|
.bind(params.folder)
|
||||||
.bind(params.kind)
|
|
||||||
.bind(params.name)
|
.bind(params.name)
|
||||||
.fetch_optional(&mut *tx)
|
.fetch_optional(&mut *tx)
|
||||||
.await?
|
.await?
|
||||||
} else {
|
} else {
|
||||||
sqlx::query_as(
|
sqlx::query_as(
|
||||||
"SELECT id, version, tags, metadata FROM entries \
|
"SELECT id, version, folder, type, tags, metadata, notes FROM entries \
|
||||||
WHERE user_id IS NULL AND namespace = $1 AND kind = $2 AND name = $3",
|
WHERE user_id IS NULL AND folder = $1 AND name = $2",
|
||||||
)
|
)
|
||||||
.bind(params.namespace)
|
.bind(params.folder)
|
||||||
.bind(params.kind)
|
|
||||||
.bind(params.name)
|
.bind(params.name)
|
||||||
.fetch_optional(&mut *tx)
|
.fetch_optional(&mut *tx)
|
||||||
.await?
|
.await?
|
||||||
@@ -216,8 +223,8 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
db::EntrySnapshotParams {
|
db::EntrySnapshotParams {
|
||||||
entry_id: ex.id,
|
entry_id: ex.id,
|
||||||
user_id: params.user_id,
|
user_id: params.user_id,
|
||||||
namespace: params.namespace,
|
folder: params.folder,
|
||||||
kind: params.kind,
|
entry_type: params.entry_type,
|
||||||
name: params.name,
|
name: params.name,
|
||||||
version: ex.version,
|
version: ex.version,
|
||||||
action: "add",
|
action: "add",
|
||||||
@@ -232,10 +239,13 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
|
|
||||||
let entry_id: Uuid = if let Some(uid) = params.user_id {
|
let entry_id: Uuid = if let Some(uid) = params.user_id {
|
||||||
sqlx::query_scalar(
|
sqlx::query_scalar(
|
||||||
r#"INSERT INTO entries (user_id, namespace, kind, name, tags, metadata, version, updated_at)
|
r#"INSERT INTO entries (user_id, folder, type, name, notes, tags, metadata, version, updated_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, 1, NOW())
|
VALUES ($1, $2, $3, $4, $5, $6, $7, 1, NOW())
|
||||||
ON CONFLICT (user_id, namespace, kind, name) WHERE user_id IS NOT NULL
|
ON CONFLICT (user_id, folder, name) WHERE user_id IS NOT NULL
|
||||||
DO UPDATE SET
|
DO UPDATE SET
|
||||||
|
folder = EXCLUDED.folder,
|
||||||
|
type = EXCLUDED.type,
|
||||||
|
notes = EXCLUDED.notes,
|
||||||
tags = EXCLUDED.tags,
|
tags = EXCLUDED.tags,
|
||||||
metadata = EXCLUDED.metadata,
|
metadata = EXCLUDED.metadata,
|
||||||
version = entries.version + 1,
|
version = entries.version + 1,
|
||||||
@@ -243,38 +253,44 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
RETURNING id"#,
|
RETURNING id"#,
|
||||||
)
|
)
|
||||||
.bind(uid)
|
.bind(uid)
|
||||||
.bind(params.namespace)
|
.bind(params.folder)
|
||||||
.bind(params.kind)
|
.bind(params.entry_type)
|
||||||
.bind(params.name)
|
.bind(params.name)
|
||||||
|
.bind(params.notes)
|
||||||
.bind(params.tags)
|
.bind(params.tags)
|
||||||
.bind(&metadata)
|
.bind(&metadata)
|
||||||
.fetch_one(&mut *tx)
|
.fetch_one(&mut *tx)
|
||||||
.await?
|
.await?
|
||||||
} else {
|
} else {
|
||||||
sqlx::query_scalar(
|
sqlx::query_scalar(
|
||||||
r#"INSERT INTO entries (namespace, kind, name, tags, metadata, version, updated_at)
|
r#"INSERT INTO entries (folder, type, name, notes, tags, metadata, version, updated_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, 1, NOW())
|
VALUES ($1, $2, $3, $4, $5, $6, 1, NOW())
|
||||||
ON CONFLICT (namespace, kind, name) WHERE user_id IS NULL
|
ON CONFLICT (folder, name) WHERE user_id IS NULL
|
||||||
DO UPDATE SET
|
DO UPDATE SET
|
||||||
|
folder = EXCLUDED.folder,
|
||||||
|
type = EXCLUDED.type,
|
||||||
|
notes = EXCLUDED.notes,
|
||||||
tags = EXCLUDED.tags,
|
tags = EXCLUDED.tags,
|
||||||
metadata = EXCLUDED.metadata,
|
metadata = EXCLUDED.metadata,
|
||||||
version = entries.version + 1,
|
version = entries.version + 1,
|
||||||
updated_at = NOW()
|
updated_at = NOW()
|
||||||
RETURNING id"#,
|
RETURNING id"#,
|
||||||
)
|
)
|
||||||
.bind(params.namespace)
|
.bind(params.folder)
|
||||||
.bind(params.kind)
|
.bind(params.entry_type)
|
||||||
.bind(params.name)
|
.bind(params.name)
|
||||||
|
.bind(params.notes)
|
||||||
.bind(params.tags)
|
.bind(params.tags)
|
||||||
.bind(&metadata)
|
.bind(&metadata)
|
||||||
.fetch_one(&mut *tx)
|
.fetch_one(&mut *tx)
|
||||||
.await?
|
.await?
|
||||||
};
|
};
|
||||||
|
|
||||||
let new_entry_version: i64 = sqlx::query_scalar("SELECT version FROM entries WHERE id = $1")
|
let current_entry_version: i64 =
|
||||||
.bind(entry_id)
|
sqlx::query_scalar("SELECT version FROM entries WHERE id = $1")
|
||||||
.fetch_one(&mut *tx)
|
.bind(entry_id)
|
||||||
.await?;
|
.fetch_one(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
if existing.is_none()
|
if existing.is_none()
|
||||||
&& let Err(e) = db::snapshot_entry_history(
|
&& let Err(e) = db::snapshot_entry_history(
|
||||||
@@ -282,10 +298,10 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
db::EntrySnapshotParams {
|
db::EntrySnapshotParams {
|
||||||
entry_id,
|
entry_id,
|
||||||
user_id: params.user_id,
|
user_id: params.user_id,
|
||||||
namespace: params.namespace,
|
folder: params.folder,
|
||||||
kind: params.kind,
|
entry_type: params.entry_type,
|
||||||
name: params.name,
|
name: params.name,
|
||||||
version: new_entry_version,
|
version: current_entry_version,
|
||||||
action: "create",
|
action: "create",
|
||||||
tags: params.tags,
|
tags: params.tags,
|
||||||
metadata: &metadata,
|
metadata: &metadata,
|
||||||
@@ -300,23 +316,25 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
struct ExistingField {
|
struct ExistingField {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
field_name: String,
|
name: String,
|
||||||
encrypted: Vec<u8>,
|
encrypted: Vec<u8>,
|
||||||
}
|
}
|
||||||
let existing_fields: Vec<ExistingField> =
|
let existing_fields: Vec<ExistingField> = sqlx::query_as(
|
||||||
sqlx::query_as("SELECT id, field_name, encrypted FROM secrets WHERE entry_id = $1")
|
"SELECT s.id, s.name, s.encrypted \
|
||||||
.bind(entry_id)
|
FROM entry_secrets es \
|
||||||
.fetch_all(&mut *tx)
|
JOIN secrets s ON s.id = es.secret_id \
|
||||||
.await?;
|
WHERE es.entry_id = $1",
|
||||||
|
)
|
||||||
|
.bind(entry_id)
|
||||||
|
.fetch_all(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
for f in &existing_fields {
|
for f in &existing_fields {
|
||||||
if let Err(e) = db::snapshot_secret_history(
|
if let Err(e) = db::snapshot_secret_history(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
db::SecretSnapshotParams {
|
db::SecretSnapshotParams {
|
||||||
entry_id,
|
|
||||||
secret_id: f.id,
|
secret_id: f.id,
|
||||||
entry_version: new_entry_version - 1,
|
name: &f.name,
|
||||||
field_name: &f.field_name,
|
|
||||||
encrypted: &f.encrypted,
|
encrypted: &f.encrypted,
|
||||||
action: "add",
|
action: "add",
|
||||||
},
|
},
|
||||||
@@ -327,28 +345,76 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlx::query("DELETE FROM secrets WHERE entry_id = $1")
|
sqlx::query("DELETE FROM entry_secrets WHERE entry_id = $1")
|
||||||
.bind(entry_id)
|
.bind(entry_id)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
"DELETE FROM secrets s \
|
||||||
|
WHERE NOT EXISTS (SELECT 1 FROM entry_secrets es WHERE es.secret_id = s.id)",
|
||||||
|
)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (field_name, field_value) in &flat_fields {
|
||||||
|
let encrypted = crypto::encrypt_json(master_key, field_value)?;
|
||||||
|
let secret_id: Uuid = sqlx::query_scalar(
|
||||||
|
"INSERT INTO secrets (user_id, name, type, encrypted) VALUES ($1, $2, $3, $4) RETURNING id",
|
||||||
|
)
|
||||||
|
.bind(params.user_id)
|
||||||
|
.bind(field_name)
|
||||||
|
.bind(infer_secret_type(field_name))
|
||||||
|
.bind(&encrypted)
|
||||||
|
.fetch_one(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
sqlx::query("INSERT INTO entry_secrets (entry_id, secret_id) VALUES ($1, $2)")
|
||||||
|
.bind(entry_id)
|
||||||
|
.bind(secret_id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let flat_fields = flatten_json_fields("", &secret_json);
|
for link_name in &link_secret_names {
|
||||||
for (field_name, field_value) in &flat_fields {
|
let secret_ids: Vec<Uuid> = if let Some(uid) = params.user_id {
|
||||||
let encrypted = crypto::encrypt_json(master_key, field_value)?;
|
sqlx::query_scalar("SELECT id FROM secrets WHERE user_id = $1 AND name = $2")
|
||||||
sqlx::query("INSERT INTO secrets (entry_id, field_name, encrypted) VALUES ($1, $2, $3)")
|
.bind(uid)
|
||||||
.bind(entry_id)
|
.bind(link_name)
|
||||||
.bind(field_name)
|
.fetch_all(&mut *tx)
|
||||||
.bind(&encrypted)
|
.await?
|
||||||
.execute(&mut *tx)
|
} else {
|
||||||
.await?;
|
sqlx::query_scalar("SELECT id FROM secrets WHERE user_id IS NULL AND name = $1")
|
||||||
|
.bind(link_name)
|
||||||
|
.fetch_all(&mut *tx)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
|
|
||||||
|
match secret_ids.len() {
|
||||||
|
0 => anyhow::bail!("Not found: secret named '{}'", link_name),
|
||||||
|
1 => {
|
||||||
|
sqlx::query(
|
||||||
|
"INSERT INTO entry_secrets (entry_id, secret_id) VALUES ($1, $2) ON CONFLICT DO NOTHING",
|
||||||
|
)
|
||||||
|
.bind(entry_id)
|
||||||
|
.bind(secret_ids[0])
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
n => anyhow::bail!(
|
||||||
|
"Ambiguous: {} secrets named '{}' found. Please deduplicate names first.",
|
||||||
|
n,
|
||||||
|
link_name
|
||||||
|
),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::audit::log_tx(
|
crate::audit::log_tx(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
|
params.user_id,
|
||||||
"add",
|
"add",
|
||||||
params.namespace,
|
params.folder,
|
||||||
params.kind,
|
params.entry_type,
|
||||||
params.name,
|
params.name,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"tags": params.tags,
|
"tags": params.tags,
|
||||||
@@ -361,18 +427,65 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
tx.commit().await?;
|
tx.commit().await?;
|
||||||
|
|
||||||
Ok(AddResult {
|
Ok(AddResult {
|
||||||
namespace: params.namespace.to_string(),
|
|
||||||
kind: params.kind.to_string(),
|
|
||||||
name: params.name.to_string(),
|
name: params.name.to_string(),
|
||||||
|
folder: params.folder.to_string(),
|
||||||
|
entry_type: params.entry_type.to_string(),
|
||||||
tags: params.tags.to_vec(),
|
tags: params.tags.to_vec(),
|
||||||
meta_keys,
|
meta_keys,
|
||||||
secret_keys,
|
secret_keys,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn infer_secret_type(name: &str) -> &'static str {
|
||||||
|
match name {
|
||||||
|
"ssh_key" => "pem",
|
||||||
|
"password" => "password",
|
||||||
|
"phone" | "phone_2" => "phone",
|
||||||
|
"webhook_url" | "address" => "url",
|
||||||
|
"access_key_id"
|
||||||
|
| "access_key_secret"
|
||||||
|
| "global_api_key"
|
||||||
|
| "api_key"
|
||||||
|
| "secret_key"
|
||||||
|
| "personal_access_token"
|
||||||
|
| "runner_token"
|
||||||
|
| "GOOGLE_CLIENT_ID"
|
||||||
|
| "GOOGLE_CLIENT_SECRET" => "token",
|
||||||
|
_ => "text",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_link_secret_names(
|
||||||
|
link_secret_names: &[String],
|
||||||
|
new_secret_names: &BTreeSet<String>,
|
||||||
|
) -> Result<Vec<String>> {
|
||||||
|
let mut deduped = Vec::new();
|
||||||
|
let mut seen = HashSet::new();
|
||||||
|
|
||||||
|
for raw in link_secret_names {
|
||||||
|
let trimmed = raw.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
anyhow::bail!("link_secret_names contains an empty name");
|
||||||
|
}
|
||||||
|
if new_secret_names.contains(trimmed) {
|
||||||
|
anyhow::bail!(
|
||||||
|
"Conflict: secret '{}' is provided both in secrets/secrets_obj and link_secret_names",
|
||||||
|
trimmed
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if seen.insert(trimmed.to_string()) {
|
||||||
|
deduped.push(trimmed.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(deduped)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use std::collections::BTreeSet;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_nested_file_shorthand() {
|
fn parse_nested_file_shorthand() {
|
||||||
@@ -401,4 +514,199 @@ mod tests {
|
|||||||
assert_eq!(fields[1].0, "credentials.type");
|
assert_eq!(fields[1].0, "credentials.type");
|
||||||
assert_eq!(fields[2].0, "username");
|
assert_eq!(fields[2].0, "username");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validate_link_secret_names_conflict_with_new_secret() {
|
||||||
|
let mut new_names = BTreeSet::new();
|
||||||
|
new_names.insert("password".to_string());
|
||||||
|
let err = validate_link_secret_names(&[String::from("password")], &new_names)
|
||||||
|
.expect_err("must fail on overlap");
|
||||||
|
assert!(
|
||||||
|
err.to_string()
|
||||||
|
.contains("provided both in secrets/secrets_obj and link_secret_names")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validate_link_secret_names_dedup_and_trim() {
|
||||||
|
let names = vec![
|
||||||
|
" shared_key ".to_string(),
|
||||||
|
"shared_key".to_string(),
|
||||||
|
"runner_token".to_string(),
|
||||||
|
];
|
||||||
|
let deduped = validate_link_secret_names(&names, &BTreeSet::new()).unwrap();
|
||||||
|
assert_eq!(deduped, vec!["shared_key", "runner_token"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn maybe_test_pool() -> Option<PgPool> {
|
||||||
|
let Ok(url) = std::env::var("SECRETS_DATABASE_URL") else {
|
||||||
|
eprintln!("skip add linkage tests: SECRETS_DATABASE_URL is not set");
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let Ok(pool) = PgPool::connect(&url).await else {
|
||||||
|
eprintln!("skip add linkage tests: cannot connect to database");
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
if let Err(e) = crate::db::migrate(&pool).await {
|
||||||
|
eprintln!("skip add linkage tests: migrate failed: {e}");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(pool)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cleanup_test_rows(pool: &PgPool, marker: &str) -> Result<()> {
|
||||||
|
sqlx::query(
|
||||||
|
"DELETE FROM entries WHERE user_id IS NULL AND (name LIKE $1 OR folder LIKE $1)",
|
||||||
|
)
|
||||||
|
.bind(format!("%{marker}%"))
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
sqlx::query(
|
||||||
|
"DELETE FROM secrets WHERE user_id IS NULL AND name LIKE $1 \
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM entry_secrets es WHERE es.secret_id = secrets.id)",
|
||||||
|
)
|
||||||
|
.bind(format!("%{marker}%"))
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn add_links_existing_secret_by_unique_name() -> Result<()> {
|
||||||
|
let Some(pool) = maybe_test_pool().await else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let suffix = Uuid::from_u128(rand::random()).to_string();
|
||||||
|
let marker = format!("link_unique_{}", &suffix[..8]);
|
||||||
|
let secret_name = format!("{}_secret", marker);
|
||||||
|
let entry_name = format!("{}_entry", marker);
|
||||||
|
|
||||||
|
cleanup_test_rows(&pool, &marker).await?;
|
||||||
|
|
||||||
|
let secret_id: Uuid = sqlx::query_scalar(
|
||||||
|
"INSERT INTO secrets (user_id, name, type, encrypted) VALUES (NULL, $1, 'text', $2) RETURNING id",
|
||||||
|
)
|
||||||
|
.bind(&secret_name)
|
||||||
|
.bind(vec![1_u8, 2, 3])
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
run(
|
||||||
|
&pool,
|
||||||
|
AddParams {
|
||||||
|
name: &entry_name,
|
||||||
|
folder: &marker,
|
||||||
|
entry_type: "service",
|
||||||
|
notes: "",
|
||||||
|
tags: &[],
|
||||||
|
meta_entries: &[],
|
||||||
|
secret_entries: &[],
|
||||||
|
link_secret_names: std::slice::from_ref(&secret_name),
|
||||||
|
user_id: None,
|
||||||
|
},
|
||||||
|
&[0_u8; 32],
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let linked: bool = sqlx::query_scalar(
|
||||||
|
"SELECT EXISTS( \
|
||||||
|
SELECT 1 FROM entry_secrets es \
|
||||||
|
JOIN entries e ON e.id = es.entry_id \
|
||||||
|
WHERE e.user_id IS NULL AND e.name = $1 AND es.secret_id = $2 \
|
||||||
|
)",
|
||||||
|
)
|
||||||
|
.bind(&entry_name)
|
||||||
|
.bind(secret_id)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await?;
|
||||||
|
assert!(linked);
|
||||||
|
|
||||||
|
cleanup_test_rows(&pool, &marker).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn add_link_secret_name_not_found_fails() -> Result<()> {
|
||||||
|
let Some(pool) = maybe_test_pool().await else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let suffix = Uuid::from_u128(rand::random()).to_string();
|
||||||
|
let marker = format!("link_missing_{}", &suffix[..8]);
|
||||||
|
let secret_name = format!("{}_secret", marker);
|
||||||
|
let entry_name = format!("{}_entry", marker);
|
||||||
|
|
||||||
|
cleanup_test_rows(&pool, &marker).await?;
|
||||||
|
|
||||||
|
let err = run(
|
||||||
|
&pool,
|
||||||
|
AddParams {
|
||||||
|
name: &entry_name,
|
||||||
|
folder: &marker,
|
||||||
|
entry_type: "service",
|
||||||
|
notes: "",
|
||||||
|
tags: &[],
|
||||||
|
meta_entries: &[],
|
||||||
|
secret_entries: &[],
|
||||||
|
link_secret_names: std::slice::from_ref(&secret_name),
|
||||||
|
user_id: None,
|
||||||
|
},
|
||||||
|
&[0_u8; 32],
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect_err("must fail when linked secret is not found");
|
||||||
|
assert!(err.to_string().contains("Not found: secret named"));
|
||||||
|
|
||||||
|
cleanup_test_rows(&pool, &marker).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn add_link_secret_name_ambiguous_fails() -> Result<()> {
|
||||||
|
let Some(pool) = maybe_test_pool().await else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let suffix = Uuid::from_u128(rand::random()).to_string();
|
||||||
|
let marker = format!("link_amb_{}", &suffix[..8]);
|
||||||
|
let secret_name = format!("{}_dup_secret", marker);
|
||||||
|
let entry_name = format!("{}_entry", marker);
|
||||||
|
|
||||||
|
cleanup_test_rows(&pool, &marker).await?;
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
"INSERT INTO secrets (user_id, name, type, encrypted) VALUES (NULL, $1, 'text', $2)",
|
||||||
|
)
|
||||||
|
.bind(&secret_name)
|
||||||
|
.bind(vec![1_u8])
|
||||||
|
.execute(&pool)
|
||||||
|
.await?;
|
||||||
|
sqlx::query(
|
||||||
|
"INSERT INTO secrets (user_id, name, type, encrypted) VALUES (NULL, $1, 'text', $2)",
|
||||||
|
)
|
||||||
|
.bind(&secret_name)
|
||||||
|
.bind(vec![2_u8])
|
||||||
|
.execute(&pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let err = run(
|
||||||
|
&pool,
|
||||||
|
AddParams {
|
||||||
|
name: &entry_name,
|
||||||
|
folder: &marker,
|
||||||
|
entry_type: "service",
|
||||||
|
notes: "",
|
||||||
|
tags: &[],
|
||||||
|
meta_entries: &[],
|
||||||
|
secret_entries: &[],
|
||||||
|
link_secret_names: std::slice::from_ref(&secret_name),
|
||||||
|
user_id: None,
|
||||||
|
},
|
||||||
|
&[0_u8; 32],
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect_err("must fail on ambiguous linked secret name");
|
||||||
|
assert!(err.to_string().contains("Ambiguous:"));
|
||||||
|
|
||||||
|
cleanup_test_rows(&pool, &marker).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
crates/secrets-core/src/service/audit_log.rs
Normal file
23
crates/secrets-core/src/service/audit_log.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::models::AuditLogEntry;
|
||||||
|
|
||||||
|
pub async fn list_for_user(pool: &PgPool, user_id: Uuid, limit: i64) -> Result<Vec<AuditLogEntry>> {
|
||||||
|
let limit = limit.clamp(1, 200);
|
||||||
|
|
||||||
|
let rows = sqlx::query_as(
|
||||||
|
"SELECT id, user_id, action, folder, type, name, detail, created_at \
|
||||||
|
FROM audit_log \
|
||||||
|
WHERE user_id = $1 \
|
||||||
|
ORDER BY created_at DESC, id DESC \
|
||||||
|
LIMIT $2",
|
||||||
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(limit)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(rows)
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -12,8 +12,8 @@ use crate::service::search::{fetch_entries, fetch_secrets_for_entries};
|
|||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub async fn build_env_map(
|
pub async fn build_env_map(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
namespace: Option<&str>,
|
folder: Option<&str>,
|
||||||
kind: Option<&str>,
|
entry_type: Option<&str>,
|
||||||
name: Option<&str>,
|
name: Option<&str>,
|
||||||
tags: &[String],
|
tags: &[String],
|
||||||
only_fields: &[String],
|
only_fields: &[String],
|
||||||
@@ -21,12 +21,13 @@ pub async fn build_env_map(
|
|||||||
master_key: &[u8; 32],
|
master_key: &[u8; 32],
|
||||||
user_id: Option<Uuid>,
|
user_id: Option<Uuid>,
|
||||||
) -> Result<HashMap<String, String>> {
|
) -> Result<HashMap<String, String>> {
|
||||||
let entries = fetch_entries(pool, namespace, kind, name, tags, None, user_id).await?;
|
let entries = fetch_entries(pool, folder, entry_type, name, tags, None, user_id).await?;
|
||||||
|
|
||||||
let mut combined: HashMap<String, String> = HashMap::new();
|
let mut combined: HashMap<String, String> = HashMap::new();
|
||||||
|
|
||||||
for entry in &entries {
|
for entry in &entries {
|
||||||
let entry_map = build_entry_env_map(pool, entry, only_fields, prefix, master_key).await?;
|
let entry_map =
|
||||||
|
build_entry_env_map(pool, entry, only_fields, prefix, master_key, user_id).await?;
|
||||||
combined.extend(entry_map);
|
combined.extend(entry_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,6 +40,7 @@ async fn build_entry_env_map(
|
|||||||
only_fields: &[String],
|
only_fields: &[String],
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
master_key: &[u8; 32],
|
master_key: &[u8; 32],
|
||||||
|
user_id: Option<Uuid>,
|
||||||
) -> Result<HashMap<String, String>> {
|
) -> Result<HashMap<String, String>> {
|
||||||
let entry_ids = vec![entry.id];
|
let entry_ids = vec![entry.id];
|
||||||
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
||||||
@@ -49,7 +51,7 @@ async fn build_entry_env_map(
|
|||||||
} else {
|
} else {
|
||||||
all_fields
|
all_fields
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|f| only_fields.contains(&f.field_name))
|
.filter(|f| only_fields.contains(&f.name))
|
||||||
.collect()
|
.collect()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -61,23 +63,28 @@ async fn build_entry_env_map(
|
|||||||
let key = format!(
|
let key = format!(
|
||||||
"{}_{}",
|
"{}_{}",
|
||||||
effective_prefix,
|
effective_prefix,
|
||||||
f.field_name.to_uppercase().replace(['-', '.'], "_")
|
f.name.to_uppercase().replace(['-', '.'], "_")
|
||||||
);
|
);
|
||||||
map.insert(key, json_to_env_string(&decrypted));
|
map.insert(key, json_to_env_string(&decrypted));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve key_ref
|
// Resolve key_ref. Supported formats: "name" or "folder/name".
|
||||||
if let Some(key_ref) = entry.metadata.get("key_ref").and_then(|v| v.as_str()) {
|
if let Some(key_ref) = entry.metadata.get("key_ref").and_then(|v| v.as_str()) {
|
||||||
let key_entries = fetch_entries(
|
let (ref_folder, ref_name) = if let Some((f, n)) = key_ref.split_once('/') {
|
||||||
pool,
|
(Some(f), n)
|
||||||
Some(&entry.namespace),
|
} else {
|
||||||
Some("key"),
|
(None, key_ref)
|
||||||
Some(key_ref),
|
};
|
||||||
&[],
|
let key_entries =
|
||||||
None,
|
fetch_entries(pool, ref_folder, None, Some(ref_name), &[], None, user_id).await?;
|
||||||
None,
|
|
||||||
)
|
if key_entries.len() > 1 {
|
||||||
.await?;
|
anyhow::bail!(
|
||||||
|
"key_ref '{}' matched {} entries; qualify with folder/name to resolve the ambiguity",
|
||||||
|
key_ref,
|
||||||
|
key_entries.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(key_entry) = key_entries.first() {
|
if let Some(key_entry) = key_entries.first() {
|
||||||
let key_ids = vec![key_entry.id];
|
let key_ids = vec![key_entry.id];
|
||||||
@@ -90,12 +97,12 @@ async fn build_entry_env_map(
|
|||||||
let key_var = format!(
|
let key_var = format!(
|
||||||
"{}_{}",
|
"{}_{}",
|
||||||
key_prefix,
|
key_prefix,
|
||||||
f.field_name.to_uppercase().replace(['-', '.'], "_")
|
f.name.to_uppercase().replace(['-', '.'], "_")
|
||||||
);
|
);
|
||||||
map.insert(key_var, json_to_env_string(&decrypted));
|
map.insert(key_var, json_to_env_string(&decrypted));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tracing::warn!(key_ref, "key_ref target not found");
|
tracing::warn!(key_ref, ?user_id, "key_ref target not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ use crate::models::{ExportData, ExportEntry, ExportFormat};
|
|||||||
use crate::service::search::{fetch_entries, fetch_secrets_for_entries};
|
use crate::service::search::{fetch_entries, fetch_secrets_for_entries};
|
||||||
|
|
||||||
pub struct ExportParams<'a> {
|
pub struct ExportParams<'a> {
|
||||||
pub namespace: Option<&'a str>,
|
pub folder: Option<&'a str>,
|
||||||
pub kind: Option<&'a str>,
|
pub entry_type: Option<&'a str>,
|
||||||
pub name: Option<&'a str>,
|
pub name: Option<&'a str>,
|
||||||
pub tags: &'a [String],
|
pub tags: &'a [String],
|
||||||
pub query: Option<&'a str>,
|
pub query: Option<&'a str>,
|
||||||
@@ -25,8 +25,8 @@ pub async fn export(
|
|||||||
) -> Result<ExportData> {
|
) -> Result<ExportData> {
|
||||||
let entries = fetch_entries(
|
let entries = fetch_entries(
|
||||||
pool,
|
pool,
|
||||||
params.namespace,
|
params.folder,
|
||||||
params.kind,
|
params.entry_type,
|
||||||
params.name,
|
params.name,
|
||||||
params.tags,
|
params.tags,
|
||||||
params.query,
|
params.query,
|
||||||
@@ -55,16 +55,17 @@ pub async fn export(
|
|||||||
let mut map = BTreeMap::new();
|
let mut map = BTreeMap::new();
|
||||||
for f in fields {
|
for f in fields {
|
||||||
let decrypted = crypto::decrypt_json(mk, &f.encrypted)?;
|
let decrypted = crypto::decrypt_json(mk, &f.encrypted)?;
|
||||||
map.insert(f.field_name.clone(), decrypted);
|
map.insert(f.name.clone(), decrypted);
|
||||||
}
|
}
|
||||||
Some(map)
|
Some(map)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export_entries.push(ExportEntry {
|
export_entries.push(ExportEntry {
|
||||||
namespace: entry.namespace.clone(),
|
|
||||||
kind: entry.kind.clone(),
|
|
||||||
name: entry.name.clone(),
|
name: entry.name.clone(),
|
||||||
|
folder: entry.folder.clone(),
|
||||||
|
entry_type: entry.entry_type.clone(),
|
||||||
|
notes: entry.notes.clone(),
|
||||||
tags: entry.tags.clone(),
|
tags: entry.tags.clone(),
|
||||||
metadata: entry.metadata.clone(),
|
metadata: entry.metadata.clone(),
|
||||||
secrets,
|
secrets,
|
||||||
|
|||||||
@@ -5,31 +5,19 @@ use std::collections::HashMap;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::crypto;
|
use crate::crypto;
|
||||||
use crate::service::search::{fetch_entries, fetch_secrets_for_entries};
|
use crate::service::search::{fetch_secrets_for_entries, resolve_entry, resolve_entry_by_id};
|
||||||
|
|
||||||
/// Decrypt a single named field from an entry.
|
/// Decrypt a single named field from an entry.
|
||||||
|
/// `folder` is optional; if omitted and multiple entries share the name, an error is returned.
|
||||||
pub async fn get_secret_field(
|
pub async fn get_secret_field(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
namespace: &str,
|
|
||||||
kind: &str,
|
|
||||||
name: &str,
|
name: &str,
|
||||||
|
folder: Option<&str>,
|
||||||
field_name: &str,
|
field_name: &str,
|
||||||
master_key: &[u8; 32],
|
master_key: &[u8; 32],
|
||||||
user_id: Option<Uuid>,
|
user_id: Option<Uuid>,
|
||||||
) -> Result<Value> {
|
) -> Result<Value> {
|
||||||
let entries = fetch_entries(
|
let entry = resolve_entry(pool, name, folder, user_id).await?;
|
||||||
pool,
|
|
||||||
Some(namespace),
|
|
||||||
Some(kind),
|
|
||||||
Some(name),
|
|
||||||
&[],
|
|
||||||
None,
|
|
||||||
user_id,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let entry = entries
|
|
||||||
.first()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Not found: [{}/{}] {}", namespace, kind, name))?;
|
|
||||||
|
|
||||||
let entry_ids = vec![entry.id];
|
let entry_ids = vec![entry.id];
|
||||||
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
||||||
@@ -37,34 +25,22 @@ pub async fn get_secret_field(
|
|||||||
|
|
||||||
let field = fields
|
let field = fields
|
||||||
.iter()
|
.iter()
|
||||||
.find(|f| f.field_name == field_name)
|
.find(|f| f.name == field_name)
|
||||||
.ok_or_else(|| anyhow::anyhow!("Secret field '{}' not found", field_name))?;
|
.ok_or_else(|| anyhow::anyhow!("Secret field '{}' not found", field_name))?;
|
||||||
|
|
||||||
crypto::decrypt_json(master_key, &field.encrypted)
|
crypto::decrypt_json(master_key, &field.encrypted)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decrypt all secret fields from an entry. Returns a map field_name → decrypted Value.
|
/// Decrypt all secret fields from an entry. Returns a map field_name → decrypted Value.
|
||||||
|
/// `folder` is optional; if omitted and multiple entries share the name, an error is returned.
|
||||||
pub async fn get_all_secrets(
|
pub async fn get_all_secrets(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
namespace: &str,
|
|
||||||
kind: &str,
|
|
||||||
name: &str,
|
name: &str,
|
||||||
|
folder: Option<&str>,
|
||||||
master_key: &[u8; 32],
|
master_key: &[u8; 32],
|
||||||
user_id: Option<Uuid>,
|
user_id: Option<Uuid>,
|
||||||
) -> Result<HashMap<String, Value>> {
|
) -> Result<HashMap<String, Value>> {
|
||||||
let entries = fetch_entries(
|
let entry = resolve_entry(pool, name, folder, user_id).await?;
|
||||||
pool,
|
|
||||||
Some(namespace),
|
|
||||||
Some(kind),
|
|
||||||
Some(name),
|
|
||||||
&[],
|
|
||||||
None,
|
|
||||||
user_id,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let entry = entries
|
|
||||||
.first()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Not found: [{}/{}] {}", namespace, kind, name))?;
|
|
||||||
|
|
||||||
let entry_ids = vec![entry.id];
|
let entry_ids = vec![entry.id];
|
||||||
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
||||||
@@ -73,7 +49,56 @@ pub async fn get_all_secrets(
|
|||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
for f in fields {
|
for f in fields {
|
||||||
let decrypted = crypto::decrypt_json(master_key, &f.encrypted)?;
|
let decrypted = crypto::decrypt_json(master_key, &f.encrypted)?;
|
||||||
map.insert(f.field_name.clone(), decrypted);
|
map.insert(f.name.clone(), decrypted);
|
||||||
|
}
|
||||||
|
Ok(map)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decrypt a single named field from an entry, located by its UUID.
|
||||||
|
pub async fn get_secret_field_by_id(
|
||||||
|
pool: &PgPool,
|
||||||
|
entry_id: Uuid,
|
||||||
|
field_name: &str,
|
||||||
|
master_key: &[u8; 32],
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
) -> Result<Value> {
|
||||||
|
resolve_entry_by_id(pool, entry_id, user_id)
|
||||||
|
.await
|
||||||
|
.map_err(|_| anyhow::anyhow!("Entry with id '{}' not found", entry_id))?;
|
||||||
|
|
||||||
|
let entry_ids = vec![entry_id];
|
||||||
|
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
||||||
|
let fields = secrets_map.get(&entry_id).map(Vec::as_slice).unwrap_or(&[]);
|
||||||
|
|
||||||
|
let field = fields
|
||||||
|
.iter()
|
||||||
|
.find(|f| f.name == field_name)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Secret field '{}' not found", field_name))?;
|
||||||
|
|
||||||
|
crypto::decrypt_json(master_key, &field.encrypted)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decrypt all secret fields from an entry, located by its UUID.
|
||||||
|
/// Returns a map field_name → decrypted Value.
|
||||||
|
pub async fn get_all_secrets_by_id(
|
||||||
|
pool: &PgPool,
|
||||||
|
entry_id: Uuid,
|
||||||
|
master_key: &[u8; 32],
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
) -> Result<HashMap<String, Value>> {
|
||||||
|
// Validate entry exists (and that it belongs to the requesting user)
|
||||||
|
resolve_entry_by_id(pool, entry_id, user_id)
|
||||||
|
.await
|
||||||
|
.map_err(|_| anyhow::anyhow!("Entry with id '{}' not found", entry_id))?;
|
||||||
|
|
||||||
|
let entry_ids = vec![entry_id];
|
||||||
|
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
||||||
|
let fields = secrets_map.get(&entry_id).map(Vec::as_slice).unwrap_or(&[]);
|
||||||
|
|
||||||
|
let mut map = HashMap::new();
|
||||||
|
for f in fields {
|
||||||
|
let decrypted = crypto::decrypt_json(master_key, &f.encrypted)?;
|
||||||
|
map.insert(f.name.clone(), decrypted);
|
||||||
}
|
}
|
||||||
Ok(map)
|
Ok(map)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,19 +3,21 @@ use serde_json::Value;
|
|||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::service::search::resolve_entry;
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
pub struct HistoryEntry {
|
pub struct HistoryEntry {
|
||||||
pub version: i64,
|
pub version: i64,
|
||||||
pub action: String,
|
pub action: String,
|
||||||
pub actor: String,
|
|
||||||
pub created_at: String,
|
pub created_at: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return version history for the entry identified by `name`.
|
||||||
|
/// `folder` is optional; if omitted and multiple entries share the name, an error is returned.
|
||||||
pub async fn run(
|
pub async fn run(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
namespace: &str,
|
|
||||||
kind: &str,
|
|
||||||
name: &str,
|
name: &str,
|
||||||
|
folder: Option<&str>,
|
||||||
limit: u32,
|
limit: u32,
|
||||||
user_id: Option<Uuid>,
|
user_id: Option<Uuid>,
|
||||||
) -> Result<Vec<HistoryEntry>> {
|
) -> Result<Vec<HistoryEntry>> {
|
||||||
@@ -23,43 +25,25 @@ pub async fn run(
|
|||||||
struct Row {
|
struct Row {
|
||||||
version: i64,
|
version: i64,
|
||||||
action: String,
|
action: String,
|
||||||
actor: String,
|
|
||||||
created_at: chrono::DateTime<chrono::Utc>,
|
created_at: chrono::DateTime<chrono::Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
let rows: Vec<Row> = if let Some(uid) = user_id {
|
let entry = resolve_entry(pool, name, folder, user_id).await?;
|
||||||
sqlx::query_as(
|
|
||||||
"SELECT version, action, actor, created_at FROM entries_history \
|
let rows: Vec<Row> = sqlx::query_as(
|
||||||
WHERE namespace = $1 AND kind = $2 AND name = $3 AND user_id = $4 \
|
"SELECT version, action, created_at FROM entries_history \
|
||||||
ORDER BY id DESC LIMIT $5",
|
WHERE entry_id = $1 ORDER BY id DESC LIMIT $2",
|
||||||
)
|
)
|
||||||
.bind(namespace)
|
.bind(entry.id)
|
||||||
.bind(kind)
|
.bind(limit as i64)
|
||||||
.bind(name)
|
.fetch_all(pool)
|
||||||
.bind(uid)
|
.await?;
|
||||||
.bind(limit as i64)
|
|
||||||
.fetch_all(pool)
|
|
||||||
.await?
|
|
||||||
} else {
|
|
||||||
sqlx::query_as(
|
|
||||||
"SELECT version, action, actor, created_at FROM entries_history \
|
|
||||||
WHERE namespace = $1 AND kind = $2 AND name = $3 AND user_id IS NULL \
|
|
||||||
ORDER BY id DESC LIMIT $4",
|
|
||||||
)
|
|
||||||
.bind(namespace)
|
|
||||||
.bind(kind)
|
|
||||||
.bind(name)
|
|
||||||
.bind(limit as i64)
|
|
||||||
.fetch_all(pool)
|
|
||||||
.await?
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(rows
|
Ok(rows
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|r| HistoryEntry {
|
.map(|r| HistoryEntry {
|
||||||
version: r.version,
|
version: r.version,
|
||||||
action: r.action,
|
action: r.action,
|
||||||
actor: r.actor,
|
|
||||||
created_at: r.created_at.format("%Y-%m-%dT%H:%M:%SZ").to_string(),
|
created_at: r.created_at.format("%Y-%m-%dT%H:%M:%SZ").to_string(),
|
||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
@@ -67,12 +51,11 @@ pub async fn run(
|
|||||||
|
|
||||||
pub async fn run_json(
|
pub async fn run_json(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
namespace: &str,
|
|
||||||
kind: &str,
|
|
||||||
name: &str,
|
name: &str,
|
||||||
|
folder: Option<&str>,
|
||||||
limit: u32,
|
limit: u32,
|
||||||
user_id: Option<Uuid>,
|
user_id: Option<Uuid>,
|
||||||
) -> Result<Value> {
|
) -> Result<Value> {
|
||||||
let entries = run(pool, namespace, kind, name, limit, user_id).await?;
|
let entries = run(pool, name, folder, limit, user_id).await?;
|
||||||
Ok(serde_json::to_value(entries)?)
|
Ok(serde_json::to_value(entries)?)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,10 +47,9 @@ pub async fn run(
|
|||||||
for entry in &data.entries {
|
for entry in &data.entries {
|
||||||
let exists: bool = sqlx::query_scalar(
|
let exists: bool = sqlx::query_scalar(
|
||||||
"SELECT EXISTS(SELECT 1 FROM entries \
|
"SELECT EXISTS(SELECT 1 FROM entries \
|
||||||
WHERE namespace = $1 AND kind = $2 AND name = $3 AND user_id IS NOT DISTINCT FROM $4)",
|
WHERE folder = $1 AND name = $2 AND user_id IS NOT DISTINCT FROM $3)",
|
||||||
)
|
)
|
||||||
.bind(&entry.namespace)
|
.bind(&entry.folder)
|
||||||
.bind(&entry.kind)
|
|
||||||
.bind(&entry.name)
|
.bind(&entry.name)
|
||||||
.bind(params.user_id)
|
.bind(params.user_id)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
@@ -59,9 +58,7 @@ pub async fn run(
|
|||||||
|
|
||||||
if exists && !params.force {
|
if exists && !params.force {
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow::anyhow!(
|
||||||
"Import aborted: conflict on [{}/{}/{}]",
|
"Import aborted: conflict on '{}'",
|
||||||
entry.namespace,
|
|
||||||
entry.kind,
|
|
||||||
entry.name
|
entry.name
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@@ -81,12 +78,14 @@ pub async fn run(
|
|||||||
match add_run(
|
match add_run(
|
||||||
pool,
|
pool,
|
||||||
AddParams {
|
AddParams {
|
||||||
namespace: &entry.namespace,
|
|
||||||
kind: &entry.kind,
|
|
||||||
name: &entry.name,
|
name: &entry.name,
|
||||||
|
folder: &entry.folder,
|
||||||
|
entry_type: &entry.entry_type,
|
||||||
|
notes: &entry.notes,
|
||||||
tags: &entry.tags,
|
tags: &entry.tags,
|
||||||
meta_entries: &meta_entries,
|
meta_entries: &meta_entries,
|
||||||
secret_entries: &secret_entries,
|
secret_entries: &secret_entries,
|
||||||
|
link_secret_names: &[],
|
||||||
user_id: params.user_id,
|
user_id: params.user_id,
|
||||||
},
|
},
|
||||||
master_key,
|
master_key,
|
||||||
@@ -98,8 +97,6 @@ pub async fn run(
|
|||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::error!(
|
tracing::error!(
|
||||||
namespace = entry.namespace,
|
|
||||||
kind = entry.kind,
|
|
||||||
name = entry.name,
|
name = entry.name,
|
||||||
error = %e,
|
error = %e,
|
||||||
"failed to import entry"
|
"failed to import entry"
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
pub mod add;
|
pub mod add;
|
||||||
pub mod api_key;
|
pub mod api_key;
|
||||||
|
pub mod audit_log;
|
||||||
pub mod delete;
|
pub mod delete;
|
||||||
pub mod env_map;
|
pub mod env_map;
|
||||||
pub mod export;
|
pub mod export;
|
||||||
|
|||||||
@@ -3,92 +3,145 @@ use serde_json::Value;
|
|||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::crypto;
|
|
||||||
use crate::db;
|
use crate::db;
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
pub struct RollbackResult {
|
pub struct RollbackResult {
|
||||||
pub namespace: String,
|
|
||||||
pub kind: String,
|
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub folder: String,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
pub entry_type: String,
|
||||||
pub restored_version: i64,
|
pub restored_version: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Roll back entry `name` to `to_version` (or the most recent snapshot if None).
|
||||||
|
/// `folder` is optional; if omitted and multiple entries share the name, an error is returned.
|
||||||
pub async fn run(
|
pub async fn run(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
namespace: &str,
|
|
||||||
kind: &str,
|
|
||||||
name: &str,
|
name: &str,
|
||||||
|
folder: Option<&str>,
|
||||||
to_version: Option<i64>,
|
to_version: Option<i64>,
|
||||||
master_key: &[u8; 32],
|
master_key: &[u8; 32],
|
||||||
user_id: Option<Uuid>,
|
user_id: Option<Uuid>,
|
||||||
) -> Result<RollbackResult> {
|
) -> Result<RollbackResult> {
|
||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
struct EntryHistoryRow {
|
struct EntryHistoryRow {
|
||||||
entry_id: Uuid,
|
folder: String,
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
entry_type: String,
|
||||||
version: i64,
|
version: i64,
|
||||||
action: String,
|
action: String,
|
||||||
tags: Vec<String>,
|
tags: Vec<String>,
|
||||||
metadata: Value,
|
metadata: Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
let snap: Option<EntryHistoryRow> = if let Some(ver) = to_version {
|
// Disambiguate: find the unique entry_id for (name, folder).
|
||||||
if let Some(uid) = user_id {
|
// Query entries_history by entry_id once we know it; first resolve via name + optional folder.
|
||||||
sqlx::query_as(
|
let entry_id: Option<Uuid> = if let Some(uid) = user_id {
|
||||||
"SELECT entry_id, version, action, tags, metadata FROM entries_history \
|
if let Some(f) = folder {
|
||||||
WHERE namespace = $1 AND kind = $2 AND name = $3 AND version = $4 \
|
sqlx::query_scalar(
|
||||||
AND user_id = $5 ORDER BY id DESC LIMIT 1",
|
"SELECT DISTINCT entry_id FROM entries_history \
|
||||||
|
WHERE name = $1 AND folder = $2 AND user_id = $3 LIMIT 1",
|
||||||
)
|
)
|
||||||
.bind(namespace)
|
|
||||||
.bind(kind)
|
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.bind(ver)
|
.bind(f)
|
||||||
.bind(uid)
|
.bind(uid)
|
||||||
.fetch_optional(pool)
|
.fetch_optional(pool)
|
||||||
.await?
|
.await?
|
||||||
} else {
|
} else {
|
||||||
sqlx::query_as(
|
let ids: Vec<Uuid> = sqlx::query_scalar(
|
||||||
"SELECT entry_id, version, action, tags, metadata FROM entries_history \
|
"SELECT DISTINCT entry_id FROM entries_history \
|
||||||
WHERE namespace = $1 AND kind = $2 AND name = $3 AND version = $4 \
|
WHERE name = $1 AND user_id = $2",
|
||||||
AND user_id IS NULL ORDER BY id DESC LIMIT 1",
|
|
||||||
)
|
)
|
||||||
.bind(namespace)
|
|
||||||
.bind(kind)
|
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.bind(ver)
|
.bind(uid)
|
||||||
.fetch_optional(pool)
|
.fetch_all(pool)
|
||||||
.await?
|
.await?;
|
||||||
|
match ids.len() {
|
||||||
|
0 => None,
|
||||||
|
1 => Some(ids[0]),
|
||||||
|
_ => {
|
||||||
|
let folders: Vec<String> = sqlx::query_scalar(
|
||||||
|
"SELECT DISTINCT folder FROM entries_history \
|
||||||
|
WHERE name = $1 AND user_id = $2",
|
||||||
|
)
|
||||||
|
.bind(name)
|
||||||
|
.bind(uid)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await?;
|
||||||
|
anyhow::bail!(
|
||||||
|
"Ambiguous: entries named '{}' exist in folders: [{}]. \
|
||||||
|
Specify 'folder' to disambiguate.",
|
||||||
|
name,
|
||||||
|
folders.join(", ")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if let Some(uid) = user_id {
|
} else if let Some(f) = folder {
|
||||||
sqlx::query_as(
|
sqlx::query_scalar(
|
||||||
"SELECT entry_id, version, action, tags, metadata FROM entries_history \
|
"SELECT DISTINCT entry_id FROM entries_history \
|
||||||
WHERE namespace = $1 AND kind = $2 AND name = $3 \
|
WHERE name = $1 AND folder = $2 AND user_id IS NULL LIMIT 1",
|
||||||
AND user_id = $4 ORDER BY id DESC LIMIT 1",
|
|
||||||
)
|
)
|
||||||
.bind(namespace)
|
|
||||||
.bind(kind)
|
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.bind(uid)
|
.bind(f)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
let ids: Vec<Uuid> = sqlx::query_scalar(
|
||||||
|
"SELECT DISTINCT entry_id FROM entries_history \
|
||||||
|
WHERE name = $1 AND user_id IS NULL",
|
||||||
|
)
|
||||||
|
.bind(name)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await?;
|
||||||
|
match ids.len() {
|
||||||
|
0 => None,
|
||||||
|
1 => Some(ids[0]),
|
||||||
|
_ => {
|
||||||
|
let folders: Vec<String> = sqlx::query_scalar(
|
||||||
|
"SELECT DISTINCT folder FROM entries_history \
|
||||||
|
WHERE name = $1 AND user_id IS NULL",
|
||||||
|
)
|
||||||
|
.bind(name)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await?;
|
||||||
|
anyhow::bail!(
|
||||||
|
"Ambiguous: entries named '{}' exist in folders: [{}]. \
|
||||||
|
Specify 'folder' to disambiguate.",
|
||||||
|
name,
|
||||||
|
folders.join(", ")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let entry_id = entry_id.ok_or_else(|| anyhow::anyhow!("No history found for '{}'", name))?;
|
||||||
|
|
||||||
|
let snap: Option<EntryHistoryRow> = if let Some(ver) = to_version {
|
||||||
|
sqlx::query_as(
|
||||||
|
"SELECT folder, type, version, action, tags, metadata \
|
||||||
|
FROM entries_history \
|
||||||
|
WHERE entry_id = $1 AND version = $2 ORDER BY id DESC LIMIT 1",
|
||||||
|
)
|
||||||
|
.bind(entry_id)
|
||||||
|
.bind(ver)
|
||||||
.fetch_optional(pool)
|
.fetch_optional(pool)
|
||||||
.await?
|
.await?
|
||||||
} else {
|
} else {
|
||||||
sqlx::query_as(
|
sqlx::query_as(
|
||||||
"SELECT entry_id, version, action, tags, metadata FROM entries_history \
|
"SELECT folder, type, version, action, tags, metadata \
|
||||||
WHERE namespace = $1 AND kind = $2 AND name = $3 \
|
FROM entries_history \
|
||||||
AND user_id IS NULL ORDER BY id DESC LIMIT 1",
|
WHERE entry_id = $1 ORDER BY id DESC LIMIT 1",
|
||||||
)
|
)
|
||||||
.bind(namespace)
|
.bind(entry_id)
|
||||||
.bind(kind)
|
|
||||||
.bind(name)
|
|
||||||
.fetch_optional(pool)
|
.fetch_optional(pool)
|
||||||
.await?
|
.await?
|
||||||
};
|
};
|
||||||
|
|
||||||
let snap = snap.ok_or_else(|| {
|
let snap = snap.ok_or_else(|| {
|
||||||
anyhow::anyhow!(
|
anyhow::anyhow!(
|
||||||
"No history found for [{}/{}] {}{}.",
|
"No history found for '{}'{}.",
|
||||||
namespace,
|
|
||||||
kind,
|
|
||||||
name,
|
name,
|
||||||
to_version
|
to_version
|
||||||
.map(|v| format!(" at version {}", v))
|
.map(|v| format!(" at version {}", v))
|
||||||
@@ -96,33 +149,7 @@ pub async fn run(
|
|||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
#[derive(sqlx::FromRow)]
|
let _ = master_key;
|
||||||
struct SecretHistoryRow {
|
|
||||||
field_name: String,
|
|
||||||
encrypted: Vec<u8>,
|
|
||||||
action: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
let field_snaps: Vec<SecretHistoryRow> = sqlx::query_as(
|
|
||||||
"SELECT field_name, encrypted, action FROM secrets_history \
|
|
||||||
WHERE entry_id = $1 AND entry_version = $2 ORDER BY field_name",
|
|
||||||
)
|
|
||||||
.bind(snap.entry_id)
|
|
||||||
.bind(snap.version)
|
|
||||||
.fetch_all(pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
for f in &field_snaps {
|
|
||||||
if f.action != "delete" && !f.encrypted.is_empty() {
|
|
||||||
crypto::decrypt_json(master_key, &f.encrypted).map_err(|e| {
|
|
||||||
anyhow::anyhow!(
|
|
||||||
"Cannot decrypt snapshot for field '{}': {}",
|
|
||||||
f.field_name,
|
|
||||||
e
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut tx = pool.begin().await?;
|
let mut tx = pool.begin().await?;
|
||||||
|
|
||||||
@@ -130,43 +157,32 @@ pub async fn run(
|
|||||||
struct LiveEntry {
|
struct LiveEntry {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
version: i64,
|
version: i64,
|
||||||
|
folder: String,
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
entry_type: String,
|
||||||
tags: Vec<String>,
|
tags: Vec<String>,
|
||||||
metadata: Value,
|
metadata: Value,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
notes: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query live entry with correct user_id scoping to avoid PK conflicts
|
// Lock the live entry if it exists (matched by entry_id for precision).
|
||||||
let live: Option<LiveEntry> = if let Some(uid) = user_id {
|
let live: Option<LiveEntry> = sqlx::query_as(
|
||||||
sqlx::query_as(
|
"SELECT id, version, folder, type, tags, metadata, notes FROM entries \
|
||||||
"SELECT id, version, tags, metadata FROM entries \
|
WHERE id = $1 FOR UPDATE",
|
||||||
WHERE user_id = $1 AND namespace = $2 AND kind = $3 AND name = $4 FOR UPDATE",
|
)
|
||||||
)
|
.bind(entry_id)
|
||||||
.bind(uid)
|
.fetch_optional(&mut *tx)
|
||||||
.bind(namespace)
|
.await?;
|
||||||
.bind(kind)
|
|
||||||
.bind(name)
|
|
||||||
.fetch_optional(&mut *tx)
|
|
||||||
.await?
|
|
||||||
} else {
|
|
||||||
sqlx::query_as(
|
|
||||||
"SELECT id, version, tags, metadata FROM entries \
|
|
||||||
WHERE user_id IS NULL AND namespace = $1 AND kind = $2 AND name = $3 FOR UPDATE",
|
|
||||||
)
|
|
||||||
.bind(namespace)
|
|
||||||
.bind(kind)
|
|
||||||
.bind(name)
|
|
||||||
.fetch_optional(&mut *tx)
|
|
||||||
.await?
|
|
||||||
};
|
|
||||||
|
|
||||||
let entry_id = if let Some(ref lr) = live {
|
let live_entry_id = if let Some(ref lr) = live {
|
||||||
// Snapshot current state before overwriting
|
|
||||||
if let Err(e) = db::snapshot_entry_history(
|
if let Err(e) = db::snapshot_entry_history(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
db::EntrySnapshotParams {
|
db::EntrySnapshotParams {
|
||||||
entry_id: lr.id,
|
entry_id: lr.id,
|
||||||
user_id,
|
user_id,
|
||||||
namespace,
|
folder: &lr.folder,
|
||||||
kind,
|
entry_type: &lr.entry_type,
|
||||||
name,
|
name,
|
||||||
version: lr.version,
|
version: lr.version,
|
||||||
action: "rollback",
|
action: "rollback",
|
||||||
@@ -182,23 +198,25 @@ pub async fn run(
|
|||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
struct LiveField {
|
struct LiveField {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
field_name: String,
|
name: String,
|
||||||
encrypted: Vec<u8>,
|
encrypted: Vec<u8>,
|
||||||
}
|
}
|
||||||
let live_fields: Vec<LiveField> =
|
let live_fields: Vec<LiveField> = sqlx::query_as(
|
||||||
sqlx::query_as("SELECT id, field_name, encrypted FROM secrets WHERE entry_id = $1")
|
"SELECT s.id, s.name, s.encrypted \
|
||||||
.bind(lr.id)
|
FROM entry_secrets es \
|
||||||
.fetch_all(&mut *tx)
|
JOIN secrets s ON s.id = es.secret_id \
|
||||||
.await?;
|
WHERE es.entry_id = $1",
|
||||||
|
)
|
||||||
|
.bind(lr.id)
|
||||||
|
.fetch_all(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
for f in &live_fields {
|
for f in &live_fields {
|
||||||
if let Err(e) = db::snapshot_secret_history(
|
if let Err(e) = db::snapshot_secret_history(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
db::SecretSnapshotParams {
|
db::SecretSnapshotParams {
|
||||||
entry_id: lr.id,
|
|
||||||
secret_id: f.id,
|
secret_id: f.id,
|
||||||
entry_version: lr.version,
|
name: &f.name,
|
||||||
field_name: &f.field_name,
|
|
||||||
encrypted: &f.encrypted,
|
encrypted: &f.encrypted,
|
||||||
action: "rollback",
|
action: "rollback",
|
||||||
},
|
},
|
||||||
@@ -209,7 +227,6 @@ pub async fn run(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the existing row in-place to preserve its primary key and user_id
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"UPDATE entries SET tags = $1, metadata = $2, version = version + 1, \
|
"UPDATE entries SET tags = $1, metadata = $2, version = version + 1, \
|
||||||
updated_at = NOW() WHERE id = $3",
|
updated_at = NOW() WHERE id = $3",
|
||||||
@@ -222,16 +239,15 @@ pub async fn run(
|
|||||||
|
|
||||||
lr.id
|
lr.id
|
||||||
} else {
|
} else {
|
||||||
// No live entry — insert a fresh one with a new UUID
|
|
||||||
if let Some(uid) = user_id {
|
if let Some(uid) = user_id {
|
||||||
sqlx::query_scalar(
|
sqlx::query_scalar(
|
||||||
"INSERT INTO entries \
|
"INSERT INTO entries \
|
||||||
(user_id, namespace, kind, name, tags, metadata, version, updated_at) \
|
(user_id, folder, type, name, notes, tags, metadata, version, updated_at) \
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW()) RETURNING id",
|
VALUES ($1, $2, $3, $4, '', $5, $6, $7, NOW()) RETURNING id",
|
||||||
)
|
)
|
||||||
.bind(uid)
|
.bind(uid)
|
||||||
.bind(namespace)
|
.bind(&snap.folder)
|
||||||
.bind(kind)
|
.bind(&snap.entry_type)
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.bind(&snap.tags)
|
.bind(&snap.tags)
|
||||||
.bind(&snap.metadata)
|
.bind(&snap.metadata)
|
||||||
@@ -241,11 +257,11 @@ pub async fn run(
|
|||||||
} else {
|
} else {
|
||||||
sqlx::query_scalar(
|
sqlx::query_scalar(
|
||||||
"INSERT INTO entries \
|
"INSERT INTO entries \
|
||||||
(namespace, kind, name, tags, metadata, version, updated_at) \
|
(folder, type, name, notes, tags, metadata, version, updated_at) \
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, NOW()) RETURNING id",
|
VALUES ($1, $2, $3, '', $4, $5, $6, NOW()) RETURNING id",
|
||||||
)
|
)
|
||||||
.bind(namespace)
|
.bind(&snap.folder)
|
||||||
.bind(kind)
|
.bind(&snap.entry_type)
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.bind(&snap.tags)
|
.bind(&snap.tags)
|
||||||
.bind(&snap.metadata)
|
.bind(&snap.metadata)
|
||||||
@@ -255,28 +271,16 @@ pub async fn run(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
sqlx::query("DELETE FROM secrets WHERE entry_id = $1")
|
// In N:N mode, rollback restores entry metadata/tags only.
|
||||||
.bind(entry_id)
|
// Secret snapshots are kept for audit but secret linkage/content is not rewritten here.
|
||||||
.execute(&mut *tx)
|
let _ = live_entry_id;
|
||||||
.await?;
|
|
||||||
|
|
||||||
for f in &field_snaps {
|
|
||||||
if f.action == "delete" {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
sqlx::query("INSERT INTO secrets (entry_id, field_name, encrypted) VALUES ($1, $2, $3)")
|
|
||||||
.bind(entry_id)
|
|
||||||
.bind(&f.field_name)
|
|
||||||
.bind(&f.encrypted)
|
|
||||||
.execute(&mut *tx)
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
crate::audit::log_tx(
|
crate::audit::log_tx(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
|
user_id,
|
||||||
"rollback",
|
"rollback",
|
||||||
namespace,
|
&snap.folder,
|
||||||
kind,
|
&snap.entry_type,
|
||||||
name,
|
name,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"restored_version": snap.version,
|
"restored_version": snap.version,
|
||||||
@@ -288,9 +292,9 @@ pub async fn run(
|
|||||||
tx.commit().await?;
|
tx.commit().await?;
|
||||||
|
|
||||||
Ok(RollbackResult {
|
Ok(RollbackResult {
|
||||||
namespace: namespace.to_string(),
|
|
||||||
kind: kind.to_string(),
|
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
|
folder: snap.folder,
|
||||||
|
entry_type: snap.entry_type,
|
||||||
restored_version: snap.version,
|
restored_version: snap.version,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ use crate::models::{Entry, SecretField};
|
|||||||
pub const FETCH_ALL_LIMIT: u32 = 100_000;
|
pub const FETCH_ALL_LIMIT: u32 = 100_000;
|
||||||
|
|
||||||
pub struct SearchParams<'a> {
|
pub struct SearchParams<'a> {
|
||||||
pub namespace: Option<&'a str>,
|
pub folder: Option<&'a str>,
|
||||||
pub kind: Option<&'a str>,
|
pub entry_type: Option<&'a str>,
|
||||||
pub name: Option<&'a str>,
|
pub name: Option<&'a str>,
|
||||||
pub tags: &'a [String],
|
pub tags: &'a [String],
|
||||||
pub query: Option<&'a str>,
|
pub query: Option<&'a str>,
|
||||||
@@ -27,49 +27,46 @@ pub struct SearchResult {
|
|||||||
pub secret_schemas: HashMap<Uuid, Vec<SecretField>>,
|
pub secret_schemas: HashMap<Uuid, Vec<SecretField>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(pool: &PgPool, params: SearchParams<'_>) -> Result<SearchResult> {
|
/// List `entries` rows matching params (paged, ordered per `params.sort`).
|
||||||
let entries = fetch_entries_paged(pool, ¶ms).await?;
|
/// Does not read the `secrets` table.
|
||||||
let entry_ids: Vec<Uuid> = entries.iter().map(|e| e.id).collect();
|
pub async fn list_entries(pool: &PgPool, params: SearchParams<'_>) -> Result<Vec<Entry>> {
|
||||||
let secret_schemas = if !entry_ids.is_empty() {
|
|
||||||
fetch_secret_schemas(pool, &entry_ids).await?
|
|
||||||
} else {
|
|
||||||
HashMap::new()
|
|
||||||
};
|
|
||||||
Ok(SearchResult {
|
|
||||||
entries,
|
|
||||||
secret_schemas,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Fetch entries matching the given filters — returns all matching entries up to FETCH_ALL_LIMIT.
|
|
||||||
pub async fn fetch_entries(
|
|
||||||
pool: &PgPool,
|
|
||||||
namespace: Option<&str>,
|
|
||||||
kind: Option<&str>,
|
|
||||||
name: Option<&str>,
|
|
||||||
tags: &[String],
|
|
||||||
query: Option<&str>,
|
|
||||||
user_id: Option<Uuid>,
|
|
||||||
) -> Result<Vec<Entry>> {
|
|
||||||
let params = SearchParams {
|
|
||||||
namespace,
|
|
||||||
kind,
|
|
||||||
name,
|
|
||||||
tags,
|
|
||||||
query,
|
|
||||||
sort: "name",
|
|
||||||
limit: FETCH_ALL_LIMIT,
|
|
||||||
offset: 0,
|
|
||||||
user_id,
|
|
||||||
};
|
|
||||||
fetch_entries_paged(pool, ¶ms).await
|
fetch_entries_paged(pool, ¶ms).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<Entry>> {
|
/// Count `entries` rows matching the same filters as [`list_entries`] (ignores `sort` / `limit` / `offset`).
|
||||||
|
/// Does not read the `secrets` table.
|
||||||
|
pub async fn count_entries(pool: &PgPool, a: &SearchParams<'_>) -> Result<i64> {
|
||||||
|
let (where_clause, _) = entry_where_clause_and_next_idx(a);
|
||||||
|
let sql = format!("SELECT COUNT(*)::bigint FROM entries {where_clause}");
|
||||||
|
let mut q = sqlx::query_scalar::<_, i64>(&sql);
|
||||||
|
if let Some(uid) = a.user_id {
|
||||||
|
q = q.bind(uid);
|
||||||
|
}
|
||||||
|
if let Some(v) = a.folder {
|
||||||
|
q = q.bind(v);
|
||||||
|
}
|
||||||
|
if let Some(v) = a.entry_type {
|
||||||
|
q = q.bind(v);
|
||||||
|
}
|
||||||
|
if let Some(v) = a.name {
|
||||||
|
q = q.bind(v);
|
||||||
|
}
|
||||||
|
for tag in a.tags {
|
||||||
|
q = q.bind(tag);
|
||||||
|
}
|
||||||
|
if let Some(v) = a.query {
|
||||||
|
let pattern = format!("%{}%", v.replace('%', "\\%").replace('_', "\\_"));
|
||||||
|
q = q.bind(pattern);
|
||||||
|
}
|
||||||
|
let n = q.fetch_one(pool).await?;
|
||||||
|
Ok(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shared WHERE clause and the next `$n` index (for LIMIT/OFFSET in paged queries).
|
||||||
|
fn entry_where_clause_and_next_idx(a: &SearchParams<'_>) -> (String, i32) {
|
||||||
let mut conditions: Vec<String> = Vec::new();
|
let mut conditions: Vec<String> = Vec::new();
|
||||||
let mut idx: i32 = 1;
|
let mut idx: i32 = 1;
|
||||||
|
|
||||||
// user_id filtering — always comes first when present
|
|
||||||
if a.user_id.is_some() {
|
if a.user_id.is_some() {
|
||||||
conditions.push(format!("user_id = ${}", idx));
|
conditions.push(format!("user_id = ${}", idx));
|
||||||
idx += 1;
|
idx += 1;
|
||||||
@@ -77,12 +74,12 @@ async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<
|
|||||||
conditions.push("user_id IS NULL".to_string());
|
conditions.push("user_id IS NULL".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.namespace.is_some() {
|
if a.folder.is_some() {
|
||||||
conditions.push(format!("namespace = ${}", idx));
|
conditions.push(format!("folder = ${}", idx));
|
||||||
idx += 1;
|
idx += 1;
|
||||||
}
|
}
|
||||||
if a.kind.is_some() {
|
if a.entry_type.is_some() {
|
||||||
conditions.push(format!("kind = ${}", idx));
|
conditions.push(format!("type = ${}", idx));
|
||||||
idx += 1;
|
idx += 1;
|
||||||
}
|
}
|
||||||
if a.name.is_some() {
|
if a.name.is_some() {
|
||||||
@@ -106,14 +103,64 @@ async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<
|
|||||||
}
|
}
|
||||||
if a.query.is_some() {
|
if a.query.is_some() {
|
||||||
conditions.push(format!(
|
conditions.push(format!(
|
||||||
"(name ILIKE ${i} ESCAPE '\\' OR namespace ILIKE ${i} ESCAPE '\\' \
|
"(name ILIKE ${i} ESCAPE '\\' OR folder ILIKE ${i} ESCAPE '\\' \
|
||||||
OR kind ILIKE ${i} ESCAPE '\\' OR metadata::text ILIKE ${i} ESCAPE '\\' \
|
OR type ILIKE ${i} ESCAPE '\\' OR notes ILIKE ${i} ESCAPE '\\' \
|
||||||
|
OR metadata::text ILIKE ${i} ESCAPE '\\' \
|
||||||
OR EXISTS (SELECT 1 FROM unnest(tags) t WHERE t ILIKE ${i} ESCAPE '\\'))",
|
OR EXISTS (SELECT 1 FROM unnest(tags) t WHERE t ILIKE ${i} ESCAPE '\\'))",
|
||||||
i = idx
|
i = idx
|
||||||
));
|
));
|
||||||
idx += 1;
|
idx += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let where_clause = if conditions.is_empty() {
|
||||||
|
String::new()
|
||||||
|
} else {
|
||||||
|
format!("WHERE {}", conditions.join(" AND "))
|
||||||
|
};
|
||||||
|
(where_clause, idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(pool: &PgPool, params: SearchParams<'_>) -> Result<SearchResult> {
|
||||||
|
let entries = fetch_entries_paged(pool, ¶ms).await?;
|
||||||
|
let entry_ids: Vec<Uuid> = entries.iter().map(|e| e.id).collect();
|
||||||
|
let secret_schemas = if !entry_ids.is_empty() {
|
||||||
|
fetch_secret_schemas(pool, &entry_ids).await?
|
||||||
|
} else {
|
||||||
|
HashMap::new()
|
||||||
|
};
|
||||||
|
Ok(SearchResult {
|
||||||
|
entries,
|
||||||
|
secret_schemas,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch entries matching the given filters — returns all matching entries up to FETCH_ALL_LIMIT.
|
||||||
|
pub async fn fetch_entries(
|
||||||
|
pool: &PgPool,
|
||||||
|
folder: Option<&str>,
|
||||||
|
entry_type: Option<&str>,
|
||||||
|
name: Option<&str>,
|
||||||
|
tags: &[String],
|
||||||
|
query: Option<&str>,
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
) -> Result<Vec<Entry>> {
|
||||||
|
let params = SearchParams {
|
||||||
|
folder,
|
||||||
|
entry_type,
|
||||||
|
name,
|
||||||
|
tags,
|
||||||
|
query,
|
||||||
|
sort: "name",
|
||||||
|
limit: FETCH_ALL_LIMIT,
|
||||||
|
offset: 0,
|
||||||
|
user_id,
|
||||||
|
};
|
||||||
|
list_entries(pool, params).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<Entry>> {
|
||||||
|
let (where_clause, idx) = entry_where_clause_and_next_idx(a);
|
||||||
|
|
||||||
let order = match a.sort {
|
let order = match a.sort {
|
||||||
"updated" => "updated_at DESC",
|
"updated" => "updated_at DESC",
|
||||||
"created" => "created_at DESC",
|
"created" => "created_at DESC",
|
||||||
@@ -121,30 +168,22 @@ async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<
|
|||||||
};
|
};
|
||||||
|
|
||||||
let limit_idx = idx;
|
let limit_idx = idx;
|
||||||
idx += 1;
|
let offset_idx = idx + 1;
|
||||||
let offset_idx = idx;
|
|
||||||
|
|
||||||
let where_clause = if conditions.is_empty() {
|
|
||||||
String::new()
|
|
||||||
} else {
|
|
||||||
format!("WHERE {}", conditions.join(" AND "))
|
|
||||||
};
|
|
||||||
|
|
||||||
let sql = format!(
|
let sql = format!(
|
||||||
"SELECT id, COALESCE(user_id, '00000000-0000-0000-0000-000000000000'::uuid) AS user_id, \
|
"SELECT id, user_id, folder, type, name, notes, tags, metadata, version, \
|
||||||
namespace, kind, name, tags, metadata, version, created_at, updated_at \
|
created_at, updated_at \
|
||||||
FROM entries {where_clause} ORDER BY {order} LIMIT ${limit_idx} OFFSET ${offset_idx}"
|
FROM entries {where_clause} ORDER BY {order} LIMIT ${limit_idx} OFFSET ${offset_idx}"
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut q = sqlx::query_as::<_, EntryRaw>(&sql);
|
let mut q = sqlx::query_as::<_, EntryRaw>(&sql);
|
||||||
|
|
||||||
if let Some(uid) = a.user_id {
|
if let Some(uid) = a.user_id {
|
||||||
q = q.bind(uid);
|
q = q.bind(uid);
|
||||||
}
|
}
|
||||||
if let Some(v) = a.namespace {
|
if let Some(v) = a.folder {
|
||||||
q = q.bind(v);
|
q = q.bind(v);
|
||||||
}
|
}
|
||||||
if let Some(v) = a.kind {
|
if let Some(v) = a.entry_type {
|
||||||
q = q.bind(v);
|
q = q.bind(v);
|
||||||
}
|
}
|
||||||
if let Some(v) = a.name {
|
if let Some(v) = a.name {
|
||||||
@@ -171,8 +210,12 @@ pub async fn fetch_secret_schemas(
|
|||||||
if entry_ids.is_empty() {
|
if entry_ids.is_empty() {
|
||||||
return Ok(HashMap::new());
|
return Ok(HashMap::new());
|
||||||
}
|
}
|
||||||
let fields: Vec<SecretField> = sqlx::query_as(
|
let fields: Vec<EntrySecretRow> = sqlx::query_as(
|
||||||
"SELECT * FROM secrets WHERE entry_id = ANY($1) ORDER BY entry_id, field_name",
|
"SELECT es.entry_id, s.id, s.user_id, s.name, s.type, s.encrypted, s.version, s.created_at, s.updated_at \
|
||||||
|
FROM entry_secrets es \
|
||||||
|
JOIN secrets s ON s.id = es.secret_id \
|
||||||
|
WHERE es.entry_id = ANY($1) \
|
||||||
|
ORDER BY es.entry_id, es.sort_order, s.name",
|
||||||
)
|
)
|
||||||
.bind(entry_ids)
|
.bind(entry_ids)
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
@@ -180,7 +223,8 @@ pub async fn fetch_secret_schemas(
|
|||||||
|
|
||||||
let mut map: HashMap<Uuid, Vec<SecretField>> = HashMap::new();
|
let mut map: HashMap<Uuid, Vec<SecretField>> = HashMap::new();
|
||||||
for f in fields {
|
for f in fields {
|
||||||
map.entry(f.entry_id).or_default().push(f);
|
let entry_id = f.entry_id;
|
||||||
|
map.entry(entry_id).or_default().push(f.secret());
|
||||||
}
|
}
|
||||||
Ok(map)
|
Ok(map)
|
||||||
}
|
}
|
||||||
@@ -193,8 +237,12 @@ pub async fn fetch_secrets_for_entries(
|
|||||||
if entry_ids.is_empty() {
|
if entry_ids.is_empty() {
|
||||||
return Ok(HashMap::new());
|
return Ok(HashMap::new());
|
||||||
}
|
}
|
||||||
let fields: Vec<SecretField> = sqlx::query_as(
|
let fields: Vec<EntrySecretRow> = sqlx::query_as(
|
||||||
"SELECT * FROM secrets WHERE entry_id = ANY($1) ORDER BY entry_id, field_name",
|
"SELECT es.entry_id, s.id, s.user_id, s.name, s.type, s.encrypted, s.version, s.created_at, s.updated_at \
|
||||||
|
FROM entry_secrets es \
|
||||||
|
JOIN secrets s ON s.id = es.secret_id \
|
||||||
|
WHERE es.entry_id = ANY($1) \
|
||||||
|
ORDER BY es.entry_id, es.sort_order, s.name",
|
||||||
)
|
)
|
||||||
.bind(entry_ids)
|
.bind(entry_ids)
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
@@ -202,21 +250,87 @@ pub async fn fetch_secrets_for_entries(
|
|||||||
|
|
||||||
let mut map: HashMap<Uuid, Vec<SecretField>> = HashMap::new();
|
let mut map: HashMap<Uuid, Vec<SecretField>> = HashMap::new();
|
||||||
for f in fields {
|
for f in fields {
|
||||||
map.entry(f.entry_id).or_default().push(f);
|
let entry_id = f.entry_id;
|
||||||
|
map.entry(entry_id).or_default().push(f.secret());
|
||||||
}
|
}
|
||||||
Ok(map)
|
Ok(map)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Internal raw row (because user_id is nullable in DB) ─────────────────────
|
/// Resolve exactly one entry by its UUID primary key.
|
||||||
|
///
|
||||||
|
/// Returns an error if the entry does not exist or does not belong to the given user.
|
||||||
|
pub async fn resolve_entry_by_id(
|
||||||
|
pool: &PgPool,
|
||||||
|
id: Uuid,
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
) -> Result<crate::models::Entry> {
|
||||||
|
let row: Option<EntryRaw> = if let Some(uid) = user_id {
|
||||||
|
sqlx::query_as(
|
||||||
|
"SELECT id, user_id, folder, type, name, notes, tags, metadata, version, \
|
||||||
|
created_at, updated_at FROM entries WHERE id = $1 AND user_id = $2",
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.bind(uid)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
sqlx::query_as(
|
||||||
|
"SELECT id, user_id, folder, type, name, notes, tags, metadata, version, \
|
||||||
|
created_at, updated_at FROM entries WHERE id = $1 AND user_id IS NULL",
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
|
row.map(Entry::from)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Entry with id '{}' not found", id))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolve exactly one entry by name, with optional folder for disambiguation.
|
||||||
|
///
|
||||||
|
/// - If `folder` is provided: exact `(folder, name)` match.
|
||||||
|
/// - If `folder` is None and exactly one entry matches: returns it.
|
||||||
|
/// - If `folder` is None and multiple entries match: returns an error listing
|
||||||
|
/// the folders and asking the caller to specify one.
|
||||||
|
pub async fn resolve_entry(
|
||||||
|
pool: &PgPool,
|
||||||
|
name: &str,
|
||||||
|
folder: Option<&str>,
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
) -> Result<crate::models::Entry> {
|
||||||
|
let entries = fetch_entries(pool, folder, None, Some(name), &[], None, user_id).await?;
|
||||||
|
match entries.len() {
|
||||||
|
0 => {
|
||||||
|
if let Some(f) = folder {
|
||||||
|
anyhow::bail!("Not found: '{}' in folder '{}'", name, f)
|
||||||
|
} else {
|
||||||
|
anyhow::bail!("Not found: '{}'", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
1 => Ok(entries.into_iter().next().unwrap()),
|
||||||
|
_ => {
|
||||||
|
let folders: Vec<&str> = entries.iter().map(|e| e.folder.as_str()).collect();
|
||||||
|
anyhow::bail!(
|
||||||
|
"Ambiguous: {} entries named '{}' found in folders: [{}]. \
|
||||||
|
Specify 'folder' to disambiguate.",
|
||||||
|
entries.len(),
|
||||||
|
name,
|
||||||
|
folders.join(", ")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Internal raw row (because user_id is nullable in DB) ─────────────────────
|
||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
struct EntryRaw {
|
struct EntryRaw {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
#[allow(dead_code)] // Selected for row shape; Entry model has no user_id field
|
user_id: Option<Uuid>,
|
||||||
user_id: Uuid,
|
folder: String,
|
||||||
namespace: String,
|
#[sqlx(rename = "type")]
|
||||||
kind: String,
|
entry_type: String,
|
||||||
name: String,
|
name: String,
|
||||||
|
notes: String,
|
||||||
tags: Vec<String>,
|
tags: Vec<String>,
|
||||||
metadata: Value,
|
metadata: Value,
|
||||||
version: i64,
|
version: i64,
|
||||||
@@ -228,9 +342,11 @@ impl From<EntryRaw> for Entry {
|
|||||||
fn from(r: EntryRaw) -> Self {
|
fn from(r: EntryRaw) -> Self {
|
||||||
Entry {
|
Entry {
|
||||||
id: r.id,
|
id: r.id,
|
||||||
namespace: r.namespace,
|
user_id: r.user_id,
|
||||||
kind: r.kind,
|
folder: r.folder,
|
||||||
|
entry_type: r.entry_type,
|
||||||
name: r.name,
|
name: r.name,
|
||||||
|
notes: r.notes,
|
||||||
tags: r.tags,
|
tags: r.tags,
|
||||||
metadata: r.metadata,
|
metadata: r.metadata,
|
||||||
version: r.version,
|
version: r.version,
|
||||||
@@ -239,3 +355,32 @@ impl From<EntryRaw> for Entry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow)]
|
||||||
|
struct EntrySecretRow {
|
||||||
|
entry_id: Uuid,
|
||||||
|
id: Uuid,
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
name: String,
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
secret_type: String,
|
||||||
|
encrypted: Vec<u8>,
|
||||||
|
version: i64,
|
||||||
|
created_at: chrono::DateTime<chrono::Utc>,
|
||||||
|
updated_at: chrono::DateTime<chrono::Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EntrySecretRow {
|
||||||
|
fn secret(self) -> SecretField {
|
||||||
|
SecretField {
|
||||||
|
id: self.id,
|
||||||
|
user_id: self.user_id,
|
||||||
|
name: self.name,
|
||||||
|
secret_type: self.secret_type,
|
||||||
|
encrypted: self.encrypted,
|
||||||
|
version: self.version,
|
||||||
|
created_at: self.created_at,
|
||||||
|
updated_at: self.updated_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,17 +5,18 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use crate::crypto;
|
use crate::crypto;
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::models::EntryRow;
|
use crate::models::{EntryRow, EntryWriteRow};
|
||||||
use crate::service::add::{
|
use crate::service::add::{
|
||||||
collect_field_paths, collect_key_paths, flatten_json_fields, insert_path, parse_key_path,
|
collect_field_paths, collect_key_paths, flatten_json_fields, infer_secret_type, insert_path,
|
||||||
parse_kv, remove_path,
|
parse_key_path, parse_kv, remove_path,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
pub struct UpdateResult {
|
pub struct UpdateResult {
|
||||||
pub namespace: String,
|
|
||||||
pub kind: String,
|
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub folder: String,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
pub entry_type: String,
|
||||||
pub add_tags: Vec<String>,
|
pub add_tags: Vec<String>,
|
||||||
pub remove_tags: Vec<String>,
|
pub remove_tags: Vec<String>,
|
||||||
pub meta_keys: Vec<String>,
|
pub meta_keys: Vec<String>,
|
||||||
@@ -25,9 +26,10 @@ pub struct UpdateResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct UpdateParams<'a> {
|
pub struct UpdateParams<'a> {
|
||||||
pub namespace: &'a str,
|
|
||||||
pub kind: &'a str,
|
|
||||||
pub name: &'a str,
|
pub name: &'a str,
|
||||||
|
/// Optional folder for disambiguation when multiple entries share the same name.
|
||||||
|
pub folder: Option<&'a str>,
|
||||||
|
pub notes: Option<&'a str>,
|
||||||
pub add_tags: &'a [String],
|
pub add_tags: &'a [String],
|
||||||
pub remove_tags: &'a [String],
|
pub remove_tags: &'a [String],
|
||||||
pub meta_entries: &'a [String],
|
pub meta_entries: &'a [String],
|
||||||
@@ -44,45 +46,76 @@ pub async fn run(
|
|||||||
) -> Result<UpdateResult> {
|
) -> Result<UpdateResult> {
|
||||||
let mut tx = pool.begin().await?;
|
let mut tx = pool.begin().await?;
|
||||||
|
|
||||||
let row: Option<EntryRow> = if let Some(uid) = params.user_id {
|
// Fetch matching rows with FOR UPDATE; use folder when provided to resolve ambiguity.
|
||||||
|
let rows: Vec<EntryRow> = if let Some(uid) = params.user_id {
|
||||||
|
if let Some(folder) = params.folder {
|
||||||
|
sqlx::query_as(
|
||||||
|
"SELECT id, version, folder, type, tags, metadata, notes FROM entries \
|
||||||
|
WHERE user_id = $1 AND folder = $2 AND name = $3 FOR UPDATE",
|
||||||
|
)
|
||||||
|
.bind(uid)
|
||||||
|
.bind(folder)
|
||||||
|
.bind(params.name)
|
||||||
|
.fetch_all(&mut *tx)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
sqlx::query_as(
|
||||||
|
"SELECT id, version, folder, type, tags, metadata, notes FROM entries \
|
||||||
|
WHERE user_id = $1 AND name = $2 FOR UPDATE",
|
||||||
|
)
|
||||||
|
.bind(uid)
|
||||||
|
.bind(params.name)
|
||||||
|
.fetch_all(&mut *tx)
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
} else if let Some(folder) = params.folder {
|
||||||
sqlx::query_as(
|
sqlx::query_as(
|
||||||
"SELECT id, version, tags, metadata FROM entries \
|
"SELECT id, version, folder, type, tags, metadata, notes FROM entries \
|
||||||
WHERE user_id = $1 AND namespace = $2 AND kind = $3 AND name = $4 FOR UPDATE",
|
WHERE user_id IS NULL AND folder = $1 AND name = $2 FOR UPDATE",
|
||||||
)
|
)
|
||||||
.bind(uid)
|
.bind(folder)
|
||||||
.bind(params.namespace)
|
|
||||||
.bind(params.kind)
|
|
||||||
.bind(params.name)
|
.bind(params.name)
|
||||||
.fetch_optional(&mut *tx)
|
.fetch_all(&mut *tx)
|
||||||
.await?
|
.await?
|
||||||
} else {
|
} else {
|
||||||
sqlx::query_as(
|
sqlx::query_as(
|
||||||
"SELECT id, version, tags, metadata FROM entries \
|
"SELECT id, version, folder, type, tags, metadata, notes FROM entries \
|
||||||
WHERE user_id IS NULL AND namespace = $1 AND kind = $2 AND name = $3 FOR UPDATE",
|
WHERE user_id IS NULL AND name = $1 FOR UPDATE",
|
||||||
)
|
)
|
||||||
.bind(params.namespace)
|
|
||||||
.bind(params.kind)
|
|
||||||
.bind(params.name)
|
.bind(params.name)
|
||||||
.fetch_optional(&mut *tx)
|
.fetch_all(&mut *tx)
|
||||||
.await?
|
.await?
|
||||||
};
|
};
|
||||||
|
|
||||||
let row = row.ok_or_else(|| {
|
let row = match rows.len() {
|
||||||
anyhow::anyhow!(
|
0 => {
|
||||||
"Not found: [{}/{}] {}. Use `add` to create it first.",
|
tx.rollback().await?;
|
||||||
params.namespace,
|
anyhow::bail!(
|
||||||
params.kind,
|
"Not found: '{}'. Use `add` to create it first.",
|
||||||
params.name
|
params.name
|
||||||
)
|
)
|
||||||
})?;
|
}
|
||||||
|
1 => rows.into_iter().next().unwrap(),
|
||||||
|
_ => {
|
||||||
|
tx.rollback().await?;
|
||||||
|
let folders: Vec<&str> = rows.iter().map(|r| r.folder.as_str()).collect();
|
||||||
|
anyhow::bail!(
|
||||||
|
"Ambiguous: {} entries named '{}' found in folders: [{}]. \
|
||||||
|
Specify 'folder' to disambiguate.",
|
||||||
|
rows.len(),
|
||||||
|
params.name,
|
||||||
|
folders.join(", ")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if let Err(e) = db::snapshot_entry_history(
|
if let Err(e) = db::snapshot_entry_history(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
db::EntrySnapshotParams {
|
db::EntrySnapshotParams {
|
||||||
entry_id: row.id,
|
entry_id: row.id,
|
||||||
user_id: params.user_id,
|
user_id: params.user_id,
|
||||||
namespace: params.namespace,
|
folder: &row.folder,
|
||||||
kind: params.kind,
|
entry_type: &row.entry_type,
|
||||||
name: params.name,
|
name: params.name,
|
||||||
version: row.version,
|
version: row.version,
|
||||||
action: "update",
|
action: "update",
|
||||||
@@ -117,12 +150,16 @@ pub async fn run(
|
|||||||
}
|
}
|
||||||
let metadata = Value::Object(meta_map);
|
let metadata = Value::Object(meta_map);
|
||||||
|
|
||||||
|
let new_notes = params.notes.unwrap_or(&row.notes);
|
||||||
|
|
||||||
let result = sqlx::query(
|
let result = sqlx::query(
|
||||||
"UPDATE entries SET tags = $1, metadata = $2, version = version + 1, updated_at = NOW() \
|
"UPDATE entries SET tags = $1, metadata = $2, notes = $3, \
|
||||||
WHERE id = $3 AND version = $4",
|
version = version + 1, updated_at = NOW() \
|
||||||
|
WHERE id = $4 AND version = $5",
|
||||||
)
|
)
|
||||||
.bind(&tags)
|
.bind(&tags)
|
||||||
.bind(&metadata)
|
.bind(&metadata)
|
||||||
|
.bind(new_notes)
|
||||||
.bind(row.id)
|
.bind(row.id)
|
||||||
.bind(row.version)
|
.bind(row.version)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
@@ -131,15 +168,11 @@ pub async fn run(
|
|||||||
if result.rows_affected() == 0 {
|
if result.rows_affected() == 0 {
|
||||||
tx.rollback().await?;
|
tx.rollback().await?;
|
||||||
anyhow::bail!(
|
anyhow::bail!(
|
||||||
"Concurrent modification detected for [{}/{}] {}. Please retry.",
|
"Concurrent modification detected for '{}'. Please retry.",
|
||||||
params.namespace,
|
|
||||||
params.kind,
|
|
||||||
params.name
|
params.name
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let new_version = row.version + 1;
|
|
||||||
|
|
||||||
for entry in params.secret_entries {
|
for entry in params.secret_entries {
|
||||||
let (path, field_value) = parse_kv(entry)?;
|
let (path, field_value) = parse_kv(entry)?;
|
||||||
let flat = flatten_json_fields("", &{
|
let flat = flatten_json_fields("", &{
|
||||||
@@ -157,7 +190,10 @@ pub async fn run(
|
|||||||
encrypted: Vec<u8>,
|
encrypted: Vec<u8>,
|
||||||
}
|
}
|
||||||
let ef: Option<ExistingField> = sqlx::query_as(
|
let ef: Option<ExistingField> = sqlx::query_as(
|
||||||
"SELECT id, encrypted FROM secrets WHERE entry_id = $1 AND field_name = $2",
|
"SELECT s.id, s.encrypted \
|
||||||
|
FROM entry_secrets es \
|
||||||
|
JOIN secrets s ON s.id = es.secret_id \
|
||||||
|
WHERE es.entry_id = $1 AND s.name = $2",
|
||||||
)
|
)
|
||||||
.bind(row.id)
|
.bind(row.id)
|
||||||
.bind(field_name)
|
.bind(field_name)
|
||||||
@@ -168,10 +204,8 @@ pub async fn run(
|
|||||||
&& let Err(e) = db::snapshot_secret_history(
|
&& let Err(e) = db::snapshot_secret_history(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
db::SecretSnapshotParams {
|
db::SecretSnapshotParams {
|
||||||
entry_id: row.id,
|
|
||||||
secret_id: ef.id,
|
secret_id: ef.id,
|
||||||
entry_version: row.version,
|
name: field_name,
|
||||||
field_name,
|
|
||||||
encrypted: &ef.encrypted,
|
encrypted: &ef.encrypted,
|
||||||
action: "update",
|
action: "update",
|
||||||
},
|
},
|
||||||
@@ -181,16 +215,30 @@ pub async fn run(
|
|||||||
tracing::warn!(error = %e, "failed to snapshot secret field history");
|
tracing::warn!(error = %e, "failed to snapshot secret field history");
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlx::query(
|
if let Some(ef) = ef {
|
||||||
"INSERT INTO secrets (entry_id, field_name, encrypted) VALUES ($1, $2, $3) \
|
sqlx::query(
|
||||||
ON CONFLICT (entry_id, field_name) DO UPDATE SET \
|
"UPDATE secrets SET encrypted = $1, version = version + 1, updated_at = NOW() WHERE id = $2",
|
||||||
encrypted = EXCLUDED.encrypted, version = secrets.version + 1, updated_at = NOW()",
|
)
|
||||||
)
|
.bind(&encrypted)
|
||||||
.bind(row.id)
|
.bind(ef.id)
|
||||||
.bind(field_name)
|
.execute(&mut *tx)
|
||||||
.bind(&encrypted)
|
.await?;
|
||||||
.execute(&mut *tx)
|
} else {
|
||||||
.await?;
|
let secret_id: Uuid = sqlx::query_scalar(
|
||||||
|
"INSERT INTO secrets (user_id, name, type, encrypted) VALUES ($1, $2, $3, $4) RETURNING id",
|
||||||
|
)
|
||||||
|
.bind(params.user_id)
|
||||||
|
.bind(field_name)
|
||||||
|
.bind(infer_secret_type(field_name))
|
||||||
|
.bind(&encrypted)
|
||||||
|
.fetch_one(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
sqlx::query("INSERT INTO entry_secrets (entry_id, secret_id) VALUES ($1, $2)")
|
||||||
|
.bind(row.id)
|
||||||
|
.bind(secret_id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,7 +252,10 @@ pub async fn run(
|
|||||||
encrypted: Vec<u8>,
|
encrypted: Vec<u8>,
|
||||||
}
|
}
|
||||||
let field: Option<FieldToDelete> = sqlx::query_as(
|
let field: Option<FieldToDelete> = sqlx::query_as(
|
||||||
"SELECT id, encrypted FROM secrets WHERE entry_id = $1 AND field_name = $2",
|
"SELECT s.id, s.encrypted \
|
||||||
|
FROM entry_secrets es \
|
||||||
|
JOIN secrets s ON s.id = es.secret_id \
|
||||||
|
WHERE es.entry_id = $1 AND s.name = $2",
|
||||||
)
|
)
|
||||||
.bind(row.id)
|
.bind(row.id)
|
||||||
.bind(&field_name)
|
.bind(&field_name)
|
||||||
@@ -215,10 +266,8 @@ pub async fn run(
|
|||||||
if let Err(e) = db::snapshot_secret_history(
|
if let Err(e) = db::snapshot_secret_history(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
db::SecretSnapshotParams {
|
db::SecretSnapshotParams {
|
||||||
entry_id: row.id,
|
|
||||||
secret_id: f.id,
|
secret_id: f.id,
|
||||||
entry_version: new_version,
|
name: &field_name,
|
||||||
field_name: &field_name,
|
|
||||||
encrypted: &f.encrypted,
|
encrypted: &f.encrypted,
|
||||||
action: "delete",
|
action: "delete",
|
||||||
},
|
},
|
||||||
@@ -227,10 +276,19 @@ pub async fn run(
|
|||||||
{
|
{
|
||||||
tracing::warn!(error = %e, "failed to snapshot secret field history before delete");
|
tracing::warn!(error = %e, "failed to snapshot secret field history before delete");
|
||||||
}
|
}
|
||||||
sqlx::query("DELETE FROM secrets WHERE id = $1")
|
sqlx::query("DELETE FROM entry_secrets WHERE entry_id = $1 AND secret_id = $2")
|
||||||
|
.bind(row.id)
|
||||||
.bind(f.id)
|
.bind(f.id)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
sqlx::query(
|
||||||
|
"DELETE FROM secrets s \
|
||||||
|
WHERE s.id = $1 \
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM entry_secrets es WHERE es.secret_id = s.id)",
|
||||||
|
)
|
||||||
|
.bind(f.id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,9 +299,10 @@ pub async fn run(
|
|||||||
|
|
||||||
crate::audit::log_tx(
|
crate::audit::log_tx(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
|
params.user_id,
|
||||||
"update",
|
"update",
|
||||||
params.namespace,
|
"",
|
||||||
params.kind,
|
"",
|
||||||
params.name,
|
params.name,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"add_tags": params.add_tags,
|
"add_tags": params.add_tags,
|
||||||
@@ -259,9 +318,9 @@ pub async fn run(
|
|||||||
tx.commit().await?;
|
tx.commit().await?;
|
||||||
|
|
||||||
Ok(UpdateResult {
|
Ok(UpdateResult {
|
||||||
namespace: params.namespace.to_string(),
|
|
||||||
kind: params.kind.to_string(),
|
|
||||||
name: params.name.to_string(),
|
name: params.name.to_string(),
|
||||||
|
folder: row.folder.clone(),
|
||||||
|
entry_type: row.entry_type.clone(),
|
||||||
add_tags: params.add_tags.to_vec(),
|
add_tags: params.add_tags.to_vec(),
|
||||||
remove_tags: params.remove_tags.to_vec(),
|
remove_tags: params.remove_tags.to_vec(),
|
||||||
meta_keys,
|
meta_keys,
|
||||||
@@ -270,3 +329,118 @@ pub async fn run(
|
|||||||
remove_secrets: remove_secret_keys,
|
remove_secrets: remove_secret_keys,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update non-sensitive entry columns by primary key (multi-tenant: `user_id` must match).
|
||||||
|
/// Does not read or modify `secrets` rows.
|
||||||
|
pub struct UpdateEntryFieldsByIdParams<'a> {
|
||||||
|
pub folder: &'a str,
|
||||||
|
pub entry_type: &'a str,
|
||||||
|
pub name: &'a str,
|
||||||
|
pub notes: &'a str,
|
||||||
|
pub tags: &'a [String],
|
||||||
|
pub metadata: &'a serde_json::Value,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_fields_by_id(
|
||||||
|
pool: &PgPool,
|
||||||
|
entry_id: Uuid,
|
||||||
|
user_id: Uuid,
|
||||||
|
params: UpdateEntryFieldsByIdParams<'_>,
|
||||||
|
) -> Result<()> {
|
||||||
|
if params.folder.len() > 128 {
|
||||||
|
anyhow::bail!("folder must be at most 128 characters");
|
||||||
|
}
|
||||||
|
if params.entry_type.len() > 64 {
|
||||||
|
anyhow::bail!("type must be at most 64 characters");
|
||||||
|
}
|
||||||
|
if params.name.len() > 256 {
|
||||||
|
anyhow::bail!("name must be at most 256 characters");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
|
||||||
|
let row: Option<EntryWriteRow> = sqlx::query_as(
|
||||||
|
"SELECT id, version, folder, type, name, tags, metadata, notes FROM entries \
|
||||||
|
WHERE id = $1 AND user_id = $2 FOR UPDATE",
|
||||||
|
)
|
||||||
|
.bind(entry_id)
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let row = match row {
|
||||||
|
Some(r) => r,
|
||||||
|
None => {
|
||||||
|
tx.rollback().await?;
|
||||||
|
anyhow::bail!("Entry not found");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(e) = db::snapshot_entry_history(
|
||||||
|
&mut tx,
|
||||||
|
db::EntrySnapshotParams {
|
||||||
|
entry_id: row.id,
|
||||||
|
user_id: Some(user_id),
|
||||||
|
folder: &row.folder,
|
||||||
|
entry_type: &row.entry_type,
|
||||||
|
name: &row.name,
|
||||||
|
version: row.version,
|
||||||
|
action: "update",
|
||||||
|
tags: &row.tags,
|
||||||
|
metadata: &row.metadata,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
tracing::warn!(error = %e, "failed to snapshot entry history before web update");
|
||||||
|
}
|
||||||
|
|
||||||
|
let res = sqlx::query(
|
||||||
|
"UPDATE entries SET folder = $1, type = $2, name = $3, notes = $4, tags = $5, metadata = $6, \
|
||||||
|
version = version + 1, updated_at = NOW() \
|
||||||
|
WHERE id = $7 AND version = $8",
|
||||||
|
)
|
||||||
|
.bind(params.folder)
|
||||||
|
.bind(params.entry_type)
|
||||||
|
.bind(params.name)
|
||||||
|
.bind(params.notes)
|
||||||
|
.bind(params.tags)
|
||||||
|
.bind(params.metadata)
|
||||||
|
.bind(row.id)
|
||||||
|
.bind(row.version)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
if let sqlx::Error::Database(ref d) = e
|
||||||
|
&& d.code().as_deref() == Some("23505")
|
||||||
|
{
|
||||||
|
return anyhow::anyhow!(
|
||||||
|
"An entry with this folder and name already exists for your account."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
e.into()
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if res.rows_affected() == 0 {
|
||||||
|
tx.rollback().await?;
|
||||||
|
anyhow::bail!("Concurrent modification detected. Please refresh and try again.");
|
||||||
|
}
|
||||||
|
|
||||||
|
crate::audit::log_tx(
|
||||||
|
&mut tx,
|
||||||
|
Some(user_id),
|
||||||
|
"update",
|
||||||
|
params.folder,
|
||||||
|
params.entry_type,
|
||||||
|
params.name,
|
||||||
|
serde_json::json!({
|
||||||
|
"source": "web",
|
||||||
|
"entry_id": entry_id,
|
||||||
|
"fields": ["folder", "type", "name", "notes", "tags", "metadata"],
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "secrets-mcp"
|
name = "secrets-mcp"
|
||||||
version = "0.1.5"
|
version = "0.3.8"
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
@@ -17,8 +17,10 @@ rmcp = { version = "1", features = ["server", "macros", "transport-streamable-ht
|
|||||||
axum = "0.8"
|
axum = "0.8"
|
||||||
axum-extra = { version = "0.10", features = ["typed-header"] }
|
axum-extra = { version = "0.10", features = ["typed-header"] }
|
||||||
tower = "0.5"
|
tower = "0.5"
|
||||||
tower-http = { version = "0.6", features = ["cors"] }
|
tower-http = { version = "0.6", features = ["cors", "trace"] }
|
||||||
tower-sessions = "0.14"
|
tower-sessions = "0.14"
|
||||||
|
tower-sessions-sqlx-store-chrono = { version = "0.14", features = ["postgres"] }
|
||||||
|
time = "0.3"
|
||||||
|
|
||||||
# OAuth (manual token exchange via reqwest)
|
# OAuth (manual token exchange via reqwest)
|
||||||
reqwest.workspace = true
|
reqwest.workspace = true
|
||||||
|
|||||||
262
crates/secrets-mcp/src/logging.rs
Normal file
262
crates/secrets-mcp/src/logging.rs
Normal file
@@ -0,0 +1,262 @@
|
|||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use axum::{
|
||||||
|
body::{Body, Bytes, to_bytes},
|
||||||
|
extract::{ConnectInfo, Request},
|
||||||
|
http::{
|
||||||
|
HeaderMap, Method, StatusCode,
|
||||||
|
header::{CONTENT_LENGTH, CONTENT_TYPE, USER_AGENT},
|
||||||
|
},
|
||||||
|
middleware::Next,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Axum middleware that logs structured info for every HTTP request.
|
||||||
|
///
|
||||||
|
/// All requests: method, path, status, latency_ms, client_ip, user_agent.
|
||||||
|
/// POST /mcp requests: additionally parses JSON-RPC body for jsonrpc_method,
|
||||||
|
/// tool_name, jsonrpc_id, mcp_session, batch_size.
|
||||||
|
///
|
||||||
|
/// Sensitive headers (Authorization, X-Encryption-Key) and secret values
|
||||||
|
/// are never logged.
|
||||||
|
pub async fn request_logging_middleware(req: Request, next: Next) -> Response {
|
||||||
|
let method = req.method().clone();
|
||||||
|
let path = req.uri().path().to_string();
|
||||||
|
let ip = client_ip(&req);
|
||||||
|
let ua = header_str(req.headers(), USER_AGENT);
|
||||||
|
let content_len = header_str(req.headers(), CONTENT_LENGTH).and_then(|v| v.parse::<u64>().ok());
|
||||||
|
let mcp_session = req
|
||||||
|
.headers()
|
||||||
|
.get("mcp-session-id")
|
||||||
|
.or_else(|| req.headers().get("x-mcp-session"))
|
||||||
|
.and_then(|v| v.to_str().ok())
|
||||||
|
.map(|s| s.to_string());
|
||||||
|
|
||||||
|
let is_mcp_post = path.starts_with("/mcp") && method == Method::POST;
|
||||||
|
let is_json = header_str(req.headers(), CONTENT_TYPE)
|
||||||
|
.map(|ct| ct.contains("application/json"))
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
|
let start = Instant::now();
|
||||||
|
|
||||||
|
// For MCP JSON-RPC POST requests, buffer body to extract JSON-RPC metadata.
|
||||||
|
// We cap at 512 KiB to avoid buffering large payloads.
|
||||||
|
if is_mcp_post && is_json {
|
||||||
|
let cap = content_len.unwrap_or(0);
|
||||||
|
if cap <= 512 * 1024 {
|
||||||
|
let (parts, body) = req.into_parts();
|
||||||
|
match to_bytes(body, 512 * 1024).await {
|
||||||
|
Ok(bytes) => {
|
||||||
|
let rpc = parse_jsonrpc_meta(&bytes);
|
||||||
|
let req = Request::from_parts(parts, Body::from(bytes));
|
||||||
|
let resp = next.run(req).await;
|
||||||
|
let status = resp.status().as_u16();
|
||||||
|
let elapsed = start.elapsed().as_millis();
|
||||||
|
log_mcp_request(
|
||||||
|
&method,
|
||||||
|
&path,
|
||||||
|
status,
|
||||||
|
elapsed,
|
||||||
|
ip.as_deref(),
|
||||||
|
ua.as_deref(),
|
||||||
|
content_len,
|
||||||
|
mcp_session.as_deref(),
|
||||||
|
&rpc,
|
||||||
|
);
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!(path, error = %e, "failed to buffer MCP request body for logging");
|
||||||
|
let elapsed = start.elapsed().as_millis();
|
||||||
|
tracing::info!(
|
||||||
|
method = method.as_str(),
|
||||||
|
path,
|
||||||
|
status = StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
|
||||||
|
elapsed_ms = elapsed,
|
||||||
|
client_ip = ip.as_deref(),
|
||||||
|
ua = ua.as_deref(),
|
||||||
|
content_length = content_len,
|
||||||
|
mcp_session = mcp_session.as_deref(),
|
||||||
|
"mcp request",
|
||||||
|
);
|
||||||
|
return (
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"failed to read request body",
|
||||||
|
)
|
||||||
|
.into_response();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let resp = next.run(req).await;
|
||||||
|
let status = resp.status().as_u16();
|
||||||
|
let elapsed = start.elapsed().as_millis();
|
||||||
|
|
||||||
|
// Known client probe patterns that legitimately 404 — downgrade to debug to
|
||||||
|
// avoid noise in production logs. These are:
|
||||||
|
// • GET /.well-known/* — OAuth/OIDC discovery by MCP clients (RFC 8414 / RFC 9728)
|
||||||
|
// • GET /mcp → 404 — old SSE-transport compatibility probe by clients
|
||||||
|
let is_expected_probe_404 = status == 404
|
||||||
|
&& (path.starts_with("/.well-known/")
|
||||||
|
|| (method == Method::GET && path.starts_with("/mcp")));
|
||||||
|
|
||||||
|
if is_expected_probe_404 {
|
||||||
|
tracing::debug!(
|
||||||
|
method = method.as_str(),
|
||||||
|
path,
|
||||||
|
status,
|
||||||
|
elapsed_ms = elapsed,
|
||||||
|
client_ip = ip.as_deref(),
|
||||||
|
ua = ua.as_deref(),
|
||||||
|
"probe request (not found — expected)",
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
log_http_request(
|
||||||
|
&method,
|
||||||
|
&path,
|
||||||
|
status,
|
||||||
|
elapsed,
|
||||||
|
ip.as_deref(),
|
||||||
|
ua.as_deref(),
|
||||||
|
content_len,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
resp
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Logging helpers ───────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
fn log_http_request(
|
||||||
|
method: &Method,
|
||||||
|
path: &str,
|
||||||
|
status: u16,
|
||||||
|
elapsed_ms: u128,
|
||||||
|
client_ip: Option<&str>,
|
||||||
|
ua: Option<&str>,
|
||||||
|
content_length: Option<u64>,
|
||||||
|
) {
|
||||||
|
tracing::info!(
|
||||||
|
method = method.as_str(),
|
||||||
|
path,
|
||||||
|
status,
|
||||||
|
elapsed_ms,
|
||||||
|
client_ip,
|
||||||
|
ua,
|
||||||
|
content_length,
|
||||||
|
"http request",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
fn log_mcp_request(
|
||||||
|
method: &Method,
|
||||||
|
path: &str,
|
||||||
|
status: u16,
|
||||||
|
elapsed_ms: u128,
|
||||||
|
client_ip: Option<&str>,
|
||||||
|
ua: Option<&str>,
|
||||||
|
content_length: Option<u64>,
|
||||||
|
mcp_session: Option<&str>,
|
||||||
|
rpc: &JsonRpcMeta,
|
||||||
|
) {
|
||||||
|
tracing::info!(
|
||||||
|
method = method.as_str(),
|
||||||
|
path,
|
||||||
|
status,
|
||||||
|
elapsed_ms,
|
||||||
|
client_ip,
|
||||||
|
ua,
|
||||||
|
content_length,
|
||||||
|
mcp_session,
|
||||||
|
jsonrpc = rpc.rpc_method.as_deref(),
|
||||||
|
tool = rpc.tool_name.as_deref(),
|
||||||
|
jsonrpc_id = rpc.request_id.as_deref(),
|
||||||
|
batch_size = rpc.batch_size,
|
||||||
|
"mcp request",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── JSON-RPC body parsing ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct JsonRpcMeta {
|
||||||
|
request_id: Option<String>,
|
||||||
|
rpc_method: Option<String>,
|
||||||
|
tool_name: Option<String>,
|
||||||
|
batch_size: Option<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_jsonrpc_meta(bytes: &Bytes) -> JsonRpcMeta {
|
||||||
|
let Ok(value) = serde_json::from_slice::<serde_json::Value>(bytes) else {
|
||||||
|
return JsonRpcMeta::default();
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(arr) = value.as_array() {
|
||||||
|
// Batch request: summarise method(s) from first element only
|
||||||
|
let first = arr.first().map(parse_single).unwrap_or_default();
|
||||||
|
return JsonRpcMeta {
|
||||||
|
batch_size: Some(arr.len()),
|
||||||
|
..first
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_single(&value)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_single(value: &serde_json::Value) -> JsonRpcMeta {
|
||||||
|
let request_id = value.get("id").and_then(json_to_string);
|
||||||
|
let rpc_method = value
|
||||||
|
.get("method")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.map(|s| s.to_string());
|
||||||
|
let tool_name = value
|
||||||
|
.pointer("/params/name")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.map(|s| s.to_string());
|
||||||
|
|
||||||
|
JsonRpcMeta {
|
||||||
|
request_id,
|
||||||
|
rpc_method,
|
||||||
|
tool_name,
|
||||||
|
batch_size: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn json_to_string(value: &serde_json::Value) -> Option<String> {
|
||||||
|
match value {
|
||||||
|
serde_json::Value::Null => None,
|
||||||
|
serde_json::Value::String(s) => Some(s.clone()),
|
||||||
|
serde_json::Value::Number(n) => Some(n.to_string()),
|
||||||
|
serde_json::Value::Bool(b) => Some(b.to_string()),
|
||||||
|
other => Some(other.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Header helpers ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
fn header_str(headers: &HeaderMap, name: impl axum::http::header::AsHeaderName) -> Option<String> {
|
||||||
|
headers
|
||||||
|
.get(name)
|
||||||
|
.and_then(|v| v.to_str().ok())
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn client_ip(req: &Request) -> Option<String> {
|
||||||
|
if let Some(first) = req
|
||||||
|
.headers()
|
||||||
|
.get("x-forwarded-for")
|
||||||
|
.and_then(|v| v.to_str().ok())
|
||||||
|
.and_then(|s| s.split(',').next())
|
||||||
|
{
|
||||||
|
let s = first.trim();
|
||||||
|
if !s.is_empty() {
|
||||||
|
return Some(s.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
req.extensions()
|
||||||
|
.get::<ConnectInfo<SocketAddr>>()
|
||||||
|
.map(|c| c.ip().to_string())
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
mod auth;
|
mod auth;
|
||||||
|
mod logging;
|
||||||
mod oauth;
|
mod oauth;
|
||||||
mod tools;
|
mod tools;
|
||||||
mod web;
|
mod web;
|
||||||
@@ -14,10 +15,13 @@ use rmcp::transport::streamable_http_server::{
|
|||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use tower_http::cors::{Any, CorsLayer};
|
use tower_http::cors::{Any, CorsLayer};
|
||||||
use tower_sessions::cookie::SameSite;
|
use tower_sessions::cookie::SameSite;
|
||||||
use tower_sessions::{MemoryStore, SessionManagerLayer};
|
use tower_sessions::session_store::ExpiredDeletion;
|
||||||
|
use tower_sessions::{Expiry, SessionManagerLayer};
|
||||||
|
use tower_sessions_sqlx_store_chrono::PostgresStore;
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
use tracing_subscriber::fmt::time::FormatTime;
|
||||||
|
|
||||||
use secrets_core::config::resolve_db_url;
|
use secrets_core::config::resolve_db_config;
|
||||||
use secrets_core::db::{create_pool, migrate};
|
use secrets_core::db::{create_pool, migrate};
|
||||||
|
|
||||||
use crate::oauth::OAuthConfig;
|
use crate::oauth::OAuthConfig;
|
||||||
@@ -46,21 +50,37 @@ fn load_oauth_config(prefix: &str, base_url: &str, path: &str) -> Option<OAuthCo
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Log line timestamps in the process local timezone (honors `TZ` / system zone).
|
||||||
|
#[derive(Clone, Copy, Default)]
|
||||||
|
struct LocalRfc3339Time;
|
||||||
|
|
||||||
|
impl FormatTime for LocalRfc3339Time {
|
||||||
|
fn format_time(&self, w: &mut tracing_subscriber::fmt::format::Writer<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
w,
|
||||||
|
"{}",
|
||||||
|
chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, false)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
// Load .env if present
|
// Load .env if present
|
||||||
let _ = dotenvy::dotenv();
|
let _ = dotenvy::dotenv();
|
||||||
|
|
||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
|
.with_timer(LocalRfc3339Time)
|
||||||
.with_env_filter(
|
.with_env_filter(
|
||||||
EnvFilter::try_from_default_env().unwrap_or_else(|_| "secrets_mcp=info".into()),
|
EnvFilter::try_from_default_env()
|
||||||
|
.unwrap_or_else(|_| "secrets_mcp=info,tower_http=info".into()),
|
||||||
)
|
)
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
// ── Database ──────────────────────────────────────────────────────────────
|
// ── Database ──────────────────────────────────────────────────────────────
|
||||||
let db_url = resolve_db_url("")
|
let db_config = resolve_db_config("")
|
||||||
.context("Database not configured. Set SECRETS_DATABASE_URL environment variable.")?;
|
.context("Database not configured. Set SECRETS_DATABASE_URL environment variable.")?;
|
||||||
let pool = create_pool(&db_url)
|
let pool = create_pool(&db_config)
|
||||||
.await
|
.await
|
||||||
.context("failed to connect to database")?;
|
.context("failed to connect to database")?;
|
||||||
migrate(&pool)
|
migrate(&pool)
|
||||||
@@ -70,7 +90,8 @@ async fn main() -> Result<()> {
|
|||||||
|
|
||||||
// ── Configuration ─────────────────────────────────────────────────────────
|
// ── Configuration ─────────────────────────────────────────────────────────
|
||||||
let base_url = load_env_var("BASE_URL").unwrap_or_else(|| "http://localhost:9315".to_string());
|
let base_url = load_env_var("BASE_URL").unwrap_or_else(|| "http://localhost:9315".to_string());
|
||||||
let bind_addr = load_env_var("SECRETS_MCP_BIND").unwrap_or_else(|| "0.0.0.0:9315".to_string());
|
let bind_addr =
|
||||||
|
load_env_var("SECRETS_MCP_BIND").unwrap_or_else(|| "127.0.0.1:9315".to_string());
|
||||||
|
|
||||||
// ── OAuth providers ───────────────────────────────────────────────────────
|
// ── OAuth providers ───────────────────────────────────────────────────────
|
||||||
let google_config = load_oauth_config("GOOGLE", &base_url, "/auth/google/callback");
|
let google_config = load_oauth_config("GOOGLE", &base_url, "/auth/google/callback");
|
||||||
@@ -81,12 +102,23 @@ async fn main() -> Result<()> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Session store ─────────────────────────────────────────────────────────
|
// ── Session store (PostgreSQL-backed) ─────────────────────────────────────
|
||||||
let session_store = MemoryStore::default();
|
let session_store = PostgresStore::new(pool.clone());
|
||||||
|
session_store
|
||||||
|
.migrate()
|
||||||
|
.await
|
||||||
|
.context("failed to run session table migration")?;
|
||||||
|
// Prune expired rows every hour; task is aborted when the server shuts down.
|
||||||
|
let session_cleanup = tokio::spawn(
|
||||||
|
session_store
|
||||||
|
.clone()
|
||||||
|
.continuously_delete_expired(tokio::time::Duration::from_secs(3600)),
|
||||||
|
);
|
||||||
// Strict would drop the session cookie on redirect from Google → our origin (cross-site nav).
|
// Strict would drop the session cookie on redirect from Google → our origin (cross-site nav).
|
||||||
let session_layer = SessionManagerLayer::new(session_store)
|
let session_layer = SessionManagerLayer::new(session_store)
|
||||||
.with_secure(base_url.starts_with("https://"))
|
.with_secure(base_url.starts_with("https://"))
|
||||||
.with_same_site(SameSite::Lax);
|
.with_same_site(SameSite::Lax)
|
||||||
|
.with_expiry(Expiry::OnInactivity(time::Duration::days(14)));
|
||||||
|
|
||||||
// ── App state ─────────────────────────────────────────────────────────────
|
// ── App state ─────────────────────────────────────────────────────────────
|
||||||
let app_state = AppState {
|
let app_state = AppState {
|
||||||
@@ -120,6 +152,9 @@ async fn main() -> Result<()> {
|
|||||||
let router = Router::new()
|
let router = Router::new()
|
||||||
.merge(web::web_router())
|
.merge(web::web_router())
|
||||||
.nest_service("/mcp", mcp_service)
|
.nest_service("/mcp", mcp_service)
|
||||||
|
.layer(axum::middleware::from_fn(
|
||||||
|
logging::request_logging_middleware,
|
||||||
|
))
|
||||||
.layer(axum::middleware::from_fn_with_state(
|
.layer(axum::middleware::from_fn_with_state(
|
||||||
pool,
|
pool,
|
||||||
auth::bearer_auth_middleware,
|
auth::bearer_auth_middleware,
|
||||||
@@ -144,6 +179,7 @@ async fn main() -> Result<()> {
|
|||||||
.await
|
.await
|
||||||
.context("server error")?;
|
.context("server error")?;
|
||||||
|
|
||||||
|
session_cleanup.abort();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
28
crates/secrets-mcp/static/llms.txt
Normal file
28
crates/secrets-mcp/static/llms.txt
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# Secrets MCP
|
||||||
|
|
||||||
|
> 给 AI 与自动化工具的简要说明。本站是 **secrets-mcp**:Streamable HTTP **MCP**(Model Context Protocol)与 **Web 控制台** 的组合,用于在多租户场景下存储条目元数据与加密后的秘密字段;持久化在 PostgreSQL。用户通过 OAuth(如已配置)登录 Web;MCP 调用使用 API Key 与加密相关请求头。
|
||||||
|
|
||||||
|
## 公开页面
|
||||||
|
|
||||||
|
- **`/`**:公开首页,说明安全架构(客户端密钥派生、密文存储、多租户与审计等),无需登录。
|
||||||
|
|
||||||
|
## 不应抓取或索引的内容
|
||||||
|
|
||||||
|
- **`/mcp`**:MCP 流式 HTTP 端点(JSON-RPC 等),**不是** HTML 文档,也不适合作为公开知识库来源。
|
||||||
|
- **`/api/*`**:会话或 API Key 相关的 HTTP API。
|
||||||
|
- **`/login`**:登录入口页(`noindex` / robots 通常 disallow)。
|
||||||
|
- **`/dashboard`、`/audit`、`/auth/*`、`/account/*`**:需浏览器会话,属于用户私有界面与 OAuth 流程。
|
||||||
|
|
||||||
|
## 给 AI 助手的实用提示
|
||||||
|
|
||||||
|
- 向用户说明连接方式时:MCP 基址为 `{BASE_URL}/mcp`(`BASE_URL` 由部署方设置),通常需要 `Authorization: Bearer <api_key>`;读写加密秘密时还需按部署文档传递 `X-Encryption-Key` 等头(与客户端模式有关)。
|
||||||
|
- **不要编造**本实例的数据库 URL、OAuth 密钥、回调地址或任何凭据;一律以用户环境变量与运维文档为准。
|
||||||
|
- Web 端在浏览器内用密码短语派生密钥完成端到端加密;MCP 路径下服务端可能在请求周期内临时使用客户端提供的密钥处理密文(架构细节见项目 README「加密架构」)。
|
||||||
|
|
||||||
|
## 延伸阅读
|
||||||
|
|
||||||
|
- 源码仓库:<https://gitea.refining.dev/refining/secrets>(`README.md`、`AGENTS.md` 含环境变量、表结构与运维约定)。
|
||||||
|
|
||||||
|
## 关于本文件
|
||||||
|
|
||||||
|
- 遵循常见的 **`/llms.txt`** 约定,便于人类与 LLM 快速了解站点性质与抓取边界;同文可在 **`/ai.txt`** 获取。
|
||||||
31
crates/secrets-mcp/static/robots.txt
Normal file
31
crates/secrets-mcp/static/robots.txt
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# Secrets MCP — robots.txt
|
||||||
|
# 本站为需登录的私密控制台与 MCP API;以下路径请勿抓取,以免浪费配额并避免误索引敏感端点。
|
||||||
|
# This host serves an authenticated dashboard and machine APIs; please skip crawling the paths below.
|
||||||
|
|
||||||
|
User-agent: *
|
||||||
|
Disallow: /mcp
|
||||||
|
Disallow: /api/
|
||||||
|
Disallow: /dashboard
|
||||||
|
Disallow: /audit
|
||||||
|
Disallow: /auth/
|
||||||
|
Disallow: /login
|
||||||
|
Disallow: /account/
|
||||||
|
|
||||||
|
# 首页 `/` 为公开安全说明页,允许抓取。
|
||||||
|
|
||||||
|
# 面向 AI / LLM 的机器可读站点说明(Markdown):/llms.txt
|
||||||
|
# Human & AI-readable site summary: /llms.txt (also /ai.txt)
|
||||||
|
|
||||||
|
User-agent: GPTBot
|
||||||
|
User-agent: Google-Extended
|
||||||
|
User-agent: anthropic-ai
|
||||||
|
User-agent: Claude-Web
|
||||||
|
User-agent: PerplexityBot
|
||||||
|
User-agent: Bytespider
|
||||||
|
Disallow: /mcp
|
||||||
|
Disallow: /api/
|
||||||
|
Disallow: /dashboard
|
||||||
|
Disallow: /audit
|
||||||
|
Disallow: /auth/
|
||||||
|
Disallow: /login
|
||||||
|
Disallow: /account/
|
||||||
155
crates/secrets-mcp/templates/audit.html
Normal file
155
crates/secrets-mcp/templates/audit.html
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<link rel="icon" href="/favicon.svg?v={{ version }}" type="image/svg+xml">
|
||||||
|
<title>Secrets — Audit</title>
|
||||||
|
<style>
|
||||||
|
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
|
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600&family=Inter:wght@400;500;600&display=swap');
|
||||||
|
:root {
|
||||||
|
--bg: #0d1117; --surface: #161b22; --surface2: #21262d;
|
||||||
|
--border: #30363d; --text: #e6edf3; --text-muted: #8b949e;
|
||||||
|
--accent: #58a6ff; --accent-hover: #79b8ff;
|
||||||
|
}
|
||||||
|
body { background: var(--bg); color: var(--text); font-family: 'Inter', sans-serif; min-height: 100vh; }
|
||||||
|
.layout { display: flex; min-height: 100vh; }
|
||||||
|
.sidebar {
|
||||||
|
width: 220px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
|
||||||
|
padding: 24px 16px; display: flex; flex-direction: column; gap: 20px;
|
||||||
|
}
|
||||||
|
.sidebar-logo { font-family: 'JetBrains Mono', monospace; font-size: 16px; font-weight: 600;
|
||||||
|
color: var(--text); text-decoration: none; padding: 0 10px; }
|
||||||
|
.sidebar-logo span { color: var(--accent); }
|
||||||
|
.sidebar-menu { display: flex; flex-direction: column; gap: 6px; }
|
||||||
|
.sidebar-link {
|
||||||
|
padding: 10px 12px; border-radius: 8px; color: var(--text-muted); text-decoration: none;
|
||||||
|
border: 1px solid transparent; font-size: 13px; font-weight: 500;
|
||||||
|
}
|
||||||
|
.sidebar-link:hover { background: var(--surface2); color: var(--text); }
|
||||||
|
.sidebar-link.active {
|
||||||
|
background: rgba(88,166,255,0.12); color: var(--text); border-color: rgba(88,166,255,0.35);
|
||||||
|
}
|
||||||
|
.content-shell { flex: 1; min-width: 0; display: flex; flex-direction: column; }
|
||||||
|
.topbar {
|
||||||
|
background: var(--surface); border-bottom: 1px solid var(--border); padding: 0 24px;
|
||||||
|
display: flex; align-items: center; gap: 12px; min-height: 52px;
|
||||||
|
}
|
||||||
|
.topbar-spacer { flex: 1; }
|
||||||
|
.nav-user { font-size: 13px; color: var(--text-muted); }
|
||||||
|
.btn-sign-out {
|
||||||
|
padding: 5px 12px; border-radius: 6px; border: 1px solid var(--border);
|
||||||
|
background: none; color: var(--text); font-size: 12px; text-decoration: none; cursor: pointer;
|
||||||
|
}
|
||||||
|
.btn-sign-out:hover { background: var(--surface2); }
|
||||||
|
.main { padding: 32px 24px 40px; flex: 1; }
|
||||||
|
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
|
||||||
|
padding: 24px; width: 100%; max-width: 1180px; margin: 0 auto; }
|
||||||
|
.card-title { font-size: 20px; font-weight: 600; margin-bottom: 8px; }
|
||||||
|
.card-subtitle { color: var(--text-muted); font-size: 13px; margin-bottom: 20px; }
|
||||||
|
.empty { color: var(--text-muted); font-size: 14px; padding: 20px 0; }
|
||||||
|
table { width: 100%; border-collapse: collapse; }
|
||||||
|
th, td { text-align: left; vertical-align: top; padding: 12px 10px; border-top: 1px solid var(--border); }
|
||||||
|
th { color: var(--text-muted); font-size: 12px; font-weight: 600; }
|
||||||
|
td { font-size: 13px; }
|
||||||
|
.mono { font-family: 'JetBrains Mono', monospace; }
|
||||||
|
.detail {
|
||||||
|
background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
|
||||||
|
padding: 10px; white-space: pre-wrap; word-break: break-word; font-size: 12px;
|
||||||
|
max-width: 460px;
|
||||||
|
}
|
||||||
|
@media (max-width: 900px) {
|
||||||
|
.layout { flex-direction: column; }
|
||||||
|
.sidebar {
|
||||||
|
width: 100%; border-right: none; border-bottom: 1px solid var(--border);
|
||||||
|
padding: 16px; gap: 14px;
|
||||||
|
}
|
||||||
|
.sidebar-menu { flex-direction: row; }
|
||||||
|
.sidebar-link { flex: 1; text-align: center; }
|
||||||
|
.main { padding: 20px 12px 28px; }
|
||||||
|
.card { padding: 16px; }
|
||||||
|
.topbar { padding: 12px 16px; flex-wrap: wrap; }
|
||||||
|
table, thead, tbody, th, td, tr { display: block; }
|
||||||
|
thead { display: none; }
|
||||||
|
tr { border-top: 1px solid var(--border); padding: 12px 0; }
|
||||||
|
td { border-top: none; padding: 6px 0; }
|
||||||
|
td::before {
|
||||||
|
display: block; color: var(--text-muted); font-size: 11px;
|
||||||
|
margin-bottom: 4px; text-transform: uppercase;
|
||||||
|
}
|
||||||
|
td.col-time::before { content: "Time"; }
|
||||||
|
td.col-action::before { content: "Action"; }
|
||||||
|
td.col-target::before { content: "Target"; }
|
||||||
|
td.col-detail::before { content: "Detail"; }
|
||||||
|
.detail { max-width: none; }
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="layout">
|
||||||
|
<aside class="sidebar">
|
||||||
|
<a href="/dashboard" class="sidebar-logo"><span>secrets</span></a>
|
||||||
|
<nav class="sidebar-menu">
|
||||||
|
<a href="/dashboard" class="sidebar-link">MCP</a>
|
||||||
|
<a href="/entries" class="sidebar-link">条目</a>
|
||||||
|
<a href="/audit" class="sidebar-link active">审计</a>
|
||||||
|
</nav>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<div class="content-shell">
|
||||||
|
<div class="topbar">
|
||||||
|
<span class="topbar-spacer"></span>
|
||||||
|
<span class="nav-user">{{ user_name }}{% if !user_email.is_empty() %} · {{ user_email }}{% endif %}</span>
|
||||||
|
<form action="/auth/logout" method="post" style="display:inline">
|
||||||
|
<button type="submit" class="btn-sign-out">退出</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<main class="main">
|
||||||
|
<section class="card">
|
||||||
|
<div class="card-title">我的审计</div>
|
||||||
|
<div class="card-subtitle">展示最近 100 条与当前用户相关的新审计记录。时间为浏览器本地时区。</div>
|
||||||
|
|
||||||
|
{% if entries.is_empty() %}
|
||||||
|
<div class="empty">暂无审计记录。</div>
|
||||||
|
{% else %}
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>时间</th>
|
||||||
|
<th>动作</th>
|
||||||
|
<th>目标</th>
|
||||||
|
<th>详情</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for entry in entries %}
|
||||||
|
<tr>
|
||||||
|
<td class="col-time mono"><time class="audit-local-time" datetime="{{ entry.created_at_iso }}">{{ entry.created_at_iso }}</time></td>
|
||||||
|
<td class="col-action mono">{{ entry.action }}</td>
|
||||||
|
<td class="col-target mono">{{ entry.target }}</td>
|
||||||
|
<td class="col-detail"><pre class="detail">{{ entry.detail }}</pre></td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
{% endif %}
|
||||||
|
</section>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
(function () {
|
||||||
|
document.querySelectorAll('time.audit-local-time[datetime]').forEach(function (el) {
|
||||||
|
var raw = el.getAttribute('datetime');
|
||||||
|
var d = raw ? new Date(raw) : null;
|
||||||
|
if (d && !isNaN(d.getTime())) {
|
||||||
|
el.textContent = d.toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'medium' });
|
||||||
|
el.title = raw + ' (UTC)';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -16,13 +16,29 @@
|
|||||||
}
|
}
|
||||||
body { background: var(--bg); color: var(--text); font-family: 'Inter', sans-serif; min-height: 100vh; }
|
body { background: var(--bg); color: var(--text); font-family: 'Inter', sans-serif; min-height: 100vh; }
|
||||||
|
|
||||||
/* Nav */
|
.layout { display: flex; min-height: 100vh; }
|
||||||
.nav { background: var(--surface); border-bottom: 1px solid var(--border);
|
.sidebar {
|
||||||
padding: 0 24px; display: flex; align-items: center; gap: 12px; height: 52px; }
|
width: 220px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
|
||||||
.nav-logo { font-family: 'JetBrains Mono', monospace; font-size: 15px; font-weight: 600;
|
padding: 24px 16px; display: flex; flex-direction: column; gap: 20px;
|
||||||
color: var(--text); text-decoration: none; }
|
}
|
||||||
.nav-logo span { color: var(--accent); }
|
.sidebar-logo { font-family: 'JetBrains Mono', monospace; font-size: 16px; font-weight: 600;
|
||||||
.nav-spacer { flex: 1; }
|
color: var(--text); text-decoration: none; padding: 0 10px; }
|
||||||
|
.sidebar-logo span { color: var(--accent); }
|
||||||
|
.sidebar-menu { display: flex; flex-direction: column; gap: 6px; }
|
||||||
|
.sidebar-link {
|
||||||
|
padding: 10px 12px; border-radius: 8px; color: var(--text-muted); text-decoration: none;
|
||||||
|
border: 1px solid transparent; font-size: 13px; font-weight: 500;
|
||||||
|
}
|
||||||
|
.sidebar-link:hover { background: var(--surface2); color: var(--text); }
|
||||||
|
.sidebar-link.active {
|
||||||
|
background: rgba(88,166,255,0.12); color: var(--text); border-color: rgba(88,166,255,0.35);
|
||||||
|
}
|
||||||
|
.content-shell { flex: 1; min-width: 0; display: flex; flex-direction: column; }
|
||||||
|
.topbar {
|
||||||
|
background: var(--surface); border-bottom: 1px solid var(--border); padding: 0 24px;
|
||||||
|
display: flex; align-items: center; gap: 12px; min-height: 52px;
|
||||||
|
}
|
||||||
|
.topbar-spacer { flex: 1; }
|
||||||
.nav-user { font-size: 13px; color: var(--text-muted); }
|
.nav-user { font-size: 13px; color: var(--text-muted); }
|
||||||
.lang-bar { display: flex; gap: 2px; background: var(--surface2); border-radius: 6px; padding: 2px; }
|
.lang-bar { display: flex; gap: 2px; background: var(--surface2); border-radius: 6px; padding: 2px; }
|
||||||
.lang-btn { padding: 3px 9px; border: none; background: none; color: var(--text-muted);
|
.lang-btn { padding: 3px 9px; border: none; background: none; color: var(--text-muted);
|
||||||
@@ -32,11 +48,19 @@
|
|||||||
background: none; color: var(--text); font-size: 12px; cursor: pointer; }
|
background: none; color: var(--text); font-size: 12px; cursor: pointer; }
|
||||||
.btn-sign-out:hover { background: var(--surface2); }
|
.btn-sign-out:hover { background: var(--surface2); }
|
||||||
|
|
||||||
/* Main: column so footer can sit at bottom of viewport when content is short */
|
/* Main content column */
|
||||||
.main { display: flex; flex-direction: column; align-items: center;
|
.main { display: flex; flex-direction: column; align-items: center;
|
||||||
padding: 48px 24px 24px; min-height: calc(100vh - 52px); }
|
padding: 24px 20px 8px; min-height: 0; }
|
||||||
|
.app-footer {
|
||||||
|
margin-top: auto;
|
||||||
|
text-align: center;
|
||||||
|
padding: 4px 20px 12px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: #9da7b3;
|
||||||
|
font-family: 'JetBrains Mono', monospace;
|
||||||
|
}
|
||||||
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
|
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
|
||||||
padding: 32px; width: 100%; max-width: 980px; }
|
padding: 24px; width: 100%; max-width: 980px; }
|
||||||
.card-title { font-size: 18px; font-weight: 600; margin-bottom: 24px; }
|
.card-title { font-size: 18px; font-weight: 600; margin-bottom: 24px; }
|
||||||
/* Form */
|
/* Form */
|
||||||
.field { margin-bottom: 12px; }
|
.field { margin-bottom: 12px; }
|
||||||
@@ -123,28 +147,41 @@
|
|||||||
background: none; color: var(--text); font-size: 13px; cursor: pointer; }
|
background: none; color: var(--text); font-size: 13px; cursor: pointer; }
|
||||||
.btn-modal-cancel:hover { background: var(--surface2); }
|
.btn-modal-cancel:hover { background: var(--surface2); }
|
||||||
|
|
||||||
@media (max-width: 720px) {
|
@media (max-width: 900px) {
|
||||||
.config-tabs { grid-template-columns: 1fr; }
|
.layout { flex-direction: column; }
|
||||||
|
.sidebar {
|
||||||
|
width: 100%; border-right: none; border-bottom: 1px solid var(--border);
|
||||||
|
padding: 16px; gap: 14px;
|
||||||
|
}
|
||||||
|
.sidebar-menu { flex-direction: row; }
|
||||||
|
.sidebar-link { flex: 1; text-align: center; }
|
||||||
}
|
}
|
||||||
|
|
||||||
.app-footer {
|
@media (max-width: 720px) {
|
||||||
margin-top: auto;
|
.config-tabs { grid-template-columns: 1fr; }
|
||||||
width: 100%;
|
.topbar { padding: 12px 16px; flex-wrap: wrap; }
|
||||||
max-width: 980px;
|
.main { padding: 16px 12px 6px; }
|
||||||
flex-shrink: 0;
|
.app-footer { padding: 4px 12px 10px; }
|
||||||
text-align: center;
|
.card { padding: 18px; }
|
||||||
padding-top: 28px;
|
|
||||||
font-size: 12px;
|
|
||||||
color: #9da7b3;
|
|
||||||
font-family: 'JetBrains Mono', monospace;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body data-has-passphrase="{{ has_passphrase }}" data-base-url="{{ base_url }}">
|
<body data-has-passphrase="{{ has_passphrase }}" data-base-url="{{ base_url }}">
|
||||||
|
|
||||||
<nav class="nav">
|
<div class="layout">
|
||||||
<a href="/dashboard" class="nav-logo"><span>secrets</span></a>
|
<aside class="sidebar">
|
||||||
<span class="nav-spacer"></span>
|
<a href="/dashboard" class="sidebar-logo"><span>secrets</span></a>
|
||||||
|
<nav class="sidebar-menu">
|
||||||
|
<a href="/dashboard" class="sidebar-link active">MCP</a>
|
||||||
|
<a href="/entries" class="sidebar-link">条目</a>
|
||||||
|
<a href="/audit" class="sidebar-link">审计</a>
|
||||||
|
</nav>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<div class="content-shell">
|
||||||
|
<div class="topbar">
|
||||||
|
<span class="topbar-spacer"></span>
|
||||||
<span class="nav-user">{{ user_name }}{% if !user_email.is_empty() %} · {{ user_email }}{% endif %}</span>
|
<span class="nav-user">{{ user_name }}{% if !user_email.is_empty() %} · {{ user_email }}{% endif %}</span>
|
||||||
<div class="lang-bar">
|
<div class="lang-bar">
|
||||||
<button class="lang-btn" onclick="setLang('zh-CN')">简</button>
|
<button class="lang-btn" onclick="setLang('zh-CN')">简</button>
|
||||||
@@ -154,7 +191,7 @@
|
|||||||
<form action="/auth/logout" method="post" style="display:inline">
|
<form action="/auth/logout" method="post" style="display:inline">
|
||||||
<button type="submit" class="btn-sign-out" data-i18n="signOut">退出</button>
|
<button type="submit" class="btn-sign-out" data-i18n="signOut">退出</button>
|
||||||
</form>
|
</form>
|
||||||
</nav>
|
</div>
|
||||||
|
|
||||||
<div class="main">
|
<div class="main">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
@@ -258,9 +295,11 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
<footer class="app-footer">{{ version }}</footer>
|
<footer class="app-footer">{{ version }}</footer>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- ── Change passphrase modal ──────────────────────────────────────────────── -->
|
<!-- ── Change passphrase modal ──────────────────────────────────────────────── -->
|
||||||
<div class="modal-bd" id="change-modal">
|
<div class="modal-bd" id="change-modal">
|
||||||
|
|||||||
490
crates/secrets-mcp/templates/entries.html
Normal file
490
crates/secrets-mcp/templates/entries.html
Normal file
@@ -0,0 +1,490 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<link rel="icon" href="/favicon.svg?v={{ version }}" type="image/svg+xml">
|
||||||
|
<title>Secrets — 条目</title>
|
||||||
|
<style>
|
||||||
|
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
|
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600&family=Inter:wght@400;500;600&display=swap');
|
||||||
|
:root {
|
||||||
|
--bg: #0d1117; --surface: #161b22; --surface2: #21262d;
|
||||||
|
--border: #30363d; --text: #e6edf3; --text-muted: #8b949e;
|
||||||
|
--accent: #58a6ff; --accent-hover: #79b8ff;
|
||||||
|
}
|
||||||
|
body { background: var(--bg); color: var(--text); font-family: 'Inter', sans-serif; min-height: 100vh; }
|
||||||
|
.layout { display: flex; min-height: 100vh; }
|
||||||
|
.sidebar {
|
||||||
|
width: 220px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
|
||||||
|
padding: 24px 16px; display: flex; flex-direction: column; gap: 20px;
|
||||||
|
}
|
||||||
|
.sidebar-logo { font-family: 'JetBrains Mono', monospace; font-size: 16px; font-weight: 600;
|
||||||
|
color: var(--text); text-decoration: none; padding: 0 10px; }
|
||||||
|
.sidebar-logo span { color: var(--accent); }
|
||||||
|
.sidebar-menu { display: flex; flex-direction: column; gap: 6px; }
|
||||||
|
.sidebar-link {
|
||||||
|
padding: 10px 12px; border-radius: 8px; color: var(--text-muted); text-decoration: none;
|
||||||
|
border: 1px solid transparent; font-size: 13px; font-weight: 500;
|
||||||
|
}
|
||||||
|
.sidebar-link:hover { background: var(--surface2); color: var(--text); }
|
||||||
|
.sidebar-link.active {
|
||||||
|
background: rgba(88,166,255,0.12); color: var(--text); border-color: rgba(88,166,255,0.35);
|
||||||
|
}
|
||||||
|
.content-shell { flex: 1; min-width: 0; display: flex; flex-direction: column; }
|
||||||
|
.topbar {
|
||||||
|
background: var(--surface); border-bottom: 1px solid var(--border); padding: 0 24px;
|
||||||
|
display: flex; align-items: center; gap: 12px; min-height: 52px;
|
||||||
|
}
|
||||||
|
.topbar-spacer { flex: 1; }
|
||||||
|
.nav-user { font-size: 13px; color: var(--text-muted); }
|
||||||
|
.btn-sign-out {
|
||||||
|
padding: 5px 12px; border-radius: 6px; border: 1px solid var(--border);
|
||||||
|
background: none; color: var(--text); font-size: 12px; text-decoration: none; cursor: pointer;
|
||||||
|
}
|
||||||
|
.btn-sign-out:hover { background: var(--surface2); }
|
||||||
|
.main { padding: 32px 24px 40px; flex: 1; }
|
||||||
|
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
|
||||||
|
padding: 24px; width: 100%; max-width: 1480px; margin: 0 auto; }
|
||||||
|
.card-title { font-size: 20px; font-weight: 600; margin-bottom: 8px; }
|
||||||
|
.card-subtitle { color: var(--text-muted); font-size: 13px; margin-bottom: 20px; }
|
||||||
|
.filter-bar {
|
||||||
|
display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 16px;
|
||||||
|
margin-bottom: 20px; padding: 16px; background: var(--bg); border: 1px solid var(--border);
|
||||||
|
border-radius: 10px;
|
||||||
|
}
|
||||||
|
.filter-field { display: flex; flex-direction: column; gap: 6px; min-width: 140px; flex: 1; }
|
||||||
|
.filter-field label { font-size: 12px; color: var(--text-muted); font-weight: 500; }
|
||||||
|
.filter-field input {
|
||||||
|
background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
|
||||||
|
color: var(--text); padding: 8px 10px; font-size: 13px; font-family: 'JetBrains Mono', monospace;
|
||||||
|
outline: none; width: 100%;
|
||||||
|
}
|
||||||
|
.filter-field input:focus { border-color: var(--accent); }
|
||||||
|
.filter-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
|
||||||
|
.btn-filter {
|
||||||
|
padding: 8px 16px; border-radius: 6px; border: none; background: var(--accent); color: #0d1117;
|
||||||
|
font-size: 13px; font-weight: 600; cursor: pointer;
|
||||||
|
}
|
||||||
|
.btn-filter:hover { background: var(--accent-hover); }
|
||||||
|
.btn-clear {
|
||||||
|
padding: 8px 14px; border-radius: 6px; border: 1px solid var(--border); background: transparent;
|
||||||
|
color: var(--text-muted); font-size: 13px; text-decoration: none; cursor: pointer;
|
||||||
|
}
|
||||||
|
.btn-clear:hover { background: var(--surface2); color: var(--text); }
|
||||||
|
.empty { color: var(--text-muted); font-size: 14px; padding: 20px 0; }
|
||||||
|
.table-wrap {
|
||||||
|
overflow: auto;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 10px;
|
||||||
|
background: var(--bg);
|
||||||
|
max-height: 72vh;
|
||||||
|
}
|
||||||
|
table {
|
||||||
|
width: max-content;
|
||||||
|
min-width: 1240px;
|
||||||
|
border-collapse: separate;
|
||||||
|
border-spacing: 0;
|
||||||
|
}
|
||||||
|
th, td { text-align: left; vertical-align: top; padding: 12px 10px; border-top: 1px solid var(--border); }
|
||||||
|
th {
|
||||||
|
color: var(--text-muted);
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 600;
|
||||||
|
white-space: nowrap;
|
||||||
|
position: sticky;
|
||||||
|
top: 0;
|
||||||
|
z-index: 2;
|
||||||
|
background: var(--surface);
|
||||||
|
}
|
||||||
|
td { font-size: 13px; line-height: 1.45; }
|
||||||
|
tbody tr:nth-child(2n) td { background: rgba(255, 255, 255, 0.01); }
|
||||||
|
.mono { font-family: 'JetBrains Mono', monospace; }
|
||||||
|
.col-updated { min-width: 168px; }
|
||||||
|
.col-folder { min-width: 128px; }
|
||||||
|
.col-type { min-width: 108px; }
|
||||||
|
.col-name { min-width: 180px; max-width: 260px; }
|
||||||
|
.col-tags { min-width: 160px; max-width: 220px; }
|
||||||
|
.col-actions { min-width: 132px; }
|
||||||
|
.cell-name, .cell-tags-val {
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
word-break: break-word;
|
||||||
|
}
|
||||||
|
.cell-notes, .cell-meta { min-width: 260px; max-width: 360px; }
|
||||||
|
.notes-scroll {
|
||||||
|
max-height: 120px;
|
||||||
|
overflow: auto;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
word-break: break-word;
|
||||||
|
padding: 8px;
|
||||||
|
background: var(--bg);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
.detail {
|
||||||
|
background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
|
||||||
|
padding: 10px; white-space: pre-wrap; word-break: break-word; font-size: 12px;
|
||||||
|
max-width: 360px; max-height: 120px; overflow: auto;
|
||||||
|
}
|
||||||
|
.col-actions { white-space: nowrap; }
|
||||||
|
.row-actions { display: flex; flex-wrap: wrap; gap: 6px; }
|
||||||
|
.col-secrets { min-width: 300px; max-width: 420px; }
|
||||||
|
.secret-list { display: flex; flex-wrap: wrap; gap: 6px; max-width: 400px; }
|
||||||
|
.secret-chip {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 999px;
|
||||||
|
padding: 3px 8px;
|
||||||
|
font-size: 11px;
|
||||||
|
background: var(--surface2);
|
||||||
|
font-family: 'JetBrains Mono', monospace;
|
||||||
|
max-width: 100%;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
.secret-name {
|
||||||
|
min-width: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
.secret-type {
|
||||||
|
color: var(--text-muted);
|
||||||
|
border-left: 1px solid var(--border);
|
||||||
|
padding-left: 6px;
|
||||||
|
}
|
||||||
|
.btn-unlink-secret {
|
||||||
|
border: none;
|
||||||
|
background: transparent;
|
||||||
|
color: #f85149;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 1;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
.btn-row {
|
||||||
|
padding: 4px 10px; border-radius: 6px; font-size: 12px; cursor: pointer;
|
||||||
|
border: 1px solid var(--border); background: var(--surface2); color: var(--text-muted);
|
||||||
|
font-family: inherit;
|
||||||
|
}
|
||||||
|
.btn-row:hover { color: var(--text); border-color: var(--text-muted); }
|
||||||
|
.btn-row.danger:hover { border-color: #f85149; color: #f85149; }
|
||||||
|
.modal-overlay {
|
||||||
|
position: fixed; inset: 0; background: rgba(1, 4, 9, 0.65); z-index: 200;
|
||||||
|
display: flex; align-items: center; justify-content: center; padding: 16px;
|
||||||
|
}
|
||||||
|
.modal-overlay[hidden] { display: none !important; }
|
||||||
|
.modal {
|
||||||
|
background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
|
||||||
|
padding: 22px; width: 100%; max-width: 520px; max-height: 90vh; overflow: auto;
|
||||||
|
box-shadow: 0 16px 48px rgba(0,0,0,0.45);
|
||||||
|
}
|
||||||
|
.modal-title { font-size: 16px; font-weight: 600; margin-bottom: 14px; }
|
||||||
|
.modal-field { margin-bottom: 12px; }
|
||||||
|
.modal-field label { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 5px; }
|
||||||
|
.modal-field input, .modal-field textarea {
|
||||||
|
width: 100%; background: var(--bg); border: 1px solid var(--border); border-radius: 6px;
|
||||||
|
color: var(--text); padding: 8px 10px; font-size: 13px; font-family: 'JetBrains Mono', monospace;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
.modal-field textarea { min-height: 72px; resize: vertical; }
|
||||||
|
.modal-field textarea.metadata-edit { min-height: 140px; }
|
||||||
|
.modal-error { color: #f85149; font-size: 12px; margin-bottom: 10px; display: none; }
|
||||||
|
.modal-error.visible { display: block; }
|
||||||
|
.modal-footer { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; margin-top: 16px; }
|
||||||
|
.btn-modal { padding: 8px 16px; border-radius: 6px; font-size: 13px; cursor: pointer; font-family: inherit; border: 1px solid var(--border); background: transparent; color: var(--text); }
|
||||||
|
.btn-modal.primary { background: var(--accent); color: #0d1117; border-color: transparent; font-weight: 600; }
|
||||||
|
.btn-modal.primary:hover { background: var(--accent-hover); }
|
||||||
|
.btn-modal.danger { border-color: #f85149; color: #f85149; }
|
||||||
|
@media (max-width: 900px) {
|
||||||
|
.layout { flex-direction: column; }
|
||||||
|
.sidebar {
|
||||||
|
width: 100%; border-right: none; border-bottom: 1px solid var(--border);
|
||||||
|
padding: 16px; gap: 14px;
|
||||||
|
}
|
||||||
|
.sidebar-menu { flex-direction: row; flex-wrap: wrap; }
|
||||||
|
.sidebar-link { flex: 1; text-align: center; min-width: 72px; }
|
||||||
|
.main { padding: 20px 12px 28px; }
|
||||||
|
.card { padding: 16px; }
|
||||||
|
.topbar { padding: 12px 16px; flex-wrap: wrap; }
|
||||||
|
.table-wrap { max-height: none; border: none; background: transparent; }
|
||||||
|
table, thead, tbody, th, td, tr { display: block; min-width: 0; width: 100%; }
|
||||||
|
thead { display: none; }
|
||||||
|
tr { border-top: 1px solid var(--border); padding: 12px 0; }
|
||||||
|
td { border-top: none; padding: 6px 0; max-width: none; }
|
||||||
|
td::before {
|
||||||
|
display: block; color: var(--text-muted); font-size: 11px;
|
||||||
|
margin-bottom: 4px; text-transform: uppercase;
|
||||||
|
}
|
||||||
|
td.col-updated::before { content: "更新"; }
|
||||||
|
td.col-folder::before { content: "Folder"; }
|
||||||
|
td.col-type::before { content: "Type"; }
|
||||||
|
td.col-name::before { content: "Name"; }
|
||||||
|
td.col-notes::before { content: "Notes"; }
|
||||||
|
td.col-tags::before { content: "Tags"; }
|
||||||
|
td.col-meta::before { content: "Metadata"; }
|
||||||
|
td.col-secrets::before { content: "Secrets"; }
|
||||||
|
td.col-actions::before { content: "操作"; }
|
||||||
|
.detail, .notes-scroll, .secret-list { max-width: none; }
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="layout">
|
||||||
|
<aside class="sidebar">
|
||||||
|
<a href="/dashboard" class="sidebar-logo"><span>secrets</span></a>
|
||||||
|
<nav class="sidebar-menu">
|
||||||
|
<a href="/dashboard" class="sidebar-link">MCP</a>
|
||||||
|
<a href="/entries" class="sidebar-link active">条目</a>
|
||||||
|
<a href="/audit" class="sidebar-link">审计</a>
|
||||||
|
</nav>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<div class="content-shell">
|
||||||
|
<div class="topbar">
|
||||||
|
<span class="topbar-spacer"></span>
|
||||||
|
<span class="nav-user">{{ user_name }}{% if !user_email.is_empty() %} · {{ user_email }}{% endif %}</span>
|
||||||
|
<form action="/auth/logout" method="post" style="display:inline">
|
||||||
|
<button type="submit" class="btn-sign-out">退出</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<main class="main">
|
||||||
|
<section class="card">
|
||||||
|
<div class="card-title">我的条目</div>
|
||||||
|
<div class="card-subtitle">在当前筛选条件下,共 <strong>{{ total_count }}</strong> 条记录;本页显示 <strong>{{ shown_count }}</strong> 条(按更新时间降序,单页最多 {{ limit }} 条)。不含密文字段。时间为浏览器本地时区。提示:非敏感地址类字段(如 address / endpoint / url)建议放在 Metadata(例如 <code>metadata.address</code>),仅密码/令牌等放 Secrets。</div>
|
||||||
|
|
||||||
|
<form class="filter-bar" method="get" action="/entries">
|
||||||
|
<div class="filter-field">
|
||||||
|
<label for="filter-folder">Folder(精确匹配)</label>
|
||||||
|
<input id="filter-folder" name="folder" type="text" value="{{ filter_folder }}" placeholder="例如 refining" autocomplete="off">
|
||||||
|
</div>
|
||||||
|
<div class="filter-field">
|
||||||
|
<label for="filter-type">Type(精确匹配)</label>
|
||||||
|
<input id="filter-type" name="type" type="text" value="{{ filter_type }}" placeholder="例如 server" autocomplete="off">
|
||||||
|
</div>
|
||||||
|
<div class="filter-actions">
|
||||||
|
<button type="submit" class="btn-filter">筛选</button>
|
||||||
|
<a href="/entries" class="btn-clear">清空</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
{% if entries.is_empty() %}
|
||||||
|
<div class="empty">暂无条目。</div>
|
||||||
|
{% else %}
|
||||||
|
<div class="table-wrap">
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>更新</th>
|
||||||
|
<th>Folder</th>
|
||||||
|
<th>Type</th>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Notes</th>
|
||||||
|
<th>Tags</th>
|
||||||
|
<th>Metadata</th>
|
||||||
|
<th>Secrets</th>
|
||||||
|
<th>操作</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for entry in entries %}
|
||||||
|
<tr data-entry-id="{{ entry.id }}">
|
||||||
|
<td class="col-updated mono"><time class="entry-local-time" datetime="{{ entry.updated_at_iso }}">{{ entry.updated_at_iso }}</time></td>
|
||||||
|
<td class="col-folder mono cell-folder">{{ entry.folder }}</td>
|
||||||
|
<td class="col-type mono cell-type">{{ entry.entry_type }}</td>
|
||||||
|
<td class="col-name mono cell-name">{{ entry.name }}</td>
|
||||||
|
<td class="col-notes cell-notes">{% if !entry.notes.is_empty() %}<div class="notes-scroll cell-notes-val">{{ entry.notes }}</div>{% endif %}</td>
|
||||||
|
<td class="col-tags mono cell-tags-val">{{ entry.tags }}</td>
|
||||||
|
<td class="col-meta cell-meta"><pre class="detail cell-meta-val">{{ entry.metadata }}</pre></td>
|
||||||
|
<td class="col-secrets">
|
||||||
|
<div class="secret-list">
|
||||||
|
{% for s in entry.secrets %}
|
||||||
|
<span class="secret-chip">
|
||||||
|
<span class="secret-name" title="{{ s.name }}">{{ s.name }}</span>
|
||||||
|
<span class="secret-type">{{ s.secret_type }}</span>
|
||||||
|
<button type="button" class="btn-unlink-secret" data-secret-id="{{ s.id }}" data-secret-name="{{ s.name }}" title="解除关联">x</button>
|
||||||
|
</span>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td class="col-actions">
|
||||||
|
<div class="row-actions">
|
||||||
|
<button type="button" class="btn-row btn-edit">编辑</button>
|
||||||
|
<button type="button" class="btn-row danger btn-del">删除</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
</section>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="edit-overlay" class="modal-overlay" hidden>
|
||||||
|
<div class="modal" role="dialog" aria-modal="true" aria-labelledby="edit-title">
|
||||||
|
<div class="modal-title" id="edit-title">编辑条目</div>
|
||||||
|
<div id="edit-error" class="modal-error"></div>
|
||||||
|
<div class="modal-field"><label for="edit-folder">Folder</label><input id="edit-folder" type="text" autocomplete="off"></div>
|
||||||
|
<div class="modal-field"><label for="edit-type">Type</label><input id="edit-type" type="text" autocomplete="off"></div>
|
||||||
|
<div class="modal-field"><label for="edit-name">Name</label><input id="edit-name" type="text" autocomplete="off"></div>
|
||||||
|
<div class="modal-field"><label for="edit-notes">Notes</label><textarea id="edit-notes"></textarea></div>
|
||||||
|
<div class="modal-field"><label for="edit-tags">Tags(逗号分隔)</label><input id="edit-tags" type="text" autocomplete="off"></div>
|
||||||
|
<div class="modal-field"><label for="edit-metadata">Metadata(JSON 对象)</label><textarea id="edit-metadata" class="metadata-edit"></textarea></div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn-modal" id="edit-cancel">取消</button>
|
||||||
|
<button type="button" class="btn-modal primary" id="edit-save">保存</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
(function () {
|
||||||
|
document.querySelectorAll('time.entry-local-time[datetime]').forEach(function (el) {
|
||||||
|
var raw = el.getAttribute('datetime');
|
||||||
|
var d = raw ? new Date(raw) : null;
|
||||||
|
if (d && !isNaN(d.getTime())) {
|
||||||
|
el.textContent = d.toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'medium' });
|
||||||
|
el.title = raw + ' (UTC)';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var editOverlay = document.getElementById('edit-overlay');
|
||||||
|
var editError = document.getElementById('edit-error');
|
||||||
|
var editFolder = document.getElementById('edit-folder');
|
||||||
|
var editType = document.getElementById('edit-type');
|
||||||
|
var editName = document.getElementById('edit-name');
|
||||||
|
var editNotes = document.getElementById('edit-notes');
|
||||||
|
var editTags = document.getElementById('edit-tags');
|
||||||
|
var editMetadata = document.getElementById('edit-metadata');
|
||||||
|
var currentEntryId = null;
|
||||||
|
|
||||||
|
function showEditErr(msg) {
|
||||||
|
editError.textContent = msg || '';
|
||||||
|
editError.classList.toggle('visible', !!msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
function openEdit(tr) {
|
||||||
|
var id = tr.getAttribute('data-entry-id');
|
||||||
|
if (!id) return;
|
||||||
|
currentEntryId = id;
|
||||||
|
showEditErr('');
|
||||||
|
editFolder.value = tr.querySelector('.cell-folder') ? tr.querySelector('.cell-folder').textContent.trim() : '';
|
||||||
|
editType.value = tr.querySelector('.cell-type') ? tr.querySelector('.cell-type').textContent.trim() : '';
|
||||||
|
editName.value = tr.querySelector('.cell-name') ? tr.querySelector('.cell-name').textContent.trim() : '';
|
||||||
|
editNotes.value = tr.querySelector('.cell-notes-val') ? tr.querySelector('.cell-notes-val').textContent : '';
|
||||||
|
var tagsText = tr.querySelector('.cell-tags-val') ? tr.querySelector('.cell-tags-val').textContent.trim() : '';
|
||||||
|
editTags.value = tagsText;
|
||||||
|
var metaPre = tr.querySelector('.cell-meta-val');
|
||||||
|
editMetadata.value = metaPre ? metaPre.textContent : '{}';
|
||||||
|
editOverlay.hidden = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeEdit() {
|
||||||
|
editOverlay.hidden = true;
|
||||||
|
currentEntryId = null;
|
||||||
|
showEditErr('');
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('edit-cancel').addEventListener('click', closeEdit);
|
||||||
|
editOverlay.addEventListener('click', function (e) {
|
||||||
|
if (e.target === editOverlay) closeEdit();
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('edit-save').addEventListener('click', function () {
|
||||||
|
if (!currentEntryId) return;
|
||||||
|
var meta;
|
||||||
|
try {
|
||||||
|
meta = JSON.parse(editMetadata.value);
|
||||||
|
} catch (err) {
|
||||||
|
showEditErr('Metadata 不是合法 JSON');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (meta === null || typeof meta !== 'object' || Array.isArray(meta)) {
|
||||||
|
showEditErr('Metadata 必须是 JSON 对象');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var tags = editTags.value.split(',').map(function (s) { return s.trim(); }).filter(Boolean);
|
||||||
|
var body = {
|
||||||
|
folder: editFolder.value,
|
||||||
|
type: editType.value,
|
||||||
|
name: editName.value.trim(),
|
||||||
|
notes: editNotes.value,
|
||||||
|
tags: tags,
|
||||||
|
metadata: meta
|
||||||
|
};
|
||||||
|
showEditErr('');
|
||||||
|
fetch('/api/entries/' + encodeURIComponent(currentEntryId), {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
credentials: 'same-origin',
|
||||||
|
body: JSON.stringify(body)
|
||||||
|
}).then(function (r) {
|
||||||
|
return r.json().then(function (data) {
|
||||||
|
if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status));
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
}).then(function () {
|
||||||
|
closeEdit();
|
||||||
|
window.location.reload();
|
||||||
|
}).catch(function (e) {
|
||||||
|
showEditErr(e.message || String(e));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
document.querySelectorAll('tr[data-entry-id]').forEach(function (tr) {
|
||||||
|
tr.querySelector('.btn-edit').addEventListener('click', function () { openEdit(tr); });
|
||||||
|
tr.querySelector('.btn-del').addEventListener('click', function () {
|
||||||
|
var id = tr.getAttribute('data-entry-id');
|
||||||
|
var nameEl = tr.querySelector('.cell-name');
|
||||||
|
var name = nameEl ? nameEl.textContent.trim() : '';
|
||||||
|
if (!id) return;
|
||||||
|
if (!confirm('确定删除条目「' + name + '」?')) return;
|
||||||
|
fetch('/api/entries/' + encodeURIComponent(id), { method: 'DELETE', credentials: 'same-origin' })
|
||||||
|
.then(function (r) {
|
||||||
|
return r.json().then(function (data) {
|
||||||
|
if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status));
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.then(function (data) {
|
||||||
|
if (data && Array.isArray(data.migrated) && data.migrated.length > 0) {
|
||||||
|
alert('已自动迁移共享 key 引用:' + data.migrated.length + ' 个条目完成重定向。');
|
||||||
|
}
|
||||||
|
window.location.reload();
|
||||||
|
})
|
||||||
|
.catch(function (e) { alert(e.message || String(e)); });
|
||||||
|
});
|
||||||
|
|
||||||
|
tr.querySelectorAll('.btn-unlink-secret').forEach(function (btn) {
|
||||||
|
btn.addEventListener('click', function () {
|
||||||
|
var entryId = tr.getAttribute('data-entry-id');
|
||||||
|
var secretId = btn.getAttribute('data-secret-id');
|
||||||
|
var secretName = btn.getAttribute('data-secret-name') || '';
|
||||||
|
if (!entryId || !secretId) return;
|
||||||
|
if (!confirm('确定解除 secret 关联「' + secretName + '」?')) return;
|
||||||
|
fetch('/api/entries/' + encodeURIComponent(entryId) + '/secrets/' + encodeURIComponent(secretId), {
|
||||||
|
method: 'DELETE',
|
||||||
|
credentials: 'same-origin'
|
||||||
|
}).then(function (r) {
|
||||||
|
return r.json().then(function (data) {
|
||||||
|
if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status));
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
}).then(function () {
|
||||||
|
window.location.reload();
|
||||||
|
}).catch(function (e) {
|
||||||
|
alert(e.message || String(e));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
269
crates/secrets-mcp/templates/home.html
Normal file
269
crates/secrets-mcp/templates/home.html
Normal file
@@ -0,0 +1,269 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<meta name="description" content="Secrets MCP:基于 Model Context Protocol 的密钥与配置管理。密码短语在浏览器本地 PBKDF2 派生,密文 AES-GCM 存储,完整审计与历史版本。">
|
||||||
|
<meta name="keywords" content="secrets management,MCP,Model Context Protocol,end-to-end encryption,AES-GCM,PBKDF2,API key,密钥管理">
|
||||||
|
<meta name="robots" content="index, follow">
|
||||||
|
<link rel="canonical" href="{{ base_url }}/">
|
||||||
|
<link rel="icon" href="/favicon.svg?v={{ version }}" type="image/svg+xml">
|
||||||
|
<title>Secrets MCP — 端到端加密的密钥管理</title>
|
||||||
|
<meta property="og:type" content="website">
|
||||||
|
<meta property="og:url" content="{{ base_url }}/">
|
||||||
|
<meta property="og:title" content="Secrets MCP — 端到端加密的密钥管理">
|
||||||
|
<meta property="og:description" content="密码短语客户端派生,密文存储;MCP API 与 Web 控制台,多租户与审计。">
|
||||||
|
<meta name="twitter:card" content="summary">
|
||||||
|
<meta name="twitter:title" content="Secrets MCP — 端到端加密的密钥管理">
|
||||||
|
<meta name="twitter:description" content="密码短语客户端派生,密文存储;MCP API 与 Web 控制台,多租户与审计。">
|
||||||
|
<style>
|
||||||
|
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
|
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@500;600&family=Inter:wght@400;500;600&display=swap');
|
||||||
|
:root {
|
||||||
|
--bg: #0d1117;
|
||||||
|
--surface: #161b22;
|
||||||
|
--surface2: #21262d;
|
||||||
|
--border: #30363d;
|
||||||
|
--text: #e6edf3;
|
||||||
|
--text-muted: #8b949e;
|
||||||
|
--accent: #58a6ff;
|
||||||
|
--accent-hover: #79b8ff;
|
||||||
|
}
|
||||||
|
html, body { height: 100%; overflow: hidden; }
|
||||||
|
@supports (height: 100dvh) {
|
||||||
|
html, body { height: 100dvh; }
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
background: var(--bg);
|
||||||
|
color: var(--text);
|
||||||
|
font-family: 'Inter', sans-serif;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
.nav {
|
||||||
|
flex-shrink: 0;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
padding: 14px 24px;
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
background: var(--surface);
|
||||||
|
}
|
||||||
|
.brand {
|
||||||
|
font-family: 'JetBrains Mono', monospace;
|
||||||
|
font-size: 15px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text);
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
.brand span { color: var(--accent); }
|
||||||
|
.nav-right { display: flex; align-items: center; gap: 14px; }
|
||||||
|
.lang-bar { display: flex; gap: 2px; background: rgba(255,255,255,0.04); border-radius: 6px; padding: 2px; }
|
||||||
|
.lang-btn {
|
||||||
|
padding: 4px 10px; border: none; background: none; color: var(--text-muted);
|
||||||
|
font-size: 12px; cursor: pointer; border-radius: 4px;
|
||||||
|
}
|
||||||
|
.lang-btn.active { background: var(--border); color: var(--text); }
|
||||||
|
.cta {
|
||||||
|
display: inline-flex; align-items: center; justify-content: center;
|
||||||
|
padding: 8px 18px; border-radius: 8px; font-size: 13px; font-weight: 600;
|
||||||
|
text-decoration: none; border: 1px solid var(--accent);
|
||||||
|
background: rgba(88, 166, 255, 0.12); color: var(--accent);
|
||||||
|
transition: background 0.15s, color 0.15s;
|
||||||
|
}
|
||||||
|
.cta:hover { background: var(--accent); color: var(--bg); }
|
||||||
|
.main {
|
||||||
|
flex: 1;
|
||||||
|
min-height: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 16px 24px 12px;
|
||||||
|
gap: 20px;
|
||||||
|
}
|
||||||
|
.hero { text-align: center; max-width: 720px; }
|
||||||
|
.hero h1 { font-size: clamp(20px, 4vw, 28px); font-weight: 600; margin-bottom: 8px; line-height: 1.25; }
|
||||||
|
.hero .tagline { color: var(--text-muted); font-size: clamp(13px, 2vw, 15px); line-height: 1.5; }
|
||||||
|
.grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(3, 1fr);
|
||||||
|
gap: 12px;
|
||||||
|
width: 100%;
|
||||||
|
max-width: 900px;
|
||||||
|
}
|
||||||
|
@media (max-width: 900px) {
|
||||||
|
.grid { grid-template-columns: repeat(2, 1fr); }
|
||||||
|
}
|
||||||
|
@media (max-width: 480px) {
|
||||||
|
.grid { grid-template-columns: 1fr; gap: 8px; }
|
||||||
|
.main { justify-content: flex-start; padding-top: 12px; }
|
||||||
|
}
|
||||||
|
.card {
|
||||||
|
background: var(--surface);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 10px;
|
||||||
|
padding: 14px 14px 12px;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
.card-icon {
|
||||||
|
width: 32px; height: 32px; border-radius: 8px;
|
||||||
|
background: var(--surface2);
|
||||||
|
display: flex; align-items: center; justify-content: center;
|
||||||
|
margin-bottom: 10px; color: var(--accent);
|
||||||
|
}
|
||||||
|
.card-icon svg { width: 18px; height: 18px; }
|
||||||
|
.card h2 { font-size: 13px; font-weight: 600; margin-bottom: 6px; line-height: 1.3; }
|
||||||
|
.card p { font-size: 12px; color: var(--text-muted); line-height: 1.45; }
|
||||||
|
.foot {
|
||||||
|
flex-shrink: 0;
|
||||||
|
text-align: center;
|
||||||
|
padding: 8px 16px 12px;
|
||||||
|
font-size: 11px;
|
||||||
|
color: var(--text-muted);
|
||||||
|
border-top: 1px solid var(--border);
|
||||||
|
background: var(--surface);
|
||||||
|
}
|
||||||
|
.foot a { color: var(--accent); text-decoration: none; }
|
||||||
|
.foot a:hover { text-decoration: underline; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header class="nav">
|
||||||
|
<a class="brand" href="/">secrets<span>-mcp</span></a>
|
||||||
|
<div class="nav-right">
|
||||||
|
<div class="lang-bar">
|
||||||
|
<button type="button" class="lang-btn" onclick="setLang('zh-CN')">简</button>
|
||||||
|
<button type="button" class="lang-btn" onclick="setLang('zh-TW')">繁</button>
|
||||||
|
<button type="button" class="lang-btn" onclick="setLang('en')">EN</button>
|
||||||
|
</div>
|
||||||
|
{% if is_logged_in %}
|
||||||
|
<a class="cta" href="/dashboard" data-i18n="ctaDashboard">进入控制台</a>
|
||||||
|
{% else %}
|
||||||
|
<a class="cta" href="/login" data-i18n="ctaLogin">登录</a>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
<main class="main">
|
||||||
|
<div class="hero">
|
||||||
|
<h1 data-i18n="heroTitle">端到端加密的密钥与配置管理</h1>
|
||||||
|
<p class="tagline" data-i18n="heroTagline">Streamable HTTP MCP 与 Web 控制台:元数据与密文分库存储,密钥永不离开你的客户端逻辑。</p>
|
||||||
|
</div>
|
||||||
|
<div class="grid">
|
||||||
|
<article class="card">
|
||||||
|
<div class="card-icon" aria-hidden="true">
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 11c1.66 0 3-1.34 3-3V5c0-1.66-1.34-3-3-3S9 3.34 9 5v3c0 1.66 1.34 3 3 3z"/><path d="M19 10v1a7 7 0 01-14 0v-1"/><path d="M12 14v7M9 18h6"/></svg>
|
||||||
|
</div>
|
||||||
|
<h2 data-i18n="c1t">客户端密钥派生</h2>
|
||||||
|
<p data-i18n="c1d">PBKDF2-SHA256(约 60 万次)在浏览器本地从密码短语派生密钥;服务端仅保存盐与校验值,不持有密码或明文主密钥。</p>
|
||||||
|
</article>
|
||||||
|
<article class="card">
|
||||||
|
<div class="card-icon" aria-hidden="true">
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0110 0v4"/></svg>
|
||||||
|
</div>
|
||||||
|
<h2 data-i18n="c2t">AES-256-GCM 加密</h2>
|
||||||
|
<p data-i18n="c2d">敏感字段以 AES-GCM 密文落库;Web 端在本地加解密,明文默认不经过服务端持久化。</p>
|
||||||
|
</article>
|
||||||
|
<article class="card">
|
||||||
|
<div class="card-icon" aria-hidden="true">
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><path d="M14 2v6h6M16 13H8M16 17H8M10 9H8"/></svg>
|
||||||
|
</div>
|
||||||
|
<h2 data-i18n="c3t">审计与历史</h2>
|
||||||
|
<p data-i18n="c3d">操作写入审计日志;条目与密文保留历史版本,支持按版本查看与恢复。</p>
|
||||||
|
</article>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
<footer class="foot">
|
||||||
|
<span data-i18n="versionLabel">版本</span> {{ version }} ·
|
||||||
|
<a href="/llms.txt">llms.txt</a>
|
||||||
|
<span data-i18n="sep"> · </span>
|
||||||
|
<a href="https://gitea.refining.dev/refining/secrets" target="_blank" rel="noopener noreferrer" data-i18n="footRepo">源码仓库</a>
|
||||||
|
{% if !is_logged_in %}
|
||||||
|
<span data-i18n="sep"> · </span>
|
||||||
|
<a href="/login" data-i18n="footLogin">登录</a>
|
||||||
|
{% endif %}
|
||||||
|
</footer>
|
||||||
|
<script>
|
||||||
|
const T = {
|
||||||
|
'zh-CN': {
|
||||||
|
docTitle: 'Secrets MCP — 端到端加密的密钥管理',
|
||||||
|
ctaDashboard: '进入控制台',
|
||||||
|
ctaLogin: '登录',
|
||||||
|
heroTitle: '端到端加密的密钥与配置管理',
|
||||||
|
heroTagline: 'Streamable HTTP MCP 与 Web 控制台:元数据与密文分库存储,密钥永不离开你的客户端逻辑。',
|
||||||
|
c1t: '客户端密钥派生',
|
||||||
|
c1d: 'PBKDF2-SHA256(约 60 万次)在浏览器本地从密码短语派生密钥;服务端仅保存盐与校验值,不持有密码或明文主密钥。',
|
||||||
|
c2t: 'AES-256-GCM 加密',
|
||||||
|
c2d: '敏感字段以 AES-GCM 密文落库;Web 端在本地加解密,明文默认不经过服务端持久化。',
|
||||||
|
c3t: '审计与历史',
|
||||||
|
c3d: '操作写入审计日志;条目与密文保留历史版本,支持按版本查看与恢复。',
|
||||||
|
versionLabel: '版本',
|
||||||
|
sep: ' · ',
|
||||||
|
footRepo: '源码仓库',
|
||||||
|
footLogin: '登录',
|
||||||
|
},
|
||||||
|
'zh-TW': {
|
||||||
|
docTitle: 'Secrets MCP — 端到端加密的金鑰管理',
|
||||||
|
ctaDashboard: '進入控制台',
|
||||||
|
ctaLogin: '登入',
|
||||||
|
heroTitle: '端到端加密的金鑰與設定管理',
|
||||||
|
heroTagline: 'Streamable HTTP MCP 與 Web 控制台:中繼資料與密文分庫儲存,金鑰不離開你的用戶端邏輯。',
|
||||||
|
c1t: '用戶端金鑰派生',
|
||||||
|
c1d: 'PBKDF2-SHA256(約 60 萬次)在瀏覽器本地從密碼片語派生金鑰;伺服端僅保存鹽與校驗值,不持有密碼或明文主金鑰。',
|
||||||
|
c2t: 'AES-256-GCM 加密',
|
||||||
|
c2d: '敏感欄位以 AES-GCM 密文落庫;Web 端在本地加解密,明文預設不經伺服端持久化。',
|
||||||
|
c3t: '稽核與歷史',
|
||||||
|
c3d: '操作寫入稽核日誌;條目與密文保留歷史版本,支援依版本檢視與還原。',
|
||||||
|
versionLabel: '版本',
|
||||||
|
sep: ' · ',
|
||||||
|
footRepo: '原始碼倉庫',
|
||||||
|
footLogin: '登入',
|
||||||
|
},
|
||||||
|
'en': {
|
||||||
|
docTitle: 'Secrets MCP — End-to-end encrypted secrets',
|
||||||
|
ctaDashboard: 'Open dashboard',
|
||||||
|
ctaLogin: 'Sign in',
|
||||||
|
heroTitle: 'End-to-end encrypted secrets and configuration',
|
||||||
|
heroTagline: 'Streamable HTTP MCP plus web console: metadata and ciphertext stored separately; keys stay on your client.',
|
||||||
|
c1t: 'Client-side key derivation',
|
||||||
|
c1d: 'PBKDF2-SHA256 (~600k iterations) derives keys from your passphrase in the browser; the server stores only salt and a verification blob, never your password or raw master key.',
|
||||||
|
c2t: 'AES-256-GCM',
|
||||||
|
c2d: 'Secret fields are stored as AES-GCM ciphertext; the web UI encrypts and decrypts locally so plaintext is not persisted server-side by default.',
|
||||||
|
c3t: 'Audit and history',
|
||||||
|
c3d: 'Operations are audited; entries and secrets keep version history for review and rollback.',
|
||||||
|
versionLabel: 'Version',
|
||||||
|
sep: ' · ',
|
||||||
|
footRepo: 'Source repository',
|
||||||
|
footLogin: 'Sign in',
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let currentLang = localStorage.getItem('lang') || 'zh-CN';
|
||||||
|
|
||||||
|
function t(key) {
|
||||||
|
return (T[currentLang] && T[currentLang][key]) || T['en'][key] || key;
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyLang() {
|
||||||
|
document.documentElement.lang = currentLang;
|
||||||
|
document.title = t('docTitle');
|
||||||
|
document.querySelectorAll('[data-i18n]').forEach(el => {
|
||||||
|
const key = el.getAttribute('data-i18n');
|
||||||
|
el.textContent = t(key);
|
||||||
|
});
|
||||||
|
document.querySelectorAll('.lang-btn').forEach(btn => {
|
||||||
|
const map = { 'zh-CN': '简', 'zh-TW': '繁', 'en': 'EN' };
|
||||||
|
btn.classList.toggle('active', btn.textContent === map[currentLang]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function setLang(lang) {
|
||||||
|
currentLang = lang;
|
||||||
|
localStorage.setItem('lang', lang);
|
||||||
|
applyLang();
|
||||||
|
}
|
||||||
|
|
||||||
|
applyLang();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -3,8 +3,19 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<meta name="robots" content="noindex, follow">
|
||||||
|
<meta name="description" content="登录 Secrets MCP Web 控制台,安全管理跨设备加密 secrets。">
|
||||||
|
<meta name="keywords" content="Secrets MCP,登录,OAuth,密钥管理">
|
||||||
|
<link rel="canonical" href="{{ base_url }}/login">
|
||||||
<link rel="icon" href="/favicon.svg?v={{ version }}" type="image/svg+xml">
|
<link rel="icon" href="/favicon.svg?v={{ version }}" type="image/svg+xml">
|
||||||
<title>Secrets — Sign In</title>
|
<title>登录 — Secrets MCP</title>
|
||||||
|
<meta property="og:type" content="website">
|
||||||
|
<meta property="og:url" content="{{ base_url }}/login">
|
||||||
|
<meta property="og:title" content="登录 — Secrets MCP">
|
||||||
|
<meta property="og:description" content="登录 Web 控制台,管理加密存储的密钥与配置。">
|
||||||
|
<meta name="twitter:card" content="summary">
|
||||||
|
<meta name="twitter:title" content="登录 — Secrets MCP">
|
||||||
|
<meta name="twitter:description" content="登录 Web 控制台,管理加密存储的密钥与配置。">
|
||||||
<style>
|
<style>
|
||||||
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');
|
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');
|
||||||
@@ -17,6 +28,7 @@
|
|||||||
--accent: #58a6ff;
|
--accent: #58a6ff;
|
||||||
--accent-hover: #79b8ff;
|
--accent-hover: #79b8ff;
|
||||||
--google: #4285f4;
|
--google: #4285f4;
|
||||||
|
--danger: #f85149;
|
||||||
}
|
}
|
||||||
body { background: var(--bg); color: var(--text); font-family: 'Inter', sans-serif;
|
body { background: var(--bg); color: var(--text); font-family: 'Inter', sans-serif;
|
||||||
min-height: 100vh; display: flex; align-items: center; justify-content: center; }
|
min-height: 100vh; display: flex; align-items: center; justify-content: center; }
|
||||||
@@ -25,11 +37,24 @@
|
|||||||
padding: 48px 40px; width: 100%; max-width: 400px;
|
padding: 48px 40px; width: 100%; max-width: 400px;
|
||||||
box-shadow: 0 8px 32px rgba(0,0,0,0.4);
|
box-shadow: 0 8px 32px rgba(0,0,0,0.4);
|
||||||
}
|
}
|
||||||
.topbar { display: flex; justify-content: flex-end; margin-bottom: 20px; }
|
.topbar { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 20px; gap: 12px; }
|
||||||
.lang-bar { display: flex; gap: 2px; background: rgba(255,255,255,0.04); border-radius: 6px; padding: 2px; }
|
.back-home {
|
||||||
|
font-size: 13px; color: var(--accent); text-decoration: none; white-space: nowrap;
|
||||||
|
}
|
||||||
|
.back-home:hover { text-decoration: underline; }
|
||||||
|
.lang-bar { display: flex; gap: 2px; background: rgba(255,255,255,0.04); border-radius: 6px; padding: 2px; flex-shrink: 0; }
|
||||||
.lang-btn { padding: 3px 9px; border: none; background: none; color: var(--text-muted);
|
.lang-btn { padding: 3px 9px; border: none; background: none; color: var(--text-muted);
|
||||||
font-size: 12px; cursor: pointer; border-radius: 4px; }
|
font-size: 12px; cursor: pointer; border-radius: 4px; }
|
||||||
.lang-btn.active { background: var(--border); color: var(--text); }
|
.lang-btn.active { background: var(--border); color: var(--text); }
|
||||||
|
.oauth-alert {
|
||||||
|
display: none;
|
||||||
|
margin-bottom: 16px; padding: 10px 12px; border-radius: 8px;
|
||||||
|
font-size: 13px; line-height: 1.4;
|
||||||
|
background: rgba(248, 81, 73, 0.12);
|
||||||
|
border: 1px solid rgba(248, 81, 73, 0.35);
|
||||||
|
color: #ffa198;
|
||||||
|
}
|
||||||
|
.oauth-alert.visible { display: block; }
|
||||||
h1 { font-size: 22px; font-weight: 600; margin-bottom: 8px; }
|
h1 { font-size: 22px; font-weight: 600; margin-bottom: 8px; }
|
||||||
.subtitle { color: var(--text-muted); font-size: 14px; margin-bottom: 32px; }
|
.subtitle { color: var(--text-muted); font-size: 14px; margin-bottom: 32px; }
|
||||||
.btn {
|
.btn {
|
||||||
@@ -48,12 +73,14 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="topbar">
|
<div class="topbar">
|
||||||
|
<a class="back-home" href="/" data-i18n="backHome">返回首页</a>
|
||||||
<div class="lang-bar">
|
<div class="lang-bar">
|
||||||
<button class="lang-btn" onclick="setLang('zh-CN')">简</button>
|
<button type="button" class="lang-btn" onclick="setLang('zh-CN')">简</button>
|
||||||
<button class="lang-btn" onclick="setLang('zh-TW')">繁</button>
|
<button type="button" class="lang-btn" onclick="setLang('zh-TW')">繁</button>
|
||||||
<button class="lang-btn" onclick="setLang('en')">EN</button>
|
<button type="button" class="lang-btn" onclick="setLang('en')">EN</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div id="oauth-alert" class="oauth-alert" role="alert"></div>
|
||||||
<h1 data-i18n="title">登录</h1>
|
<h1 data-i18n="title">登录</h1>
|
||||||
<p class="subtitle" data-i18n="subtitle">安全管理你的跨设备 secrets。</p>
|
<p class="subtitle" data-i18n="subtitle">安全管理你的跨设备 secrets。</p>
|
||||||
|
|
||||||
@@ -78,22 +105,40 @@
|
|||||||
<script>
|
<script>
|
||||||
const T = {
|
const T = {
|
||||||
'zh-CN': {
|
'zh-CN': {
|
||||||
|
docTitle: '登录 — Secrets MCP',
|
||||||
|
backHome: '返回首页',
|
||||||
title: '登录',
|
title: '登录',
|
||||||
subtitle: '安全管理你的跨设备 secrets。',
|
subtitle: '安全管理你的跨设备 secrets。',
|
||||||
google: '使用 Google 登录',
|
google: '使用 Google 登录',
|
||||||
noProviders: '未配置登录方式,请联系管理员。',
|
noProviders: '未配置登录方式,请联系管理员。',
|
||||||
|
err_oauth_error: '登录失败:授权提供方返回错误,请重试。',
|
||||||
|
err_oauth_missing_code: '登录失败:未收到授权码,请重试。',
|
||||||
|
err_oauth_missing_state: '登录失败:缺少安全校验参数,请重试。',
|
||||||
|
err_oauth_state: '登录失败:会话校验不匹配(可能因 Cookie 策略或服务器重启)。请返回首页再试。',
|
||||||
},
|
},
|
||||||
'zh-TW': {
|
'zh-TW': {
|
||||||
|
docTitle: '登入 — Secrets MCP',
|
||||||
|
backHome: '返回首頁',
|
||||||
title: '登入',
|
title: '登入',
|
||||||
subtitle: '安全管理你的跨裝置 secrets。',
|
subtitle: '安全管理你的跨裝置 secrets。',
|
||||||
google: '使用 Google 登入',
|
google: '使用 Google 登入',
|
||||||
noProviders: '尚未設定登入方式,請聯絡管理員。',
|
noProviders: '尚未設定登入方式,請聯絡管理員。',
|
||||||
|
err_oauth_error: '登入失敗:授權方回傳錯誤,請再試一次。',
|
||||||
|
err_oauth_missing_code: '登入失敗:未取得授權碼,請再試一次。',
|
||||||
|
err_oauth_missing_state: '登入失敗:缺少安全校驗參數,請再試一次。',
|
||||||
|
err_oauth_state: '登入失敗:工作階段校驗不符(可能與 Cookie 政策或伺服器重啟有關)。請回到首頁再試。',
|
||||||
},
|
},
|
||||||
'en': {
|
'en': {
|
||||||
|
docTitle: 'Sign in — Secrets MCP',
|
||||||
|
backHome: 'Back to home',
|
||||||
title: 'Sign in',
|
title: 'Sign in',
|
||||||
subtitle: 'Manage your cross-device secrets securely.',
|
subtitle: 'Manage your cross-device secrets securely.',
|
||||||
google: 'Continue with Google',
|
google: 'Continue with Google',
|
||||||
noProviders: 'No login providers configured. Please contact your administrator.',
|
noProviders: 'No login providers configured. Please contact your administrator.',
|
||||||
|
err_oauth_error: 'Sign-in failed: the identity provider returned an error. Please try again.',
|
||||||
|
err_oauth_missing_code: 'Sign-in failed: no authorization code was returned. Please try again.',
|
||||||
|
err_oauth_missing_state: 'Sign-in failed: missing security state. Please try again.',
|
||||||
|
err_oauth_state: 'Sign-in failed: session state mismatch (often cookies or server restart). Open the home page and try again.',
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -101,8 +146,23 @@
|
|||||||
|
|
||||||
function t(key) { return T[currentLang][key] || T['en'][key] || key; }
|
function t(key) { return T[currentLang][key] || T['en'][key] || key; }
|
||||||
|
|
||||||
|
function showOAuthError() {
|
||||||
|
const params = new URLSearchParams(window.location.search);
|
||||||
|
const code = params.get('error');
|
||||||
|
const el = document.getElementById('oauth-alert');
|
||||||
|
if (!code || !code.startsWith('oauth_')) {
|
||||||
|
el.classList.remove('visible');
|
||||||
|
el.textContent = '';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const key = 'err_' + code;
|
||||||
|
el.textContent = t(key) || t('err_oauth_error');
|
||||||
|
el.classList.add('visible');
|
||||||
|
}
|
||||||
|
|
||||||
function applyLang() {
|
function applyLang() {
|
||||||
document.documentElement.lang = currentLang;
|
document.documentElement.lang = currentLang;
|
||||||
|
document.title = t('docTitle');
|
||||||
document.querySelectorAll('[data-i18n]').forEach(el => {
|
document.querySelectorAll('[data-i18n]').forEach(el => {
|
||||||
const key = el.getAttribute('data-i18n');
|
const key = el.getAttribute('data-i18n');
|
||||||
el.textContent = t(key);
|
el.textContent = t(key);
|
||||||
@@ -111,6 +171,7 @@
|
|||||||
const map = { 'zh-CN': '简', 'zh-TW': '繁', 'en': 'EN' };
|
const map = { 'zh-CN': '简', 'zh-TW': '繁', 'en': 'EN' };
|
||||||
btn.classList.toggle('active', btn.textContent === map[currentLang]);
|
btn.classList.toggle('active', btn.textContent === map[currentLang]);
|
||||||
});
|
});
|
||||||
|
showOAuthError();
|
||||||
}
|
}
|
||||||
|
|
||||||
function setLang(lang) {
|
function setLang(lang) {
|
||||||
|
|||||||
@@ -2,7 +2,14 @@
|
|||||||
# 复制此文件为 .env 并填写真实值
|
# 复制此文件为 .env 并填写真实值
|
||||||
|
|
||||||
# ─── 数据库 ───────────────────────────────────────────────────────────
|
# ─── 数据库 ───────────────────────────────────────────────────────────
|
||||||
SECRETS_DATABASE_URL=postgres://postgres:PASSWORD@HOST:PORT/secrets-mcp
|
# Web 会话(tower-sessions)与业务数据共用此库;启动时会自动 migrate 会话表,无需额外环境变量。
|
||||||
|
SECRETS_DATABASE_URL=postgres://postgres:PASSWORD@db.refining.ltd:5432/secrets-mcp
|
||||||
|
# 强烈建议生产使用 verify-full(至少 verify-ca)
|
||||||
|
SECRETS_DATABASE_SSL_MODE=verify-full
|
||||||
|
# 私有 CA 或自建链路时填写 CA 根证书路径;使用公共受信 CA 可留空
|
||||||
|
# SECRETS_DATABASE_SSL_ROOT_CERT=/etc/secrets/pg-ca.crt
|
||||||
|
# 当设为 prod/production 时,服务会拒绝弱 TLS 模式(prefer/disable/allow/require)
|
||||||
|
SECRETS_ENV=production
|
||||||
|
|
||||||
# ─── 服务地址 ─────────────────────────────────────────────────────────
|
# ─── 服务地址 ─────────────────────────────────────────────────────────
|
||||||
# 内网监听地址(Cloudflare / Nginx 反代时填内网端口)
|
# 内网监听地址(Cloudflare / Nginx 反代时填内网端口)
|
||||||
@@ -21,6 +28,9 @@ GOOGLE_CLIENT_SECRET=
|
|||||||
# WECHAT_APP_CLIENT_ID=
|
# WECHAT_APP_CLIENT_ID=
|
||||||
# WECHAT_APP_CLIENT_SECRET=
|
# WECHAT_APP_CLIENT_SECRET=
|
||||||
|
|
||||||
|
# ─── 日志(可选)──────────────────────────────────────────────────────
|
||||||
|
# RUST_LOG=secrets_mcp=debug
|
||||||
|
|
||||||
# ─── 注意 ─────────────────────────────────────────────────────────────
|
# ─── 注意 ─────────────────────────────────────────────────────────────
|
||||||
# SERVER_MASTER_KEY 已不再需要。
|
# SERVER_MASTER_KEY 已不再需要。
|
||||||
# 新架构(E2EE)中,加密密钥由用户密码短语在客户端本地派生,服务端不持有原始密钥。
|
# 新架构(E2EE)中,加密密钥由用户密码短语在客户端本地派生,服务端不持有原始密钥。
|
||||||
|
|||||||
92
deploy/postgres-tls-hardening.md
Normal file
92
deploy/postgres-tls-hardening.md
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
# PostgreSQL TLS Hardening Runbook
|
||||||
|
|
||||||
|
This runbook applies to:
|
||||||
|
|
||||||
|
- PostgreSQL server: `47.117.131.22` (`db.refining.ltd`)
|
||||||
|
- `secrets-mcp` app server: `47.238.146.244` (`secrets.refining.app`)
|
||||||
|
|
||||||
|
## 1) Issue certificate for `db.refining.ltd` (Let's Encrypt + Cloudflare DNS-01)
|
||||||
|
|
||||||
|
Install `acme.sh` on the PostgreSQL server and use a Cloudflare API token with DNS edit permission for the target zone.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl https://get.acme.sh | sh -s email=ops@refining.ltd
|
||||||
|
export CF_Token="your_cloudflare_dns_token"
|
||||||
|
export CF_Zone_ID="your_zone_id"
|
||||||
|
~/.acme.sh/acme.sh --issue --dns dns_cf -d db.refining.ltd --keylength ec-256
|
||||||
|
```
|
||||||
|
|
||||||
|
Install cert/key into a PostgreSQL-readable path:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo mkdir -p /etc/postgresql/tls
|
||||||
|
sudo ~/.acme.sh/acme.sh --install-cert -d db.refining.ltd --ecc \
|
||||||
|
--fullchain-file /etc/postgresql/tls/fullchain.pem \
|
||||||
|
--key-file /etc/postgresql/tls/privkey.pem \
|
||||||
|
--reloadcmd "systemctl reload postgresql || systemctl restart postgresql"
|
||||||
|
sudo chown -R postgres:postgres /etc/postgresql/tls
|
||||||
|
sudo chmod 600 /etc/postgresql/tls/privkey.pem
|
||||||
|
sudo chmod 644 /etc/postgresql/tls/fullchain.pem
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2) Configure PostgreSQL TLS and access rules
|
||||||
|
|
||||||
|
In `postgresql.conf`:
|
||||||
|
|
||||||
|
```conf
|
||||||
|
ssl = on
|
||||||
|
ssl_cert_file = '/etc/postgresql/tls/fullchain.pem'
|
||||||
|
ssl_key_file = '/etc/postgresql/tls/privkey.pem'
|
||||||
|
```
|
||||||
|
|
||||||
|
In `pg_hba.conf`, allow app traffic via TLS only (example):
|
||||||
|
|
||||||
|
```conf
|
||||||
|
hostssl secrets-mcp postgres 47.238.146.244/32 scram-sha-256
|
||||||
|
```
|
||||||
|
|
||||||
|
Keep a safe admin path (`local` socket or restricted source CIDR) before removing old plaintext `host` rules.
|
||||||
|
|
||||||
|
Reload PostgreSQL:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo systemctl reload postgresql
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3) Verify server-side TLS
|
||||||
|
|
||||||
|
```bash
|
||||||
|
openssl s_client -starttls postgres -connect db.refining.ltd:5432 -servername db.refining.ltd
|
||||||
|
```
|
||||||
|
|
||||||
|
The handshake should succeed and the certificate should match `db.refining.ltd`.
|
||||||
|
|
||||||
|
## 4) Update `secrets-mcp` app server env
|
||||||
|
|
||||||
|
Use environment values like:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
SECRETS_DATABASE_URL=postgres://postgres:***@db.refining.ltd:5432/secrets-mcp
|
||||||
|
SECRETS_DATABASE_SSL_MODE=verify-full
|
||||||
|
SECRETS_ENV=production
|
||||||
|
```
|
||||||
|
|
||||||
|
If you use private CA instead of public CA, also set:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
SECRETS_DATABASE_SSL_ROOT_CERT=/etc/secrets/pg-ca.crt
|
||||||
|
```
|
||||||
|
|
||||||
|
Restart `secrets-mcp` after updating env.
|
||||||
|
|
||||||
|
## 5) Verify from app server
|
||||||
|
|
||||||
|
Run positive and negative checks:
|
||||||
|
|
||||||
|
- Positive: app starts, migrations pass, dashboard + MCP API work.
|
||||||
|
- Negative:
|
||||||
|
- wrong hostname -> connection fails
|
||||||
|
- wrong CA file -> connection fails
|
||||||
|
- disable TLS on DB -> connection fails
|
||||||
|
|
||||||
|
This ensures no silent downgrade to weak TLS in production.
|
||||||
126
migrations/001_nn_schema.sql
Normal file
126
migrations/001_nn_schema.sql
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
-- Entry-Secret N:N migration (manual SQL)
|
||||||
|
-- Safe to re-run: uses IF EXISTS/IF NOT EXISTS guards.
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- 1) secrets: add new columns
|
||||||
|
ALTER TABLE secrets
|
||||||
|
ADD COLUMN IF NOT EXISTS user_id UUID REFERENCES users(id) ON DELETE SET NULL;
|
||||||
|
ALTER TABLE secrets
|
||||||
|
ADD COLUMN IF NOT EXISTS type VARCHAR(64) NOT NULL DEFAULT 'text';
|
||||||
|
|
||||||
|
-- 2) rename field_name -> name (idempotent)
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'secrets' AND column_name = 'field_name'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE secrets RENAME COLUMN field_name TO name;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- 3) create join table
|
||||||
|
CREATE TABLE IF NOT EXISTS entry_secrets (
|
||||||
|
entry_id UUID NOT NULL REFERENCES entries(id) ON DELETE CASCADE,
|
||||||
|
secret_id UUID NOT NULL REFERENCES secrets(id) ON DELETE CASCADE,
|
||||||
|
sort_order INT NOT NULL DEFAULT 0,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
PRIMARY KEY (entry_id, secret_id)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_entry_secrets_secret_id ON entry_secrets(secret_id);
|
||||||
|
|
||||||
|
-- 4) backfill user_id and relationship from old secrets.entry_id
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'secrets' AND column_name = 'entry_id'
|
||||||
|
) THEN
|
||||||
|
UPDATE secrets s
|
||||||
|
SET user_id = e.user_id
|
||||||
|
FROM entries e
|
||||||
|
WHERE s.entry_id = e.id AND s.user_id IS NULL;
|
||||||
|
|
||||||
|
INSERT INTO entry_secrets(entry_id, secret_id, sort_order)
|
||||||
|
SELECT entry_id, id, 0
|
||||||
|
FROM secrets
|
||||||
|
WHERE entry_id IS NOT NULL
|
||||||
|
ON CONFLICT DO NOTHING;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- 5) backfill secret types
|
||||||
|
UPDATE secrets SET type = 'pem' WHERE name IN ('ssh_key');
|
||||||
|
UPDATE secrets SET type = 'password' WHERE name IN ('password');
|
||||||
|
UPDATE secrets SET type = 'phone' WHERE name LIKE 'phone%';
|
||||||
|
UPDATE secrets SET type = 'url' WHERE name IN ('webhook_url', 'address');
|
||||||
|
UPDATE secrets
|
||||||
|
SET type = 'token'
|
||||||
|
WHERE name IN (
|
||||||
|
'access_key_id',
|
||||||
|
'access_key_secret',
|
||||||
|
'global_api_key',
|
||||||
|
'api_key',
|
||||||
|
'secret_key',
|
||||||
|
'personal_access_token',
|
||||||
|
'runner_token',
|
||||||
|
'GOOGLE_CLIENT_ID',
|
||||||
|
'GOOGLE_CLIENT_SECRET'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- 6) drop old entry_id path
|
||||||
|
ALTER TABLE secrets DROP CONSTRAINT IF EXISTS secrets_entry_id_fkey;
|
||||||
|
DROP INDEX IF EXISTS idx_secrets_entry_id;
|
||||||
|
ALTER TABLE secrets DROP CONSTRAINT IF EXISTS secrets_entry_id_field_name_key;
|
||||||
|
ALTER TABLE secrets DROP CONSTRAINT IF EXISTS secrets_entry_id_name_key;
|
||||||
|
ALTER TABLE secrets DROP COLUMN IF EXISTS entry_id;
|
||||||
|
|
||||||
|
-- 7) add indexes for new access paths
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_secrets_user_id
|
||||||
|
ON secrets(user_id) WHERE user_id IS NOT NULL;
|
||||||
|
DO $$
|
||||||
|
DECLARE
|
||||||
|
duplicate_samples TEXT;
|
||||||
|
BEGIN
|
||||||
|
SELECT string_agg(
|
||||||
|
format('user_id=%s, name=%s, count=%s', t.user_id, t.name, t.cnt),
|
||||||
|
E'\n'
|
||||||
|
)
|
||||||
|
INTO duplicate_samples
|
||||||
|
FROM (
|
||||||
|
SELECT user_id::TEXT AS user_id, name, COUNT(*) AS cnt
|
||||||
|
FROM secrets
|
||||||
|
WHERE user_id IS NOT NULL
|
||||||
|
GROUP BY user_id, name
|
||||||
|
HAVING COUNT(*) > 1
|
||||||
|
ORDER BY cnt DESC, user_id, name
|
||||||
|
LIMIT 20
|
||||||
|
) t;
|
||||||
|
|
||||||
|
IF duplicate_samples IS NOT NULL THEN
|
||||||
|
RAISE EXCEPTION
|
||||||
|
'Cannot enforce unique constraint on secrets(user_id, name). Duplicates found:%',
|
||||||
|
E'\n' || duplicate_samples
|
||||||
|
USING HINT = 'Please deduplicate conflicting rows, then rerun migration.';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_secrets_unique_user_name
|
||||||
|
ON secrets(user_id, name) WHERE user_id IS NOT NULL;
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_secrets_name ON secrets(name);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_secrets_type ON secrets(type);
|
||||||
|
|
||||||
|
-- 8) secrets_history: rename and remove entry-scoped columns
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'secrets_history' AND column_name = 'field_name'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE secrets_history RENAME COLUMN field_name TO name;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
ALTER TABLE secrets_history DROP COLUMN IF EXISTS entry_id;
|
||||||
|
ALTER TABLE secrets_history DROP COLUMN IF EXISTS entry_version;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
67
migrations/002_data_cleanup.sql
Normal file
67
migrations/002_data_cleanup.sql
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
-- Metadata cleanup migration (manual SQL)
|
||||||
|
-- Keep tags/type as dedicated columns; remove duplicated metadata keys.
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- 1) Promote metadata.type -> entries.type when present.
|
||||||
|
UPDATE entries
|
||||||
|
SET type = metadata->>'type'
|
||||||
|
WHERE metadata->>'type' IS NOT NULL
|
||||||
|
AND metadata->>'type' <> '';
|
||||||
|
|
||||||
|
-- 2) Remove metadata.type.
|
||||||
|
UPDATE entries
|
||||||
|
SET metadata = metadata - 'type'
|
||||||
|
WHERE metadata ? 'type';
|
||||||
|
|
||||||
|
-- 3) Remove metadata.environment (duplicated by tags prod/dev).
|
||||||
|
UPDATE entries
|
||||||
|
SET metadata = metadata - 'environment'
|
||||||
|
WHERE metadata ? 'environment';
|
||||||
|
|
||||||
|
-- 4) Remove metadata.account when equal to folder.
|
||||||
|
UPDATE entries
|
||||||
|
SET metadata = metadata - 'account'
|
||||||
|
WHERE metadata->>'account' = folder;
|
||||||
|
|
||||||
|
-- 5) Normalize manufacturer -> provider.
|
||||||
|
UPDATE entries
|
||||||
|
SET metadata = (metadata - 'manufacturer')
|
||||||
|
|| jsonb_build_object('provider', metadata->>'manufacturer')
|
||||||
|
WHERE metadata ? 'manufacturer'
|
||||||
|
AND NOT metadata ? 'provider';
|
||||||
|
|
||||||
|
UPDATE entries
|
||||||
|
SET metadata = metadata - 'manufacturer'
|
||||||
|
WHERE metadata ? 'manufacturer'
|
||||||
|
AND metadata ? 'provider';
|
||||||
|
|
||||||
|
-- 6) Drop ssh_key_format (moved to secrets.type).
|
||||||
|
UPDATE entries
|
||||||
|
SET metadata = metadata - 'ssh_key_format'
|
||||||
|
WHERE metadata ? 'ssh_key_format';
|
||||||
|
|
||||||
|
-- 7) Remove display_name when duplicated by name.
|
||||||
|
UPDATE entries
|
||||||
|
SET metadata = metadata - 'display_name'
|
||||||
|
WHERE metadata->>'display_name' = name;
|
||||||
|
|
||||||
|
-- 8) Condense server_* metadata into server_ref.
|
||||||
|
UPDATE entries
|
||||||
|
SET metadata = metadata
|
||||||
|
- 'server_account'
|
||||||
|
- 'server_hostname'
|
||||||
|
- 'server_location'
|
||||||
|
- 'server_public_ip'
|
||||||
|
|| CASE
|
||||||
|
WHEN metadata ? 'server_entry_name'
|
||||||
|
THEN jsonb_build_object('server_ref', metadata->>'server_entry_name')
|
||||||
|
ELSE '{}'::jsonb
|
||||||
|
END
|
||||||
|
WHERE metadata ? 'server_entry_name' OR metadata ? 'server_account';
|
||||||
|
|
||||||
|
UPDATE entries
|
||||||
|
SET metadata = metadata - 'server_entry_name'
|
||||||
|
WHERE metadata ? 'server_entry_name';
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
22
scripts/cleanup-orphan-user-ids.sql
Normal file
22
scripts/cleanup-orphan-user-ids.sql
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
-- Run against prod BEFORE deploying secrets-mcp with FK migration.
|
||||||
|
-- Requires: write access to SECRETS_DATABASE_URL.
|
||||||
|
-- Example: psql "$SECRETS_DATABASE_URL" -v ON_ERROR_STOP=1 -f scripts/cleanup-orphan-user-ids.sql
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
UPDATE entries
|
||||||
|
SET user_id = NULL
|
||||||
|
WHERE user_id IS NOT NULL
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM users u WHERE u.id = entries.user_id);
|
||||||
|
|
||||||
|
UPDATE entries_history
|
||||||
|
SET user_id = NULL
|
||||||
|
WHERE user_id IS NOT NULL
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM users u WHERE u.id = entries_history.user_id);
|
||||||
|
|
||||||
|
UPDATE audit_log
|
||||||
|
SET user_id = NULL
|
||||||
|
WHERE user_id IS NOT NULL
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM users u WHERE u.id = audit_log.user_id);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
81
scripts/migrate-db-prod-to-nn-test.sh
Executable file
81
scripts/migrate-db-prod-to-nn-test.sh
Executable file
@@ -0,0 +1,81 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Migrate PostgreSQL data from secrets-mcp-prod to secrets-nn-test.
|
||||||
|
#
|
||||||
|
# Prereqs: pg_dump and pg_restore (PostgreSQL client tools) on PATH.
|
||||||
|
# TLS: Use the same connection parameters as your MCP / app (e.g. sslmode=verify-full
|
||||||
|
# and PGSSLROOTCERT if needed). If local psql fails with "certificate verify failed",
|
||||||
|
# run this script from a host that trusts the server CA, or set PGSSLROOTCERT.
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# export SOURCE_DATABASE_URL='postgres://USER:PASS@host:5432/secrets-mcp-prod?sslmode=verify-full'
|
||||||
|
# export TARGET_DATABASE_URL='postgres://USER:PASS@host:5432/secrets-nn-test?sslmode=verify-full'
|
||||||
|
# ./scripts/migrate-db-prod-to-nn-test.sh
|
||||||
|
#
|
||||||
|
# Options (env):
|
||||||
|
# BACKUP_TARGET_FIRST=1 # default: dump target to ./backup-secrets-nn-test-<timestamp>.dump before restore
|
||||||
|
# RUN_NN_SQL=1 # default: run migrations/001_nn_schema.sql then 002_data_cleanup.sql on target after restore
|
||||||
|
# SKIP_TARGET_BACKUP=1 # skip target backup
|
||||||
|
#
|
||||||
|
# WARNINGS:
|
||||||
|
# - pg_restore with --clean --if-exists drops objects that exist in the dump; target DB is replaced
|
||||||
|
# to match the logical content of the source dump (same as typical full restore).
|
||||||
|
# - Optionally keep a manual dump of the target before proceeding.
|
||||||
|
# - 001_nn_schema.sql will fail if secrets has duplicate (user_id, name) after backfill; fix data first.
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||||
|
cd "$ROOT"
|
||||||
|
|
||||||
|
SOURCE_URL="${SOURCE_DATABASE_URL:-}"
|
||||||
|
TARGET_URL="${TARGET_DATABASE_URL:-}"
|
||||||
|
|
||||||
|
if [[ -z "$SOURCE_URL" || -z "$TARGET_URL" ]]; then
|
||||||
|
echo "Set SOURCE_DATABASE_URL and TARGET_DATABASE_URL (postgres URLs)." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! command -v pg_dump >/dev/null || ! command -v pg_restore >/dev/null; then
|
||||||
|
echo "pg_dump and pg_restore are required." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
TS="$(date +%Y%m%d%H%M%S)"
|
||||||
|
DUMP_FILE="${DUMP_FILE:-$ROOT/tmp/secrets-mcp-prod-${TS}.dump}"
|
||||||
|
mkdir -p "$(dirname "$DUMP_FILE")"
|
||||||
|
|
||||||
|
if [[ "${EXCLUDE_TOWER_SESSIONS:-}" == "1" ]]; then
|
||||||
|
echo "==> Excluding schema tower_sessions from dump"
|
||||||
|
pg_dump "$SOURCE_URL" -Fc --no-owner --no-acl --exclude-schema=tower_sessions -f "$DUMP_FILE"
|
||||||
|
else
|
||||||
|
echo "==> Dumping source (custom format) -> $DUMP_FILE"
|
||||||
|
pg_dump "$SOURCE_URL" -Fc --no-owner --no-acl -f "$DUMP_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "${SKIP_TARGET_BACKUP:-}" != "1" && "${BACKUP_TARGET_FIRST:-1}" == "1" ]]; then
|
||||||
|
BACKUP_FILE="$ROOT/tmp/secrets-nn-test-before-${TS}.dump"
|
||||||
|
echo "==> Backing up target -> $BACKUP_FILE"
|
||||||
|
pg_dump "$TARGET_URL" -Fc --no-owner --no-acl -f "$BACKUP_FILE" || {
|
||||||
|
echo "Target backup failed (empty DB is OK). Continuing." >&2
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "==> Restoring into target (--clean --if-exists)"
|
||||||
|
pg_restore -d "$TARGET_URL" --no-owner --no-acl --clean --if-exists --verbose "$DUMP_FILE"
|
||||||
|
|
||||||
|
if [[ "${RUN_NN_SQL:-1}" == "1" ]]; then
|
||||||
|
if [[ ! -f "$ROOT/migrations/001_nn_schema.sql" ]]; then
|
||||||
|
echo "migrations/001_nn_schema.sql not found; skip NN SQL." >&2
|
||||||
|
else
|
||||||
|
echo "==> Applying migrations/001_nn_schema.sql on target"
|
||||||
|
psql "$TARGET_URL" -v ON_ERROR_STOP=1 -f "$ROOT/migrations/001_nn_schema.sql"
|
||||||
|
fi
|
||||||
|
if [[ -f "$ROOT/migrations/002_data_cleanup.sql" ]]; then
|
||||||
|
echo "==> Applying migrations/002_data_cleanup.sql on target"
|
||||||
|
psql "$TARGET_URL" -v ON_ERROR_STOP=1 -f "$ROOT/migrations/002_data_cleanup.sql"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "==> Done. Suggested verification:"
|
||||||
|
echo " psql \"\$TARGET_DATABASE_URL\" -c \"SELECT COUNT(*) FROM entries; SELECT COUNT(*) FROM secrets; SELECT COUNT(*) FROM entry_secrets;\""
|
||||||
|
echo " ./scripts/release-check.sh # optional app-side sanity"
|
||||||
194
scripts/migrate-v0.3.0.sql
Normal file
194
scripts/migrate-v0.3.0.sql
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
-- ============================================================================
|
||||||
|
-- migrate-v0.3.0.sql
|
||||||
|
-- Schema migration from v0.2.x → v0.3.0
|
||||||
|
--
|
||||||
|
-- Changes:
|
||||||
|
-- • entries: namespace → folder, kind → type; add notes column
|
||||||
|
-- • audit_log: namespace → folder, kind → type
|
||||||
|
-- • entries_history: namespace → folder, kind → type; add user_id column
|
||||||
|
-- • Unique index: (user_id, name) → (user_id, folder, name)
|
||||||
|
-- Same name in different folders is now allowed; no rename needed.
|
||||||
|
--
|
||||||
|
-- Safe to run multiple times (fully idempotent).
|
||||||
|
-- Preserves all data in users, entries, secrets.
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- ── entries: rename namespace→folder, kind→type ──────────────────────────────
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries' AND column_name = 'namespace'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE entries RENAME COLUMN namespace TO folder;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries' AND column_name = 'kind'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE entries RENAME COLUMN kind TO type;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Set NOT NULL + default for folder/type in entries
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries' AND column_name = 'folder'
|
||||||
|
) THEN
|
||||||
|
UPDATE entries SET folder = '' WHERE folder IS NULL;
|
||||||
|
ALTER TABLE entries ALTER COLUMN folder SET NOT NULL;
|
||||||
|
ALTER TABLE entries ALTER COLUMN folder SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries' AND column_name = 'type'
|
||||||
|
) THEN
|
||||||
|
UPDATE entries SET type = '' WHERE type IS NULL;
|
||||||
|
ALTER TABLE entries ALTER COLUMN type SET NOT NULL;
|
||||||
|
ALTER TABLE entries ALTER COLUMN type SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Add notes column to entries if missing
|
||||||
|
ALTER TABLE entries ADD COLUMN IF NOT EXISTS notes TEXT NOT NULL DEFAULT '';
|
||||||
|
|
||||||
|
-- ── audit_log: rename namespace→folder, kind→type ────────────────────────────
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'audit_log' AND column_name = 'namespace'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE audit_log RENAME COLUMN namespace TO folder;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'audit_log' AND column_name = 'kind'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE audit_log RENAME COLUMN kind TO type;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'audit_log' AND column_name = 'folder'
|
||||||
|
) THEN
|
||||||
|
UPDATE audit_log SET folder = '' WHERE folder IS NULL;
|
||||||
|
ALTER TABLE audit_log ALTER COLUMN folder SET NOT NULL;
|
||||||
|
ALTER TABLE audit_log ALTER COLUMN folder SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'audit_log' AND column_name = 'type'
|
||||||
|
) THEN
|
||||||
|
UPDATE audit_log SET type = '' WHERE type IS NULL;
|
||||||
|
ALTER TABLE audit_log ALTER COLUMN type SET NOT NULL;
|
||||||
|
ALTER TABLE audit_log ALTER COLUMN type SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
ALTER TABLE audit_log DROP COLUMN IF EXISTS actor;
|
||||||
|
|
||||||
|
-- ── entries_history: rename namespace→folder, kind→type; add user_id ─────────
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries_history' AND column_name = 'namespace'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE entries_history RENAME COLUMN namespace TO folder;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries_history' AND column_name = 'kind'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE entries_history RENAME COLUMN kind TO type;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries_history' AND column_name = 'folder'
|
||||||
|
) THEN
|
||||||
|
UPDATE entries_history SET folder = '' WHERE folder IS NULL;
|
||||||
|
ALTER TABLE entries_history ALTER COLUMN folder SET NOT NULL;
|
||||||
|
ALTER TABLE entries_history ALTER COLUMN folder SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$ BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'entries_history' AND column_name = 'type'
|
||||||
|
) THEN
|
||||||
|
UPDATE entries_history SET type = '' WHERE type IS NULL;
|
||||||
|
ALTER TABLE entries_history ALTER COLUMN type SET NOT NULL;
|
||||||
|
ALTER TABLE entries_history ALTER COLUMN type SET DEFAULT '';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
ALTER TABLE entries_history ADD COLUMN IF NOT EXISTS user_id UUID;
|
||||||
|
ALTER TABLE entries_history DROP COLUMN IF EXISTS actor;
|
||||||
|
|
||||||
|
-- ── secrets_history: drop actor column ───────────────────────────────────────
|
||||||
|
ALTER TABLE secrets_history DROP COLUMN IF EXISTS actor;
|
||||||
|
|
||||||
|
-- ── Rebuild unique indexes: (user_id, folder, name) ──────────────────────────
|
||||||
|
-- Note: folder is now part of the key, so same name in different folders is
|
||||||
|
-- naturally distinct — no rename of existing rows needed.
|
||||||
|
DROP INDEX IF EXISTS idx_entries_unique_legacy;
|
||||||
|
DROP INDEX IF EXISTS idx_entries_unique_user;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_entries_unique_legacy
|
||||||
|
ON entries(folder, name)
|
||||||
|
WHERE user_id IS NULL;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_entries_unique_user
|
||||||
|
ON entries(user_id, folder, name)
|
||||||
|
WHERE user_id IS NOT NULL;
|
||||||
|
|
||||||
|
-- ── Replace old namespace/kind indexes with folder/type ──────────────────────
|
||||||
|
DROP INDEX IF EXISTS idx_entries_namespace;
|
||||||
|
DROP INDEX IF EXISTS idx_entries_kind;
|
||||||
|
DROP INDEX IF EXISTS idx_audit_log_ns_kind;
|
||||||
|
DROP INDEX IF EXISTS idx_entries_history_ns_kind_name;
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_entries_folder
|
||||||
|
ON entries(folder) WHERE folder <> '';
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_entries_type
|
||||||
|
ON entries(type) WHERE type <> '';
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_entries_user_id
|
||||||
|
ON entries(user_id) WHERE user_id IS NOT NULL;
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_audit_log_folder_type
|
||||||
|
ON audit_log(folder, type);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_entries_history_folder_type_name
|
||||||
|
ON entries_history(folder, type, name, version DESC);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_entries_history_user_id
|
||||||
|
ON entries_history(user_id) WHERE user_id IS NOT NULL;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
-- ── Verification queries (run these manually to confirm) ─────────────────────
|
||||||
|
-- SELECT column_name, data_type FROM information_schema.columns
|
||||||
|
-- WHERE table_name = 'entries' ORDER BY ordinal_position;
|
||||||
|
-- SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'entries';
|
||||||
|
-- SELECT COUNT(*) FROM entries;
|
||||||
|
-- SELECT COUNT(*) FROM users;
|
||||||
|
-- SELECT COUNT(*) FROM secrets;
|
||||||
@@ -12,12 +12,13 @@ echo "==> 当前 secrets-mcp 版本: ${version}"
|
|||||||
echo "==> 检查是否已存在 tag: ${tag}"
|
echo "==> 检查是否已存在 tag: ${tag}"
|
||||||
|
|
||||||
if git rev-parse "refs/tags/${tag}" >/dev/null 2>&1; then
|
if git rev-parse "refs/tags/${tag}" >/dev/null 2>&1; then
|
||||||
echo "错误: 已存在 tag ${tag}"
|
echo "提示: 已存在 tag ${tag},将按重复构建处理,不阻断检查。"
|
||||||
echo "请先 bump crates/secrets-mcp/Cargo.toml 中的 version,再执行 cargo build 同步 Cargo.lock。"
|
echo "如需创建新的发布版本,请先 bump crates/secrets-mcp/Cargo.toml 中的 version。"
|
||||||
exit 1
|
else
|
||||||
|
echo "==> 未发现重复 tag,将创建新版本"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "==> 未发现重复 tag,开始执行检查"
|
echo "==> 开始执行检查"
|
||||||
cargo fmt -- --check
|
cargo fmt -- --check
|
||||||
cargo clippy --locked -- -D warnings
|
cargo clippy --locked -- -D warnings
|
||||||
cargo test --locked
|
cargo test --locked
|
||||||
|
|||||||
Reference in New Issue
Block a user