完善日志审计身份与致命错误落盘,补充三数据库清理验收
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-08 22:53:20 +08:00
1 parent b77b272a5e
commit b4337dca40
10 files changed
+314 -11

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
package app
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"git.pcux.cn/xuans/Smail/services/runtimelog"
log "github.com/sirupsen/logrus"
)
func TestFatalErrorIsLoggedBeforeRuntimeClose(t *testing.T) {
for _, panics := range []bool{false, true} {
root := t.TempDir()
previousHooks := log.StandardLogger().ReplaceHooks(make(log.LevelHooks))
writer, err := runtimelog.Start(root)
if err != nil {
log.StandardLogger().ReplaceHooks(previousHooks)
t.Fatal(err)
}
err = func() (runErr error) {
defer finishRuntimeLogs(writer, &runErr)
if panics {
panic("fatal-probe")
}
return errors.New("fatal-probe")
}()
log.StandardLogger().ReplaceHooks(previousHooks)
if err == nil || !strings.Contains(err.Error(), "fatal-probe") {
t.Fatalf("fatal error lost: %v", err)
}
files, err := filepath.Glob(filepath.Join(root, "log", "运行日志", "runtime-*.log"))
if err != nil || len(files) != 1 {
t.Fatalf("missing runtime log: %v", err)
}
data, err := os.ReadFile(files[0])
if err != nil || !strings.Contains(string(data), "fatal-probe") {
t.Fatal("fatal error was not written before close")
}
}
}
+124
View File
@@ -0,0 +1,124 @@
package logstore
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"os"
"strings"
"sync"
"testing"
"time"
"git.pcux.cn/xuans/Smail/db/migration"
"git.pcux.cn/xuans/Smail/utils/logpolicy"
mysql "github.com/go-sql-driver/mysql"
"github.com/lib/pq"
)
// 禁止 database/sql 在断线后重建连接,以免临时表消失后误访问同名的永久表。
type logTestConnector struct {
driver.Connector
mu sync.Mutex
used bool
}
func (c *logTestConnector) Connect(ctx context.Context) (driver.Conn, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.used {
return nil, errors.New("temporary log test connection cannot reconnect")
}
c.used = true
return c.Connector.Connect(ctx)
}
// 使用连接私有的临时表,关闭连接自动回收,不迁移或清空任何既有业务表。
func TestLogStoreServerDatabaseIntegration(t *testing.T) {
for _, test := range []struct {
name, driver, environment string
dialect migration.Dialect
}{
{"mysql", "mysql", "SMAIL_TEST_MYSQL_DSN", migration.DialectMySQL},
{"postgres", "postgres", "SMAIL_TEST_POSTGRES_DSN", migration.DialectPostgres},
} {
t.Run(test.name, func(t *testing.T) {
dsn := strings.TrimSpace(os.Getenv(test.environment))
if dsn == "" {
t.Skip(test.environment + " is not set")
}
var connector driver.Connector
var err error
if test.dialect == migration.DialectMySQL {
configuration, parseErr := mysql.ParseDSN(dsn)
if parseErr != nil {
t.Fatal("test database connection configuration is invalid")
}
connector, err = mysql.NewConnector(configuration)
} else {
connector, err = pq.NewConnector(dsn)
}
if err != nil {
t.Fatal("test database connection configuration is invalid")
}
database := sql.OpenDB(&logTestConnector{Connector: connector})
defer database.Close()
database.SetMaxOpenConns(1)
database.SetMaxIdleConns(1)
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
if err := database.PingContext(ctx); err != nil {
t.Fatal("dedicated test database is unavailable")
}
migrations := migration.DefaultMigrations()
for _, statement := range migrations[len(migrations)-1].StatementsFor(test.dialect) {
if strings.HasPrefix(statement, "CREATE TABLE IF NOT EXISTS ") {
statement = strings.Replace(statement, "CREATE TABLE IF NOT EXISTS ", "CREATE TEMPORARY TABLE ", 1)
}
if _, err := database.ExecContext(ctx, statement); err != nil {
t.Fatal("temporary log schema creation failed")
}
}
store, err := New(database, test.dialect)
if err != nil {
t.Fatal(err)
}
now := time.Now().UTC()
cutoff := logpolicy.Cutoff(now)
store.now = func() time.Time { return now.Add(-logpolicy.Retention) }
for _, kind := range []string{"operation", "config", "mail"} {
for index := 0; index < 1003; index++ {
at := cutoff.Add(-time.Nanosecond)
if index >= 1001 {
at = cutoff.Add(time.Duration(index - 1001))
}
entry := Entry{ID: fmt.Sprintf("%s-%d", kind, index), CreatedAt: at, Kind: kind, ActorID: "42", Action: "兼容验证", Result: "success", Payload: []byte(`{"message":"中文日志"}`)}
if err := store.Write(ctx, entry); err != nil {
t.Fatal(err)
}
if index == 1002 {
if err := store.Write(ctx, entry); err != nil {
t.Fatal("idempotent replay failed")
}
}
}
}
store.now = func() time.Time { return now }
for _, kind := range []string{"operation", "config", "mail"} {
items, err := store.Query(ctx, Query{Kind: kind, ActorID: "42"})
if err != nil || len(items) != 2 || items[0].Action != "兼容验证" {
t.Fatalf("query or retention failed: kind=%s count=%d err=%v", kind, len(items), err)
}
}
deleted, err := store.Cleanup(ctx, now)
if err != nil || deleted != 3003 {
t.Fatalf("batch cleanup failed: deleted=%d err=%v", deleted, err)
}
if deleted, err := store.Cleanup(ctx, now); err != nil || deleted != 0 {
t.Fatalf("repeated cleanup failed: deleted=%d err=%v", deleted, err)
}
})
}
}
+38
View File
@@ -0,0 +1,38 @@
package controllers
import (
"context"
"encoding/json"
"net/http/httptest"
"testing"
"git.pcux.cn/xuans/Smail/config"
"git.pcux.cn/xuans/Smail/dto/response"
"git.pcux.cn/xuans/Smail/models"
"git.pcux.cn/xuans/Smail/services/access"
"git.pcux.cn/xuans/Smail/session"
appcontext "git.pcux.cn/xuans/Smail/utils/context"
"github.com/alexedwards/scs/v2"
)
func TestCompletedLoginSetsAuditActor(t *testing.T) {
previousConfig, previousSession := config.Instance, session.Instance
t.Cleanup(func() { config.Instance, session.Instance = previousConfig, previousSession })
config.Instance = &config.Config{Domain: "example.test"}
session.Instance = scs.New()
loaded, err := session.Instance.Load(context.Background(), "")
if err != nil {
t.Fatal(err)
}
ctx := &appcontext.Context{Context: loaded}
request := httptest.NewRequest("POST", "https://example.test/api/security/login/verify", nil).WithContext(loaded)
writer := httptest.NewRecorder()
completeLoginSession(ctx, writer, request, &models.User{ID: 42, Account: "audit-probe", Role: string(access.RoleSuperAdmin)})
var result response.Response
if err := json.Unmarshal(writer.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
if result.ErrorNo != 0 || ctx.UserID != 42 || ctx.UserAccount != "audit-probe" || ctx.Role != access.RoleSuperAdmin {
t.Fatalf("completed login lost audit actor: code=%d id=%d role=%s", result.ErrorNo, ctx.UserID, ctx.Role)
}
}
+58
View File
@@ -0,0 +1,58 @@
package http_server
import (
"context"
"database/sql"
"encoding/json"
"net/http/httptest"
"path/filepath"
"testing"
"git.pcux.cn/xuans/Smail/db/migration"
"git.pcux.cn/xuans/Smail/dto/response"
"git.pcux.cn/xuans/Smail/services/access"
"git.pcux.cn/xuans/Smail/services/logstore"
appcontext "git.pcux.cn/xuans/Smail/utils/context"
_ "modernc.org/sqlite"
)
func TestOperationLogUsesCompletedIdentityWithoutSensitivePayload(t *testing.T) {
database, err := sql.Open("sqlite", filepath.Join(t.TempDir(), "operations.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
database.SetMaxOpenConns(1)
migrations := migration.DefaultMigrations()
for _, statement := range migrations[len(migrations)-1].StatementsFor(migration.DialectSQLite) {
if _, err := database.Exec(statement); err != nil {
t.Fatal(err)
}
}
store, err := logstore.New(database, migration.DialectSQLite)
if err != nil {
t.Fatal(err)
}
runtime := logstore.Start(store)
defer runtime.Stop()
for _, path := range []string{"/api/v1/login", "/api/v1/security/login/verify", "/api/v1/security/webauthn/login/finish"} {
ctx := &appcontext.Context{}
writer, finish := operationLog(ctx, httptest.NewRequest("POST", path, nil), httptest.NewRecorder())
ctx.UserID, ctx.UserAccount, ctx.Role = 42, "audit-probe", access.RoleSuperAdmin
response.NewSuccessResponse(map[string]string{"token": "must-not-store"}).FPrint(writer)
finish()
}
entries, err := store.Query(context.Background(), logstore.Query{Kind: "operation", ActorID: "42"})
if err != nil || len(entries) != 3 {
t.Fatalf("missing completed login records: %d, %v", len(entries), err)
}
for _, entry := range entries {
var payload map[string]any
if err := json.Unmarshal(entry.Payload, &payload); err != nil {
t.Fatal(err)
}
if entry.Result != "success" || payload["actorName"] != "audit-probe" || len(payload) != 3 {
t.Fatalf("incorrect or excessive audit metadata: %+v", payload)
}
}
}
+4
View File
@@ -49,8 +49,12 @@ if ((suite === 'all' || suite === 'backend') && result === 0) {
const args = extra[0] === '--' ? extra.slice(1) : extra
// 独立回归源码集中放在 test/backend,通过 overlay 保留同包测试语义。
const replacements = {
'backend/internal/app/runtime_log_test.go': 'test/backend/app_runtime_log_test.go',
'backend/listen/http_server/operation_log_test.go': 'test/backend/operation_log_test.go',
'backend/controllers/login_audit_test.go': 'test/backend/login_audit_test.go',
'backend/controllers/logs_api_test.go': 'test/backend/logs_api_test.go',
'backend/services/logstore/log_store_test.go': 'test/backend/log_store_test.go',
'backend/services/logstore/log_store_integration_test.go': 'test/backend/log_store_integration_test.go',
'backend/services/runtimelog/runtime_logs_test.go': 'test/backend/runtime_logs_test.go',
'backend/db/migration/log_migration_test.go': 'test/backend/log_migration_test.go',
'backend/config/audit_changes_test.go': 'test/backend/audit_changes_test.go',