1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 16:29:17 +08:00

feat: 应用关联 redis 时自动填充密码 (#1826)

This commit is contained in:
zhengkunwang 2023-08-03 18:36:01 +08:00 committed by GitHub
parent 7c6f8ff7c4
commit 70319aca45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 14 deletions

View File

@ -15,6 +15,10 @@ type AuthParam struct {
RootPassword string `json:"PANEL_DB_ROOT_PASSWORD"` RootPassword string `json:"PANEL_DB_ROOT_PASSWORD"`
} }
type RedisAuthParam struct {
RootPassword string `json:"PANEL_REDIS_ROOT_PASSWORD"`
}
type ContainerExec struct { type ContainerExec struct {
ContainerName string `json:"containerName"` ContainerName string `json:"containerName"`
DbParam AppDatabase `json:"dbParam"` DbParam AppDatabase `json:"dbParam"`

View File

@ -83,15 +83,13 @@ func checkPort(key string, params map[string]interface{}) (int, error) {
func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall, params map[string]interface{}) error { func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall, params map[string]interface{}) error {
var dbConfig dto.AppDatabase var dbConfig dto.AppDatabase
if app.Type == "runtime" { if app.Type == "runtime" {
var authParam dto.AuthParam switch app.Key {
paramByte, err := json.Marshal(params) case "mysql", "mariadb", "postgresql":
if err != nil { if password, ok := params["PANEL_DB_ROOT_PASSWORD"]; ok {
return err if password != "" {
authParam := dto.AuthParam{
RootPassword: password.(string),
} }
if err := json.Unmarshal(paramByte, &authParam); err != nil {
return err
}
if authParam.RootPassword != "" {
authByte, err := json.Marshal(authParam) authByte, err := json.Marshal(authParam)
if err != nil { if err != nil {
return err return err
@ -99,12 +97,27 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall
appInstall.Param = string(authByte) appInstall.Param = string(authByte)
} }
} }
case "redis":
if password, ok := params["PANEL_REDIS_ROOT_PASSWORD"]; ok {
if password != "" {
authParam := dto.RedisAuthParam{
RootPassword: password.(string),
}
authByte, err := json.Marshal(authParam)
if err != nil {
return err
}
appInstall.Param = string(authByte)
}
}
}
}
if app.Type == "website" || app.Type == "tool" { if app.Type == "website" || app.Type == "tool" {
paramByte, err := json.Marshal(params) paramByte, err := json.Marshal(params)
if err != nil { if err != nil {
return err return err
} }
if err := json.Unmarshal(paramByte, &dbConfig); err != nil { if err = json.Unmarshal(paramByte, &dbConfig); err != nil {
return err return err
} }
} }

View File

@ -36,6 +36,7 @@ func Init() {
migrations.UpdateAppDetail, migrations.UpdateAppDetail,
migrations.EncryptHostPassword, migrations.EncryptHostPassword,
migrations.AddRemoteDB, migrations.AddRemoteDB,
migrations.UpdateRedisParam,
}) })
if err := m.Migrate(); err != nil { if err := m.Migrate(); err != nil {
global.LOG.Error(err) global.LOG.Error(err)

View File

@ -523,3 +523,27 @@ var AddRemoteDB = &gormigrate.Migration{
return nil return nil
}, },
} }
var UpdateRedisParam = &gormigrate.Migration{
ID: "20230804-update-redis-param",
Migrate: func(tx *gorm.DB) error {
var (
app model.App
appInstall model.AppInstall
)
if err := global.DB.Where("key = ?", "redis").First(&app).Error; err != nil {
return nil
}
if err := global.DB.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil {
return nil
}
appInstall.Param = strings.ReplaceAll(appInstall.Param, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD")
appInstall.DockerCompose = strings.ReplaceAll(appInstall.DockerCompose, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD")
appInstall.Env = strings.ReplaceAll(appInstall.Env, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD")
if err := tx.Model(&model.AppInstall{}).Where("id = ?", appInstall.ID).Updates(appInstall).Error; err != nil {
return err
}
return nil
},
}