Compare commits
9 Commits
df701f21b9
...
secrets-mc
| Author | SHA1 | Date | |
|---|---|---|---|
| dd24f7cc44 | |||
|
|
aefad33870 | ||
|
|
0ffb81e57f | ||
|
|
4a1654c820 | ||
|
|
a15e2eaf4a | ||
|
|
1518388374 | ||
| b99d821644 | |||
|
|
32f275f88a | ||
|
|
c6fb457734 |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -2,6 +2,7 @@
|
|||||||
.env
|
.env
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.cursor/
|
.cursor/
|
||||||
# Google OAuth 下载的 JSON 凭据文件
|
*.pem
|
||||||
|
tmp/
|
||||||
client_secret_*.apps.googleusercontent.com.json
|
client_secret_*.apps.googleusercontent.com.json
|
||||||
*.pem
|
node_modules/
|
||||||
35
AGENTS.md
35
AGENTS.md
@@ -55,13 +55,24 @@ entries (
|
|||||||
```sql
|
```sql
|
||||||
secrets (
|
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)
|
)
|
||||||
|
-- 唯一:UNIQUE(user_id, name) WHERE user_id IS NOT NULL
|
||||||
|
```
|
||||||
|
|
||||||
|
```sql
|
||||||
|
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)
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -108,17 +119,20 @@ oauth_accounts (
|
|||||||
| 字段 | 含义 | 示例 |
|
| 字段 | 含义 | 示例 |
|
||||||
|------|------|------|
|
|------|------|------|
|
||||||
| `folder` | 隔离空间(参与唯一键) | `refining` |
|
| `folder` | 隔离空间(参与唯一键) | `refining` |
|
||||||
| `type` | 软分类(不参与唯一键) | `server`, `service`, `key`, `person` |
|
| `type` | 软分类(不参与唯一键,用户自定义) | `server`, `service`, `account`, `person`, `document` |
|
||||||
| `name` | 标识名 | `gitea`, `aliyun` |
|
| `name` | 标识名 | `gitea`, `aliyun` |
|
||||||
| `notes` | 非敏感说明 | 自由文本 |
|
| `notes` | 非敏感说明 | 自由文本 |
|
||||||
| `tags` | 标签 | `["aliyun","prod"]` |
|
| `tags` | 标签 | `["aliyun","prod"]` |
|
||||||
| `metadata` | 明文描述 | `ip`、`url`、`key_ref` |
|
| `metadata` | 明文描述 | `ip`、`url`、`subtype` |
|
||||||
| `secrets.field_name` | 加密字段名(明文) | `token`, `ssh_key` |
|
| `secrets.name` | 密钥名称(调用方提供) | `token`, `ssh_key`, `password` |
|
||||||
|
| `secrets.type` | 密钥类型(调用方提供,默认 `text`) | `text`, `password`, `key` |
|
||||||
| `secrets.encrypted` | 密文 | AES-GCM |
|
| `secrets.encrypted` | 密文 | AES-GCM |
|
||||||
|
|
||||||
### PEM 共享(`key_ref`)
|
### 共享密钥(N:N 关联)
|
||||||
|
|
||||||
建议将共享 PEM 存为 **`type=key`** 的 entry;其它记录在 `metadata.key_ref` 指向目标 entry 的 `name`(支持 `folder/name` 格式消歧)。删除被引用 key 时,服务会自动迁移为单副本 + 重定向(复制到首个引用方,其余引用方改指向新 owner);解析逻辑见 `secrets_core::service::env_map`。
|
多个 entry 可共享同一 secret 字段,通过 `entry_secrets` 中间表关联。
|
||||||
|
添加条目时通过 `link_secret_names` 参数指定要关联的已有 secret name(按 `(user_id, name)` 精确匹配)。
|
||||||
|
删除 entry 时仅解除关联,secret 本身若仍被引用则保留;不再被任何 entry 引用时自动清理。
|
||||||
|
|
||||||
## 代码规范
|
## 代码规范
|
||||||
|
|
||||||
@@ -166,6 +180,9 @@ git tag -l 'secrets-mcp-*'
|
|||||||
| 变量 | 说明 |
|
| 变量 | 说明 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| `SECRETS_DATABASE_URL` | **必填**。PostgreSQL URL。 |
|
| `SECRETS_DATABASE_URL` | **必填**。PostgreSQL URL。 |
|
||||||
|
| `SECRETS_DATABASE_SSL_MODE` | 可选但强烈建议生产必填。推荐 `verify-full`(至少 `verify-ca`)。 |
|
||||||
|
| `SECRETS_DATABASE_SSL_ROOT_CERT` | 可选。私有 CA 或自签链路时指定 CA 根证书路径。 |
|
||||||
|
| `SECRETS_ENV` | 可选。设为 `prod` / `production` 时会拒绝弱 PostgreSQL TLS 模式。 |
|
||||||
| `BASE_URL` | 对外基址;OAuth 回调 `${BASE_URL}/auth/google/callback`。 |
|
| `BASE_URL` | 对外基址;OAuth 回调 `${BASE_URL}/auth/google/callback`。 |
|
||||||
| `SECRETS_MCP_BIND` | 监听地址,默认 `127.0.0.1:9315`(容器/远程直接暴露时需改为 `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` | 可选;仅运行时配置。 |
|
||||||
|
|||||||
135
Cargo.lock
generated
135
Cargo.lock
generated
@@ -464,6 +464,20 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dashmap"
|
||||||
|
version = "6.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"crossbeam-utils",
|
||||||
|
"hashbrown 0.14.5",
|
||||||
|
"lock_api",
|
||||||
|
"once_cell",
|
||||||
|
"parking_lot_core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "der"
|
name = "der"
|
||||||
version = "0.7.10"
|
version = "0.7.10"
|
||||||
@@ -596,6 +610,12 @@ version = "0.1.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "foldhash"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "form_urlencoded"
|
name = "form_urlencoded"
|
||||||
version = "1.2.2"
|
version = "1.2.2"
|
||||||
@@ -687,6 +707,12 @@ version = "0.3.32"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393"
|
checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-timer"
|
||||||
|
version = "3.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-util"
|
name = "futures-util"
|
||||||
version = "0.3.32"
|
version = "0.3.32"
|
||||||
@@ -765,6 +791,35 @@ dependencies = [
|
|||||||
"polyval",
|
"polyval",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "governor"
|
||||||
|
version = "0.10.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9efcab3c1958580ff1f25a2a41be1668f7603d849bb63af523b208a3cc1223b8"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"dashmap",
|
||||||
|
"futures-sink",
|
||||||
|
"futures-timer",
|
||||||
|
"futures-util",
|
||||||
|
"getrandom 0.3.4",
|
||||||
|
"hashbrown 0.16.1",
|
||||||
|
"nonzero_ext",
|
||||||
|
"parking_lot",
|
||||||
|
"portable-atomic",
|
||||||
|
"quanta",
|
||||||
|
"rand 0.9.2",
|
||||||
|
"smallvec",
|
||||||
|
"spinning_top",
|
||||||
|
"web-time",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.14.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.15.5"
|
version = "0.15.5"
|
||||||
@@ -773,7 +828,7 @@ checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"allocator-api2",
|
"allocator-api2",
|
||||||
"equivalent",
|
"equivalent",
|
||||||
"foldhash",
|
"foldhash 0.1.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -781,6 +836,11 @@ name = "hashbrown"
|
|||||||
version = "0.16.1"
|
version = "0.16.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
|
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
|
||||||
|
dependencies = [
|
||||||
|
"allocator-api2",
|
||||||
|
"equivalent",
|
||||||
|
"foldhash 0.2.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hashlink"
|
name = "hashlink"
|
||||||
@@ -1283,6 +1343,12 @@ dependencies = [
|
|||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nonzero_ext"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "38bf9645c8b145698bb0b18a4637dcacbc421ea49bef2317e4fd8065a387cf21"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nu-ansi-term"
|
name = "nu-ansi-term"
|
||||||
version = "0.50.3"
|
version = "0.50.3"
|
||||||
@@ -1463,6 +1529,12 @@ dependencies = [
|
|||||||
"universal-hash",
|
"universal-hash",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "portable-atomic"
|
||||||
|
version = "1.13.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "potential_utf"
|
name = "potential_utf"
|
||||||
version = "0.1.4"
|
version = "0.1.4"
|
||||||
@@ -1506,6 +1578,21 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quanta"
|
||||||
|
version = "0.12.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f3ab5a9d756f0d97bdc89019bd2e4ea098cf9cde50ee7564dde6b81ccc8f06c7"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
"libc",
|
||||||
|
"once_cell",
|
||||||
|
"raw-cpuid",
|
||||||
|
"wasi",
|
||||||
|
"web-sys",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quinn"
|
name = "quinn"
|
||||||
version = "0.11.9"
|
version = "0.11.9"
|
||||||
@@ -1658,6 +1745,15 @@ version = "0.10.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0c8d0fd677905edcbeedbf2edb6494d676f0e98d54d5cf9bda0b061cb8fb8aba"
|
checksum = "0c8d0fd677905edcbeedbf2edb6494d676f0e98d54d5cf9bda0b061cb8fb8aba"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "raw-cpuid"
|
||||||
|
version = "11.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "498cd0dc59d73224351ee52a95fee0f1a617a2eae0e7d9d720cc622c73a54186"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.18"
|
version = "0.5.18"
|
||||||
@@ -1960,6 +2056,7 @@ dependencies = [
|
|||||||
"sha2",
|
"sha2",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"toml",
|
"toml",
|
||||||
"tracing",
|
"tracing",
|
||||||
@@ -1968,7 +2065,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "secrets-mcp"
|
name = "secrets-mcp"
|
||||||
version = "0.3.7"
|
version = "0.5.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"askama",
|
"askama",
|
||||||
@@ -1976,6 +2073,7 @@ dependencies = [
|
|||||||
"axum-extra",
|
"axum-extra",
|
||||||
"chrono",
|
"chrono",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
|
"governor",
|
||||||
"http",
|
"http",
|
||||||
"rand 0.10.0",
|
"rand 0.10.0",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
@@ -1994,6 +2092,7 @@ dependencies = [
|
|||||||
"tower-sessions-sqlx-store-chrono",
|
"tower-sessions-sqlx-store-chrono",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
|
"url",
|
||||||
"urlencoding",
|
"urlencoding",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
@@ -2194,6 +2293,15 @@ dependencies = [
|
|||||||
"lock_api",
|
"lock_api",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "spinning_top"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d96d2d1d716fb500937168cc09353ffdc7a012be8475ac7308e1bdf0e3923300"
|
||||||
|
dependencies = [
|
||||||
|
"lock_api",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spki"
|
name = "spki"
|
||||||
version = "0.7.3"
|
version = "0.7.3"
|
||||||
@@ -2716,6 +2824,7 @@ dependencies = [
|
|||||||
"futures-util",
|
"futures-util",
|
||||||
"http",
|
"http",
|
||||||
"http-body",
|
"http-body",
|
||||||
|
"http-body-util",
|
||||||
"iri-string",
|
"iri-string",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"tower",
|
"tower",
|
||||||
@@ -3166,6 +3275,28 @@ dependencies = [
|
|||||||
"wasite",
|
"wasite",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-i686-pc-windows-gnu",
|
||||||
|
"winapi-x86_64-pc-windows-gnu",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-i686-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-core"
|
name = "windows-core"
|
||||||
version = "0.62.2"
|
version = "0.62.2"
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ rand = "^0.10.0"
|
|||||||
|
|
||||||
# Utils
|
# Utils
|
||||||
anyhow = "^1.0.102"
|
anyhow = "^1.0.102"
|
||||||
|
thiserror = "^2"
|
||||||
chrono = { version = "^0.4.44", features = ["serde"] }
|
chrono = { version = "^0.4.44", features = ["serde"] }
|
||||||
uuid = { version = "^1.22.0", features = ["serde"] }
|
uuid = { version = "^1.22.0", features = ["serde"] }
|
||||||
tracing = "^0.1"
|
tracing = "^0.1"
|
||||||
|
|||||||
62
README.md
62
README.md
@@ -54,10 +54,31 @@ SECRETS_ENV=production
|
|||||||
|
|
||||||
条目在逻辑上以 **`(folder, name)`** 在用户内唯一(数据库唯一索引:`user_id + folder + name`)。同名可在不同 folder 下各存一条(例如 `refining/aliyun` 与 `ricnsmart/aliyun`)。
|
条目在逻辑上以 **`(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,然后继续删除。
|
|------|-------------|------|
|
||||||
|
| `secrets_find` | 否 | 发现条目(返回含 secret_fields schema),支持 `name_query` 模糊匹配 |
|
||||||
|
| `secrets_search` | 否 | 搜索条目,支持 `query`/`folder`/`type`/`name` 过滤、`sort`/`offset` 分页、`summary` 摘要模式 |
|
||||||
|
| `secrets_get` | 是 | 按 UUID `id` 获取单条条目及解密后的 secrets |
|
||||||
|
| `secrets_add` | 是 | 添加新条目,支持 `meta_obj`/`secrets_obj` JSON 对象参数、`secret_types` 指定密钥类型、`link_secret_names` 关联已有 secret |
|
||||||
|
| `secrets_update` | 是 | 更新条目,支持 `id` 或 `name`+`folder` 定位 |
|
||||||
|
| `secrets_delete` | 否 | 删除条目,支持 `id` 或 `name`+`folder` 定位;`dry_run=true` 预览删除 |
|
||||||
|
| `secrets_history` | 否 | 查看条目历史,支持 `id` 或 `name`+`folder` 定位 |
|
||||||
|
| `secrets_rollback` | 是 | 回滚条目到指定历史版本,支持 `id` 或 `name`+`folder` 定位 |
|
||||||
|
| `secrets_export` | 是 | 导出条目(含解密明文),支持 JSON/TOML/YAML 格式 |
|
||||||
|
| `secrets_env_map` | 是 | 将 secrets 转换为环境变量映射(`UPPER(entry)_UPPER(field)` 格式),支持 `prefix` |
|
||||||
|
| `secrets_overview` | 否 | 返回各 folder 和 type 的 entry 计数概览 |
|
||||||
|
|
||||||
|
### 消歧规则
|
||||||
|
|
||||||
|
- **按 `name` 定位的工具**(`secrets_update` / `secrets_delete` / `secrets_history` / `secrets_rollback`):若该用户下仅一条匹配则直接执行;若多条(同 `name`、不同 `folder`)则返回错误并提示补全 `folder`。也可直接传 `id`(UUID)跳过消歧。
|
||||||
|
- **`secrets_get`** 仅支持通过 `id`(UUID)获取。
|
||||||
|
- **`secrets_delete`** 的 `dry_run=true` 与真实删除使用相同消歧规则——唯一则预览一条,多条则报错并要求 `folder`。
|
||||||
|
|
||||||
|
### 共享密钥
|
||||||
|
|
||||||
|
N:N 关联下,删除 entry 仅解除关联,被共享的 secret 若仍被其他 entry 引用则保留;无引用时自动清理。
|
||||||
|
|
||||||
## 加密架构(混合 E2EE)
|
## 加密架构(混合 E2EE)
|
||||||
|
|
||||||
@@ -151,25 +172,32 @@ flowchart LR
|
|||||||
|
|
||||||
## 数据模型
|
## 数据模型
|
||||||
|
|
||||||
主表 **`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`**(`folder`、`type`、`name`、`notes`、`tags`、`metadata`,多租户时带 `user_id`)+ 子表 **`secrets`**(每行一个加密字段:`name`、`type`、`encrypted`,通过 `entry_secrets` 中间表与 entry 建立 N:N 关联)。**唯一性**:`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`);已有库在进程启动时亦由同一 `migrate()` 增量补齐表、索引与 N:N 结构。若需从更早版本对照一次性 SQL,可在 git 历史中检索已移除的 `scripts/migrate-v0.3.0.sql`。**Web 登录会话**(tower-sessions)使用同一 `SECRETS_DATABASE_URL`,进程启动时对会话存储执行迁移(见 `secrets-mcp` 中 `PostgresStore::migrate`),无需额外环境变量。
|
||||||
|
|
||||||
| 位置 | 字段 | 说明 |
|
| 位置 | 字段 | 说明 |
|
||||||
|------|------|------|
|
|------|------|------|
|
||||||
| entries | folder | 组织/隔离空间,如 `refining`、`ricnsmart`;参与唯一键 |
|
| entries | folder | 组织/隔离空间,如 `refining`、`ricnsmart`;参与唯一键 |
|
||||||
| entries | type | 软分类,如 `server`、`service`、`key`、`person`(可扩展,不参与唯一键) |
|
| entries | type | 软分类,用户自定义,如 `server`、`service`、`account`、`person`、`document`(不参与唯一键) |
|
||||||
| entries | name | 人类可读标识;与 `folder` 一起在用户内唯一 |
|
| entries | name | 人类可读标识;与 `folder` 一起在用户内唯一 |
|
||||||
| entries | notes | 非敏感说明文本 |
|
| entries | notes | 非敏感说明文本 |
|
||||||
| entries | metadata | 明文 JSON(ip、url、`key_ref` 等) |
|
| entries | metadata | 明文 JSON(ip、url、subtype 等) |
|
||||||
| secrets | field_name | 明文字段名,便于 schema 展示 |
|
| secrets | name | 密钥名称(调用方提供) |
|
||||||
|
| secrets | type | 密钥类型(调用方提供,默认 `text`) |
|
||||||
| secrets | encrypted | AES-GCM 密文(含 nonce) |
|
| secrets | encrypted | AES-GCM 密文(含 nonce) |
|
||||||
| users | key_salt | PBKDF2 salt(32B),首次设置密码短语时写入 |
|
| users | key_salt | PBKDF2 salt(32B),首次设置密码短语时写入 |
|
||||||
| users | key_check | 派生密钥加密已知常量,用于验证密码短语 |
|
| users | key_check | 派生密钥加密已知常量,用于验证密码短语 |
|
||||||
| users | key_params | 派生算法参数,如 `{"alg":"pbkdf2-sha256","iterations":600000}` |
|
| users | key_params | 派生算法参数,如 `{"alg":"pbkdf2-sha256","iterations":600000}` |
|
||||||
|
|
||||||
### PEM 共享(`key_ref`)
|
### 共享密钥(N:N 关联)
|
||||||
|
|
||||||
同一 PEM 可被多条 `server` 等记录引用:建议将 PEM 存为 **`type=key`** 的 entry,在其它条目的 `metadata.key_ref` 中写目标 entry 的 `name`(支持 `folder/name` 格式消歧);轮换时只更新该目标记录即可。
|
多个条目可共享同一密文字段,通过 `entry_secrets` 中间表实现 N:N 关联:
|
||||||
删除共享 key 时,系统会自动迁移引用:将密文复制到首个引用方(单副本),其余引用方的 `key_ref` 自动重定向到该新 owner,再删除原 key 记录。
|
- 添加条目时可通过 `link_secret_names` 参数关联已有的 secret(按 `(user_id, name)` 精确匹配查找)
|
||||||
|
- 同一 secret 可被多个 entry 引用,删除某 entry 不会级联删除被共享的 secret
|
||||||
|
- 当 secret 不再被任何 entry 引用时,自动清理(`NOT EXISTS` 子查询)
|
||||||
|
|
||||||
|
### 类型(Type)
|
||||||
|
|
||||||
|
`type` 字段用于软分类,由用户自由填写,不做任何自动转换或归一化。常见示例:`server`、`service`、`account`、`person`、`document`,但任何值均可接受。
|
||||||
|
|
||||||
## 审计日志
|
## 审计日志
|
||||||
|
|
||||||
@@ -188,10 +216,18 @@ LIMIT 20;
|
|||||||
```
|
```
|
||||||
Cargo.toml
|
Cargo.toml
|
||||||
crates/secrets-core/ # db / crypto / models / audit / service
|
crates/secrets-core/ # db / crypto / models / audit / service
|
||||||
|
src/
|
||||||
|
taxonomy.rs # SECRET_TYPE_OPTIONS(secret 字段类型下拉选项)
|
||||||
|
service/ # 业务逻辑(add, search, update, delete, export, env_map 等)
|
||||||
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)
|
release-check.sh # 发版前 fmt / clippy / test
|
||||||
deploy/ # systemd、.env 示例
|
setup-gitea-actions.sh
|
||||||
|
sync-test-to-prod.sh # 测试库同步到生产(按需)
|
||||||
|
deploy/
|
||||||
|
.env.example # 环境变量模板
|
||||||
|
secrets-mcp.service # systemd 服务文件(生产部署用)
|
||||||
|
postgres-tls-hardening.md # PostgreSQL TLS 加固运维手册
|
||||||
```
|
```
|
||||||
|
|
||||||
## CI/CD(Gitea Actions)
|
## CI/CD(Gitea Actions)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ path = "src/lib.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
aes-gcm.workspace = true
|
aes-gcm.workspace = true
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
|
thiserror.workspace = true
|
||||||
chrono.workspace = true
|
chrono.workspace = true
|
||||||
rand.workspace = true
|
rand.workspace = true
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ use aes_gcm::{
|
|||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result, bail};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
|
use crate::error::AppError;
|
||||||
|
|
||||||
const NONCE_LEN: usize = 12;
|
const NONCE_LEN: usize = 12;
|
||||||
|
|
||||||
// ─── AES-256-GCM encrypt / decrypt ───────────────────────────────────────────
|
// ─── AES-256-GCM encrypt / decrypt ───────────────────────────────────────────
|
||||||
@@ -38,7 +40,7 @@ pub fn decrypt(master_key: &[u8; 32], data: &[u8]) -> Result<Vec<u8>> {
|
|||||||
let nonce = Nonce::from_slice(nonce_bytes);
|
let nonce = Nonce::from_slice(nonce_bytes);
|
||||||
cipher
|
cipher
|
||||||
.decrypt(nonce, ciphertext)
|
.decrypt(nonce, ciphertext)
|
||||||
.map_err(|_| anyhow::anyhow!("decryption failed — wrong master key or corrupted data"))
|
.map_err(|_| AppError::DecryptionFailed.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── JSON helpers ─────────────────────────────────────────────────────────────
|
// ─── JSON helpers ─────────────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -36,12 +36,31 @@ fn build_connect_options(config: &DatabaseConfig) -> Result<PgConnectOptions> {
|
|||||||
pub async fn create_pool(config: &DatabaseConfig) -> Result<PgPool> {
|
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 connect_options = build_connect_options(config)?;
|
||||||
|
|
||||||
|
// Connection pool configuration from environment
|
||||||
|
let max_connections = std::env::var("SECRETS_DATABASE_POOL_SIZE")
|
||||||
|
.ok()
|
||||||
|
.and_then(|v| v.parse::<u32>().ok())
|
||||||
|
.unwrap_or(10);
|
||||||
|
|
||||||
|
let acquire_timeout_secs = std::env::var("SECRETS_DATABASE_ACQUIRE_TIMEOUT")
|
||||||
|
.ok()
|
||||||
|
.and_then(|v| v.parse::<u64>().ok())
|
||||||
|
.unwrap_or(5);
|
||||||
|
|
||||||
let pool = PgPoolOptions::new()
|
let pool = PgPoolOptions::new()
|
||||||
.max_connections(10)
|
.max_connections(max_connections)
|
||||||
.acquire_timeout(std::time::Duration::from_secs(5))
|
.acquire_timeout(std::time::Duration::from_secs(acquire_timeout_secs))
|
||||||
|
.max_lifetime(std::time::Duration::from_secs(1800)) // 30 minutes
|
||||||
|
.idle_timeout(std::time::Duration::from_secs(600)) // 10 minutes
|
||||||
.connect_with(connect_options)
|
.connect_with(connect_options)
|
||||||
.await?;
|
.await?;
|
||||||
tracing::debug!("database connection established");
|
|
||||||
|
tracing::debug!(
|
||||||
|
max_connections,
|
||||||
|
acquire_timeout_secs,
|
||||||
|
"database connection established"
|
||||||
|
);
|
||||||
Ok(pool)
|
Ok(pool)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,16 +102,30 @@ pub async fn migrate(pool: &PgPool) -> Result<()> {
|
|||||||
-- ── 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 (
|
||||||
@@ -141,17 +174,13 @@ pub async fn migrate(pool: &PgPool) -> Result<()> {
|
|||||||
-- ── 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,
|
||||||
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);
|
||||||
|
|
||||||
@@ -210,6 +239,16 @@ pub async fn migrate(pool: &PgPool) -> Result<()> {
|
|||||||
END IF;
|
END IF;
|
||||||
END $$;
|
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
|
DO $$ BEGIN
|
||||||
IF NOT EXISTS (
|
IF NOT EXISTS (
|
||||||
SELECT 1 FROM pg_constraint WHERE conname = 'fk_audit_log_user_id'
|
SELECT 1 FROM pg_constraint WHERE conname = 'fk_audit_log_user_id'
|
||||||
@@ -499,10 +538,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,
|
||||||
}
|
}
|
||||||
@@ -513,13 +550,11 @@ pub async fn snapshot_secret_history(
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"INSERT INTO secrets_history \
|
"INSERT INTO secrets_history \
|
||||||
(entry_id, secret_id, entry_version, field_name, encrypted, action) \
|
(secret_id, name, encrypted, action) \
|
||||||
VALUES ($1, $2, $3, $4, $5, $6)",
|
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)
|
||||||
.execute(&mut **tx)
|
.execute(&mut **tx)
|
||||||
|
|||||||
172
crates/secrets-core/src/error.rs
Normal file
172
crates/secrets-core/src/error.rs
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
use sqlx::error::DatabaseError;
|
||||||
|
|
||||||
|
/// Structured business errors for the secrets service.
|
||||||
|
///
|
||||||
|
/// These replace ad-hoc `anyhow` strings for expected failure modes,
|
||||||
|
/// allowing MCP and Web layers to map to appropriate protocol-level errors.
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum AppError {
|
||||||
|
#[error("A secret with the name '{secret_name}' already exists for this user")]
|
||||||
|
ConflictSecretName { secret_name: String },
|
||||||
|
|
||||||
|
#[error("An entry with folder='{folder}' and name='{name}' already exists")]
|
||||||
|
ConflictEntryName { folder: String, name: String },
|
||||||
|
|
||||||
|
#[error("Entry not found")]
|
||||||
|
NotFoundEntry,
|
||||||
|
|
||||||
|
#[error("User not found")]
|
||||||
|
NotFoundUser,
|
||||||
|
|
||||||
|
#[error("Secret not found")]
|
||||||
|
NotFoundSecret,
|
||||||
|
|
||||||
|
#[error("Authentication failed")]
|
||||||
|
AuthenticationFailed,
|
||||||
|
|
||||||
|
#[error("Unauthorized: insufficient permissions")]
|
||||||
|
Unauthorized,
|
||||||
|
|
||||||
|
#[error("Validation failed: {message}")]
|
||||||
|
Validation { message: String },
|
||||||
|
|
||||||
|
#[error("Concurrent modification detected")]
|
||||||
|
ConcurrentModification,
|
||||||
|
|
||||||
|
#[error("Decryption failed — the encryption key may be incorrect")]
|
||||||
|
DecryptionFailed,
|
||||||
|
|
||||||
|
#[error("Encryption key not set — user must set passphrase first")]
|
||||||
|
EncryptionKeyNotSet,
|
||||||
|
|
||||||
|
#[error(transparent)]
|
||||||
|
Internal(#[from] anyhow::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AppError {
|
||||||
|
/// Try to convert a sqlx database error into a structured `AppError`.
|
||||||
|
///
|
||||||
|
/// The caller should provide the context (which table was being written,
|
||||||
|
/// what values were being inserted) so we can produce a meaningful error.
|
||||||
|
pub fn from_db_error(err: sqlx::Error, ctx: DbErrorContext<'_>) -> Self {
|
||||||
|
if let sqlx::Error::Database(ref db_err) = err
|
||||||
|
&& db_err.code().as_deref() == Some("23505")
|
||||||
|
{
|
||||||
|
return Self::from_unique_violation(db_err.as_ref(), ctx);
|
||||||
|
}
|
||||||
|
AppError::Internal(err.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_unique_violation(db_err: &dyn DatabaseError, ctx: DbErrorContext<'_>) -> Self {
|
||||||
|
let constraint = db_err.constraint();
|
||||||
|
|
||||||
|
match constraint {
|
||||||
|
Some("idx_secrets_unique_user_name") => AppError::ConflictSecretName {
|
||||||
|
secret_name: ctx.secret_name.unwrap_or("unknown").to_string(),
|
||||||
|
},
|
||||||
|
Some("idx_entries_unique_user") | Some("idx_entries_unique_legacy") => {
|
||||||
|
AppError::ConflictEntryName {
|
||||||
|
folder: ctx.folder.unwrap_or("").to_string(),
|
||||||
|
name: ctx.name.unwrap_or("unknown").to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// Fall back to message-based detection for unnamed constraints
|
||||||
|
let msg = db_err.message();
|
||||||
|
if msg.contains("secrets") {
|
||||||
|
AppError::ConflictSecretName {
|
||||||
|
secret_name: ctx.secret_name.unwrap_or("unknown").to_string(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
AppError::ConflictEntryName {
|
||||||
|
folder: ctx.folder.unwrap_or("").to_string(),
|
||||||
|
name: ctx.name.unwrap_or("unknown").to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Context hints used when converting a database error to `AppError`.
|
||||||
|
#[derive(Debug, Default, Clone, Copy)]
|
||||||
|
pub struct DbErrorContext<'a> {
|
||||||
|
pub secret_name: Option<&'a str>,
|
||||||
|
pub folder: Option<&'a str>,
|
||||||
|
pub name: Option<&'a str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> DbErrorContext<'a> {
|
||||||
|
pub fn secret_name(name: &'a str) -> Self {
|
||||||
|
Self {
|
||||||
|
secret_name: Some(name),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn entry(folder: &'a str, name: &'a str) -> Self {
|
||||||
|
Self {
|
||||||
|
folder: Some(folder),
|
||||||
|
name: Some(name),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn app_error_display_messages() {
|
||||||
|
let err = AppError::ConflictSecretName {
|
||||||
|
secret_name: "token".to_string(),
|
||||||
|
};
|
||||||
|
assert!(err.to_string().contains("token"));
|
||||||
|
|
||||||
|
let err = AppError::ConflictEntryName {
|
||||||
|
folder: "refining".to_string(),
|
||||||
|
name: "gitea".to_string(),
|
||||||
|
};
|
||||||
|
assert!(err.to_string().contains("refining"));
|
||||||
|
assert!(err.to_string().contains("gitea"));
|
||||||
|
|
||||||
|
let err = AppError::NotFoundEntry;
|
||||||
|
assert_eq!(err.to_string(), "Entry not found");
|
||||||
|
|
||||||
|
let err = AppError::NotFoundUser;
|
||||||
|
assert_eq!(err.to_string(), "User not found");
|
||||||
|
|
||||||
|
let err = AppError::NotFoundSecret;
|
||||||
|
assert_eq!(err.to_string(), "Secret not found");
|
||||||
|
|
||||||
|
let err = AppError::AuthenticationFailed;
|
||||||
|
assert_eq!(err.to_string(), "Authentication failed");
|
||||||
|
|
||||||
|
let err = AppError::Unauthorized;
|
||||||
|
assert!(err.to_string().contains("Unauthorized"));
|
||||||
|
|
||||||
|
let err = AppError::Validation {
|
||||||
|
message: "too long".to_string(),
|
||||||
|
};
|
||||||
|
assert!(err.to_string().contains("too long"));
|
||||||
|
|
||||||
|
let err = AppError::ConcurrentModification;
|
||||||
|
assert!(err.to_string().contains("Concurrent modification"));
|
||||||
|
|
||||||
|
let err = AppError::EncryptionKeyNotSet;
|
||||||
|
assert!(err.to_string().contains("Encryption key not set"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn db_error_context_helpers() {
|
||||||
|
let ctx = DbErrorContext::secret_name("my_key");
|
||||||
|
assert_eq!(ctx.secret_name, Some("my_key"));
|
||||||
|
assert!(ctx.folder.is_none());
|
||||||
|
|
||||||
|
let ctx = DbErrorContext::entry("prod", "db-creds");
|
||||||
|
assert_eq!(ctx.folder, Some("prod"));
|
||||||
|
assert_eq!(ctx.name, Some("db-creds"));
|
||||||
|
assert!(ctx.secret_name.is_none());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,5 +2,7 @@ pub mod audit;
|
|||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
|
pub mod error;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
pub mod service;
|
pub mod service;
|
||||||
|
pub mod taxonomy;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ 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, person, …).
|
/// A top-level entry (server, service, account, 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 {
|
||||||
@@ -27,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,
|
||||||
@@ -83,7 +86,7 @@ impl From<&EntryWriteRow> for EntryRow {
|
|||||||
#[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>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
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;
|
||||||
|
|
||||||
use crate::crypto;
|
use crate::crypto;
|
||||||
use crate::db;
|
use crate::db;
|
||||||
|
use crate::error::{AppError, DbErrorContext};
|
||||||
use crate::models::EntryRow;
|
use crate::models::EntryRow;
|
||||||
|
|
||||||
// ── Key/value parsing helpers ─────────────────────────────────────────────────
|
// ── Key/value parsing helpers ─────────────────────────────────────────────────
|
||||||
@@ -176,15 +178,26 @@ pub struct AddParams<'a> {
|
|||||||
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 secret_types: &'a std::collections::HashMap<String, 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>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) -> Result<AddResult> {
|
pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) -> Result<AddResult> {
|
||||||
let metadata = build_json(params.meta_entries)?;
|
let Value::Object(metadata_map) = build_json(params.meta_entries)? else {
|
||||||
|
unreachable!("build_json always returns a JSON object");
|
||||||
|
};
|
||||||
|
let entry_type = params.entry_type.trim();
|
||||||
|
let metadata = Value::Object(metadata_map);
|
||||||
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?;
|
||||||
|
|
||||||
@@ -217,7 +230,7 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
entry_id: ex.id,
|
entry_id: ex.id,
|
||||||
user_id: params.user_id,
|
user_id: params.user_id,
|
||||||
folder: params.folder,
|
folder: params.folder,
|
||||||
entry_type: params.entry_type,
|
entry_type,
|
||||||
name: params.name,
|
name: params.name,
|
||||||
version: ex.version,
|
version: ex.version,
|
||||||
action: "add",
|
action: "add",
|
||||||
@@ -247,7 +260,7 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
)
|
)
|
||||||
.bind(uid)
|
.bind(uid)
|
||||||
.bind(params.folder)
|
.bind(params.folder)
|
||||||
.bind(params.entry_type)
|
.bind(entry_type)
|
||||||
.bind(params.name)
|
.bind(params.name)
|
||||||
.bind(params.notes)
|
.bind(params.notes)
|
||||||
.bind(params.tags)
|
.bind(params.tags)
|
||||||
@@ -270,7 +283,7 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
RETURNING id"#,
|
RETURNING id"#,
|
||||||
)
|
)
|
||||||
.bind(params.folder)
|
.bind(params.folder)
|
||||||
.bind(params.entry_type)
|
.bind(entry_type)
|
||||||
.bind(params.name)
|
.bind(params.name)
|
||||||
.bind(params.notes)
|
.bind(params.notes)
|
||||||
.bind(params.tags)
|
.bind(params.tags)
|
||||||
@@ -279,10 +292,11 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
.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(
|
||||||
@@ -291,9 +305,9 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
entry_id,
|
entry_id,
|
||||||
user_id: params.user_id,
|
user_id: params.user_id,
|
||||||
folder: params.folder,
|
folder: params.folder,
|
||||||
entry_type: params.entry_type,
|
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,
|
||||||
@@ -308,23 +322,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",
|
||||||
},
|
},
|
||||||
@@ -335,21 +351,80 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlx::query("DELETE FROM secrets WHERE entry_id = $1")
|
let orphan_candidates: Vec<Uuid> = existing_fields.iter().map(|f| f.id).collect();
|
||||||
|
|
||||||
|
sqlx::query("DELETE FROM entry_secrets WHERE entry_id = $1")
|
||||||
.bind(entry_id)
|
.bind(entry_id)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
if !orphan_candidates.is_empty() {
|
||||||
|
sqlx::query(
|
||||||
|
"DELETE FROM secrets s \
|
||||||
|
WHERE s.id = ANY($1) \
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM entry_secrets es WHERE es.secret_id = s.id)",
|
||||||
|
)
|
||||||
|
.bind(&orphan_candidates)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (field_name, field_value) in &flat_fields {
|
||||||
|
let encrypted = crypto::encrypt_json(master_key, field_value)?;
|
||||||
|
let secret_type = params
|
||||||
|
.secret_types
|
||||||
|
.get(field_name)
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
.unwrap_or("text");
|
||||||
|
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(secret_type)
|
||||||
|
.bind(&encrypted)
|
||||||
|
.fetch_one(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::from_db_error(e, DbErrorContext::secret_name(field_name)))?;
|
||||||
|
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(
|
||||||
@@ -357,7 +432,7 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
params.user_id,
|
params.user_id,
|
||||||
"add",
|
"add",
|
||||||
params.folder,
|
params.folder,
|
||||||
params.entry_type,
|
entry_type,
|
||||||
params.name,
|
params.name,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"tags": params.tags,
|
"tags": params.tags,
|
||||||
@@ -372,16 +447,44 @@ pub async fn run(pool: &PgPool, params: AddParams<'_>, master_key: &[u8; 32]) ->
|
|||||||
Ok(AddResult {
|
Ok(AddResult {
|
||||||
name: params.name.to_string(),
|
name: params.name.to_string(),
|
||||||
folder: params.folder.to_string(),
|
folder: params.folder.to_string(),
|
||||||
entry_type: params.entry_type.to_string(),
|
entry_type: entry_type.to_string(),
|
||||||
tags: params.tags.to_vec(),
|
tags: params.tags.to_vec(),
|
||||||
meta_keys,
|
meta_keys,
|
||||||
secret_keys,
|
secret_keys,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
||||||
@@ -410,4 +513,267 @@ 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: &[],
|
||||||
|
secret_types: &Default::default(),
|
||||||
|
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: &[],
|
||||||
|
secret_types: &Default::default(),
|
||||||
|
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: &[],
|
||||||
|
secret_types: &Default::default(),
|
||||||
|
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(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn add_duplicate_secret_name_returns_conflict_error() -> Result<()> {
|
||||||
|
let Some(pool) = maybe_test_pool().await else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let suffix = Uuid::from_u128(rand::random()).to_string();
|
||||||
|
let marker = format!("dup_secret_{}", &suffix[..8]);
|
||||||
|
let entry_name = format!("{}_entry", marker);
|
||||||
|
let secret_name = "shared_token";
|
||||||
|
|
||||||
|
cleanup_test_rows(&pool, &marker).await?;
|
||||||
|
|
||||||
|
// First add succeeds
|
||||||
|
run(
|
||||||
|
&pool,
|
||||||
|
AddParams {
|
||||||
|
name: &entry_name,
|
||||||
|
folder: &marker,
|
||||||
|
entry_type: "service",
|
||||||
|
notes: "",
|
||||||
|
tags: &[],
|
||||||
|
meta_entries: &[],
|
||||||
|
secret_entries: &[format!("{}=value1", secret_name)],
|
||||||
|
secret_types: &Default::default(),
|
||||||
|
link_secret_names: &[],
|
||||||
|
user_id: None,
|
||||||
|
},
|
||||||
|
&[0_u8; 32],
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// Second add with same secret name under same user_id should fail with ConflictSecretName
|
||||||
|
let entry_name2 = format!("{}_entry2", marker);
|
||||||
|
let err = run(
|
||||||
|
&pool,
|
||||||
|
AddParams {
|
||||||
|
name: &entry_name2,
|
||||||
|
folder: &marker,
|
||||||
|
entry_type: "service",
|
||||||
|
notes: "",
|
||||||
|
tags: &[],
|
||||||
|
meta_entries: &[],
|
||||||
|
secret_entries: &[format!("{}=value2", secret_name)],
|
||||||
|
secret_types: &Default::default(),
|
||||||
|
link_secret_names: &[],
|
||||||
|
user_id: None,
|
||||||
|
},
|
||||||
|
&[0_u8; 32],
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect_err("must fail on duplicate secret name");
|
||||||
|
|
||||||
|
let app_err = err
|
||||||
|
.downcast_ref::<crate::error::AppError>()
|
||||||
|
.expect("error should be AppError");
|
||||||
|
assert!(
|
||||||
|
matches!(app_err, crate::error::AppError::ConflictSecretName { .. }),
|
||||||
|
"expected ConflictSecretName, got: {}",
|
||||||
|
app_err
|
||||||
|
);
|
||||||
|
|
||||||
|
cleanup_test_rows(&pool, &marker).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ use anyhow::Result;
|
|||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::error::AppError;
|
||||||
|
|
||||||
const KEY_PREFIX: &str = "sk_";
|
const KEY_PREFIX: &str = "sk_";
|
||||||
|
|
||||||
/// Generate a new API key: `sk_<64 hex chars>` = 67 characters total.
|
/// Generate a new API key: `sk_<64 hex chars>` = 67 characters total.
|
||||||
@@ -14,23 +16,32 @@ pub fn generate_api_key() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Return the user's existing API key, or generate and store a new one if NULL.
|
/// Return the user's existing API key, or generate and store a new one if NULL.
|
||||||
|
/// Uses a transaction with atomic update to prevent TOCTOU race conditions.
|
||||||
pub async fn ensure_api_key(pool: &PgPool, user_id: Uuid) -> Result<String> {
|
pub async fn ensure_api_key(pool: &PgPool, user_id: Uuid) -> Result<String> {
|
||||||
let existing: Option<(Option<String>,)> =
|
let mut tx = pool.begin().await?;
|
||||||
sqlx::query_as("SELECT api_key FROM users WHERE id = $1")
|
|
||||||
.bind(user_id)
|
|
||||||
.fetch_optional(pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if let Some((Some(key),)) = existing {
|
// Lock the row and check existing key
|
||||||
|
let existing: (Option<String>,) =
|
||||||
|
sqlx::query_as("SELECT api_key FROM users WHERE id = $1 FOR UPDATE")
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await?
|
||||||
|
.ok_or(AppError::NotFoundUser)?;
|
||||||
|
|
||||||
|
if let Some(key) = existing.0 {
|
||||||
|
tx.commit().await?;
|
||||||
return Ok(key);
|
return Ok(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate and store new key atomically
|
||||||
let new_key = generate_api_key();
|
let new_key = generate_api_key();
|
||||||
sqlx::query("UPDATE users SET api_key = $1 WHERE id = $2")
|
sqlx::query("UPDATE users SET api_key = $1 WHERE id = $2")
|
||||||
.bind(&new_key)
|
.bind(&new_key)
|
||||||
.bind(user_id)
|
.bind(user_id)
|
||||||
.execute(pool)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
Ok(new_key)
|
Ok(new_key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ pub struct DeletedEntry {
|
|||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
pub struct DeleteResult {
|
pub struct DeleteResult {
|
||||||
pub deleted: Vec<DeletedEntry>,
|
pub deleted: Vec<DeletedEntry>,
|
||||||
pub migrated: Vec<String>,
|
|
||||||
pub dry_run: bool,
|
pub dry_run: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,175 +31,7 @@ pub struct DeleteParams<'a> {
|
|||||||
pub user_id: Option<Uuid>,
|
pub user_id: Option<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, sqlx::FromRow)]
|
/// Delete a single entry by id (multi-tenant: `user_id` must match).
|
||||||
struct KeyReferrer {
|
|
||||||
id: Uuid,
|
|
||||||
folder: String,
|
|
||||||
#[sqlx(rename = "type")]
|
|
||||||
entry_type: String,
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ref_label(r: &KeyReferrer) -> String {
|
|
||||||
format!("{}/{} ({})", r.folder, r.name, r.entry_type)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ref_path(r: &KeyReferrer) -> String {
|
|
||||||
format!("{}/{}", r.folder, r.name)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn fetch_key_referrers_pool(
|
|
||||||
pool: &PgPool,
|
|
||||||
key_entry_id: Uuid,
|
|
||||||
key_folder: &str,
|
|
||||||
key_name: &str,
|
|
||||||
user_id: Option<Uuid>,
|
|
||||||
) -> Result<Vec<KeyReferrer>> {
|
|
||||||
let qualified = format!("{}/{}", key_folder, key_name);
|
|
||||||
let refs: Vec<KeyReferrer> = if let Some(uid) = user_id {
|
|
||||||
sqlx::query_as(
|
|
||||||
"SELECT id, folder, type, name FROM entries \
|
|
||||||
WHERE user_id = $1 AND id <> $2 \
|
|
||||||
AND (metadata->>'key_ref' = $3 OR metadata->>'key_ref' = $4) \
|
|
||||||
ORDER BY folder, type, name",
|
|
||||||
)
|
|
||||||
.bind(uid)
|
|
||||||
.bind(key_entry_id)
|
|
||||||
.bind(key_name)
|
|
||||||
.bind(&qualified)
|
|
||||||
.fetch_all(pool)
|
|
||||||
.await?
|
|
||||||
} else {
|
|
||||||
sqlx::query_as(
|
|
||||||
"SELECT id, folder, type, name FROM entries \
|
|
||||||
WHERE user_id IS NULL AND id <> $1 \
|
|
||||||
AND (metadata->>'key_ref' = $2 OR metadata->>'key_ref' = $3) \
|
|
||||||
ORDER BY folder, type, name",
|
|
||||||
)
|
|
||||||
.bind(key_entry_id)
|
|
||||||
.bind(key_name)
|
|
||||||
.bind(&qualified)
|
|
||||||
.fetch_all(pool)
|
|
||||||
.await?
|
|
||||||
};
|
|
||||||
Ok(refs)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn migrate_key_refs_if_needed(
|
|
||||||
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
|
||||||
key_row: &EntryRow,
|
|
||||||
key_name: &str,
|
|
||||||
user_id: Option<Uuid>,
|
|
||||||
dry_run: bool,
|
|
||||||
) -> Result<Vec<String>> {
|
|
||||||
let qualified = format!("{}/{}", key_row.folder, key_name);
|
|
||||||
let refs: Vec<KeyReferrer> = if let Some(uid) = user_id {
|
|
||||||
sqlx::query_as(
|
|
||||||
"SELECT id, folder, type, name FROM entries \
|
|
||||||
WHERE user_id = $1 AND id <> $2 \
|
|
||||||
AND (metadata->>'key_ref' = $3 OR metadata->>'key_ref' = $4) \
|
|
||||||
ORDER BY folder, type, name",
|
|
||||||
)
|
|
||||||
.bind(uid)
|
|
||||||
.bind(key_row.id)
|
|
||||||
.bind(key_name)
|
|
||||||
.bind(&qualified)
|
|
||||||
.fetch_all(&mut **tx)
|
|
||||||
.await?
|
|
||||||
} else {
|
|
||||||
sqlx::query_as(
|
|
||||||
"SELECT id, folder, type, name FROM entries \
|
|
||||||
WHERE user_id IS NULL AND id <> $1 \
|
|
||||||
AND (metadata->>'key_ref' = $2 OR metadata->>'key_ref' = $3) \
|
|
||||||
ORDER BY folder, type, name",
|
|
||||||
)
|
|
||||||
.bind(key_row.id)
|
|
||||||
.bind(key_name)
|
|
||||||
.bind(&qualified)
|
|
||||||
.fetch_all(&mut **tx)
|
|
||||||
.await?
|
|
||||||
};
|
|
||||||
|
|
||||||
if refs.is_empty() {
|
|
||||||
return Ok(vec![]);
|
|
||||||
}
|
|
||||||
if dry_run {
|
|
||||||
return Ok(refs.iter().map(ref_label).collect());
|
|
||||||
}
|
|
||||||
|
|
||||||
let owner = &refs[0];
|
|
||||||
let owner_path = ref_path(owner);
|
|
||||||
let key_fields: Vec<SecretFieldRow> =
|
|
||||||
sqlx::query_as("SELECT id, field_name, encrypted FROM secrets WHERE entry_id = $1")
|
|
||||||
.bind(key_row.id)
|
|
||||||
.fetch_all(&mut **tx)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
for f in &key_fields {
|
|
||||||
sqlx::query(
|
|
||||||
"INSERT INTO secrets (entry_id, field_name, encrypted) VALUES ($1, $2, $3) \
|
|
||||||
ON CONFLICT (entry_id, field_name) DO NOTHING",
|
|
||||||
)
|
|
||||||
.bind(owner.id)
|
|
||||||
.bind(&f.field_name)
|
|
||||||
.bind(&f.encrypted)
|
|
||||||
.execute(&mut **tx)
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlx::query(
|
|
||||||
"UPDATE entries SET metadata = metadata - 'key_ref', \
|
|
||||||
version = version + 1, updated_at = NOW() WHERE id = $1",
|
|
||||||
)
|
|
||||||
.bind(owner.id)
|
|
||||||
.execute(&mut **tx)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
crate::audit::log_tx(
|
|
||||||
tx,
|
|
||||||
user_id,
|
|
||||||
"key_migrate",
|
|
||||||
&owner.folder,
|
|
||||||
&owner.entry_type,
|
|
||||||
&owner.name,
|
|
||||||
json!({
|
|
||||||
"from_key": format!("{}/{}", key_row.folder, key_name),
|
|
||||||
"role": "new_owner",
|
|
||||||
"redirect_target": owner_path,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
for r in refs.iter().skip(1) {
|
|
||||||
sqlx::query(
|
|
||||||
"UPDATE entries SET metadata = jsonb_set(metadata, '{key_ref}', to_jsonb($2::text), true), \
|
|
||||||
version = version + 1, updated_at = NOW() WHERE id = $1",
|
|
||||||
)
|
|
||||||
.bind(r.id)
|
|
||||||
.bind(&owner_path)
|
|
||||||
.execute(&mut **tx)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
crate::audit::log_tx(
|
|
||||||
tx,
|
|
||||||
user_id,
|
|
||||||
"key_migrate",
|
|
||||||
&r.folder,
|
|
||||||
&r.entry_type,
|
|
||||||
&r.name,
|
|
||||||
json!({
|
|
||||||
"from_key": format!("{}/{}", key_row.folder, key_name),
|
|
||||||
"role": "redirected_ref",
|
|
||||||
"redirect_to": owner_path,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(refs.iter().map(ref_label).collect())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Delete a single entry by id (multi-tenant: `user_id` must match). Cascades `secrets` via FK.
|
|
||||||
pub async fn delete_by_id(pool: &PgPool, entry_id: Uuid, user_id: Uuid) -> Result<DeleteResult> {
|
pub async fn delete_by_id(pool: &PgPool, entry_id: Uuid, user_id: Uuid) -> Result<DeleteResult> {
|
||||||
let mut tx = pool.begin().await?;
|
let mut tx = pool.begin().await?;
|
||||||
let row: Option<EntryWriteRow> = sqlx::query_as(
|
let row: Option<EntryWriteRow> = sqlx::query_as(
|
||||||
@@ -224,8 +55,6 @@ pub async fn delete_by_id(pool: &PgPool, entry_id: Uuid, user_id: Uuid) -> Resul
|
|||||||
let entry_type = row.entry_type.clone();
|
let entry_type = row.entry_type.clone();
|
||||||
let name = row.name.clone();
|
let name = row.name.clone();
|
||||||
let entry_row: EntryRow = (&row).into();
|
let entry_row: EntryRow = (&row).into();
|
||||||
let migrated =
|
|
||||||
migrate_key_refs_if_needed(&mut tx, &entry_row, &name, Some(user_id), false).await?;
|
|
||||||
|
|
||||||
snapshot_and_delete(
|
snapshot_and_delete(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
@@ -254,7 +83,6 @@ pub async fn delete_by_id(pool: &PgPool, entry_id: Uuid, user_id: Uuid) -> Resul
|
|||||||
folder,
|
folder,
|
||||||
entry_type,
|
entry_type,
|
||||||
}],
|
}],
|
||||||
migrated,
|
|
||||||
dry_run: false,
|
dry_run: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -294,6 +122,7 @@ async fn delete_one(
|
|||||||
// - 2+ matches → disambiguation error (same as non-dry-run)
|
// - 2+ matches → disambiguation error (same as non-dry-run)
|
||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
struct DryRunRow {
|
struct DryRunRow {
|
||||||
|
#[allow(dead_code)]
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
folder: String,
|
folder: String,
|
||||||
#[sqlx(rename = "type")]
|
#[sqlx(rename = "type")]
|
||||||
@@ -339,20 +168,16 @@ async fn delete_one(
|
|||||||
return match rows.len() {
|
return match rows.len() {
|
||||||
0 => Ok(DeleteResult {
|
0 => Ok(DeleteResult {
|
||||||
deleted: vec![],
|
deleted: vec![],
|
||||||
migrated: vec![],
|
|
||||||
dry_run: true,
|
dry_run: true,
|
||||||
}),
|
}),
|
||||||
1 => {
|
1 => {
|
||||||
let row = rows.into_iter().next().unwrap();
|
let row = rows.into_iter().next().unwrap();
|
||||||
let refs =
|
|
||||||
fetch_key_referrers_pool(pool, row.id, &row.folder, name, user_id).await?;
|
|
||||||
Ok(DeleteResult {
|
Ok(DeleteResult {
|
||||||
deleted: vec![DeletedEntry {
|
deleted: vec![DeletedEntry {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
folder: row.folder,
|
folder: row.folder,
|
||||||
entry_type: row.entry_type,
|
entry_type: row.entry_type,
|
||||||
}],
|
}],
|
||||||
migrated: refs.iter().map(ref_label).collect(),
|
|
||||||
dry_run: true,
|
dry_run: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -417,7 +242,6 @@ async fn delete_one(
|
|||||||
tx.rollback().await?;
|
tx.rollback().await?;
|
||||||
return Ok(DeleteResult {
|
return Ok(DeleteResult {
|
||||||
deleted: vec![],
|
deleted: vec![],
|
||||||
migrated: vec![],
|
|
||||||
dry_run: false,
|
dry_run: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -437,7 +261,6 @@ async fn delete_one(
|
|||||||
|
|
||||||
let folder = row.folder.clone();
|
let folder = row.folder.clone();
|
||||||
let entry_type = row.entry_type.clone();
|
let entry_type = row.entry_type.clone();
|
||||||
let migrated = migrate_key_refs_if_needed(&mut tx, &row, name, user_id, false).await?;
|
|
||||||
snapshot_and_delete(&mut tx, &folder, &entry_type, name, &row, user_id).await?;
|
snapshot_and_delete(&mut tx, &folder, &entry_type, name, &row, user_id).await?;
|
||||||
crate::audit::log_tx(
|
crate::audit::log_tx(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
@@ -457,7 +280,6 @@ async fn delete_one(
|
|||||||
folder,
|
folder,
|
||||||
entry_type,
|
entry_type,
|
||||||
}],
|
}],
|
||||||
migrated,
|
|
||||||
dry_run: false,
|
dry_run: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -497,33 +319,29 @@ async fn delete_bulk(
|
|||||||
}
|
}
|
||||||
if entry_type.is_some() {
|
if entry_type.is_some() {
|
||||||
conditions.push(format!("type = ${}", idx));
|
conditions.push(format!("type = ${}", idx));
|
||||||
|
idx += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let where_clause = format!("WHERE {}", conditions.join(" AND "));
|
let where_clause = format!("WHERE {}", conditions.join(" AND "));
|
||||||
let sql = format!(
|
let _ = idx; // used only for placeholder numbering in conditions
|
||||||
"SELECT id, version, folder, type, name, metadata, tags, notes \
|
|
||||||
FROM entries {where_clause} ORDER BY type, name"
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut q = sqlx::query_as::<_, FullEntryRow>(&sql);
|
|
||||||
if let Some(uid) = user_id {
|
|
||||||
q = q.bind(uid);
|
|
||||||
}
|
|
||||||
if let Some(f) = folder {
|
|
||||||
q = q.bind(f);
|
|
||||||
}
|
|
||||||
if let Some(t) = entry_type {
|
|
||||||
q = q.bind(t);
|
|
||||||
}
|
|
||||||
let rows = q.fetch_all(pool).await?;
|
|
||||||
|
|
||||||
if dry_run {
|
if dry_run {
|
||||||
let mut migrated: Vec<String> = Vec::new();
|
let sql = format!(
|
||||||
for row in &rows {
|
"SELECT id, version, folder, type, name, metadata, tags, notes \
|
||||||
let refs =
|
FROM entries {where_clause} ORDER BY type, name"
|
||||||
fetch_key_referrers_pool(pool, row.id, &row.folder, &row.name, user_id).await?;
|
);
|
||||||
migrated.extend(refs.iter().map(ref_label));
|
let mut q = sqlx::query_as::<_, FullEntryRow>(&sql);
|
||||||
|
if let Some(uid) = user_id {
|
||||||
|
q = q.bind(uid);
|
||||||
}
|
}
|
||||||
|
if let Some(f) = folder {
|
||||||
|
q = q.bind(f);
|
||||||
|
}
|
||||||
|
if let Some(t) = entry_type {
|
||||||
|
q = q.bind(t);
|
||||||
|
}
|
||||||
|
let rows = q.fetch_all(pool).await?;
|
||||||
|
|
||||||
let deleted = rows
|
let deleted = rows
|
||||||
.iter()
|
.iter()
|
||||||
.map(|r| DeletedEntry {
|
.map(|r| DeletedEntry {
|
||||||
@@ -534,15 +352,31 @@ async fn delete_bulk(
|
|||||||
.collect();
|
.collect();
|
||||||
return Ok(DeleteResult {
|
return Ok(DeleteResult {
|
||||||
deleted,
|
deleted,
|
||||||
migrated,
|
|
||||||
dry_run: true,
|
dry_run: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
|
||||||
|
let sql = format!(
|
||||||
|
"SELECT id, version, folder, type, name, metadata, tags, notes \
|
||||||
|
FROM entries {where_clause} ORDER BY type, name FOR UPDATE"
|
||||||
|
);
|
||||||
|
let mut q = sqlx::query_as::<_, FullEntryRow>(&sql);
|
||||||
|
if let Some(uid) = user_id {
|
||||||
|
q = q.bind(uid);
|
||||||
|
}
|
||||||
|
if let Some(f) = folder {
|
||||||
|
q = q.bind(f);
|
||||||
|
}
|
||||||
|
if let Some(t) = entry_type {
|
||||||
|
q = q.bind(t);
|
||||||
|
}
|
||||||
|
let rows = q.fetch_all(&mut *tx).await?;
|
||||||
|
|
||||||
let mut deleted = Vec::with_capacity(rows.len());
|
let mut deleted = Vec::with_capacity(rows.len());
|
||||||
let mut migrated: Vec<String> = Vec::new();
|
|
||||||
for row in &rows {
|
for row in &rows {
|
||||||
let entry_row = EntryRow {
|
let entry_row: EntryRow = EntryRow {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
version: row.version,
|
version: row.version,
|
||||||
folder: row.folder.clone(),
|
folder: row.folder.clone(),
|
||||||
@@ -551,9 +385,6 @@ async fn delete_bulk(
|
|||||||
metadata: row.metadata.clone(),
|
metadata: row.metadata.clone(),
|
||||||
notes: row.notes.clone(),
|
notes: row.notes.clone(),
|
||||||
};
|
};
|
||||||
let mut tx = pool.begin().await?;
|
|
||||||
let m = migrate_key_refs_if_needed(&mut tx, &entry_row, &row.name, user_id, false).await?;
|
|
||||||
migrated.extend(m);
|
|
||||||
snapshot_and_delete(
|
snapshot_and_delete(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
&row.folder,
|
&row.folder,
|
||||||
@@ -573,7 +404,6 @@ async fn delete_bulk(
|
|||||||
json!({"bulk": true}),
|
json!({"bulk": true}),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
tx.commit().await?;
|
|
||||||
deleted.push(DeletedEntry {
|
deleted.push(DeletedEntry {
|
||||||
name: row.name.clone(),
|
name: row.name.clone(),
|
||||||
folder: row.folder.clone(),
|
folder: row.folder.clone(),
|
||||||
@@ -581,9 +411,10 @@ async fn delete_bulk(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
|
|
||||||
Ok(DeleteResult {
|
Ok(DeleteResult {
|
||||||
deleted,
|
deleted,
|
||||||
migrated,
|
|
||||||
dry_run: false,
|
dry_run: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -615,20 +446,22 @@ async fn snapshot_and_delete(
|
|||||||
tracing::warn!(error = %e, "failed to snapshot entry history before delete");
|
tracing::warn!(error = %e, "failed to snapshot entry history before delete");
|
||||||
}
|
}
|
||||||
|
|
||||||
let fields: Vec<SecretFieldRow> =
|
let fields: Vec<SecretFieldRow> = 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(row.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(row.id)
|
||||||
|
.fetch_all(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
for f in &fields {
|
for f in &fields {
|
||||||
if let Err(e) = db::snapshot_secret_history(
|
if let Err(e) = db::snapshot_secret_history(
|
||||||
tx,
|
tx,
|
||||||
db::SecretSnapshotParams {
|
db::SecretSnapshotParams {
|
||||||
entry_id: row.id,
|
|
||||||
secret_id: f.id,
|
secret_id: f.id,
|
||||||
entry_version: row.version,
|
name: &f.name,
|
||||||
field_name: &f.field_name,
|
|
||||||
encrypted: &f.encrypted,
|
encrypted: &f.encrypted,
|
||||||
action: "delete",
|
action: "delete",
|
||||||
},
|
},
|
||||||
@@ -644,266 +477,171 @@ async fn snapshot_and_delete(
|
|||||||
.execute(&mut **tx)
|
.execute(&mut **tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
let secret_ids: Vec<Uuid> = fields.iter().map(|f| f.id).collect();
|
||||||
|
if !secret_ids.is_empty() {
|
||||||
|
sqlx::query(
|
||||||
|
"DELETE FROM secrets s \
|
||||||
|
WHERE s.id = ANY($1) \
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM entry_secrets es WHERE es.secret_id = s.id)",
|
||||||
|
)
|
||||||
|
.bind(&secret_ids)
|
||||||
|
.execute(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use serde_json::json;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
async fn maybe_test_pool() -> Option<PgPool> {
|
async fn maybe_test_pool() -> Option<PgPool> {
|
||||||
let Ok(url) = std::env::var("SECRETS_DATABASE_URL") else {
|
let Ok(url) = std::env::var("SECRETS_DATABASE_URL") else {
|
||||||
eprintln!("skip delete migration tests: SECRETS_DATABASE_URL is not set");
|
eprintln!("skip delete tests: SECRETS_DATABASE_URL is not set");
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
let Ok(pool) = PgPool::connect(&url).await else {
|
let Ok(pool) = PgPool::connect(&url).await else {
|
||||||
eprintln!("skip delete migration tests: cannot connect to database");
|
eprintln!("skip delete tests: cannot connect to database");
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
if let Err(e) = crate::db::migrate(&pool).await {
|
if let Err(e) = crate::db::migrate(&pool).await {
|
||||||
eprintln!("skip delete migration tests: migrate failed: {e}");
|
eprintln!("skip delete tests: migrate failed: {e}");
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Some(pool)
|
Some(pool)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn insert_entry(
|
async fn cleanup_single_user_rows(pool: &PgPool, marker: &str) -> Result<()> {
|
||||||
pool: &PgPool,
|
|
||||||
id: Uuid,
|
|
||||||
user_id: Uuid,
|
|
||||||
folder: &str,
|
|
||||||
entry_type: &str,
|
|
||||||
name: &str,
|
|
||||||
metadata: serde_json::Value,
|
|
||||||
) -> Result<()> {
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"INSERT INTO entries (id, user_id, folder, type, name, notes, tags, metadata, version) \
|
"DELETE FROM entries WHERE user_id IS NULL AND (name LIKE $1 OR folder LIKE $1)",
|
||||||
VALUES ($1, $2, $3, $4, $5, '', ARRAY[]::text[], $6, 1)",
|
|
||||||
)
|
)
|
||||||
.bind(id)
|
.bind(format!("%{marker}%"))
|
||||||
.bind(user_id)
|
.execute(pool)
|
||||||
.bind(folder)
|
.await?;
|
||||||
.bind(entry_type)
|
sqlx::query(
|
||||||
.bind(name)
|
"DELETE FROM secrets WHERE user_id IS NULL AND name LIKE $1 \
|
||||||
.bind(metadata)
|
AND NOT EXISTS (SELECT 1 FROM entry_secrets es WHERE es.secret_id = secrets.id)",
|
||||||
|
)
|
||||||
|
.bind(format!("%{marker}%"))
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn delete_shared_key_dry_run_reports_migration_without_writes() -> Result<()> {
|
async fn delete_dry_run_reports_matching_entry_without_writes() -> Result<()> {
|
||||||
let Some(pool) = maybe_test_pool().await else {
|
let Some(pool) = maybe_test_pool().await else {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
let suffix = Uuid::from_u128(rand::random()).to_string();
|
||||||
|
let marker = format!("delete_dry_{}", &suffix[..8]);
|
||||||
|
let entry_name = format!("{}_entry", marker);
|
||||||
|
|
||||||
let user_id = Uuid::from_u128(rand::random());
|
cleanup_single_user_rows(&pool, &marker).await?;
|
||||||
let key_id = Uuid::from_u128(rand::random());
|
|
||||||
let ref_a = Uuid::from_u128(rand::random());
|
|
||||||
let ref_b = Uuid::from_u128(rand::random());
|
|
||||||
|
|
||||||
insert_entry(
|
sqlx::query(
|
||||||
&pool,
|
"INSERT INTO entries (user_id, folder, type, name, notes, tags, metadata) \
|
||||||
key_id,
|
VALUES (NULL, $1, 'service', $2, '', '{}', '{}')",
|
||||||
user_id,
|
|
||||||
"kfolder",
|
|
||||||
"key",
|
|
||||||
"shared-key",
|
|
||||||
json!({}),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
sqlx::query("INSERT INTO secrets (entry_id, field_name, encrypted) VALUES ($1, $2, $3)")
|
|
||||||
.bind(key_id)
|
|
||||||
.bind("pem")
|
|
||||||
.bind(vec![1_u8, 2, 3])
|
|
||||||
.execute(&pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
insert_entry(
|
|
||||||
&pool,
|
|
||||||
ref_a,
|
|
||||||
user_id,
|
|
||||||
"afolder",
|
|
||||||
"server",
|
|
||||||
"srv-a",
|
|
||||||
json!({"key_ref":"kfolder/shared-key"}),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
insert_entry(
|
|
||||||
&pool,
|
|
||||||
ref_b,
|
|
||||||
user_id,
|
|
||||||
"bfolder",
|
|
||||||
"server",
|
|
||||||
"srv-b",
|
|
||||||
json!({"key_ref":"shared-key"}),
|
|
||||||
)
|
)
|
||||||
|
.bind(&marker)
|
||||||
|
.bind(&entry_name)
|
||||||
|
.execute(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let result = run(
|
let result = run(
|
||||||
&pool,
|
&pool,
|
||||||
DeleteParams {
|
DeleteParams {
|
||||||
name: Some("shared-key"),
|
name: Some(&entry_name),
|
||||||
folder: Some("kfolder"),
|
folder: Some(&marker),
|
||||||
entry_type: None,
|
entry_type: None,
|
||||||
dry_run: true,
|
dry_run: true,
|
||||||
user_id: Some(user_id),
|
user_id: None,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
assert!(result.dry_run);
|
assert!(result.dry_run);
|
||||||
assert_eq!(result.deleted.len(), 1);
|
assert_eq!(result.deleted.len(), 1);
|
||||||
assert_eq!(result.migrated.len(), 2);
|
assert_eq!(result.deleted[0].name, entry_name);
|
||||||
|
|
||||||
let key_exists: bool = sqlx::query_scalar(
|
let still_exists: bool = sqlx::query_scalar(
|
||||||
"SELECT EXISTS(SELECT 1 FROM entries WHERE id = $1 AND user_id = $2)",
|
"SELECT EXISTS(SELECT 1 FROM entries WHERE user_id IS NULL AND folder = $1 AND name = $2)",
|
||||||
)
|
)
|
||||||
.bind(key_id)
|
.bind(&marker)
|
||||||
.bind(user_id)
|
.bind(&entry_name)
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
assert!(key_exists);
|
assert!(still_exists);
|
||||||
|
|
||||||
let ref_a_key_ref: Option<String> =
|
cleanup_single_user_rows(&pool, &marker).await?;
|
||||||
sqlx::query_scalar("SELECT metadata->>'key_ref' FROM entries WHERE id = $1")
|
|
||||||
.bind(ref_a)
|
|
||||||
.fetch_one(&pool)
|
|
||||||
.await?;
|
|
||||||
let ref_b_key_ref: Option<String> =
|
|
||||||
sqlx::query_scalar("SELECT metadata->>'key_ref' FROM entries WHERE id = $1")
|
|
||||||
.bind(ref_b)
|
|
||||||
.fetch_one(&pool)
|
|
||||||
.await?;
|
|
||||||
assert_eq!(ref_a_key_ref.as_deref(), Some("kfolder/shared-key"));
|
|
||||||
assert_eq!(ref_b_key_ref.as_deref(), Some("shared-key"));
|
|
||||||
|
|
||||||
sqlx::query("DELETE FROM entries WHERE user_id = $1")
|
|
||||||
.bind(user_id)
|
|
||||||
.execute(&pool)
|
|
||||||
.await?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn delete_shared_key_auto_migrates_single_copy_and_redirects_refs() -> Result<()> {
|
async fn delete_by_id_removes_entry_and_orphan_secret() -> Result<()> {
|
||||||
let Some(pool) = maybe_test_pool().await else {
|
let Some(pool) = maybe_test_pool().await else {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
let suffix = Uuid::from_u128(rand::random()).to_string();
|
||||||
|
let marker = format!("delete_id_{}", &suffix[..8]);
|
||||||
let user_id = Uuid::from_u128(rand::random());
|
let user_id = Uuid::from_u128(rand::random());
|
||||||
let key_id = Uuid::from_u128(rand::random());
|
let entry_name = format!("{}_entry", marker);
|
||||||
let ref_a = Uuid::from_u128(rand::random());
|
let secret_name = format!("{}_secret", marker);
|
||||||
let ref_b = Uuid::from_u128(rand::random());
|
|
||||||
let ref_c = Uuid::from_u128(rand::random());
|
|
||||||
|
|
||||||
insert_entry(
|
sqlx::query("DELETE FROM entries WHERE user_id = $1 AND folder = $2")
|
||||||
&pool,
|
.bind(user_id)
|
||||||
key_id,
|
.bind(&marker)
|
||||||
user_id,
|
.execute(&pool)
|
||||||
"kfolder",
|
.await?;
|
||||||
"key",
|
sqlx::query("DELETE FROM secrets WHERE user_id = $1 AND name = $2")
|
||||||
"shared-key",
|
.bind(user_id)
|
||||||
json!({}),
|
.bind(&secret_name)
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
sqlx::query("INSERT INTO secrets (entry_id, field_name, encrypted) VALUES ($1, $2, $3)")
|
|
||||||
.bind(key_id)
|
|
||||||
.bind("pem")
|
|
||||||
.bind(vec![7_u8, 8, 9])
|
|
||||||
.execute(&pool)
|
.execute(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// owner candidate (sorted first by folder)
|
let entry_id: Uuid = sqlx::query_scalar(
|
||||||
insert_entry(
|
"INSERT INTO entries (user_id, folder, type, name, notes, tags, metadata) \
|
||||||
&pool,
|
VALUES ($1, $2, 'service', $3, '', '{}', '{}') RETURNING id",
|
||||||
ref_a,
|
|
||||||
user_id,
|
|
||||||
"afolder",
|
|
||||||
"server",
|
|
||||||
"srv-a",
|
|
||||||
json!({"key_ref":"kfolder/shared-key"}),
|
|
||||||
)
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(&marker)
|
||||||
|
.bind(&entry_name)
|
||||||
|
.fetch_one(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
insert_entry(
|
let secret_id: Uuid = sqlx::query_scalar(
|
||||||
&pool,
|
"INSERT INTO secrets (user_id, name, type, encrypted) VALUES ($1, $2, 'text', $3) RETURNING id",
|
||||||
ref_b,
|
|
||||||
user_id,
|
|
||||||
"bfolder",
|
|
||||||
"server",
|
|
||||||
"srv-b",
|
|
||||||
json!({"key_ref":"shared-key"}),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
insert_entry(
|
|
||||||
&pool,
|
|
||||||
ref_c,
|
|
||||||
user_id,
|
|
||||||
"cfolder",
|
|
||||||
"service",
|
|
||||||
"svc-c",
|
|
||||||
json!({"key_ref":"kfolder/shared-key"}),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let result = run(
|
|
||||||
&pool,
|
|
||||||
DeleteParams {
|
|
||||||
name: Some("shared-key"),
|
|
||||||
folder: Some("kfolder"),
|
|
||||||
entry_type: None,
|
|
||||||
dry_run: false,
|
|
||||||
user_id: Some(user_id),
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(&secret_name)
|
||||||
|
.bind(vec![1_u8, 2, 3])
|
||||||
|
.fetch_one(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
sqlx::query("INSERT INTO entry_secrets (entry_id, secret_id) VALUES ($1, $2)")
|
||||||
|
.bind(entry_id)
|
||||||
|
.bind(secret_id)
|
||||||
|
.execute(&pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let result = delete_by_id(&pool, entry_id, user_id).await?;
|
||||||
assert!(!result.dry_run);
|
assert!(!result.dry_run);
|
||||||
assert_eq!(result.deleted.len(), 1);
|
assert_eq!(result.deleted.len(), 1);
|
||||||
assert_eq!(result.migrated.len(), 3);
|
assert_eq!(result.deleted[0].name, entry_name);
|
||||||
|
|
||||||
let key_exists: bool = sqlx::query_scalar(
|
let entry_exists: bool =
|
||||||
"SELECT EXISTS(SELECT 1 FROM entries WHERE id = $1 AND user_id = $2)",
|
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM entries WHERE id = $1)")
|
||||||
)
|
.bind(entry_id)
|
||||||
.bind(key_id)
|
|
||||||
.bind(user_id)
|
|
||||||
.fetch_one(&pool)
|
|
||||||
.await?;
|
|
||||||
assert!(!key_exists);
|
|
||||||
|
|
||||||
let owner_key_ref: Option<String> =
|
|
||||||
sqlx::query_scalar("SELECT metadata->>'key_ref' FROM entries WHERE id = $1")
|
|
||||||
.bind(ref_a)
|
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
let ref_b_key_ref: Option<String> =
|
let secret_exists: bool =
|
||||||
sqlx::query_scalar("SELECT metadata->>'key_ref' FROM entries WHERE id = $1")
|
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM secrets WHERE id = $1)")
|
||||||
.bind(ref_b)
|
.bind(secret_id)
|
||||||
.fetch_one(&pool)
|
|
||||||
.await?;
|
|
||||||
let ref_c_key_ref: Option<String> =
|
|
||||||
sqlx::query_scalar("SELECT metadata->>'key_ref' FROM entries WHERE id = $1")
|
|
||||||
.bind(ref_c)
|
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
assert!(!entry_exists);
|
||||||
|
assert!(!secret_exists);
|
||||||
|
|
||||||
assert_eq!(owner_key_ref, None);
|
|
||||||
assert_eq!(ref_b_key_ref.as_deref(), Some("afolder/srv-a"));
|
|
||||||
assert_eq!(ref_c_key_ref.as_deref(), Some("afolder/srv-a"));
|
|
||||||
|
|
||||||
let owner_has_copied: bool = sqlx::query_scalar(
|
|
||||||
"SELECT EXISTS(SELECT 1 FROM secrets WHERE entry_id = $1 AND field_name = 'pem')",
|
|
||||||
)
|
|
||||||
.bind(ref_a)
|
|
||||||
.fetch_one(&pool)
|
|
||||||
.await?;
|
|
||||||
assert!(owner_has_copied);
|
|
||||||
|
|
||||||
sqlx::query("DELETE FROM entries WHERE user_id = $1")
|
|
||||||
.bind(user_id)
|
|
||||||
.execute(&pool)
|
|
||||||
.await?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +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>,
|
_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?;
|
||||||
@@ -51,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()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -63,49 +63,11 @@ 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. Supported formats: "name" or "folder/name".
|
|
||||||
if let Some(key_ref) = entry.metadata.get("key_ref").and_then(|v| v.as_str()) {
|
|
||||||
let (ref_folder, ref_name) = if let Some((f, n)) = key_ref.split_once('/') {
|
|
||||||
(Some(f), n)
|
|
||||||
} else {
|
|
||||||
(None, key_ref)
|
|
||||||
};
|
|
||||||
let key_entries =
|
|
||||||
fetch_entries(pool, ref_folder, None, Some(ref_name), &[], None, user_id).await?;
|
|
||||||
|
|
||||||
if key_entries.len() > 1 {
|
|
||||||
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() {
|
|
||||||
let key_ids = vec![key_entry.id];
|
|
||||||
let key_fields_map = fetch_secrets_for_entries(pool, &key_ids).await?;
|
|
||||||
let empty = vec![];
|
|
||||||
let key_fields = key_fields_map.get(&key_entry.id).unwrap_or(&empty);
|
|
||||||
let key_prefix = env_prefix(key_entry, prefix);
|
|
||||||
for f in key_fields {
|
|
||||||
let decrypted = crypto::decrypt_json(master_key, &f.encrypted)?;
|
|
||||||
let key_var = format!(
|
|
||||||
"{}_{}",
|
|
||||||
key_prefix,
|
|
||||||
f.field_name.to_uppercase().replace(['-', '.'], "_")
|
|
||||||
);
|
|
||||||
map.insert(key_var, json_to_env_string(&decrypted));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
tracing::warn!(key_ref, ?user_id, "key_ref target not found");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(map)
|
Ok(map)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ 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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ 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)
|
||||||
@@ -49,7 +49,7 @@ 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)
|
Ok(map)
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ pub async fn get_secret_field_by_id(
|
|||||||
|
|
||||||
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)
|
||||||
@@ -98,7 +98,7 @@ pub async fn get_all_secrets_by_id(
|
|||||||
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)
|
Ok(map)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,6 +85,8 @@ pub async fn run(
|
|||||||
tags: &entry.tags,
|
tags: &entry.tags,
|
||||||
meta_entries: &meta_entries,
|
meta_entries: &meta_entries,
|
||||||
secret_entries: &secret_entries,
|
secret_entries: &secret_entries,
|
||||||
|
secret_types: &Default::default(),
|
||||||
|
link_secret_names: &[],
|
||||||
user_id: params.user_id,
|
user_id: params.user_id,
|
||||||
},
|
},
|
||||||
master_key,
|
master_key,
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ 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)]
|
||||||
@@ -27,7 +26,6 @@ pub async fn run(
|
|||||||
) -> Result<RollbackResult> {
|
) -> Result<RollbackResult> {
|
||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
struct EntryHistoryRow {
|
struct EntryHistoryRow {
|
||||||
entry_id: Uuid,
|
|
||||||
folder: String,
|
folder: String,
|
||||||
#[sqlx(rename = "type")]
|
#[sqlx(rename = "type")]
|
||||||
entry_type: String,
|
entry_type: String,
|
||||||
@@ -122,7 +120,7 @@ pub async fn run(
|
|||||||
|
|
||||||
let snap: Option<EntryHistoryRow> = if let Some(ver) = to_version {
|
let snap: Option<EntryHistoryRow> = if let Some(ver) = to_version {
|
||||||
sqlx::query_as(
|
sqlx::query_as(
|
||||||
"SELECT entry_id, folder, type, version, action, tags, metadata \
|
"SELECT folder, type, version, action, tags, metadata \
|
||||||
FROM entries_history \
|
FROM entries_history \
|
||||||
WHERE entry_id = $1 AND version = $2 ORDER BY id DESC LIMIT 1",
|
WHERE entry_id = $1 AND version = $2 ORDER BY id DESC LIMIT 1",
|
||||||
)
|
)
|
||||||
@@ -132,7 +130,7 @@ pub async fn run(
|
|||||||
.await?
|
.await?
|
||||||
} else {
|
} else {
|
||||||
sqlx::query_as(
|
sqlx::query_as(
|
||||||
"SELECT entry_id, folder, type, version, action, tags, metadata \
|
"SELECT folder, type, version, action, tags, metadata \
|
||||||
FROM entries_history \
|
FROM entries_history \
|
||||||
WHERE entry_id = $1 ORDER BY id DESC LIMIT 1",
|
WHERE entry_id = $1 ORDER BY id DESC LIMIT 1",
|
||||||
)
|
)
|
||||||
@@ -151,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?;
|
||||||
|
|
||||||
@@ -226,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",
|
||||||
},
|
},
|
||||||
@@ -297,22 +271,9 @@ pub async fn run(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
sqlx::query("DELETE FROM secrets WHERE entry_id = $1")
|
// In N:N mode, rollback restores entry metadata/tags only.
|
||||||
.bind(live_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(live_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,
|
||||||
|
|||||||
@@ -8,10 +8,23 @@ use crate::models::{Entry, SecretField};
|
|||||||
|
|
||||||
pub const FETCH_ALL_LIMIT: u32 = 100_000;
|
pub const FETCH_ALL_LIMIT: u32 = 100_000;
|
||||||
|
|
||||||
|
/// Build an ILIKE pattern for fuzzy matching, escaping `%` and `_` literals.
|
||||||
|
pub fn ilike_pattern(value: &str) -> String {
|
||||||
|
format!(
|
||||||
|
"%{}%",
|
||||||
|
value
|
||||||
|
.replace('\\', "\\\\")
|
||||||
|
.replace('%', "\\%")
|
||||||
|
.replace('_', "\\_")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub struct SearchParams<'a> {
|
pub struct SearchParams<'a> {
|
||||||
pub folder: Option<&'a str>,
|
pub folder: Option<&'a str>,
|
||||||
pub entry_type: Option<&'a str>,
|
pub entry_type: Option<&'a str>,
|
||||||
pub name: Option<&'a str>,
|
pub name: Option<&'a str>,
|
||||||
|
/// Fuzzy match on `entries.name` only (ILIKE with escaped `%`/`_`).
|
||||||
|
pub name_query: Option<&'a str>,
|
||||||
pub tags: &'a [String],
|
pub tags: &'a [String],
|
||||||
pub query: Option<&'a str>,
|
pub query: Option<&'a str>,
|
||||||
pub sort: &'a str,
|
pub sort: &'a str,
|
||||||
@@ -51,11 +64,15 @@ pub async fn count_entries(pool: &PgPool, a: &SearchParams<'_>) -> Result<i64> {
|
|||||||
if let Some(v) = a.name {
|
if let Some(v) = a.name {
|
||||||
q = q.bind(v);
|
q = q.bind(v);
|
||||||
}
|
}
|
||||||
|
if let Some(v) = a.name_query {
|
||||||
|
let pattern = ilike_pattern(v);
|
||||||
|
q = q.bind(pattern);
|
||||||
|
}
|
||||||
for tag in a.tags {
|
for tag in a.tags {
|
||||||
q = q.bind(tag);
|
q = q.bind(tag);
|
||||||
}
|
}
|
||||||
if let Some(v) = a.query {
|
if let Some(v) = a.query {
|
||||||
let pattern = format!("%{}%", v.replace('%', "\\%").replace('_', "\\_"));
|
let pattern = ilike_pattern(v);
|
||||||
q = q.bind(pattern);
|
q = q.bind(pattern);
|
||||||
}
|
}
|
||||||
let n = q.fetch_one(pool).await?;
|
let n = q.fetch_one(pool).await?;
|
||||||
@@ -86,6 +103,10 @@ fn entry_where_clause_and_next_idx(a: &SearchParams<'_>) -> (String, i32) {
|
|||||||
conditions.push(format!("name = ${}", idx));
|
conditions.push(format!("name = ${}", idx));
|
||||||
idx += 1;
|
idx += 1;
|
||||||
}
|
}
|
||||||
|
if a.name_query.is_some() {
|
||||||
|
conditions.push(format!("name ILIKE ${} ESCAPE '\\'", idx));
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
if !a.tags.is_empty() {
|
if !a.tags.is_empty() {
|
||||||
let placeholders: Vec<String> = a
|
let placeholders: Vec<String> = a
|
||||||
.tags
|
.tags
|
||||||
@@ -135,6 +156,7 @@ pub async fn run(pool: &PgPool, params: SearchParams<'_>) -> Result<SearchResult
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch entries matching the given filters — returns all matching entries up to FETCH_ALL_LIMIT.
|
/// Fetch entries matching the given filters — returns all matching entries up to FETCH_ALL_LIMIT.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub async fn fetch_entries(
|
pub async fn fetch_entries(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
folder: Option<&str>,
|
folder: Option<&str>,
|
||||||
@@ -148,6 +170,7 @@ pub async fn fetch_entries(
|
|||||||
folder,
|
folder,
|
||||||
entry_type,
|
entry_type,
|
||||||
name,
|
name,
|
||||||
|
name_query: None,
|
||||||
tags,
|
tags,
|
||||||
query,
|
query,
|
||||||
sort: "name",
|
sort: "name",
|
||||||
@@ -189,11 +212,15 @@ async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<
|
|||||||
if let Some(v) = a.name {
|
if let Some(v) = a.name {
|
||||||
q = q.bind(v);
|
q = q.bind(v);
|
||||||
}
|
}
|
||||||
|
if let Some(v) = a.name_query {
|
||||||
|
let pattern = ilike_pattern(v);
|
||||||
|
q = q.bind(pattern);
|
||||||
|
}
|
||||||
for tag in a.tags {
|
for tag in a.tags {
|
||||||
q = q.bind(tag);
|
q = q.bind(tag);
|
||||||
}
|
}
|
||||||
if let Some(v) = a.query {
|
if let Some(v) = a.query {
|
||||||
let pattern = format!("%{}%", v.replace('%', "\\%").replace('_', "\\_"));
|
let pattern = ilike_pattern(v);
|
||||||
q = q.bind(pattern);
|
q = q.bind(pattern);
|
||||||
}
|
}
|
||||||
q = q.bind(a.limit as i64).bind(a.offset as i64);
|
q = q.bind(a.limit as i64).bind(a.offset as i64);
|
||||||
@@ -210,8 +237,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)
|
||||||
@@ -219,7 +250,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)
|
||||||
}
|
}
|
||||||
@@ -232,8 +264,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)
|
||||||
@@ -241,7 +277,8 @@ 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)
|
||||||
}
|
}
|
||||||
@@ -345,3 +382,42 @@ 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ilike_pattern_escapes_backslash_percent_and_underscore() {
|
||||||
|
assert_eq!(ilike_pattern(r"hello\_100%"), r"%hello\\\_100\%%");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use crate::crypto;
|
use crate::crypto;
|
||||||
use crate::db;
|
use crate::db;
|
||||||
|
use crate::error::{AppError, DbErrorContext};
|
||||||
use crate::models::{EntryRow, EntryWriteRow};
|
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, insert_path, parse_key_path,
|
||||||
@@ -23,6 +24,8 @@ pub struct UpdateResult {
|
|||||||
pub remove_meta: Vec<String>,
|
pub remove_meta: Vec<String>,
|
||||||
pub secret_keys: Vec<String>,
|
pub secret_keys: Vec<String>,
|
||||||
pub remove_secrets: Vec<String>,
|
pub remove_secrets: Vec<String>,
|
||||||
|
pub linked_secrets: Vec<String>,
|
||||||
|
pub unlinked_secrets: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct UpdateParams<'a> {
|
pub struct UpdateParams<'a> {
|
||||||
@@ -35,7 +38,10 @@ pub struct UpdateParams<'a> {
|
|||||||
pub meta_entries: &'a [String],
|
pub meta_entries: &'a [String],
|
||||||
pub remove_meta: &'a [String],
|
pub remove_meta: &'a [String],
|
||||||
pub secret_entries: &'a [String],
|
pub secret_entries: &'a [String],
|
||||||
|
pub secret_types: &'a std::collections::HashMap<String, String>,
|
||||||
pub remove_secrets: &'a [String],
|
pub remove_secrets: &'a [String],
|
||||||
|
pub link_secret_names: &'a [String],
|
||||||
|
pub unlink_secret_names: &'a [String],
|
||||||
pub user_id: Option<Uuid>,
|
pub user_id: Option<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,10 +96,7 @@ pub async fn run(
|
|||||||
let row = match rows.len() {
|
let row = match rows.len() {
|
||||||
0 => {
|
0 => {
|
||||||
tx.rollback().await?;
|
tx.rollback().await?;
|
||||||
anyhow::bail!(
|
return Err(AppError::NotFoundEntry.into());
|
||||||
"Not found: '{}'. Use `add` to create it first.",
|
|
||||||
params.name
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
1 => rows.into_iter().next().unwrap(),
|
1 => rows.into_iter().next().unwrap(),
|
||||||
_ => {
|
_ => {
|
||||||
@@ -167,14 +170,9 @@ pub async fn run(
|
|||||||
|
|
||||||
if result.rows_affected() == 0 {
|
if result.rows_affected() == 0 {
|
||||||
tx.rollback().await?;
|
tx.rollback().await?;
|
||||||
anyhow::bail!(
|
return Err(AppError::ConcurrentModification.into());
|
||||||
"Concurrent modification detected for '{}'. Please retry.",
|
|
||||||
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("", &{
|
||||||
@@ -192,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)
|
||||||
@@ -203,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",
|
||||||
},
|
},
|
||||||
@@ -216,16 +215,36 @@ 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_type = params
|
||||||
|
.secret_types
|
||||||
|
.get(field_name)
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
.unwrap_or("text");
|
||||||
|
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.to_string())
|
||||||
|
.bind(secret_type)
|
||||||
|
.bind(&encrypted)
|
||||||
|
.fetch_one(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::from_db_error(e, DbErrorContext::secret_name(field_name)))?;
|
||||||
|
sqlx::query("INSERT INTO entry_secrets (entry_id, secret_id) VALUES ($1, $2)")
|
||||||
|
.bind(row.id)
|
||||||
|
.bind(secret_id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,7 +258,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)
|
||||||
@@ -250,10 +272,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",
|
||||||
},
|
},
|
||||||
@@ -262,10 +282,114 @@ 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?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Link existing secrets by name
|
||||||
|
let mut linked_secrets = Vec::new();
|
||||||
|
for link_name in params.link_secret_names {
|
||||||
|
let link_name = link_name.trim();
|
||||||
|
if link_name.is_empty() {
|
||||||
|
anyhow::bail!("link_secret_names contains an empty name");
|
||||||
|
}
|
||||||
|
let secret_ids: Vec<Uuid> = if let Some(uid) = params.user_id {
|
||||||
|
sqlx::query_scalar("SELECT id FROM secrets WHERE user_id = $1 AND name = $2")
|
||||||
|
.bind(uid)
|
||||||
|
.bind(link_name)
|
||||||
|
.fetch_all(&mut *tx)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
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(row.id)
|
||||||
|
.bind(secret_ids[0])
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
linked_secrets.push(link_name.to_string());
|
||||||
|
}
|
||||||
|
n => anyhow::bail!(
|
||||||
|
"Ambiguous: {} secrets named '{}' found. Please deduplicate names first.",
|
||||||
|
n,
|
||||||
|
link_name
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unlink secrets by name
|
||||||
|
let mut unlinked_secrets = Vec::new();
|
||||||
|
for unlink_name in params.unlink_secret_names {
|
||||||
|
let unlink_name = unlink_name.trim();
|
||||||
|
if unlink_name.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow)]
|
||||||
|
struct SecretToUnlink {
|
||||||
|
id: Uuid,
|
||||||
|
encrypted: Vec<u8>,
|
||||||
|
}
|
||||||
|
let secret: Option<SecretToUnlink> = sqlx::query_as(
|
||||||
|
"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(unlink_name)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if let Some(s) = secret {
|
||||||
|
if let Err(e) = db::snapshot_secret_history(
|
||||||
|
&mut tx,
|
||||||
|
db::SecretSnapshotParams {
|
||||||
|
secret_id: s.id,
|
||||||
|
name: unlink_name,
|
||||||
|
encrypted: &s.encrypted,
|
||||||
|
action: "delete",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
tracing::warn!(error = %e, "failed to snapshot secret field history before unlink");
|
||||||
|
}
|
||||||
|
sqlx::query("DELETE FROM entry_secrets WHERE entry_id = $1 AND secret_id = $2")
|
||||||
|
.bind(row.id)
|
||||||
|
.bind(s.id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.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(s.id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
unlinked_secrets.push(unlink_name.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,6 +412,8 @@ pub async fn run(
|
|||||||
"remove_meta": remove_meta_keys,
|
"remove_meta": remove_meta_keys,
|
||||||
"secret_keys": secret_keys,
|
"secret_keys": secret_keys,
|
||||||
"remove_secrets": remove_secret_keys,
|
"remove_secrets": remove_secret_keys,
|
||||||
|
"linked_secrets": linked_secrets,
|
||||||
|
"unlinked_secrets": unlinked_secrets,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@@ -304,6 +430,8 @@ pub async fn run(
|
|||||||
remove_meta: remove_meta_keys,
|
remove_meta: remove_meta_keys,
|
||||||
secret_keys,
|
secret_keys,
|
||||||
remove_secrets: remove_secret_keys,
|
remove_secrets: remove_secret_keys,
|
||||||
|
linked_secrets,
|
||||||
|
unlinked_secrets,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,13 +452,13 @@ pub async fn update_fields_by_id(
|
|||||||
user_id: Uuid,
|
user_id: Uuid,
|
||||||
params: UpdateEntryFieldsByIdParams<'_>,
|
params: UpdateEntryFieldsByIdParams<'_>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
if params.folder.len() > 128 {
|
if params.folder.chars().count() > 128 {
|
||||||
anyhow::bail!("folder must be at most 128 characters");
|
anyhow::bail!("folder must be at most 128 characters");
|
||||||
}
|
}
|
||||||
if params.entry_type.len() > 64 {
|
if params.entry_type.chars().count() > 64 {
|
||||||
anyhow::bail!("type must be at most 64 characters");
|
anyhow::bail!("type must be at most 64 characters");
|
||||||
}
|
}
|
||||||
if params.name.len() > 256 {
|
if params.name.chars().count() > 256 {
|
||||||
anyhow::bail!("name must be at most 256 characters");
|
anyhow::bail!("name must be at most 256 characters");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,7 +477,7 @@ pub async fn update_fields_by_id(
|
|||||||
Some(r) => r,
|
Some(r) => r,
|
||||||
None => {
|
None => {
|
||||||
tx.rollback().await?;
|
tx.rollback().await?;
|
||||||
anyhow::bail!("Entry not found");
|
return Err(AppError::NotFoundEntry.into());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -372,13 +500,15 @@ pub async fn update_fields_by_id(
|
|||||||
tracing::warn!(error = %e, "failed to snapshot entry history before web update");
|
tracing::warn!(error = %e, "failed to snapshot entry history before web update");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let entry_type = params.entry_type.trim();
|
||||||
|
|
||||||
let res = sqlx::query(
|
let res = sqlx::query(
|
||||||
"UPDATE entries SET folder = $1, type = $2, name = $3, notes = $4, tags = $5, metadata = $6, \
|
"UPDATE entries SET folder = $1, type = $2, name = $3, notes = $4, tags = $5, metadata = $6, \
|
||||||
version = version + 1, updated_at = NOW() \
|
version = version + 1, updated_at = NOW() \
|
||||||
WHERE id = $7 AND version = $8",
|
WHERE id = $7 AND version = $8",
|
||||||
)
|
)
|
||||||
.bind(params.folder)
|
.bind(params.folder)
|
||||||
.bind(params.entry_type)
|
.bind(entry_type)
|
||||||
.bind(params.name)
|
.bind(params.name)
|
||||||
.bind(params.notes)
|
.bind(params.notes)
|
||||||
.bind(params.tags)
|
.bind(params.tags)
|
||||||
@@ -391,16 +521,17 @@ pub async fn update_fields_by_id(
|
|||||||
if let sqlx::Error::Database(ref d) = e
|
if let sqlx::Error::Database(ref d) = e
|
||||||
&& d.code().as_deref() == Some("23505")
|
&& d.code().as_deref() == Some("23505")
|
||||||
{
|
{
|
||||||
return anyhow::anyhow!(
|
return AppError::ConflictEntryName {
|
||||||
"An entry with this folder and name already exists for your account."
|
folder: params.folder.to_string(),
|
||||||
);
|
name: params.name.to_string(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
e.into()
|
AppError::Internal(e.into())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
if res.rows_affected() == 0 {
|
if res.rows_affected() == 0 {
|
||||||
tx.rollback().await?;
|
tx.rollback().await?;
|
||||||
anyhow::bail!("Concurrent modification detected. Please refresh and try again.");
|
return Err(AppError::ConcurrentModification.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::audit::log_tx(
|
crate::audit::log_tx(
|
||||||
@@ -408,7 +539,7 @@ pub async fn update_fields_by_id(
|
|||||||
Some(user_id),
|
Some(user_id),
|
||||||
"update",
|
"update",
|
||||||
params.folder,
|
params.folder,
|
||||||
params.entry_type,
|
entry_type,
|
||||||
params.name,
|
params.name,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"source": "web",
|
"source": "web",
|
||||||
|
|||||||
@@ -16,14 +16,17 @@ pub struct OAuthProfile {
|
|||||||
/// Find or create a user from an OAuth profile.
|
/// Find or create a user from an OAuth profile.
|
||||||
/// Returns (user, is_new) where is_new indicates first-time registration.
|
/// Returns (user, is_new) where is_new indicates first-time registration.
|
||||||
pub async fn find_or_create_user(pool: &PgPool, profile: OAuthProfile) -> Result<(User, bool)> {
|
pub async fn find_or_create_user(pool: &PgPool, profile: OAuthProfile) -> Result<(User, bool)> {
|
||||||
// Check if this OAuth account already exists
|
// Use a transaction with FOR UPDATE to prevent TOCTOU race conditions
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
|
||||||
|
// Check if this OAuth account already exists (with row lock)
|
||||||
let existing: Option<OauthAccount> = sqlx::query_as(
|
let existing: Option<OauthAccount> = sqlx::query_as(
|
||||||
"SELECT id, user_id, provider, provider_id, email, name, avatar_url, created_at \
|
"SELECT id, user_id, provider, provider_id, email, name, avatar_url, created_at \
|
||||||
FROM oauth_accounts WHERE provider = $1 AND provider_id = $2",
|
FROM oauth_accounts WHERE provider = $1 AND provider_id = $2 FOR UPDATE",
|
||||||
)
|
)
|
||||||
.bind(&profile.provider)
|
.bind(&profile.provider)
|
||||||
.bind(&profile.provider_id)
|
.bind(&profile.provider_id)
|
||||||
.fetch_optional(pool)
|
.fetch_optional(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if let Some(oa) = existing {
|
if let Some(oa) = existing {
|
||||||
@@ -32,8 +35,9 @@ pub async fn find_or_create_user(pool: &PgPool, profile: OAuthProfile) -> Result
|
|||||||
FROM users WHERE id = $1",
|
FROM users WHERE id = $1",
|
||||||
)
|
)
|
||||||
.bind(oa.user_id)
|
.bind(oa.user_id)
|
||||||
.fetch_one(pool)
|
.fetch_one(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
tx.commit().await?;
|
||||||
return Ok((user, false));
|
return Ok((user, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,8 +47,6 @@ pub async fn find_or_create_user(pool: &PgPool, profile: OAuthProfile) -> Result
|
|||||||
.clone()
|
.clone()
|
||||||
.unwrap_or_else(|| profile.email.clone().unwrap_or_else(|| "User".to_string()));
|
.unwrap_or_else(|| profile.email.clone().unwrap_or_else(|| "User".to_string()));
|
||||||
|
|
||||||
let mut tx = pool.begin().await?;
|
|
||||||
|
|
||||||
let user: User = sqlx::query_as(
|
let user: User = sqlx::query_as(
|
||||||
"INSERT INTO users (email, name, avatar_url) \
|
"INSERT INTO users (email, name, avatar_url) \
|
||||||
VALUES ($1, $2, $3) \
|
VALUES ($1, $2, $3) \
|
||||||
@@ -125,13 +127,16 @@ pub async fn bind_oauth_account(
|
|||||||
user_id: Uuid,
|
user_id: Uuid,
|
||||||
profile: OAuthProfile,
|
profile: OAuthProfile,
|
||||||
) -> Result<OauthAccount> {
|
) -> Result<OauthAccount> {
|
||||||
// Check if this provider_id is already linked to someone else
|
// Use a transaction with FOR UPDATE to prevent TOCTOU race conditions
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
|
||||||
|
// Check if this provider_id is already linked to someone else (with row lock)
|
||||||
let conflict: Option<(Uuid,)> = sqlx::query_as(
|
let conflict: Option<(Uuid,)> = sqlx::query_as(
|
||||||
"SELECT user_id FROM oauth_accounts WHERE provider = $1 AND provider_id = $2",
|
"SELECT user_id FROM oauth_accounts WHERE provider = $1 AND provider_id = $2 FOR UPDATE",
|
||||||
)
|
)
|
||||||
.bind(&profile.provider)
|
.bind(&profile.provider)
|
||||||
.bind(&profile.provider_id)
|
.bind(&profile.provider_id)
|
||||||
.fetch_optional(pool)
|
.fetch_optional(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if let Some((existing_user_id,)) = conflict {
|
if let Some((existing_user_id,)) = conflict {
|
||||||
@@ -148,11 +153,11 @@ pub async fn bind_oauth_account(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let existing_provider_for_user: Option<(String,)> = sqlx::query_as(
|
let existing_provider_for_user: Option<(String,)> = sqlx::query_as(
|
||||||
"SELECT provider_id FROM oauth_accounts WHERE user_id = $1 AND provider = $2",
|
"SELECT provider_id FROM oauth_accounts WHERE user_id = $1 AND provider = $2 FOR UPDATE",
|
||||||
)
|
)
|
||||||
.bind(user_id)
|
.bind(user_id)
|
||||||
.bind(&profile.provider)
|
.bind(&profile.provider)
|
||||||
.fetch_optional(pool)
|
.fetch_optional(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if existing_provider_for_user.is_some() {
|
if existing_provider_for_user.is_some() {
|
||||||
@@ -174,9 +179,10 @@ pub async fn bind_oauth_account(
|
|||||||
.bind(&profile.email)
|
.bind(&profile.email)
|
||||||
.bind(&profile.name)
|
.bind(&profile.name)
|
||||||
.bind(&profile.avatar_url)
|
.bind(&profile.avatar_url)
|
||||||
.fetch_one(pool)
|
.fetch_one(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
Ok(account)
|
Ok(account)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
crates/secrets-core/src/taxonomy.rs
Normal file
4
crates/secrets-core/src/taxonomy.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
/// Canonical secret type options for UI dropdowns.
|
||||||
|
pub const SECRET_TYPE_OPTIONS: &[&str] = &[
|
||||||
|
"text", "password", "token", "api-key", "ssh-key", "url", "phone", "id-card",
|
||||||
|
];
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "secrets-mcp"
|
name = "secrets-mcp"
|
||||||
version = "0.3.7"
|
version = "0.5.2"
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
@@ -17,9 +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", "trace"] }
|
tower-http = { version = "0.6", features = ["cors", "trace", "limit"] }
|
||||||
tower-sessions = "0.14"
|
tower-sessions = "0.14"
|
||||||
tower-sessions-sqlx-store-chrono = { version = "0.14", features = ["postgres"] }
|
tower-sessions-sqlx-store-chrono = { version = "0.14", features = ["postgres"] }
|
||||||
|
governor = { version = "0.10", features = ["std", "jitter"] }
|
||||||
time = "0.3"
|
time = "0.3"
|
||||||
|
|
||||||
# OAuth (manual token exchange via reqwest)
|
# OAuth (manual token exchange via reqwest)
|
||||||
@@ -44,3 +45,4 @@ dotenvy.workspace = true
|
|||||||
urlencoding = "2"
|
urlencoding = "2"
|
||||||
schemars = "1"
|
schemars = "1"
|
||||||
http = "1"
|
http = "1"
|
||||||
|
url = "2"
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
use std::net::SocketAddr;
|
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{ConnectInfo, Request, State},
|
extract::{Request, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
middleware::Next,
|
middleware::Next,
|
||||||
response::Response,
|
response::Response,
|
||||||
@@ -11,29 +9,14 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use secrets_core::service::api_key::validate_api_key;
|
use secrets_core::service::api_key::validate_api_key;
|
||||||
|
|
||||||
|
use crate::client_ip;
|
||||||
|
|
||||||
/// Injected into request extensions after Bearer token validation.
|
/// Injected into request extensions after Bearer token validation.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct AuthUser {
|
pub struct AuthUser {
|
||||||
pub user_id: Uuid,
|
pub user_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log_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())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Axum middleware that validates Bearer API keys for the /mcp route.
|
/// Axum middleware that validates Bearer API keys for the /mcp route.
|
||||||
/// Passes all non-MCP paths through without authentication.
|
/// Passes all non-MCP paths through without authentication.
|
||||||
pub async fn bearer_auth_middleware(
|
pub async fn bearer_auth_middleware(
|
||||||
@@ -43,7 +26,7 @@ pub async fn bearer_auth_middleware(
|
|||||||
) -> Result<Response, StatusCode> {
|
) -> Result<Response, StatusCode> {
|
||||||
let path = req.uri().path();
|
let path = req.uri().path();
|
||||||
let method = req.method().as_str();
|
let method = req.method().as_str();
|
||||||
let client_ip = log_client_ip(&req);
|
let client_ip = client_ip::extract_client_ip(&req);
|
||||||
|
|
||||||
// Only authenticate /mcp paths
|
// Only authenticate /mcp paths
|
||||||
if !path.starts_with("/mcp") {
|
if !path.starts_with("/mcp") {
|
||||||
@@ -66,7 +49,7 @@ pub async fn bearer_auth_middleware(
|
|||||||
tracing::warn!(
|
tracing::warn!(
|
||||||
method,
|
method,
|
||||||
path,
|
path,
|
||||||
client_ip = client_ip.as_deref(),
|
%client_ip,
|
||||||
"invalid Authorization header format on /mcp (expected Bearer …)"
|
"invalid Authorization header format on /mcp (expected Bearer …)"
|
||||||
);
|
);
|
||||||
return Err(StatusCode::UNAUTHORIZED);
|
return Err(StatusCode::UNAUTHORIZED);
|
||||||
@@ -75,7 +58,7 @@ pub async fn bearer_auth_middleware(
|
|||||||
tracing::warn!(
|
tracing::warn!(
|
||||||
method,
|
method,
|
||||||
path,
|
path,
|
||||||
client_ip = client_ip.as_deref(),
|
%client_ip,
|
||||||
"missing Authorization header on /mcp"
|
"missing Authorization header on /mcp"
|
||||||
);
|
);
|
||||||
return Err(StatusCode::UNAUTHORIZED);
|
return Err(StatusCode::UNAUTHORIZED);
|
||||||
@@ -93,7 +76,7 @@ pub async fn bearer_auth_middleware(
|
|||||||
tracing::warn!(
|
tracing::warn!(
|
||||||
method,
|
method,
|
||||||
path,
|
path,
|
||||||
client_ip = client_ip.as_deref(),
|
%client_ip,
|
||||||
key_prefix = %&raw_key.chars().take(12).collect::<String>(),
|
key_prefix = %&raw_key.chars().take(12).collect::<String>(),
|
||||||
key_len = raw_key.len(),
|
key_len = raw_key.len(),
|
||||||
"invalid api key (not found in database — e.g. revoked key or DB was reset; update MCP client Bearer token)"
|
"invalid api key (not found in database — e.g. revoked key or DB was reset; update MCP client Bearer token)"
|
||||||
@@ -104,7 +87,7 @@ pub async fn bearer_auth_middleware(
|
|||||||
tracing::error!(
|
tracing::error!(
|
||||||
method,
|
method,
|
||||||
path,
|
path,
|
||||||
client_ip = client_ip.as_deref(),
|
%client_ip,
|
||||||
error = %e,
|
error = %e,
|
||||||
"api key validation error"
|
"api key validation error"
|
||||||
);
|
);
|
||||||
|
|||||||
65
crates/secrets-mcp/src/client_ip.rs
Normal file
65
crates/secrets-mcp/src/client_ip.rs
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
use axum::extract::Request;
|
||||||
|
use std::net::{IpAddr, SocketAddr};
|
||||||
|
|
||||||
|
/// Extract the client IP from a request.
|
||||||
|
///
|
||||||
|
/// When the `TRUST_PROXY` environment variable is set to `1` or `true`, the
|
||||||
|
/// `X-Forwarded-For` and `X-Real-IP` headers are consulted first, which is
|
||||||
|
/// appropriate when the service runs behind a trusted reverse proxy (e.g.
|
||||||
|
/// Caddy). Otherwise — or if those headers are absent/empty — the direct TCP
|
||||||
|
/// connection address from `ConnectInfo` is used.
|
||||||
|
///
|
||||||
|
/// **Important**: only enable `TRUST_PROXY` when the application is guaranteed
|
||||||
|
/// to receive traffic exclusively through a controlled reverse proxy. Enabling
|
||||||
|
/// it on a directly-exposed port allows clients to spoof their IP address and
|
||||||
|
/// bypass per-IP rate limiting.
|
||||||
|
pub fn extract_client_ip(req: &Request) -> String {
|
||||||
|
if trust_proxy_enabled() {
|
||||||
|
if let Some(ip) = forwarded_for_ip(req.headers()) {
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
if let Some(ip) = real_ip(req.headers()) {
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
connect_info_ip(req).unwrap_or_else(|| "unknown".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn trust_proxy_enabled() -> bool {
|
||||||
|
static CACHE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||||
|
*CACHE.get_or_init(|| {
|
||||||
|
matches!(
|
||||||
|
std::env::var("TRUST_PROXY").as_deref(),
|
||||||
|
Ok("1") | Ok("true") | Ok("yes")
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn forwarded_for_ip(headers: &axum::http::HeaderMap) -> Option<String> {
|
||||||
|
let value = headers.get("x-forwarded-for")?.to_str().ok()?;
|
||||||
|
let first = value.split(',').next()?.trim();
|
||||||
|
if first.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
validate_ip(first)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn real_ip(headers: &axum::http::HeaderMap) -> Option<String> {
|
||||||
|
let value = headers.get("x-real-ip")?.to_str().ok()?;
|
||||||
|
let ip = value.trim();
|
||||||
|
if ip.is_empty() { None } else { validate_ip(ip) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate that a string is a valid IP address.
|
||||||
|
/// Returns Some(ip) if valid, None otherwise.
|
||||||
|
fn validate_ip(s: &str) -> Option<String> {
|
||||||
|
s.parse::<IpAddr>().ok().map(|ip| ip.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn connect_info_ip(req: &Request) -> Option<String> {
|
||||||
|
req.extensions()
|
||||||
|
.get::<axum::extract::ConnectInfo<SocketAddr>>()
|
||||||
|
.map(|c| c.0.ip().to_string())
|
||||||
|
}
|
||||||
54
crates/secrets-mcp/src/error.rs
Normal file
54
crates/secrets-mcp/src/error.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
use secrets_core::error::AppError;
|
||||||
|
|
||||||
|
/// Map a structured `AppError` to an MCP protocol error.
|
||||||
|
///
|
||||||
|
/// This replaces the previous pattern of swallowing all errors into `-32603`.
|
||||||
|
pub fn app_error_to_mcp(err: &AppError) -> rmcp::ErrorData {
|
||||||
|
match err {
|
||||||
|
AppError::ConflictSecretName { secret_name } => rmcp::ErrorData::invalid_request(
|
||||||
|
format!(
|
||||||
|
"A secret with the name '{secret_name}' already exists for your account. \
|
||||||
|
Secret names must be unique per user."
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
AppError::ConflictEntryName { folder, name } => rmcp::ErrorData::invalid_request(
|
||||||
|
format!(
|
||||||
|
"An entry with folder='{folder}' and name='{name}' already exists. \
|
||||||
|
The combination of folder and name must be unique."
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
AppError::NotFoundEntry => rmcp::ErrorData::invalid_request(
|
||||||
|
"Entry not found. Use secrets_find to discover existing entries.",
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
AppError::NotFoundUser => rmcp::ErrorData::invalid_request("User not found.", None),
|
||||||
|
AppError::NotFoundSecret => rmcp::ErrorData::invalid_request("Secret not found.", None),
|
||||||
|
AppError::AuthenticationFailed => rmcp::ErrorData::invalid_request(
|
||||||
|
"Authentication failed. Please check your API key or login credentials.",
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
AppError::Unauthorized => rmcp::ErrorData::invalid_request(
|
||||||
|
"Unauthorized: you do not have permission to access this resource.",
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
AppError::Validation { message } => rmcp::ErrorData::invalid_request(message.clone(), None),
|
||||||
|
AppError::ConcurrentModification => rmcp::ErrorData::invalid_request(
|
||||||
|
"The entry was modified by another request. Please refresh and try again.",
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
AppError::DecryptionFailed => rmcp::ErrorData::invalid_request(
|
||||||
|
"Decryption failed — the encryption key may be incorrect or does not match the data.",
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
AppError::EncryptionKeyNotSet => rmcp::ErrorData::invalid_request(
|
||||||
|
"Encryption key not set. You must set a passphrase before using this feature.",
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
AppError::Internal(_) => rmcp::ErrorData::internal_error(
|
||||||
|
"Request failed due to a server error. Check service logs if you need details.",
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
mod auth;
|
mod auth;
|
||||||
|
mod client_ip;
|
||||||
|
mod error;
|
||||||
mod logging;
|
mod logging;
|
||||||
mod oauth;
|
mod oauth;
|
||||||
|
mod rate_limit;
|
||||||
mod tools;
|
mod tools;
|
||||||
|
mod validation;
|
||||||
mod web;
|
mod web;
|
||||||
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
@@ -40,6 +44,14 @@ fn load_env_var(name: &str) -> Option<String> {
|
|||||||
std::env::var(name).ok().filter(|s| !s.is_empty())
|
std::env::var(name).ok().filter(|s| !s.is_empty())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Pretty-print bind address in logs (`127.0.0.1` → `localhost`); actual socket bind unchanged.
|
||||||
|
fn listen_addr_log_display(bind_addr: &str) -> String {
|
||||||
|
bind_addr
|
||||||
|
.strip_prefix("127.0.0.1:")
|
||||||
|
.map(|port| format!("localhost:{port}"))
|
||||||
|
.unwrap_or_else(|| bind_addr.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
fn load_oauth_config(prefix: &str, base_url: &str, path: &str) -> Option<OAuthConfig> {
|
fn load_oauth_config(prefix: &str, base_url: &str, path: &str) -> Option<OAuthConfig> {
|
||||||
let client_id = load_env_var(&format!("{}_CLIENT_ID", prefix))?;
|
let client_id = load_env_var(&format!("{}_CLIENT_ID", prefix))?;
|
||||||
let client_secret = load_env_var(&format!("{}_CLIENT_SECRET", prefix))?;
|
let client_secret = load_env_var(&format!("{}_CLIENT_SECRET", prefix))?;
|
||||||
@@ -144,10 +156,43 @@ async fn main() -> Result<()> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// ── Router ────────────────────────────────────────────────────────────────
|
// ── Router ────────────────────────────────────────────────────────────────
|
||||||
let cors = CorsLayer::new()
|
// CORS: restrict origins in production, allow all in development
|
||||||
.allow_origin(Any)
|
let is_production = matches!(
|
||||||
.allow_methods(Any)
|
load_env_var("SECRETS_ENV")
|
||||||
.allow_headers(Any);
|
.as_deref()
|
||||||
|
.map(|s| s.to_ascii_lowercase())
|
||||||
|
.as_deref(),
|
||||||
|
Some("prod" | "production")
|
||||||
|
);
|
||||||
|
|
||||||
|
let cors = if is_production {
|
||||||
|
// Only use the origin part (scheme://host:port) of BASE_URL for CORS.
|
||||||
|
// Browsers send Origin without path, so including a path would cause mismatches.
|
||||||
|
let allowed_origin = if let Ok(parsed) = base_url.parse::<url::Url>() {
|
||||||
|
let origin = parsed.origin().ascii_serialization();
|
||||||
|
origin
|
||||||
|
.parse::<axum::http::HeaderValue>()
|
||||||
|
.unwrap_or_else(|_| panic!("invalid BASE_URL origin: {}", origin))
|
||||||
|
} else {
|
||||||
|
base_url
|
||||||
|
.parse::<axum::http::HeaderValue>()
|
||||||
|
.unwrap_or_else(|_| panic!("invalid BASE_URL: {}", base_url))
|
||||||
|
};
|
||||||
|
CorsLayer::new()
|
||||||
|
.allow_origin(allowed_origin)
|
||||||
|
.allow_methods(Any)
|
||||||
|
.allow_headers(Any)
|
||||||
|
.allow_credentials(true)
|
||||||
|
} else {
|
||||||
|
CorsLayer::new()
|
||||||
|
.allow_origin(Any)
|
||||||
|
.allow_methods(Any)
|
||||||
|
.allow_headers(Any)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Rate limiting
|
||||||
|
let rate_limit_state = rate_limit::RateLimitState::new();
|
||||||
|
let rate_limit_cleanup = rate_limit::spawn_cleanup_task(rate_limit_state.ip_limiter.clone());
|
||||||
|
|
||||||
let router = Router::new()
|
let router = Router::new()
|
||||||
.merge(web::web_router())
|
.merge(web::web_router())
|
||||||
@@ -159,6 +204,10 @@ async fn main() -> Result<()> {
|
|||||||
pool,
|
pool,
|
||||||
auth::bearer_auth_middleware,
|
auth::bearer_auth_middleware,
|
||||||
))
|
))
|
||||||
|
.layer(axum::middleware::from_fn_with_state(
|
||||||
|
rate_limit_state.clone(),
|
||||||
|
rate_limit::rate_limit_middleware,
|
||||||
|
))
|
||||||
.layer(session_layer)
|
.layer(session_layer)
|
||||||
.layer(cors)
|
.layer(cors)
|
||||||
.with_state(app_state);
|
.with_state(app_state);
|
||||||
@@ -168,7 +217,10 @@ async fn main() -> Result<()> {
|
|||||||
.await
|
.await
|
||||||
.with_context(|| format!("failed to bind to {}", bind_addr))?;
|
.with_context(|| format!("failed to bind to {}", bind_addr))?;
|
||||||
|
|
||||||
tracing::info!("Secrets MCP Server listening on http://{}", bind_addr);
|
tracing::info!(
|
||||||
|
"Secrets MCP Server listening on http://{}",
|
||||||
|
listen_addr_log_display(&bind_addr)
|
||||||
|
);
|
||||||
tracing::info!("MCP endpoint: {}/mcp", base_url);
|
tracing::info!("MCP endpoint: {}/mcp", base_url);
|
||||||
|
|
||||||
axum::serve(
|
axum::serve(
|
||||||
@@ -180,12 +232,28 @@ async fn main() -> Result<()> {
|
|||||||
.context("server error")?;
|
.context("server error")?;
|
||||||
|
|
||||||
session_cleanup.abort();
|
session_cleanup.abort();
|
||||||
|
rate_limit_cleanup.abort();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn shutdown_signal() {
|
async fn shutdown_signal() {
|
||||||
tokio::signal::ctrl_c()
|
let ctrl_c = tokio::signal::ctrl_c();
|
||||||
.await
|
|
||||||
.expect("failed to install CTRL+C signal handler");
|
#[cfg(unix)]
|
||||||
|
let terminate = async {
|
||||||
|
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
||||||
|
.expect("failed to install SIGTERM handler")
|
||||||
|
.recv()
|
||||||
|
.await;
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
let terminate = std::future::pending::<()>();
|
||||||
|
|
||||||
|
tokio::select! {
|
||||||
|
_ = ctrl_c => {},
|
||||||
|
_ = terminate => {},
|
||||||
|
}
|
||||||
|
|
||||||
tracing::info!("Shutting down gracefully...");
|
tracing::info!("Shutting down gracefully...");
|
||||||
}
|
}
|
||||||
|
|||||||
160
crates/secrets-mcp/src/rate_limit.rs
Normal file
160
crates/secrets-mcp/src/rate_limit.rs
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
use std::num::NonZeroU32;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use axum::{
|
||||||
|
extract::{Request, State},
|
||||||
|
http::{HeaderMap, HeaderValue, StatusCode},
|
||||||
|
middleware::Next,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
use governor::{
|
||||||
|
Quota, RateLimiter,
|
||||||
|
clock::{Clock, DefaultClock},
|
||||||
|
state::{InMemoryState, NotKeyed, keyed::DashMapStateStore},
|
||||||
|
};
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
use crate::client_ip;
|
||||||
|
|
||||||
|
/// Per-IP rate limiter (keyed by client IP string)
|
||||||
|
type IpRateLimiter = RateLimiter<String, DashMapStateStore<String>, DefaultClock>;
|
||||||
|
|
||||||
|
/// Global rate limiter (not keyed)
|
||||||
|
type GlobalRateLimiter = RateLimiter<NotKeyed, InMemoryState, DefaultClock>;
|
||||||
|
|
||||||
|
/// Parse a u32 env value into NonZeroU32, logging a warning and falling back
|
||||||
|
/// to the default if the value is zero.
|
||||||
|
fn nz_or_log(value: u32, default: u32, name: &str) -> NonZeroU32 {
|
||||||
|
NonZeroU32::new(value).unwrap_or_else(|| {
|
||||||
|
tracing::warn!(
|
||||||
|
configured = value,
|
||||||
|
default,
|
||||||
|
"{name} must be non-zero, using default"
|
||||||
|
);
|
||||||
|
NonZeroU32::new(default).unwrap()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct RateLimitState {
|
||||||
|
pub ip_limiter: Arc<IpRateLimiter>,
|
||||||
|
pub global_limiter: Arc<GlobalRateLimiter>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RateLimitState {
|
||||||
|
/// Create a new RateLimitState with default limits.
|
||||||
|
///
|
||||||
|
/// Default limits (can be overridden via environment variables):
|
||||||
|
/// - Global: 100 req/s, burst 200
|
||||||
|
/// - Per-IP: 20 req/s, burst 40
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let global_rate = std::env::var("RATE_LIMIT_GLOBAL_PER_SECOND")
|
||||||
|
.ok()
|
||||||
|
.and_then(|v| v.parse::<u32>().ok())
|
||||||
|
.unwrap_or(100);
|
||||||
|
|
||||||
|
let global_burst = std::env::var("RATE_LIMIT_GLOBAL_BURST")
|
||||||
|
.ok()
|
||||||
|
.and_then(|v| v.parse::<u32>().ok())
|
||||||
|
.unwrap_or(200);
|
||||||
|
|
||||||
|
let ip_rate = std::env::var("RATE_LIMIT_IP_PER_SECOND")
|
||||||
|
.ok()
|
||||||
|
.and_then(|v| v.parse::<u32>().ok())
|
||||||
|
.unwrap_or(20);
|
||||||
|
|
||||||
|
let ip_burst = std::env::var("RATE_LIMIT_IP_BURST")
|
||||||
|
.ok()
|
||||||
|
.and_then(|v| v.parse::<u32>().ok())
|
||||||
|
.unwrap_or(40);
|
||||||
|
|
||||||
|
let global_rate_nz = nz_or_log(global_rate, 100, "RATE_LIMIT_GLOBAL_PER_SECOND");
|
||||||
|
let global_burst_nz = nz_or_log(global_burst, 200, "RATE_LIMIT_GLOBAL_BURST");
|
||||||
|
let ip_rate_nz = nz_or_log(ip_rate, 20, "RATE_LIMIT_IP_PER_SECOND");
|
||||||
|
let ip_burst_nz = nz_or_log(ip_burst, 40, "RATE_LIMIT_IP_BURST");
|
||||||
|
|
||||||
|
let global_quota = Quota::per_second(global_rate_nz).allow_burst(global_burst_nz);
|
||||||
|
let ip_quota = Quota::per_second(ip_rate_nz).allow_burst(ip_burst_nz);
|
||||||
|
|
||||||
|
tracing::info!(
|
||||||
|
global_rate = global_rate_nz.get(),
|
||||||
|
global_burst = global_burst_nz.get(),
|
||||||
|
ip_rate = ip_rate_nz.get(),
|
||||||
|
ip_burst = ip_burst_nz.get(),
|
||||||
|
"rate limiter initialized"
|
||||||
|
);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
global_limiter: Arc::new(RateLimiter::direct(global_quota)),
|
||||||
|
ip_limiter: Arc::new(RateLimiter::dashmap(ip_quota)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Rate limiting middleware function.
|
||||||
|
///
|
||||||
|
/// Checks both global and per-IP rate limits before allowing the request through.
|
||||||
|
/// Returns 429 Too Many Requests if either limit is exceeded.
|
||||||
|
pub async fn rate_limit_middleware(
|
||||||
|
State(rl): State<RateLimitState>,
|
||||||
|
req: Request,
|
||||||
|
next: Next,
|
||||||
|
) -> Result<Response, Response> {
|
||||||
|
// Check global rate limit first
|
||||||
|
if let Err(negative) = rl.global_limiter.check() {
|
||||||
|
let retry_after = negative.wait_time_from(DefaultClock::default().now());
|
||||||
|
tracing::warn!(
|
||||||
|
retry_after_secs = retry_after.as_secs(),
|
||||||
|
"global rate limit exceeded"
|
||||||
|
);
|
||||||
|
return Err(too_many_requests_response(Some(retry_after)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check per-IP rate limit
|
||||||
|
let key = client_ip::extract_client_ip(&req);
|
||||||
|
if let Err(negative) = rl.ip_limiter.check_key(&key) {
|
||||||
|
let retry_after = negative.wait_time_from(DefaultClock::default().now());
|
||||||
|
tracing::warn!(
|
||||||
|
client_ip = %key,
|
||||||
|
retry_after_secs = retry_after.as_secs(),
|
||||||
|
"per-IP rate limit exceeded"
|
||||||
|
);
|
||||||
|
return Err(too_many_requests_response(Some(retry_after)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(next.run(req).await)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start a background task to clean up expired rate limiter entries.
|
||||||
|
///
|
||||||
|
/// This should be called once during application startup.
|
||||||
|
/// The task runs every 60 seconds and will be aborted on shutdown.
|
||||||
|
pub fn spawn_cleanup_task(ip_limiter: Arc<IpRateLimiter>) -> tokio::task::JoinHandle<()> {
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let mut interval = tokio::time::interval(Duration::from_secs(60));
|
||||||
|
loop {
|
||||||
|
interval.tick().await;
|
||||||
|
ip_limiter.retain_recent();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a 429 Too Many Requests response.
|
||||||
|
fn too_many_requests_response(retry_after: Option<Duration>) -> Response {
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert("Content-Type", HeaderValue::from_static("application/json"));
|
||||||
|
|
||||||
|
if let Some(duration) = retry_after {
|
||||||
|
let secs = duration.as_secs().max(1);
|
||||||
|
if let Ok(value) = HeaderValue::from_str(&secs.to_string()) {
|
||||||
|
headers.insert("Retry-After", value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = json!({
|
||||||
|
"error": "Too many requests, please try again later"
|
||||||
|
});
|
||||||
|
|
||||||
|
(StatusCode::TOO_MANY_REQUESTS, headers, body.to_string()).into_response()
|
||||||
|
}
|
||||||
@@ -13,11 +13,155 @@ use rmcp::{
|
|||||||
tool, tool_handler, tool_router,
|
tool, tool_handler, tool_router,
|
||||||
};
|
};
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Deserializer, de};
|
||||||
use serde_json::{Map, Value};
|
use serde_json::{Map, Value};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::validation;
|
||||||
|
|
||||||
|
// ── Serde helpers for numeric parameters that may arrive as strings ──────────
|
||||||
|
|
||||||
|
mod deser {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
/// Deserialize a value that may come as a JSON number or a JSON string.
|
||||||
|
pub fn option_u32_from_string<'de, D>(deserializer: D) -> Result<Option<u32>, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
enum NumOrStr {
|
||||||
|
Num(u32),
|
||||||
|
Str(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
match Option::<NumOrStr>::deserialize(deserializer)? {
|
||||||
|
None => Ok(None),
|
||||||
|
Some(NumOrStr::Num(n)) => Ok(Some(n)),
|
||||||
|
Some(NumOrStr::Str(s)) => {
|
||||||
|
if s.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
s.parse::<u32>().map(Some).map_err(de::Error::custom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deserialize an i64 that may come as a JSON number or a JSON string.
|
||||||
|
pub fn option_i64_from_string<'de, D>(deserializer: D) -> Result<Option<i64>, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
enum NumOrStr {
|
||||||
|
Num(i64),
|
||||||
|
Str(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
match Option::<NumOrStr>::deserialize(deserializer)? {
|
||||||
|
None => Ok(None),
|
||||||
|
Some(NumOrStr::Num(n)) => Ok(Some(n)),
|
||||||
|
Some(NumOrStr::Str(s)) => {
|
||||||
|
if s.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
s.parse::<i64>().map(Some).map_err(de::Error::custom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deserialize a bool that may come as a JSON bool or a JSON string ("true"/"false").
|
||||||
|
pub fn option_bool_from_string<'de, D>(deserializer: D) -> Result<Option<bool>, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
enum BoolOrStr {
|
||||||
|
Bool(bool),
|
||||||
|
Str(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
match Option::<BoolOrStr>::deserialize(deserializer)? {
|
||||||
|
None => Ok(None),
|
||||||
|
Some(BoolOrStr::Bool(b)) => Ok(Some(b)),
|
||||||
|
Some(BoolOrStr::Str(s)) => {
|
||||||
|
if s.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
s.parse::<bool>().map(Some).map_err(de::Error::custom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deserialize a Vec<String> that may come as a JSON array or a JSON string containing an array.
|
||||||
|
pub fn option_vec_string_from_string<'de, D>(
|
||||||
|
deserializer: D,
|
||||||
|
) -> Result<Option<Vec<String>>, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
enum VecOrStr {
|
||||||
|
Vec(Vec<String>),
|
||||||
|
Str(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
match Option::<VecOrStr>::deserialize(deserializer)? {
|
||||||
|
None => Ok(None),
|
||||||
|
Some(VecOrStr::Vec(v)) => Ok(Some(v)),
|
||||||
|
Some(VecOrStr::Str(s)) => {
|
||||||
|
if s.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
serde_json::from_str(&s)
|
||||||
|
.map(Some)
|
||||||
|
.map_err(|e| {
|
||||||
|
de::Error::custom(format!(
|
||||||
|
"invalid string value for array field: expected a JSON array, e.g. '[\"a\",\"b\"]': {e}"
|
||||||
|
))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deserialize a Map<String, Value> that may come as a JSON object or a JSON string containing an object.
|
||||||
|
pub fn option_map_from_string<'de, D>(
|
||||||
|
deserializer: D,
|
||||||
|
) -> Result<Option<Map<String, Value>>, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
enum MapOrStr {
|
||||||
|
Map(Map<String, Value>),
|
||||||
|
Str(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
match Option::<MapOrStr>::deserialize(deserializer)? {
|
||||||
|
None => Ok(None),
|
||||||
|
Some(MapOrStr::Map(m)) => Ok(Some(m)),
|
||||||
|
Some(MapOrStr::Str(s)) => {
|
||||||
|
if s.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
serde_json::from_str(&s)
|
||||||
|
.map(Some)
|
||||||
|
.map_err(|e| {
|
||||||
|
de::Error::custom(format!(
|
||||||
|
"invalid string value for object field: expected a JSON object, e.g. '{{\"key\":\"value\"}}': {e}"
|
||||||
|
))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
use secrets_core::models::ExportFormat;
|
use secrets_core::models::ExportFormat;
|
||||||
use secrets_core::service::{
|
use secrets_core::service::{
|
||||||
add::{AddParams, run as svc_add},
|
add::{AddParams, run as svc_add},
|
||||||
@@ -31,6 +175,7 @@ use secrets_core::service::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::auth::AuthUser;
|
use crate::auth::AuthUser;
|
||||||
|
use crate::error;
|
||||||
|
|
||||||
// ── MCP client-facing errors (no internal details) ───────────────────────────
|
// ── MCP client-facing errors (no internal details) ───────────────────────────
|
||||||
|
|
||||||
@@ -50,6 +195,17 @@ fn mcp_err_internal_logged(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn mcp_err_from_anyhow(
|
||||||
|
tool: &'static str,
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
err: anyhow::Error,
|
||||||
|
) -> rmcp::ErrorData {
|
||||||
|
if let Some(app_err) = err.downcast_ref::<secrets_core::error::AppError>() {
|
||||||
|
return error::app_error_to_mcp(app_err);
|
||||||
|
}
|
||||||
|
mcp_err_internal_logged(tool, user_id, err)
|
||||||
|
}
|
||||||
|
|
||||||
fn mcp_err_invalid_encryption_key_logged(err: impl std::fmt::Display) -> rmcp::ErrorData {
|
fn mcp_err_invalid_encryption_key_logged(err: impl std::fmt::Display) -> rmcp::ErrorData {
|
||||||
tracing::warn!(error = %err, "invalid X-Encryption-Key");
|
tracing::warn!(error = %err, "invalid X-Encryption-Key");
|
||||||
rmcp::ErrorData::invalid_request(
|
rmcp::ErrorData::invalid_request(
|
||||||
@@ -162,14 +318,22 @@ struct FindInput {
|
|||||||
query: Option<String>,
|
query: Option<String>,
|
||||||
#[schemars(description = "Exact folder filter (e.g. 'refining', 'ricnsmart')")]
|
#[schemars(description = "Exact folder filter (e.g. 'refining', 'ricnsmart')")]
|
||||||
folder: Option<String>,
|
folder: Option<String>,
|
||||||
#[schemars(description = "Exact type filter (e.g. 'server', 'service', 'person', 'key')")]
|
#[schemars(
|
||||||
|
description = "Exact type filter (e.g. 'server', 'service', 'account', 'person', 'document'). User-defined, any value accepted."
|
||||||
|
)]
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
entry_type: Option<String>,
|
entry_type: Option<String>,
|
||||||
#[schemars(description = "Exact name filter")]
|
#[schemars(description = "Exact name filter. For fuzzy matching use name_query instead.")]
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
|
#[schemars(
|
||||||
|
description = "Fuzzy name filter (ILIKE, case-insensitive partial match). Use this instead of 'name' when you don't know the exact name."
|
||||||
|
)]
|
||||||
|
name_query: Option<String>,
|
||||||
#[schemars(description = "Tag filters (all must match)")]
|
#[schemars(description = "Tag filters (all must match)")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
tags: Option<Vec<String>>,
|
tags: Option<Vec<String>>,
|
||||||
#[schemars(description = "Max results (default 20)")]
|
#[schemars(description = "Max results (default 20)")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_u32_from_string")]
|
||||||
limit: Option<u32>,
|
limit: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,20 +343,30 @@ struct SearchInput {
|
|||||||
query: Option<String>,
|
query: Option<String>,
|
||||||
#[schemars(description = "Folder filter (e.g. 'refining', 'personal', 'family')")]
|
#[schemars(description = "Folder filter (e.g. 'refining', 'personal', 'family')")]
|
||||||
folder: Option<String>,
|
folder: Option<String>,
|
||||||
#[schemars(description = "Type filter (e.g. 'server', 'service', 'person', 'key')")]
|
#[schemars(
|
||||||
|
description = "Type filter (e.g. 'server', 'service', 'account', 'person', 'document'). User-defined, any value accepted."
|
||||||
|
)]
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
entry_type: Option<String>,
|
entry_type: Option<String>,
|
||||||
#[schemars(description = "Exact name to match")]
|
#[schemars(description = "Exact name to match. For fuzzy matching use name_query instead.")]
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
|
#[schemars(
|
||||||
|
description = "Fuzzy name filter (ILIKE, case-insensitive partial match). Use this instead of 'name' when you don't know the exact name."
|
||||||
|
)]
|
||||||
|
name_query: Option<String>,
|
||||||
#[schemars(description = "Tag filters (all must match)")]
|
#[schemars(description = "Tag filters (all must match)")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
tags: Option<Vec<String>>,
|
tags: Option<Vec<String>>,
|
||||||
#[schemars(description = "Return only summary fields (name/tags/notes/updated_at)")]
|
#[schemars(description = "Return only summary fields (name/tags/notes/updated_at)")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_bool_from_string")]
|
||||||
summary: Option<bool>,
|
summary: Option<bool>,
|
||||||
#[schemars(description = "Sort order: 'name' (default), 'updated', 'created'")]
|
#[schemars(description = "Sort order: 'name' (default), 'updated', 'created'")]
|
||||||
sort: Option<String>,
|
sort: Option<String>,
|
||||||
#[schemars(description = "Max results (default 20)")]
|
#[schemars(description = "Max results (default 20)")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_u32_from_string")]
|
||||||
limit: Option<u32>,
|
limit: Option<u32>,
|
||||||
#[schemars(description = "Pagination offset (default 0)")]
|
#[schemars(description = "Pagination offset (default 0)")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_u32_from_string")]
|
||||||
offset: Option<u32>,
|
offset: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,26 +385,43 @@ struct AddInput {
|
|||||||
#[schemars(description = "Folder for organization (optional, e.g. 'personal', 'refining')")]
|
#[schemars(description = "Folder for organization (optional, e.g. 'personal', 'refining')")]
|
||||||
folder: Option<String>,
|
folder: Option<String>,
|
||||||
#[schemars(
|
#[schemars(
|
||||||
description = "Type/category of this entry (optional, e.g. 'server', 'person', 'key')"
|
description = "Type/category of this entry (optional, e.g. 'server', 'service', 'account', 'person', 'document'). Free-form, choose what best describes the entry."
|
||||||
)]
|
)]
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
entry_type: Option<String>,
|
entry_type: Option<String>,
|
||||||
#[schemars(description = "Free-text notes for this entry (optional)")]
|
#[schemars(description = "Free-text notes for this entry (optional)")]
|
||||||
notes: Option<String>,
|
notes: Option<String>,
|
||||||
#[schemars(description = "Tags for this entry")]
|
#[schemars(description = "Tags for this entry")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
tags: Option<Vec<String>>,
|
tags: Option<Vec<String>>,
|
||||||
#[schemars(description = "Metadata fields as 'key=value' or 'key:=json' strings")]
|
#[schemars(description = "Metadata fields as 'key=value' or 'key:=json' strings")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
meta: Option<Vec<String>>,
|
meta: Option<Vec<String>>,
|
||||||
#[schemars(
|
#[schemars(
|
||||||
description = "Metadata fields as a JSON object {\"key\": value}. Merged with 'meta' if both provided."
|
description = "Metadata fields as a JSON object {\"key\": value}. Merged with 'meta' if both provided."
|
||||||
)]
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_map_from_string")]
|
||||||
meta_obj: Option<Map<String, Value>>,
|
meta_obj: Option<Map<String, Value>>,
|
||||||
#[schemars(description = "Secret fields as 'key=value' strings")]
|
#[schemars(
|
||||||
|
description = "Secret fields as 'key=value' strings. Reminder: non-sensitive endpoint/address fields should go to metadata.address instead of secrets."
|
||||||
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
secrets: Option<Vec<String>>,
|
secrets: Option<Vec<String>>,
|
||||||
#[schemars(
|
#[schemars(
|
||||||
description = "Secret fields as a JSON object {\"key\": \"value\"}. Merged with 'secrets' if both provided."
|
description = "Secret fields as a JSON object {\"key\": \"value\"}. Merged with 'secrets' if both provided. Reminder: non-sensitive endpoint/address fields should go to metadata.address."
|
||||||
)]
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_map_from_string")]
|
||||||
secrets_obj: Option<Map<String, Value>>,
|
secrets_obj: Option<Map<String, Value>>,
|
||||||
|
#[schemars(
|
||||||
|
description = "Secret types as {\"secret_name\": \"type\"}. Keys must match secret field names. Missing keys default to \"text\"."
|
||||||
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_map_from_string")]
|
||||||
|
secret_types: Option<Map<String, Value>>,
|
||||||
|
#[schemars(
|
||||||
|
description = "Link existing secrets by secret name. Names must resolve uniquely under current user."
|
||||||
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
|
link_secret_names: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, JsonSchema)]
|
#[derive(Debug, Deserialize, JsonSchema)]
|
||||||
@@ -248,25 +439,50 @@ struct UpdateInput {
|
|||||||
#[schemars(description = "Update the notes field")]
|
#[schemars(description = "Update the notes field")]
|
||||||
notes: Option<String>,
|
notes: Option<String>,
|
||||||
#[schemars(description = "Tags to add")]
|
#[schemars(description = "Tags to add")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
add_tags: Option<Vec<String>>,
|
add_tags: Option<Vec<String>>,
|
||||||
#[schemars(description = "Tags to remove")]
|
#[schemars(description = "Tags to remove")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
remove_tags: Option<Vec<String>>,
|
remove_tags: Option<Vec<String>>,
|
||||||
#[schemars(description = "Metadata fields to update/add as 'key=value' strings")]
|
#[schemars(description = "Metadata fields to update/add as 'key=value' strings")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
meta: Option<Vec<String>>,
|
meta: Option<Vec<String>>,
|
||||||
#[schemars(
|
#[schemars(
|
||||||
description = "Metadata fields to update/add as a JSON object {\"key\": value}. Merged with 'meta' if both provided."
|
description = "Metadata fields to update/add as a JSON object {\"key\": value}. Merged with 'meta' if both provided."
|
||||||
)]
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_map_from_string")]
|
||||||
meta_obj: Option<Map<String, Value>>,
|
meta_obj: Option<Map<String, Value>>,
|
||||||
#[schemars(description = "Metadata field keys to remove")]
|
#[schemars(description = "Metadata field keys to remove")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
remove_meta: Option<Vec<String>>,
|
remove_meta: Option<Vec<String>>,
|
||||||
#[schemars(description = "Secret fields to update/add as 'key=value' strings")]
|
#[schemars(
|
||||||
|
description = "Secret fields to update/add as 'key=value' strings. Reminder: non-sensitive endpoint/address fields should go to metadata.address instead of secrets."
|
||||||
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
secrets: Option<Vec<String>>,
|
secrets: Option<Vec<String>>,
|
||||||
#[schemars(
|
#[schemars(
|
||||||
description = "Secret fields to update/add as a JSON object {\"key\": \"value\"}. Merged with 'secrets' if both provided."
|
description = "Secret fields to update/add as a JSON object {\"key\": \"value\"}. Merged with 'secrets' if both provided. Reminder: non-sensitive endpoint/address fields should go to metadata.address."
|
||||||
)]
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_map_from_string")]
|
||||||
secrets_obj: Option<Map<String, Value>>,
|
secrets_obj: Option<Map<String, Value>>,
|
||||||
|
#[schemars(
|
||||||
|
description = "Secret types as {\"secret_name\": \"type\"}. Keys must match secret field names. Missing keys default to \"text\"."
|
||||||
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_map_from_string")]
|
||||||
|
secret_types: Option<Map<String, Value>>,
|
||||||
#[schemars(description = "Secret field keys to remove")]
|
#[schemars(description = "Secret field keys to remove")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
remove_secrets: Option<Vec<String>>,
|
remove_secrets: Option<Vec<String>>,
|
||||||
|
#[schemars(
|
||||||
|
description = "Link existing secrets by name to this entry. Names must resolve uniquely under current user."
|
||||||
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
|
link_secret_names: Option<Vec<String>>,
|
||||||
|
#[schemars(
|
||||||
|
description = "Unlink secrets by name from this entry. Orphaned secrets are auto-deleted."
|
||||||
|
)]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
|
unlink_secret_names: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, JsonSchema)]
|
#[derive(Debug, Deserialize, JsonSchema)]
|
||||||
@@ -285,6 +501,7 @@ struct DeleteInput {
|
|||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
entry_type: Option<String>,
|
entry_type: Option<String>,
|
||||||
#[schemars(description = "Preview deletions without writing")]
|
#[schemars(description = "Preview deletions without writing")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_bool_from_string")]
|
||||||
dry_run: Option<bool>,
|
dry_run: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,6 +518,7 @@ struct HistoryInput {
|
|||||||
)]
|
)]
|
||||||
id: Option<String>,
|
id: Option<String>,
|
||||||
#[schemars(description = "Max history entries to return (default 20)")]
|
#[schemars(description = "Max history entries to return (default 20)")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_u32_from_string")]
|
||||||
limit: Option<u32>,
|
limit: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,6 +535,7 @@ struct RollbackInput {
|
|||||||
)]
|
)]
|
||||||
id: Option<String>,
|
id: Option<String>,
|
||||||
#[schemars(description = "Target version number. Omit to restore the most recent snapshot.")]
|
#[schemars(description = "Target version number. Omit to restore the most recent snapshot.")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_i64_from_string")]
|
||||||
to_version: Option<i64>,
|
to_version: Option<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,6 +549,7 @@ struct ExportInput {
|
|||||||
#[schemars(description = "Exact name filter")]
|
#[schemars(description = "Exact name filter")]
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
#[schemars(description = "Tag filters")]
|
#[schemars(description = "Tag filters")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
tags: Option<Vec<String>>,
|
tags: Option<Vec<String>>,
|
||||||
#[schemars(description = "Fuzzy query")]
|
#[schemars(description = "Fuzzy query")]
|
||||||
query: Option<String>,
|
query: Option<String>,
|
||||||
@@ -347,8 +567,10 @@ struct EnvMapInput {
|
|||||||
#[schemars(description = "Exact name filter")]
|
#[schemars(description = "Exact name filter")]
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
#[schemars(description = "Tag filters")]
|
#[schemars(description = "Tag filters")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
tags: Option<Vec<String>>,
|
tags: Option<Vec<String>>,
|
||||||
#[schemars(description = "Only include these secret fields")]
|
#[schemars(description = "Only include these secret fields")]
|
||||||
|
#[serde(default, deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
only_fields: Option<Vec<String>>,
|
only_fields: Option<Vec<String>>,
|
||||||
#[schemars(description = "Environment variable name prefix. \
|
#[schemars(description = "Environment variable name prefix. \
|
||||||
Variable names are built as UPPER(prefix)_UPPER(entry_name)_UPPER(field_name), \
|
Variable names are built as UPPER(prefix)_UPPER(entry_name)_UPPER(field_name), \
|
||||||
@@ -373,6 +595,44 @@ fn map_to_kv_strings(map: Map<String, Value>) -> Vec<String> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check if any KV string would trigger a server-side file read.
|
||||||
|
///
|
||||||
|
/// `parse_kv` in secrets-core supports two file-read syntaxes:
|
||||||
|
/// - `key=@path` (has `=`, value starts with `@`)
|
||||||
|
/// - `key@path` (no `=`, split on `@`)
|
||||||
|
///
|
||||||
|
/// Both are legitimate for CLI usage but must be rejected in the MCP context
|
||||||
|
/// where the server process runs remotely and the caller controls the path.
|
||||||
|
///
|
||||||
|
/// Note: `key:=json` is intentionally skipped here. Although the value may
|
||||||
|
/// contain `@` characters (e.g. `config:=@/etc/passwd`), the `:=` branch in
|
||||||
|
/// `parse_kv` treats the right-hand side as raw JSON and never performs file
|
||||||
|
/// reads. The `@` in such cases is just data, not a file reference.
|
||||||
|
fn contains_file_reference(entries: &[String]) -> Option<String> {
|
||||||
|
for entry in entries {
|
||||||
|
// key:=json — safe, skip before checking for `=`
|
||||||
|
if entry.contains(":=") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// key=@path
|
||||||
|
if let Some((_, value)) = entry.split_once('=') {
|
||||||
|
if value.starts_with('@') {
|
||||||
|
return Some(entry.clone());
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// key@path (no `=` present)
|
||||||
|
// parse_kv treats entries without `=` that contain `@` as file-read
|
||||||
|
// syntax (key@path). This includes strings like "user@example.com"
|
||||||
|
// if passed without a `=` separator — which is correct to reject here
|
||||||
|
// since the MCP server runs remotely and cannot read local files.
|
||||||
|
if entry.contains('@') {
|
||||||
|
return Some(entry.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse a UUID string, returning an MCP error on failure.
|
/// Parse a UUID string, returning an MCP error on failure.
|
||||||
fn parse_uuid(s: &str) -> Result<Uuid, rmcp::ErrorData> {
|
fn parse_uuid(s: &str) -> Result<Uuid, rmcp::ErrorData> {
|
||||||
s.parse::<Uuid>()
|
s.parse::<Uuid>()
|
||||||
@@ -404,6 +664,7 @@ impl SecretsService {
|
|||||||
folder = input.folder.as_deref(),
|
folder = input.folder.as_deref(),
|
||||||
entry_type = input.entry_type.as_deref(),
|
entry_type = input.entry_type.as_deref(),
|
||||||
name = input.name.as_deref(),
|
name = input.name.as_deref(),
|
||||||
|
name_query = input.name_query.as_deref(),
|
||||||
query = input.query.as_deref(),
|
query = input.query.as_deref(),
|
||||||
"tool call start",
|
"tool call start",
|
||||||
);
|
);
|
||||||
@@ -414,6 +675,7 @@ impl SecretsService {
|
|||||||
folder: input.folder.as_deref(),
|
folder: input.folder.as_deref(),
|
||||||
entry_type: input.entry_type.as_deref(),
|
entry_type: input.entry_type.as_deref(),
|
||||||
name: input.name.as_deref(),
|
name: input.name.as_deref(),
|
||||||
|
name_query: input.name_query.as_deref(),
|
||||||
tags: &tags,
|
tags: &tags,
|
||||||
query: input.query.as_deref(),
|
query: input.query.as_deref(),
|
||||||
sort: "name",
|
sort: "name",
|
||||||
@@ -429,10 +691,20 @@ impl SecretsService {
|
|||||||
.entries
|
.entries
|
||||||
.iter()
|
.iter()
|
||||||
.map(|e| {
|
.map(|e| {
|
||||||
let schema: Vec<&str> = result
|
let schema: Vec<serde_json::Value> = result
|
||||||
.secret_schemas
|
.secret_schemas
|
||||||
.get(&e.id)
|
.get(&e.id)
|
||||||
.map(|f| f.iter().map(|s| s.field_name.as_str()).collect())
|
.map(|f| {
|
||||||
|
f.iter()
|
||||||
|
.map(|s| {
|
||||||
|
serde_json::json!({
|
||||||
|
"id": s.id,
|
||||||
|
"name": s.name,
|
||||||
|
"type": s.secret_type,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"id": e.id,
|
"id": e.id,
|
||||||
@@ -481,6 +753,7 @@ impl SecretsService {
|
|||||||
folder = input.folder.as_deref(),
|
folder = input.folder.as_deref(),
|
||||||
entry_type = input.entry_type.as_deref(),
|
entry_type = input.entry_type.as_deref(),
|
||||||
name = input.name.as_deref(),
|
name = input.name.as_deref(),
|
||||||
|
name_query = input.name_query.as_deref(),
|
||||||
query = input.query.as_deref(),
|
query = input.query.as_deref(),
|
||||||
"tool call start",
|
"tool call start",
|
||||||
);
|
);
|
||||||
@@ -491,6 +764,7 @@ impl SecretsService {
|
|||||||
folder: input.folder.as_deref(),
|
folder: input.folder.as_deref(),
|
||||||
entry_type: input.entry_type.as_deref(),
|
entry_type: input.entry_type.as_deref(),
|
||||||
name: input.name.as_deref(),
|
name: input.name.as_deref(),
|
||||||
|
name_query: input.name_query.as_deref(),
|
||||||
tags: &tags,
|
tags: &tags,
|
||||||
query: input.query.as_deref(),
|
query: input.query.as_deref(),
|
||||||
sort: input.sort.as_deref().unwrap_or("name"),
|
sort: input.sort.as_deref().unwrap_or("name"),
|
||||||
@@ -517,10 +791,20 @@ impl SecretsService {
|
|||||||
"updated_at": e.updated_at.format("%Y-%m-%dT%H:%M:%SZ").to_string(),
|
"updated_at": e.updated_at.format("%Y-%m-%dT%H:%M:%SZ").to_string(),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let schema: Vec<&str> = result
|
let schema: Vec<serde_json::Value> = result
|
||||||
.secret_schemas
|
.secret_schemas
|
||||||
.get(&e.id)
|
.get(&e.id)
|
||||||
.map(|f| f.iter().map(|s| s.field_name.as_str()).collect())
|
.map(|f| {
|
||||||
|
f.iter()
|
||||||
|
.map(|s| {
|
||||||
|
serde_json::json!({
|
||||||
|
"id": s.id,
|
||||||
|
"name": s.name,
|
||||||
|
"type": s.secret_type,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"id": e.id,
|
"id": e.id,
|
||||||
@@ -579,7 +863,7 @@ impl SecretsService {
|
|||||||
let value =
|
let value =
|
||||||
get_secret_field_by_id(&self.pool, entry_id, field_name, &user_key, Some(user_id))
|
get_secret_field_by_id(&self.pool, entry_id, field_name, &user_key, Some(user_id))
|
||||||
.await
|
.await
|
||||||
.map_err(|e| mcp_err_internal_logged("secrets_get", None, e))?;
|
.map_err(|e| mcp_err_from_anyhow("secrets_get", Some(user_id), e))?;
|
||||||
|
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
tool = "secrets_get",
|
tool = "secrets_get",
|
||||||
@@ -593,7 +877,7 @@ impl SecretsService {
|
|||||||
} else {
|
} else {
|
||||||
let secrets = get_all_secrets_by_id(&self.pool, entry_id, &user_key, Some(user_id))
|
let secrets = get_all_secrets_by_id(&self.pool, entry_id, &user_key, Some(user_id))
|
||||||
.await
|
.await
|
||||||
.map_err(|e| mcp_err_internal_logged("secrets_get", None, e))?;
|
.map_err(|e| mcp_err_from_anyhow("secrets_get", Some(user_id), e))?;
|
||||||
|
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
tool = "secrets_get",
|
tool = "secrets_get",
|
||||||
@@ -635,10 +919,39 @@ impl SecretsService {
|
|||||||
if let Some(obj) = input.meta_obj {
|
if let Some(obj) = input.meta_obj {
|
||||||
meta.extend(map_to_kv_strings(obj));
|
meta.extend(map_to_kv_strings(obj));
|
||||||
}
|
}
|
||||||
|
if let Some(offending) = contains_file_reference(&meta) {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!("@file syntax is not allowed in MCP tools: '{}'", offending),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
let mut secrets = input.secrets.unwrap_or_default();
|
let mut secrets = input.secrets.unwrap_or_default();
|
||||||
if let Some(obj) = input.secrets_obj {
|
if let Some(obj) = input.secrets_obj {
|
||||||
secrets.extend(map_to_kv_strings(obj));
|
secrets.extend(map_to_kv_strings(obj));
|
||||||
}
|
}
|
||||||
|
if let Some(offending) = contains_file_reference(&secrets) {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!("@file syntax is not allowed in MCP tools: '{}'", offending),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Input length validation
|
||||||
|
validation::validate_input_lengths(
|
||||||
|
&input.name,
|
||||||
|
input.folder.as_deref(),
|
||||||
|
input.entry_type.as_deref(),
|
||||||
|
input.notes.as_deref(),
|
||||||
|
)?;
|
||||||
|
validation::validate_tags(&tags)?;
|
||||||
|
validation::validate_meta_entries(&meta)?;
|
||||||
|
|
||||||
|
let secret_types = input.secret_types.unwrap_or_default();
|
||||||
|
let secret_types_map: std::collections::HashMap<String, String> = secret_types
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|(k, v)| v.as_str().map(|s| (k, s.to_string())))
|
||||||
|
.collect();
|
||||||
|
let link_secret_names = input.link_secret_names.unwrap_or_default();
|
||||||
let folder = input.folder.as_deref().unwrap_or("");
|
let folder = input.folder.as_deref().unwrap_or("");
|
||||||
let entry_type = input.entry_type.as_deref().unwrap_or("");
|
let entry_type = input.entry_type.as_deref().unwrap_or("");
|
||||||
let notes = input.notes.as_deref().unwrap_or("");
|
let notes = input.notes.as_deref().unwrap_or("");
|
||||||
@@ -653,12 +966,14 @@ impl SecretsService {
|
|||||||
tags: &tags,
|
tags: &tags,
|
||||||
meta_entries: &meta,
|
meta_entries: &meta,
|
||||||
secret_entries: &secrets,
|
secret_entries: &secrets,
|
||||||
|
secret_types: &secret_types_map,
|
||||||
|
link_secret_names: &link_secret_names,
|
||||||
user_id: Some(user_id),
|
user_id: Some(user_id),
|
||||||
},
|
},
|
||||||
&user_key,
|
&user_key,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| mcp_err_internal_logged("secrets_add", Some(user_id), e))?;
|
.map_err(|e| mcp_err_from_anyhow("secrets_add", Some(user_id), e))?;
|
||||||
|
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
tool = "secrets_add",
|
tool = "secrets_add",
|
||||||
@@ -710,12 +1025,42 @@ impl SecretsService {
|
|||||||
if let Some(obj) = input.meta_obj {
|
if let Some(obj) = input.meta_obj {
|
||||||
meta.extend(map_to_kv_strings(obj));
|
meta.extend(map_to_kv_strings(obj));
|
||||||
}
|
}
|
||||||
|
if let Some(offending) = contains_file_reference(&meta) {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!("@file syntax is not allowed in MCP tools: '{}'", offending),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
let remove_meta = input.remove_meta.unwrap_or_default();
|
let remove_meta = input.remove_meta.unwrap_or_default();
|
||||||
let mut secrets = input.secrets.unwrap_or_default();
|
let mut secrets = input.secrets.unwrap_or_default();
|
||||||
if let Some(obj) = input.secrets_obj {
|
if let Some(obj) = input.secrets_obj {
|
||||||
secrets.extend(map_to_kv_strings(obj));
|
secrets.extend(map_to_kv_strings(obj));
|
||||||
}
|
}
|
||||||
|
if let Some(offending) = contains_file_reference(&secrets) {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!("@file syntax is not allowed in MCP tools: '{}'", offending),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Input length validation
|
||||||
|
validation::validate_input_lengths(
|
||||||
|
&input.name,
|
||||||
|
input.folder.as_deref(),
|
||||||
|
None,
|
||||||
|
input.notes.as_deref(),
|
||||||
|
)?;
|
||||||
|
validation::validate_tags(&add_tags)?;
|
||||||
|
validation::validate_meta_entries(&meta)?;
|
||||||
|
|
||||||
|
let secret_types = input.secret_types.unwrap_or_default();
|
||||||
|
let secret_types_map: std::collections::HashMap<String, String> = secret_types
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|(k, v)| v.as_str().map(|s| (k, s.to_string())))
|
||||||
|
.collect();
|
||||||
let remove_secrets = input.remove_secrets.unwrap_or_default();
|
let remove_secrets = input.remove_secrets.unwrap_or_default();
|
||||||
|
let link_secret_names = input.link_secret_names.unwrap_or_default();
|
||||||
|
let unlink_secret_names = input.unlink_secret_names.unwrap_or_default();
|
||||||
|
|
||||||
let result = svc_update(
|
let result = svc_update(
|
||||||
&self.pool,
|
&self.pool,
|
||||||
@@ -728,13 +1073,16 @@ impl SecretsService {
|
|||||||
meta_entries: &meta,
|
meta_entries: &meta,
|
||||||
remove_meta: &remove_meta,
|
remove_meta: &remove_meta,
|
||||||
secret_entries: &secrets,
|
secret_entries: &secrets,
|
||||||
|
secret_types: &secret_types_map,
|
||||||
remove_secrets: &remove_secrets,
|
remove_secrets: &remove_secrets,
|
||||||
|
link_secret_names: &link_secret_names,
|
||||||
|
unlink_secret_names: &unlink_secret_names,
|
||||||
user_id: Some(user_id),
|
user_id: Some(user_id),
|
||||||
},
|
},
|
||||||
&user_key,
|
&user_key,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| mcp_err_internal_logged("secrets_update", Some(user_id), e))?;
|
.map_err(|e| mcp_err_from_anyhow("secrets_update", Some(user_id), e))?;
|
||||||
|
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
tool = "secrets_update",
|
tool = "secrets_update",
|
||||||
@@ -970,7 +1318,7 @@ impl SecretsService {
|
|||||||
Some(&user_key),
|
Some(&user_key),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| mcp_err_internal_logged("secrets_export", Some(user_id), e))?;
|
.map_err(|e| mcp_err_from_anyhow("secrets_export", Some(user_id), e))?;
|
||||||
|
|
||||||
let fmt = format.parse::<ExportFormat>().map_err(|e| {
|
let fmt = format.parse::<ExportFormat>().map_err(|e| {
|
||||||
tracing::warn!(
|
tracing::warn!(
|
||||||
@@ -986,7 +1334,7 @@ impl SecretsService {
|
|||||||
})?;
|
})?;
|
||||||
let serialized = fmt
|
let serialized = fmt
|
||||||
.serialize(&data)
|
.serialize(&data)
|
||||||
.map_err(|e| mcp_err_internal_logged("secrets_export", Some(user_id), e))?;
|
.map_err(|e| mcp_err_from_anyhow("secrets_export", Some(user_id), e))?;
|
||||||
|
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
tool = "secrets_export",
|
tool = "secrets_export",
|
||||||
@@ -1037,7 +1385,7 @@ impl SecretsService {
|
|||||||
Some(user_id),
|
Some(user_id),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| mcp_err_internal_logged("secrets_env_map", Some(user_id), e))?;
|
.map_err(|e| mcp_err_from_anyhow("secrets_env_map", Some(user_id), e))?;
|
||||||
|
|
||||||
let entry_count = env_map.len();
|
let entry_count = env_map.len();
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
@@ -1137,3 +1485,202 @@ impl ServerHandler for SecretsService {
|
|||||||
info
|
info
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod deser_tests {
|
||||||
|
use super::deser;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct TestU32 {
|
||||||
|
#[serde(deserialize_with = "deser::option_u32_from_string")]
|
||||||
|
val: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct TestI64 {
|
||||||
|
#[serde(deserialize_with = "deser::option_i64_from_string")]
|
||||||
|
val: Option<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct TestBool {
|
||||||
|
#[serde(deserialize_with = "deser::option_bool_from_string")]
|
||||||
|
val: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct TestVec {
|
||||||
|
#[serde(deserialize_with = "deser::option_vec_string_from_string")]
|
||||||
|
val: Option<Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct TestMap {
|
||||||
|
#[serde(deserialize_with = "deser::option_map_from_string")]
|
||||||
|
val: Option<serde_json::Map<String, serde_json::Value>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// option_u32_from_string
|
||||||
|
#[test]
|
||||||
|
fn u32_native_number() {
|
||||||
|
let v: TestU32 = serde_json::from_value(json!({"val": 42})).unwrap();
|
||||||
|
assert_eq!(v.val, Some(42));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn u32_string_number() {
|
||||||
|
let v: TestU32 = serde_json::from_value(json!({"val": "42"})).unwrap();
|
||||||
|
assert_eq!(v.val, Some(42));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn u32_empty_string() {
|
||||||
|
let v: TestU32 = serde_json::from_value(json!({"val": ""})).unwrap();
|
||||||
|
assert_eq!(v.val, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn u32_none() {
|
||||||
|
let v: TestU32 = serde_json::from_value(json!({"val": null})).unwrap();
|
||||||
|
assert_eq!(v.val, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
// option_i64_from_string
|
||||||
|
#[test]
|
||||||
|
fn i64_native_number() {
|
||||||
|
let v: TestI64 = serde_json::from_value(json!({"val": -100})).unwrap();
|
||||||
|
assert_eq!(v.val, Some(-100));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn i64_string_number() {
|
||||||
|
let v: TestI64 = serde_json::from_value(json!({"val": "999"})).unwrap();
|
||||||
|
assert_eq!(v.val, Some(999));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn i64_empty_string() {
|
||||||
|
let v: TestI64 = serde_json::from_value(json!({"val": ""})).unwrap();
|
||||||
|
assert_eq!(v.val, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn i64_none() {
|
||||||
|
let v: TestI64 = serde_json::from_value(json!({"val": null})).unwrap();
|
||||||
|
assert_eq!(v.val, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
// option_bool_from_string
|
||||||
|
#[test]
|
||||||
|
fn bool_native_true() {
|
||||||
|
let v: TestBool = serde_json::from_value(json!({"val": true})).unwrap();
|
||||||
|
assert_eq!(v.val, Some(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bool_native_false() {
|
||||||
|
let v: TestBool = serde_json::from_value(json!({"val": false})).unwrap();
|
||||||
|
assert_eq!(v.val, Some(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bool_string_true() {
|
||||||
|
let v: TestBool = serde_json::from_value(json!({"val": "true"})).unwrap();
|
||||||
|
assert_eq!(v.val, Some(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bool_string_false() {
|
||||||
|
let v: TestBool = serde_json::from_value(json!({"val": "false"})).unwrap();
|
||||||
|
assert_eq!(v.val, Some(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bool_empty_string() {
|
||||||
|
let v: TestBool = serde_json::from_value(json!({"val": ""})).unwrap();
|
||||||
|
assert_eq!(v.val, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bool_none() {
|
||||||
|
let v: TestBool = serde_json::from_value(json!({"val": null})).unwrap();
|
||||||
|
assert_eq!(v.val, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
// option_vec_string_from_string
|
||||||
|
#[test]
|
||||||
|
fn vec_native_array() {
|
||||||
|
let v: TestVec = serde_json::from_value(json!({"val": ["a", "b"]})).unwrap();
|
||||||
|
assert_eq!(v.val, Some(vec!["a".to_string(), "b".to_string()]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vec_json_string_array() {
|
||||||
|
let v: TestVec = serde_json::from_value(json!({"val": "[\"x\",\"y\"]"})).unwrap();
|
||||||
|
assert_eq!(v.val, Some(vec!["x".to_string(), "y".to_string()]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vec_empty_string() {
|
||||||
|
let v: TestVec = serde_json::from_value(json!({"val": ""})).unwrap();
|
||||||
|
assert_eq!(v.val, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vec_none() {
|
||||||
|
let v: TestVec = serde_json::from_value(json!({"val": null})).unwrap();
|
||||||
|
assert_eq!(v.val, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vec_invalid_string_errors() {
|
||||||
|
let err = serde_json::from_value::<TestVec>(json!({"val": "not-json"}))
|
||||||
|
.expect_err("should fail on invalid JSON");
|
||||||
|
let msg = err.to_string();
|
||||||
|
assert!(msg.contains("invalid string value for array field"));
|
||||||
|
assert!(msg.contains("expected a JSON array"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// option_map_from_string
|
||||||
|
#[test]
|
||||||
|
fn map_native_object() {
|
||||||
|
let v: TestMap = serde_json::from_value(json!({"val": {"key": "value"}})).unwrap();
|
||||||
|
assert!(v.val.is_some());
|
||||||
|
let m = v.val.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
m.get("key"),
|
||||||
|
Some(&serde_json::Value::String("value".to_string()))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn map_json_string_object() {
|
||||||
|
let v: TestMap = serde_json::from_value(json!({"val": "{\"a\":1}"})).unwrap();
|
||||||
|
assert!(v.val.is_some());
|
||||||
|
let m = v.val.unwrap();
|
||||||
|
assert_eq!(m.get("a"), Some(&serde_json::Value::Number(1.into())));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn map_empty_string() {
|
||||||
|
let v: TestMap = serde_json::from_value(json!({"val": ""})).unwrap();
|
||||||
|
assert_eq!(v.val, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn map_none() {
|
||||||
|
let v: TestMap = serde_json::from_value(json!({"val": null})).unwrap();
|
||||||
|
assert_eq!(v.val, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn map_invalid_string_errors() {
|
||||||
|
let err = serde_json::from_value::<TestMap>(json!({"val": "not-json"}))
|
||||||
|
.expect_err("should fail on invalid JSON");
|
||||||
|
let msg = err.to_string();
|
||||||
|
assert!(msg.contains("invalid string value for object field"));
|
||||||
|
assert!(msg.contains("expected a JSON object"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
149
crates/secrets-mcp/src/validation.rs
Normal file
149
crates/secrets-mcp/src/validation.rs
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
/// Validation constants for input field lengths.
|
||||||
|
pub const MAX_NAME_LENGTH: usize = 256;
|
||||||
|
pub const MAX_FOLDER_LENGTH: usize = 128;
|
||||||
|
pub const MAX_ENTRY_TYPE_LENGTH: usize = 64;
|
||||||
|
pub const MAX_NOTES_LENGTH: usize = 10000;
|
||||||
|
pub const MAX_TAG_LENGTH: usize = 64;
|
||||||
|
pub const MAX_TAG_COUNT: usize = 50;
|
||||||
|
pub const MAX_META_KEY_LENGTH: usize = 128;
|
||||||
|
pub const MAX_META_VALUE_LENGTH: usize = 4096;
|
||||||
|
pub const MAX_META_COUNT: usize = 100;
|
||||||
|
|
||||||
|
/// Validate input field lengths for MCP tools.
|
||||||
|
///
|
||||||
|
/// Returns an error if any field exceeds its maximum length.
|
||||||
|
pub fn validate_input_lengths(
|
||||||
|
name: &str,
|
||||||
|
folder: Option<&str>,
|
||||||
|
entry_type: Option<&str>,
|
||||||
|
notes: Option<&str>,
|
||||||
|
) -> Result<(), rmcp::ErrorData> {
|
||||||
|
if name.chars().count() > MAX_NAME_LENGTH {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!("name must be at most {} characters", MAX_NAME_LENGTH),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if let Some(folder) = folder
|
||||||
|
&& folder.chars().count() > MAX_FOLDER_LENGTH
|
||||||
|
{
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!("folder must be at most {} characters", MAX_FOLDER_LENGTH),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if let Some(entry_type) = entry_type
|
||||||
|
&& entry_type.chars().count() > MAX_ENTRY_TYPE_LENGTH
|
||||||
|
{
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!("type must be at most {} characters", MAX_ENTRY_TYPE_LENGTH),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if let Some(notes) = notes
|
||||||
|
&& notes.chars().count() > MAX_NOTES_LENGTH
|
||||||
|
{
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!("notes must be at most {} characters", MAX_NOTES_LENGTH),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate the tags list.
|
||||||
|
///
|
||||||
|
/// Checks total count and per-tag character length.
|
||||||
|
pub fn validate_tags(tags: &[String]) -> Result<(), rmcp::ErrorData> {
|
||||||
|
if tags.len() > MAX_TAG_COUNT {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!("at most {} tags are allowed", MAX_TAG_COUNT),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
for tag in tags {
|
||||||
|
if tag.chars().count() > MAX_TAG_LENGTH {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!(
|
||||||
|
"tag '{}' exceeds the maximum length of {} characters",
|
||||||
|
tag, MAX_TAG_LENGTH
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate metadata KV strings (key=value / key:=json format).
|
||||||
|
///
|
||||||
|
/// Checks total count and per-key/per-value character lengths.
|
||||||
|
/// This is a best-effort check on the raw KV strings before parsing;
|
||||||
|
/// keys containing `:` path separators are checked as a whole.
|
||||||
|
pub fn validate_meta_entries(entries: &[String]) -> Result<(), rmcp::ErrorData> {
|
||||||
|
if entries.len() > MAX_META_COUNT {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!("at most {} metadata entries are allowed", MAX_META_COUNT),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
for entry in entries {
|
||||||
|
// key:=json — check both key and JSON value length
|
||||||
|
if let Some((key, value)) = entry.split_once(":=") {
|
||||||
|
if key.chars().count() > MAX_META_KEY_LENGTH {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!(
|
||||||
|
"metadata key '{}' exceeds the maximum length of {} characters",
|
||||||
|
key, MAX_META_KEY_LENGTH
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if value.chars().count() > MAX_META_VALUE_LENGTH {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!(
|
||||||
|
"metadata JSON value for key '{}' exceeds the maximum length of {} characters",
|
||||||
|
key, MAX_META_VALUE_LENGTH
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// key=value or key@path
|
||||||
|
if let Some((key, value)) = entry.split_once('=') {
|
||||||
|
if key.chars().count() > MAX_META_KEY_LENGTH {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!(
|
||||||
|
"metadata key '{}' exceeds the maximum length of {} characters",
|
||||||
|
key, MAX_META_KEY_LENGTH
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if value.chars().count() > MAX_META_VALUE_LENGTH {
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!(
|
||||||
|
"metadata value for key '{}' exceeds the maximum length of {} characters",
|
||||||
|
key, MAX_META_VALUE_LENGTH
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Fallback: entry without = or := — check total length
|
||||||
|
let max_total = MAX_META_KEY_LENGTH + MAX_META_VALUE_LENGTH;
|
||||||
|
if entry.chars().count() > max_total {
|
||||||
|
let preview = entry.chars().take(50).collect::<String>();
|
||||||
|
return Err(rmcp::ErrorData::invalid_params(
|
||||||
|
format!(
|
||||||
|
"metadata entry '{}' exceeds the maximum length of {} characters",
|
||||||
|
preview, max_total
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -17,11 +17,12 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use secrets_core::audit::log_login;
|
use secrets_core::audit::log_login;
|
||||||
use secrets_core::crypto::hex;
|
use secrets_core::crypto::hex;
|
||||||
|
use secrets_core::error::AppError;
|
||||||
use secrets_core::service::{
|
use secrets_core::service::{
|
||||||
api_key::{ensure_api_key, regenerate_api_key},
|
api_key::{ensure_api_key, regenerate_api_key},
|
||||||
audit_log::list_for_user,
|
audit_log::list_for_user,
|
||||||
delete::delete_by_id,
|
delete::delete_by_id,
|
||||||
search::{SearchParams, count_entries, list_entries},
|
search::{SearchParams, fetch_secret_schemas, ilike_pattern, list_entries},
|
||||||
update::{UpdateEntryFieldsByIdParams, update_fields_by_id},
|
update::{UpdateEntryFieldsByIdParams, update_fields_by_id},
|
||||||
user::{
|
user::{
|
||||||
OAuthProfile, bind_oauth_account, find_or_create_user, get_user_by_id,
|
OAuthProfile, bind_oauth_account, find_or_create_user, get_user_by_id,
|
||||||
@@ -88,15 +89,16 @@ struct EntriesPageTemplate {
|
|||||||
user_name: String,
|
user_name: String,
|
||||||
user_email: String,
|
user_email: String,
|
||||||
entries: Vec<EntryListItemView>,
|
entries: Vec<EntryListItemView>,
|
||||||
total_count: i64,
|
folder_tabs: Vec<FolderTabView>,
|
||||||
shown_count: usize,
|
type_options: Vec<String>,
|
||||||
limit: u32,
|
secret_type_options_json: String,
|
||||||
filter_folder: String,
|
filter_folder: String,
|
||||||
|
filter_name: String,
|
||||||
filter_type: String,
|
filter_type: String,
|
||||||
version: &'static str,
|
version: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Non-sensitive fields only (no `secrets` / ciphertext).
|
/// Non-sensitive entry fields; `secrets` lists field names/types only (no ciphertext).
|
||||||
struct EntryListItemView {
|
struct EntryListItemView {
|
||||||
id: String,
|
id: String,
|
||||||
folder: String,
|
folder: String,
|
||||||
@@ -104,17 +106,37 @@ struct EntryListItemView {
|
|||||||
name: String,
|
name: String,
|
||||||
notes: String,
|
notes: String,
|
||||||
tags: String,
|
tags: String,
|
||||||
metadata: String,
|
/// Compact JSON for `data-entry-metadata` (dialog editor).
|
||||||
/// RFC3339 UTC for `<time datetime>`; localized in entries.html.
|
metadata_json: String,
|
||||||
|
/// Secret field summaries for table + dialog chips.
|
||||||
|
secrets: Vec<SecretSummaryView>,
|
||||||
|
/// JSON array of `{ id, name, secret_type }` for dialog secret chips.
|
||||||
|
secrets_json: String,
|
||||||
|
/// RFC3339 UTC; shown in edit dialog.
|
||||||
updated_at_iso: String,
|
updated_at_iso: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct SecretSummaryView {
|
||||||
|
id: String,
|
||||||
|
name: String,
|
||||||
|
secret_type: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FolderTabView {
|
||||||
|
name: String,
|
||||||
|
count: i64,
|
||||||
|
href: String,
|
||||||
|
active: bool,
|
||||||
|
}
|
||||||
|
|
||||||
/// Cap for HTML list (avoids loading unbounded rows into memory).
|
/// Cap for HTML list (avoids loading unbounded rows into memory).
|
||||||
const ENTRIES_PAGE_LIMIT: u32 = 5_000;
|
const ENTRIES_PAGE_LIMIT: u32 = 5_000;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct EntriesQuery {
|
struct EntriesQuery {
|
||||||
folder: Option<String>,
|
folder: Option<String>,
|
||||||
|
name: Option<String>,
|
||||||
/// URL query key is `type` (maps to DB column `entries.type`).
|
/// URL query key is `type` (maps to DB column `entries.type`).
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
entry_type: Option<String>,
|
entry_type: Option<String>,
|
||||||
@@ -176,6 +198,7 @@ pub fn web_router() -> Router<AppState> {
|
|||||||
.route("/robots.txt", get(robots_txt))
|
.route("/robots.txt", get(robots_txt))
|
||||||
.route("/llms.txt", get(llms_txt))
|
.route("/llms.txt", get(llms_txt))
|
||||||
.route("/ai.txt", get(ai_txt))
|
.route("/ai.txt", get(ai_txt))
|
||||||
|
.route("/static/i18n.js", get(i18n_js))
|
||||||
.route("/favicon.svg", get(favicon_svg))
|
.route("/favicon.svg", get(favicon_svg))
|
||||||
.route(
|
.route(
|
||||||
"/favicon.ico",
|
"/favicon.ico",
|
||||||
@@ -207,6 +230,12 @@ pub fn web_router() -> Router<AppState> {
|
|||||||
"/api/entries/{id}",
|
"/api/entries/{id}",
|
||||||
patch(api_entry_patch).delete(api_entry_delete),
|
patch(api_entry_patch).delete(api_entry_delete),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
"/api/entries/{entry_id}/secrets/{secret_id}",
|
||||||
|
axum::routing::delete(api_entry_secret_unlink),
|
||||||
|
)
|
||||||
|
.route("/api/secrets/{secret_id}", patch(api_secret_patch))
|
||||||
|
.route("/api/secrets/check-name", get(api_secret_check_name))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn text_asset_response(content: &'static str, content_type: &'static str) -> Response {
|
fn text_asset_response(content: &'static str, content_type: &'static str) -> Response {
|
||||||
@@ -236,6 +265,13 @@ async fn ai_txt() -> Response {
|
|||||||
llms_txt().await
|
llms_txt().await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn i18n_js() -> Response {
|
||||||
|
text_asset_response(
|
||||||
|
include_str!("../templates/i18n.js"),
|
||||||
|
"application/javascript; charset=utf-8",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
async fn favicon_svg() -> Response {
|
async fn favicon_svg() -> Response {
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(StatusCode::OK)
|
.status(StatusCode::OK)
|
||||||
@@ -554,11 +590,17 @@ async fn entries_page(
|
|||||||
.map(|s| s.trim())
|
.map(|s| s.trim())
|
||||||
.filter(|s| !s.is_empty())
|
.filter(|s| !s.is_empty())
|
||||||
.map(|s| s.to_string());
|
.map(|s| s.to_string());
|
||||||
|
let name_filter = q
|
||||||
|
.name
|
||||||
|
.as_ref()
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|s| s.to_string());
|
||||||
let params = SearchParams {
|
let params = SearchParams {
|
||||||
folder: folder_filter.as_deref(),
|
folder: folder_filter.as_deref(),
|
||||||
entry_type: type_filter.as_deref(),
|
entry_type: type_filter.as_deref(),
|
||||||
name: None,
|
name: None,
|
||||||
|
name_query: name_filter.as_deref(),
|
||||||
tags: &[],
|
tags: &[],
|
||||||
query: None,
|
query: None,
|
||||||
sort: "updated",
|
sort: "updated",
|
||||||
@@ -567,29 +609,151 @@ async fn entries_page(
|
|||||||
user_id: Some(user_id),
|
user_id: Some(user_id),
|
||||||
};
|
};
|
||||||
|
|
||||||
let total_count = count_entries(&state.pool, ¶ms).await.map_err(|e| {
|
|
||||||
tracing::error!(error = %e, "failed to count entries for web");
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let rows = list_entries(&state.pool, params).await.map_err(|e| {
|
let rows = list_entries(&state.pool, params).await.map_err(|e| {
|
||||||
tracing::error!(error = %e, "failed to load entries list for web");
|
tracing::error!(error = %e, "failed to load entries list for web");
|
||||||
StatusCode::INTERNAL_SERVER_ERROR
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
})?;
|
})?;
|
||||||
let shown_count = rows.len();
|
let entry_ids: Vec<Uuid> = rows.iter().map(|e| e.id).collect();
|
||||||
|
let secret_schemas = fetch_secret_schemas(&state.pool, &entry_ids)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
tracing::error!(error = %e, "failed to load secret schema list for web");
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?;
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow)]
|
||||||
|
struct FolderCountRow {
|
||||||
|
folder: String,
|
||||||
|
count: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut folder_sql =
|
||||||
|
"SELECT folder, COUNT(*)::bigint AS count FROM entries WHERE user_id = $1".to_string();
|
||||||
|
let mut bind_idx = 2;
|
||||||
|
if type_filter.is_some() {
|
||||||
|
folder_sql.push_str(&format!(" AND type = ${bind_idx}"));
|
||||||
|
bind_idx += 1;
|
||||||
|
}
|
||||||
|
if name_filter.is_some() {
|
||||||
|
folder_sql.push_str(&format!(" AND name ILIKE ${bind_idx} ESCAPE '\\'"));
|
||||||
|
bind_idx += 1;
|
||||||
|
}
|
||||||
|
let _ = bind_idx;
|
||||||
|
folder_sql.push_str(" GROUP BY folder ORDER BY folder");
|
||||||
|
|
||||||
|
let mut folder_query = sqlx::query_as::<_, FolderCountRow>(&folder_sql).bind(user_id);
|
||||||
|
if let Some(t) = type_filter.as_deref() {
|
||||||
|
folder_query = folder_query.bind(t);
|
||||||
|
}
|
||||||
|
if let Some(n) = name_filter.as_deref() {
|
||||||
|
folder_query = folder_query.bind(ilike_pattern(n));
|
||||||
|
}
|
||||||
|
let folder_rows: Vec<FolderCountRow> =
|
||||||
|
folder_query.fetch_all(&state.pool).await.map_err(|e| {
|
||||||
|
tracing::error!(error = %e, "failed to load folder tabs for web");
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?;
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow)]
|
||||||
|
struct TypeOptionRow {
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
entry_type: String,
|
||||||
|
}
|
||||||
|
let mut type_options: Vec<String> = sqlx::query_as::<_, TypeOptionRow>(
|
||||||
|
"SELECT DISTINCT type FROM entries WHERE user_id = $1 ORDER BY type",
|
||||||
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_all(&state.pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
tracing::error!(error = %e, "failed to load type options for web");
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?
|
||||||
|
.into_iter()
|
||||||
|
.map(|r| r.entry_type)
|
||||||
|
.filter(|t| !t.is_empty())
|
||||||
|
.collect();
|
||||||
|
if let Some(current) = type_filter.as_ref()
|
||||||
|
&& !current.is_empty()
|
||||||
|
&& !type_options.iter().any(|t| t == current)
|
||||||
|
{
|
||||||
|
type_options.push(current.clone());
|
||||||
|
type_options.sort_unstable();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn entries_href(folder: Option<&str>, entry_type: Option<&str>, name: Option<&str>) -> String {
|
||||||
|
let mut pairs: Vec<String> = Vec::new();
|
||||||
|
if let Some(f) = folder
|
||||||
|
&& !f.is_empty()
|
||||||
|
{
|
||||||
|
pairs.push(format!("folder={}", urlencoding::encode(f)));
|
||||||
|
}
|
||||||
|
if let Some(t) = entry_type
|
||||||
|
&& !t.is_empty()
|
||||||
|
{
|
||||||
|
pairs.push(format!("type={}", urlencoding::encode(t)));
|
||||||
|
}
|
||||||
|
if let Some(n) = name
|
||||||
|
&& !n.is_empty()
|
||||||
|
{
|
||||||
|
pairs.push(format!("name={}", urlencoding::encode(n)));
|
||||||
|
}
|
||||||
|
if pairs.is_empty() {
|
||||||
|
"/entries".to_string()
|
||||||
|
} else {
|
||||||
|
format!("/entries?{}", pairs.join("&"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let all_count: i64 = folder_rows.iter().map(|r| r.count).sum();
|
||||||
|
let mut folder_tabs: Vec<FolderTabView> = Vec::with_capacity(folder_rows.len() + 1);
|
||||||
|
folder_tabs.push(FolderTabView {
|
||||||
|
name: "全部".to_string(),
|
||||||
|
count: all_count,
|
||||||
|
href: entries_href(None, type_filter.as_deref(), name_filter.as_deref()),
|
||||||
|
active: folder_filter.is_none(),
|
||||||
|
});
|
||||||
|
for r in folder_rows {
|
||||||
|
let name = r.folder;
|
||||||
|
folder_tabs.push(FolderTabView {
|
||||||
|
href: entries_href(Some(&name), type_filter.as_deref(), name_filter.as_deref()),
|
||||||
|
active: folder_filter.as_deref() == Some(name.as_str()),
|
||||||
|
name,
|
||||||
|
count: r.count,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let entries = rows
|
let entries = rows
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|e| EntryListItemView {
|
.map(|e| {
|
||||||
id: e.id.to_string(),
|
let secrets: Vec<SecretSummaryView> = secret_schemas
|
||||||
folder: e.folder,
|
.get(&e.id)
|
||||||
entry_type: e.entry_type,
|
.map(|fields| {
|
||||||
name: e.name,
|
fields
|
||||||
notes: e.notes,
|
.iter()
|
||||||
tags: e.tags.join(", "),
|
.map(|f| SecretSummaryView {
|
||||||
metadata: serde_json::to_string_pretty(&e.metadata)
|
id: f.id.to_string(),
|
||||||
.unwrap_or_else(|_| "{}".to_string()),
|
name: f.name.clone(),
|
||||||
updated_at_iso: e.updated_at.to_rfc3339_opts(SecondsFormat::Secs, true),
|
secret_type: f.secret_type.clone(),
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
let secrets_json = serde_json::to_string(&secrets).unwrap_or_else(|_| "[]".to_string());
|
||||||
|
let metadata_json =
|
||||||
|
serde_json::to_string(&e.metadata).unwrap_or_else(|_| "{}".to_string());
|
||||||
|
EntryListItemView {
|
||||||
|
id: e.id.to_string(),
|
||||||
|
folder: e.folder,
|
||||||
|
entry_type: e.entry_type,
|
||||||
|
name: e.name,
|
||||||
|
notes: e.notes,
|
||||||
|
tags: e.tags.join(", "),
|
||||||
|
metadata_json,
|
||||||
|
secrets,
|
||||||
|
secrets_json,
|
||||||
|
updated_at_iso: e.updated_at.to_rfc3339_opts(SecondsFormat::Secs, true),
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@@ -597,10 +761,17 @@ async fn entries_page(
|
|||||||
user_name: user.name.clone(),
|
user_name: user.name.clone(),
|
||||||
user_email: user.email.clone().unwrap_or_default(),
|
user_email: user.email.clone().unwrap_or_default(),
|
||||||
entries,
|
entries,
|
||||||
total_count,
|
folder_tabs,
|
||||||
shown_count,
|
type_options,
|
||||||
limit: ENTRIES_PAGE_LIMIT,
|
secret_type_options_json: serde_json::to_string(
|
||||||
|
&secrets_core::taxonomy::SECRET_TYPE_OPTIONS
|
||||||
|
.iter()
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
.unwrap_or_default(),
|
||||||
filter_folder: folder_filter.unwrap_or_default(),
|
filter_folder: folder_filter.unwrap_or_default(),
|
||||||
|
filter_name: name_filter.unwrap_or_default(),
|
||||||
filter_type: type_filter.unwrap_or_default(),
|
filter_type: type_filter.unwrap_or_default(),
|
||||||
version: env!("CARGO_PKG_VERSION"),
|
version: env!("CARGO_PKG_VERSION"),
|
||||||
};
|
};
|
||||||
@@ -896,24 +1067,53 @@ struct EntryPatchBody {
|
|||||||
|
|
||||||
type EntryApiError = (StatusCode, Json<serde_json::Value>);
|
type EntryApiError = (StatusCode, Json<serde_json::Value>);
|
||||||
|
|
||||||
fn map_entry_mutation_err(e: anyhow::Error) -> EntryApiError {
|
#[derive(Clone, Copy)]
|
||||||
let msg = e.to_string();
|
enum UiLang {
|
||||||
if msg.contains("Entry not found") {
|
ZhCn,
|
||||||
return (
|
ZhTw,
|
||||||
StatusCode::NOT_FOUND,
|
En,
|
||||||
Json(json!({ "error": "条目不存在或无权访问" })),
|
}
|
||||||
);
|
|
||||||
|
fn request_ui_lang(headers: &HeaderMap) -> UiLang {
|
||||||
|
let Some(raw) = headers
|
||||||
|
.get(header::ACCEPT_LANGUAGE)
|
||||||
|
.and_then(|v| v.to_str().ok())
|
||||||
|
else {
|
||||||
|
return UiLang::ZhCn;
|
||||||
|
};
|
||||||
|
let lower = raw.to_ascii_lowercase();
|
||||||
|
if lower.contains("zh-tw") || lower.contains("zh-hk") || lower.contains("zh-hant") {
|
||||||
|
UiLang::ZhTw
|
||||||
|
} else if lower.contains("zh") {
|
||||||
|
UiLang::ZhCn
|
||||||
|
} else if lower.contains("en") {
|
||||||
|
UiLang::En
|
||||||
|
} else {
|
||||||
|
UiLang::ZhCn
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tr(lang: UiLang, zh_cn: &'static str, zh_tw: &'static str, en: &'static str) -> &'static str {
|
||||||
|
match lang {
|
||||||
|
UiLang::ZhCn => zh_cn,
|
||||||
|
UiLang::ZhTw => zh_tw,
|
||||||
|
UiLang::En => en,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_entry_mutation_err(e: anyhow::Error, lang: UiLang) -> EntryApiError {
|
||||||
|
if let Some(app_err) = e.downcast_ref::<AppError>() {
|
||||||
|
return map_app_error(app_err, lang);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback for legacy string-based errors and raw sqlx errors
|
||||||
|
let msg = e.to_string();
|
||||||
if msg.contains("already exists") {
|
if msg.contains("already exists") {
|
||||||
return (
|
return (
|
||||||
StatusCode::CONFLICT,
|
StatusCode::CONFLICT,
|
||||||
Json(json!({ "error": "该账号下已存在相同 folder + name 的条目" })),
|
Json(
|
||||||
);
|
json!({ "error": tr(lang, "该账号下已存在相同 folder + name 的条目", "此帳號下已存在相同 folder + name 的條目", "An entry with the same folder + name already exists for this account") }),
|
||||||
}
|
),
|
||||||
if msg.contains("Concurrent modification") {
|
|
||||||
return (
|
|
||||||
StatusCode::CONFLICT,
|
|
||||||
Json(json!({ "error": "条目已被修改,请刷新后重试" })),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if msg.contains("must be at most") {
|
if msg.contains("must be at most") {
|
||||||
@@ -922,19 +1122,75 @@ fn map_entry_mutation_err(e: anyhow::Error) -> EntryApiError {
|
|||||||
tracing::error!(error = %e, "entry mutation failed");
|
tracing::error!(error = %e, "entry mutation failed");
|
||||||
(
|
(
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
Json(json!({ "error": "操作失败,请稍后重试" })),
|
Json(
|
||||||
|
json!({ "error": tr(lang, "操作失败,请稍后重试", "操作失敗,請稍後重試", "Operation failed, please try again later") }),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn map_app_error(err: &AppError, lang: UiLang) -> EntryApiError {
|
||||||
|
match err {
|
||||||
|
AppError::ConflictEntryName { .. } | AppError::ConflictSecretName { .. } => (
|
||||||
|
StatusCode::CONFLICT,
|
||||||
|
Json(json!({ "error": err.to_string() })),
|
||||||
|
),
|
||||||
|
AppError::NotFoundEntry | AppError::NotFoundUser | AppError::NotFoundSecret => (
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "资源不存在或无权访问", "資源不存在或無權存取", "Resource not found or no access") }),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
AppError::AuthenticationFailed | AppError::Unauthorized => (
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "认证失败或无权访问", "認證失敗或無權存取", "Authentication failed or unauthorized") }),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
AppError::Validation { message } => {
|
||||||
|
(StatusCode::BAD_REQUEST, Json(json!({ "error": message })))
|
||||||
|
}
|
||||||
|
AppError::ConcurrentModification => (
|
||||||
|
StatusCode::CONFLICT,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "条目已被修改,请刷新后重试", "條目已被修改,請重新整理後重試", "Entry was modified, please refresh and try again") }),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
AppError::DecryptionFailed => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "解密失败,请检查密码短语", "解密失敗,請檢查密碼短語", "Decryption failed — please check your passphrase") }),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
AppError::EncryptionKeyNotSet => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "请先设置密码短语后再使用此功能", "請先設定密碼短語再使用此功能", "Please set a passphrase before using this feature") }),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
AppError::Internal(_) => {
|
||||||
|
tracing::error!(error = %err, "internal error in entry mutation");
|
||||||
|
(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "操作失败,请稍后重试", "操作失敗,請稍後重試", "Operation failed, please try again later") }),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn api_entry_patch(
|
async fn api_entry_patch(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
session: Session,
|
session: Session,
|
||||||
|
headers: HeaderMap,
|
||||||
Path(entry_id): Path<Uuid>,
|
Path(entry_id): Path<Uuid>,
|
||||||
Json(body): Json<EntryPatchBody>,
|
Json(body): Json<EntryPatchBody>,
|
||||||
) -> Result<Json<serde_json::Value>, EntryApiError> {
|
) -> Result<Json<serde_json::Value>, EntryApiError> {
|
||||||
let user_id = current_user_id(&session)
|
let lang = request_ui_lang(&headers);
|
||||||
.await
|
let user_id = current_user_id(&session).await.ok_or((
|
||||||
.ok_or((StatusCode::UNAUTHORIZED, Json(json!({ "error": "未登录" }))))?;
|
StatusCode::UNAUTHORIZED,
|
||||||
|
Json(json!({ "error": tr(lang, "未登录", "尚未登入", "Not logged in") })),
|
||||||
|
))?;
|
||||||
|
|
||||||
let folder = body.folder.trim();
|
let folder = body.folder.trim();
|
||||||
let entry_type = body.entry_type.trim();
|
let entry_type = body.entry_type.trim();
|
||||||
@@ -944,7 +1200,9 @@ async fn api_entry_patch(
|
|||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
return Err((
|
return Err((
|
||||||
StatusCode::BAD_REQUEST,
|
StatusCode::BAD_REQUEST,
|
||||||
Json(json!({ "error": "name 不能为空" })),
|
Json(
|
||||||
|
json!({ "error": tr(lang, "name 不能为空", "name 不能為空", "name cannot be empty") }),
|
||||||
|
),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -958,7 +1216,9 @@ async fn api_entry_patch(
|
|||||||
if !body.metadata.is_object() {
|
if !body.metadata.is_object() {
|
||||||
return Err((
|
return Err((
|
||||||
StatusCode::BAD_REQUEST,
|
StatusCode::BAD_REQUEST,
|
||||||
Json(json!({ "error": "metadata 必须是 JSON 对象" })),
|
Json(
|
||||||
|
json!({ "error": tr(lang, "metadata 必须是 JSON 对象", "metadata 必須是 JSON 物件", "metadata must be a JSON object") }),
|
||||||
|
),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -976,7 +1236,7 @@ async fn api_entry_patch(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(map_entry_mutation_err)?;
|
.map_err(|e| map_entry_mutation_err(e, lang))?;
|
||||||
|
|
||||||
Ok(Json(json!({ "ok": true })))
|
Ok(Json(json!({ "ok": true })))
|
||||||
}
|
}
|
||||||
@@ -984,19 +1244,383 @@ async fn api_entry_patch(
|
|||||||
async fn api_entry_delete(
|
async fn api_entry_delete(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
session: Session,
|
session: Session,
|
||||||
|
headers: HeaderMap,
|
||||||
Path(entry_id): Path<Uuid>,
|
Path(entry_id): Path<Uuid>,
|
||||||
) -> Result<Json<serde_json::Value>, EntryApiError> {
|
) -> Result<Json<serde_json::Value>, EntryApiError> {
|
||||||
let user_id = current_user_id(&session)
|
let lang = request_ui_lang(&headers);
|
||||||
.await
|
let user_id = current_user_id(&session).await.ok_or((
|
||||||
.ok_or((StatusCode::UNAUTHORIZED, Json(json!({ "error": "未登录" }))))?;
|
StatusCode::UNAUTHORIZED,
|
||||||
|
Json(json!({ "error": tr(lang, "未登录", "尚未登入", "Not logged in") })),
|
||||||
|
))?;
|
||||||
|
|
||||||
let result = delete_by_id(&state.pool, entry_id, user_id)
|
delete_by_id(&state.pool, entry_id, user_id)
|
||||||
.await
|
.await
|
||||||
.map_err(map_entry_mutation_err)?;
|
.map_err(|e| map_entry_mutation_err(e, lang))?;
|
||||||
|
|
||||||
Ok(Json(json!({
|
Ok(Json(json!({
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"migrated": result.migrated,
|
})))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct SecretCheckNameQuery {
|
||||||
|
name: String,
|
||||||
|
exclude_secret_id: Option<Uuid>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct SecretCheckNameResponse {
|
||||||
|
ok: bool,
|
||||||
|
available: bool,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
error: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn api_secret_check_name(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
session: Session,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Query(params): Query<SecretCheckNameQuery>,
|
||||||
|
) -> Result<Json<SecretCheckNameResponse>, EntryApiError> {
|
||||||
|
let lang = request_ui_lang(&headers);
|
||||||
|
let user_id = current_user_id(&session).await.ok_or((
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
Json(json!({ "error": tr(lang, "未登录", "尚未登入", "Not logged in") })),
|
||||||
|
))?;
|
||||||
|
|
||||||
|
let name = params.name.trim();
|
||||||
|
if name.is_empty() {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "secret name 不能为空", "secret name 不能為空", "secret name cannot be empty") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if name.chars().count() > 256 {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "secret name 长度不能超过 256 个字符", "secret name 長度不能超過 256 個字元", "secret name must be at most 256 characters") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let count: i64 = if let Some(exclude_id) = params.exclude_secret_id {
|
||||||
|
sqlx::query_scalar::<_, i64>(
|
||||||
|
"SELECT COUNT(*) FROM secrets WHERE user_id = $1 AND name = $2 AND id != $3",
|
||||||
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(name)
|
||||||
|
.bind(exclude_id)
|
||||||
|
.fetch_one(&state.pool)
|
||||||
|
.await
|
||||||
|
} else {
|
||||||
|
sqlx::query_scalar::<_, i64>(
|
||||||
|
"SELECT COUNT(*) FROM secrets WHERE user_id = $1 AND name = $2",
|
||||||
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(name)
|
||||||
|
.fetch_one(&state.pool)
|
||||||
|
.await
|
||||||
|
}.map_err(|e| {
|
||||||
|
tracing::error!(error = %e, "failed to check secret name availability");
|
||||||
|
(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "操作失败,请稍后重试", "操作失敗,請稍後重試", "Operation failed, please try again later") }),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let available = count == 0;
|
||||||
|
let error = if available {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(
|
||||||
|
tr(
|
||||||
|
lang,
|
||||||
|
"该用户下已存在相同 name 的密文",
|
||||||
|
"該用戶下已存在相同 name 的密文",
|
||||||
|
"A secret with the same name already exists for this user",
|
||||||
|
)
|
||||||
|
.to_string(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Json(SecretCheckNameResponse {
|
||||||
|
ok: true,
|
||||||
|
available,
|
||||||
|
error,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct SecretPatchBody {
|
||||||
|
name: Option<String>,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
secret_type: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn api_secret_patch(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
session: Session,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Path(secret_id): Path<Uuid>,
|
||||||
|
Json(body): Json<SecretPatchBody>,
|
||||||
|
) -> Result<Json<serde_json::Value>, EntryApiError> {
|
||||||
|
#[derive(Serialize, sqlx::FromRow)]
|
||||||
|
struct LinkedEntryAuditRow {
|
||||||
|
folder: String,
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
entry_type: String,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
let lang = request_ui_lang(&headers);
|
||||||
|
let user_id = current_user_id(&session).await.ok_or((
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
Json(json!({ "error": tr(lang, "未登录", "尚未登入", "Not logged in") })),
|
||||||
|
))?;
|
||||||
|
|
||||||
|
let name = body.name.as_ref().map(|s| s.trim());
|
||||||
|
let secret_type = body.secret_type.as_ref().map(|s| s.trim());
|
||||||
|
|
||||||
|
if let Some(n) = name {
|
||||||
|
if n.is_empty() {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "secret name 不能为空", "secret name 不能為空", "secret name cannot be empty") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if n.chars().count() > 256 {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "secret name 长度不能超过 256 个字符", "secret name 長度不能超過 256 個字元", "secret name must be at most 256 characters") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(t) = secret_type {
|
||||||
|
if t.is_empty() {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "secret type 不能为空", "secret type 不能為空", "secret type cannot be empty") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if t.chars().count() > 64 {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "secret type 长度不能超过 64 个字符", "secret type 長度不能超過 64 個字元", "secret type must be at most 64 characters") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if name.is_none() && secret_type.is_none() {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "至少需要提供 name 或 type 之一", "至少需要提供 name 或 type 之一", "At least one of name or type is required") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut tx = state
|
||||||
|
.pool
|
||||||
|
.begin()
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e.into(), lang))?;
|
||||||
|
|
||||||
|
let secret_row: Option<(String, String)> =
|
||||||
|
sqlx::query_as("SELECT name, type FROM secrets WHERE id = $1 AND user_id = $2 FOR UPDATE")
|
||||||
|
.bind(secret_id)
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e.into(), lang))?;
|
||||||
|
|
||||||
|
let Some((old_name, old_type)) = secret_row else {
|
||||||
|
let _ = tx.rollback().await;
|
||||||
|
return Err((
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "密文不存在或无权访问", "密文不存在或無權存取", "Secret not found or no access") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
let linked_entries: Vec<LinkedEntryAuditRow> = sqlx::query_as(
|
||||||
|
"SELECT e.folder, e.type, e.name \
|
||||||
|
FROM entry_secrets es \
|
||||||
|
JOIN entries e ON e.id = es.entry_id \
|
||||||
|
WHERE es.secret_id = $1 AND e.user_id = $2 \
|
||||||
|
ORDER BY e.folder, e.type, e.name",
|
||||||
|
)
|
||||||
|
.bind(secret_id)
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_all(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e.into(), lang))?;
|
||||||
|
|
||||||
|
let new_name = name.unwrap_or(&old_name).to_string();
|
||||||
|
let new_type = secret_type.unwrap_or(&old_type).to_string();
|
||||||
|
|
||||||
|
let result = sqlx::query(
|
||||||
|
"UPDATE secrets SET name = $1, type = $2, version = version + 1, updated_at = NOW() \
|
||||||
|
WHERE id = $3",
|
||||||
|
)
|
||||||
|
.bind(&new_name)
|
||||||
|
.bind(&new_type)
|
||||||
|
.bind(secret_id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
if let Err(e) = result {
|
||||||
|
if let Some(db_err) = e.as_database_error()
|
||||||
|
&& db_err.code() == Some("23505".into())
|
||||||
|
{
|
||||||
|
let _ = tx.rollback().await;
|
||||||
|
return Err(map_app_error(
|
||||||
|
&AppError::ConflictSecretName {
|
||||||
|
secret_name: new_name.clone(),
|
||||||
|
},
|
||||||
|
lang,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let _ = tx.rollback().await;
|
||||||
|
return Err(map_entry_mutation_err(e.into(), lang));
|
||||||
|
}
|
||||||
|
|
||||||
|
secrets_core::audit::log_tx(
|
||||||
|
&mut tx,
|
||||||
|
Some(user_id),
|
||||||
|
"rename_secret",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
&old_name,
|
||||||
|
json!({
|
||||||
|
"source": "web",
|
||||||
|
"secret_id": secret_id,
|
||||||
|
"old_name": old_name,
|
||||||
|
"new_name": new_name,
|
||||||
|
"old_type": old_type,
|
||||||
|
"new_type": new_type,
|
||||||
|
"linked_entries": linked_entries,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
tx.commit()
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e.into(), lang))?;
|
||||||
|
|
||||||
|
Ok(Json(json!({ "ok": true })))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn api_entry_secret_unlink(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
session: Session,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Path((entry_id, secret_id)): Path<(Uuid, Uuid)>,
|
||||||
|
) -> Result<Json<serde_json::Value>, EntryApiError> {
|
||||||
|
#[derive(sqlx::FromRow)]
|
||||||
|
struct EntryAuditRow {
|
||||||
|
folder: String,
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
entry_type: String,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
let lang = request_ui_lang(&headers);
|
||||||
|
let user_id = current_user_id(&session).await.ok_or((
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
Json(json!({ "error": tr(lang, "未登录", "尚未登入", "Not logged in") })),
|
||||||
|
))?;
|
||||||
|
|
||||||
|
let mut tx = state
|
||||||
|
.pool
|
||||||
|
.begin()
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e.into(), lang))?;
|
||||||
|
|
||||||
|
let entry_row: Option<EntryAuditRow> =
|
||||||
|
sqlx::query_as("SELECT folder, type, name FROM entries WHERE id = $1 AND user_id = $2")
|
||||||
|
.bind(entry_id)
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e.into(), lang))?;
|
||||||
|
|
||||||
|
let Some(entry_row) = entry_row else {
|
||||||
|
let _ = tx.rollback().await;
|
||||||
|
return Err((
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "条目不存在或无权访问", "條目不存在或無權存取", "Entry not found or no access") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
let deleted = sqlx::query("DELETE FROM entry_secrets WHERE entry_id = $1 AND secret_id = $2")
|
||||||
|
.bind(entry_id)
|
||||||
|
.bind(secret_id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e.into(), lang))?
|
||||||
|
.rows_affected();
|
||||||
|
|
||||||
|
if deleted == 0 {
|
||||||
|
let _ = tx.rollback().await;
|
||||||
|
return Err((
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
Json(json!({ "error": tr(lang, "关联不存在", "關聯不存在", "Relation not found") })),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let secret_deleted = 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(secret_id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e.into(), lang))?
|
||||||
|
.rows_affected()
|
||||||
|
> 0;
|
||||||
|
|
||||||
|
secrets_core::audit::log_tx(
|
||||||
|
&mut tx,
|
||||||
|
Some(user_id),
|
||||||
|
"unlink_secret",
|
||||||
|
&entry_row.folder,
|
||||||
|
&entry_row.entry_type,
|
||||||
|
&entry_row.name,
|
||||||
|
json!({
|
||||||
|
"source": "web",
|
||||||
|
"entry_id": entry_id,
|
||||||
|
"secret_id": secret_id,
|
||||||
|
"deleted_secret": secret_deleted,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
tx.commit()
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e.into(), lang))?;
|
||||||
|
|
||||||
|
Ok(Json(json!({
|
||||||
|
"ok": true,
|
||||||
|
"deleted_relation": true,
|
||||||
|
"deleted_secret": secret_deleted,
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1044,3 +1668,27 @@ fn format_audit_target(folder: &str, entry_type: &str, name: &str) -> String {
|
|||||||
name.to_string()
|
name.to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn request_ui_lang_prefers_zh_cn_over_en_fallback() {
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert(header::ACCEPT_LANGUAGE, "zh-CN, en;q=0.5".parse().unwrap());
|
||||||
|
|
||||||
|
assert!(matches!(request_ui_lang(&headers), UiLang::ZhCn));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn request_ui_lang_detects_traditional_chinese_variants() {
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert(
|
||||||
|
header::ACCEPT_LANGUAGE,
|
||||||
|
"zh-Hant, en;q=0.5".parse().unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(matches!(request_ui_lang(&headers), UiLang::ZhTw));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,6 +38,10 @@
|
|||||||
}
|
}
|
||||||
.topbar-spacer { flex: 1; }
|
.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-btn { padding: 3px 9px; 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); }
|
||||||
.btn-sign-out {
|
.btn-sign-out {
|
||||||
padding: 5px 12px; border-radius: 6px; border: 1px solid var(--border);
|
padding: 5px 12px; border-radius: 6px; border: 1px solid var(--border);
|
||||||
background: none; color: var(--text); font-size: 12px; text-decoration: none; cursor: pointer;
|
background: none; color: var(--text); font-size: 12px; text-decoration: none; cursor: pointer;
|
||||||
@@ -46,8 +50,7 @@
|
|||||||
.main { padding: 32px 24px 40px; flex: 1; }
|
.main { padding: 32px 24px 40px; flex: 1; }
|
||||||
.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: 24px; width: 100%; max-width: 1180px; margin: 0 auto; }
|
padding: 24px; width: 100%; max-width: 1180px; margin: 0 auto; }
|
||||||
.card-title { font-size: 20px; font-weight: 600; margin-bottom: 8px; }
|
.card-title { font-size: 20px; font-weight: 600; margin-bottom: 20px; }
|
||||||
.card-subtitle { color: var(--text-muted); font-size: 13px; margin-bottom: 20px; }
|
|
||||||
.empty { color: var(--text-muted); font-size: 14px; padding: 20px 0; }
|
.empty { color: var(--text-muted); font-size: 14px; padding: 20px 0; }
|
||||||
table { width: 100%; border-collapse: collapse; }
|
table { width: 100%; border-collapse: collapse; }
|
||||||
th, td { text-align: left; vertical-align: top; padding: 12px 10px; border-top: 1px solid var(--border); }
|
th, td { text-align: left; vertical-align: top; padding: 12px 10px; border-top: 1px solid var(--border); }
|
||||||
@@ -77,11 +80,8 @@
|
|||||||
td::before {
|
td::before {
|
||||||
display: block; color: var(--text-muted); font-size: 11px;
|
display: block; color: var(--text-muted); font-size: 11px;
|
||||||
margin-bottom: 4px; text-transform: uppercase;
|
margin-bottom: 4px; text-transform: uppercase;
|
||||||
|
content: attr(data-label);
|
||||||
}
|
}
|
||||||
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; }
|
.detail { max-width: none; }
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
@@ -91,9 +91,9 @@
|
|||||||
<aside class="sidebar">
|
<aside class="sidebar">
|
||||||
<a href="/dashboard" class="sidebar-logo"><span>secrets</span></a>
|
<a href="/dashboard" class="sidebar-logo"><span>secrets</span></a>
|
||||||
<nav class="sidebar-menu">
|
<nav class="sidebar-menu">
|
||||||
<a href="/dashboard" class="sidebar-link">MCP</a>
|
<a href="/dashboard" class="sidebar-link" data-i18n="navMcp">MCP</a>
|
||||||
<a href="/entries" class="sidebar-link">条目</a>
|
<a href="/entries" class="sidebar-link" data-i18n="navEntries">条目</a>
|
||||||
<a href="/audit" class="sidebar-link active">审计</a>
|
<a href="/audit" class="sidebar-link active" data-i18n="navAudit">审计</a>
|
||||||
</nav>
|
</nav>
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
@@ -101,35 +101,39 @@
|
|||||||
<div class="topbar">
|
<div class="topbar">
|
||||||
<span class="topbar-spacer"></span>
|
<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">
|
||||||
|
<button class="lang-btn" onclick="setLang('zh-CN')">简</button>
|
||||||
|
<button class="lang-btn" onclick="setLang('zh-TW')">繁</button>
|
||||||
|
<button class="lang-btn" onclick="setLang('en')">EN</button>
|
||||||
|
</div>
|
||||||
<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">退出</button>
|
<button type="submit" class="btn-sign-out" data-i18n="signOut">退出</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<main class="main">
|
<main class="main">
|
||||||
<section class="card">
|
<section class="card">
|
||||||
<div class="card-title">我的审计</div>
|
<div class="card-title" data-i18n="auditTitle">我的审计</div>
|
||||||
<div class="card-subtitle">展示最近 100 条与当前用户相关的新审计记录。时间为浏览器本地时区。</div>
|
|
||||||
|
|
||||||
{% if entries.is_empty() %}
|
{% if entries.is_empty() %}
|
||||||
<div class="empty">暂无审计记录。</div>
|
<div class="empty" data-i18n="emptyAudit">暂无审计记录。</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>时间</th>
|
<th data-i18n="colTime">时间</th>
|
||||||
<th>动作</th>
|
<th data-i18n="colAction">动作</th>
|
||||||
<th>目标</th>
|
<th data-i18n="colTarget">目标</th>
|
||||||
<th>详情</th>
|
<th data-i18n="colDetail">详情</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for entry in entries %}
|
{% for entry in entries %}
|
||||||
<tr>
|
<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-time mono" data-label="时间"><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-action mono" data-label="动作">{{ entry.action }}</td>
|
||||||
<td class="col-target mono">{{ entry.target }}</td>
|
<td class="col-target mono" data-label="目标">{{ entry.target }}</td>
|
||||||
<td class="col-detail"><pre class="detail">{{ entry.detail }}</pre></td>
|
<td class="col-detail" data-label="详情"><pre class="detail">{{ entry.detail }}</pre></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
@@ -139,8 +143,28 @@
|
|||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<script src="/static/i18n.js"></script>
|
||||||
<script>
|
<script>
|
||||||
(function () {
|
(function () {
|
||||||
|
I18N_PAGE = {
|
||||||
|
'zh-CN': { pageTitle: 'Secrets — 审计', auditTitle: '我的审计', emptyAudit: '暂无审计记录。', colTime: '时间', colAction: '动作', colTarget: '目标', colDetail: '详情' },
|
||||||
|
'zh-TW': { pageTitle: 'Secrets — 審計', auditTitle: '我的審計', emptyAudit: '暫無審計記錄。', colTime: '時間', colAction: '動作', colTarget: '目標', colDetail: '詳情' },
|
||||||
|
en: { pageTitle: 'Secrets — Audit', auditTitle: 'My audit', emptyAudit: 'No audit records.', colTime: 'Time', colAction: 'Action', colTarget: 'Target', colDetail: 'Detail' }
|
||||||
|
};
|
||||||
|
|
||||||
|
window.applyPageLang = function () {
|
||||||
|
document.querySelectorAll('tbody tr').forEach(function (tr) {
|
||||||
|
var time = tr.querySelector('.col-time');
|
||||||
|
var action = tr.querySelector('.col-action');
|
||||||
|
var target = tr.querySelector('.col-target');
|
||||||
|
var detail = tr.querySelector('.col-detail');
|
||||||
|
if (time) time.setAttribute('data-label', t('mobileLabelTime'));
|
||||||
|
if (action) action.setAttribute('data-label', t('mobileLabelAction'));
|
||||||
|
if (target) target.setAttribute('data-label', t('mobileLabelTarget'));
|
||||||
|
if (detail) detail.setAttribute('data-label', t('mobileLabelDetail'));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
document.querySelectorAll('time.audit-local-time[datetime]').forEach(function (el) {
|
document.querySelectorAll('time.audit-local-time[datetime]').forEach(function (el) {
|
||||||
var raw = el.getAttribute('datetime');
|
var raw = el.getAttribute('datetime');
|
||||||
var d = raw ? new Date(raw) : null;
|
var d = raw ? new Date(raw) : null;
|
||||||
@@ -149,6 +173,7 @@
|
|||||||
el.title = raw + ' (UTC)';
|
el.title = raw + ' (UTC)';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
applyLang();
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
76
crates/secrets-mcp/templates/i18n.js
Normal file
76
crates/secrets-mcp/templates/i18n.js
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
var I18N_SHARED = {
|
||||||
|
'zh-CN': {
|
||||||
|
pageTitleBase: 'Secrets',
|
||||||
|
navMcp: 'MCP',
|
||||||
|
navEntries: '条目',
|
||||||
|
navAudit: '审计',
|
||||||
|
signOut: '退出',
|
||||||
|
mobileLabelTime: '时间',
|
||||||
|
mobileLabelAction: '动作',
|
||||||
|
mobileLabelTarget: '目标',
|
||||||
|
mobileLabelDetail: '详情'
|
||||||
|
},
|
||||||
|
'zh-TW': {
|
||||||
|
pageTitleBase: 'Secrets',
|
||||||
|
navMcp: 'MCP',
|
||||||
|
navEntries: '條目',
|
||||||
|
navAudit: '審計',
|
||||||
|
signOut: '登出',
|
||||||
|
mobileLabelTime: '時間',
|
||||||
|
mobileLabelAction: '動作',
|
||||||
|
mobileLabelTarget: '目標',
|
||||||
|
mobileLabelDetail: '詳情'
|
||||||
|
},
|
||||||
|
en: {
|
||||||
|
pageTitleBase: 'Secrets',
|
||||||
|
navMcp: 'MCP',
|
||||||
|
navEntries: 'Entries',
|
||||||
|
navAudit: 'Audit',
|
||||||
|
signOut: 'Sign out',
|
||||||
|
mobileLabelTime: 'Time',
|
||||||
|
mobileLabelAction: 'Action',
|
||||||
|
mobileLabelTarget: 'Target',
|
||||||
|
mobileLabelDetail: 'Detail'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var currentLang = localStorage.getItem('lang') || 'zh-CN';
|
||||||
|
var I18N_PAGE = {};
|
||||||
|
|
||||||
|
function t(key) {
|
||||||
|
var dict = I18N_PAGE[currentLang] || I18N_PAGE['en'] || {};
|
||||||
|
var val = dict[key] || (I18N_SHARED[currentLang] && I18N_SHARED[currentLang][key]) || (I18N_SHARED.en && I18N_SHARED.en[key]) || key;
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
function tf(key, vars) {
|
||||||
|
var tpl = t(key);
|
||||||
|
return Object.keys(vars || {}).reduce(function (acc, k) {
|
||||||
|
return acc.replace(new RegExp('\\{' + k + '\\}', 'g'), String(vars[k]));
|
||||||
|
}, tpl);
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyLang() {
|
||||||
|
document.documentElement.lang = currentLang;
|
||||||
|
var title = t('pageTitle');
|
||||||
|
if (title) document.title = title;
|
||||||
|
document.querySelectorAll('[data-i18n]').forEach(function (el) {
|
||||||
|
var key = el.getAttribute('data-i18n');
|
||||||
|
el.textContent = t(key);
|
||||||
|
});
|
||||||
|
document.querySelectorAll('[data-i18n-ph]').forEach(function (el) {
|
||||||
|
var key = el.getAttribute('data-i18n-ph');
|
||||||
|
el.placeholder = t(key);
|
||||||
|
});
|
||||||
|
document.querySelectorAll('.lang-btn').forEach(function (btn) {
|
||||||
|
var map = { 'zh-CN': '简', 'zh-TW': '繁', en: 'EN' };
|
||||||
|
btn.classList.toggle('active', btn.textContent === map[currentLang]);
|
||||||
|
});
|
||||||
|
if (typeof applyPageLang === 'function') applyPageLang();
|
||||||
|
}
|
||||||
|
|
||||||
|
window.setLang = function (lang) {
|
||||||
|
currentLang = lang;
|
||||||
|
localStorage.setItem('lang', lang);
|
||||||
|
applyLang();
|
||||||
|
};
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
-- 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;
|
|
||||||
@@ -1,194 +0,0 @@
|
|||||||
-- ============================================================================
|
|
||||||
-- 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;
|
|
||||||
95
scripts/sync-test-to-prod.sh
Executable file
95
scripts/sync-test-to-prod.sh
Executable file
@@ -0,0 +1,95 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# 同步测试环境数据到生产环境
|
||||||
|
# 用法: ./scripts/sync-test-to-prod.sh
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
# PostgreSQL 客户端工具路径 (Homebrew libpq)
|
||||||
|
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
|
||||||
|
|
||||||
|
# SSL 配置
|
||||||
|
export PGSSLMODE=verify-full
|
||||||
|
export PGSSLROOTCERT=/etc/ssl/cert.pem
|
||||||
|
|
||||||
|
# 测试环境
|
||||||
|
TEST_DB="postgres://postgres:Voson_2026_Pg18!@db.refining.ltd:5432/secrets-nn-test"
|
||||||
|
|
||||||
|
# 生产环境
|
||||||
|
PROD_DB="postgres://postgres:Voson_2026_Pg18!@db.refining.ltd:5432/secrets-nn-prod"
|
||||||
|
|
||||||
|
echo "========================================="
|
||||||
|
echo " 测试环境 -> 生产环境 数据同步"
|
||||||
|
echo "========================================="
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# 确认操作
|
||||||
|
read -p "⚠️ 此操作将覆盖生产环境数据,确认继续? (yes/no): " confirm
|
||||||
|
if [ "$confirm" != "yes" ]; then
|
||||||
|
echo "已取消"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "步骤 1/4: 导出测试环境数据..."
|
||||||
|
TEMP_DIR=$(mktemp -d)
|
||||||
|
trap "rm -rf $TEMP_DIR" EXIT
|
||||||
|
|
||||||
|
# 导出测试环境数据(不含审计日志和历史记录)
|
||||||
|
pg_dump "$TEST_DB" \
|
||||||
|
--table=entries \
|
||||||
|
--table=secrets \
|
||||||
|
--table=entry_secrets \
|
||||||
|
--table=users \
|
||||||
|
--table=oauth_accounts \
|
||||||
|
--data-only \
|
||||||
|
--column-inserts \
|
||||||
|
--no-owner \
|
||||||
|
--no-privileges \
|
||||||
|
> "$TEMP_DIR/test_data.sql"
|
||||||
|
|
||||||
|
echo "✓ 测试数据已导出到临时文件"
|
||||||
|
echo " 文件大小: $(du -h "$TEMP_DIR/test_data.sql" | cut -f1)"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "步骤 2/4: 备份当前生产数据..."
|
||||||
|
pg_dump "$PROD_DB" \
|
||||||
|
--table=entries \
|
||||||
|
--table=secrets \
|
||||||
|
--table=entry_secrets \
|
||||||
|
--table=users \
|
||||||
|
--table=oauth_accounts \
|
||||||
|
--data-only \
|
||||||
|
--column-inserts \
|
||||||
|
--no-owner \
|
||||||
|
--no-privileges \
|
||||||
|
> "$TEMP_DIR/prod_backup_$(date +%Y%m%d_%H%M%S).sql"
|
||||||
|
|
||||||
|
echo "✓ 生产数据已备份"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "步骤 3/4: 清空生产环境目标表..."
|
||||||
|
psql "$PROD_DB" <<'SQL'
|
||||||
|
TRUNCATE TABLE entry_secrets CASCADE;
|
||||||
|
TRUNCATE TABLE secrets CASCADE;
|
||||||
|
TRUNCATE TABLE entries CASCADE;
|
||||||
|
SQL
|
||||||
|
|
||||||
|
echo "✓ 生产环境目标表已清空"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "步骤 4/4: 导入测试数据到生产环境..."
|
||||||
|
psql "$PROD_DB" -f "$TEMP_DIR/test_data.sql" 2>&1 | tail -20
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "验证数据..."
|
||||||
|
echo "生产环境数据统计:"
|
||||||
|
psql "$PROD_DB" -c "SELECT 'users' as table_name, count(*) FROM users UNION ALL SELECT 'entries', count(*) FROM entries UNION ALL SELECT 'secrets', count(*) FROM secrets UNION ALL SELECT 'entry_secrets', count(*) FROM entry_secrets UNION ALL SELECT 'oauth_accounts', count(*) FROM oauth_accounts ORDER BY table_name;"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "========================================="
|
||||||
|
echo " ✓ 数据同步完成!"
|
||||||
|
echo "========================================="
|
||||||
|
echo ""
|
||||||
|
echo "提示:"
|
||||||
|
echo " - 生产数据备份已保存在临时目录"
|
||||||
|
echo " - 临时文件将在脚本退出后自动删除"
|
||||||
Reference in New Issue
Block a user