Merge pull request #335 from samwafgo/feat_318_same_cloud_multiple_accounts

feat:add same cloud multiple accounts
This commit is contained in:
samwafgo
2025-05-28 09:42:11 +08:00
committed by GitHub
22 changed files with 586 additions and 73 deletions
+5 -4
View File
@@ -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
)
+111
View File
@@ -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)
}
}
-6
View File
@@ -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
}
+4 -3
View File
@@ -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)
}
-1
View File
@@ -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相关*********/
+1 -5
View File
@@ -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
}
}
+9
View File
@@ -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"` //分组归属云
}
+5 -3
View File
@@ -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"` //备注
}
+26
View File
@@ -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
}
+10 -6
View File
@@ -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"`
+16 -14
View File
@@ -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"`
+1
View File
@@ -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 获取到期提示信息
+1
View File
@@ -36,6 +36,7 @@ type ApiGroup struct {
WafOtpRouter
AnalysisRouter
WafPrivateInfoRouter
WafPrivateGroupRouter
WafCacheRuleRouter
WafTunnelRouter
WafVpConfigRouter
+20
View File
@@ -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)
}
@@ -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
}
+92 -12
View File
@@ -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
}
+19 -15
View File
@@ -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
+3
View File
@@ -149,6 +149,9 @@ func InitCoreDb(currentDir string) (bool, error) {
//密钥信息
db.AutoMigrate(&model.PrivateInfo{})
//密钥分组信息
db.AutoMigrate(&model.PrivateGroup{})
//缓存规则
db.AutoMigrate(&model.CacheRule{})
+73
View File
@@ -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())
+8
View File
@@ -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))
+1
View File
@@ -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)
+4 -4
View File
@@ -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))
}
}
}