Compare commits
16 Commits
secrets-mc
...
secrets-mc
| Author | SHA1 | Date | |
|---|---|---|---|
| dd24f7cc44 | |||
|
|
aefad33870 | ||
|
|
0ffb81e57f | ||
|
|
4a1654c820 | ||
|
|
a15e2eaf4a | ||
|
|
1518388374 | ||
| b99d821644 | |||
|
|
32f275f88a | ||
|
|
c6fb457734 | ||
| df701f21b9 | |||
| c3c536200e | |||
| 7909f7102d | |||
| 87a29af82d | |||
| 1b11f7e976 | |||
| 08e81363c9 | |||
|
|
beade4503d |
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` 指向该 key 的 `name`。更新 key 记录后,引用方通过服务层解析合并逻辑即可使用新密钥(实现见 `secrets_core::service`)。
|
多个 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.0"
|
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"
|
||||||
|
|||||||
82
README.md
82
README.md
@@ -17,7 +17,10 @@ cargo build --release -p secrets-mcp
|
|||||||
|
|
||||||
| 变量 | 说明 |
|
| 变量 | 说明 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| `SECRETS_DATABASE_URL` | **必填**。PostgreSQL 连接串(建议专用库,如 `secrets-mcp`)。 |
|
| `SECRETS_DATABASE_URL` | **必填**。PostgreSQL 连接串(推荐使用域名,例如 `db.refining.ltd`,避免直连 IP)。 |
|
||||||
|
| `SECRETS_DATABASE_SSL_MODE` | 可选但强烈建议生产必填。推荐 `verify-full`(至少 `verify-ca`),避免回退到弱 TLS 模式。 |
|
||||||
|
| `SECRETS_DATABASE_SSL_ROOT_CERT` | 可选。私有 CA 或自签链路时指定 CA 根证书路径(如 `/etc/secrets/pg-ca.crt`)。 |
|
||||||
|
| `SECRETS_ENV` | 可选。设为 `prod` / `production` 时会拒绝弱 PostgreSQL TLS 模式(`prefer`、`disable`、`allow`、`require`)。 |
|
||||||
| `BASE_URL` | 对外访问基址;OAuth 回调为 `{BASE_URL}/auth/google/callback`。默认 `http://localhost:9315`。 |
|
| `BASE_URL` | 对外访问基址;OAuth 回调为 `{BASE_URL}/auth/google/callback`。默认 `http://localhost:9315`。 |
|
||||||
| `SECRETS_MCP_BIND` | 监听地址,默认 `127.0.0.1:9315`。容器内或直接对外暴露端口时请改为 `0.0.0.0:9315`;反代时常为 `127.0.0.1:9315`。 |
|
| `SECRETS_MCP_BIND` | 监听地址,默认 `127.0.0.1:9315`。容器内或直接对外暴露端口时请改为 `0.0.0.0:9315`;反代时常为 `127.0.0.1:9315`。 |
|
||||||
| `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET` | 可选;不配置则无 Google 登录入口。运行时从环境读取,勿写入 CI、勿打入二进制。 |
|
| `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET` | 可选;不配置则无 Google 登录入口。运行时从环境读取,勿写入 CI、勿打入二进制。 |
|
||||||
@@ -27,16 +30,55 @@ cargo build --release -p secrets-mcp
|
|||||||
cargo run -p secrets-mcp
|
cargo run -p secrets-mcp
|
||||||
```
|
```
|
||||||
|
|
||||||
|
生产推荐示例(PostgreSQL TLS):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
SECRETS_DATABASE_URL=postgres://postgres:***@db.refining.ltd:5432/secrets-mcp
|
||||||
|
SECRETS_DATABASE_SSL_MODE=verify-full
|
||||||
|
SECRETS_DATABASE_SSL_ROOT_CERT=/etc/secrets/pg-ca.crt
|
||||||
|
SECRETS_ENV=production
|
||||||
|
```
|
||||||
|
|
||||||
- **Web**:`BASE_URL`(登录、Dashboard、设置密码短语、创建 API Key)。
|
- **Web**:`BASE_URL`(登录、Dashboard、设置密码短语、创建 API Key)。
|
||||||
- **MCP**:Streamable HTTP 基址 `{BASE_URL}/mcp`,需 `Authorization: Bearer <api_key>` + `X-Encryption-Key: <hex>` 请求头(读密文工具须带密钥)。
|
- **MCP**:Streamable HTTP 基址 `{BASE_URL}/mcp`,需 `Authorization: Bearer <api_key>` + `X-Encryption-Key: <hex>` 请求头(读密文工具须带密钥)。
|
||||||
|
|
||||||
|
## PostgreSQL TLS 加固
|
||||||
|
|
||||||
|
- 推荐将数据库域名单独设置为 `db.refining.ltd`,服务域名保持 `secrets.refining.app`。
|
||||||
|
- 数据库证书建议使用可校验链路(如 Let's Encrypt 或私有 CA),并保证证书 `SAN` 包含 `db.refining.ltd`。
|
||||||
|
- PostgreSQL 侧建议使用 `hostssl` 规则限制应用来源(如 `47.238.146.244/32`),逐步移除公网明文 `host` 访问。
|
||||||
|
- 应用端推荐 `SECRETS_DATABASE_SSL_MODE=verify-full`;仅在过渡阶段可临时用 `verify-ca`。
|
||||||
|
- 可执行运维步骤见 [`deploy/postgres-tls-hardening.md`](deploy/postgres-tls-hardening.md)。
|
||||||
|
|
||||||
## MCP 与 AI 工作流(v0.3+)
|
## MCP 与 AI 工作流(v0.3+)
|
||||||
|
|
||||||
条目在逻辑上以 **`(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`。
|
| 工具 | 需要加密密钥 | 说明 |
|
||||||
|
|------|-------------|------|
|
||||||
|
| `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)
|
||||||
|
|
||||||
@@ -130,24 +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` 中写该 key 条目的 `name`;轮换时只更新 key 对应记录即可。
|
多个条目可共享同一密文字段,通过 `entry_secrets` 中间表实现 N:N 关联:
|
||||||
|
- 添加条目时可通过 `link_secret_names` 参数关联已有的 secret(按 `(user_id, name)` 精确匹配查找)
|
||||||
|
- 同一 secret 可被多个 entry 引用,删除某 entry 不会级联删除被共享的 secret
|
||||||
|
- 当 secret 不再被任何 entry 引用时,自动清理(`NOT EXISTS` 子查询)
|
||||||
|
|
||||||
|
### 类型(Type)
|
||||||
|
|
||||||
|
`type` 字段用于软分类,由用户自由填写,不做任何自动转换或归一化。常见示例:`server`、`service`、`account`、`person`、`document`,但任何值均可接受。
|
||||||
|
|
||||||
## 审计日志
|
## 审计日志
|
||||||
|
|
||||||
@@ -166,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
|
||||||
|
|||||||
@@ -1,4 +1,15 @@
|
|||||||
use anyhow::Result;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use sqlx::postgres::PgSslMode;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct DatabaseConfig {
|
||||||
|
pub url: String,
|
||||||
|
pub ssl_mode: Option<PgSslMode>,
|
||||||
|
pub ssl_root_cert: Option<PathBuf>,
|
||||||
|
pub enforce_strict_tls: bool,
|
||||||
|
}
|
||||||
|
|
||||||
/// Resolve database URL from environment.
|
/// Resolve database URL from environment.
|
||||||
/// Priority: `SECRETS_DATABASE_URL` env var → error.
|
/// Priority: `SECRETS_DATABASE_URL` env var → error.
|
||||||
@@ -18,3 +29,54 @@ pub fn resolve_db_url(override_url: &str) -> Result<String> {
|
|||||||
Example: SECRETS_DATABASE_URL=postgres://user:pass@host:port/dbname"
|
Example: SECRETS_DATABASE_URL=postgres://user:pass@host:port/dbname"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn env_var_non_empty(name: &str) -> Option<String> {
|
||||||
|
std::env::var(name)
|
||||||
|
.ok()
|
||||||
|
.filter(|value| !value.trim().is_empty())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_ssl_mode_from_env() -> Result<Option<PgSslMode>> {
|
||||||
|
let Some(mode) = env_var_non_empty("SECRETS_DATABASE_SSL_MODE") else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
|
let parsed = mode.parse::<PgSslMode>().with_context(|| {
|
||||||
|
format!(
|
||||||
|
"Invalid SECRETS_DATABASE_SSL_MODE='{mode}'. Use one of: disable, allow, prefer, require, verify-ca, verify-full."
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
Ok(Some(parsed))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_ssl_root_cert_from_env() -> Result<Option<PathBuf>> {
|
||||||
|
let Some(path) = env_var_non_empty("SECRETS_DATABASE_SSL_ROOT_CERT") else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
let path = PathBuf::from(path);
|
||||||
|
if !path.exists() {
|
||||||
|
anyhow::bail!(
|
||||||
|
"SECRETS_DATABASE_SSL_ROOT_CERT points to a missing file: {}",
|
||||||
|
path.display()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(Some(path))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_production_env() -> bool {
|
||||||
|
matches!(
|
||||||
|
env_var_non_empty("SECRETS_ENV")
|
||||||
|
.as_deref()
|
||||||
|
.map(|value| value.to_ascii_lowercase()),
|
||||||
|
Some(value) if value == "prod" || value == "production"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resolve_db_config(override_url: &str) -> Result<DatabaseConfig> {
|
||||||
|
Ok(DatabaseConfig {
|
||||||
|
url: resolve_db_url(override_url)?,
|
||||||
|
ssl_mode: parse_ssl_mode_from_env()?,
|
||||||
|
ssl_root_cert: resolve_ssl_root_cert_from_env()?,
|
||||||
|
enforce_strict_tls: is_production_env(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -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 ─────────────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -1,16 +1,66 @@
|
|||||||
use anyhow::Result;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::{PgConnectOptions, PgPoolOptions, PgSslMode};
|
||||||
|
|
||||||
pub async fn create_pool(database_url: &str) -> Result<PgPool> {
|
use crate::config::DatabaseConfig;
|
||||||
|
|
||||||
|
fn build_connect_options(config: &DatabaseConfig) -> Result<PgConnectOptions> {
|
||||||
|
let mut options = PgConnectOptions::from_str(&config.url)
|
||||||
|
.with_context(|| "failed to parse SECRETS_DATABASE_URL".to_string())?;
|
||||||
|
|
||||||
|
if let Some(mode) = config.ssl_mode {
|
||||||
|
options = options.ssl_mode(mode);
|
||||||
|
}
|
||||||
|
if let Some(path) = &config.ssl_root_cert {
|
||||||
|
options = options.ssl_root_cert(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.enforce_strict_tls
|
||||||
|
&& !matches!(
|
||||||
|
options.get_ssl_mode(),
|
||||||
|
PgSslMode::VerifyCa | PgSslMode::VerifyFull
|
||||||
|
)
|
||||||
|
{
|
||||||
|
anyhow::bail!(
|
||||||
|
"Refusing to start in production with weak PostgreSQL TLS mode. \
|
||||||
|
Set SECRETS_DATABASE_SSL_MODE=verify-ca or verify-full."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_pool(config: &DatabaseConfig) -> Result<PgPool> {
|
||||||
tracing::debug!("connecting to database");
|
tracing::debug!("connecting to database");
|
||||||
|
let connect_options = build_connect_options(config)?;
|
||||||
|
|
||||||
|
// 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))
|
||||||
.connect(database_url)
|
.max_lifetime(std::time::Duration::from_secs(1800)) // 30 minutes
|
||||||
|
.idle_timeout(std::time::Duration::from_secs(600)) // 10 minutes
|
||||||
|
.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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,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 (
|
||||||
@@ -110,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);
|
||||||
|
|
||||||
@@ -179,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'
|
||||||
@@ -468,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,
|
||||||
}
|
}
|
||||||
@@ -482,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,
|
||||||
@@ -51,11 +54,39 @@ pub struct EntryRow {
|
|||||||
pub notes: String,
|
pub notes: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Entry row including `name` (used for id-scoped web / service updates).
|
||||||
|
#[derive(Debug, sqlx::FromRow)]
|
||||||
|
pub struct EntryWriteRow {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub version: i64,
|
||||||
|
pub folder: String,
|
||||||
|
#[sqlx(rename = "type")]
|
||||||
|
pub entry_type: String,
|
||||||
|
pub name: String,
|
||||||
|
pub tags: Vec<String>,
|
||||||
|
pub metadata: Value,
|
||||||
|
pub notes: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&EntryWriteRow> for EntryRow {
|
||||||
|
fn from(r: &EntryWriteRow) -> Self {
|
||||||
|
EntryRow {
|
||||||
|
id: r.id,
|
||||||
|
version: r.version,
|
||||||
|
folder: r.folder.clone(),
|
||||||
|
entry_type: r.entry_type.clone(),
|
||||||
|
tags: r.tags.clone(),
|
||||||
|
metadata: r.metadata.clone(),
|
||||||
|
notes: r.notes.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Minimal secret field row fetched before snapshots or cascade deletes.
|
/// Minimal secret field row fetched before snapshots or cascade deletes.
|
||||||
#[derive(Debug, sqlx::FromRow)]
|
#[derive(Debug, sqlx::FromRow)]
|
||||||
pub struct SecretFieldRow {
|
pub struct SecretFieldRow {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub field_name: String,
|
pub name: String,
|
||||||
pub encrypted: Vec<u8>,
|
pub encrypted: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use sqlx::PgPool;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::models::{EntryRow, SecretFieldRow};
|
use crate::models::{EntryRow, EntryWriteRow, SecretFieldRow};
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
pub struct DeletedEntry {
|
pub struct DeletedEntry {
|
||||||
@@ -31,6 +31,62 @@ pub struct DeleteParams<'a> {
|
|||||||
pub user_id: Option<Uuid>,
|
pub user_id: Option<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Delete a single entry by id (multi-tenant: `user_id` must match).
|
||||||
|
pub async fn delete_by_id(pool: &PgPool, entry_id: Uuid, user_id: Uuid) -> Result<DeleteResult> {
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
let row: Option<EntryWriteRow> = sqlx::query_as(
|
||||||
|
"SELECT id, version, folder, type, name, tags, metadata, notes FROM entries \
|
||||||
|
WHERE id = $1 AND user_id = $2 FOR UPDATE",
|
||||||
|
)
|
||||||
|
.bind(entry_id)
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let row = match row {
|
||||||
|
Some(r) => r,
|
||||||
|
None => {
|
||||||
|
tx.rollback().await?;
|
||||||
|
anyhow::bail!("Entry not found");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let folder = row.folder.clone();
|
||||||
|
let entry_type = row.entry_type.clone();
|
||||||
|
let name = row.name.clone();
|
||||||
|
let entry_row: EntryRow = (&row).into();
|
||||||
|
|
||||||
|
snapshot_and_delete(
|
||||||
|
&mut tx,
|
||||||
|
&folder,
|
||||||
|
&entry_type,
|
||||||
|
&name,
|
||||||
|
&entry_row,
|
||||||
|
Some(user_id),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
crate::audit::log_tx(
|
||||||
|
&mut tx,
|
||||||
|
Some(user_id),
|
||||||
|
"delete",
|
||||||
|
&folder,
|
||||||
|
&entry_type,
|
||||||
|
&name,
|
||||||
|
json!({ "source": "web", "entry_id": entry_id }),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
tx.commit().await?;
|
||||||
|
|
||||||
|
Ok(DeleteResult {
|
||||||
|
deleted: vec![DeletedEntry {
|
||||||
|
name,
|
||||||
|
folder,
|
||||||
|
entry_type,
|
||||||
|
}],
|
||||||
|
dry_run: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn run(pool: &PgPool, params: DeleteParams<'_>) -> Result<DeleteResult> {
|
pub async fn run(pool: &PgPool, params: DeleteParams<'_>) -> Result<DeleteResult> {
|
||||||
match params.name {
|
match params.name {
|
||||||
Some(name) => delete_one(pool, name, params.folder, params.dry_run, params.user_id).await,
|
Some(name) => delete_one(pool, name, params.folder, params.dry_run, params.user_id).await,
|
||||||
@@ -66,6 +122,8 @@ 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,
|
||||||
folder: String,
|
folder: String,
|
||||||
#[sqlx(rename = "type")]
|
#[sqlx(rename = "type")]
|
||||||
entry_type: String,
|
entry_type: String,
|
||||||
@@ -74,7 +132,7 @@ async fn delete_one(
|
|||||||
let rows: Vec<DryRunRow> = if let Some(uid) = user_id {
|
let rows: Vec<DryRunRow> = if let Some(uid) = user_id {
|
||||||
if let Some(f) = folder {
|
if let Some(f) = folder {
|
||||||
sqlx::query_as(
|
sqlx::query_as(
|
||||||
"SELECT folder, type FROM entries WHERE user_id = $1 AND folder = $2 AND name = $3",
|
"SELECT id, folder, type FROM entries WHERE user_id = $1 AND folder = $2 AND name = $3",
|
||||||
)
|
)
|
||||||
.bind(uid)
|
.bind(uid)
|
||||||
.bind(f)
|
.bind(f)
|
||||||
@@ -82,25 +140,29 @@ async fn delete_one(
|
|||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
.await?
|
.await?
|
||||||
} else {
|
} else {
|
||||||
sqlx::query_as("SELECT folder, type FROM entries WHERE user_id = $1 AND name = $2")
|
sqlx::query_as(
|
||||||
.bind(uid)
|
"SELECT id, folder, type FROM entries WHERE user_id = $1 AND name = $2",
|
||||||
.bind(name)
|
)
|
||||||
.fetch_all(pool)
|
.bind(uid)
|
||||||
.await?
|
.bind(name)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await?
|
||||||
}
|
}
|
||||||
} else if let Some(f) = folder {
|
} else if let Some(f) = folder {
|
||||||
sqlx::query_as(
|
sqlx::query_as(
|
||||||
"SELECT folder, type FROM entries WHERE user_id IS NULL AND folder = $1 AND name = $2",
|
"SELECT id, folder, type FROM entries WHERE user_id IS NULL AND folder = $1 AND name = $2",
|
||||||
)
|
)
|
||||||
.bind(f)
|
.bind(f)
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
.await?
|
.await?
|
||||||
} else {
|
} else {
|
||||||
sqlx::query_as("SELECT folder, type FROM entries WHERE user_id IS NULL AND name = $1")
|
sqlx::query_as(
|
||||||
.bind(name)
|
"SELECT id, folder, type FROM entries WHERE user_id IS NULL AND name = $1",
|
||||||
.fetch_all(pool)
|
)
|
||||||
.await?
|
.bind(name)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await?
|
||||||
};
|
};
|
||||||
|
|
||||||
return match rows.len() {
|
return match rows.len() {
|
||||||
@@ -257,27 +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 sql = format!(
|
||||||
|
"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?;
|
||||||
|
|
||||||
let deleted = rows
|
let deleted = rows
|
||||||
.iter()
|
.iter()
|
||||||
.map(|r| DeletedEntry {
|
.map(|r| DeletedEntry {
|
||||||
@@ -292,9 +356,27 @@ async fn delete_bulk(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
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(),
|
||||||
@@ -303,7 +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?;
|
|
||||||
snapshot_and_delete(
|
snapshot_and_delete(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
&row.folder,
|
&row.folder,
|
||||||
@@ -323,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(),
|
||||||
@@ -331,6 +411,8 @@ async fn delete_bulk(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
|
|
||||||
Ok(DeleteResult {
|
Ok(DeleteResult {
|
||||||
deleted,
|
deleted,
|
||||||
dry_run: false,
|
dry_run: false,
|
||||||
@@ -364,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",
|
||||||
},
|
},
|
||||||
@@ -393,5 +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)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
async fn maybe_test_pool() -> Option<PgPool> {
|
||||||
|
let Ok(url) = std::env::var("SECRETS_DATABASE_URL") else {
|
||||||
|
eprintln!("skip delete tests: SECRETS_DATABASE_URL is not set");
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let Ok(pool) = PgPool::connect(&url).await else {
|
||||||
|
eprintln!("skip delete tests: cannot connect to database");
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
if let Err(e) = crate::db::migrate(&pool).await {
|
||||||
|
eprintln!("skip delete tests: migrate failed: {e}");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(pool)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cleanup_single_user_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 delete_dry_run_reports_matching_entry_without_writes() -> Result<()> {
|
||||||
|
let Some(pool) = maybe_test_pool().await else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let suffix = Uuid::from_u128(rand::random()).to_string();
|
||||||
|
let marker = format!("delete_dry_{}", &suffix[..8]);
|
||||||
|
let entry_name = format!("{}_entry", marker);
|
||||||
|
|
||||||
|
cleanup_single_user_rows(&pool, &marker).await?;
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
"INSERT INTO entries (user_id, folder, type, name, notes, tags, metadata) \
|
||||||
|
VALUES (NULL, $1, 'service', $2, '', '{}', '{}')",
|
||||||
|
)
|
||||||
|
.bind(&marker)
|
||||||
|
.bind(&entry_name)
|
||||||
|
.execute(&pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let result = run(
|
||||||
|
&pool,
|
||||||
|
DeleteParams {
|
||||||
|
name: Some(&entry_name),
|
||||||
|
folder: Some(&marker),
|
||||||
|
entry_type: None,
|
||||||
|
dry_run: true,
|
||||||
|
user_id: None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert!(result.dry_run);
|
||||||
|
assert_eq!(result.deleted.len(), 1);
|
||||||
|
assert_eq!(result.deleted[0].name, entry_name);
|
||||||
|
|
||||||
|
let still_exists: bool = sqlx::query_scalar(
|
||||||
|
"SELECT EXISTS(SELECT 1 FROM entries WHERE user_id IS NULL AND folder = $1 AND name = $2)",
|
||||||
|
)
|
||||||
|
.bind(&marker)
|
||||||
|
.bind(&entry_name)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await?;
|
||||||
|
assert!(still_exists);
|
||||||
|
|
||||||
|
cleanup_single_user_rows(&pool, &marker).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn delete_by_id_removes_entry_and_orphan_secret() -> Result<()> {
|
||||||
|
let Some(pool) = maybe_test_pool().await else {
|
||||||
|
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 entry_name = format!("{}_entry", marker);
|
||||||
|
let secret_name = format!("{}_secret", marker);
|
||||||
|
|
||||||
|
sqlx::query("DELETE FROM entries WHERE user_id = $1 AND folder = $2")
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(&marker)
|
||||||
|
.execute(&pool)
|
||||||
|
.await?;
|
||||||
|
sqlx::query("DELETE FROM secrets WHERE user_id = $1 AND name = $2")
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(&secret_name)
|
||||||
|
.execute(&pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let entry_id: Uuid = sqlx::query_scalar(
|
||||||
|
"INSERT INTO entries (user_id, folder, type, name, notes, tags, metadata) \
|
||||||
|
VALUES ($1, $2, 'service', $3, '', '{}', '{}') RETURNING id",
|
||||||
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(&marker)
|
||||||
|
.bind(&entry_name)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await?;
|
||||||
|
let secret_id: Uuid = sqlx::query_scalar(
|
||||||
|
"INSERT INTO secrets (user_id, name, type, encrypted) VALUES ($1, $2, 'text', $3) RETURNING id",
|
||||||
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(&secret_name)
|
||||||
|
.bind(vec![1_u8, 2, 3])
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.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_eq!(result.deleted.len(), 1);
|
||||||
|
assert_eq!(result.deleted[0].name, entry_name);
|
||||||
|
|
||||||
|
let entry_exists: bool =
|
||||||
|
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM entries WHERE id = $1)")
|
||||||
|
.bind(entry_id)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await?;
|
||||||
|
let secret_exists: bool =
|
||||||
|
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM secrets WHERE id = $1)")
|
||||||
|
.bind(secret_id)
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await?;
|
||||||
|
assert!(!entry_exists);
|
||||||
|
assert!(!secret_exists);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ pub async fn build_env_map(
|
|||||||
let mut combined: HashMap<String, String> = HashMap::new();
|
let mut combined: HashMap<String, String> = HashMap::new();
|
||||||
|
|
||||||
for entry in &entries {
|
for entry in &entries {
|
||||||
let entry_map = build_entry_env_map(pool, entry, only_fields, prefix, master_key).await?;
|
let entry_map =
|
||||||
|
build_entry_env_map(pool, entry, only_fields, prefix, master_key, user_id).await?;
|
||||||
combined.extend(entry_map);
|
combined.extend(entry_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,6 +40,7 @@ async fn build_entry_env_map(
|
|||||||
only_fields: &[String],
|
only_fields: &[String],
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
master_key: &[u8; 32],
|
master_key: &[u8; 32],
|
||||||
|
_user_id: Option<Uuid>,
|
||||||
) -> Result<HashMap<String, String>> {
|
) -> Result<HashMap<String, String>> {
|
||||||
let entry_ids = vec![entry.id];
|
let entry_ids = vec![entry.id];
|
||||||
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
||||||
@@ -49,7 +51,7 @@ async fn build_entry_env_map(
|
|||||||
} else {
|
} else {
|
||||||
all_fields
|
all_fields
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|f| only_fields.contains(&f.field_name))
|
.filter(|f| only_fields.contains(&f.name))
|
||||||
.collect()
|
.collect()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -61,36 +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
|
|
||||||
if let Some(key_ref) = entry.metadata.get("key_ref").and_then(|v| v.as_str()) {
|
|
||||||
let key_entries =
|
|
||||||
fetch_entries(pool, None, Some("key"), Some(key_ref), &[], None, None).await?;
|
|
||||||
|
|
||||||
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, "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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use std::collections::HashMap;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::crypto;
|
use crate::crypto;
|
||||||
use crate::service::search::{fetch_secrets_for_entries, resolve_entry};
|
use crate::service::search::{fetch_secrets_for_entries, resolve_entry, resolve_entry_by_id};
|
||||||
|
|
||||||
/// Decrypt a single named field from an entry.
|
/// Decrypt a single named field from an entry.
|
||||||
/// `folder` is optional; if omitted and multiple entries share the name, an error is returned.
|
/// `folder` is optional; if omitted and multiple entries share the name, an error is returned.
|
||||||
@@ -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,56 @@ pub async fn get_all_secrets(
|
|||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
for f in fields {
|
for f in fields {
|
||||||
let decrypted = crypto::decrypt_json(master_key, &f.encrypted)?;
|
let decrypted = crypto::decrypt_json(master_key, &f.encrypted)?;
|
||||||
map.insert(f.field_name.clone(), decrypted);
|
map.insert(f.name.clone(), decrypted);
|
||||||
|
}
|
||||||
|
Ok(map)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decrypt a single named field from an entry, located by its UUID.
|
||||||
|
pub async fn get_secret_field_by_id(
|
||||||
|
pool: &PgPool,
|
||||||
|
entry_id: Uuid,
|
||||||
|
field_name: &str,
|
||||||
|
master_key: &[u8; 32],
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
) -> Result<Value> {
|
||||||
|
resolve_entry_by_id(pool, entry_id, user_id)
|
||||||
|
.await
|
||||||
|
.map_err(|_| anyhow::anyhow!("Entry with id '{}' not found", entry_id))?;
|
||||||
|
|
||||||
|
let entry_ids = vec![entry_id];
|
||||||
|
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
||||||
|
let fields = secrets_map.get(&entry_id).map(Vec::as_slice).unwrap_or(&[]);
|
||||||
|
|
||||||
|
let field = fields
|
||||||
|
.iter()
|
||||||
|
.find(|f| f.name == field_name)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Secret field '{}' not found", field_name))?;
|
||||||
|
|
||||||
|
crypto::decrypt_json(master_key, &field.encrypted)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decrypt all secret fields from an entry, located by its UUID.
|
||||||
|
/// Returns a map field_name → decrypted Value.
|
||||||
|
pub async fn get_all_secrets_by_id(
|
||||||
|
pool: &PgPool,
|
||||||
|
entry_id: Uuid,
|
||||||
|
master_key: &[u8; 32],
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
) -> Result<HashMap<String, Value>> {
|
||||||
|
// Validate entry exists (and that it belongs to the requesting user)
|
||||||
|
resolve_entry_by_id(pool, entry_id, user_id)
|
||||||
|
.await
|
||||||
|
.map_err(|_| anyhow::anyhow!("Entry with id '{}' not found", entry_id))?;
|
||||||
|
|
||||||
|
let entry_ids = vec![entry_id];
|
||||||
|
let secrets_map = fetch_secrets_for_entries(pool, &entry_ids).await?;
|
||||||
|
let fields = secrets_map.get(&entry_id).map(Vec::as_slice).unwrap_or(&[]);
|
||||||
|
|
||||||
|
let mut map = HashMap::new();
|
||||||
|
for f in fields {
|
||||||
|
let decrypted = crypto::decrypt_json(master_key, &f.encrypted)?;
|
||||||
|
map.insert(f.name.clone(), decrypted);
|
||||||
}
|
}
|
||||||
Ok(map)
|
Ok(map)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
@@ -27,49 +40,50 @@ pub struct SearchResult {
|
|||||||
pub secret_schemas: HashMap<Uuid, Vec<SecretField>>,
|
pub secret_schemas: HashMap<Uuid, Vec<SecretField>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(pool: &PgPool, params: SearchParams<'_>) -> Result<SearchResult> {
|
/// List `entries` rows matching params (paged, ordered per `params.sort`).
|
||||||
let entries = fetch_entries_paged(pool, ¶ms).await?;
|
/// Does not read the `secrets` table.
|
||||||
let entry_ids: Vec<Uuid> = entries.iter().map(|e| e.id).collect();
|
pub async fn list_entries(pool: &PgPool, params: SearchParams<'_>) -> Result<Vec<Entry>> {
|
||||||
let secret_schemas = if !entry_ids.is_empty() {
|
|
||||||
fetch_secret_schemas(pool, &entry_ids).await?
|
|
||||||
} else {
|
|
||||||
HashMap::new()
|
|
||||||
};
|
|
||||||
Ok(SearchResult {
|
|
||||||
entries,
|
|
||||||
secret_schemas,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Fetch entries matching the given filters — returns all matching entries up to FETCH_ALL_LIMIT.
|
|
||||||
pub async fn fetch_entries(
|
|
||||||
pool: &PgPool,
|
|
||||||
folder: Option<&str>,
|
|
||||||
entry_type: Option<&str>,
|
|
||||||
name: Option<&str>,
|
|
||||||
tags: &[String],
|
|
||||||
query: Option<&str>,
|
|
||||||
user_id: Option<Uuid>,
|
|
||||||
) -> Result<Vec<Entry>> {
|
|
||||||
let params = SearchParams {
|
|
||||||
folder,
|
|
||||||
entry_type,
|
|
||||||
name,
|
|
||||||
tags,
|
|
||||||
query,
|
|
||||||
sort: "name",
|
|
||||||
limit: FETCH_ALL_LIMIT,
|
|
||||||
offset: 0,
|
|
||||||
user_id,
|
|
||||||
};
|
|
||||||
fetch_entries_paged(pool, ¶ms).await
|
fetch_entries_paged(pool, ¶ms).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<Entry>> {
|
/// Count `entries` rows matching the same filters as [`list_entries`] (ignores `sort` / `limit` / `offset`).
|
||||||
|
/// Does not read the `secrets` table.
|
||||||
|
pub async fn count_entries(pool: &PgPool, a: &SearchParams<'_>) -> Result<i64> {
|
||||||
|
let (where_clause, _) = entry_where_clause_and_next_idx(a);
|
||||||
|
let sql = format!("SELECT COUNT(*)::bigint FROM entries {where_clause}");
|
||||||
|
let mut q = sqlx::query_scalar::<_, i64>(&sql);
|
||||||
|
if let Some(uid) = a.user_id {
|
||||||
|
q = q.bind(uid);
|
||||||
|
}
|
||||||
|
if let Some(v) = a.folder {
|
||||||
|
q = q.bind(v);
|
||||||
|
}
|
||||||
|
if let Some(v) = a.entry_type {
|
||||||
|
q = q.bind(v);
|
||||||
|
}
|
||||||
|
if let Some(v) = a.name {
|
||||||
|
q = q.bind(v);
|
||||||
|
}
|
||||||
|
if let Some(v) = a.name_query {
|
||||||
|
let pattern = ilike_pattern(v);
|
||||||
|
q = q.bind(pattern);
|
||||||
|
}
|
||||||
|
for tag in a.tags {
|
||||||
|
q = q.bind(tag);
|
||||||
|
}
|
||||||
|
if let Some(v) = a.query {
|
||||||
|
let pattern = ilike_pattern(v);
|
||||||
|
q = q.bind(pattern);
|
||||||
|
}
|
||||||
|
let n = q.fetch_one(pool).await?;
|
||||||
|
Ok(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shared WHERE clause and the next `$n` index (for LIMIT/OFFSET in paged queries).
|
||||||
|
fn entry_where_clause_and_next_idx(a: &SearchParams<'_>) -> (String, i32) {
|
||||||
let mut conditions: Vec<String> = Vec::new();
|
let mut conditions: Vec<String> = Vec::new();
|
||||||
let mut idx: i32 = 1;
|
let mut idx: i32 = 1;
|
||||||
|
|
||||||
// user_id filtering — always comes first when present
|
|
||||||
if a.user_id.is_some() {
|
if a.user_id.is_some() {
|
||||||
conditions.push(format!("user_id = ${}", idx));
|
conditions.push(format!("user_id = ${}", idx));
|
||||||
idx += 1;
|
idx += 1;
|
||||||
@@ -89,6 +103,10 @@ async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<
|
|||||||
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
|
||||||
@@ -115,6 +133,57 @@ async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<
|
|||||||
idx += 1;
|
idx += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let where_clause = if conditions.is_empty() {
|
||||||
|
String::new()
|
||||||
|
} else {
|
||||||
|
format!("WHERE {}", conditions.join(" AND "))
|
||||||
|
};
|
||||||
|
(where_clause, idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(pool: &PgPool, params: SearchParams<'_>) -> Result<SearchResult> {
|
||||||
|
let entries = fetch_entries_paged(pool, ¶ms).await?;
|
||||||
|
let entry_ids: Vec<Uuid> = entries.iter().map(|e| e.id).collect();
|
||||||
|
let secret_schemas = if !entry_ids.is_empty() {
|
||||||
|
fetch_secret_schemas(pool, &entry_ids).await?
|
||||||
|
} else {
|
||||||
|
HashMap::new()
|
||||||
|
};
|
||||||
|
Ok(SearchResult {
|
||||||
|
entries,
|
||||||
|
secret_schemas,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch entries matching the given filters — returns all matching entries up to FETCH_ALL_LIMIT.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub async fn fetch_entries(
|
||||||
|
pool: &PgPool,
|
||||||
|
folder: Option<&str>,
|
||||||
|
entry_type: Option<&str>,
|
||||||
|
name: Option<&str>,
|
||||||
|
tags: &[String],
|
||||||
|
query: Option<&str>,
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
) -> Result<Vec<Entry>> {
|
||||||
|
let params = SearchParams {
|
||||||
|
folder,
|
||||||
|
entry_type,
|
||||||
|
name,
|
||||||
|
name_query: None,
|
||||||
|
tags,
|
||||||
|
query,
|
||||||
|
sort: "name",
|
||||||
|
limit: FETCH_ALL_LIMIT,
|
||||||
|
offset: 0,
|
||||||
|
user_id,
|
||||||
|
};
|
||||||
|
list_entries(pool, params).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<Entry>> {
|
||||||
|
let (where_clause, idx) = entry_where_clause_and_next_idx(a);
|
||||||
|
|
||||||
let order = match a.sort {
|
let order = match a.sort {
|
||||||
"updated" => "updated_at DESC",
|
"updated" => "updated_at DESC",
|
||||||
"created" => "created_at DESC",
|
"created" => "created_at DESC",
|
||||||
@@ -122,14 +191,7 @@ async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<
|
|||||||
};
|
};
|
||||||
|
|
||||||
let limit_idx = idx;
|
let limit_idx = idx;
|
||||||
idx += 1;
|
let offset_idx = idx + 1;
|
||||||
let offset_idx = idx;
|
|
||||||
|
|
||||||
let where_clause = if conditions.is_empty() {
|
|
||||||
String::new()
|
|
||||||
} else {
|
|
||||||
format!("WHERE {}", conditions.join(" AND "))
|
|
||||||
};
|
|
||||||
|
|
||||||
let sql = format!(
|
let sql = format!(
|
||||||
"SELECT id, user_id, folder, type, name, notes, tags, metadata, version, \
|
"SELECT id, user_id, folder, type, name, notes, tags, metadata, version, \
|
||||||
@@ -138,7 +200,6 @@ async fn fetch_entries_paged(pool: &PgPool, a: &SearchParams<'_>) -> Result<Vec<
|
|||||||
);
|
);
|
||||||
|
|
||||||
let mut q = sqlx::query_as::<_, EntryRaw>(&sql);
|
let mut q = sqlx::query_as::<_, EntryRaw>(&sql);
|
||||||
|
|
||||||
if let Some(uid) = a.user_id {
|
if let Some(uid) = a.user_id {
|
||||||
q = q.bind(uid);
|
q = q.bind(uid);
|
||||||
}
|
}
|
||||||
@@ -151,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);
|
||||||
@@ -172,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)
|
||||||
@@ -181,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)
|
||||||
}
|
}
|
||||||
@@ -194,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)
|
||||||
@@ -203,11 +277,42 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resolve exactly one entry by its UUID primary key.
|
||||||
|
///
|
||||||
|
/// Returns an error if the entry does not exist or does not belong to the given user.
|
||||||
|
pub async fn resolve_entry_by_id(
|
||||||
|
pool: &PgPool,
|
||||||
|
id: Uuid,
|
||||||
|
user_id: Option<Uuid>,
|
||||||
|
) -> Result<crate::models::Entry> {
|
||||||
|
let row: Option<EntryRaw> = if let Some(uid) = user_id {
|
||||||
|
sqlx::query_as(
|
||||||
|
"SELECT id, user_id, folder, type, name, notes, tags, metadata, version, \
|
||||||
|
created_at, updated_at FROM entries WHERE id = $1 AND user_id = $2",
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.bind(uid)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
sqlx::query_as(
|
||||||
|
"SELECT id, user_id, folder, type, name, notes, tags, metadata, version, \
|
||||||
|
created_at, updated_at FROM entries WHERE id = $1 AND user_id IS NULL",
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
|
row.map(Entry::from)
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Entry with id '{}' not found", id))
|
||||||
|
}
|
||||||
|
|
||||||
/// Resolve exactly one entry by name, with optional folder for disambiguation.
|
/// Resolve exactly one entry by name, with optional folder for disambiguation.
|
||||||
///
|
///
|
||||||
/// - If `folder` is provided: exact `(folder, name)` match.
|
/// - If `folder` is provided: exact `(folder, name)` match.
|
||||||
@@ -277,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,7 +5,8 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use crate::crypto;
|
use crate::crypto;
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::models::EntryRow;
|
use crate::error::{AppError, DbErrorContext};
|
||||||
|
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,
|
||||||
parse_kv, remove_path,
|
parse_kv, remove_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,5 +430,125 @@ 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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update non-sensitive entry columns by primary key (multi-tenant: `user_id` must match).
|
||||||
|
/// Does not read or modify `secrets` rows.
|
||||||
|
pub struct UpdateEntryFieldsByIdParams<'a> {
|
||||||
|
pub folder: &'a str,
|
||||||
|
pub entry_type: &'a str,
|
||||||
|
pub name: &'a str,
|
||||||
|
pub notes: &'a str,
|
||||||
|
pub tags: &'a [String],
|
||||||
|
pub metadata: &'a serde_json::Value,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_fields_by_id(
|
||||||
|
pool: &PgPool,
|
||||||
|
entry_id: Uuid,
|
||||||
|
user_id: Uuid,
|
||||||
|
params: UpdateEntryFieldsByIdParams<'_>,
|
||||||
|
) -> Result<()> {
|
||||||
|
if params.folder.chars().count() > 128 {
|
||||||
|
anyhow::bail!("folder must be at most 128 characters");
|
||||||
|
}
|
||||||
|
if params.entry_type.chars().count() > 64 {
|
||||||
|
anyhow::bail!("type must be at most 64 characters");
|
||||||
|
}
|
||||||
|
if params.name.chars().count() > 256 {
|
||||||
|
anyhow::bail!("name must be at most 256 characters");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
|
||||||
|
let row: Option<EntryWriteRow> = sqlx::query_as(
|
||||||
|
"SELECT id, version, folder, type, name, tags, metadata, notes FROM entries \
|
||||||
|
WHERE id = $1 AND user_id = $2 FOR UPDATE",
|
||||||
|
)
|
||||||
|
.bind(entry_id)
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let row = match row {
|
||||||
|
Some(r) => r,
|
||||||
|
None => {
|
||||||
|
tx.rollback().await?;
|
||||||
|
return Err(AppError::NotFoundEntry.into());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(e) = db::snapshot_entry_history(
|
||||||
|
&mut tx,
|
||||||
|
db::EntrySnapshotParams {
|
||||||
|
entry_id: row.id,
|
||||||
|
user_id: Some(user_id),
|
||||||
|
folder: &row.folder,
|
||||||
|
entry_type: &row.entry_type,
|
||||||
|
name: &row.name,
|
||||||
|
version: row.version,
|
||||||
|
action: "update",
|
||||||
|
tags: &row.tags,
|
||||||
|
metadata: &row.metadata,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
tracing::warn!(error = %e, "failed to snapshot entry history before web update");
|
||||||
|
}
|
||||||
|
|
||||||
|
let entry_type = params.entry_type.trim();
|
||||||
|
|
||||||
|
let res = sqlx::query(
|
||||||
|
"UPDATE entries SET folder = $1, type = $2, name = $3, notes = $4, tags = $5, metadata = $6, \
|
||||||
|
version = version + 1, updated_at = NOW() \
|
||||||
|
WHERE id = $7 AND version = $8",
|
||||||
|
)
|
||||||
|
.bind(params.folder)
|
||||||
|
.bind(entry_type)
|
||||||
|
.bind(params.name)
|
||||||
|
.bind(params.notes)
|
||||||
|
.bind(params.tags)
|
||||||
|
.bind(params.metadata)
|
||||||
|
.bind(row.id)
|
||||||
|
.bind(row.version)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
if let sqlx::Error::Database(ref d) = e
|
||||||
|
&& d.code().as_deref() == Some("23505")
|
||||||
|
{
|
||||||
|
return AppError::ConflictEntryName {
|
||||||
|
folder: params.folder.to_string(),
|
||||||
|
name: params.name.to_string(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
AppError::Internal(e.into())
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if res.rows_affected() == 0 {
|
||||||
|
tx.rollback().await?;
|
||||||
|
return Err(AppError::ConcurrentModification.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
crate::audit::log_tx(
|
||||||
|
&mut tx,
|
||||||
|
Some(user_id),
|
||||||
|
"update",
|
||||||
|
params.folder,
|
||||||
|
entry_type,
|
||||||
|
params.name,
|
||||||
|
serde_json::json!({
|
||||||
|
"source": "web",
|
||||||
|
"entry_id": entry_id,
|
||||||
|
"fields": ["folder", "type", "name", "notes", "tags", "metadata"],
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -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.0"
|
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;
|
||||||
@@ -21,7 +25,7 @@ use tower_sessions_sqlx_store_chrono::PostgresStore;
|
|||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
use tracing_subscriber::fmt::time::FormatTime;
|
use tracing_subscriber::fmt::time::FormatTime;
|
||||||
|
|
||||||
use secrets_core::config::resolve_db_url;
|
use secrets_core::config::resolve_db_config;
|
||||||
use secrets_core::db::{create_pool, migrate};
|
use secrets_core::db::{create_pool, migrate};
|
||||||
|
|
||||||
use crate::oauth::OAuthConfig;
|
use crate::oauth::OAuthConfig;
|
||||||
@@ -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))?;
|
||||||
@@ -78,9 +90,9 @@ async fn main() -> Result<()> {
|
|||||||
.init();
|
.init();
|
||||||
|
|
||||||
// ── Database ──────────────────────────────────────────────────────────────
|
// ── Database ──────────────────────────────────────────────────────────────
|
||||||
let db_url = resolve_db_url("")
|
let db_config = resolve_db_config("")
|
||||||
.context("Database not configured. Set SECRETS_DATABASE_URL environment variable.")?;
|
.context("Database not configured. Set SECRETS_DATABASE_URL environment variable.")?;
|
||||||
let pool = create_pool(&db_url)
|
let pool = create_pool(&db_config)
|
||||||
.await
|
.await
|
||||||
.context("failed to connect to database")?;
|
.context("failed to connect to database")?;
|
||||||
migrate(&pool)
|
migrate(&pool)
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
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(())
|
||||||
|
}
|
||||||
@@ -8,17 +8,22 @@ use axum::{
|
|||||||
extract::{ConnectInfo, Path, Query, State},
|
extract::{ConnectInfo, Path, Query, State},
|
||||||
http::{HeaderMap, StatusCode, header},
|
http::{HeaderMap, StatusCode, header},
|
||||||
response::{Html, IntoResponse, Redirect, Response},
|
response::{Html, IntoResponse, Redirect, Response},
|
||||||
routing::{get, post},
|
routing::{get, patch, post},
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::json;
|
||||||
use tower_sessions::Session;
|
use tower_sessions::Session;
|
||||||
use uuid::Uuid;
|
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,
|
||||||
|
search::{SearchParams, fetch_secret_schemas, ilike_pattern, list_entries},
|
||||||
|
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,
|
||||||
unbind_oauth_account, update_user_key_setup,
|
unbind_oauth_account, update_user_key_setup,
|
||||||
@@ -78,6 +83,65 @@ struct AuditEntryView {
|
|||||||
detail: String,
|
detail: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Template)]
|
||||||
|
#[template(path = "entries.html")]
|
||||||
|
struct EntriesPageTemplate {
|
||||||
|
user_name: String,
|
||||||
|
user_email: String,
|
||||||
|
entries: Vec<EntryListItemView>,
|
||||||
|
folder_tabs: Vec<FolderTabView>,
|
||||||
|
type_options: Vec<String>,
|
||||||
|
secret_type_options_json: String,
|
||||||
|
filter_folder: String,
|
||||||
|
filter_name: String,
|
||||||
|
filter_type: String,
|
||||||
|
version: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Non-sensitive entry fields; `secrets` lists field names/types only (no ciphertext).
|
||||||
|
struct EntryListItemView {
|
||||||
|
id: String,
|
||||||
|
folder: String,
|
||||||
|
entry_type: String,
|
||||||
|
name: String,
|
||||||
|
notes: String,
|
||||||
|
tags: String,
|
||||||
|
/// Compact JSON for `data-entry-metadata` (dialog editor).
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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).
|
||||||
|
const ENTRIES_PAGE_LIMIT: u32 = 5_000;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct EntriesQuery {
|
||||||
|
folder: Option<String>,
|
||||||
|
name: Option<String>,
|
||||||
|
/// URL query key is `type` (maps to DB column `entries.type`).
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
entry_type: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
// ── App state helpers ─────────────────────────────────────────────────────────
|
// ── App state helpers ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
fn google_cfg(state: &AppState) -> Option<&OAuthConfig> {
|
fn google_cfg(state: &AppState) -> Option<&OAuthConfig> {
|
||||||
@@ -134,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",
|
||||||
@@ -149,6 +214,7 @@ pub fn web_router() -> Router<AppState> {
|
|||||||
.route("/auth/google/callback", get(auth_google_callback))
|
.route("/auth/google/callback", get(auth_google_callback))
|
||||||
.route("/auth/logout", post(auth_logout))
|
.route("/auth/logout", post(auth_logout))
|
||||||
.route("/dashboard", get(dashboard))
|
.route("/dashboard", get(dashboard))
|
||||||
|
.route("/entries", get(entries_page))
|
||||||
.route("/audit", get(audit_page))
|
.route("/audit", get(audit_page))
|
||||||
.route("/account/bind/google", get(account_bind_google))
|
.route("/account/bind/google", get(account_bind_google))
|
||||||
.route(
|
.route(
|
||||||
@@ -160,6 +226,16 @@ pub fn web_router() -> Router<AppState> {
|
|||||||
.route("/api/key-setup", post(api_key_setup))
|
.route("/api/key-setup", post(api_key_setup))
|
||||||
.route("/api/apikey", get(api_apikey_get))
|
.route("/api/apikey", get(api_apikey_get))
|
||||||
.route("/api/apikey/regenerate", post(api_apikey_regenerate))
|
.route("/api/apikey/regenerate", post(api_apikey_regenerate))
|
||||||
|
.route(
|
||||||
|
"/api/entries/{id}",
|
||||||
|
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 {
|
||||||
@@ -189,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)
|
||||||
@@ -478,6 +561,224 @@ async fn dashboard(
|
|||||||
render_template(tmpl)
|
render_template(tmpl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn entries_page(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
session: Session,
|
||||||
|
Query(q): Query<EntriesQuery>,
|
||||||
|
) -> Result<Response, StatusCode> {
|
||||||
|
let Some(user_id) = current_user_id(&session).await else {
|
||||||
|
return Ok(Redirect::to("/login").into_response());
|
||||||
|
};
|
||||||
|
|
||||||
|
let user = match get_user_by_id(&state.pool, user_id).await.map_err(|e| {
|
||||||
|
tracing::error!(error = %e, %user_id, "failed to load user for entries page");
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})? {
|
||||||
|
Some(u) => u,
|
||||||
|
None => return Ok(Redirect::to("/login").into_response()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let folder_filter = q
|
||||||
|
.folder
|
||||||
|
.as_ref()
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|s| s.to_string());
|
||||||
|
let type_filter = q
|
||||||
|
.entry_type
|
||||||
|
.as_ref()
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.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 {
|
||||||
|
folder: folder_filter.as_deref(),
|
||||||
|
entry_type: type_filter.as_deref(),
|
||||||
|
name: None,
|
||||||
|
name_query: name_filter.as_deref(),
|
||||||
|
tags: &[],
|
||||||
|
query: None,
|
||||||
|
sort: "updated",
|
||||||
|
limit: ENTRIES_PAGE_LIMIT,
|
||||||
|
offset: 0,
|
||||||
|
user_id: Some(user_id),
|
||||||
|
};
|
||||||
|
|
||||||
|
let rows = list_entries(&state.pool, params).await.map_err(|e| {
|
||||||
|
tracing::error!(error = %e, "failed to load entries list for web");
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?;
|
||||||
|
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
|
||||||
|
.into_iter()
|
||||||
|
.map(|e| {
|
||||||
|
let secrets: Vec<SecretSummaryView> = secret_schemas
|
||||||
|
.get(&e.id)
|
||||||
|
.map(|fields| {
|
||||||
|
fields
|
||||||
|
.iter()
|
||||||
|
.map(|f| SecretSummaryView {
|
||||||
|
id: f.id.to_string(),
|
||||||
|
name: f.name.clone(),
|
||||||
|
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();
|
||||||
|
|
||||||
|
let tmpl = EntriesPageTemplate {
|
||||||
|
user_name: user.name.clone(),
|
||||||
|
user_email: user.email.clone().unwrap_or_default(),
|
||||||
|
entries,
|
||||||
|
folder_tabs,
|
||||||
|
type_options,
|
||||||
|
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_name: name_filter.unwrap_or_default(),
|
||||||
|
filter_type: type_filter.unwrap_or_default(),
|
||||||
|
version: env!("CARGO_PKG_VERSION"),
|
||||||
|
};
|
||||||
|
|
||||||
|
render_template(tmpl)
|
||||||
|
}
|
||||||
|
|
||||||
async fn audit_page(
|
async fn audit_page(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
session: Session,
|
session: Session,
|
||||||
@@ -751,6 +1052,578 @@ async fn api_apikey_regenerate(
|
|||||||
Ok(Json(ApiKeyResponse { api_key }))
|
Ok(Json(ApiKeyResponse { api_key }))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Entry management (Web UI, non-sensitive fields only) ───────────────────────
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct EntryPatchBody {
|
||||||
|
folder: String,
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
entry_type: String,
|
||||||
|
name: String,
|
||||||
|
notes: String,
|
||||||
|
tags: Vec<String>,
|
||||||
|
metadata: serde_json::Value,
|
||||||
|
}
|
||||||
|
|
||||||
|
type EntryApiError = (StatusCode, Json<serde_json::Value>);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
enum UiLang {
|
||||||
|
ZhCn,
|
||||||
|
ZhTw,
|
||||||
|
En,
|
||||||
|
}
|
||||||
|
|
||||||
|
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") {
|
||||||
|
return (
|
||||||
|
StatusCode::CONFLICT,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "该账号下已存在相同 folder + name 的条目", "此帳號下已存在相同 folder + name 的條目", "An entry with the same folder + name already exists for this account") }),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if msg.contains("must be at most") {
|
||||||
|
return (StatusCode::BAD_REQUEST, Json(json!({ "error": msg })));
|
||||||
|
}
|
||||||
|
tracing::error!(error = %e, "entry mutation failed");
|
||||||
|
(
|
||||||
|
StatusCode::INTERNAL_SERVER_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(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
session: Session,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Path(entry_id): Path<Uuid>,
|
||||||
|
Json(body): Json<EntryPatchBody>,
|
||||||
|
) -> Result<Json<serde_json::Value>, 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 folder = body.folder.trim();
|
||||||
|
let entry_type = body.entry_type.trim();
|
||||||
|
let name = body.name.trim();
|
||||||
|
let notes = body.notes.trim();
|
||||||
|
|
||||||
|
if name.is_empty() {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "name 不能为空", "name 不能為空", "name cannot be empty") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let tags: Vec<String> = body
|
||||||
|
.tags
|
||||||
|
.into_iter()
|
||||||
|
.map(|t| t.trim().to_string())
|
||||||
|
.filter(|t| !t.is_empty())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if !body.metadata.is_object() {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(
|
||||||
|
json!({ "error": tr(lang, "metadata 必须是 JSON 对象", "metadata 必須是 JSON 物件", "metadata must be a JSON object") }),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
update_fields_by_id(
|
||||||
|
&state.pool,
|
||||||
|
entry_id,
|
||||||
|
user_id,
|
||||||
|
UpdateEntryFieldsByIdParams {
|
||||||
|
folder,
|
||||||
|
entry_type,
|
||||||
|
name,
|
||||||
|
notes,
|
||||||
|
tags: &tags,
|
||||||
|
metadata: &body.metadata,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e, lang))?;
|
||||||
|
|
||||||
|
Ok(Json(json!({ "ok": true })))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn api_entry_delete(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
session: Session,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Path(entry_id): Path<Uuid>,
|
||||||
|
) -> Result<Json<serde_json::Value>, 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") })),
|
||||||
|
))?;
|
||||||
|
|
||||||
|
delete_by_id(&state.pool, entry_id, user_id)
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_entry_mutation_err(e, lang))?;
|
||||||
|
|
||||||
|
Ok(Json(json!({
|
||||||
|
"ok": true,
|
||||||
|
})))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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,
|
||||||
|
})))
|
||||||
|
}
|
||||||
|
|
||||||
// ── OAuth / Well-known ────────────────────────────────────────────────────────
|
// ── OAuth / Well-known ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/// RFC 9728 — OAuth 2.0 Protected Resource Metadata.
|
/// RFC 9728 — OAuth 2.0 Protected Resource Metadata.
|
||||||
@@ -795,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,8 +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="/audit" class="sidebar-link active">审计</a>
|
<a href="/entries" class="sidebar-link" data-i18n="navEntries">条目</a>
|
||||||
|
<a href="/audit" class="sidebar-link active" data-i18n="navAudit">审计</a>
|
||||||
</nav>
|
</nav>
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
@@ -100,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>
|
||||||
@@ -138,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;
|
||||||
@@ -148,6 +173,7 @@
|
|||||||
el.title = raw + ' (UTC)';
|
el.title = raw + ' (UTC)';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
applyLang();
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -174,6 +174,7 @@
|
|||||||
<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 active">MCP</a>
|
<a href="/dashboard" class="sidebar-link active">MCP</a>
|
||||||
|
<a href="/entries" class="sidebar-link">条目</a>
|
||||||
<a href="/audit" class="sidebar-link">审计</a>
|
<a href="/audit" class="sidebar-link">审计</a>
|
||||||
</nav>
|
</nav>
|
||||||
</aside>
|
</aside>
|
||||||
|
|||||||
1272
crates/secrets-mcp/templates/entries.html
Normal file
1272
crates/secrets-mcp/templates/entries.html
Normal file
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();
|
||||||
|
};
|
||||||
@@ -3,7 +3,13 @@
|
|||||||
|
|
||||||
# ─── 数据库 ───────────────────────────────────────────────────────────
|
# ─── 数据库 ───────────────────────────────────────────────────────────
|
||||||
# Web 会话(tower-sessions)与业务数据共用此库;启动时会自动 migrate 会话表,无需额外环境变量。
|
# Web 会话(tower-sessions)与业务数据共用此库;启动时会自动 migrate 会话表,无需额外环境变量。
|
||||||
SECRETS_DATABASE_URL=postgres://postgres:PASSWORD@HOST:PORT/secrets-mcp
|
SECRETS_DATABASE_URL=postgres://postgres:PASSWORD@db.refining.ltd:5432/secrets-mcp
|
||||||
|
# 强烈建议生产使用 verify-full(至少 verify-ca)
|
||||||
|
SECRETS_DATABASE_SSL_MODE=verify-full
|
||||||
|
# 私有 CA 或自建链路时填写 CA 根证书路径;使用公共受信 CA 可留空
|
||||||
|
# SECRETS_DATABASE_SSL_ROOT_CERT=/etc/secrets/pg-ca.crt
|
||||||
|
# 当设为 prod/production 时,服务会拒绝弱 TLS 模式(prefer/disable/allow/require)
|
||||||
|
SECRETS_ENV=production
|
||||||
|
|
||||||
# ─── 服务地址 ─────────────────────────────────────────────────────────
|
# ─── 服务地址 ─────────────────────────────────────────────────────────
|
||||||
# 内网监听地址(Cloudflare / Nginx 反代时填内网端口)
|
# 内网监听地址(Cloudflare / Nginx 反代时填内网端口)
|
||||||
|
|||||||
92
deploy/postgres-tls-hardening.md
Normal file
92
deploy/postgres-tls-hardening.md
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
# PostgreSQL TLS Hardening Runbook
|
||||||
|
|
||||||
|
This runbook applies to:
|
||||||
|
|
||||||
|
- PostgreSQL server: `47.117.131.22` (`db.refining.ltd`)
|
||||||
|
- `secrets-mcp` app server: `47.238.146.244` (`secrets.refining.app`)
|
||||||
|
|
||||||
|
## 1) Issue certificate for `db.refining.ltd` (Let's Encrypt + Cloudflare DNS-01)
|
||||||
|
|
||||||
|
Install `acme.sh` on the PostgreSQL server and use a Cloudflare API token with DNS edit permission for the target zone.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl https://get.acme.sh | sh -s email=ops@refining.ltd
|
||||||
|
export CF_Token="your_cloudflare_dns_token"
|
||||||
|
export CF_Zone_ID="your_zone_id"
|
||||||
|
~/.acme.sh/acme.sh --issue --dns dns_cf -d db.refining.ltd --keylength ec-256
|
||||||
|
```
|
||||||
|
|
||||||
|
Install cert/key into a PostgreSQL-readable path:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo mkdir -p /etc/postgresql/tls
|
||||||
|
sudo ~/.acme.sh/acme.sh --install-cert -d db.refining.ltd --ecc \
|
||||||
|
--fullchain-file /etc/postgresql/tls/fullchain.pem \
|
||||||
|
--key-file /etc/postgresql/tls/privkey.pem \
|
||||||
|
--reloadcmd "systemctl reload postgresql || systemctl restart postgresql"
|
||||||
|
sudo chown -R postgres:postgres /etc/postgresql/tls
|
||||||
|
sudo chmod 600 /etc/postgresql/tls/privkey.pem
|
||||||
|
sudo chmod 644 /etc/postgresql/tls/fullchain.pem
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2) Configure PostgreSQL TLS and access rules
|
||||||
|
|
||||||
|
In `postgresql.conf`:
|
||||||
|
|
||||||
|
```conf
|
||||||
|
ssl = on
|
||||||
|
ssl_cert_file = '/etc/postgresql/tls/fullchain.pem'
|
||||||
|
ssl_key_file = '/etc/postgresql/tls/privkey.pem'
|
||||||
|
```
|
||||||
|
|
||||||
|
In `pg_hba.conf`, allow app traffic via TLS only (example):
|
||||||
|
|
||||||
|
```conf
|
||||||
|
hostssl secrets-mcp postgres 47.238.146.244/32 scram-sha-256
|
||||||
|
```
|
||||||
|
|
||||||
|
Keep a safe admin path (`local` socket or restricted source CIDR) before removing old plaintext `host` rules.
|
||||||
|
|
||||||
|
Reload PostgreSQL:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo systemctl reload postgresql
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3) Verify server-side TLS
|
||||||
|
|
||||||
|
```bash
|
||||||
|
openssl s_client -starttls postgres -connect db.refining.ltd:5432 -servername db.refining.ltd
|
||||||
|
```
|
||||||
|
|
||||||
|
The handshake should succeed and the certificate should match `db.refining.ltd`.
|
||||||
|
|
||||||
|
## 4) Update `secrets-mcp` app server env
|
||||||
|
|
||||||
|
Use environment values like:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
SECRETS_DATABASE_URL=postgres://postgres:***@db.refining.ltd:5432/secrets-mcp
|
||||||
|
SECRETS_DATABASE_SSL_MODE=verify-full
|
||||||
|
SECRETS_ENV=production
|
||||||
|
```
|
||||||
|
|
||||||
|
If you use private CA instead of public CA, also set:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
SECRETS_DATABASE_SSL_ROOT_CERT=/etc/secrets/pg-ca.crt
|
||||||
|
```
|
||||||
|
|
||||||
|
Restart `secrets-mcp` after updating env.
|
||||||
|
|
||||||
|
## 5) Verify from app server
|
||||||
|
|
||||||
|
Run positive and negative checks:
|
||||||
|
|
||||||
|
- Positive: app starts, migrations pass, dashboard + MCP API work.
|
||||||
|
- Negative:
|
||||||
|
- wrong hostname -> connection fails
|
||||||
|
- wrong CA file -> connection fails
|
||||||
|
- disable TLS on DB -> connection fails
|
||||||
|
|
||||||
|
This ensures no silent downgrade to weak TLS in production.
|
||||||
@@ -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