2020-07-29 19:34:54 +08:00
|
|
|
package grants
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2020-10-25 21:27:28 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
|
2023-06-30 18:08:30 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
2020-10-25 21:27:28 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2020-07-29 19:34:54 +08:00
|
|
|
"github.com/iwind/TeaGo/actions"
|
2023-03-19 17:48:43 +08:00
|
|
|
"golang.org/x/crypto/ssh"
|
2020-07-29 19:34:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CreateAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateAction) Init() {
|
|
|
|
|
this.Nav("", "grant", "create")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateAction) RunGet(params struct{}) {
|
2023-06-28 19:07:42 +08:00
|
|
|
this.Data["methods"] = grantutils.AllGrantMethods(this.LangCode())
|
2020-07-29 19:34:54 +08:00
|
|
|
|
|
|
|
|
this.Show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateAction) RunPost(params struct {
|
|
|
|
|
Name string
|
|
|
|
|
Method string
|
|
|
|
|
Username string
|
|
|
|
|
Password string
|
|
|
|
|
PrivateKey string
|
2021-11-06 15:31:07 +08:00
|
|
|
Passphrase string
|
2020-07-29 19:34:54 +08:00
|
|
|
Description string
|
2021-12-06 19:24:30 +08:00
|
|
|
Su bool
|
2020-07-29 19:34:54 +08:00
|
|
|
|
|
|
|
|
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":
|
2021-06-30 14:55:21 +08:00
|
|
|
if len(params.Username) == 0 {
|
|
|
|
|
this.FailField("username", "请输入SSH登录用户名")
|
|
|
|
|
}
|
2020-07-29 19:34:54 +08:00
|
|
|
if len(params.PrivateKey) == 0 {
|
|
|
|
|
this.FailField("privateKey", "请输入RSA私钥")
|
|
|
|
|
}
|
2023-03-19 17:48:43 +08:00
|
|
|
|
|
|
|
|
// 验证私钥
|
|
|
|
|
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
|
|
|
|
|
}
|
2020-07-29 19:34:54 +08:00
|
|
|
default:
|
|
|
|
|
this.Fail("请选择正确的认证方式")
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 21:37:48 +08:00
|
|
|
createResp, err := this.RPC().NodeGrantRPC().CreateNodeGrant(this.AdminContext(), &pb.CreateNodeGrantRequest{
|
2020-07-29 19:34:54 +08:00
|
|
|
Name: params.Name,
|
|
|
|
|
Method: params.Method,
|
|
|
|
|
Username: params.Username,
|
|
|
|
|
Password: params.Password,
|
|
|
|
|
PrivateKey: params.PrivateKey,
|
2021-11-06 15:31:07 +08:00
|
|
|
Passphrase: params.Passphrase,
|
2020-07-29 19:34:54 +08:00
|
|
|
Description: params.Description,
|
2021-12-06 19:24:30 +08:00
|
|
|
Su: params.Su,
|
2020-07-29 19:34:54 +08:00
|
|
|
NodeId: 0,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 21:37:48 +08:00
|
|
|
// 创建日志
|
2023-06-30 18:08:30 +08:00
|
|
|
defer this.CreateLogInfo(codes.NodeGrant_LogCreateSSHGrant, createResp.NodeGrantId)
|
2020-11-10 21:37:48 +08:00
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
this.Success()
|
|
|
|
|
}
|