保存日志权限分页与归档并发修正,继续运行验收
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-08 22:44:21 +08:00
1 parent 5faadcd6eb
commit b77b272a5e
16 files changed
+280 -40

No files matched your search

+9 -1
View File
@@ -2,6 +2,7 @@ package controllers
import (
"context"
"errors"
"net/http"
"strconv"
"time"
@@ -12,14 +13,21 @@ import (
"git.pcux.cn/xuans/Smail/services/logstore"
"git.pcux.cn/xuans/Smail/services/runtimelog"
appcontext "git.pcux.cn/xuans/Smail/utils/context"
log "github.com/sirupsen/logrus"
)
func logQueryError(w http.ResponseWriter, err error) {
if errors.Is(err, logstore.ErrInvalid) || errors.Is(err, runtimelog.ErrInvalid) {
writeConfigError(w, config.ErrInvalidAuditQuery)
return
}
log.WithError(err).Warn("日志查询失败")
response.NewErrorResponse(response.ServerError, "日志查询失败,请检查日志存储状态", "LOG_QUERY_FAILED").FPrint(w)
}
// GetLogs 只读日志入口,授权在服务端检查,不依赖导航隐藏。
func GetLogs(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
w.Header().Set("Cache-Control", "no-store")
if err := authorizeCapability(ctx, access.CapabilityAuditRead, "", false); err != nil {
writeConfigAccessError(w, err)
return
@@ -42,7 +50,7 @@ func GetLogs(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request)
if values.Get("kind") == "runtime" {
writer := runtimelog.Current()
if writer == nil {
logQueryError(w, runtimelog.ErrInvalid)
logQueryError(w, logstore.ErrUnavailable)
return
}
if values.Get("file") == "" {
+5 -1
View File
@@ -94,7 +94,7 @@ func TestAdminPluginRestartScope(t *testing.T) {
func TestManagementEndpointCatalog(t *testing.T) {
endpoints := ManagementEndpoints()
if len(endpoints) != 34 {
if len(endpoints) != 35 {
t.Fatalf("unexpected endpoint count: %d", len(endpoints))
}
seen := map[string]struct{}{}
@@ -112,6 +112,10 @@ func TestManagementEndpointCatalog(t *testing.T) {
if !ok || endpoint.Capability != CapabilityPluginWriteOwn {
t.Fatal("plugin settings endpoint was not matched")
}
endpoint, ok = LookupEndpoint("GET", "/api/v1/logs")
if !ok || endpoint.Capability != CapabilityAuditRead {
t.Fatal("log endpoint was not matched")
}
endpoint, ok = LookupEndpoint("POST", "/api/v1/config/smtp/validate")
if !ok || endpoint.Capability != CapabilityConfigWriteGlobal {
t.Fatal("configuration validation endpoint was not matched")
+1
View File
@@ -27,6 +27,7 @@ var managementEndpoints = []Endpoint{
{Method: "GET", Path: "/api/v1/config/:section", Capability: CapabilityConfigRead},
{Method: "PUT", Path: "/api/v1/config/:section", Capability: CapabilityConfigWriteGlobal},
{Method: "GET", Path: "/api/v1/audits/config", Capability: CapabilityAuditRead},
{Method: "GET", Path: "/api/v1/logs", Capability: CapabilityAuditRead},
{Method: "GET", Path: "/api/v1/audits/config/:id", Capability: CapabilityAuditRead},
{Method: "POST", Path: "/api/v1/audits/config", Capability: CapabilityAuditExport},
{Method: "POST", Path: "/api/v1/audits/config/export", Capability: CapabilityAuditExport},
+9 -5
View File
@@ -81,19 +81,24 @@ func (w *Writer) Maintain(ctx context.Context, now time.Time) error {
if now.IsZero() {
return ErrInvalid
}
w.mu.Lock()
defer w.mu.Unlock()
w.archiveMu.Lock()
defer w.archiveMu.Unlock()
if w.closed {
return nil
}
// 关闭当前文件后维护,避免 Windows 无法替换句柄;下一条日志会重新打开。
if w.file != nil {
cutoff := logpolicy.Cutoff(now)
w.mu.Lock()
openDay, _ := time.Parse("2006-01-02", w.day)
// 仅关闭本轮要处理的历史句柄,当天写入不与历史文件扫描共用长时间锁。
if w.file != nil && (!openDay.After(cutoff) || !openDay.Add(24*time.Hour).After(now.Add(-logpolicy.CompressionAge))) {
err := errors.Join(w.file.Sync(), w.file.Close())
w.file = nil
if err != nil {
w.mu.Unlock()
return err
}
}
w.mu.Unlock()
directory, err := w.root.Open(".")
if err != nil {
return err
@@ -103,7 +108,6 @@ func (w *Writer) Maintain(ctx context.Context, now time.Time) error {
if err != nil || closeErr != nil {
return errors.Join(err, closeErr)
}
cutoff := logpolicy.Cutoff(now)
var failures []error
for _, entry := range entries {
if err := ctx.Err(); err != nil {
+4 -4
View File
@@ -22,8 +22,8 @@ type Page struct {
}
func (w *Writer) Files() ([]File, error) {
w.mu.Lock()
defer w.mu.Unlock()
w.archiveMu.Lock()
defer w.archiveMu.Unlock()
if w.closed {
return nil, ErrInvalid
}
@@ -57,8 +57,8 @@ func (w *Writer) Files() ([]File, error) {
// Read 仅允许固定名称的受管日志,不接受任意路径;压缩内容在服务端流式读取。
func (w *Writer) Read(ctx context.Context, name string, offset, limit int) (Page, error) {
w.mu.Lock()
defer w.mu.Unlock()
w.archiveMu.Lock()
defer w.archiveMu.Unlock()
if _, ok := fileDay(name); !ok || offset < 0 || offset > 1000000 || limit < 1 || limit > 500 || w.closed {
return Page{}, ErrInvalid
}
+23 -13
View File
@@ -24,14 +24,15 @@ var ErrInvalid = errors.New("运行日志文件或查询参数无效")
var active atomic.Pointer[Writer]
type Writer struct {
mu sync.Mutex
root *os.Root
file *os.File
day string
now func() time.Time
cancel context.CancelFunc
done chan struct{}
closed bool
archiveMu sync.Mutex
mu sync.Mutex
root *os.Root
file *os.File
day string
now func() time.Time
cancel context.CancelFunc
done chan struct{}
closed bool
}
func Current() *Writer { return active.Load() }
@@ -95,11 +96,6 @@ func (w *Writer) Fire(entry *log.Entry) error {
}
func (w *Writer) WriteAt(at time.Time, message string) error {
w.mu.Lock()
defer w.mu.Unlock()
if w.closed {
return nil
}
if at.IsZero() {
return ErrInvalid
}
@@ -111,6 +107,18 @@ func (w *Writer) WriteAt(at time.Time, message string) error {
message = string([]rune(message)[:min(len([]rune(message)), (maxLineBytes-128)/4)]) + "…"
}
day := at.UTC().Format("2006-01-02")
w.mu.Lock()
// 当天句柄已打开时直接追加,不等待历史归档的读取或压缩。
if !w.closed && (w.file == nil || w.day != day) {
w.mu.Unlock()
w.archiveMu.Lock()
defer w.archiveMu.Unlock()
w.mu.Lock()
}
defer w.mu.Unlock()
if w.closed {
return nil
}
if w.file == nil || w.day != day {
if w.file != nil {
if err := errors.Join(w.file.Sync(), w.file.Close()); err != nil {
@@ -158,6 +166,8 @@ func (w *Writer) Close() error {
w.cancel()
<-w.done
}
w.archiveMu.Lock()
defer w.archiveMu.Unlock()
w.mu.Lock()
defer w.mu.Unlock()
if w.closed {