Skip to content

Commit e55b265

Browse files
committed
feat: 主机密码加密存储
1 parent 695d4b4 commit e55b265

File tree

11 files changed

+643
-811
lines changed

11 files changed

+643
-811
lines changed

backend/app/api/v1/host.go

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/1Panel-dev/1Panel/backend/app/dto"
88
"github.com/1Panel-dev/1Panel/backend/constant"
99
"github.com/1Panel-dev/1Panel/backend/global"
10-
"github.com/1Panel-dev/1Panel/backend/utils/copier"
10+
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
1111
"github.com/gin-gonic/gin"
1212
)
1313

@@ -36,15 +36,25 @@ func (b *BaseApi) CreateHost(c *gin.Context) {
3636
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
3737
return
3838
}
39-
req.Password = string(password)
39+
passwordItem, err := encrypt.StringEncrypt(string(password))
40+
if err != nil {
41+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
42+
return
43+
}
44+
req.Password = passwordItem
4045
}
4146
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
4247
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
4348
if err != nil {
4449
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
4550
return
4651
}
47-
req.PrivateKey = string(privateKey)
52+
keyItem, err := encrypt.StringEncrypt(string(privateKey))
53+
if err != nil {
54+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
55+
return
56+
}
57+
req.Password = keyItem
4858
}
4959

5060
host, err := hostService.Create(req)
@@ -148,33 +158,6 @@ func (b *BaseApi) SearchHost(c *gin.Context) {
148158
})
149159
}
150160

151-
// @Tags Host
152-
// @Summary Load host info
153-
// @Description 加载主机信息
154-
// @Accept json
155-
// @Param id path integer true "request"
156-
// @Success 200 {object} dto.HostInfo
157-
// @Security ApiKeyAuth
158-
// @Router /hosts/:id [get]
159-
func (b *BaseApi) GetHostInfo(c *gin.Context) {
160-
id, err := helper.GetParamID(c)
161-
if err != nil {
162-
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
163-
return
164-
}
165-
host, err := hostService.GetHostInfo(id)
166-
if err != nil {
167-
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
168-
return
169-
}
170-
var hostDto dto.HostInfo
171-
if err := copier.Copy(&hostDto, host); err != nil {
172-
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
173-
return
174-
}
175-
helper.SuccessWithData(c, hostDto)
176-
}
177-
178161
// @Tags Host
179162
// @Summary Delete host
180163
// @Description 删除主机
@@ -227,15 +210,25 @@ func (b *BaseApi) UpdateHost(c *gin.Context) {
227210
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
228211
return
229212
}
230-
req.Password = string(password)
213+
passwordItem, err := encrypt.StringEncrypt(string(password))
214+
if err != nil {
215+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
216+
return
217+
}
218+
req.Password = passwordItem
231219
}
232220
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
233221
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
234222
if err != nil {
235223
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
236224
return
237225
}
238-
req.PrivateKey = string(privateKey)
226+
keyItem, err := encrypt.StringEncrypt(string(privateKey))
227+
if err != nil {
228+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
229+
return
230+
}
231+
req.PrivateKey = keyItem
239232
}
240233

241234
upMap := make(map[string]interface{})

