取消独立配置审计并将配置结果写入运行日志
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-09 01:46:30 +08:00
1 parent 42a173f742
commit ae224649f3
25 files changed
+291 -313

No files matched your search

+110
View File
@@ -0,0 +1,110 @@
package config
import (
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
"git.pcux.cn/xuans/Smail/services/runtimelog"
log "github.com/sirupsen/logrus"
)
func TestDefaultConfigManagerUsesRuntimeLogAndPreservesLegacyFile(t *testing.T) {
for _, withSecurity := range []bool{false, true} {
t.Run(map[bool]string{false: "默认构造", true: "默认安全配置"}[withSecurity], func(t *testing.T) {
root := t.TempDir()
writeRuntimeFixture(t, root, validRuntimeConfig())
legacy, err := NewFileAuditWriter(root)
if err != nil {
t.Fatal(err)
}
history := []byte("保留旧审计原文,不解析、不导入、不追加\n")
if err := os.WriteFile(legacy.path, history, 0600); err != nil {
t.Fatal(err)
}
logger := log.StandardLogger()
oldHooks, oldOutput, oldFormatter, oldLevel, oldCaller := logger.Hooks, logger.Out, logger.Formatter, logger.GetLevel(), logger.ReportCaller
var output bytes.Buffer
logger.ReplaceHooks(make(log.LevelHooks))
logger.SetOutput(&output)
logger.SetFormatter(&logFormatter{})
logger.SetLevel(log.InfoLevel)
logger.SetReportCaller(true)
t.Cleanup(func() {
logger.ReplaceHooks(oldHooks)
logger.SetOutput(oldOutput)
logger.SetFormatter(oldFormatter)
logger.SetLevel(oldLevel)
logger.SetReportCaller(oldCaller)
})
writer, err := runtimelog.Start(root)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = writer.Close() })
options := ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}
var manager *ConfigManager
if withSecurity {
manager, err = NewConfigManagerWithSecurity(root, options, ConfigSecurity{})
} else {
manager, err = NewConfigManager(root, options)
}
if err != nil {
t.Fatal(err)
}
if _, ok := manager.security.Audit.(RuntimeConfigLogWriter); !ok {
t.Fatal("默认写入器仍使用独立配置审计")
}
payload, err := json.Marshal(manager.Snapshot().Email)
if err != nil {
t.Fatal(err)
}
updated, err := manager.Update(context.Background(), UpdateRequest{Section: SectionEmail, ExpectedGeneration: 1, Payload: payload, Actor: authorizedConfigActor()})
if err != nil {
t.Fatal(err)
}
if _, err := manager.Apply(context.Background(), updated.Generation); err != nil {
t.Fatal(err)
}
if manager.Status().SetupState != SetupStateReady {
t.Fatal("取消审计影响了配置保存")
}
if _, err := manager.AuditQuery(context.Background(), ConfigAuditQuery{}); err != ErrAuditQueryUnavailable {
t.Fatal("默认管理器仍提供审计查询")
}
event := ConfigAuditEvent{Operation: ConfigOperationRollback, ActorID: "1", ActorRole: "super_admin", ActorName: "不应记录的名称", FromGeneration: 2, ToGeneration: 3, Result: "failure", CreatedAt: time.Now(), Changes: []ConfigAuditChange{{Field: "root.database.password", Before: "不应记录的旧值", After: "不应记录的新值"}}}
if err := (RuntimeConfigLogWriter{}).WriteConfigAudit(context.Background(), event); err != nil {
t.Fatal(err)
}
page, err := writer.Query(context.Background(), runtimelog.Query{Limit: 10})
if err != nil {
t.Fatal(err)
}
text := strings.Join(page.Lines, "\n")
if len(page.Lines) != 2 || !strings.Contains(text, "保存配置成功") || !strings.Contains(text, "回滚配置失败") || !strings.Contains(text, "[warning]") {
t.Fatalf("运行日志缺少配置结果:%s", text)
}
if strings.Contains(text, "不应记录") || strings.Contains(text, "password") || strings.Contains(text, "\"changes\"") {
t.Fatal("运行日志泄露配置详情")
}
unchanged, err := os.ReadFile(legacy.path)
if err != nil || !bytes.Equal(unchanged, history) {
t.Fatal("旧审计文件被修改")
}
files, err := os.ReadDir(filepath.Join(root, "log"))
if err != nil {
t.Fatal(err)
}
for _, file := range files {
if strings.Contains(file.Name(), "audit") {
t.Fatal("生成了独立审计文件")
}
}
})
}
}
+35 -7
View File
@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
@@ -39,7 +40,7 @@ func testStore(t *testing.T) *Store {
func TestLogStoreEmptyEntriesAreNotInserted(t *testing.T) {
s := testStore(t)
ctx := context.Background()
for _, kind := range []string{"config", "operation", "mail"} {
for _, kind := range []string{"operation", "mail"} {
for _, payload := range []string{`{}`, `null`, `[]`, `" "`, `{"message":"***"}`} {
if err := s.Write(ctx, Entry{Kind: kind, CreatedAt: time.Now(), Action: "---", Payload: []byte(payload)}); err != nil {
t.Fatal(err)
@@ -129,7 +130,7 @@ func TestLogRetentionBoundariesAndBusinessSafety(t *testing.T) {
}
}
func TestLogDialectStatementsAndAuditReplay(t *testing.T) {
func TestLogDialectStatementsAndRetiredAuditWriter(t *testing.T) {
s := testStore(t)
for _, dialect := range []migration.Dialect{migration.DialectSQLite, migration.DialectMySQL, migration.DialectPostgres} {
s.dialect = dialect
@@ -158,11 +159,38 @@ func TestLogDialectStatementsAndAuditReplay(t *testing.T) {
}
}
items, err := audit.Query(context.Background(), config.ConfigAuditQuery{})
if err != nil || len(items) != 1 || items[0].Changes[0].After != "debug" {
t.Fatalf("audit: %+v, %v", items, err)
if err != nil || len(items) != 0 {
t.Fatalf("已停用的配置审计仍写入数据库:%+v, %v", items, err)
}
data, err := audit.ExportCSV(context.Background(), config.ConfigAuditQuery{})
if err != nil || !strings.Contains(string(data), "tester") || !strings.Contains(string(data), "debug") {
t.Fatalf("csv: %s, %v", data, err)
if err := s.Write(context.Background(), Entry{Kind: "config", CreatedAt: time.Now(), Payload: []byte(`{}`)}); err != ErrInvalid {
t.Fatal("数据库写入入口必须拒绝配置审计")
}
}
func TestRetiredAuditImportPreservesLegacyFiles(t *testing.T) {
s := testStore(t)
root := t.TempDir()
directory := filepath.Join(root, "log")
if err := os.MkdirAll(directory, 0700); err != nil {
t.Fatal(err)
}
path := filepath.Join(directory, "config-audit-v2.jsonl")
history := []byte("旧审计内容不再解析或导入\n")
if err := os.WriteFile(path, history, 0600); err != nil {
t.Fatal(err)
}
audit := &AuditWriter{Store: s}
for index := 0; index < 2; index++ {
if err := audit.ImportLegacy(context.Background(), root); err != nil {
t.Fatal(err)
}
}
data, err := os.ReadFile(path)
if err != nil || string(data) != string(history) {
t.Fatal("旧审计文件被修改")
}
var count int
if err := s.db.QueryRow("SELECT COUNT(*) FROM operation_log").Scan(&count); err != nil || count != 0 {
t.Fatal("已停用的旧审计导入仍写入数据库")
}
}
+1 -1
View File
@@ -69,7 +69,7 @@ func TestLogsAPIRejectsUnscopedAccessAndValidatesQueries(t *testing.T) {
}
}
}
for _, query := range []string{"kind=unknown", "kind=mail&limit=501", "kind=mail&offset=-1", "kind=mail&from=invalid", "kind=mail&from=2026-09-08T00:00:00Z&to=2026-09-01T00:00:00Z"} {
for _, query := range []string{"kind=config", "kind=unknown", "kind=mail&limit=501", "kind=mail&offset=-1", "kind=mail&from=invalid", "kind=mail&from=2026-09-08T00:00:00Z&to=2026-09-01T00:00:00Z"} {
if got := call(access.RoleSuperAdmin, query); got.ErrorNo != response.ParamsError {
t.Fatalf("invalid query accepted: %s => %d", query, got.ErrorNo)
}
+18
View File
@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
@@ -16,6 +17,23 @@ import (
_ "modernc.org/sqlite"
)
func TestRetiredConfigAuditRoutesReturnGone(t *testing.T) {
for _, path := range []string{"/api/v1/audits/config", "/api/v1/audits/config/export", "/api/v1/audits/config/audit-00000000000000000000000000000000"} {
for _, method := range []string{http.MethodGet, http.MethodPost} {
request := httptest.NewRequest(method, path, nil)
recorder := httptest.NewRecorder()
if path == "/api/v1/audits/config" {
configAuditRoute(&appcontext.Context{}, recorder, request)
} else {
configAuditSubRoute(&appcontext.Context{}, recorder, request)
}
if recorder.Code != http.StatusGone || recorder.Header().Get("Cache-Control") != "no-store" || recorder.Header().Get("Content-Disposition") != "" {
t.Fatalf("已停用入口仍可查询或导出:%s %s", method, path)
}
}
}
}
func TestOperationLogUsesCompletedIdentityWithoutSensitivePayload(t *testing.T) {
database, err := sql.Open("sqlite", filepath.Join(t.TempDir(), "operations.db"))
if err != nil {
+4 -1
View File
@@ -31,8 +31,11 @@ test('配置管理只保留服务配置与维护入口,不再包含插件菜
await page.selectSection('invalid')
assert.equal(page.selectedSection.value, 'base')
allowed.add('audit.read')
assert.deepEqual(page.maintenanceOptions.value.map(item => item.value), ['audit'])
assert.deepEqual(page.maintenanceOptions.value.map(item => item.value), [])
await page.selectSection('audit')
assert.equal(page.selectedSection.value, 'base')
allowed.add('config.rollback')
await page.selectSection('backups')
assert.equal(page.activePage.value.component.name, 'ConfigMaintenance')
allowed.delete('config.read')
assert.equal(page.activePage.value, undefined)
+33 -56
View File
@@ -60,61 +60,46 @@ function deferred() {
}
for (const cancelled of [false, true]) {
test(`审计导出身份过期后确认密码再重试,取消不误报失败:取消=${cancelled}`, async context => {
const originalDocument = globalThis.document
let downloads = 0
globalThis.document = { createElement: () => ({ click: () => { downloads++ } }) }
context.after(() => { globalThis.document = originalDocument })
test(`共享身份确认保留单次重试,移除审计不影响其他操作:取消=${cancelled}`, async () => {
const requests = []
let prompts = 0
const { page, messages, successes } = fixture(() => {}, 'audit', {
const helpers = compileRecentAuthentication({
prompt: async () => {
prompts++
if (cancelled) throw 'cancel'
return { value: 'test-password' }
},
http: {
post: async (url, body) => {
requests.push(url)
if (url.endsWith('/reauth')) {
assert.equal(body.password, 'test-password')
return { errorNo: 0 }
}
if (requests.length === 1) {
throw Object.assign(new Error('需要最近认证'), { apiErrorCode: 'RECENT_AUTH_REQUIRED' })
}
return new Blob(['action\nupdate'], { type: 'text/csv' })
}
}
}, {
post: async (url, body) => {
requests.push(url)
assert.equal(body.password, 'test-password')
return { errorNo: 0 }
}
})
await page.exportAudit('csv')
let attempts = 0
const action = helpers.requestWithRecentAuthentication(async () => {
if (++attempts === 1) throw Object.assign(new Error('需要最近认证'), { apiErrorCode: 'RECENT_AUTH_REQUIRED' })
return { errorNo: 0 }
})
if (cancelled) await assert.rejects(action, error => helpers.isRecentAuthenticationCancelled(error))
else assert.equal((await action).errorNo, 0)
assert.equal(prompts, 1)
assert.equal(downloads, cancelled ? 0 : 1)
assert.deepEqual(messages, [])
assert.equal(successes.length, cancelled ? 0 : 1)
assert.equal(page.working.value, '')
assert.deepEqual(requests, cancelled
? ['/api/v1/audits/config']
: ['/api/v1/audits/config', '/api/v1/security/reauth', '/api/v1/audits/config'])
assert.equal(attempts, cancelled ? 1 : 2)
assert.deepEqual(requests, cancelled ? [] : ['/api/v1/security/reauth'])
})
}
test('审计导出确认身份后仍被拒绝时只重试一次,不能下载错误文件', async () => {
let exports = 0
const { page, messages } = fixture(() => {}, 'audit', {
prompt: async () => ({ value: 'test-password' }),
http: {
post: async url => {
if (url.endsWith('/reauth')) return { errorNo: 0 }
exports++
throw Object.assign(new Error('需要最近认证'), { apiErrorCode: 'RECENT_AUTH_REQUIRED' })
}
}
})
await page.exportAudit('csv')
assert.equal(exports, 2)
assert.deepEqual(messages, ['需要最近认证'])
assert.equal(page.working.value, '')
test('移除配置审计查询、导出和维护面板,不保留隐藏请求', async () => {
const requests = []
const { page } = fixture(async path => {
requests.push(path)
return { errorNo: 0, data: { available: true } }
}, 'audit')
await page.loadAll()
assert.deepEqual(requests, ['/api/v1/config/status'])
assert.equal(page.loadAudit, undefined)
assert.equal(page.exportAudit, undefined)
assert.doesNotMatch(source, /audits\/config|auditItems|canExportAudit/)
})
test('证书页从状态接口读取生效反代模式,不额外加载基础配置', async () => {
@@ -210,12 +195,9 @@ test('配置页不加载备份或审计,维护页失败只显示本页错误',
assert.equal(page.editorText.value, '{}')
assert.deepEqual(requests, ['/api/v1/config/status', '/api/v1/config/base'])
const backup = fixture(get, 'backups').page
const audit = fixture(get, 'audit').page
await Promise.all([backup.loadAll(), audit.loadAll()])
await backup.loadAll()
assert.equal(backup.backupsError.value, '配置服务暂时不可用')
assert.equal(audit.auditError.value, '配置服务暂时不可用')
assert.equal(backup.editorText.value, '')
assert.equal(audit.editorText.value, '')
assert.deepEqual(messages, [])
await page.loadBackups()
assert.deepEqual(messages, [])
@@ -229,24 +211,19 @@ test('附属模块失败清除旧数据和回滚选择,重试成功清除错
return { errorNo: 0, data: [{ generation: 2 }] }
})
page.backups.value = [{ generation: 1 }]
page.auditItems.value = [{ id: 'old' }]
page.rollbackGeneration.value = 1
page.rollbackPreview.value = { targetGeneration: 1 }
page.rollbackConfirmed.value = true
await Promise.all([page.loadBackups(), page.loadAudit()])
await page.loadBackups()
assert.deepEqual(page.backups.value, [])
assert.deepEqual(page.auditItems.value, [])
assert.equal(page.rollbackGeneration.value, 0)
assert.equal(page.rollbackPreview.value, null)
assert.equal(page.rollbackConfirmed.value, false)
fail = false
await Promise.all([page.loadBackups(), page.loadAudit()])
await page.loadBackups()
assert.equal(page.backupsError.value, '')
assert.equal(page.auditError.value, '')
assert.equal(page.backups.value.length, 1)
assert.equal(page.auditItems.value.length, 1)
assert.equal(page.backupsLoading.value, false)
assert.equal(page.auditLoading.value, false)
})
test('状态请求失败后可重试恢复,不保留不可用提示', async () => {
@@ -478,10 +455,10 @@ test('独立页模板可编译,编辑器不再连接维护面板且权限绑
assert.match(template, /v-if="canWrite" class="editor-footer"/)
assert.doesNotMatch(template, /v-model="confirmed"/)
assert.doesNotMatch(template, /maintenance-panel|migration-panel|rollback-panel|audit-panel/)
for (const [key, capability] of [['migration', 'canMigrate'], ['restart', 'canRestart'], ['backups', 'canRollback'], ['audit', 'canAudit']]) {
for (const [key, capability] of [['migration', 'canMigrate'], ['restart', 'canRestart'], ['backups', 'canRollback']]) {
assert.ok(maintenance.includes(`pageKey === '${key}' && ${capability}`))
}
assert.match(maintenance, /v-if="canExportAudit"/)
assert.doesNotMatch(maintenance, /audit-panel|canExportAudit|loadAudit/)
for (const name of ['BaseConfig', 'EmailConfig', 'SslConfig', 'PluginsConfig', 'ConfigMaintenance']) {
const page = readFileSync(new URL(`${name}/${name}.vue`, root), 'utf8')
const { descriptor, errors } = parse(page)
+7
View File
@@ -25,6 +25,13 @@ test('日志页面使用共享 HTTP 客户端的命名导出', () => {
assert.match(read('LogSettings.js'), /import\s*\{\s*http\s*\}\s*from\s*['"]@\/utils\/axios['"]/)
})
test('日志分类移除配置审计,保留操作、收发信与运行日志', () => {
const { page } = fixture(async () => ({ errorNo: 0, data: {} }))
assert.deepEqual(page.categories.map(item => item.value), ['operation', 'mail', 'runtime'])
assert.equal(page.category.value, 'operation')
assert.doesNotMatch(read('LogSettings.vue'), /category === 'config'|changes\.length/)
})
test('日志页面模板和样式能编译,详情不使用原始 HTML 渲染', () => {
const require = createRequire(new URL('../../frontend/package.json', import.meta.url))
const { parse, compileScript, compileStyle } = require('@vue/compiler-sfc')
+1
View File
@@ -61,6 +61,7 @@ if ((suite === 'all' || suite === 'backend') && result === 0) {
'backend/config/audit_changes_test.go': 'test/backend/audit_changes_test.go',
'backend/config/json_format_test.go': 'test/backend/json_format_test.go',
'backend/config/log_formatter_test.go': 'test/backend/log_formatter_test.go',
'backend/config/config_runtime_log_test.go': 'test/backend/config_runtime_log_test.go',
'backend/config/deployment_state_test.go': 'test/backend/deployment_state_test.go',
'backend/config/ssl_retention_test.go': 'test/backend/ssl_retention_test.go',
'backend/utils/runtimepath/layout_test.go': 'test/backend/runtime_layout_test.go',