Skip to content

Commit

Permalink
generate agent_secret for old users (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
uubulb authored Mar 8, 2025
1 parent 79884c7 commit 9ee5595
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
17 changes: 10 additions & 7 deletions cmd/dashboard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ var (
frontendDist embed.FS
)

func initSystem() {
func initSystem() error {
// 初始化管理员账户
var usersCount int64
if err := singleton.DB.Model(&model.User{}).Count(&usersCount).Error; err != nil {
panic(err)
return err
}
if usersCount == 0 {
hash, err := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
if err != nil {
panic(err)
return err
}
admin := model.User{
Username: "admin",
Password: string(hash),
}
if err := singleton.DB.Create(&admin).Error; err != nil {
panic(err)
return err
}
}

Expand All @@ -65,13 +65,14 @@ func initSystem() {

// 每天的3:30 对 监控记录 和 流量记录 进行清理
if _, err := singleton.CronShared.AddFunc("0 30 3 * * *", singleton.CleanServiceHistory); err != nil {
panic(err)
return err
}

// 每小时对流量记录进行打点
if _, err := singleton.CronShared.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil {
panic(err)
return err
}
return nil
}

// @title Nezha Monitoring API
Expand Down Expand Up @@ -111,7 +112,9 @@ func main() {
singleton.InitConfigFromPath(dashboardCliParam.ConfigFile)
singleton.InitTimezoneAndCache()
singleton.InitDBFromPath(dashboardCliParam.DatabaseLocation)
initSystem()
if err := initSystem(); err != nil {
log.Fatal(err)
}

l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort))
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
RoleMember
)

const DefaultAgentSecretLength = 32

type User struct {
Common
Username string `json:"username,omitempty" gorm:"uniqueIndex"`
Expand All @@ -32,7 +34,7 @@ func (u *User) BeforeSave(tx *gorm.DB) error {
return nil
}

key, err := utils.GenerateRandomString(32)
key, err := utils.GenerateRandomString(DefaultAgentSecretLength)
if err != nil {
return err
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"cmp"
"crypto/rand"
"errors"
"fmt"
"iter"
"maps"
"math/big"
Expand Down Expand Up @@ -82,6 +83,14 @@ func GenerateRandomString(n int) (string, error) {
return string(ret), nil
}

func MustGenerateRandomString(n int) string {
str, err := GenerateRandomString(n)
if err != nil {
panic(fmt.Errorf("MustGenerateRandomString: %v", err))
}
return str
}

func Uint64SubInt64(a uint64, b int64) uint64 {
if b < 0 {
return a + uint64(-b)
Expand Down
9 changes: 9 additions & 0 deletions service/singleton/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package singleton

import (
"fmt"
"sync"

"github.com/nezhahq/nezha/model"
"github.com/nezhahq/nezha/pkg/utils"
"gorm.io/gorm"
)

Expand All @@ -29,6 +31,13 @@ func initUser() {
AgentSecretToUserId[Conf.AgentSecretKey] = 0

for _, u := range users {
if u.AgentSecret == "" {
u.AgentSecret = utils.MustGenerateRandomString(model.DefaultAgentSecretLength)
if err := DB.Save(&u).Error; err != nil {
panic(fmt.Errorf("update of user %d failed: %v", u.ID, err))
}
}

UserInfoMap[u.ID] = model.UserInfo{
Role: u.Role,
AgentSecret: u.AgentSecret,
Expand Down

0 comments on commit 9ee5595

Please sign in to comment.