节点SSH密码和私钥均以掩码方式显示

This commit is contained in:
GoEdgeLab
2024-03-18 10:51:14 +08:00
parent 40e5c9a34c
commit 3c3777a08f
8 changed files with 50 additions and 23 deletions

View File

@@ -31,7 +31,14 @@ func (this *GrantAction) RunGet(params struct {
// TODO 处理节点专用的认证
grant := grantResp.NodeGrant
var grant = grantResp.NodeGrant
var privateKey = grant.PrivateKey
const maskLength = 64
if len(privateKey) > maskLength+32 {
privateKey = privateKey[:maskLength] + strings.Repeat("*", len(privateKey)-maskLength)
}
this.Data["grant"] = maps.Map{
"id": grant.Id,
"name": grant.Name,
@@ -39,7 +46,7 @@ func (this *GrantAction) RunGet(params struct {
"methodName": grantutils.FindGrantMethodName(grant.Method, this.LangCode()),
"username": grant.Username,
"password": strings.Repeat("*", len(grant.Password)),
"privateKey": grant.PrivateKey,
"privateKey": privateKey,
"passphrase": strings.Repeat("*", len(grant.Passphrase)),
"description": grant.Description,
"su": grant.Su,

View File

@@ -27,7 +27,7 @@ func (this *IndexAction) RunGet(params struct {
this.ErrorPage(err)
return
}
page := this.NewPage(countResp.Count)
var page = this.NewPage(countResp.Count)
this.Data["page"] = page.AsHTML()
grantsResp, err := this.RPC().NodeGrantRPC().ListEnabledNodeGrants(this.AdminContext(), &pb.ListEnabledNodeGrantsRequest{
@@ -39,7 +39,7 @@ func (this *IndexAction) RunGet(params struct {
this.ErrorPage(err)
return
}
grantMaps := []maps.Map{}
var grantMaps = []maps.Map{}
for _, grant := range grantsResp.NodeGrants {
// 集群数
countClustersResp, err := this.RPC().NodeClusterRPC().CountAllEnabledNodeClustersWithNodeGrantId(this.AdminContext(), &pb.CountAllEnabledNodeClustersWithNodeGrantIdRequest{NodeGrantId: grant.Id})
@@ -47,7 +47,7 @@ func (this *IndexAction) RunGet(params struct {
this.ErrorPage(err)
return
}
countClusters := countClustersResp.Count
var countClusters = countClustersResp.Count
// 节点数
countNodesResp, err := this.RPC().NodeRPC().CountAllEnabledNodesWithNodeGrantId(this.AdminContext(), &pb.CountAllEnabledNodesWithNodeGrantIdRequest{NodeGrantId: grant.Id})
@@ -55,7 +55,7 @@ func (this *IndexAction) RunGet(params struct {
this.ErrorPage(err)
return
}
countNodes := countNodesResp.Count
var countNodes = countNodesResp.Count
grantMaps = append(grantMaps, maps.Map{
"id": grant.Id,

View File

@@ -1,12 +1,14 @@
package grants
import ( "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
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/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"golang.org/x/crypto/ssh"
"strings"
)
type UpdateAction struct {
@@ -34,15 +36,23 @@ func (this *UpdateAction) RunGet(params struct {
// TODO 处理节点专用的认证
grant := grantResp.NodeGrant
var grant = grantResp.NodeGrant
// private key
var privateKey = grant.PrivateKey
const maskLength = 64
if len(privateKey) > maskLength+32 {
privateKey = privateKey[:maskLength] + strings.Repeat("*", len(privateKey)-maskLength)
}
this.Data["grant"] = maps.Map{
"id": grant.Id,
"name": grant.Name,
"method": grant.Method,
"methodName": grantutils.FindGrantMethodName(grant.Method, this.LangCode()),
"username": grant.Username,
"password": grant.Password,
"privateKey": grant.PrivateKey,
"password": strings.Repeat("*", len(grant.Password)),
"privateKey": privateKey,
"passphrase": grant.Passphrase,
"description": grant.Description,
"su": grant.Su,
@@ -85,15 +95,17 @@ func (this *UpdateAction) RunPost(params struct {
}
// 验证私钥
var err error
if len(params.Passphrase) > 0 {
_, err = ssh.ParsePrivateKeyWithPassphrase([]byte(params.PrivateKey), []byte(params.Passphrase))
} else {
_, err = ssh.ParsePrivateKey([]byte(params.PrivateKey))
}
if err != nil {
this.Fail("私钥验证失败,请检查格式:" + err.Error())
return
if !strings.HasSuffix(params.PrivateKey, "******") /* 非掩码 */ {
var err error
if len(params.Passphrase) > 0 {
_, err = ssh.ParsePrivateKeyWithPassphrase([]byte(params.PrivateKey), []byte(params.Passphrase))
} else {
_, err = ssh.ParsePrivateKey([]byte(params.PrivateKey))
}
if err != nil {
this.Fail("私钥验证失败,请检查格式:" + err.Error())
return
}
}
default:
this.Fail("请选择正确的认证方式")