working commit

This commit is contained in:
2026-06-05 18:35:25 +02:00
parent 16c52424ed
commit 557aea269d
13 changed files with 82 additions and 73 deletions
+9
View File
@@ -0,0 +1,9 @@
package config
const (
confdirPath = "/home/ziggi/Projects/grpcbase/etc/mbase"
rundirPath = "/home/ziggi/Projects/grpcbase/tmp/run"
logdirPath = "/home/ziggi/Projects/grpcbase/tmp/log"
datadirPath = "/home/ziggi/Projects/grpcbase/tmp/data"
packageVersion = "0.0.1"
)
@@ -1,4 +1,4 @@
package descriptor package descr
const ( const (
GrantModifyUsers = "modifyUsers" GrantModifyUsers = "modifyUsers"
+8 -8
View File
@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"time" "time"
"mbase/app/descriptor" "mbase/app/descr"
"mbase/pkg/auxid" "mbase/pkg/auxid"
"mbase/pkg/auxpwd" "mbase/pkg/auxpwd"
"mbase/pkg/mbctl" "mbase/pkg/mbctl"
@@ -39,7 +39,7 @@ func (lg *Logic) CreateAccount(ctx context.Context, accountID int64, params *mbc
lg.WaitDumping() lg.WaitDumping()
lg.WaitRestoring() lg.WaitRestoring()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyUsers) grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -67,7 +67,7 @@ func (lg *Logic) CreateAccount(ctx context.Context, accountID int64, params *mbc
} }
now := time.Now().Format(time.RFC3339) now := time.Now().Format(time.RFC3339)
passhash := auxpwd.MakeSHA256Hash([]byte(params.Password)) passhash := auxpwd.MakeSHA256Hash([]byte(params.Password))
accountDescr := &descriptor.Account{ accountDescr := &descr.Account{
ID: auxid.GenID(), ID: auxid.GenID(),
Username: params.Username, Username: params.Username,
Passhash: passhash, Passhash: passhash,
@@ -90,7 +90,7 @@ func (lg *Logic) UpdateAccount(ctx context.Context, accountID int64, params *mbc
lg.WaitRestoring() lg.WaitRestoring()
lg.WaitDumping() lg.WaitDumping()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyUsers) grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -99,7 +99,7 @@ func (lg *Logic) UpdateAccount(ctx context.Context, accountID int64, params *mbc
return res, err return res, err
} }
var accountDescr *descriptor.Account var accountDescr *descr.Account
var accountExists bool var accountExists bool
switch { switch {
case params.AccountID != 0: case params.AccountID != 0:
@@ -147,7 +147,7 @@ func (lg *Logic) DeleteAccount(ctx context.Context, accountID int64, params *mbc
lg.WaitDumping() lg.WaitDumping()
lg.WaitRestoring() lg.WaitRestoring()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyUsers) grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -156,7 +156,7 @@ func (lg *Logic) DeleteAccount(ctx context.Context, accountID int64, params *mbc
return res, err return res, err
} }
var accountDescr *descriptor.Account var accountDescr *descr.Account
var accountExists bool var accountExists bool
switch { switch {
case params.AccountID != 0: case params.AccountID != 0:
@@ -194,7 +194,7 @@ func (lg *Logic) ListAccounts(ctx context.Context, accountID int64, params *mbct
lg.WaitRestoring() lg.WaitRestoring()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyUsers) grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil { if err != nil {
return res, err return res, err
} }
+5 -5
View File
@@ -4,7 +4,7 @@ import (
"context" "context"
"time" "time"
"mbase/app/descriptor" "mbase/app/descr"
"mbase/pkg/auxid" "mbase/pkg/auxid"
"mbase/pkg/auxpwd" "mbase/pkg/auxpwd"
) )
@@ -34,7 +34,7 @@ func (lg *Logic) SeedAccount(ctx context.Context) (int64, error) {
if len(accountDescrs) == 0 { if len(accountDescrs) == 0 {
now := time.Now().Format(time.RFC3339) now := time.Now().Format(time.RFC3339)
passhash := auxpwd.MakeSHA256Hash([]byte(defaultSeedPassword)) passhash := auxpwd.MakeSHA256Hash([]byte(defaultSeedPassword))
accountDescr := &descriptor.Account{ accountDescr := &descr.Account{
ID: auxid.GenID(), ID: auxid.GenID(),
Username: defaultSeedUsername, Username: defaultSeedUsername,
Passhash: passhash, Passhash: passhash,
@@ -48,11 +48,11 @@ func (lg *Logic) SeedAccount(ctx context.Context) (int64, error) {
} }
accountID = accountDescr.ID accountID = accountDescr.ID
grantTypes := []string{ grantTypes := []string{
descriptor.GrantModifyUsers, descr.GrantModifyUsers,
descriptor.GrantModifyDatabase, descr.GrantModifyDatabase,
} }
for _, grantType := range grantTypes { for _, grantType := range grantTypes {
grantDescr := &descriptor.Grant{ grantDescr := &descr.Grant{
AccountID: accountDescr.ID, AccountID: accountDescr.ID,
Operation: grantType, Operation: grantType,
CreatedAt: now, CreatedAt: now,
+5 -5
View File
@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"time" "time"
"mbase/app/descriptor" "mbase/app/descr"
"mbase/pkg/mbctl" "mbase/pkg/mbctl"
"go.yaml.in/yaml/v4" "go.yaml.in/yaml/v4"
@@ -15,7 +15,7 @@ func (lg *Logic) GetDump(ctx context.Context, accountID int64, params *mbctl.Get
var err error var err error
res := &mbctl.GetDumpResult{} res := &mbctl.GetDumpResult{}
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyDatabase) grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyDatabase)
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -40,7 +40,7 @@ func (lg *Logic) GetDump(ctx context.Context, accountID int64, params *mbctl.Get
} }
lg.DumpingSemDown() lg.DumpingSemDown()
dump := descriptor.Dump{ dump := descr.Dump{
Timestamp: time.Now().Format(time.RFC3339), Timestamp: time.Now().Format(time.RFC3339),
Accounts: listAccounts, Accounts: listAccounts,
Grants: listGrants, Grants: listGrants,
@@ -59,7 +59,7 @@ func (lg *Logic) RestoreDump(ctx context.Context, accountID int64, params *mbctl
var err error var err error
res := &mbctl.RestoreDumpResult{} res := &mbctl.RestoreDumpResult{}
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyDatabase) grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyDatabase)
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -71,7 +71,7 @@ func (lg *Logic) RestoreDump(ctx context.Context, accountID int64, params *mbctl
lg.WaitDumping() lg.WaitDumping()
lg.WaitRestoring() lg.WaitRestoring()
var dump descriptor.Dump var dump descr.Dump
err = yaml.Unmarshal([]byte(params.Dump), &dump) err = yaml.Unmarshal([]byte(params.Dump), &dump)
if err != nil { if err != nil {
+10 -10
View File
@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"time" "time"
"mbase/app/descriptor" "mbase/app/descr"
"mbase/pkg/auxid" "mbase/pkg/auxid"
"mbase/pkg/mbctl" "mbase/pkg/mbctl"
) )
@@ -16,7 +16,7 @@ func (lg *Logic) SetGrant(ctx context.Context, accountID int64, params *mbctl.Se
lg.WaitDumping() lg.WaitDumping()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyUsers) grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -26,8 +26,8 @@ func (lg *Logic) SetGrant(ctx context.Context, accountID int64, params *mbctl.Se
} }
grantTypes := []string{ grantTypes := []string{
descriptor.GrantModifyUsers, descr.GrantModifyUsers,
descriptor.GrantModifyDatabase, descr.GrantModifyDatabase,
} }
var grantOk bool var grantOk bool
for _, grantType := range grantTypes { for _, grantType := range grantTypes {
@@ -41,7 +41,7 @@ func (lg *Logic) SetGrant(ctx context.Context, accountID int64, params *mbctl.Se
return res, err return res, err
} }
var accountDescr *descriptor.Account var accountDescr *descr.Account
var accountExists bool var accountExists bool
switch { switch {
case params.AccountID != 0: case params.AccountID != 0:
@@ -69,7 +69,7 @@ func (lg *Logic) SetGrant(ctx context.Context, accountID int64, params *mbctl.Se
return res, err return res, err
} }
now := time.Now().Format(time.RFC3339) now := time.Now().Format(time.RFC3339)
grantDescr := &descriptor.Grant{ grantDescr := &descr.Grant{
ID: auxid.GenID(), ID: auxid.GenID(),
AccountID: accountDescr.ID, AccountID: accountDescr.ID,
CreatedAt: now, CreatedAt: now,
@@ -88,7 +88,7 @@ func (lg *Logic) DeleteGrant(ctx context.Context, accountID int64, params *mbctl
lg.WaitDumping() lg.WaitDumping()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyUsers) grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -98,8 +98,8 @@ func (lg *Logic) DeleteGrant(ctx context.Context, accountID int64, params *mbctl
} }
grantTypes := []string{ grantTypes := []string{
descriptor.GrantModifyUsers, descr.GrantModifyUsers,
descriptor.GrantModifyDatabase, descr.GrantModifyDatabase,
} }
var grantOk bool var grantOk bool
for _, grantType := range grantTypes { for _, grantType := range grantTypes {
@@ -113,7 +113,7 @@ func (lg *Logic) DeleteGrant(ctx context.Context, accountID int64, params *mbctl
return res, err return res, err
} }
var accountDescr *descriptor.Account var accountDescr *descr.Account
var accountExists bool var accountExists bool
switch { switch {
case params.AccountID != 0: case params.AccountID != 0:
+3 -3
View File
@@ -4,17 +4,17 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"mbase/app/database" "mbase/app/maindb"
"mbase/pkg/logger" "mbase/pkg/logger"
) )
type LogicConfig struct { type LogicConfig struct {
Database *database.Database Database *maindb.Database
} }
type Logic struct { type Logic struct {
log *logger.Logger log *logger.Logger
db *database.Database db *maindb.Database
dumpingSem atomic.Bool dumpingSem atomic.Bool
restoringSem atomic.Bool restoringSem atomic.Bool
} }
@@ -1,12 +1,12 @@
package database package maindb
import ( import (
"context" "context"
"mbase/app/descriptor" "mbase/app/descr"
) )
func (db *Database) InsertAccount(ctx context.Context, account *descriptor.Account) error { func (db *Database) InsertAccount(ctx context.Context, account *descr.Account) error {
var err error var err error
request := `INSERT INTO account(id, username, passhash, disabled, created_at, updated_at) request := `INSERT INTO account(id, username, passhash, disabled, created_at, updated_at)
@@ -19,7 +19,7 @@ func (db *Database) InsertAccount(ctx context.Context, account *descriptor.Accou
return err return err
} }
func (db *Database) UpdateAccountByID(ctx context.Context, accountID int64, account *descriptor.Account) error { func (db *Database) UpdateAccountByID(ctx context.Context, accountID int64, account *descr.Account) error {
var err error var err error
request := `UPDATE account SET username = $1, passhash = $2, disabled = $3, updated_at = $4 WHERE id = $5` request := `UPDATE account SET username = $1, passhash = $2, disabled = $3, updated_at = $4 WHERE id = $5`
@@ -30,10 +30,10 @@ func (db *Database) UpdateAccountByID(ctx context.Context, accountID int64, acco
return err return err
} }
func (db *Database) ReducedListAccounts(ctx context.Context) ([]descriptor.Account, error) { func (db *Database) ReducedListAccounts(ctx context.Context) ([]descr.Account, error) {
var err error var err error
request := `SELECT id, username, disabled, created_at, updated_at FROM account` request := `SELECT id, username, disabled, created_at, updated_at FROM account`
res := make([]descriptor.Account, 0) res := make([]descr.Account, 0)
err = db.db.Select(&res, request) err = db.db.Select(&res, request)
if err != nil { if err != nil {
return res, err return res, err
@@ -41,10 +41,10 @@ func (db *Database) ReducedListAccounts(ctx context.Context) ([]descriptor.Accou
return res, err return res, err
} }
func (db *Database) CompletedListAccounts(ctx context.Context) ([]descriptor.Account, error) { func (db *Database) CompletedListAccounts(ctx context.Context) ([]descr.Account, error) {
var err error var err error
request := `SELECT * FROM account` request := `SELECT * FROM account`
res := make([]descriptor.Account, 0) res := make([]descr.Account, 0)
err = db.db.Select(&res, request) err = db.db.Select(&res, request)
if err != nil { if err != nil {
return res, err return res, err
@@ -52,13 +52,13 @@ func (db *Database) CompletedListAccounts(ctx context.Context) ([]descriptor.Acc
return res, err return res, err
} }
func (db *Database) GetAccountByID(ctx context.Context, accountID int64) (bool, *descriptor.Account, error) { func (db *Database) GetAccountByID(ctx context.Context, accountID int64) (bool, *descr.Account, error) {
var err error var err error
var res *descriptor.Account var res *descr.Account
var exists bool var exists bool
request := `SELECT id, username, passhash, disabled, created_at, updated_at request := `SELECT id, username, passhash, disabled, created_at, updated_at
FROM account WHERE id = $1 LiMIT 1` FROM account WHERE id = $1 LiMIT 1`
dbRes := make([]descriptor.Account, 0) dbRes := make([]descr.Account, 0)
err = db.db.Select(&dbRes, request, accountID) err = db.db.Select(&dbRes, request, accountID)
if err != nil { if err != nil {
return exists, res, err return exists, res, err
@@ -71,13 +71,13 @@ func (db *Database) GetAccountByID(ctx context.Context, accountID int64) (bool,
return exists, res, err return exists, res, err
} }
func (db *Database) GetAccountByUsername(ctx context.Context, username string) (bool, *descriptor.Account, error) { func (db *Database) GetAccountByUsername(ctx context.Context, username string) (bool, *descr.Account, error) {
var err error var err error
var res *descriptor.Account var res *descr.Account
var exists bool var exists bool
request := `SELECT id, username, passhash, disabled, created_at, updated_at request := `SELECT id, username, passhash, disabled, created_at, updated_at
FROM account WHERE username = $1 LIMIT 1` FROM account WHERE username = $1 LIMIT 1`
dbRes := make([]descriptor.Account, 0) dbRes := make([]descr.Account, 0)
err = db.db.Select(&dbRes, request, username) err = db.db.Select(&dbRes, request, username)
if err != nil { if err != nil {
return exists, res, err return exists, res, err
@@ -1,4 +1,4 @@
package database package maindb
import ( import (
"context" "context"
+10 -10
View File
@@ -1,12 +1,12 @@
package database package maindb
import ( import (
"context" "context"
"mbase/app/descriptor" "mbase/app/descr"
) )
func (db *Database) InsertGrant(ctx context.Context, grant *descriptor.Grant) error { func (db *Database) InsertGrant(ctx context.Context, grant *descr.Grant) error {
var err error var err error
request := `INSERT INTO grant(id, account_id, operation, created_at) request := `INSERT INTO grant(id, account_id, operation, created_at)
VALUES ($1, $2, $3, $4)` VALUES ($1, $2, $3, $4)`
@@ -17,10 +17,10 @@ func (db *Database) InsertGrant(ctx context.Context, grant *descriptor.Grant) er
return err return err
} }
func (db *Database) ListGrantsByAccountID(ctx context.Context, accountID int64) ([]descriptor.Grant, error) { func (db *Database) ListGrantsByAccountID(ctx context.Context, accountID int64) ([]descr.Grant, error) {
var err error var err error
request := `SELECT * FROM grant WHERE account_id = $1` request := `SELECT * FROM grant WHERE account_id = $1`
res := make([]descriptor.Grant, 0) res := make([]descr.Grant, 0)
err = db.db.Select(&res, request, accountID) err = db.db.Select(&res, request, accountID)
if err != nil { if err != nil {
return res, err return res, err
@@ -28,10 +28,10 @@ func (db *Database) ListGrantsByAccountID(ctx context.Context, accountID int64)
return res, err return res, err
} }
func (db *Database) ListGrants(ctx context.Context) ([]descriptor.Grant, error) { func (db *Database) ListGrants(ctx context.Context) ([]descr.Grant, error) {
var err error var err error
request := `SELECT * FROM grant` request := `SELECT * FROM grant`
res := make([]descriptor.Grant, 0) res := make([]descr.Grant, 0)
err = db.db.Select(&res, request) err = db.db.Select(&res, request)
if err != nil { if err != nil {
return res, err return res, err
@@ -39,11 +39,11 @@ func (db *Database) ListGrants(ctx context.Context) ([]descriptor.Grant, error)
return res, err return res, err
} }
func (db *Database) GetGrant(ctx context.Context, accountID int64, operation string) (bool, *descriptor.Grant, error) { func (db *Database) GetGrant(ctx context.Context, accountID int64, operation string) (bool, *descr.Grant, error) {
var err error var err error
res := &descriptor.Grant{} res := &descr.Grant{}
request := `SELECT * FROM grant WHERE account_id = $1 AND operation = $2 LIMIT 1` request := `SELECT * FROM grant WHERE account_id = $1 AND operation = $2 LIMIT 1`
dbRes := make([]descriptor.Grant, 0) dbRes := make([]descr.Grant, 0)
err = db.db.Select(&dbRes, request, accountID, operation) err = db.db.Select(&dbRes, request, accountID, operation)
if err != nil { if err != nil {
return false, res, err return false, res, err
+5 -5
View File
@@ -11,8 +11,8 @@ import (
"time" "time"
"mbase/app/config" "mbase/app/config"
"mbase/app/database" "mbase/app/maindb"
"mbase/app/descriptor" "mbase/app/descr"
"mbase/app/logic" "mbase/app/logic"
"mbase/pkg/aux509" "mbase/pkg/aux509"
"mbase/pkg/logger" "mbase/pkg/logger"
@@ -32,10 +32,10 @@ type Server struct {
log *logger.Logger log *logger.Logger
nacl *netacl.NetACL nacl *netacl.NetACL
db *database.Database db *maindb.Database
x509cert []byte x509cert []byte
x509key []byte x509key []byte
state descriptor.Server state descr.Server
sfile string sfile string
} }
@@ -164,7 +164,7 @@ func (srv *Server) Build() error {
srv.log.Infof("Network ACL is:\n%s\n", string(naclYAML)) srv.log.Infof("Network ACL is:\n%s\n", string(naclYAML))
// Create database // Create database
srv.db, err = database.NewDatabase(srv.conf.DataDir) srv.db, err = maindb.NewDatabase(srv.conf.DataDir)
err = srv.db.OpenDatabase() err = srv.db.OpenDatabase()
if err != nil { if err != nil {
+5 -5
View File
@@ -15,8 +15,8 @@ import (
"time" "time"
"mbase/app/config" "mbase/app/config"
"mbase/app/database" "mbase/app/maindb"
"mbase/app/descriptor" "mbase/app/descr"
"mbase/app/logic" "mbase/app/logic"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
@@ -61,8 +61,8 @@ type Util struct {
conf *config.Config conf *config.Config
lg *logic.Logic lg *logic.Logic
db *database.Database db *maindb.Database
state descriptor.Server state descr.Server
sfile string sfile string
accessUsername string accessUsername string
@@ -287,7 +287,7 @@ func (util *Util) Build() error {
util.sfile = filepath.Join(util.conf.DataDir, "certmanager.yaml") util.sfile = filepath.Join(util.conf.DataDir, "certmanager.yaml")
db, err := database.NewDatabase(util.conf.DataDir) db, err := maindb.NewDatabase(util.conf.DataDir)
if err != nil { if err != nil {
return err return err
} }
+6 -6
View File
@@ -11,8 +11,8 @@ import (
"path/filepath" "path/filepath"
"mbase/app/config" "mbase/app/config"
"mbase/app/database" "mbase/app/maindb"
"mbase/app/descriptor" "mbase/app/descr"
"mbase/pkg/auxtool" "mbase/pkg/auxtool"
"mbase/pkg/logger" "mbase/pkg/logger"
@@ -97,7 +97,7 @@ func (util *Util) dumpDatabase(params dumpDatabaseParams) (dumpDatabaseResult, e
if err != nil { if err != nil {
return res, err return res, err
} }
db, err := database.NewDatabase(conf.DataDir) db, err := maindb.NewDatabase(conf.DataDir)
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -121,7 +121,7 @@ func (util *Util) dumpDatabase(params dumpDatabaseParams) (dumpDatabaseResult, e
if err != nil { if err != nil {
return res, err return res, err
} }
dump := descriptor.Dump{ dump := descr.Dump{
Timestamp: auxtool.TimeNow(), Timestamp: auxtool.TimeNow(),
Accounts: listAccounts, Accounts: listAccounts,
Grants: listGrants, Grants: listGrants,
@@ -165,7 +165,7 @@ func (util *Util) restoreDatabase(params restoreDatabaseParams) (restoreDatabase
return res, err return res, err
} }
db, err := database.NewDatabase(conf.DataDir) db, err := maindb.NewDatabase(conf.DataDir)
if err != nil { if err != nil {
return res, err return res, err
} }
@@ -186,7 +186,7 @@ func (util *Util) restoreDatabase(params restoreDatabaseParams) (restoreDatabase
if err != nil { if err != nil {
return res, err return res, err
} }
dump := descriptor.Dump{} dump := descr.Dump{}
err = yaml.Unmarshal(buffer.Bytes(), &dump) err = yaml.Unmarshal(buffer.Bytes(), &dump)
if err != nil { if err != nil {
return res, err return res, err