From 870321b7b98a37c1e215f0b9ada57021a0a9bcdd Mon Sep 17 00:00:00 2001 From: samwaf Date: Wed, 28 May 2025 09:41:12 +0800 Subject: [PATCH] feat:add same cloud multiple accounts #318 --- api/entrance.go | 9 +- api/waf_privategroup_api.go | 111 +++++++++++ api/waf_privateinfo_api.go | 6 - codetemplete/code_gen_test.go | 7 +- global/global.go | 1 - main.go | 6 +- model/private_group.go | 9 + model/private_info.go | 8 +- model/request/waf_privategroup_req.go | 26 +++ model/request/waf_privateinfo_req.go | 16 +- model/request/waf_sslorder_req.go | 30 +-- model/sslorder.go | 1 + router/entrance.go | 1 + router/waf_privategroup_router.go | 20 ++ .../waf_service/waf_privategroup_service.go | 177 ++++++++++++++++++ .../waf_service/waf_privateinfo_service.go | 104 ++++++++-- service/waf_service/waf_sslorder.go | 34 ++-- wafdb/localdb.go | 3 + wafdb/patch_sql.go | 73 ++++++++ wafenginecore/sslorder.go | 8 + wafmangeweb/localserver.go | 1 + wafssl/wafssl.go | 8 +- 22 files changed, 586 insertions(+), 73 deletions(-) create mode 100644 api/waf_privategroup_api.go create mode 100644 model/private_group.go create mode 100644 model/request/waf_privategroup_req.go create mode 100644 router/waf_privategroup_router.go create mode 100644 service/waf_service/waf_privategroup_service.go diff --git a/api/entrance.go b/api/entrance.go index 3ac7ed9..2fb0efb 100644 --- a/api/entrance.go +++ b/api/entrance.go @@ -38,6 +38,7 @@ type APIGroup struct { WafOtpApi WafAnalysisApi WafPrivateInfoApi + WafPrivateGroupApi WafCacheRuleApi WafTunnelApi WafVpConfigApi @@ -93,8 +94,8 @@ var ( wafAnalysisService = waf_service.WafAnalysisServiceApp - wafPrivateInfoService = waf_service.WafPrivateInfoServiceApp - - wafCacheRuleService = waf_service.WafCacheRuleServiceApp - wafTunnelService = waf_service.WafTunnelServiceApp + wafPrivateInfoService = waf_service.WafPrivateInfoServiceApp + wafPrivateGroupService = waf_service.WafPrivateGroupServiceApp + wafCacheRuleService = waf_service.WafCacheRuleServiceApp + wafTunnelService = waf_service.WafTunnelServiceApp ) diff --git a/api/waf_privategroup_api.go b/api/waf_privategroup_api.go new file mode 100644 index 0000000..6a45849 --- /dev/null +++ b/api/waf_privategroup_api.go @@ -0,0 +1,111 @@ +package api + +import ( + "SamWaf/model/common/response" + "SamWaf/model/request" + "errors" + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) + +type WafPrivateGroupApi struct { +} + +func (w *WafPrivateGroupApi) AddApi(c *gin.Context) { + var req request.WafPrivateGroupAddReq + err := c.ShouldBindJSON(&req) + if err == nil { + + cnt := wafPrivateGroupService.CheckIsExistApi(req) + if cnt == 0 { + err = wafPrivateGroupService.AddApi(req) + if err == nil { + response.OkWithMessage("添加成功", c) + } else { + response.FailWithMessage("添加失败", c) + } + return + } else { + response.FailWithMessage("当前记录已经存在", c) + return + } + + } else { + response.FailWithMessage("解析失败", c) + } +} + +func (w *WafPrivateGroupApi) GetDetailApi(c *gin.Context) { + var req request.WafPrivateGroupDetailReq + err := c.ShouldBind(&req) + if err == nil { + bean := wafPrivateGroupService.GetDetailApi(req) + response.OkWithDetailed(bean, "获取成功", c) + } else { + response.FailWithMessage("解析失败", c) + } +} + +func (w *WafPrivateGroupApi) GetListApi(c *gin.Context) { + var req request.WafPrivateGroupSearchReq + err := c.ShouldBindJSON(&req) + if err == nil { + PrivateGroup, total, _ := wafPrivateGroupService.GetListApi(req) + response.OkWithDetailed(response.PageResult{ + List: PrivateGroup, + Total: total, + PageIndex: req.PageIndex, + PageSize: req.PageSize, + }, "获取成功", c) + } else { + response.FailWithMessage("解析失败", c) + } +} + +func (w *WafPrivateGroupApi) GetListByBelongCloudApi(c *gin.Context) { + var req request.WafPrivateGroupSearchByCloudReq + err := c.ShouldBindJSON(&req) + if err == nil { + PrivateGroup, total, _ := wafPrivateGroupService.GetListByBelongCloudApi(req) + response.OkWithDetailed(response.PageResult{ + List: PrivateGroup, + Total: total, + PageIndex: req.PageIndex, + PageSize: req.PageSize, + }, "获取成功", c) + } else { + response.FailWithMessage("解析失败", c) + } +} + +func (w *WafPrivateGroupApi) DelApi(c *gin.Context) { + var req request.WafPrivateGroupDelReq + err := c.ShouldBind(&req) + if err == nil { + err = wafPrivateGroupService.DelApi(req) + if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { + response.FailWithMessage("请检测参数", c) + } else if err != nil { + response.FailWithMessage("发生错误", c) + } else { + response.OkWithMessage("删除成功", c) + } + } else { + response.FailWithMessage("解析失败", c) + } +} + +func (w *WafPrivateGroupApi) ModifyApi(c *gin.Context) { + var req request.WafPrivateGroupEditReq + err := c.ShouldBindJSON(&req) + if err == nil { + err = wafPrivateGroupService.ModifyApi(req) + if err != nil { + response.FailWithMessage("编辑发生错误"+err.Error(), c) + } else { + response.OkWithMessage("编辑成功", c) + } + } else { + response.FailWithMessage("解析失败", c) + } +} diff --git a/api/waf_privateinfo_api.go b/api/waf_privateinfo_api.go index 8a5e760..ebdc7de 100644 --- a/api/waf_privateinfo_api.go +++ b/api/waf_privateinfo_api.go @@ -2,7 +2,6 @@ package api import ( "SamWaf/common/zlog" - "SamWaf/global" "SamWaf/model/common/response" "SamWaf/model/request" "errors" @@ -24,7 +23,6 @@ func (w *WafPrivateInfoApi) AddApi(c *gin.Context) { if cnt == 0 { err = wafPrivateInfoService.AddApi(req) if err == nil { - w.Notify() response.OkWithMessage("添加成功", c) } else { response.FailWithMessage("添加失败", c) @@ -115,13 +113,9 @@ func (w *WafPrivateInfoApi) ModifyApi(c *gin.Context) { if err != nil { response.FailWithMessage("编辑发生错误"+err.Error(), c) } else { - w.Notify() response.OkWithMessage("编辑成功", c) } } else { response.FailWithMessage("解析失败", c) } } -func (w *WafPrivateInfoApi) Notify() { - global.GWAF_CHAN_RELOAD_ENV <- 1 -} diff --git a/codetemplete/code_gen_test.go b/codetemplete/code_gen_test.go index 5c1331a..c14e7c8 100644 --- a/codetemplete/code_gen_test.go +++ b/codetemplete/code_gen_test.go @@ -9,7 +9,8 @@ import ( func TestCodeGeneration(t *testing.T) { // 唯一校验定义字段信息的字符串 fieldDefs := []string{ - "Name:name:string", + "PrivateGroupName:private_group_name:string", + "PrivateGroupBelongCloud:private_group_belong_cloud:string", } // 构造 `uniFields` 列表 @@ -27,6 +28,6 @@ func TestCodeGeneration(t *testing.T) { } } - fields := GetStructFields(model.Tunnel{}) - CodeGeneration("Tunnel", fields, uniFields) + fields := GetStructFields(model.PrivateGroup{}) + CodeGeneration("PrivateGroup", fields, uniFields) } diff --git a/global/global.go b/global/global.go index 7fe4896..1ef7fc2 100644 --- a/global/global.go +++ b/global/global.go @@ -96,7 +96,6 @@ var ( GWAF_CHAN_CLEAR_CC_WINDOWS = make(chan int, 10) //清除cc缓存信息 GWAF_CHAN_CLEAR_CC_IP = make(chan string, 10) //清除cc缓存信息IP GWAF_QUEUE_SHUTDOWN_SIGNAL chan struct{} = make(chan struct{}) // 队列关闭信号 - GWAF_CHAN_RELOAD_ENV = make(chan int, 10) //环境变量重新加载链 GWAF_SHUTDOWN_SIGNAL bool = false // 系统关闭信号 /*****CACHE相关*********/ diff --git a/main.go b/main.go index a9c8c31..9ab31f5 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,6 @@ import ( "SamWaf/wafreg" "SamWaf/wafsafeclear" "SamWaf/wafsnowflake" - "SamWaf/wafssl" "SamWaf/waftask" "SamWaf/waftunnelengine" "SamWaf/webplugin" @@ -256,7 +255,7 @@ func (m *wafSystenService) run() { waftask.TaskCreateIndexByDbName(enums.DB_STATS) } } - wafssl.InitDnsProviderEnvInfo() + //初始化队列引擎 wafqueue.InitDequeEngine() //启动队列消费 @@ -651,9 +650,6 @@ func (m *wafSystenService) run() { zlog.Debug("定时清空CCip", clearCcWindowsIp) globalobj.GWAF_RUNTIME_OBJ_WAF_ENGINE.ClearCcWindowsForIP(clearCcWindowsIp) break - case _ = <-global.GWAF_CHAN_RELOAD_ENV: - wafssl.InitDnsProviderEnvInfo() - break } } diff --git a/model/private_group.go b/model/private_group.go new file mode 100644 index 0000000..28d3a84 --- /dev/null +++ b/model/private_group.go @@ -0,0 +1,9 @@ +package model + +import "SamWaf/model/baseorm" + +type PrivateGroup struct { + baseorm.BaseOrm + PrivateGroupName string `json:"private_group_name"` //分组名称 + PrivateGroupBelongCloud string `json:"private_group_belong_cloud"` //分组归属云 +} diff --git a/model/private_info.go b/model/private_info.go index d5ed190..68231b6 100644 --- a/model/private_info.go +++ b/model/private_info.go @@ -4,7 +4,9 @@ import "SamWaf/model/baseorm" type PrivateInfo struct { baseorm.BaseOrm - PrivateKey string `json:"private_key"` //密钥key - PrivateValue string `json:"private_value"` //密钥值 - Remarks string `json:"remarks"` //备注 + PrivateKey string `json:"private_key"` //密钥key + PrivateValue string `json:"private_value"` //密钥值 + PrivateGroupName string `json:"private_group_name"` //分组名称 + PrivateGroupBelongCloud string `json:"private_group_belong_cloud"` //分组归属云 + Remarks string `json:"remarks"` //备注 } diff --git a/model/request/waf_privategroup_req.go b/model/request/waf_privategroup_req.go new file mode 100644 index 0000000..2d911ea --- /dev/null +++ b/model/request/waf_privategroup_req.go @@ -0,0 +1,26 @@ +package request + +import "SamWaf/model/common/request" + +type WafPrivateGroupAddReq struct { + PrivateGroupName string `json:"private_group_name" form:"private_group_name"` + PrivateGroupBelongCloud string `json:"private_group_belong_cloud" form:"private_group_belong_cloud"` +} +type WafPrivateGroupEditReq struct { + Id string `json:"id"` + PrivateGroupName string `json:"private_group_name" form:"private_group_name"` + PrivateGroupBelongCloud string `json:"private_group_belong_cloud" form:"private_group_belong_cloud"` +} +type WafPrivateGroupDetailReq struct { + Id string `json:"id" form:"id"` +} +type WafPrivateGroupDelReq struct { + Id string `json:"id" form:"id"` +} +type WafPrivateGroupSearchReq struct { + request.PageInfo +} +type WafPrivateGroupSearchByCloudReq struct { + PrivateGroupBelongCloud string `json:"private_group_belong_cloud" form:"private_group_belong_cloud"` + request.PageInfo +} diff --git a/model/request/waf_privateinfo_req.go b/model/request/waf_privateinfo_req.go index e83d5ab..9bd0a2d 100644 --- a/model/request/waf_privateinfo_req.go +++ b/model/request/waf_privateinfo_req.go @@ -3,14 +3,18 @@ package request import "SamWaf/model/common/request" type WafPrivateInfoAddReq struct { - PrivateKey string `json:"private_key" form:"private_key"` - PrivateValue string `json:"private_value" form:"private_value"` - Remarks string `json:"remarks" form:"remarks"` + PrivateKey string `json:"private_key" form:"private_key"` + PrivateValue string `json:"private_value" form:"private_value"` + PrivateGroupName string `json:"private_group_name"` //分组名称 + PrivateGroupBelongCloud string `json:"private_group_belong_cloud"` //分组归属云 + Remarks string `json:"remarks" form:"remarks"` } type WafPrivateInfoEditReq struct { - PrivateKey string `json:"private_key" form:"private_key"` - PrivateValue string `json:"private_value" form:"private_value"` - Remarks string `json:"remarks" form:"remarks"` + PrivateKey string `json:"private_key" form:"private_key"` + PrivateValue string `json:"private_value" form:"private_value"` + PrivateGroupName string `json:"private_group_name"` //分组名称 + PrivateGroupBelongCloud string `json:"private_group_belong_cloud"` //分组归属云 + Remarks string `json:"remarks" form:"remarks"` } type WafPrivateInfoDetailReq struct { Id string `json:"id" form:"id"` diff --git a/model/request/waf_sslorder_req.go b/model/request/waf_sslorder_req.go index e63bb38..41a0c41 100644 --- a/model/request/waf_sslorder_req.go +++ b/model/request/waf_sslorder_req.go @@ -3,22 +3,24 @@ package request import "SamWaf/model/common/request" type WafSslorderaddReq struct { - HostCode string `json:"host_code"` //网站唯一码(主要键) - ApplyPlatform string `json:"apply_platform"` //申请平台 - ApplyMethod string `json:"apply_method"` //申请方式http,dns - ApplyDns string `json:"apply_dns"` //申请dns服务商 - ApplyEmail string `json:"apply_email"` //申请邮箱 - ApplyDomain string `json:"apply_domain"` //申请域名 + HostCode string `json:"host_code"` //网站唯一码(主要键) + ApplyPlatform string `json:"apply_platform"` //申请平台 + ApplyMethod string `json:"apply_method"` //申请方式http,dns + ApplyDns string `json:"apply_dns"` //申请dns服务商 + ApplyEmail string `json:"apply_email"` //申请邮箱 + ApplyDomain string `json:"apply_domain"` //申请域名 + PrivateGroupName string `json:"private_group_name"` //密钥分组名称DNS有效 } type WafSslordereditReq struct { - Id string `json:"id"` - HostCode string `json:"host_code"` //网站唯一码(主要键) - ApplyPlatform string `json:"apply_platform"` //申请平台 - ApplyMethod string `json:"apply_method"` //申请方式http01,dns - ApplyDns string `json:"apply_dns"` //申请dns服务商 - ApplyEmail string `json:"apply_email"` //申请邮箱 - ApplyDomain string `json:"apply_domain"` //申请域名 - ApplyStatus string `json:"apply_status"` //申请状态 + Id string `json:"id"` + HostCode string `json:"host_code"` //网站唯一码(主要键) + ApplyPlatform string `json:"apply_platform"` //申请平台 + ApplyMethod string `json:"apply_method"` //申请方式http01,dns + ApplyDns string `json:"apply_dns"` //申请dns服务商 + ApplyEmail string `json:"apply_email"` //申请邮箱 + ApplyDomain string `json:"apply_domain"` //申请域名 + ApplyStatus string `json:"apply_status"` //申请状态 + PrivateGroupName string `json:"private_group_name"` //密钥分组名称DNS有效 } type WafSslorderdetailReq struct { Id string `json:"id" form:"id"` diff --git a/model/sslorder.go b/model/sslorder.go index 3fd966f..19396e8 100644 --- a/model/sslorder.go +++ b/model/sslorder.go @@ -28,6 +28,7 @@ type SslOrder struct { ResultValidTo time.Time `json:"result_valid_to"` //证书有效期结束时间 ResultCSR []byte `json:"result_csr"` //csr信息 Remarks string `json:"remarks"` //备注信息 + PrivateGroupName string `json:"private_group_name"` //密钥分组名称DNS有效 } // ExpirationMessage 获取到期提示信息 diff --git a/router/entrance.go b/router/entrance.go index 91612f9..bd408c1 100644 --- a/router/entrance.go +++ b/router/entrance.go @@ -36,6 +36,7 @@ type ApiGroup struct { WafOtpRouter AnalysisRouter WafPrivateInfoRouter + WafPrivateGroupRouter WafCacheRuleRouter WafTunnelRouter WafVpConfigRouter diff --git a/router/waf_privategroup_router.go b/router/waf_privategroup_router.go new file mode 100644 index 0000000..4856da6 --- /dev/null +++ b/router/waf_privategroup_router.go @@ -0,0 +1,20 @@ +package router + +import ( + "SamWaf/api" + "github.com/gin-gonic/gin" +) + +type WafPrivateGroupRouter struct { +} + +func (receiver *WafPrivateGroupRouter) InitWafPrivateGroupRouter(group *gin.RouterGroup) { + api := api.APIGroupAPP.WafPrivateGroupApi + router := group.Group("") + router.POST("/samwaf/wafhost/privategroup/add", api.AddApi) + router.POST("/samwaf/wafhost/privategroup/list", api.GetListApi) + router.POST("/samwaf/wafhost/privategroup/listbybelongcloud", api.GetListByBelongCloudApi) + router.GET("/samwaf/wafhost/privategroup/detail", api.GetDetailApi) + router.POST("/samwaf/wafhost/privategroup/edit", api.ModifyApi) + router.GET("/samwaf/wafhost/privategroup/del", api.DelApi) +} diff --git a/service/waf_service/waf_privategroup_service.go b/service/waf_service/waf_privategroup_service.go new file mode 100644 index 0000000..f70344f --- /dev/null +++ b/service/waf_service/waf_privategroup_service.go @@ -0,0 +1,177 @@ +package waf_service + +import ( + "SamWaf/common/uuid" + "SamWaf/customtype" + "SamWaf/global" + "SamWaf/model" + "SamWaf/model/baseorm" + "SamWaf/model/request" + "errors" + "time" +) + +type WafPrivateGroupService struct{} + +var WafPrivateGroupServiceApp = new(WafPrivateGroupService) + +func (receiver *WafPrivateGroupService) AddApi(req request.WafPrivateGroupAddReq) error { + var bean = &model.PrivateGroup{ + BaseOrm: baseorm.BaseOrm{ + Id: uuid.GenUUID(), + USER_CODE: global.GWAF_USER_CODE, + Tenant_ID: global.GWAF_TENANT_ID, + CREATE_TIME: customtype.JsonTime(time.Now()), + UPDATE_TIME: customtype.JsonTime(time.Now()), + }, + + PrivateGroupName: req.PrivateGroupName, + PrivateGroupBelongCloud: req.PrivateGroupBelongCloud, + } + global.GWAF_LOCAL_DB.Create(bean) + return nil +} + +func (receiver *WafPrivateGroupService) CheckIsExistApi(req request.WafPrivateGroupAddReq) int { + var total int64 = 0 + /*where条件*/ + var whereField = "" + var whereValues []interface{} + //where字段 + whereField = "" + + if len(req.PrivateGroupName) > 0 { + if len(whereField) > 0 { + whereField = whereField + " and " + } + whereField = whereField + " private_group_name=? " + } + + if len(req.PrivateGroupBelongCloud) > 0 { + if len(whereField) > 0 { + whereField = whereField + " and " + } + whereField = whereField + " private_group_belong_cloud=? " + } + + //where字段赋值 + + if len(req.PrivateGroupName) > 0 { + if len(whereField) > 0 { + whereValues = append(whereValues, req.PrivateGroupName) + } + } + + if len(req.PrivateGroupBelongCloud) > 0 { + if len(whereField) > 0 { + whereValues = append(whereValues, req.PrivateGroupBelongCloud) + } + } + + global.GWAF_LOCAL_DB.Model(&model.PrivateGroup{}).Where(whereField, whereValues...).Count(&total) + return int(total) +} + +func (receiver *WafPrivateGroupService) ModifyApi(req request.WafPrivateGroupEditReq) error { + // 根据唯一字段生成查询条件(只有在UniFields不为空时才进行存在性检查) + + var total int64 = 0 + /*where条件*/ + var whereField = "" + var whereValues []interface{} + //where字段 + whereField = "" + + if len(req.PrivateGroupName) > 0 { + if len(whereField) > 0 { + whereField = whereField + " and " + } + whereField = whereField + " private_group_name=? " + } + + if len(req.PrivateGroupBelongCloud) > 0 { + if len(whereField) > 0 { + whereField = whereField + " and " + } + whereField = whereField + " private_group_belong_cloud=? " + } + + //where字段赋值 + + if len(req.PrivateGroupName) > 0 { + whereValues = append(whereValues, req.PrivateGroupName) + } + + if len(req.PrivateGroupBelongCloud) > 0 { + whereValues = append(whereValues, req.PrivateGroupBelongCloud) + } + + global.GWAF_LOCAL_DB.Model(&model.PrivateGroup{}).Where(whereField, whereValues...).Count(&total) + // 查询是否已存在记录 + var bean model.PrivateGroup + global.GWAF_LOCAL_DB.Model(&model.PrivateGroup{}).Where(whereField, whereValues...).Limit(1).Find(&bean) + + if int(total) > 0 && bean.Id != "" && bean.Id != req.Id { + return errors.New("当前记录已经存在") + } + + beanMap := map[string]interface{}{ + "PrivateGroupName": req.PrivateGroupName, + "PrivateGroupBelongCloud": req.PrivateGroupBelongCloud, + "UPDATE_TIME": customtype.JsonTime(time.Now()), + } + err := global.GWAF_LOCAL_DB.Model(model.PrivateGroup{}).Where("id = ?", req.Id).Updates(beanMap).Error + + return err +} +func (receiver *WafPrivateGroupService) GetDetailApi(req request.WafPrivateGroupDetailReq) model.PrivateGroup { + var bean model.PrivateGroup + global.GWAF_LOCAL_DB.Where("id=?", req.Id).Find(&bean) + return bean +} +func (receiver *WafPrivateGroupService) GetDetailByIdApi(id string) model.PrivateGroup { + var bean model.PrivateGroup + global.GWAF_LOCAL_DB.Where("id=?", id).Find(&bean) + return bean +} +func (receiver *WafPrivateGroupService) GetListApi(req request.WafPrivateGroupSearchReq) ([]model.PrivateGroup, int64, error) { + var list []model.PrivateGroup + var total int64 = 0 + global.GWAF_LOCAL_DB.Model(&model.PrivateGroup{}).Limit(req.PageSize).Offset(req.PageSize * (req.PageIndex - 1)).Find(&list) + global.GWAF_LOCAL_DB.Model(&model.PrivateGroup{}).Count(&total) + + return list, total, nil +} +func (receiver *WafPrivateGroupService) GetListByBelongCloudApi(req request.WafPrivateGroupSearchByCloudReq) ([]model.PrivateGroup, int64, error) { + var list []model.PrivateGroup + var total int64 = 0 + + /*where条件*/ + var whereField = "" + var whereValues []interface{} + //where字段 + whereField = "" + + whereField = whereField + " private_group_belong_cloud=? " + + //where字段赋值 + if len(req.PrivateGroupBelongCloud) > 0 { + whereValues = append(whereValues, req.PrivateGroupBelongCloud) + } else { + whereValues = append(whereValues, "samwaf-not-exist") + } + + global.GWAF_LOCAL_DB.Model(&model.PrivateGroup{}).Where(whereField, whereValues...).Limit(req.PageSize).Offset(req.PageSize * (req.PageIndex - 1)).Find(&list) + global.GWAF_LOCAL_DB.Model(&model.PrivateGroup{}).Where(whereField, whereValues...).Count(&total) + + return list, total, nil +} +func (receiver *WafPrivateGroupService) DelApi(req request.WafPrivateGroupDelReq) error { + var bean model.PrivateGroup + err := global.GWAF_LOCAL_DB.Where("id = ?", req.Id).First(&bean).Error + if err != nil { + return err + } + err = global.GWAF_LOCAL_DB.Where("id = ?", req.Id).Delete(model.PrivateGroup{}).Error + return err +} diff --git a/service/waf_service/waf_privateinfo_service.go b/service/waf_service/waf_privateinfo_service.go index 89132ec..06ef117 100644 --- a/service/waf_service/waf_privateinfo_service.go +++ b/service/waf_service/waf_privateinfo_service.go @@ -25,9 +25,11 @@ func (receiver *WafPrivateInfoService) AddApi(req request.WafPrivateInfoAddReq) UPDATE_TIME: customtype.JsonTime(time.Now()), }, - PrivateKey: req.PrivateKey, - PrivateValue: req.PrivateValue, - Remarks: req.Remarks, + PrivateKey: req.PrivateKey, + PrivateValue: req.PrivateValue, + PrivateGroupName: req.PrivateGroupName, + PrivateGroupBelongCloud: req.PrivateGroupBelongCloud, + Remarks: req.Remarks, } global.GWAF_LOCAL_DB.Create(bean) return nil @@ -47,7 +49,18 @@ func (receiver *WafPrivateInfoService) CheckIsExistApi(req request.WafPrivateInf } whereField = whereField + " private_key=? " } - + if len(req.PrivateGroupName) > 0 { + if len(whereField) > 0 { + whereField = whereField + " and " + } + whereField = whereField + " private_group_name=? " + } + if len(req.PrivateGroupBelongCloud) > 0 { + if len(whereField) > 0 { + whereField = whereField + " and " + } + whereField = whereField + " private_group_belong_cloud=? " + } //where字段赋值 if len(req.PrivateKey) > 0 { @@ -55,6 +68,16 @@ func (receiver *WafPrivateInfoService) CheckIsExistApi(req request.WafPrivateInf whereValues = append(whereValues, req.PrivateKey) } } + if len(req.PrivateGroupName) > 0 { + if len(whereField) > 0 { + whereValues = append(whereValues, req.PrivateGroupName) + } + } + if len(req.PrivateGroupBelongCloud) > 0 { + if len(whereField) > 0 { + whereValues = append(whereValues, req.PrivateGroupBelongCloud) + } + } global.GWAF_LOCAL_DB.Model(&model.PrivateInfo{}).Where(whereField, whereValues...).Count(&total) return int(total) @@ -75,12 +98,34 @@ func (receiver *WafPrivateInfoService) ModifyWithOutValueApi(req request.WafPriv } whereField = whereField + " private_key=? " } + if len(req.PrivateGroupName) > 0 { + if len(whereField) > 0 { + whereField = whereField + " and " + } + whereField = whereField + " private_group_name=? " + } + if len(req.PrivateGroupBelongCloud) > 0 { + if len(whereField) > 0 { + whereField = whereField + " and " + } + whereField = whereField + " private_group_belong_cloud=? " + } //where字段赋值 if len(req.PrivateKey) > 0 { whereValues = append(whereValues, req.PrivateKey) } + if len(req.PrivateGroupName) > 0 { + if len(whereField) > 0 { + whereValues = append(whereValues, req.PrivateGroupName) + } + } + if len(req.PrivateGroupBelongCloud) > 0 { + if len(whereField) > 0 { + whereValues = append(whereValues, req.PrivateGroupBelongCloud) + } + } global.GWAF_LOCAL_DB.Model(&model.PrivateInfo{}).Where(whereField, whereValues...).Count(&total) // 查询是否已存在记录 @@ -92,10 +137,11 @@ func (receiver *WafPrivateInfoService) ModifyWithOutValueApi(req request.WafPriv } beanMap := map[string]interface{}{ - "PrivateKey": req.PrivateKey, - "Remarks": req.Remarks, - - "UPDATE_TIME": customtype.JsonTime(time.Now()), + "PrivateKey": req.PrivateKey, + "Remarks": req.Remarks, + "PrivateGroupName": req.PrivateGroupName, + "PrivateGroupBelongCloud": req.PrivateGroupBelongCloud, + "UPDATE_TIME": customtype.JsonTime(time.Now()), } err := global.GWAF_LOCAL_DB.Model(model.PrivateInfo{}).Where("private_key = ?", req.PrivateKey).Updates(beanMap).Error @@ -118,13 +164,35 @@ func (receiver *WafPrivateInfoService) ModifyApi(req request.WafPrivateInfoEditR } whereField = whereField + " private_key=? " } - + if len(req.PrivateGroupName) > 0 { + if len(whereField) > 0 { + whereField = whereField + " and " + } + whereField = whereField + " private_group_name=? " + } + if len(req.PrivateGroupBelongCloud) > 0 { + if len(whereField) > 0 { + whereField = whereField + " and " + } + whereField = whereField + " private_group_belong_cloud=? " + } //where字段赋值 if len(req.PrivateKey) > 0 { whereValues = append(whereValues, req.PrivateKey) } + if len(req.PrivateGroupName) > 0 { + if len(whereField) > 0 { + whereValues = append(whereValues, req.PrivateGroupName) + } + } + if len(req.PrivateGroupBelongCloud) > 0 { + if len(whereField) > 0 { + whereValues = append(whereValues, req.PrivateGroupBelongCloud) + } + } + global.GWAF_LOCAL_DB.Model(&model.PrivateInfo{}).Where(whereField, whereValues...).Count(&total) // 查询是否已存在记录 var bean model.PrivateInfo @@ -136,9 +204,11 @@ func (receiver *WafPrivateInfoService) ModifyApi(req request.WafPrivateInfoEditR beanMap := map[string]interface{}{ - "PrivateKey": req.PrivateKey, - "PrivateValue": req.PrivateValue, - "Remarks": req.Remarks, + "PrivateKey": req.PrivateKey, + "PrivateValue": req.PrivateValue, + "PrivateGroupName": req.PrivateGroupName, + "PrivateGroupBelongCloud": req.PrivateGroupBelongCloud, + "Remarks": req.Remarks, "UPDATE_TIME": customtype.JsonTime(time.Now()), } @@ -182,3 +252,13 @@ func (receiver *WafPrivateInfoService) GetListPureApi() ([]model.PrivateInfo, in return list, total, nil } + +// GetListByGroupAndBelongCloudPureApi 依据分组名称和所属云查询 +func (receiver *WafPrivateInfoService) GetListByGroupAndBelongCloudPureApi(groupName string, belongCloud string) ([]model.PrivateInfo, int64, error) { + var list []model.PrivateInfo + var total int64 = 0 + global.GWAF_LOCAL_DB.Model(&model.PrivateInfo{}).Where("private_group_name = ? and private_group_belong_cloud=?", groupName, belongCloud).Find(&list) + global.GWAF_LOCAL_DB.Model(&model.PrivateInfo{}).Where("private_group_name = ? and private_group_belong_cloud=?", groupName, belongCloud).Count(&total) + + return list, total, nil +} diff --git a/service/waf_service/waf_sslorder.go b/service/waf_service/waf_sslorder.go index 56f217a..3bfb811 100644 --- a/service/waf_service/waf_sslorder.go +++ b/service/waf_service/waf_sslorder.go @@ -23,13 +23,14 @@ func (receiver *WafSSLOrderService) AddApi(req request.WafSslorderaddReq) (model CREATE_TIME: customtype.JsonTime(time.Now()), UPDATE_TIME: customtype.JsonTime(time.Now()), }, - HostCode: req.HostCode, - ApplyPlatform: req.ApplyPlatform, - ApplyMethod: req.ApplyMethod, - ApplyDns: req.ApplyDns, - ApplyDomain: req.ApplyDomain, - ApplyEmail: req.ApplyEmail, - ApplyStatus: "submitted", + HostCode: req.HostCode, + ApplyPlatform: req.ApplyPlatform, + ApplyMethod: req.ApplyMethod, + ApplyDns: req.ApplyDns, + ApplyDomain: req.ApplyDomain, + ApplyEmail: req.ApplyEmail, + ApplyStatus: "submitted", + PrivateGroupName: req.PrivateGroupName, } global.GWAF_LOCAL_DB.Create(bean) @@ -39,14 +40,15 @@ func (receiver *WafSSLOrderService) AddApi(req request.WafSslorderaddReq) (model func (receiver *WafSSLOrderService) ModifyApi(req request.WafSslordereditReq) error { sslOrderMap := map[string]interface{}{ - "HostCode": req.HostCode, - "ApplyPlatform": req.ApplyPlatform, - "ApplyMethod": req.ApplyMethod, - "ApplyDns": req.ApplyDns, - "ApplyDomain": req.ApplyDomain, - "ApplyEmail": req.ApplyEmail, - "ApplyStatus": req.ApplyStatus, - "UPDATE_TIME": customtype.JsonTime(time.Now()), + "HostCode": req.HostCode, + "ApplyPlatform": req.ApplyPlatform, + "ApplyMethod": req.ApplyMethod, + "ApplyDns": req.ApplyDns, + "ApplyDomain": req.ApplyDomain, + "ApplyEmail": req.ApplyEmail, + "ApplyStatus": req.ApplyStatus, + "PrivateGroupName": req.PrivateGroupName, + "UPDATE_TIME": customtype.JsonTime(time.Now()), } err := global.GWAF_LOCAL_DB.Model(model.SslOrder{}).Where("id = ?", req.Id).Updates(sslOrderMap).Error @@ -120,6 +122,7 @@ func (receiver *WafSSLOrderService) ModifyById(sslOrder model.SslOrder) error { "ResultCSR": sslOrder.ResultCSR, "ResultValidTo": sslOrder.ResultValidTo, "ResultError": sslOrder.ResultError, + "PrivateGroupName": sslOrder.PrivateGroupName, "UPDATE_TIME": customtype.JsonTime(time.Now()), } err := global.GWAF_LOCAL_DB.Model(model.SslOrder{}).Where("id = ?", sslOrder.Id).Updates(sslOrderMap).Error @@ -154,6 +157,7 @@ func (receiver *WafSSLOrderService) RenewAdd(orderId string) (model.SslOrder, er ResultCertificate: order.ResultCertificate, ResultIssuerCertificate: order.ResultIssuerCertificate, ResultCSR: order.ResultCSR, + PrivateGroupName: order.PrivateGroupName, } global.GWAF_LOCAL_DB.Create(bean) return *bean, nil diff --git a/wafdb/localdb.go b/wafdb/localdb.go index ee4817b..e4ce954 100644 --- a/wafdb/localdb.go +++ b/wafdb/localdb.go @@ -149,6 +149,9 @@ func InitCoreDb(currentDir string) (bool, error) { //密钥信息 db.AutoMigrate(&model.PrivateInfo{}) + //密钥分组信息 + db.AutoMigrate(&model.PrivateGroup{}) + //缓存规则 db.AutoMigrate(&model.CacheRule{}) diff --git a/wafdb/patch_sql.go b/wafdb/patch_sql.go index 877e12e..ba33b3d 100644 --- a/wafdb/patch_sql.go +++ b/wafdb/patch_sql.go @@ -1,7 +1,12 @@ package wafdb import ( + "SamWaf/common/uuid" "SamWaf/common/zlog" + "SamWaf/customtype" + "SamWaf/global" + "SamWaf/model" + "SamWaf/model/baseorm" "gorm.io/gorm" "time" ) @@ -96,6 +101,74 @@ func pathCoreSql(db *gorm.DB) { } else { zlog.Info("db", "hosts: default_encoding init successfully") } + //20250527 初始化分组信息 model.PrivateGroup + // 初始化四个云服务提供商的默认分组 + cloudProviders := []string{"alidns", "huaweicloud", "tencentcloud", "cloudflare"} + for _, cloudProvider := range cloudProviders { + // 检查该云服务提供商是否已有分组记录 + var count int64 + db.Model(&model.PrivateGroup{}).Where("private_group_belong_cloud = ?", cloudProvider).Count(&count) + + // 如果不存在记录,则插入默认分组 + if count == 0 { + privateGroup := model.PrivateGroup{ + BaseOrm: baseorm.BaseOrm{ + Id: uuid.GenUUID(), + USER_CODE: global.GWAF_USER_CODE, + Tenant_ID: global.GWAF_TENANT_ID, + CREATE_TIME: customtype.JsonTime(time.Now()), + UPDATE_TIME: customtype.JsonTime(time.Now()), + }, + PrivateGroupName: "default", + PrivateGroupBelongCloud: cloudProvider, + } + + err := db.Create(&privateGroup).Error + if err != nil { + zlog.Error("db", "init cloud group fail", "cloud", cloudProvider, "error", err.Error()) + } else { + zlog.Info("db", "init cloud group success", "cloud", cloudProvider) + } + } + } + //如果申请记录里面没有分组信息,那么就默认放到default分组 + err = db.Exec("UPDATE ssl_orders SET private_group_name='default' WHERE private_group_name IS NULL").Error + if err != nil { + panic("failed to ssl_orders: private_group_name " + err.Error()) + } else { + zlog.Info("db", "ssl_orders: private_group_name init successfully") + } + // 更新private_infos表中空分组信息 + err = db.Exec("UPDATE private_infos SET private_group_name='default' WHERE private_group_name IS NULL OR private_group_name=''").Error + if err != nil { + panic("failed to update private_infos: private_group_name " + err.Error()) + } else { + zlog.Info("db", "private_infos: private_group_name updated successfully") + } + + // 根据环境变量名称更新所属云信息 + envCloudMap := map[string]string{ + "ALICLOUD_ACCESS_KEY": "alidns", + "ALICLOUD_SECRET_KEY": "alidns", + "ALICLOUD_SECURITY_TOKEN": "alidns", + "HUAWEICLOUD_ACCESS_KEY_ID": "huaweicloud", + "HUAWEICLOUD_SECRET_ACCESS_KEY": "huaweicloud", + "HUAWEICLOUD_REGION": "huaweicloud", + "TENCENTCLOUD_SECRET_ID": "tencentcloud", + "TENCENTCLOUD_SECRET_KEY": "tencentcloud", + "CF_DNS_API_TOKEN": "cloudflare", + } + + // 遍历环境变量映射,更新对应的所属云信息 + for envKey, cloudName := range envCloudMap { + err = db.Exec("UPDATE private_infos SET private_group_belong_cloud=? WHERE private_key=? AND (private_group_belong_cloud IS NULL OR private_group_belong_cloud='')", cloudName, envKey).Error + if err != nil { + zlog.Error("db", "update dns key fail", "env", envKey, "cloud", cloudName, "error", err.Error()) + } else { + zlog.Info("db", "update dns key successfully", "env", envKey, "cloud", cloudName) + } + } + // 记录结束时间并计算耗时 duration := time.Since(startTime) zlog.Info("create core default value completely", "duration", duration.String()) diff --git a/wafenginecore/sslorder.go b/wafenginecore/sslorder.go index 14eed33..044010d 100644 --- a/wafenginecore/sslorder.go +++ b/wafenginecore/sslorder.go @@ -12,6 +12,7 @@ import ( "SamWaf/service/waf_service" "SamWaf/utils" "SamWaf/utils/ssl" + "SamWaf/wafssl" "errors" "fmt" "time" @@ -25,6 +26,13 @@ var ( // ApplySSLOrder 申请SSL证书处理 func (waf *WafEngine) ApplySSLOrder(chanType int, bean model.SslOrder) { + if bean.ApplyMethod == "dns01" { + //加载环境变量 + privateGroupName := bean.PrivateGroupName + privateGroupBelongCloud := bean.ApplyDns + //查询环境变量信息 + wafssl.LoadDnsProviderEnvInfo(privateGroupName, privateGroupBelongCloud) + } if chanType == enums.ChanSslOrderSubmitted { //发起申请 zlog.Info(fmt.Sprintf("%s 正在进行首次证书申请", bean.ApplyDomain)) diff --git a/wafmangeweb/localserver.go b/wafmangeweb/localserver.go index 5df41ef..5730171 100644 --- a/wafmangeweb/localserver.go +++ b/wafmangeweb/localserver.go @@ -70,6 +70,7 @@ func (web *WafWebManager) initRouter(r *gin.Engine) { router.ApiGroupApp.InitWafOtpRouter(RouterGroup) router.ApiGroupApp.InitAnalysisRouter(RouterGroup) router.ApiGroupApp.InitWafPrivateInfoRouter(RouterGroup) + router.ApiGroupApp.InitWafPrivateGroupRouter(RouterGroup) router.ApiGroupApp.InitWafCacheRuleRouter(RouterGroup) router.ApiGroupApp.InitWafTunnelRouter(RouterGroup) router.ApiGroupApp.InitWafVpConfigRouter(RouterGroup) diff --git a/wafssl/wafssl.go b/wafssl/wafssl.go index fe02839..78e8f1b 100644 --- a/wafssl/wafssl.go +++ b/wafssl/wafssl.go @@ -9,16 +9,16 @@ import ( "strings" ) -// InitDnsProviderEnvInfo 初始化dns所需要的参数信息 -func InitDnsProviderEnvInfo() { - privateInfos, _, err := waf_service.WafPrivateInfoServiceApp.GetListPureApi() +// LoadDnsProviderEnvInfo 初始化dns所需要的参数信息 +func LoadDnsProviderEnvInfo(groupName string, belongCloud string) { + privateInfos, _, err := waf_service.WafPrivateInfoServiceApp.GetListByGroupAndBelongCloudPureApi(groupName, belongCloud) if err == nil { for _, info := range privateInfos { err := os.Setenv(info.PrivateKey, info.PrivateValue) if err != nil { return } else { - zlog.Info(fmt.Sprintf("ENV `%s` LOADED", info.PrivateKey)) + zlog.Info(fmt.Sprintf("Cloud `%s` Group `%s` ENV `%s` LOADED", belongCloud, groupName, info.PrivateKey)) } } }