mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-03 04:26:34 +08:00
增加认证节点管理
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package models
|
||||
package authority
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -1,4 +1,4 @@
|
||||
package models
|
||||
package authority
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
@@ -1,4 +1,4 @@
|
||||
package models
|
||||
package authority
|
||||
|
||||
// AuthorityKey 企业版认证信息
|
||||
type AuthorityKey struct {
|
||||
1
internal/db/models/authority/authority_key_model_ext.go
Normal file
1
internal/db/models/authority/authority_key_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package authority
|
||||
198
internal/db/models/authority/authority_node_dao.go
Normal file
198
internal/db/models/authority/authority_node_dao.go
Normal file
@@ -0,0 +1,198 @@
|
||||
package authority
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
const (
|
||||
AuthorityNodeStateEnabled = 1 // 已启用
|
||||
AuthorityNodeStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type AuthorityNodeDAO dbs.DAO
|
||||
|
||||
func NewAuthorityNodeDAO() *AuthorityNodeDAO {
|
||||
return dbs.NewDAO(&AuthorityNodeDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeAuthorityNodes",
|
||||
Model: new(AuthorityNode),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*AuthorityNodeDAO)
|
||||
}
|
||||
|
||||
var SharedAuthorityNodeDAO *AuthorityNodeDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedAuthorityNodeDAO = NewAuthorityNodeDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableAuthorityNode 启用条目
|
||||
func (this *AuthorityNodeDAO) EnableAuthorityNode(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", AuthorityNodeStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableAuthorityNode 禁用条目
|
||||
func (this *AuthorityNodeDAO) DisableAuthorityNode(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", AuthorityNodeStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledAuthorityNode 查找启用中的条目
|
||||
func (this *AuthorityNodeDAO) FindEnabledAuthorityNode(tx *dbs.Tx, id int64) (*AuthorityNode, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", AuthorityNodeStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*AuthorityNode), err
|
||||
}
|
||||
|
||||
// FindAuthorityNodeName 根据主键查找名称
|
||||
func (this *AuthorityNodeDAO) FindAuthorityNodeName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// FindAllEnabledAuthorityNodes 列出所有可用认证节点
|
||||
func (this *AuthorityNodeDAO) FindAllEnabledAuthorityNodes(tx *dbs.Tx) (result []*AuthorityNode, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(AuthorityNodeStateEnabled).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// CountAllEnabledAuthorityNodes 计算认证节点数量
|
||||
func (this *AuthorityNodeDAO) CountAllEnabledAuthorityNodes(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(AuthorityNodeStateEnabled).
|
||||
Count()
|
||||
}
|
||||
|
||||
// ListEnabledAuthorityNodes 列出单页的认证节点
|
||||
func (this *AuthorityNodeDAO) ListEnabledAuthorityNodes(tx *dbs.Tx, offset int64, size int64) (result []*AuthorityNode, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(AuthorityNodeStateEnabled).
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Desc("order").
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// CreateAuthorityNode 创建认证节点
|
||||
func (this *AuthorityNodeDAO) CreateAuthorityNode(tx *dbs.Tx, name string, description string, isOn bool) (nodeId int64, err error) {
|
||||
uniqueId, err := this.GenUniqueId(tx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
secret := rands.String(32)
|
||||
err = models.NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, models.NodeRoleAuthority)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
op := NewAuthorityNodeOperator()
|
||||
op.IsOn = isOn
|
||||
op.UniqueId = uniqueId
|
||||
op.Secret = secret
|
||||
op.Name = name
|
||||
op.Description = description
|
||||
op.State = AuthorityNodeStateEnabled
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// UpdateAuthorityNode 修改认证节点
|
||||
func (this *AuthorityNodeDAO) UpdateAuthorityNode(tx *dbs.Tx, nodeId int64, name string, description string, isOn bool) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
}
|
||||
|
||||
op := NewAuthorityNodeOperator()
|
||||
op.Id = nodeId
|
||||
op.Name = name
|
||||
op.Description = description
|
||||
op.IsOn = isOn
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledAuthorityNodeWithUniqueId 根据唯一ID获取节点信息
|
||||
func (this *AuthorityNodeDAO) FindEnabledAuthorityNodeWithUniqueId(tx *dbs.Tx, uniqueId string) (*AuthorityNode, error) {
|
||||
result, err := this.Query(tx).
|
||||
Attr("uniqueId", uniqueId).
|
||||
Attr("state", AuthorityNodeStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*AuthorityNode), err
|
||||
}
|
||||
|
||||
// FindEnabledAuthorityNodeIdWithUniqueId 根据唯一ID获取节点ID
|
||||
func (this *AuthorityNodeDAO) FindEnabledAuthorityNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("uniqueId", uniqueId).
|
||||
Attr("state", AuthorityNodeStateEnabled).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// GenUniqueId 生成唯一ID
|
||||
func (this *AuthorityNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
|
||||
for {
|
||||
uniqueId := rands.HexString(32)
|
||||
ok, err := this.Query(tx).
|
||||
Attr("uniqueId", uniqueId).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ok {
|
||||
continue
|
||||
}
|
||||
return uniqueId, nil
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateNodeStatus 更改节点状态
|
||||
func (this *AuthorityNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
|
||||
if statusJSON == nil {
|
||||
return nil
|
||||
}
|
||||
_, err := this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Set("status", string(statusJSON)).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
6
internal/db/models/authority/authority_node_dao_test.go
Normal file
6
internal/db/models/authority/authority_node_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package authority
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
36
internal/db/models/authority/authority_node_model.go
Normal file
36
internal/db/models/authority/authority_node_model.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package authority
|
||||
|
||||
// AuthorityNode 监控节点
|
||||
type AuthorityNode struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn uint8 `field:"isOn"` // 是否启用
|
||||
UniqueId string `field:"uniqueId"` // 唯一ID
|
||||
Secret string `field:"secret"` // 密钥
|
||||
Name string `field:"name"` // 名称
|
||||
Description string `field:"description"` // 描述
|
||||
Order uint32 `field:"order"` // 排序
|
||||
State uint8 `field:"state"` // 状态
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||
Weight uint32 `field:"weight"` // 权重
|
||||
Status string `field:"status"` // 运行状态
|
||||
}
|
||||
|
||||
type AuthorityNodeOperator struct {
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 是否启用
|
||||
UniqueId interface{} // 唯一ID
|
||||
Secret interface{} // 密钥
|
||||
Name interface{} // 名称
|
||||
Description interface{} // 描述
|
||||
Order interface{} // 排序
|
||||
State interface{} // 状态
|
||||
CreatedAt interface{} // 创建时间
|
||||
AdminId interface{} // 管理员ID
|
||||
Weight interface{} // 权重
|
||||
Status interface{} // 运行状态
|
||||
}
|
||||
|
||||
func NewAuthorityNodeOperator() *AuthorityNodeOperator {
|
||||
return &AuthorityNodeOperator{}
|
||||
}
|
||||
1
internal/db/models/authority/authority_node_model_ext.go
Normal file
1
internal/db/models/authority/authority_node_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package authority
|
||||
@@ -1 +0,0 @@
|
||||
package models
|
||||
@@ -1,15 +1,12 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -38,7 +35,7 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
// EnableMonitorNode 启用条目
|
||||
func (this *MonitorNodeDAO) EnableMonitorNode(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -47,7 +44,7 @@ func (this *MonitorNodeDAO) EnableMonitorNode(tx *dbs.Tx, id int64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
// DisableMonitorNode 禁用条目
|
||||
func (this *MonitorNodeDAO) DisableMonitorNode(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -56,7 +53,7 @@ func (this *MonitorNodeDAO) DisableMonitorNode(tx *dbs.Tx, id int64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
// FindEnabledMonitorNode 查找启用中的条目
|
||||
func (this *MonitorNodeDAO) FindEnabledMonitorNode(tx *dbs.Tx, id int64) (*MonitorNode, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -68,7 +65,7 @@ func (this *MonitorNodeDAO) FindEnabledMonitorNode(tx *dbs.Tx, id int64) (*Monit
|
||||
return result.(*MonitorNode), err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
// FindMonitorNodeName 根据主键查找名称
|
||||
func (this *MonitorNodeDAO) FindMonitorNodeName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -76,7 +73,7 @@ func (this *MonitorNodeDAO) FindMonitorNodeName(tx *dbs.Tx, id int64) (string, e
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 列出所有可用监控节点
|
||||
// FindAllEnabledMonitorNodes 列出所有可用监控节点
|
||||
func (this *MonitorNodeDAO) FindAllEnabledMonitorNodes(tx *dbs.Tx) (result []*MonitorNode, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(MonitorNodeStateEnabled).
|
||||
@@ -87,14 +84,14 @@ func (this *MonitorNodeDAO) FindAllEnabledMonitorNodes(tx *dbs.Tx) (result []*Mo
|
||||
return
|
||||
}
|
||||
|
||||
// 计算监控节点数量
|
||||
// CountAllEnabledMonitorNodes 计算监控节点数量
|
||||
func (this *MonitorNodeDAO) CountAllEnabledMonitorNodes(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(MonitorNodeStateEnabled).
|
||||
Count()
|
||||
}
|
||||
|
||||
// 列出单页的监控节点
|
||||
// ListEnabledMonitorNodes 列出单页的监控节点
|
||||
func (this *MonitorNodeDAO) ListEnabledMonitorNodes(tx *dbs.Tx, offset int64, size int64) (result []*MonitorNode, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(MonitorNodeStateEnabled).
|
||||
@@ -107,34 +104,7 @@ func (this *MonitorNodeDAO) ListEnabledMonitorNodes(tx *dbs.Tx, offset int64, si
|
||||
return
|
||||
}
|
||||
|
||||
// 根据主机名和端口获取ID
|
||||
func (this *MonitorNodeDAO) FindEnabledMonitorNodeIdWithAddr(tx *dbs.Tx, protocol string, host string, port int) (int64, error) {
|
||||
addr := maps.Map{
|
||||
"protocol": protocol,
|
||||
"host": host,
|
||||
"portRange": strconv.Itoa(port),
|
||||
}
|
||||
addrJSON, err := json.Marshal(addr)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
one, err := this.Query(tx).
|
||||
State(MonitorNodeStateEnabled).
|
||||
Where("JSON_CONTAINS(accessAddrs, :addr)").
|
||||
Param("addr", string(addrJSON)).
|
||||
ResultPk().
|
||||
Find()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if one == nil {
|
||||
return 0, nil
|
||||
}
|
||||
return int64(one.(*MonitorNode).Id), nil
|
||||
}
|
||||
|
||||
// 创建监控节点
|
||||
// CreateMonitorNode 创建监控节点
|
||||
func (this *MonitorNodeDAO) CreateMonitorNode(tx *dbs.Tx, name string, description string, isOn bool) (nodeId int64, err error) {
|
||||
uniqueId, err := this.GenUniqueId(tx)
|
||||
if err != nil {
|
||||
@@ -161,7 +131,7 @@ func (this *MonitorNodeDAO) CreateMonitorNode(tx *dbs.Tx, name string, descripti
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 修改监控节点
|
||||
// UpdateMonitorNode 修改监控节点
|
||||
func (this *MonitorNodeDAO) UpdateMonitorNode(tx *dbs.Tx, nodeId int64, name string, description string, isOn bool) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
@@ -176,7 +146,7 @@ func (this *MonitorNodeDAO) UpdateMonitorNode(tx *dbs.Tx, nodeId int64, name str
|
||||
return err
|
||||
}
|
||||
|
||||
// 根据唯一ID获取节点信息
|
||||
// FindEnabledMonitorNodeWithUniqueId 根据唯一ID获取节点信息
|
||||
func (this *MonitorNodeDAO) FindEnabledMonitorNodeWithUniqueId(tx *dbs.Tx, uniqueId string) (*MonitorNode, error) {
|
||||
result, err := this.Query(tx).
|
||||
Attr("uniqueId", uniqueId).
|
||||
@@ -188,7 +158,7 @@ func (this *MonitorNodeDAO) FindEnabledMonitorNodeWithUniqueId(tx *dbs.Tx, uniqu
|
||||
return result.(*MonitorNode), err
|
||||
}
|
||||
|
||||
// 根据唯一ID获取节点ID
|
||||
// FindEnabledMonitorNodeIdWithUniqueId 根据唯一ID获取节点ID
|
||||
func (this *MonitorNodeDAO) FindEnabledMonitorNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("uniqueId", uniqueId).
|
||||
@@ -197,7 +167,7 @@ func (this *MonitorNodeDAO) FindEnabledMonitorNodeIdWithUniqueId(tx *dbs.Tx, uni
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// 生成唯一ID
|
||||
// GenUniqueId 生成唯一ID
|
||||
func (this *MonitorNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
|
||||
for {
|
||||
uniqueId := rands.HexString(32)
|
||||
@@ -214,7 +184,7 @@ func (this *MonitorNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// 更改节点状态
|
||||
// UpdateNodeStatus 更改节点状态
|
||||
func (this *MonitorNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
|
||||
if statusJSON == nil {
|
||||
return nil
|
||||
|
||||
@@ -3,14 +3,15 @@ package models
|
||||
type NodeRole = string
|
||||
|
||||
const (
|
||||
NodeRoleAdmin NodeRole = "admin"
|
||||
NodeRoleUser NodeRole = "user"
|
||||
NodeRoleProvider NodeRole = "provider"
|
||||
NodeRoleAPI NodeRole = "api"
|
||||
NodeRoleDatabase NodeRole = "database"
|
||||
NodeRoleLog NodeRole = "log"
|
||||
NodeRoleDNS NodeRole = "dns"
|
||||
NodeRoleMonitor NodeRole = "monitor"
|
||||
NodeRoleNode NodeRole = "node"
|
||||
NodeRoleCluster NodeRole = "cluster"
|
||||
NodeRoleAdmin NodeRole = "admin"
|
||||
NodeRoleUser NodeRole = "user"
|
||||
NodeRoleProvider NodeRole = "provider"
|
||||
NodeRoleAPI NodeRole = "api"
|
||||
NodeRoleDatabase NodeRole = "database"
|
||||
NodeRoleLog NodeRole = "log"
|
||||
NodeRoleDNS NodeRole = "dns"
|
||||
NodeRoleMonitor NodeRole = "monitor"
|
||||
NodeRoleNode NodeRole = "node"
|
||||
NodeRoleCluster NodeRole = "cluster"
|
||||
NodeRoleAuthority NodeRole = "authority"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user