fix: record original expiry in license restoration audit
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-12 16:23:02 +08:00
parent 09910db251
commit 58278ba6ed
2 changed files with 20 additions and 6 deletions

View File

@@ -4,26 +4,37 @@ import (
"context" "context"
"database/sql" "database/sql"
"database/sql/driver" "database/sql/driver"
"encoding/json"
"errors" "errors"
"github.com/gin-gonic/gin"
"net/http/httptest" "net/http/httptest"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/gin-gonic/gin"
) )
type restoreDB struct { type restoreDB struct {
*dashboardDB *dashboardDB
commands []string commands []string
failAudit bool failAudit bool
wantExpiry time.Time
} }
func (d *restoreDB) Connect(context.Context) (driver.Conn, error) { return d, nil } func (d *restoreDB) Connect(context.Context) (driver.Conn, error) { return d, nil }
func (d *restoreDB) ExecContext(_ context.Context, q string, args []driver.NamedValue) (driver.Result, error) { func (d *restoreDB) ExecContext(_ context.Context, q string, args []driver.NamedValue) (driver.Result, error) {
d.commands = append(d.commands, q) d.commands = append(d.commands, q)
if strings.Contains(q, "INSERT INTO audit_events") && d.failAudit { if strings.Contains(q, "INSERT INTO audit_events") {
var details struct {
ExpiresAt time.Time `json:"expires_at"`
}
if err := json.Unmarshal(args[3].Value.([]byte), &details); err != nil || !details.ExpiresAt.Equal(d.wantExpiry) {
d.t.Fatalf("restore audit expiry mismatch: %v %v", details.ExpiresAt, err)
}
if d.failAudit {
return nil, errors.New("audit unavailable") return nil, errors.New("audit unavailable")
} }
}
return driver.RowsAffected(1), nil return driver.RowsAffected(1), nil
} }
func TestRestoreLicense(t *testing.T) { func TestRestoreLicense(t *testing.T) {
@@ -44,7 +55,7 @@ func TestRestoreLicense(t *testing.T) {
d := &restoreDB{dashboardDB: &dashboardDB{t: t, queries: []dashboardQuery{ d := &restoreDB{dashboardDB: &dashboardDB{t: t, queries: []dashboardQuery{
{sql: licenseSelect + " WHERE l.id=? FOR UPDATE", args: []any{int64(2)}, rows: [][]driver.Value{testLicenseRow(2, tc.status, now.Add(-2*time.Hour), tc.expiry, now)}}, {sql: licenseSelect + " WHERE l.id=? FOR UPDATE", args: []any{int64(2)}, rows: [][]driver.Value{testLicenseRow(2, tc.status, now.Add(-2*time.Hour), tc.expiry, now)}},
{sql: "SELECT id,license_id,operation,idempotency_key,request_fingerprint FROM license_operations WHERE actor_admin_id=? AND operation=? AND idempotency_key=?", args: []any{int64(1), "restore", "restore-test"}}, {sql: "SELECT id,license_id,operation,idempotency_key,request_fingerprint FROM license_operations WHERE actor_admin_id=? AND operation=? AND idempotency_key=?", args: []any{int64(1), "restore", "restore-test"}},
}}, failAudit: tc.fail} }}, failAudit: tc.fail, wantExpiry: tc.expiry}
db := sql.OpenDB(d) db := sql.OpenDB(d)
defer db.Close() defer db.Close()
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@@ -368,6 +368,9 @@ func (a *BusinessAPI) operateLicense(c *gin.Context, operation string) {
apiError(c, 500, "internal_error", "操作失败,请使用相同幂等键重试") apiError(c, 500, "internal_error", "操作失败,请使用相同幂等键重试")
return return
} }
if operation == "restore" {
expires = rec.ExpiresAt
}
details, _ := json.Marshal(map[string]any{"operation": operation, "expires_at": expires, "license_id": id}) details, _ := json.Marshal(map[string]any{"operation": operation, "expires_at": expires, "license_id": id})
if _, err = tx.ExecContext(c, "INSERT INTO audit_events(actor_admin_id,action,resource_type,resource_id,details) VALUES(?,?, 'license',?,?)", adminID, "license."+operation, id, details); err != nil { if _, err = tx.ExecContext(c, "INSERT INTO audit_events(actor_admin_id,action,resource_type,resource_id,details) VALUES(?,?, 'license',?,?)", adminID, "license."+operation, id, details); err != nil {
apiError(c, 500, "internal_error", "服务暂时不可用") apiError(c, 500, "internal_error", "服务暂时不可用")