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 26284a0
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 104 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
1 change: 1 addition & 0 deletions app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ PageSize = 10
HttpHost = 0.0.0.0
CertRenewalInterval = 7
RecursiveNameservers =
SkipInstallation = false

[nginx]
AccessLogPath = /var/log/nginx/access.log
Expand Down
167 changes: 81 additions & 86 deletions internal/kernal/boot.go
Original file line number Diff line number Diff line change
@@ -1,116 +1,111 @@
package kernal

import (
"github.com/0xJacky/Nginx-UI/internal/analytic"
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/0xJacky/Nginx-UI/internal/logrotate"
"github.com/0xJacky/Nginx-UI/internal/validation"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/go-co-op/gocron"
"github.com/google/uuid"
"mime"
"runtime"
"time"
"github.com/0xJacky/Nginx-UI/internal/analytic"
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/0xJacky/Nginx-UI/internal/logrotate"
"github.com/0xJacky/Nginx-UI/internal/validation"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/go-co-op/gocron"
"github.com/google/uuid"
"mime"
"runtime"
"time"
)

func Boot() {
defer recovery()

async := []func(){
InitJsExtensionType,
InitDatabase,
InitNodeSecret,
validation.Init,
}

syncs := []func(){
analytic.RecordServerAnalytic,
}

for _, v := range async {
v()
}

for _, v := range syncs {
go v()
}
defer recovery()

async := []func(){
InitJsExtensionType,
InitDatabase,
InitNodeSecret,
validation.Init,
}

syncs := []func(){
analytic.RecordServerAnalytic,
}

for _, v := range async {
v()
}

for _, v := range syncs {
go v()
}
}

func InitAfterDatabase() {
syncs := []func(){
cert.InitRegister,
InitCronJobs,
analytic.RetrieveNodesStatus,
}

for _, v := range syncs {
go v()
}
syncs := []func(){
registerPredefinedUser,
cert.InitRegister,
InitCronJobs,
analytic.RetrieveNodesStatus,
}

for _, v := range syncs {
go v()
}
}

func recovery() {
if err := recover(); err != nil {
buf := make([]byte, 1024)
runtime.Stack(buf, false)
logger.Errorf("%s\n%s", err, buf)
}
if err := recover(); err != nil {
buf := make([]byte, 1024)
runtime.Stack(buf, false)
logger.Errorf("%s\n%s", err, buf)
}
}

func InitDatabase() {
// Skip install
if settings.ServerSettings.SkipInstallation {
skipInstall()
}

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

if "" != settings.ServerSettings.JwtSecret {
db := model.Init()
query.Init(db)

InitAfterDatabase()
}
if "" != settings.ServerSettings.JwtSecret {
db := model.Init()
query.Init(db)

InitAfterDatabase()
}
}

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 {
logger.Error("Error save settings")
}
logger.Warn("Generated NodeSecret: ", settings.ServerSettings.NodeSecret)
}
if "" == settings.ServerSettings.NodeSecret {
logger.Warn("NodeSecret is empty, generating...")
settings.ServerSettings.NodeSecret = uuid.New().String()

err := settings.Save()
if err != nil {
logger.Error("Error save settings")
}
logger.Warn("Generated NodeSecret: ", settings.ServerSettings.NodeSecret)
}
}

func InitJsExtensionType() {
// Hack: fix wrong Content Type of .js file on some OS platforms
// See https://github.com/golang/go/issues/32350
_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8")
// Hack: fix wrong Content Type of .js file on some OS platforms
// See https://github.com/golang/go/issues/32350
_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8")
}

func InitCronJobs() {
s := gocron.NewScheduler(time.UTC)
job, err := s.Every(30).Minute().SingletonMode().Do(cert.AutoCert)
s := gocron.NewScheduler(time.UTC)
job, err := s.Every(30).Minute().SingletonMode().Do(cert.AutoCert)

if err != nil {
logger.Fatalf("AutoCert Job: %v, Err: %v\n", job, err)
}
if err != nil {
logger.Fatalf("AutoCert Job: %v, Err: %v\n", job, err)
}

job, err = s.Every(settings.LogrotateSettings.Interval).Minute().SingletonMode().Do(logrotate.Exec)
job, err = s.Every(settings.LogrotateSettings.Interval).Minute().SingletonMode().Do(logrotate.Exec)

if err != nil {
logger.Fatalf("LogRotate Job: %v, Err: %v\n", job, err)
}
if err != nil {
logger.Fatalf("LogRotate Job: %v, Err: %v\n", job, err)
}

s.StartAsync()
s.StartAsync()
}
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)
}
}
3 changes: 2 additions & 1 deletion resources/docker/nginx-ui.run
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/sh
#!/command/with-contenv sh
env
nginx-ui --config /etc/nginx-ui/app.ini
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 26284a0

Please sign in to comment.