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 42dc575
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 25 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
14 changes: 6 additions & 8 deletions internal/kernal/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-co-op/gocron"
"github.com/google/uuid"
"mime"
"os"
"runtime"
"time"
)
Expand Down Expand Up @@ -41,6 +42,7 @@ func Boot() {

func InitAfterDatabase() {
syncs := []func(){
registerPredefinedUser,
cert.InitRegister,
InitCronJobs,
analytic.RetrieveNodesStatus,
Expand All @@ -60,14 +62,11 @@ func recovery() {
}

func InitDatabase() {
logger.Debug("skip installation", os.Getenv("NGINX_UI_SKIP_INSTALLATION"))

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

if "" != settings.ServerSettings.JwtSecret {
Expand All @@ -82,7 +81,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
74 changes: 74 additions & 0 deletions internal/kernal/skip_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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")

if settings.ServerSettings.JwtSecret == "" {
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)
}
}
10 changes: 4 additions & 6 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,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 +84,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: 2 additions & 0 deletions settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestSetup(t *testing.T) {
_ = os.Setenv("NGINX_UI_LOGROTATE_CMD", "logrotate /custom/logrotate.conf")
_ = os.Setenv("NGINX_UI_LOGROTATE_INTERVAL", "60")

ConfPath = "app.testing.ini"
Setup()

assert.Equal(t, "8080", ServerSettings.HttpPort)
Expand Down Expand Up @@ -96,4 +97,5 @@ func TestSetup(t *testing.T) {
assert.Equal(t, 60, LogrotateSettings.Interval)

os.Clearenv()
_ = os.Remove("app.testing.ini")
}
8 changes: 0 additions & 8 deletions settings/user.go

This file was deleted.

0 comments on commit 42dc575

Please sign in to comment.