working commit

This commit is contained in:
2026-06-09 14:10:53 +02:00
parent ee30858d11
commit 6e92720e69
26 changed files with 339 additions and 165 deletions
+24 -25
View File
@@ -20,32 +20,30 @@ const (
pidFilename = "mbased.pid"
)
var (
buildVersion = "NONE"
)
type Networks struct {
Enabled []string `json:"enabled" yaml:"enabled"`
Disabled []string `json:"disabled" yaml:"disabled"`
}
type ServiceConfig struct {
type Service struct {
Portnum uint32 `json:"port" yaml:"port"`
Address string `json:"address" yaml:"address"`
Protocol string `json:"protocol" yaml:"protocol"`
}
type Config struct {
PackageVersion string `json:"packageVersion" yaml:"packageVersion"`
Service ServiceConfig `json:"service" yaml:"service"`
Networks Networks `json:"networks" yaml:"networks"`
Hostname string `json:"hostname" yaml:"hostname"`
Debug bool `json:"debug" yaml:"debug"`
Build string `json:"build" yaml:"build"`
LogPath string `json:"logfile" yaml:"logfile"`
RunPath string `json:"runfile" yaml:"runfile"`
DataDir string `json:"datadir" yaml:"datadir"`
Daemon bool `json:"daemon" yaml:"daemon"`
Version string `json:"version" yaml:"version"`
Service Service `json:"service" yaml:"service"`
Networks Networks `json:"networks" yaml:"networks"`
Hostname string `json:"hostname" yaml:"hostname"`
Debug bool `json:"debug" yaml:"debug"`
Build string `json:"build" yaml:"build"`
LogPath string `json:"logfile" yaml:"logfile"`
RunPath string `json:"runfile" yaml:"runfile"`
DataDir string `json:"datadir" yaml:"datadir"`
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
RunUser string `json:"runUser" yaml:"runUser"`
LogLimit int64 `json:"logLimit" yaml:"logLimit"`
}
var (
@@ -54,28 +52,29 @@ var (
)
const (
defaultServiceAddress = "0.0.0.0"
defaultServiceAddress = "[::]"
defaultServiceProtocol = "tcp"
)
func NewConfig() *Config {
conf := &Config{
Service: ServiceConfig{
Service: Service{
Portnum: client.DefaultPort,
Address: defaultServiceAddress,
Protocol: defaultServiceProtocol,
},
DataDir: datadirPath,
Debug: false,
Hostname: defaultHostname,
Build: buildVersion,
Daemon: true,
PackageVersion: packageVersion,
Networks: Networks{
Enabled: defaultEnabledNetworks,
Disabled: defaultDisabledNetworks,
},
DataDir: datadirPath,
Debug: false,
Hostname: defaultHostname,
Build: packageVersion,
AsDaemon: true,
Version: packageVersion,
LogLimit: 1024 * 1024 * 10, // 10 Mb
RunUser: "daemon",
}
conf.LogPath = filepath.Join(logdirPath, logFilename)
conf.RunPath = filepath.Join(rundirPath, pidFilename)
@@ -106,7 +105,7 @@ func (conf *Config) ReadOpts() error {
exeName := filepath.Base(os.Args[0])
flag.BoolVar(&conf.Daemon, "daemon", conf.Daemon, "run as daemon")
flag.BoolVar(&conf.AsDaemon, "asDaemon", conf.AsDaemon, "run as daemon")
flag.BoolVar(&conf.Debug, "debug", conf.Debug, "on debug mode")
help := func() {
-29
View File
@@ -10,34 +10,6 @@ import (
_ "github.com/mattn/go-sqlite3"
)
const schema = `
--- DROP TABLE IF EXISTS account;
CREATE TABLE IF NOT EXISTS account (
id TEXT NOT NULL,
username TEXT NOT NULL,
passhash TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
disabled BOOL
);
CREATE UNIQUE INDEX IF NOT EXISTS account_index01
ON account(id);
CREATE UNIQUE INDEX IF NOT EXISTS account_index02
ON account(username);
--- DROP TABLE IF EXISTS grant;
CREATE TABLE IF NOT EXISTS grant (
id TEXT NOT NULL,
account_id TEXT NOT NULL,
operation TEXT NOT NULL,
created_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS grant_index01
ON grant(account_id);
CREATE UNIQUE INDEX IF NOT EXISTS grant_index02
ON grant(account_id, operation);
`
type Database struct {
datapath string
db *sqlx.DB
@@ -61,7 +33,6 @@ func (db *Database) OpenDatabase() error {
if err != nil {
return err
}
err = db.db.Ping()
if err != nil {
return err
+8 -8
View File
@@ -8,9 +8,9 @@ import (
func (db *Database) InsertGrant(ctx context.Context, grant *descr.Grant) error {
var err error
request := `INSERT INTO grant(id, account_id, operation, created_at)
request := `INSERT INTO grant(id, account_id, grantname, created_at)
VALUES ($1, $2, $3, $4)`
_, err = db.db.Exec(request, grant.ID, grant.AccountID, grant.Operation, grant.CreatedAt)
_, err = db.db.Exec(request, grant.ID, grant.AccountID, grant.Grantname, grant.CreatedAt)
if err != nil {
return err
}
@@ -39,12 +39,12 @@ func (db *Database) ListGrants(ctx context.Context) ([]descr.Grant, error) {
return res, err
}
func (db *Database) GetGrant(ctx context.Context, accountID, operation string) (bool, *descr.Grant, error) {
func (db *Database) GetGrant(ctx context.Context, accountID, grantname string) (bool, *descr.Grant, error) {
var err error
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 grantname = $2 LIMIT 1`
dbRes := make([]descr.Grant, 0)
err = db.db.Select(&dbRes, request, accountID, operation)
err = db.db.Select(&dbRes, request, accountID, grantname)
if err != nil {
return false, res, err
}
@@ -56,10 +56,10 @@ func (db *Database) GetGrant(ctx context.Context, accountID, operation string) (
return true, res, err
}
func (db *Database) DeleteGrantByAccountID(ctx context.Context, grantID, operation string) error {
func (db *Database) DeleteGrantByAccountID(ctx context.Context, grantID, grantname string) error {
var err error
request := `DELETE FROM grant WHERE account_id = $1 AND operation = $2`
_, err = db.db.Exec(request, grantID, operation)
request := `DELETE FROM grant WHERE account_id = $1 AND grantname = $2`
_, err = db.db.Exec(request, grantID, grantname)
if err != nil {
return err
}
+29
View File
@@ -0,0 +1,29 @@
package maindb
const schema = `
--- DROP TABLE IF EXISTS account;
CREATE TABLE IF NOT EXISTS account (
id TEXT NOT NULL,
username TEXT NOT NULL,
passhash TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
disabled BOOL
);
CREATE UNIQUE INDEX IF NOT EXISTS account_index01
ON account(id);
CREATE UNIQUE INDEX IF NOT EXISTS account_index02
ON account(username);
--- DROP TABLE IF EXISTS grant;
CREATE TABLE IF NOT EXISTS grant (
id TEXT NOT NULL,
account_id TEXT NOT NULL,
grantname TEXT NOT NULL,
created_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS grant_index01
ON grant(account_id);
CREATE UNIQUE INDEX IF NOT EXISTS grant_index02
ON grant(account_id, grantname);
`
+1 -1
View File
@@ -221,7 +221,7 @@ func (oper *Operator) ListAccounts(ctx context.Context, accountID string, params
}
for _, grantDescrs := range grantDescrs {
grantShortDescrs := &mbctl.GrantShortDescr{
Operation: grantDescrs.Operation,
Grantname: grantDescrs.Grantname,
CreatedAt: grantDescrs.CreatedAt,
}
accountShortDescr.Grants = append(accountShortDescr.Grants, grantShortDescrs)
+1 -1
View File
@@ -54,7 +54,7 @@ func (oper *Operator) SeedAccount(ctx context.Context) (string, error) {
for _, grantType := range grantTypes {
grantDescr := &descr.Grant{
AccountID: accountDescr.ID,
Operation: grantType,
Grantname: grantType,
CreatedAt: now,
}
err = oper.db.InsertGrant(ctx, grantDescr)
+1 -1
View File
@@ -96,7 +96,7 @@ func (oper *Operator) RestoreDump(ctx context.Context, accountID string, params
}
}
for _, grant := range dump.Grants {
oper.log.Infof("Insert grant %s for account %d", grant.Operation, grant.AccountID)
oper.log.Infof("Insert grant %s for account %d", grant.Grantname, grant.AccountID)
err = oper.db.InsertGrant(ctx, &grant)
if err != nil {
oper.log.Errorf("Insert account error: %v", err)
+7 -7
View File
@@ -31,7 +31,7 @@ func (oper *Operator) SetGrant(ctx context.Context, accountID string, params *mb
}
var grantOk bool
for _, grantType := range grantTypes {
if grantType == params.Operation {
if grantType == params.Grantname {
grantOk = true
break
}
@@ -60,12 +60,12 @@ func (oper *Operator) SetGrant(ctx context.Context, accountID string, params *mb
return res, err
}
grantExists, _, err = oper.db.GetGrant(ctx, accountDescr.ID, params.Operation)
grantExists, _, err = oper.db.GetGrant(ctx, accountDescr.ID, params.Grantname)
if err != nil {
return res, err
}
if grantExists {
err := fmt.Errorf("Grant %s for the user already exists", params.Operation)
err := fmt.Errorf("Grant %s for the user already exists", params.Grantname)
return res, err
}
now := time.Now().Format(time.RFC3339)
@@ -73,7 +73,7 @@ func (oper *Operator) SetGrant(ctx context.Context, accountID string, params *mb
ID: auxuuid.NewUUID(),
AccountID: accountDescr.ID,
CreatedAt: now,
Operation: params.Operation,
Grantname: params.Grantname,
}
err = oper.db.InsertGrant(ctx, grantDescr)
if err != nil {
@@ -103,7 +103,7 @@ func (oper *Operator) DeleteGrant(ctx context.Context, accountID string, params
}
var grantOk bool
for _, grantType := range grantTypes {
if grantType == params.Operation {
if grantType == params.Grantname {
grantOk = true
break
}
@@ -132,7 +132,7 @@ func (oper *Operator) DeleteGrant(ctx context.Context, accountID string, params
return res, err
}
grantExists, _, err = oper.db.GetGrant(ctx, accountDescr.ID, params.Operation)
grantExists, _, err = oper.db.GetGrant(ctx, accountDescr.ID, params.Grantname)
if err != nil {
return res, err
}
@@ -140,7 +140,7 @@ func (oper *Operator) DeleteGrant(ctx context.Context, accountID string, params
err := fmt.Errorf("Requested grant for the user not found")
return res, err
}
err = oper.db.DeleteGrantByAccountID(ctx, accountDescr.ID, params.Operation)
err = oper.db.DeleteGrantByAccountID(ctx, accountDescr.ID, params.Grantname)
if err != nil {
return res, err
}
+79 -16
View File
@@ -1,6 +1,7 @@
package server
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
@@ -37,6 +38,7 @@ type Server struct {
x509key []byte
state descr.Server
sfile string
logf *os.File
}
func NewServer() (*Server, error) {
@@ -117,26 +119,88 @@ func (srv *Server) Build() error {
var err error
srv.log.Infof("Build server")
currUser, err := user.Current()
if err != nil {
err = fmt.Errorf("Error getting current user: %v\n", err)
return err
}
cuid64, err := strconv.ParseInt(currUser.Uid, 10, 64)
if err != nil {
return err
}
cgid64, err := strconv.ParseInt(currUser.Gid, 10, 64)
if err != nil {
return err
}
euid := int(cuid64)
egid := int(cgid64)
if cuid64 == 0 {
usr, err := user.Lookup(srv.conf.RunUser)
if err != nil {
return err
}
uid64, err := strconv.ParseInt(usr.Uid, 10, 64)
if err != nil {
return err
}
gid64, err := strconv.ParseInt(usr.Gid, 10, 64)
if err != nil {
return err
}
euid = int(uid64)
egid = int(gid64)
}
// Mkdir log and data dir
srv.log.Infof("Create %s dir", srv.conf.DataDir)
datadir := srv.conf.DataDir
srv.log.Infof("Create %s dir", datadir)
err = os.MkdirAll(srv.conf.DataDir, 0750)
if err != nil {
return err
}
if srv.conf.Daemon {
logDir := filepath.Dir(srv.conf.LogPath)
srv.log.Infof("Create %s dir", logDir)
err = os.MkdirAll(logDir, 0750)
if err != nil {
return err
}
runDir := filepath.Dir(srv.conf.RunPath)
srv.log.Infof("Create %s dir", runDir)
err = os.MkdirAll(runDir, 0750)
if err != nil {
return err
}
err = os.Chown(datadir, euid, egid)
if err != nil {
return err
}
if srv.conf.AsDaemon {
logdir := filepath.Dir(srv.conf.LogPath)
//srv.logg.Infof("Creating log directory %s", logdir)
err = os.MkdirAll(logdir, 0750)
if err != nil {
return err
}
err = os.Chown(logdir, euid, egid)
if err != nil {
return err
}
rundir := filepath.Dir(srv.conf.RunPath)
//srv.logg.Infof("Creating run directory %s", rundir)
err = os.MkdirAll(rundir, 0750)
if err != nil {
return err
}
err = os.Chown(rundir, euid, egid)
if err != nil {
return err
}
// Redirect stderr and stout
logFile, err := os.OpenFile(srv.conf.LogPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
return err
}
err = syscall.Dup2(int(logFile.Fd()), int(os.Stdout.Fd()))
if err != nil {
return err
}
err = syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
if err != nil {
return err
}
srv.logf = logFile
}
// Create X509 certs
srv.x509cert, srv.x509key, err = aux509.CreateX509SelfSignedCert(srv.conf.Hostname)
if err != nil {
@@ -305,13 +369,12 @@ func (srv *Server) PseudoFork() error {
func (srv *Server) Daemonize() error {
var err error
if srv.conf.Daemon {
if srv.conf.AsDaemon {
// Restart process process
err = srv.PseudoFork()
if err != nil {
return err
}
// Redirect stdin
nullFile, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
if err != nil {