增加WAF日志配置/WAF策略中之日志中增加分表查询

This commit is contained in:
GoEdgeLab
2022-04-21 19:45:25 +08:00
parent 4d089a1af3
commit 305718a03c
8 changed files with 135 additions and 8 deletions

View File

@@ -34,13 +34,15 @@ func (this *LogAction) RunGet(params struct {
this.Data["path"] = this.Request.URL.Path this.Data["path"] = this.Request.URL.Path
this.Data["day"] = params.Day this.Data["day"] = params.Day
this.Data["groupId"] = params.GroupId this.Data["groupId"] = params.GroupId
this.Data["accessLogs"] = []interface{}{} this.Data["accessLogs"] = []maps.Map{}
this.Data["partition"] = params.Partition
day := params.Day var day = params.Day
ipList := []string{} var ipList = []string{}
var wafMaps = []maps.Map{}
if len(day) > 0 && regexp.MustCompile(`\d{4}-\d{2}-\d{2}`).MatchString(day) { if len(day) > 0 && regexp.MustCompile(`\d{4}-\d{2}-\d{2}`).MatchString(day) {
day = strings.ReplaceAll(day, "-", "") day = strings.ReplaceAll(day, "-", "")
size := int64(10) var size = int64(20)
resp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{ resp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
Partition: params.Partition, Partition: params.Partition,
@@ -60,11 +62,31 @@ func (this *LogAction) RunGet(params struct {
} else { } else {
this.Data["accessLogs"] = resp.HttpAccessLogs this.Data["accessLogs"] = resp.HttpAccessLogs
for _, accessLog := range resp.HttpAccessLogs { for _, accessLog := range resp.HttpAccessLogs {
// IP
if len(accessLog.RemoteAddr) > 0 { if len(accessLog.RemoteAddr) > 0 {
if !lists.ContainsString(ipList, accessLog.RemoteAddr) { if !lists.ContainsString(ipList, accessLog.RemoteAddr) {
ipList = append(ipList, accessLog.RemoteAddr) ipList = append(ipList, accessLog.RemoteAddr)
} }
} }
// WAF信息集合
if accessLog.FirewallPolicyId > 0 && accessLog.FirewallRuleGroupId > 0 && accessLog.FirewallRuleSetId > 0 {
// 检查Set是否已经存在
var existSet = false
for _, wafMap := range wafMaps {
if wafMap.GetInt64("setId") == accessLog.FirewallRuleSetId {
existSet = true
break
}
}
if !existSet {
wafMaps = append(wafMaps, maps.Map{
"policyId": accessLog.FirewallPolicyId,
"groupId": accessLog.FirewallRuleGroupId,
"setId": accessLog.FirewallRuleSetId,
})
}
}
} }
} }
this.Data["hasMore"] = resp.HasMore this.Data["hasMore"] = resp.HasMore
@@ -134,5 +156,79 @@ func (this *LogAction) RunGet(params struct {
} }
this.Data["regions"] = regionMap this.Data["regions"] = regionMap
// WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map
var wafPolicyCacheMap = map[int64]*pb.HTTPFirewallPolicy{} // id => *pb.HTTPFirewallPolicy
var wafGroupCacheMap = map[int64]*pb.HTTPFirewallRuleGroup{} // id => *pb.HTTPFirewallRuleGroup
var wafSetCacheMap = map[int64]*pb.HTTPFirewallRuleSet{} // id => *pb.HTTPFirewallRuleSet
for _, wafMap := range wafMaps {
var policyId = wafMap.GetInt64("policyId")
var groupId = wafMap.GetInt64("groupId")
var setId = wafMap.GetInt64("setId")
if policyId > 0 {
pbPolicy, ok := wafPolicyCacheMap[policyId]
if !ok {
policyResp, err := this.RPC().HTTPFirewallPolicyRPC().FindEnabledHTTPFirewallPolicy(this.AdminContext(), &pb.FindEnabledHTTPFirewallPolicyRequest{HttpFirewallPolicyId: policyId})
if err != nil {
this.ErrorPage(err)
return
}
pbPolicy = policyResp.HttpFirewallPolicy
wafPolicyCacheMap[policyId] = pbPolicy
}
if pbPolicy != nil {
wafMap = maps.Map{
"policy": maps.Map{
"id": pbPolicy.Id,
"name": pbPolicy.Name,
"serverId": pbPolicy.ServerId,
},
}
if groupId > 0 {
pbGroup, ok := wafGroupCacheMap[groupId]
if !ok {
groupResp, err := this.RPC().HTTPFirewallRuleGroupRPC().FindEnabledHTTPFirewallRuleGroup(this.AdminContext(), &pb.FindEnabledHTTPFirewallRuleGroupRequest{FirewallRuleGroupId: groupId})
if err != nil {
this.ErrorPage(err)
return
}
pbGroup = groupResp.FirewallRuleGroup
wafGroupCacheMap[groupId] = pbGroup
}
if pbGroup != nil {
wafMap["group"] = maps.Map{
"id": pbGroup.Id,
"name": pbGroup.Name,
}
if setId > 0 {
pbSet, ok := wafSetCacheMap[setId]
if !ok {
setResp, err := this.RPC().HTTPFirewallRuleSetRPC().FindEnabledHTTPFirewallRuleSet(this.AdminContext(), &pb.FindEnabledHTTPFirewallRuleSetRequest{FirewallRuleSetId: setId})
if err != nil {
this.ErrorPage(err)
return
}
pbSet = setResp.FirewallRuleSet
wafSetCacheMap[setId] = pbSet
}
if pbSet != nil {
wafMap["set"] = maps.Map{
"id": pbSet.Id,
"name": pbSet.Name,
}
}
}
}
}
}
}
wafInfos[setId] = wafMap
}
this.Data["wafInfos"] = wafInfos
this.Show() this.Show()
} }

View File

@@ -96,6 +96,7 @@ func (this *PolicyAction) RunGet(params struct {
"blockOptions": firewallPolicy.BlockOptions, "blockOptions": firewallPolicy.BlockOptions,
"useLocalFirewall": firewallPolicy.UseLocalFirewall, "useLocalFirewall": firewallPolicy.UseLocalFirewall,
"synFlood": firewallPolicy.SYNFlood, "synFlood": firewallPolicy.SYNFlood,
"log": firewallPolicy.Log,
} }
// 正在使用此策略的集群 // 正在使用此策略的集群

View File

@@ -58,6 +58,11 @@ func (this *UpdateAction) RunGet(params struct {
} }
} }
// log
if firewallPolicy.Log == nil {
firewallPolicy.Log = firewallconfigs.DefaultHTTPFirewallPolicyLogConfig
}
this.Data["firewallPolicy"] = maps.Map{ this.Data["firewallPolicy"] = maps.Map{
"id": firewallPolicy.Id, "id": firewallPolicy.Id,
"name": firewallPolicy.Name, "name": firewallPolicy.Name,
@@ -67,10 +72,11 @@ func (this *UpdateAction) RunGet(params struct {
"blockOptions": firewallPolicy.BlockOptions, "blockOptions": firewallPolicy.BlockOptions,
"useLocalFirewall": firewallPolicy.UseLocalFirewall, "useLocalFirewall": firewallPolicy.UseLocalFirewall,
"synFloodConfig": firewallPolicy.SYNFlood, "synFloodConfig": firewallPolicy.SYNFlood,
"log": firewallPolicy.Log,
} }
// 预置分组 // 预置分组
groups := []maps.Map{} var groups = []maps.Map{}
templatePolicy := firewallconfigs.HTTPFirewallTemplate() templatePolicy := firewallconfigs.HTTPFirewallTemplate()
for _, group := range templatePolicy.AllRuleGroups() { for _, group := range templatePolicy.AllRuleGroups() {
if len(group.Code) > 0 { if len(group.Code) > 0 {
@@ -101,6 +107,7 @@ func (this *UpdateAction) RunPost(params struct {
Mode string Mode string
UseLocalFirewall bool UseLocalFirewall bool
SynFloodJSON []byte SynFloodJSON []byte
LogJSON []byte
Must *actions.Must Must *actions.Must
}) { }) {
@@ -128,6 +135,7 @@ func (this *UpdateAction) RunPost(params struct {
Mode: params.Mode, Mode: params.Mode,
UseLocalFirewall: params.UseLocalFirewall, UseLocalFirewall: params.UseLocalFirewall,
SynFloodJSON: params.SynFloodJSON, SynFloodJSON: params.SynFloodJSON,
LogJSON: params.LogJSON,
}) })
if err != nil { if err != nil {
this.ErrorPage(err) this.ErrorPage(err)

View File

@@ -60,9 +60,9 @@ Vue.component("http-access-log-box", {
<span v-if="accessLog.wafInfo != null"> <span v-if="accessLog.wafInfo != null">
<a :href="(accessLog.wafInfo.policy.serverId == 0) ? '/servers/components/waf/group?firewallPolicyId=' + accessLog.firewallPolicyId + '&type=inbound&groupId=' + accessLog.firewallRuleGroupId+ '#set' + accessLog.firewallRuleSetId : '/servers/server/settings/waf/group?serverId=' + accessLog.serverId + '&firewallPolicyId=' + accessLog.firewallPolicyId + '&type=inbound&groupId=' + accessLog.firewallRuleGroupId + '#set' + accessLog.firewallRuleSetId" target="_blank"> <a :href="(accessLog.wafInfo.policy.serverId == 0) ? '/servers/components/waf/group?firewallPolicyId=' + accessLog.firewallPolicyId + '&type=inbound&groupId=' + accessLog.firewallRuleGroupId+ '#set' + accessLog.firewallRuleSetId : '/servers/server/settings/waf/group?serverId=' + accessLog.serverId + '&firewallPolicyId=' + accessLog.firewallPolicyId + '&type=inbound&groupId=' + accessLog.firewallRuleGroupId + '#set' + accessLog.firewallRuleSetId" target="_blank">
<code-label-plain> <code-label-plain>
<span class="red"> <span>
WAF -- WAF -
<span v-if="accessLog.wafInfo.group != null">{{accessLog.wafInfo.group.name}} --</span> <span v-if="accessLog.wafInfo.group != null">{{accessLog.wafInfo.group.name}} -</span>
<span v-if="accessLog.wafInfo.set != null">{{accessLog.wafInfo.set.name}}</span> <span v-if="accessLog.wafInfo.set != null">{{accessLog.wafInfo.set.name}}</span>
</span> </span>
</code-label-plain> </code-label-plain>

View File

@@ -25,6 +25,8 @@
</first-menu> </first-menu>
</form> </form>
<http-access-log-partitions-box :v-day="day" :v-partition="partition"></http-access-log-partitions-box>
<p class="comment" v-if="accessLogs.length == 0">暂时还没有日志。</p> <p class="comment" v-if="accessLogs.length == 0">暂时还没有日志。</p>
<table class="ui table selectable" v-if="accessLogs.length > 0"> <table class="ui table selectable" v-if="accessLogs.length > 0">

View File

@@ -13,5 +13,10 @@ Tea.context(function () {
} else { } else {
accessLog.region = "" accessLog.region = ""
} }
if (accessLog.firewallRuleSetId > 0 && typeof (that.wafInfos[accessLog.firewallRuleSetId]) == "object") {
accessLog.wafInfo = that.wafInfos[accessLog.firewallRuleSetId]
} else {
accessLog.wafInfo = null
}
}) })
}) })

View File

@@ -92,6 +92,13 @@
</table> </table>
</td> </td>
</tr> </tr>
<tr>
<td>记录日志</td>
<td>
<span v-if="firewallPolicy.log == null || !firewallPolicy.log.isOn">默认</span>
<span v-else class="green">开启</span>
</td>
</tr>
<tr> <tr>
<td>描述</td> <td>描述</td>
<td> <td>

View File

@@ -49,6 +49,14 @@
<td> <td>
<firewall-syn-flood-config-box :v-syn-flood-config="firewallPolicy.synFloodConfig"></firewall-syn-flood-config-box> <firewall-syn-flood-config-box :v-syn-flood-config="firewallPolicy.synFloodConfig"></firewall-syn-flood-config-box>
</td> </td>
</tr>
<tr>
<td>记录日志</td>
<td>
<input type="hidden" name="logJSON" :value="JSON.stringify(firewallPolicy.log)"/>
<checkbox name="" v-model="firewallPolicy.log.isOn"></checkbox>
<p class="comment">选中后总是记录WAF相关日志即使服务中没有开启访问日志。</p>
</td>
</tr> </tr>
<tr> <tr>
<td colspan="2"><more-options-indicator></more-options-indicator></td> <td colspan="2"><more-options-indicator></more-options-indicator></td>