Files
go-admin/common/database/initialize.go
T
zhangwenjian 484de2e698 fix🔒: stop writing the database password into the log
The startup line printed the DSN whole:

  * => goadmin:<password>@tcp(host:3306)/go-admin?...

So every deployment wrote its own database credential into its own logs,
where a log shipper, a support bundle or a screenshot of a terminal
carries it onward. Found while reading deploy output, which is exactly
how it leaks.

The host and username stay - they are what makes the line worth printing
- and only the password is replaced. Both DSN shapes this project accepts
are covered, a sqlite path is left alone, and anything unparseable is
withheld rather than echoed, since it may hold a credential too.
2026-08-27 16:17:12 +08:00

71 lines
1.9 KiB
Go

package database
import (
"time"
mycasbin "github.com/go-admin-team/go-admin-core/v2/casbin"
log "github.com/go-admin-team/go-admin-core/v2/logger"
"github.com/go-admin-team/go-admin-core/v2/sdk"
toolsConfig "github.com/go-admin-team/go-admin-core/v2/sdk/config"
"github.com/go-admin-team/go-admin-core/v2/sdk/pkg"
toolsDB "github.com/go-admin-team/go-admin-core/v2/tools/database"
. "github.com/go-admin-team/go-admin-core/v2/tools/gorm/gormlog"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"go-admin/common/global"
)
// Setup 配置数据库
func Setup() {
for k := range toolsConfig.DatabasesConfig {
setupSimpleDatabase(k, toolsConfig.DatabasesConfig[k])
}
}
func setupSimpleDatabase(host string, c *toolsConfig.Database) {
if global.Driver == "" {
global.Driver = c.Driver
}
log.Infof("%s => %s", host, pkg.Green(redactDSN(c.Source)))
registers := make([]toolsDB.ResolverConfigure, len(c.Registers))
for i := range c.Registers {
registers[i] = toolsDB.NewResolverConfigure(
c.Registers[i].Sources,
c.Registers[i].Replicas,
c.Registers[i].Policy,
c.Registers[i].Tables)
}
open, err := openerFor(c.Driver)
if err != nil {
log.Fatal(pkg.Red(err.Error()))
}
resolverConfig := toolsDB.NewConfigure(c.Source, c.MaxIdleConns, c.MaxOpenConns, c.ConnMaxIdleTime, c.ConnMaxLifeTime, registers)
db, err := resolverConfig.Init(&gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
Logger: New(
logger.Config{
SlowThreshold: time.Second,
Colorful: true,
LogLevel: logger.LogLevel(
log.DefaultLogger.Options().Level.LevelForGorm()),
},
),
}, open)
if err != nil {
log.Fatal(pkg.Red(c.Driver+" connect error :"), err)
} else {
log.Info(pkg.Green(c.Driver + " connect success !"))
}
e := mycasbin.Setup(db, "")
sdk.Runtime.SetDbByTenant(host, db)
sdk.Runtime.SetCasbinByTenant(host, e)
}