1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-14 01:34:47 +08:00

fix: 主机密码回显解密 (#1540)

This commit is contained in:
ssongliu 2023-07-05 14:02:12 +08:00 committed by GitHub
parent 319afd2d51
commit b61d8aaabc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 3 deletions

View File

@ -42,6 +42,8 @@ func (b *BaseApi) CreateHost(c *gin.Context) {
return
}
req.Password = passwordItem
req.PrivateKey = ""
req.PassPhrase = ""
}
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
@ -55,6 +57,16 @@ func (b *BaseApi) CreateHost(c *gin.Context) {
return
}
req.Password = keyItem
if len(req.PassPhrase) != 0 {
pass, err := encrypt.StringEncrypt(req.PassPhrase)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.PassPhrase = pass
}
req.Password = ""
}
host, err := hostService.Create(req)
@ -229,6 +241,15 @@ func (b *BaseApi) UpdateHost(c *gin.Context) {
return
}
req.PrivateKey = keyItem
if len(req.PassPhrase) != 0 {
pass, err := encrypt.StringEncrypt(req.PassPhrase)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.PassPhrase = pass
}
}
upMap := make(map[string]interface{})
@ -239,10 +260,12 @@ func (b *BaseApi) UpdateHost(c *gin.Context) {
upMap["user"] = req.User
upMap["auth_mode"] = req.AuthMode
upMap["remember_password"] = req.RememberPassword
if len(req.Password) != 0 {
if req.AuthMode == "password" {
upMap["password"] = req.Password
}
if len(req.PrivateKey) != 0 {
upMap["private_key"] = ""
upMap["pass_phrase"] = ""
} else {
upMap["password"] = ""
upMap["private_key"] = req.PrivateKey
upMap["pass_phrase"] = req.PassPhrase
}

View File

@ -105,6 +105,10 @@ func (u *HostService) TestLocalConn(id uint) bool {
connInfo.PrivateKey = []byte(host.PrivateKey)
}
if len(host.PassPhrase) != 0 {
host.PassPhrase, err = encrypt.StringDecrypt(host.PassPhrase)
if err != nil {
return false
}
connInfo.PassPhrase = []byte(host.PassPhrase)
}
client, err := connInfo.NewClient()
@ -133,6 +137,13 @@ func (u *HostService) GetHostInfo(id uint) (*model.Host, error) {
return nil, err
}
}
if len(host.PassPhrase) != 0 {
host.PassPhrase, err = encrypt.StringDecrypt(host.PassPhrase)
if err != nil {
return nil, err
}
}
return &host, err
}
@ -153,6 +164,25 @@ func (u *HostService) SearchWithPage(search dto.SearchHostWithPage) (int64, inte
item.Password = ""
item.PrivateKey = ""
item.PassPhrase = ""
} else {
if len(host.Password) != 0 {
item.Password, err = encrypt.StringDecrypt(host.Password)
if err != nil {
return 0, nil, err
}
}
if len(host.PrivateKey) != 0 {
item.PrivateKey, err = encrypt.StringDecrypt(host.PrivateKey)
if err != nil {
return 0, nil, err
}
}
if len(host.PassPhrase) != 0 {
item.PassPhrase, err = encrypt.StringDecrypt(host.PassPhrase)
if err != nil {
return 0, nil, err
}
}
}
dtoHosts = append(dtoHosts, item)
}

View File

@ -460,6 +460,15 @@ var EncryptHostPassword = &gormigrate.Migration{
return err
}
}
if len(host.PassPhrase) != 0 {
pass, err := encrypt.StringEncrypt(host.PassPhrase)
if err != nil {
return err
}
if err := tx.Model(&model.Host{}).Update("pass_phrase", pass).Error; err != nil {
return err
}
}
}
return nil
},