chore: 移除过时 Python 脚本,密钥生成改用 shell 脚本

- generate_keys.py 替换为 generate_keys.sh (无需 Python 依赖)
- 移除已废弃的 backfill_provider_key_status_snapshot.py
This commit is contained in:
fawney19
2026-04-11 12:43:19 +08:00
parent 80bae8bc2a
commit 3a655440b9
3 changed files with 26 additions and 134 deletions

View File

@@ -1,33 +0,0 @@
#!/usr/bin/env python
"""
生成安全密钥
"""
import secrets
def main() -> None:
# 生成JWT密钥
jwt_key = secrets.token_urlsafe(32)
# 生成独立的加密密钥
encryption_key = secrets.token_urlsafe(32)
# 生成 Redis 密码
redis_password = secrets.token_urlsafe(32)
print("\n将以下内容添加到 .env 文件:\n")
print(f"JWT_SECRET_KEY={jwt_key}")
print(f"ENCRYPTION_KEY={encryption_key}")
print(f"REDIS_PASSWORD={redis_password}")
print()
print("注意:")
print(" - JWT_SECRET_KEY 用于用户登录 token 签名")
print(" - ENCRYPTION_KEY 用于敏感数据加密(如 Provider API Keys")
print(" - REDIS_PASSWORD 用于 Redis 连接认证(并发控制)")
print(" - 这些密钥应该独立设置,避免相互耦合")
print()
if __name__ == "__main__":
main()

26
generate_keys.sh Executable file
View File

@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# 生成安全密钥
set -euo pipefail
urlsafe_rand() { openssl rand -base64 "$1" | tr '+/' '-_' | tr -d '='; }
jwt_key=$(urlsafe_rand 32)
encryption_key=$(urlsafe_rand 32)
redis_password=$(urlsafe_rand 32)
cat <<EOF
将以下内容添加到 .env 文件:
JWT_SECRET_KEY=${jwt_key}
ENCRYPTION_KEY=${encryption_key}
REDIS_PASSWORD=${redis_password}
注意:
- JWT_SECRET_KEY 用于用户登录 token 签名
- ENCRYPTION_KEY 用于敏感数据加密 (如 Provider API Keys)
- REDIS_PASSWORD 用于 Redis 连接认证 (并发控制)
- 这些密钥应该独立设置, 避免相互耦合
EOF

View File

@@ -1,101 +0,0 @@
from __future__ import annotations
import argparse
from typing import Any
from sqlalchemy import select
from sqlalchemy.orm import joinedload, load_only
from src.database import create_session
from src.models.database import Provider, ProviderAPIKey
from src.services.provider_keys.status_snapshot_store import sync_provider_key_status_snapshot
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Backfill provider_api_keys.status_snapshot from existing OAuth/account/quota fields."
)
parser.add_argument(
"--batch-size",
type=int,
default=200,
help="Number of provider keys to process per commit.",
)
parser.add_argument(
"--include-existing",
action="store_true",
help="Recompute rows that already have status_snapshot instead of only filling missing rows.",
)
return parser.parse_args()
def build_batch_stmt(
*,
batch_size: int,
last_id: str | None,
include_existing: bool,
) -> Any:
stmt = (
select(ProviderAPIKey)
.options(joinedload(ProviderAPIKey.provider).load_only(Provider.provider_type))
.order_by(ProviderAPIKey.id)
.limit(batch_size)
)
if last_id is not None:
stmt = stmt.where(ProviderAPIKey.id > last_id)
if not include_existing:
stmt = stmt.where(ProviderAPIKey.status_snapshot.is_(None))
return stmt
def main() -> int:
args = parse_args()
batch_size = max(1, int(args.batch_size or 200))
include_existing = bool(args.include_existing)
db = create_session()
processed = 0
updated = 0
last_id: str | None = None
try:
while True:
batch = (
db.execute(
build_batch_stmt(
batch_size=batch_size,
last_id=last_id,
include_existing=include_existing,
)
)
.scalars()
.all()
)
if not batch:
break
for key in batch:
last_id = str(key.id)
processed += 1
previous = getattr(key, "status_snapshot", None)
current = sync_provider_key_status_snapshot(key)
if current != previous:
updated += 1
db.commit()
print(
f"processed={processed} updated={updated} last_id={last_id}",
flush=True,
)
print(
f"backfill finished: processed={processed} updated={updated} include_existing={include_existing}",
flush=True,
)
return 0
finally:
db.close()
if __name__ == "__main__":
raise SystemExit(main())