Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions backend/app/api/v1/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/copier"
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -36,15 +36,25 @@ func (b *BaseApi) CreateHost(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.Password = string(password)
passwordItem, err := encrypt.StringEncrypt(string(password))
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.Password = passwordItem
}
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.PrivateKey = string(privateKey)
keyItem, err := encrypt.StringEncrypt(string(privateKey))
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.Password = keyItem
}

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

// @Tags Host
// @Summary Load host info
// @Description 加载主机信息
// @Accept json
// @Param id path integer true "request"
// @Success 200 {object} dto.HostInfo
// @Security ApiKeyAuth
// @Router /hosts/:id [get]
func (b *BaseApi) GetHostInfo(c *gin.Context) {
id, err := helper.GetParamID(c)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
host, err := hostService.GetHostInfo(id)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
var hostDto dto.HostInfo
if err := copier.Copy(&hostDto, host); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, hostDto)
}

// @Tags Host
// @Summary Delete host
// @Description 删除主机
Expand Down Expand Up @@ -227,15 +210,25 @@ func (b *BaseApi) UpdateHost(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.Password = string(password)
passwordItem, err := encrypt.StringEncrypt(string(password))
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.Password = passwordItem
}
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.PrivateKey = string(privateKey)
keyItem, err := encrypt.StringEncrypt(string(privateKey))
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
req.PrivateKey = keyItem
}

upMap := make(map[string]interface{})
Expand Down
28 changes: 27 additions & 1 deletion backend/app/service/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
"github.com/1Panel-dev/1Panel/backend/utils/ssh"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
Expand Down Expand Up @@ -89,7 +90,20 @@ func (u *HostService) TestLocalConn(id uint) bool {
if err := copier.Copy(&connInfo, &host); err != nil {
return false
}
connInfo.PrivateKey = []byte(host.PrivateKey)
if len(host.Password) != 0 {
host.Password, err = encrypt.StringDecrypt(host.Password)
if err != nil {
return false
}
connInfo.Password = host.Password
}
if len(host.PrivateKey) != 0 {
host.PrivateKey, err = encrypt.StringDecrypt(host.PrivateKey)
if err != nil {
return false
}
connInfo.PrivateKey = []byte(host.PrivateKey)
}
if len(host.PassPhrase) != 0 {
connInfo.PassPhrase = []byte(host.PassPhrase)
}
Expand All @@ -107,6 +121,18 @@ func (u *HostService) GetHostInfo(id uint) (*model.Host, error) {
if err != nil {
return nil, constant.ErrRecordNotFound
}
if len(host.Password) != 0 {
host.Password, err = encrypt.StringDecrypt(host.Password)
if err != nil {
return nil, err
}
}
if len(host.PrivateKey) != 0 {
host.PrivateKey, err = encrypt.StringDecrypt(host.PrivateKey)
if err != nil {
return nil, err
}
}
return &host, err
}

Expand Down
1 change: 1 addition & 0 deletions backend/init/migration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Init() {
migrations.AddBackupAccountDir,
migrations.AddMfaInterval,
migrations.UpdateAppDetail,
migrations.EncryptHostPassword,
})
if err := m.Migrate(); err != nil {
global.LOG.Error(err)
Expand Down
32 changes: 32 additions & 0 deletions backend/init/migration/migrations/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,35 @@ var UpdateAppDetail = &gormigrate.Migration{
return nil
},
}

var EncryptHostPassword = &gormigrate.Migration{
ID: "20230703-encrypt-host-password",
Migrate: func(tx *gorm.DB) error {
var hosts []model.Host
if err := tx.Find(&hosts).Error; err != nil {
return err
}

for _, host := range hosts {
if len(host.Password) != 0 {
pass, err := encrypt.StringEncrypt(host.Password)
if err != nil {
return err
}
if err := tx.Model(&model.Host{}).Where("id = ?", host.ID).Update("password", pass).Error; err != nil {
return err
}
}
if len(host.PrivateKey) != 0 {
key, err := encrypt.StringEncrypt(host.PrivateKey)
if err != nil {
return err
}
if err := tx.Model(&model.Host{}).Update("private_key", key).Error; err != nil {
return err
}
}
}
return nil
},
}
1 change: 0 additions & 1 deletion backend/router/ro_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func (s *HostRouter) InitHostRouter(Router *gin.RouterGroup) {
hostRouter.POST("/tree", baseApi.HostTree)
hostRouter.POST("/test/byinfo", baseApi.TestByInfo)
hostRouter.POST("/test/byid/:id", baseApi.TestByID)
hostRouter.GET(":id", baseApi.GetHostInfo)

hostRouter.GET("/firewall/base", baseApi.LoadFirewallBaseInfo)
hostRouter.POST("/firewall/search", baseApi.SearchFirewallRule)
Expand Down
2 changes: 1 addition & 1 deletion backend/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Start() {
log.Init()
app.Init()
db.Init()
hook.Init()
migration.Init()
validator.Init()
gob.Register(psession.SessionUser{})
Expand All @@ -42,7 +43,6 @@ func Start() {
gin.SetMode("debug")
cron.Run()
business.Init()
hook.Init()

rootRouter := router.Routers()
address := fmt.Sprintf(":%s", global.CONF.System.Port)
Expand Down
7 changes: 7 additions & 0 deletions backend/utils/encrypt/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"

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

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

func StringDecrypt(text string) (string, error) {
if len(text) == 0 {
return "", errors.New("it is not possible to decrypt an empty string.")
}
key := global.CONF.System.EncryptKey
bytesPass, err := base64.StdEncoding.DecodeString(text)
if err != nil {
Expand Down
Loading