账号:使用受控启用凭证查询启动运行实例
This commit is contained in:
1 parent
c4d2d341d4
commit
eb840da41c
5 files changed
+105
-17
No files matched your search
@@ -4,11 +4,55 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ErrInvalidUserID 表示调用方没有提供可用于所有权过滤的正数用户 ID。
|
||||
var ErrInvalidUserID = errors.New("user_id 必须大于 0")
|
||||
|
||||
// RuntimeCookieCredential 表示账号运行实例启动所需的最小凭证视图;Value 仅允许在启动流程内部短暂传递,不得写入日志、响应或持久化状态。
|
||||
type RuntimeCookieCredential struct {
|
||||
// ID 是闲鱼账号的稳定标识,用于绑定运行实例和解密作用域。
|
||||
ID string
|
||||
// Value 是已经由 repository 解密的 Cookie 明文,仅供账号运行时使用。
|
||||
Value string
|
||||
}
|
||||
|
||||
// ListEnabledRuntimeCredentials 返回所有启用账号的运行时 Cookie 凭证;该系统启动视图只选择并解密 Cookie,不读取密码、metadata 或其他账号资料。
|
||||
func (c *Cookies) ListEnabledRuntimeCredentials(ctx context.Context) ([]RuntimeCookieCredential, error) {
|
||||
// rows 是按启用状态过滤后仅包含账号 ID 和 Cookie 密文的查询结果集。
|
||||
rows, err := c.DB.QueryContext(ctx, `
|
||||
SELECT c.id, c.value
|
||||
FROM cookies c
|
||||
LEFT JOIN cookie_status cs ON cs.cookie_id = c.id
|
||||
WHERE COALESCE(cs.enabled, 1) <> 0
|
||||
ORDER BY c.id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
// credentials 保存供账号 supervisor 启动运行实例的最小明文凭证集合。
|
||||
credentials := make([]RuntimeCookieCredential, 0)
|
||||
for rows.Next() {
|
||||
// credential 保存当前行的账号 ID 和待解密 Cookie。
|
||||
var credential RuntimeCookieCredential
|
||||
// encryptedValue 保存数据库中按账号作用域加密的 Cookie 密文。
|
||||
var encryptedValue string
|
||||
// scanErr 表示当前运行时凭证行无法映射到模型的原因。
|
||||
if scanErr := rows.Scan(&credential.ID, &encryptedValue); scanErr != nil {
|
||||
return nil, scanErr
|
||||
}
|
||||
// decryptErr 表示当前账号 Cookie 密文无法用 repository 的数据密钥解密。
|
||||
var decryptErr error
|
||||
credential.Value, decryptErr = c.codec.decrypt("cookie", credential.ID, encryptedValue)
|
||||
if decryptErr != nil {
|
||||
return nil, fmt.Errorf("解密账号 %s Cookie: %w", credential.ID, decryptErr)
|
||||
}
|
||||
credentials = append(credentials, credential)
|
||||
}
|
||||
return credentials, rows.Err()
|
||||
}
|
||||
|
||||
// CookieSummary 表示不包含 Cookie、密码和加密 metadata 的账号摘要。
|
||||
type CookieSummary struct {
|
||||
// ID 是闲鱼账号的稳定标识,不是 Cookie 明文。
|
||||
|
||||
@@ -113,3 +113,46 @@ func TestCookieScopedQueriesExcludeSecrets(t *testing.T) {
|
||||
t.Fatalf("GetValueOwned userID=0 应拒绝,err=%v", invalidValueErr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListEnabledRuntimeCredentials 只返回启用账号的 Cookie,并验证不会因其他敏感字段损坏而扩大读取范围。
|
||||
func TestListEnabledRuntimeCredentials(t *testing.T) {
|
||||
// store 是当前测试使用的 SQLite repository 聚合器。
|
||||
store, cleanup := newTestDB(t)
|
||||
defer cleanup()
|
||||
// ctx 是测试数据库操作共用的上下文。
|
||||
ctx := context.Background()
|
||||
// ownerID 是测试账号所属用户,用于创建两条账号记录。
|
||||
var ownerID int64
|
||||
// ownerCreateErr 表示创建测试用户失败的原因。
|
||||
if ownerCreateErr := store.DB.QueryRowContext(ctx,
|
||||
`INSERT INTO users (username,email,password_hash) VALUES (?,?,?) RETURNING id`,
|
||||
"runtime-owner", "[email protected]", "test-hash").Scan(&ownerID); ownerCreateErr != nil {
|
||||
t.Fatalf("创建测试用户: %v", ownerCreateErr)
|
||||
}
|
||||
// enabledSaveErr 表示创建启用账号失败的原因。
|
||||
if enabledSaveErr := store.Cookies.Save(ctx, "runtime-enabled", "runtime-cookie", ownerID); enabledSaveErr != nil {
|
||||
t.Fatalf("创建启用账号: %v", enabledSaveErr)
|
||||
}
|
||||
// disabledSaveErr 表示创建禁用账号失败的原因。
|
||||
if disabledSaveErr := store.Cookies.Save(ctx, "runtime-disabled", "disabled-cookie", ownerID); disabledSaveErr != nil {
|
||||
t.Fatalf("创建禁用账号: %v", disabledSaveErr)
|
||||
}
|
||||
// statusErr 表示将测试账号标记为禁用失败的原因。
|
||||
if statusErr := store.Cookies.SetStatus(ctx, "runtime-disabled", false); statusErr != nil {
|
||||
t.Fatalf("禁用账号: %v", statusErr)
|
||||
}
|
||||
// corruptErr 表示写入故意损坏的密码和 metadata 测试值失败的原因。
|
||||
if _, corruptErr := store.DB.ExecContext(ctx,
|
||||
`UPDATE cookies SET password=?, metadata_json=? WHERE id=?`,
|
||||
"not-a-password-ciphertext", "not-a-metadata-ciphertext", "runtime-enabled"); corruptErr != nil {
|
||||
t.Fatalf("损坏非运行时字段: %v", corruptErr)
|
||||
}
|
||||
// credentials 是只应包含启用账号的最小运行时凭证集合。
|
||||
credentials, listErr := store.Cookies.ListEnabledRuntimeCredentials(ctx)
|
||||
if listErr != nil {
|
||||
t.Fatalf("ListEnabledRuntimeCredentials: %v", listErr)
|
||||
}
|
||||
if len(credentials) != 1 || credentials[0].ID != "runtime-enabled" || credentials[0].Value != "runtime-cookie" {
|
||||
t.Fatalf("运行时凭证范围或值错误: %#v", credentials)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user