Skip to content

Commit

Permalink
feat: add initializeAdminUsers
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeguo committed Jan 23, 2024
1 parent bbe1fdc commit 95b0f19
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
# Go workspace file
go.work

# Binary and Config
# Binary
/bunker
/bunker.yaml

# Database
# Data
/bunker.yaml
/database.sqlite3

# SSH Keys
/admin.yaml
/ssh_host_*_key
/ssh_client_*_key
57 changes: 56 additions & 1 deletion database.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,76 @@
package main

import (
"bytes"
"errors"
"os"
"path/filepath"
"time"

"github.com/glebarez/sqlite"
"github.com/yankeguo/bunker/model"
"github.com/yankeguo/bunker/model/dao"
"gopkg.in/yaml.v3"
"gorm.io/gorm"
)

func initializeAdminUsers(dataDir DataDir, _db *gorm.DB) (err error) {
buf, _ := os.ReadFile(filepath.Join(dataDir.String(), "admin.yaml"))
buf = bytes.TrimSpace(buf)
if len(buf) == 0 {
return
}

var m map[string]string
if err = yaml.Unmarshal(buf, &m); err != nil {
return
}

db := dao.Use(_db)

for username, password := range m {
var user *model.User
if user, err = db.User.Where(db.User.ID.Eq(username)).First(); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
user = &model.User{
ID: username,
CreatedAt: time.Now(),
VisitedAt: time.Now(),
IsAdmin: true,
}
user.SetPassword(password)

if err = db.User.Create(user); err != nil {
return
}
} else {
return
}
} else {
user.SetPassword(password)

if _, err = db.User.Where(
db.User.ID.Eq(username),
).UpdateSimple(
db.User.PasswordDigest.Value(user.PasswordDigest),
db.User.IsAdmin.Value(true),
); err != nil {
return
}
}
}

return
}

func createDatabase(dataDir DataDir) (db *gorm.DB, err error) {
if db, err = gorm.Open(
sqlite.Open(filepath.Join(dataDir.String(), "database.sqlite3")),
&gorm.Config{},
); err != nil {
return
}
if err = db.Debug().AutoMigrate(model.All...); err != nil {
if err = db.AutoMigrate(model.All...); err != nil {
return
}
return
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/yankeguo/ufx v0.2.2
go.uber.org/fx v1.20.1
golang.org/x/crypto v0.18.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/gen v0.3.25
gorm.io/gorm v1.25.5
gorm.io/plugin/dbresolver v1.5.0
Expand Down Expand Up @@ -56,7 +57,6 @@ require (
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/datatypes v1.2.0 // indirect
gorm.io/driver/mysql v1.5.2 // indirect
gorm.io/hints v1.1.2 // indirect
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func main() {
ufx.Module,
fx.Invoke(installStatic, installAPIAuthorizedKeys),
fx.Invoke(func(s *SSHServer) {}),
fx.Invoke(initializeAdminUsers),
)
if app.Err() != nil {
log.Println(app.Err().Error())
Expand Down

0 comments on commit 95b0f19

Please sign in to comment.