fix(seed): LDAP 管理密码不再硬编码,改为从 LDAP_ADMIN_PASSWORD 环境变量读取
This commit is contained in:
1 parent
95066083bd
commit
b4521d13a1
1 file changed
+24
-17
+24
-17
@@ -1,6 +1,7 @@
|
|||||||
package seed
|
package seed
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"ezops/model"
|
"ezops/model"
|
||||||
@@ -36,27 +37,33 @@ func SeedData(db *gorm.DB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. 创建默认 LDAP 配置
|
// 2. 创建默认 LDAP 配置
|
||||||
|
// LDAP 管理密码只从环境变量 LDAP_ADMIN_PASSWORD 读取,不写入代码库
|
||||||
var ldapCount int64
|
var ldapCount int64
|
||||||
db.Model(&model.LDAPConfig{}).Count(&ldapCount)
|
db.Model(&model.LDAPConfig{}).Count(&ldapCount)
|
||||||
if ldapCount == 0 {
|
if ldapCount == 0 {
|
||||||
port := 389
|
ldapPassword := os.Getenv("LDAP_ADMIN_PASSWORD")
|
||||||
useLDAPS := false
|
if ldapPassword == "" {
|
||||||
searchFilter := "(objectClass=inetOrgPerson)"
|
zap.L().Info("LDAP_ADMIN_PASSWORD 未设置,跳过 LDAP 默认配置初始化(请在页面手动配置)")
|
||||||
userMapping := `{"username":"uid","email":"mail","display_name":"cn"}`
|
|
||||||
ldapCfg := model.LDAPConfig{
|
|
||||||
LDAPHost: "8.217.205.92",
|
|
||||||
LDAPPort: &port,
|
|
||||||
UseLDAPS: &useLDAPS,
|
|
||||||
AdminDN: "cn=admin,dc=opsx,dc=vip",
|
|
||||||
AdminPassword: "LQl2GX7EEg2N",
|
|
||||||
BaseDN: "dc=opsx,dc=vip",
|
|
||||||
SearchFilter: &searchFilter,
|
|
||||||
UserMapping: &userMapping,
|
|
||||||
}
|
|
||||||
if err := db.Create(&ldapCfg).Error; err != nil {
|
|
||||||
zap.L().Warn("failed to seed LDAP config", zap.Error(err))
|
|
||||||
} else {
|
} else {
|
||||||
zap.L().Info("seeded default LDAP config (8.217.205.92:389)")
|
port := 389
|
||||||
|
useLDAPS := false
|
||||||
|
searchFilter := "(objectClass=inetOrgPerson)"
|
||||||
|
userMapping := `{"username":"uid","email":"mail","display_name":"cn"}`
|
||||||
|
ldapCfg := model.LDAPConfig{
|
||||||
|
LDAPHost: "8.217.205.92",
|
||||||
|
LDAPPort: &port,
|
||||||
|
UseLDAPS: &useLDAPS,
|
||||||
|
AdminDN: "cn=admin,dc=opsx,dc=vip",
|
||||||
|
AdminPassword: ldapPassword,
|
||||||
|
BaseDN: "dc=opsx,dc=vip",
|
||||||
|
SearchFilter: &searchFilter,
|
||||||
|
UserMapping: &userMapping,
|
||||||
|
}
|
||||||
|
if err := db.Create(&ldapCfg).Error; err != nil {
|
||||||
|
zap.L().Warn("failed to seed LDAP config", zap.Error(err))
|
||||||
|
} else {
|
||||||
|
zap.L().Info("seeded default LDAP config (8.217.205.92:389)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user