mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 05:00:25 +08:00 
			
		
		
		
	显示SSH认证相关集群、节点
This commit is contained in:
		@@ -3,7 +3,7 @@ package node
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@ package node
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/ipAddresses/ipaddressutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/actions"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,9 @@
 | 
			
		||||
package settings
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/actions"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package clusters
 | 
			
		||||
package clusterutils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,23 +1,29 @@
 | 
			
		||||
package clusters
 | 
			
		||||
package clusterutils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/iwind/TeaGo/actions"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Helper struct {
 | 
			
		||||
type ClustersHelper struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewHelper() *Helper {
 | 
			
		||||
	return &Helper{}
 | 
			
		||||
func NewClustersHelper() *ClustersHelper {
 | 
			
		||||
	return &ClustersHelper{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *Helper) BeforeAction(action *actions.ActionObject) {
 | 
			
		||||
func (this *ClustersHelper) BeforeAction(action *actions.ActionObject) {
 | 
			
		||||
	if action.Request.Method != http.MethodGet {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	action.Data["teaMenu"] = "clusters"
 | 
			
		||||
 | 
			
		||||
	selectedTabbar, _ := action.Data["mainTab"]
 | 
			
		||||
 | 
			
		||||
	tabbar := actionutils.NewTabbar()
 | 
			
		||||
	tabbar.Add("集群", "", "/clusters", "", selectedTabbar == "cluster")
 | 
			
		||||
	tabbar.Add("SSH认证", "", "/clusters/grants", "", selectedTabbar == "grant")
 | 
			
		||||
	actionutils.SetTabbar(action, tabbar)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										66
									
								
								internal/web/actions/default/clusters/grants/create.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								internal/web/actions/default/clusters/grants/create.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
package grants
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/actions"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type CreateAction struct {
 | 
			
		||||
	actionutils.ParentAction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *CreateAction) Init() {
 | 
			
		||||
	this.Nav("", "grant", "create")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *CreateAction) RunGet(params struct{}) {
 | 
			
		||||
	this.Data["methods"] = grantutils.AllGrantMethods()
 | 
			
		||||
 | 
			
		||||
	this.Show()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *CreateAction) RunPost(params struct {
 | 
			
		||||
	Name        string
 | 
			
		||||
	Method      string
 | 
			
		||||
	Username    string
 | 
			
		||||
	Password    string
 | 
			
		||||
	PrivateKey  string
 | 
			
		||||
	Description string
 | 
			
		||||
 | 
			
		||||
	Must *actions.Must
 | 
			
		||||
}) {
 | 
			
		||||
	params.Must.
 | 
			
		||||
		Field("name", params.Name).
 | 
			
		||||
		Require("请输入名称")
 | 
			
		||||
 | 
			
		||||
	switch params.Method {
 | 
			
		||||
	case "user":
 | 
			
		||||
		if len(params.Username) == 0 {
 | 
			
		||||
			this.FailField("username", "请输入SSH登录用户名")
 | 
			
		||||
		}
 | 
			
		||||
	case "privateKey":
 | 
			
		||||
		if len(params.PrivateKey) == 0 {
 | 
			
		||||
			this.FailField("privateKey", "请输入RSA私钥")
 | 
			
		||||
		}
 | 
			
		||||
	default:
 | 
			
		||||
		this.Fail("请选择正确的认证方式")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err := this.RPC().NodeGrantRPC().CreateNodeGrant(this.AdminContext(), &pb.CreateNodeGrantRequest{
 | 
			
		||||
		Name:        params.Name,
 | 
			
		||||
		Method:      params.Method,
 | 
			
		||||
		Username:    params.Username,
 | 
			
		||||
		Password:    params.Password,
 | 
			
		||||
		PrivateKey:  params.PrivateKey,
 | 
			
		||||
		Description: params.Description,
 | 
			
		||||
		NodeId:      0,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.Success()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										74
									
								
								internal/web/actions/default/clusters/grants/createPopup.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								internal/web/actions/default/clusters/grants/createPopup.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,74 @@
 | 
			
		||||
package grants
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/actions"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type CreatePopupAction struct {
 | 
			
		||||
	actionutils.ParentAction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *CreatePopupAction) Init() {
 | 
			
		||||
	this.Nav("", "", "")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *CreatePopupAction) RunGet(params struct{}) {
 | 
			
		||||
	this.Data["methods"] = grantutils.AllGrantMethods()
 | 
			
		||||
 | 
			
		||||
	this.Show()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *CreatePopupAction) RunPost(params struct {
 | 
			
		||||
	Name        string
 | 
			
		||||
	Method      string
 | 
			
		||||
	Username    string
 | 
			
		||||
	Password    string
 | 
			
		||||
	PrivateKey  string
 | 
			
		||||
	Description string
 | 
			
		||||
 | 
			
		||||
	Must *actions.Must
 | 
			
		||||
}) {
 | 
			
		||||
	params.Must.
 | 
			
		||||
		Field("name", params.Name).
 | 
			
		||||
		Require("请输入名称")
 | 
			
		||||
 | 
			
		||||
	switch params.Method {
 | 
			
		||||
	case "user":
 | 
			
		||||
		if len(params.Username) == 0 {
 | 
			
		||||
			this.FailField("username", "请输入SSH登录用户名")
 | 
			
		||||
		}
 | 
			
		||||
	case "privateKey":
 | 
			
		||||
		if len(params.PrivateKey) == 0 {
 | 
			
		||||
			this.FailField("privateKey", "请输入RSA私钥")
 | 
			
		||||
		}
 | 
			
		||||
	default:
 | 
			
		||||
		this.Fail("请选择正确的认证方式")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	createResp, err := this.RPC().NodeGrantRPC().CreateNodeGrant(this.AdminContext(), &pb.CreateNodeGrantRequest{
 | 
			
		||||
		Name:        params.Name,
 | 
			
		||||
		Method:      params.Method,
 | 
			
		||||
		Username:    params.Username,
 | 
			
		||||
		Password:    params.Password,
 | 
			
		||||
		PrivateKey:  params.PrivateKey,
 | 
			
		||||
		Description: params.Description,
 | 
			
		||||
		NodeId:      0,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.Data["grant"] = maps.Map{
 | 
			
		||||
		"id":         createResp.GrantId,
 | 
			
		||||
		"name":       params.Name,
 | 
			
		||||
		"method":     params.Method,
 | 
			
		||||
		"methodName": grantutils.FindGrantMethodName(params.Method),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.Success()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								internal/web/actions/default/clusters/grants/delete.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								internal/web/actions/default/clusters/grants/delete.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
package grants
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type DeleteAction struct {
 | 
			
		||||
	actionutils.ParentAction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *DeleteAction) RunPost(params struct {
 | 
			
		||||
	GrantId int64
 | 
			
		||||
}) {
 | 
			
		||||
	// 检查是否有别的集群或节点正在使用
 | 
			
		||||
	countResp, err := this.RPC().NodeClusterRPC().CountAllEnabledNodeClustersWithGrantId(this.AdminContext(), &pb.CountAllEnabledNodeClustersWithGrantIdRequest{
 | 
			
		||||
		GrantId: params.GrantId,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if countResp.Count > 0 {
 | 
			
		||||
		this.Fail("有集群正在使用此服务,请修改后再删除")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	countResp2, err := this.RPC().NodeRPC().CountAllEnabledNodesWithGrantId(this.AdminContext(), &pb.CountAllEnabledNodesWithGrantIdRequest{GrantId: params.GrantId})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if countResp2.Count > 0 {
 | 
			
		||||
		this.Fail("有节点正在使用此服务,请修改后再删除")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 删除
 | 
			
		||||
	_, err = this.RPC().NodeGrantRPC().DisableNodeGrant(this.AdminContext(), &pb.DisableNodeGrantRequest{GrantId: params.GrantId})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.Success()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										88
									
								
								internal/web/actions/default/clusters/grants/grant.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								internal/web/actions/default/clusters/grants/grant.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,88 @@
 | 
			
		||||
package grants
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type GrantAction struct {
 | 
			
		||||
	actionutils.ParentAction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *GrantAction) Init() {
 | 
			
		||||
	this.Nav("", "grant", "index")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *GrantAction) RunGet(params struct {
 | 
			
		||||
	GrantId int64
 | 
			
		||||
}) {
 | 
			
		||||
	grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: params.GrantId})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if grantResp.Grant == nil {
 | 
			
		||||
		this.WriteString("can not find the grant")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO 处理节点专用的认证
 | 
			
		||||
 | 
			
		||||
	grant := grantResp.Grant
 | 
			
		||||
	this.Data["grant"] = maps.Map{
 | 
			
		||||
		"id":          grant.Id,
 | 
			
		||||
		"name":        grant.Name,
 | 
			
		||||
		"method":      grant.Method,
 | 
			
		||||
		"methodName":  grantutils.FindGrantMethodName(grant.Method),
 | 
			
		||||
		"username":    grant.Username,
 | 
			
		||||
		"password":    grant.Password,
 | 
			
		||||
		"privateKey":  grant.PrivateKey,
 | 
			
		||||
		"description": grant.Description,
 | 
			
		||||
		"su":          grant.Su,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 使用此认证的集群
 | 
			
		||||
	clusterMaps := []maps.Map{}
 | 
			
		||||
	clustersResp, err := this.RPC().NodeClusterRPC().FindAllEnabledNodeClustersWithGrantId(this.AdminContext(), &pb.FindAllEnabledNodeClustersWithGrantIdRequest{GrantId: params.GrantId})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	for _, cluster := range clustersResp.Clusters {
 | 
			
		||||
		clusterMaps = append(clusterMaps, maps.Map{
 | 
			
		||||
			"id":   cluster.Id,
 | 
			
		||||
			"name": cluster.Name,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	this.Data["clusters"] = clusterMaps
 | 
			
		||||
 | 
			
		||||
	// 使用此认证的节点
 | 
			
		||||
	nodeMaps := []maps.Map{}
 | 
			
		||||
	nodesResp, err := this.RPC().NodeRPC().FindAllEnabledNodesWithGrantId(this.AdminContext(), &pb.FindAllEnabledNodesWithGrantIdRequest{GrantId: params.GrantId})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	for _, node := range nodesResp.Nodes {
 | 
			
		||||
		if node.Cluster == nil {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		clusterMap := maps.Map{
 | 
			
		||||
			"id":   node.Cluster.Id,
 | 
			
		||||
			"name": node.Cluster.Name,
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		nodeMaps = append(nodeMaps, maps.Map{
 | 
			
		||||
			"id":      node.Id,
 | 
			
		||||
			"name":    node.Name,
 | 
			
		||||
			"cluster": clusterMap,
 | 
			
		||||
			"isOn":    node.IsOn,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	this.Data["nodes"] = nodeMaps
 | 
			
		||||
 | 
			
		||||
	this.Show()
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,27 @@
 | 
			
		||||
package grantutils
 | 
			
		||||
 | 
			
		||||
import "github.com/iwind/TeaGo/maps"
 | 
			
		||||
 | 
			
		||||
// 所有的认证类型
 | 
			
		||||
func AllGrantMethods() []maps.Map {
 | 
			
		||||
	return []maps.Map{
 | 
			
		||||
		{
 | 
			
		||||
			"name":  "用户名+密码",
 | 
			
		||||
			"value": "user",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			"name":  "私钥",
 | 
			
		||||
			"value": "privateKey",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 获得对应的认证类型名称
 | 
			
		||||
func FindGrantMethodName(method string) string {
 | 
			
		||||
	for _, m := range AllGrantMethods() {
 | 
			
		||||
		if m.GetString("value") == method {
 | 
			
		||||
			return m.GetString("name")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return ""
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										67
									
								
								internal/web/actions/default/clusters/grants/index.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								internal/web/actions/default/clusters/grants/index.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
package grants
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type IndexAction struct {
 | 
			
		||||
	actionutils.ParentAction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *IndexAction) Init() {
 | 
			
		||||
	this.Nav("", "grant", "index")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *IndexAction) RunGet(params struct{}) {
 | 
			
		||||
	countResp, err := this.RPC().NodeGrantRPC().CountAllEnabledNodeGrants(this.AdminContext(), &pb.CountAllEnabledNodeGrantsRequest{})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	page := this.NewPage(countResp.Count)
 | 
			
		||||
	this.Data["page"] = page.AsHTML()
 | 
			
		||||
 | 
			
		||||
	grantsResp, err := this.RPC().NodeGrantRPC().ListEnabledNodeGrants(this.AdminContext(), &pb.ListEnabledNodeGrantsRequest{
 | 
			
		||||
		Offset: page.Offset,
 | 
			
		||||
		Size:   page.Size,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	grantMaps := []maps.Map{}
 | 
			
		||||
	for _, grant := range grantsResp.Grants {
 | 
			
		||||
		// 集群数
 | 
			
		||||
		countClustersResp, err := this.RPC().NodeClusterRPC().CountAllEnabledNodeClustersWithGrantId(this.AdminContext(), &pb.CountAllEnabledNodeClustersWithGrantIdRequest{GrantId: grant.Id})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			this.ErrorPage(err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		countClusters := countClustersResp.Count
 | 
			
		||||
 | 
			
		||||
		// 节点数
 | 
			
		||||
		countNodesResp, err := this.RPC().NodeRPC().CountAllEnabledNodesWithGrantId(this.AdminContext(), &pb.CountAllEnabledNodesWithGrantIdRequest{GrantId: grant.Id})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			this.ErrorPage(err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		countNodes := countNodesResp.Count
 | 
			
		||||
 | 
			
		||||
		grantMaps = append(grantMaps, maps.Map{
 | 
			
		||||
			"id":   grant.Id,
 | 
			
		||||
			"name": grant.Name,
 | 
			
		||||
			"method": maps.Map{
 | 
			
		||||
				"type": grant.Method,
 | 
			
		||||
				"name": grantutils.FindGrantMethodName(grant.Method),
 | 
			
		||||
			},
 | 
			
		||||
			"countClusters": countClusters,
 | 
			
		||||
			"countNodes":    countNodes,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	this.Data["grants"] = grantMaps
 | 
			
		||||
 | 
			
		||||
	this.Show()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								internal/web/actions/default/clusters/grants/init.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								internal/web/actions/default/clusters/grants/init.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
package grants
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/clusterutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
 | 
			
		||||
	"github.com/iwind/TeaGo"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	TeaGo.BeforeStart(func(server *TeaGo.Server) {
 | 
			
		||||
		server.
 | 
			
		||||
			Helper(helpers.NewUserMustAuth()).
 | 
			
		||||
			Helper(clusterutils.NewClustersHelper()).
 | 
			
		||||
			Prefix("/clusters/grants").
 | 
			
		||||
 | 
			
		||||
			// 授权管理
 | 
			
		||||
			Get("", new(IndexAction)).
 | 
			
		||||
			GetPost("/create", new(CreateAction)).
 | 
			
		||||
			GetPost("/update", new(UpdateAction)).
 | 
			
		||||
			Post("/delete", new(DeleteAction)).
 | 
			
		||||
			Get("/grant", new(GrantAction)).
 | 
			
		||||
			GetPost("/selectPopup", new(SelectPopupAction)).
 | 
			
		||||
			GetPost("/createPopup", new(CreatePopupAction)).
 | 
			
		||||
			GetPost("/updatePopup", new(UpdatePopupAction)).
 | 
			
		||||
			EndAll()
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										72
									
								
								internal/web/actions/default/clusters/grants/selectPopup.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								internal/web/actions/default/clusters/grants/selectPopup.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,72 @@
 | 
			
		||||
package grants
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/actions"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type SelectPopupAction struct {
 | 
			
		||||
	actionutils.ParentAction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *SelectPopupAction) Init() {
 | 
			
		||||
	this.Nav("", "", "")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *SelectPopupAction) RunGet(params struct{}) {
 | 
			
		||||
	// 所有的认证
 | 
			
		||||
	grantsResp, err := this.RPC().NodeGrantRPC().FindAllEnabledNodeGrants(this.AdminContext(), &pb.FindAllEnabledNodeGrantsRequest{})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	grants := grantsResp.Grants
 | 
			
		||||
	grantMaps := []maps.Map{}
 | 
			
		||||
	for _, grant := range grants {
 | 
			
		||||
		grantMaps = append(grantMaps, maps.Map{
 | 
			
		||||
			"id":         grant.Id,
 | 
			
		||||
			"name":       grant.Name,
 | 
			
		||||
			"method":     grant.Method,
 | 
			
		||||
			"methodName": grantutils.FindGrantMethodName(grant.Method),
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	this.Data["grants"] = grantMaps
 | 
			
		||||
 | 
			
		||||
	this.Show()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *SelectPopupAction) RunPost(params struct {
 | 
			
		||||
	GrantId int64
 | 
			
		||||
	Must    *actions.Must
 | 
			
		||||
}) {
 | 
			
		||||
	if params.GrantId <= 0 {
 | 
			
		||||
		this.Data["grant"] = maps.Map{
 | 
			
		||||
			"id":         params.GrantId,
 | 
			
		||||
			"name":       "",
 | 
			
		||||
			"method":     "",
 | 
			
		||||
			"methodName": "",
 | 
			
		||||
		}
 | 
			
		||||
		this.Success()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: params.GrantId})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	grant := grantResp.Grant
 | 
			
		||||
	if grant == nil {
 | 
			
		||||
		this.Fail("找不到要使用的认证")
 | 
			
		||||
	}
 | 
			
		||||
	this.Data["grant"] = maps.Map{
 | 
			
		||||
		"id":         grant.Id,
 | 
			
		||||
		"name":       grant.Name,
 | 
			
		||||
		"method":     grant.Method,
 | 
			
		||||
		"methodName": grantutils.FindGrantMethodName(grant.Method),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.Success()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										98
									
								
								internal/web/actions/default/clusters/grants/update.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								internal/web/actions/default/clusters/grants/update.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,98 @@
 | 
			
		||||
package grants
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/actions"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type UpdateAction struct {
 | 
			
		||||
	actionutils.ParentAction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *UpdateAction) Init() {
 | 
			
		||||
	this.Nav("", "grant", "update")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *UpdateAction) RunGet(params struct {
 | 
			
		||||
	GrantId int64
 | 
			
		||||
}) {
 | 
			
		||||
	this.Data["methods"] = grantutils.AllGrantMethods()
 | 
			
		||||
 | 
			
		||||
	grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: params.GrantId})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if grantResp.Grant == nil {
 | 
			
		||||
		this.WriteString("can not find the grant")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO 处理节点专用的认证
 | 
			
		||||
 | 
			
		||||
	grant := grantResp.Grant
 | 
			
		||||
	this.Data["grant"] = maps.Map{
 | 
			
		||||
		"id":          grant.Id,
 | 
			
		||||
		"name":        grant.Name,
 | 
			
		||||
		"method":      grant.Method,
 | 
			
		||||
		"methodName":  grantutils.FindGrantMethodName(grant.Method),
 | 
			
		||||
		"username":    grant.Username,
 | 
			
		||||
		"password":    grant.Password,
 | 
			
		||||
		"privateKey":  grant.PrivateKey,
 | 
			
		||||
		"description": grant.Description,
 | 
			
		||||
		"su":          grant.Su,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.Show()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *UpdateAction) RunPost(params struct {
 | 
			
		||||
	GrantId     int64
 | 
			
		||||
	Name        string
 | 
			
		||||
	Method      string
 | 
			
		||||
	Username    string
 | 
			
		||||
	Password    string
 | 
			
		||||
	PrivateKey  string
 | 
			
		||||
	Description string
 | 
			
		||||
 | 
			
		||||
	Must *actions.Must
 | 
			
		||||
}) {
 | 
			
		||||
	params.Must.
 | 
			
		||||
		Field("name", params.Name).
 | 
			
		||||
		Require("请输入名称")
 | 
			
		||||
 | 
			
		||||
	switch params.Method {
 | 
			
		||||
	case "user":
 | 
			
		||||
		if len(params.Username) == 0 {
 | 
			
		||||
			this.FailField("username", "请输入SSH登录用户名")
 | 
			
		||||
		}
 | 
			
		||||
	case "privateKey":
 | 
			
		||||
		if len(params.PrivateKey) == 0 {
 | 
			
		||||
			this.FailField("privateKey", "请输入RSA私钥")
 | 
			
		||||
		}
 | 
			
		||||
	default:
 | 
			
		||||
		this.Fail("请选择正确的认证方式")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO 检查grantId是否存在
 | 
			
		||||
 | 
			
		||||
	_, err := this.RPC().NodeGrantRPC().UpdateNodeGrant(this.AdminContext(), &pb.UpdateNodeGrantRequest{
 | 
			
		||||
		GrantId:     params.GrantId,
 | 
			
		||||
		Name:        params.Name,
 | 
			
		||||
		Method:      params.Method,
 | 
			
		||||
		Username:    params.Username,
 | 
			
		||||
		Password:    params.Password,
 | 
			
		||||
		PrivateKey:  params.PrivateKey,
 | 
			
		||||
		Description: params.Description,
 | 
			
		||||
		NodeId:      0,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.Success()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										104
									
								
								internal/web/actions/default/clusters/grants/updatePopup.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								internal/web/actions/default/clusters/grants/updatePopup.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,104 @@
 | 
			
		||||
package grants
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/actions"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type UpdatePopupAction struct {
 | 
			
		||||
	actionutils.ParentAction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *UpdatePopupAction) Init() {
 | 
			
		||||
	this.Nav("", "", "")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *UpdatePopupAction) RunGet(params struct {
 | 
			
		||||
	GrantId int64
 | 
			
		||||
}) {
 | 
			
		||||
	this.Data["methods"] = grantutils.AllGrantMethods()
 | 
			
		||||
 | 
			
		||||
	grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: params.GrantId})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if grantResp.Grant == nil {
 | 
			
		||||
		this.WriteString("找不到要操作的对象")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	grant := grantResp.Grant
 | 
			
		||||
	this.Data["grant"] = maps.Map{
 | 
			
		||||
		"id":          grant.Id,
 | 
			
		||||
		"nodeId":      grant.NodeId,
 | 
			
		||||
		"method":      grant.Method,
 | 
			
		||||
		"name":        grant.Name,
 | 
			
		||||
		"username":    grant.Username,
 | 
			
		||||
		"password":    grant.Password,
 | 
			
		||||
		"description": grant.Description,
 | 
			
		||||
		"privateKey":  grant.PrivateKey,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.Show()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *UpdatePopupAction) RunPost(params struct {
 | 
			
		||||
	GrantId     int64
 | 
			
		||||
	NodeId      int64
 | 
			
		||||
	Name        string
 | 
			
		||||
	Method      string
 | 
			
		||||
	Username    string
 | 
			
		||||
	Password    string
 | 
			
		||||
	PrivateKey  string
 | 
			
		||||
	Description string
 | 
			
		||||
 | 
			
		||||
	Must *actions.Must
 | 
			
		||||
}) {
 | 
			
		||||
	params.Must.
 | 
			
		||||
		Field("name", params.Name).
 | 
			
		||||
		Require("请输入名称")
 | 
			
		||||
 | 
			
		||||
	switch params.Method {
 | 
			
		||||
	case "user":
 | 
			
		||||
		if len(params.Username) == 0 {
 | 
			
		||||
			this.FailField("username", "请输入SSH登录用户名")
 | 
			
		||||
		}
 | 
			
		||||
	case "privateKey":
 | 
			
		||||
		if len(params.PrivateKey) == 0 {
 | 
			
		||||
			this.FailField("privateKey", "请输入RSA私钥")
 | 
			
		||||
		}
 | 
			
		||||
	default:
 | 
			
		||||
		this.Fail("请选择正确的认证方式")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 执行修改
 | 
			
		||||
	_, err := this.RPC().NodeGrantRPC().UpdateNodeGrant(this.AdminContext(), &pb.UpdateNodeGrantRequest{
 | 
			
		||||
		GrantId:     params.GrantId,
 | 
			
		||||
		Name:        params.Name,
 | 
			
		||||
		Method:      params.Method,
 | 
			
		||||
		Username:    params.Username,
 | 
			
		||||
		Password:    params.Password,
 | 
			
		||||
		PrivateKey:  params.PrivateKey,
 | 
			
		||||
		Description: params.Description,
 | 
			
		||||
		NodeId:      params.NodeId,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 返回信息
 | 
			
		||||
	this.Data["grant"] = maps.Map{
 | 
			
		||||
		"id":         params.GrantId,
 | 
			
		||||
		"name":       params.Name,
 | 
			
		||||
		"method":     params.Method,
 | 
			
		||||
		"methodName": grantutils.FindGrantMethodName(params.Method),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.Success()
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package clusters
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/clusterutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
 | 
			
		||||
	"github.com/iwind/TeaGo"
 | 
			
		||||
)
 | 
			
		||||
@@ -9,7 +10,7 @@ func init() {
 | 
			
		||||
	TeaGo.BeforeStart(func(server *TeaGo.Server) {
 | 
			
		||||
		server.
 | 
			
		||||
			Helper(helpers.NewUserMustAuth()).
 | 
			
		||||
			Helper(NewHelper()).
 | 
			
		||||
			Helper(clusterutils.NewClustersHelper()).
 | 
			
		||||
			Prefix("/clusters").
 | 
			
		||||
			Get("", new(IndexAction)).
 | 
			
		||||
			GetPost("/create", new(CreateAction)).
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user