From 0b9a819e74b6db2964e46148d7f7026bc5d16a96 Mon Sep 17 00:00:00 2001 From: winfredLIN Date: Mon, 18 Dec 2023 13:25:12 +0800 Subject: [PATCH 1/3] ci: modify bad words --- sqle/api/controller/v1/audit_plan_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sqle/api/controller/v1/audit_plan_test.go b/sqle/api/controller/v1/audit_plan_test.go index 9f90e2304f..643c2363ff 100644 --- a/sqle/api/controller/v1/audit_plan_test.go +++ b/sqle/api/controller/v1/audit_plan_test.go @@ -105,7 +105,7 @@ func TestIsCidrInBlackList(t *testing.T) { func TestIsHostInBlackList(t *testing.T) { filter := v1.ConvertToBlackFilter([]*model.BlackListAuditPlanSQL{ { - FilterContent: "test", + FilterContent: "host", FilterType: "HOST", }, { FilterContent: "some_site", @@ -114,8 +114,8 @@ func TestIsHostInBlackList(t *testing.T) { }) matchHosts := []string{ - "localtest", - "localtest.com", + "local_host", + "local_Host.com", "anyTest.io", "some-Site.org/home/", "Some_site.cn/mysql", From 636e53c2617644d7291786a39e4d1066835edbb1 Mon Sep 17 00:00:00 2001 From: winfredLIN Date: Mon, 18 Dec 2023 13:38:18 +0800 Subject: [PATCH 2/3] rename: rename function name --- sqle/api/controller/v1/audit_plan.go | 5 +++-- sqle/api/controller/v2/audit_plan.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/sqle/api/controller/v1/audit_plan.go b/sqle/api/controller/v1/audit_plan.go index 4212f4cb4b..f78e040c4b 100644 --- a/sqle/api/controller/v1/audit_plan.go +++ b/sqle/api/controller/v1/audit_plan.go @@ -800,7 +800,7 @@ func filterSQLsByBlackList(sqls []*AuditPlanSQLReqV1, blackList []*model.BlackLi filteredSQLs := []*AuditPlanSQLReqV1{} filter := ConvertToBlackFilter(blackList) for _, sql := range sqls { - if filter.IsEndpointInBlackList([]string{sql.Endpoint}) || filter.IsSqlInBlackList(sql.LastReceiveText) { + if filter.HasEndpointInBlackList([]string{sql.Endpoint}) || filter.IsSqlInBlackList(sql.LastReceiveText) { continue } filteredSQLs = append(filteredSQLs, sql) @@ -852,7 +852,8 @@ func (f BlackFilter) IsSqlInBlackList(checkSql string) bool { return false } -func (f BlackFilter) IsEndpointInBlackList(checkIps []string) bool { +// 输入一组ip若其中有一个ip在黑名单中则返回true +func (f BlackFilter) HasEndpointInBlackList(checkIps []string) bool { var checkNetIp net.IP for _, checkIp := range checkIps { checkNetIp = net.ParseIP(checkIp) diff --git a/sqle/api/controller/v2/audit_plan.go b/sqle/api/controller/v2/audit_plan.go index 2571d3ede0..82ee8a96f7 100644 --- a/sqle/api/controller/v2/audit_plan.go +++ b/sqle/api/controller/v2/audit_plan.go @@ -285,7 +285,7 @@ func filterSQLsByBlackList(sqls []*AuditPlanSQLReqV2, blackList []*model.BlackLi filteredSQLs := []*AuditPlanSQLReqV2{} filter := v1.ConvertToBlackFilter(blackList) for _, sql := range sqls { - if filter.IsEndpointInBlackList(sql.Endpoints) || filter.IsSqlInBlackList(sql.LastReceiveText) { + if filter.HasEndpointInBlackList(sql.Endpoints) || filter.IsSqlInBlackList(sql.LastReceiveText) { continue } filteredSQLs = append(filteredSQLs, sql) From 08cf85cf230d45649206bf022d95a40ddeaf83bd Mon Sep 17 00:00:00 2001 From: winfredLIN Date: Mon, 18 Dec 2023 13:38:44 +0800 Subject: [PATCH 3/3] test: fix wrong test function --- sqle/api/controller/v1/audit_plan_test.go | 48 ++++++++++++++--------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/sqle/api/controller/v1/audit_plan_test.go b/sqle/api/controller/v1/audit_plan_test.go index 643c2363ff..280bfc9d63 100644 --- a/sqle/api/controller/v1/audit_plan_test.go +++ b/sqle/api/controller/v1/audit_plan_test.go @@ -55,9 +55,10 @@ func TestIsIpInBlackList(t *testing.T) { "10.0.5.67", "192.168.1.23", } - - if !filter.IsEndpointInBlackList(matchIps) { - t.Error("Expected Ip to match blacklist") + for _, matchIp := range matchIps { + if !filter.HasEndpointInBlackList([]string{matchIp}) { + t.Error("Expected Ip to match blacklist") + } } notMatchIps := []string{ @@ -65,8 +66,10 @@ func TestIsIpInBlackList(t *testing.T) { "134.12.45.78", "50.67.89.12", } - if filter.IsEndpointInBlackList(notMatchIps) { - t.Error("Did not expect Ip to match blacklist") + for _, notMatchIp := range notMatchIps { + if filter.HasEndpointInBlackList([]string{notMatchIp}) { + t.Error("Did not expect Ip to match blacklist") + } } } @@ -84,21 +87,26 @@ func TestIsCidrInBlackList(t *testing.T) { matchIps := []string{ "10.100.1.2", "10.100.25.45", - "172.30.1.2", - "172.30.30.45", + "192.168.0.2", + "192.168.0.45", } - - if !filter.IsEndpointInBlackList(matchIps) { - t.Error("Expected CIDR to match blacklist") + for _, matchIp := range matchIps { + if !filter.HasEndpointInBlackList([]string{matchIp}) { + t.Error("Expected CIDR to match blacklist") + } } notMatchIps := []string{ "172.16.254.89", "134.12.45.78", "50.67.89.12", + "172.30.1.2", + "172.30.30.45", } - if filter.IsEndpointInBlackList(notMatchIps) { - t.Error("Did not expect CIDR to match blacklist") + for _, notMatchIp := range notMatchIps { + if filter.HasEndpointInBlackList([]string{notMatchIp}) { + t.Error("Did not expect CIDR to match blacklist") + } } } @@ -116,20 +124,24 @@ func TestIsHostInBlackList(t *testing.T) { matchHosts := []string{ "local_host", "local_Host.com", - "anyTest.io", - "some-Site.org/home/", + "any_Host.io", + "some_Site.org/home/", "Some_site.cn/mysql", } - if !filter.IsEndpointInBlackList(matchHosts) { - t.Error("Expected HOST to match blacklist") + for _, matchHost := range matchHosts { + if !filter.HasEndpointInBlackList([]string{matchHost}) { + t.Error("Expected HOST to match blacklist") + } } notMatchHosts := []string{ "other_site/home", "any_other_site/local", } - if filter.IsEndpointInBlackList(notMatchHosts) { - t.Error("Did not expect HOST to match blacklist") + for _, noMatchHost := range notMatchHosts { + if filter.HasEndpointInBlackList([]string{noMatchHost}) { + t.Error("Did not expect HOST to match blacklist") + } } }