backend/app/service/host.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/1Panel-dev/1Panel/backend/app/dto"
88
"github.com/1Panel-dev/1Panel/backend/app/model"
99
"github.com/1Panel-dev/1Panel/backend/constant"
10+
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
1011
"github.com/1Panel-dev/1Panel/backend/utils/ssh"
1112
"github.com/jinzhu/copier"
1213
"github.com/pkg/errors"
@@ -89,7 +90,20 @@ func (u *HostService) TestLocalConn(id uint) bool {
8990
if err := copier.Copy(&connInfo, &host); err != nil {
9091
return false
9192
}
92-
connInfo.PrivateKey = []byte(host.PrivateKey)
93+
if len(host.Password) != 0 {
94+
host.Password, err = encrypt.StringDecrypt(host.Password)
95+
if err != nil {
96+
return false
97+
}
98+
connInfo.Password = host.Password
99+
}
100+
if len(host.PrivateKey) != 0 {
101+
host.PrivateKey, err = encrypt.StringDecrypt(host.PrivateKey)
102+
if err != nil {
103+
return false
104+
}
105+
connInfo.PrivateKey = []byte(host.PrivateKey)
106+
}
93107
if len(host.PassPhrase) != 0 {
94108
connInfo.PassPhrase = []byte(host.PassPhrase)
95109
}
@@ -107,6 +121,18 @@ func (u *HostService) GetHostInfo(id uint) (*model.Host, error) {
107121
if err != nil {
108122
return nil, constant.ErrRecordNotFound
109123
}
124+
if len(host.Password) != 0 {
125+
host.Password, err = encrypt.StringDecrypt(host.Password)
126+
if err != nil {
127+
return nil, err
128+
}
129+
}
130+
if len(host.PrivateKey) != 0 {
131+
host.PrivateKey, err = encrypt.StringDecrypt(host.PrivateKey)
132+
if err != nil {
133+
return nil, err
134+
}
135+
}
110136
return &host, err
111137
}
112138

backend/init/migration/migrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func Init() {
3434
migrations.AddBackupAccountDir,
3535
migrations.AddMfaInterval,
3636
migrations.UpdateAppDetail,
37+
migrations.EncryptHostPassword,
3738
})
3839
if err := m.Migrate(); err != nil {
3940
global.LOG.Error(err)

backend/init/migration/migrations/init.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,35 @@ var UpdateAppDetail = &gormigrate.Migration{
426426
return nil
427427
},
428428
}
429+
430+
var EncryptHostPassword = &gormigrate.Migration{
431+
ID: "20230703-encrypt-host-password",
432+
Migrate: func(tx *gorm.DB) error {
433+
var hosts []model.Host
434+
if err := tx.Find(&hosts).Error; err != nil {
435+
return err
436+
}
437+
438+
for _, host := range hosts {
439+
if len(host.Password) != 0 {
440+
pass, err := encrypt.StringEncrypt(host.Password)
441+
if err != nil {
442+
return err
443+
}
444+
if err := tx.Model(&model.Host{}).Where("id = ?", host.ID).Update("password", pass).Error; err != nil {
445+
return err
446+
}
447+
}
448+
if len(host.PrivateKey) != 0 {
449+
key, err := encrypt.StringEncrypt(host.PrivateKey)
450+
if err != nil {
451+
return err
452+
}
453+
if err := tx.Model(&model.Host{}).Update("private_key", key).Error; err != nil {
454+
return err
455+
}
456+
}
457+
}
458+
return nil
459+
},
460+
}

backend/router/ro_host.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func (s *HostRouter) InitHostRouter(Router *gin.RouterGroup) {
2424
hostRouter.POST("/tree", baseApi.HostTree)
2525
hostRouter.POST("/test/byinfo", baseApi.TestByInfo)
2626
hostRouter.POST("/test/byid/:id", baseApi.TestByID)
27-
hostRouter.GET(":id", baseApi.GetHostInfo)
2827

2928
hostRouter.GET("/firewall/base", baseApi.LoadFirewallBaseInfo)
3029
hostRouter.POST("/firewall/search", baseApi.SearchFirewallRule)

backend/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func Start() {
3434
log.Init()
3535
app.Init()
3636
db.Init()
37+
hook.Init()
3738
migration.Init()
3839
validator.Init()
3940
gob.Register(psession.SessionUser{})
@@ -42,7 +43,6 @@ func Start() {
4243
gin.SetMode("debug")
4344
cron.Run()
4445
business.Init()
45-
hook.Init()
4646

4747
rootRouter := router.Routers()
4848
address := fmt.Sprintf(":%s", global.CONF.System.Port)

backend/utils/encrypt/encrypt.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ import (
88
"crypto/rand"
99
"encoding/base64"
1010
"encoding/hex"
11+
"errors"
1112
"fmt"
1213
"io"
1314

1415
"github.com/1Panel-dev/1Panel/backend/global"
1516
)
1617

1718
func StringEncrypt(text string) (string, error) {
19+
if len(text) == 0 {
20+
return "", errors.New("it is not possible to encrypt an empty string.")
21+
}
1822
key := global.CONF.System.EncryptKey
1923
pass := []byte(text)
2024
xpass, err := aesEncryptWithSalt([]byte(key), pass)
@@ -26,6 +30,9 @@ func StringEncrypt(text string) (string, error) {
2630
}
2731

2832
func StringDecrypt(text string) (string, error) {
33+
if len(text) == 0 {
34+
return "", errors.New("it is not possible to decrypt an empty string.")
35+
}
2936
key := global.CONF.System.EncryptKey
3037
bytesPass, err := base64.StdEncoding.DecodeString(text)
3138
if err != nil {

0 commit comments

Comments
 (0)