[日志审计]增加删除、清理和别的一些设置

This commit is contained in:
刘祥超
2020-12-02 20:31:42 +08:00
parent 86fef9153a
commit 81681f415d
28 changed files with 485 additions and 78 deletions

View File

@@ -2,11 +2,11 @@ package index
import (
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeAdmin/internal/setup"
"github.com/TeaOSLab/EdgeAdmin/internal/uimanager"
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
@@ -50,7 +50,7 @@ func (this *IndexAction) RunGet(params struct {
this.Data["token"] = stringutil.Md5(TokenSalt+timestamp) + timestamp
this.Data["from"] = params.From
config, err := uimanager.LoadUIConfig()
config, err := configloaders.LoadUIConfig()
if err != nil {
this.ErrorPage(err)
return

View File

@@ -0,0 +1,100 @@
package log
import (
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
)
type CleanAction struct {
actionutils.ParentAction
}
func (this *CleanAction) Init() {
this.Nav("", "", "clean")
}
func (this *CleanAction) RunGet(params struct{}) {
// 读取配置
config, err := configloaders.LoadLogConfig()
if err != nil {
this.ErrorPage(err)
return
}
if !config.CanClean {
this.WriteString("已设置不能清理")
return
}
this.Data["logConfig"] = config
sizeResp, err := this.RPC().LogRPC().SumLogsSize(this.AdminContext(), &pb.SumLogsSizeRequest{})
if err != nil {
this.ErrorPage(err)
return
}
sizeHuman := ""
if sizeResp.SizeBytes < 1024 {
sizeHuman = numberutils.FormatInt64(sizeResp.SizeBytes) + "字节"
} else if sizeResp.SizeBytes < 1024*1024 {
sizeHuman = fmt.Sprintf("%.2fK", float64(sizeResp.SizeBytes)/1024)
} else if sizeResp.SizeBytes < 1024*1024*1024 {
sizeHuman = fmt.Sprintf("%.2fM", float64(sizeResp.SizeBytes)/1024/1024)
} else {
sizeHuman = fmt.Sprintf("%.2fG", float64(sizeResp.SizeBytes)/1024/1024/1024)
}
this.Data["size"] = sizeHuman
this.Show()
}
func (this *CleanAction) RunPost(params struct {
Type string
Days int32
Must *actions.Must
CSRF *actionutils.CSRF
}) {
// 读取配置
config, err := configloaders.LoadLogConfig()
if err != nil {
this.ErrorPage(err)
return
}
if !config.CanClean {
this.WriteString("已设置不能清理")
return
}
switch params.Type {
case "all":
defer this.CreateLogInfo("清除全部日志")
_, err := this.RPC().LogRPC().CleanLogsPermanently(this.AdminContext(), &pb.CleanLogsPermanentlyRequest{
Days: 0,
ClearAll: true,
})
if err != nil {
this.ErrorPage(err)
return
}
case "days":
defer this.CreateLogInfo("清除 %d 以前的日志", params.Days)
_, err := this.RPC().LogRPC().CleanLogsPermanently(this.AdminContext(), &pb.CleanLogsPermanentlyRequest{
Days: params.Days,
ClearAll: false,
})
if err != nil {
this.ErrorPage(err)
return
}
default:
this.Fail("不支持的清理方式 '" + params.Type + "'")
}
this.Success()
}

View File

@@ -0,0 +1,37 @@
package log
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type DeleteAction struct {
actionutils.ParentAction
}
func (this *DeleteAction) RunPost(params struct {
LogId int64
}) {
// 读取配置
config, err := configloaders.LoadLogConfig()
if err != nil {
this.ErrorPage(err)
return
}
if !config.CanDelete {
this.Fail("已设置不能删除")
}
// 记录日志
defer this.CreateLogInfo("删除单个操作日志 %d", params.LogId)
// 执行删除
_, err = this.RPC().LogRPC().DeleteLogPermanently(this.AdminContext(), &pb.DeleteLogPermanentlyRequest{LogId: params.LogId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -1,6 +1,7 @@
package log
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/lists"
@@ -14,7 +15,7 @@ type IndexAction struct {
}
func (this *IndexAction) Init() {
this.Nav("log", "log", "")
this.Nav("log", "log", "list")
}
func (this *IndexAction) RunGet(params struct {
@@ -22,6 +23,14 @@ func (this *IndexAction) RunGet(params struct {
DayTo string
Keyword string
}) {
// 读取配置
config, err := configloaders.LoadLogConfig()
if err != nil {
this.ErrorPage(err)
return
}
this.Data["logConfig"] = config
this.Data["dayFrom"] = params.DayFrom
this.Data["dayTo"] = params.DayTo
this.Data["keyword"] = params.Keyword
@@ -76,6 +85,7 @@ func (this *IndexAction) RunGet(params struct {
}
logMaps = append(logMaps, maps.Map{
"id": log.Id,
"description": log.Description,
"userName": log.UserName,
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt),

View File

@@ -13,6 +13,9 @@ func init() {
Prefix("/log").
Get("", new(IndexAction)).
Get("/exportExcel", new(ExportExcelAction)).
Post("/delete", new(DeleteAction)).
GetPost("/clean", new(CleanAction)).
GetPost("/settings", new(SettingsAction)).
EndAll()
})

View File

@@ -0,0 +1,63 @@
package log
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
)
type SettingsAction struct {
actionutils.ParentAction
}
func (this *SettingsAction) Init() {
this.Nav("", "", "setting")
}
func (this *SettingsAction) RunGet(params struct{}) {
config, err := configloaders.LoadLogConfig()
if err != nil {
this.ErrorPage(err)
return
}
this.Data["logConfig"] = config
this.Show()
}
func (this *SettingsAction) RunPost(params struct {
CanDelete bool
CanClean bool
CapacityJSON []byte
Days int
Must *actions.Must
CSRF *actionutils.CSRF
}) {
capacity := &shared.SizeCapacity{}
err := json.Unmarshal(params.CapacityJSON, capacity)
if err != nil {
this.ErrorPage(err)
return
}
config, err := configloaders.LoadLogConfig()
if err != nil {
this.ErrorPage(err)
return
}
config.CanDelete = params.CanDelete
config.CanClean = params.CanClean
config.Days = params.Days
config.Capacity = capacity
err = configloaders.UpdateLogConfig(config)
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -2,7 +2,7 @@ package security
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/securitymanager"
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
@@ -18,7 +18,7 @@ func (this *IndexAction) Init() {
}
func (this *IndexAction) RunGet(params struct{}) {
config, err := securitymanager.LoadSecurityConfig()
config, err := configloaders.LoadSecurityConfig()
if err != nil {
this.ErrorPage(err)
return
@@ -75,7 +75,7 @@ func (this *IndexAction) RunPost(params struct {
}) {
defer this.CreateLogInfo("修改管理界面安全设置")
config, err := securitymanager.LoadSecurityConfig()
config, err := configloaders.LoadSecurityConfig()
if err != nil {
this.ErrorPage(err)
return
@@ -109,7 +109,7 @@ func (this *IndexAction) RunPost(params struct {
// 允许本地
config.AllowLocal = params.AllowLocal
err = securitymanager.UpdateSecurityConfig(config)
err = configloaders.UpdateSecurityConfig(config)
if err != nil {
this.ErrorPage(err)
return

View File

@@ -1,7 +1,7 @@
package server
import (
"github.com/TeaOSLab/EdgeAdmin/internal/uimanager"
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
@@ -15,7 +15,7 @@ func (this *IndexAction) Init() {
}
func (this *IndexAction) RunGet(params struct{}) {
config, err := uimanager.LoadUIConfig()
config, err := configloaders.LoadUIConfig()
if err != nil {
this.ErrorPage(err)
return
@@ -41,7 +41,7 @@ func (this *IndexAction) RunPost(params struct {
Field("adminSystemName", params.AdminSystemName).
Require("请输入管理员系统名称")
config, err := uimanager.LoadUIConfig()
config, err := configloaders.LoadUIConfig()
if err != nil {
this.ErrorPage(err)
return
@@ -51,7 +51,7 @@ func (this *IndexAction) RunPost(params struct {
config.ShowOpenSourceInfo = params.ShowOpenSourceInfo
config.ShowVersion = params.ShowVersion
config.Version = params.Version
err = uimanager.UpdateUIConfig(config)
err = configloaders.UpdateUIConfig(config)
if err != nil {
this.ErrorPage(err)
return

View File

@@ -1,11 +1,10 @@
package helpers
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
nodes "github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeAdmin/internal/securitymanager"
"github.com/TeaOSLab/EdgeAdmin/internal/setup"
"github.com/TeaOSLab/EdgeAdmin/internal/uimanager"
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
@@ -28,7 +27,7 @@ func (this *UserMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam
var action = actionPtr.Object()
// 安全相关
securityConfig, _ := securitymanager.LoadSecurityConfig()
securityConfig, _ := configloaders.LoadSecurityConfig()
if securityConfig == nil {
action.AddHeader("X-Frame-Options", "SAMEORIGIN")
} else if len(securityConfig.Frame) > 0 {
@@ -84,7 +83,7 @@ func (this *UserMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam
return true
}
config, err := uimanager.LoadUIConfig()
config, err := configloaders.LoadUIConfig()
if err != nil {
action.WriteString(err.Error())
return false
@@ -206,16 +205,16 @@ func (this *UserMustAuth) modules() []maps.Map {
},
},
},
{
"code": "log",
"name": "日志审计",
"icon": "history",
},
{
"code": "settings",
"name": "系统设置",
"icon": "setting",
},
{
"code": "log",
"name": "操作日志",
"icon": "history",
},
}
}

View File

@@ -1,7 +1,7 @@
package helpers
import (
"github.com/TeaOSLab/EdgeAdmin/internal/securitymanager"
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
"github.com/iwind/TeaGo/actions"
"net/http"
@@ -16,7 +16,7 @@ func (this *UserShouldAuth) BeforeAction(actionPtr actions.ActionWrapper, paramN
// 安全相关
action := this.action
securityConfig, _ := securitymanager.LoadSecurityConfig()
securityConfig, _ := configloaders.LoadSecurityConfig()
if securityConfig == nil {
action.AddHeader("X-Frame-Options", "SAMEORIGIN")
} else if len(securityConfig.Frame) > 0 {

View File

@@ -3,8 +3,8 @@ package helpers
import (
"github.com/TeaOSLab/EdgeAdmin/internal/events"
nodes "github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeAdmin/internal/securitymanager"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
"net"
@@ -23,7 +23,7 @@ func init() {
}
// 检查用户IP并支持缓存
func checkIP(config *securitymanager.SecurityConfig, ipAddr string) bool {
func checkIP(config *systemconfigs.SecurityConfig, ipAddr string) bool {
ipCacheLocker.Lock()
ipCache, ok := ipCacheMap[ipAddr]
if ok && ipCache {
@@ -46,7 +46,7 @@ func checkIP(config *securitymanager.SecurityConfig, ipAddr string) bool {
}
// 检查用户IP
func checkIPWithoutCache(config *securitymanager.SecurityConfig, ipAddr string) bool {
func checkIPWithoutCache(config *systemconfigs.SecurityConfig, ipAddr string) bool {
if config == nil {
return true
}