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