Skip to content

Commit

Permalink
feat: use env to predefine admin user #214
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed May 6, 2024
1 parent 13c4eb0 commit a8420d8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 17 deletions.
2 changes: 1 addition & 1 deletion api/system/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func InstallNginxUI(c *gin.Context) {
if "" != json.Database {
settings.ServerSettings.Database = json.Database
}
settings.ReflectFrom()

err := settings.Save()
if err != nil {
Expand All @@ -72,6 +71,7 @@ func InstallNginxUI(c *gin.Context) {
api.ErrHandler(c, err)
return
}

c.JSON(http.StatusOK, gin.H{
"message": "ok",
})
Expand Down
2 changes: 0 additions & 2 deletions api/system/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func SaveSettings(c *gin.Context) {
fillSettings(&settings.OpenAISettings, &json.Openai)
fillSettings(&settings.LogrotateSettings, &json.Logrotate)

settings.ReflectFrom()

err := settings.Save()
if err != nil {
api.ErrHandler(c, err)
Expand Down
10 changes: 3 additions & 7 deletions internal/kernal/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func Boot() {

func InitAfterDatabase() {
syncs := []func(){
registerPredefinedUser,
cert.InitRegister,
InitCronJobs,
analytic.RetrieveNodesStatus,
Expand All @@ -61,13 +62,9 @@ func recovery() {

func InitDatabase() {

// Skip installation
// Skip install
if settings.ServerSettings.SkipInstallation && settings.ServerSettings.JwtSecret == "" {
settings.ServerSettings.JwtSecret = uuid.New().String()
err := settings.Save()
if err != nil {
logger.Error(err)
}
skipInstall()
}

if "" != settings.ServerSettings.JwtSecret {
Expand All @@ -82,7 +79,6 @@ func InitNodeSecret() {
if "" == settings.ServerSettings.NodeSecret {
logger.Warn("NodeSecret is empty, generating...")
settings.ServerSettings.NodeSecret = uuid.New().String()
settings.ReflectFrom()

err := settings.Save()
if err != nil {
Expand Down
72 changes: 72 additions & 0 deletions internal/kernal/skip_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package kernal

import (
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/caarlos0/env/v11"
"github.com/google/uuid"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)

type predefinedUser struct {
Name string `json:"name"`
Password string `json:"password"`
}

func skipInstall() {
logger.Info("Skip installation mode enabled")

settings.ServerSettings.JwtSecret = uuid.New().String()

if settings.ServerSettings.NodeSecret == "" {
settings.ServerSettings.NodeSecret = uuid.New().String()
logger.Infof("NodeSecret: %s", settings.ServerSettings.NodeSecret)
}

err := settings.Save()
if err != nil {
logger.Fatal(err)
}
}

func registerPredefinedUser() {
// when skip installation mode is enabled, the predefined user will be created
if !settings.ServerSettings.SkipInstallation {
return
}
pUser := &predefinedUser{}

err := env.ParseWithOptions(pUser, env.Options{
Prefix: "NGINX_UI_PREDEFINED_USER_",
UseFieldNameByDefault: true,
})

if err != nil {
logger.Fatal(err)
}

u := query.Auth

_, err = u.First()

// Only effect when there is no user in the database
if !errors.Is(err, gorm.ErrRecordNotFound) || pUser.Name == "" || pUser.Password == "" {
return
}

// Create a new user with the predefined name and password
pwd, _ := bcrypt.GenerateFromPassword([]byte(pUser.Password), bcrypt.DefaultCost)

err = u.Create(&model.Auth{
Name: pUser.Name,
Password: string(pwd),
})

if err != nil {
logger.Error(err)
}
}
11 changes: 5 additions & 6 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func Setup() {
parseEnv(&OpenAISettings, "OPENAI_")
parseEnv(&CasdoorSettings, "CASDOOR_")
parseEnv(&LogrotateSettings, "LOGROTATE_")
parseEnv(&PredefinedUserSettings, "PREDEFINED_USER_")

// if in official docker, set the restart cmd of nginx to "nginx -s stop",
// then the supervisor of s6-overlay will start the nginx again.
Expand All @@ -69,12 +70,6 @@ func MapTo() {
}
}

func ReflectFrom() {
for k, v := range sections {
reflectFrom(k, v)
}
}

func mapTo(section string, v interface{}) {
err := Conf.Section(section).MapTo(v)
if err != nil {
Expand All @@ -90,6 +85,10 @@ func reflectFrom(section string, v interface{}) {
}

func Save() (err error) {
for k, v := range sections {
reflectFrom(k, v)
}

err = Conf.SaveTo(ConfPath)
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion settings/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package settings

type PredefinedUser struct {
User string `json:"user"`
Name string `json:"name"`
Password string `json:"password"`
}

Expand Down

0 comments on commit a8420d8

Please # to comment.