From c656b562d84b4ac0be205e73cf93ae375539a38c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D0=B5=D0=B3=20=D0=91=D0=BE=D1=80=D0=BE=D0=B4?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Sun, 7 Jun 2026 15:06:37 +0200 Subject: [PATCH] working commit --- app/config/config.go | 16 +-- app/handler/account.go | 2 +- app/maindb/database.go | 2 +- app/operator/account.go | 2 +- app/operator/database.go | 4 +- app/server/server.go | 2 +- .../account/{util.go => accutil.go} | 33 ++++- cmd/mbaseadmin/account/create.go | 46 +++---- cmd/mbaseadmin/account/delete.go | 26 +++- cmd/mbaseadmin/account/list.go | 24 +++- cmd/mbaseadmin/account/update.go | 30 ++++- .../grant/{util.go => grantutil.go} | 30 +++-- cmd/mbaseadmin/main.go | 4 + cmd/mbaseadmin/printr.go | 27 ++++ cmd/mbaseadmin/util.go | 125 +++++++++++++++++- cmd/mbasectl/attic/main.go | 2 +- proto/mbctl.proto | 1 - 17 files changed, 291 insertions(+), 85 deletions(-) rename cmd/mbaseadmin/account/{util.go => accutil.go} (66%) rename cmd/mbaseadmin/grant/{util.go => grantutil.go} (72%) create mode 100644 cmd/mbaseadmin/printr.go diff --git a/app/config/config.go b/app/config/config.go index 437f51b..6bb3b45 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -15,9 +15,9 @@ import ( const ( defaultHostname = "localhost" - configFilename = "maind.yaml" - logFilename = "maind.log" - pidFilename = "maind.pid" + configFilename = "mbased.yaml" + logFilename = "mbased.log" + pidFilename = "mbased.pid" ) var ( @@ -93,12 +93,6 @@ func (conf *Config) ReadFile() error { if err != nil { return err } - conf.Normalize() - err = conf.Validate() - if err != nil { - return err - } - return nil } @@ -140,10 +134,12 @@ func (conf *Config) String() (string, error) { return res, err } -func (conf *Config) Normalize() { +func (conf *Config) Normalize() error { + var err error if conf.Service.Portnum == 0 { conf.Service.Portnum = client.DefaultPort } + return err } func (conf *Config) Validate() error { diff --git a/app/handler/account.go b/app/handler/account.go index 8faebde..40ba988 100644 --- a/app/handler/account.go +++ b/app/handler/account.go @@ -23,7 +23,7 @@ func (hand *Handler) Authentificate(ctx context.Context) (string, error) { } username := meta["username"][0] password := meta["password"][0] - validated, accountID, err := hand.lg.ValidateAcount(ctx, username, password) + validated, accountID, err := hand.lg.ValidateAccount(ctx, username, password) if !validated { err := status.Errorf(codes.PermissionDenied, "Wrong auth data") return accountID, err diff --git a/app/maindb/database.go b/app/maindb/database.go index a466c58..d884dc7 100644 --- a/app/maindb/database.go +++ b/app/maindb/database.go @@ -56,7 +56,7 @@ func NewDatabase(datapath string) (*Database, error) { func (db *Database) OpenDatabase() error { var err error - dbPath := filepath.Join(db.datapath, "certmanager.db") + dbPath := filepath.Join(db.datapath, "main.db") db.db, err = sqlx.Open("sqlite3", dbPath) if err != nil { return err diff --git a/app/operator/account.go b/app/operator/account.go index f3c251b..b63bd98 100644 --- a/app/operator/account.go +++ b/app/operator/account.go @@ -11,7 +11,7 @@ import ( "mbase/pkg/mbctl" ) -func (lg *Logic) ValidateAcount(ctx context.Context, username, password string) (bool, string, error) { +func (lg *Logic) ValidateAccount(ctx context.Context, username, password string) (bool, string, error) { var err error var accountID string var valid bool diff --git a/app/operator/database.go b/app/operator/database.go index 895ca08..f76363f 100644 --- a/app/operator/database.go +++ b/app/operator/database.go @@ -10,8 +10,8 @@ import ( ) const ( - defaultSeedUsername = "certman" - defaultSeedPassword = "certman" + defaultSeedUsername = "mbase" + defaultSeedPassword = "mbase" ) func (lg *Logic) CleanDatabase(ctx context.Context) error { diff --git a/app/server/server.go b/app/server/server.go index 516f11a..539f776 100644 --- a/app/server/server.go +++ b/app/server/server.go @@ -62,7 +62,7 @@ func (srv *Server) Configure() error { if err != nil { return err } - srv.sfile = filepath.Join(srv.conf.DataDir, "certmanager.yaml") + srv.sfile = filepath.Join(srv.conf.DataDir, "mbase.yaml") return err } diff --git a/cmd/mbaseadmin/account/util.go b/cmd/mbaseadmin/account/accutil.go similarity index 66% rename from cmd/mbaseadmin/account/util.go rename to cmd/mbaseadmin/account/accutil.go index f152959..1fd29c8 100644 --- a/cmd/mbaseadmin/account/util.go +++ b/cmd/mbaseadmin/account/accutil.go @@ -4,6 +4,9 @@ package account import ( + "context" + "fmt" + "mbase/app/operator" "github.com/spf13/cobra" @@ -17,11 +20,12 @@ const ( type Util struct { oper *operator.Logic subCmd *cobra.Command - createAccountParams createAccountParams - listAccountsParams listAccountsParams - updateAccountParams updateAccountParams - deleteAccountParams deleteAccountParams + createAccountParams CreateAccountParams + listAccountsParams ListAccountsParams + updateAccountParams UpdateAccountParams + deleteAccountParams DeleteAccountParams commonParams CommonParams + operID string } func NewUtil(oper *operator.Logic) *Util { @@ -41,9 +45,10 @@ func (util *Util) GetRooCmd() *cobra.Command { func (util *Util) BuildCmds() *cobra.Command { subCmd := &cobra.Command{ - Use: "account", - Short: "\nAccount operations", - SilenceUsage: true, + Use: "account", + Short: "\nAccount operations", + SilenceUsage: true, + PersistentPreRunE: util.ValidateOper, } subCmd.CompletionOptions.DisableDefaultCmd = true @@ -79,6 +84,8 @@ func (util *Util) BuildCmds() *cobra.Command { Args: cobra.ExactArgs(1), Run: util.UpdateAccount, } + updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.Password, "pass", "P", util.updateAccountParams.Password, "New Password") + //updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.Username, "user", "U", util.updateAccountParams.Username, "New Username") subCmd.AddCommand(updateAccountsCmd) var deleteAccountCmd = &cobra.Command{ @@ -92,3 +99,15 @@ func (util *Util) BuildCmds() *cobra.Command { util.subCmd = subCmd return util.subCmd } + +func (util *Util) ValidateOper(cmd *cobra.Command, args []string) error { + var err error + ctx := context.Background() + authOk, operID, err := util.oper.ValidateAccount(ctx, util.commonParams.Username, util.commonParams.Password) + if !authOk { + err := fmt.Errorf("Authentification error") + return err + } + util.operID = operID + return err +} diff --git a/cmd/mbaseadmin/account/create.go b/cmd/mbaseadmin/account/create.go index bb1bf9c..8929e9f 100644 --- a/cmd/mbaseadmin/account/create.go +++ b/cmd/mbaseadmin/account/create.go @@ -4,50 +4,40 @@ package account import ( + "context" + + "mbase/pkg/mbctl" + "github.com/spf13/cobra" ) func (util *Util) CreateAccount(cmd *cobra.Command, args []string) { - //util.createAccountParams.Filename = args[0] - res, err := util.createAccount(util.createAccountParams) + util.createAccountParams.Username = args[0] + util.createAccountParams.Password = args[1] + res, err := util.createAccount(&util.createAccountParams) printResponse(res, err) } -type createAccountParams struct { +type CreateAccountParams struct { Username string Password string } -type createAccountResult struct{} - -func (util *Util) createAccount(params createAccountParams) (createAccountResult, error) { - var err error - res := createAccountResult{} - /* - operParams := &mbctl.CreateAccountParams{ - Username: params.Username, - Password: params.Password, - } - res, err = util.lg.CreateAccount(ctx, operID, params) - if err != nil { - return res, err - } - */ - return res, err +type CreateAccountResult struct { + AccountID string `json:"accountId"` } -/* -func (util *Util) CreateAccount(ctx context.Context, operID string) (*mbctl.CreateAccountResult, error) { +func (util *Util) createAccount(params *CreateAccountParams) (*CreateAccountResult, error) { var err error - res := &mbctl.CreateAccountResult{} - - params := &mbctl.CreateAccountParams{ - Username: util.username, - Password: util.password, + res := &CreateAccountResult{} + ctx := context.Background() + operParams := &mbctl.CreateAccountParams{ + Username: params.Username, + Password: params.Password, } - res, err = util.lg.CreateAccount(ctx, operID, params) + operRes, err := util.oper.CreateAccount(ctx, util.operID, operParams) if err != nil { return res, err } + res.AccountID = operRes.AccountID return res, err } -*/ diff --git a/cmd/mbaseadmin/account/delete.go b/cmd/mbaseadmin/account/delete.go index 8d77b1d..8106e4d 100644 --- a/cmd/mbaseadmin/account/delete.go +++ b/cmd/mbaseadmin/account/delete.go @@ -4,20 +4,34 @@ package account import ( + "context" + + "mbase/pkg/mbctl" + "github.com/spf13/cobra" ) func (util *Util) DeleteAccount(cmd *cobra.Command, args []string) { - //util.deleteAccountParams.Filename = args[0] - res, err := util.deleteAccount(util.deleteAccountParams) + util.deleteAccountParams.Username = args[0] + res, err := util.deleteAccount(&util.deleteAccountParams) printResponse(res, err) } -type deleteAccountParams struct{} -type deleteAccountResult struct{} +type DeleteAccountParams struct { + Username string +} +type DeleteAccountResult struct{} -func (util *Util) deleteAccount(params deleteAccountParams) (deleteAccountResult, error) { +func (util *Util) deleteAccount(params *DeleteAccountParams) (*DeleteAccountResult, error) { var err error - res := deleteAccountResult{} + res := &DeleteAccountResult{} + ctx := context.Background() + operParams := &mbctl.DeleteAccountParams{ + Username: params.Username, + } + _, err = util.oper.DeleteAccount(ctx, util.operID, operParams) + if err != nil { + return res, err + } return res, err } diff --git a/cmd/mbaseadmin/account/list.go b/cmd/mbaseadmin/account/list.go index 7b603dc..044f193 100644 --- a/cmd/mbaseadmin/account/list.go +++ b/cmd/mbaseadmin/account/list.go @@ -4,20 +4,32 @@ package account import ( + "context" + + "mbase/pkg/mbctl" + "github.com/spf13/cobra" ) func (util *Util) ListAccounts(cmd *cobra.Command, args []string) { - //util.listAccountsParams.Filename = args[0] - res, err := util.listAccounts(util.listAccountsParams) + res, err := util.listAccounts(&util.listAccountsParams) printResponse(res, err) } -type listAccountsParams struct{} -type listAccountsResult struct{} +type ListAccountsParams struct{} +type ListAccountsResult struct { + Accounts []*mbctl.AccountShortDescr `json:"accounts"` +} -func (util *Util) listAccounts(params listAccountsParams) (listAccountsResult, error) { +func (util *Util) listAccounts(params *ListAccountsParams) (*ListAccountsResult, error) { var err error - res := listAccountsResult{} + ctx := context.Background() + res := &ListAccountsResult{} + operParams := &mbctl.ListAccountsParams{} + operRes, err := util.oper.ListAccounts(ctx, util.operID, operParams) + if err != nil { + return res, err + } + res.Accounts = operRes.Accounts return res, err } diff --git a/cmd/mbaseadmin/account/update.go b/cmd/mbaseadmin/account/update.go index ab0ad0d..c418929 100644 --- a/cmd/mbaseadmin/account/update.go +++ b/cmd/mbaseadmin/account/update.go @@ -4,20 +4,38 @@ package account import ( + "context" + + "mbase/pkg/mbctl" + "github.com/spf13/cobra" ) func (util *Util) UpdateAccount(cmd *cobra.Command, args []string) { - //util.updateAccountParams.Filename = args[0] - res, err := util.updateAccount(util.updateAccountParams) + util.updateAccountParams.Username = args[0] + res, err := util.updateAccount(&util.updateAccountParams) printResponse(res, err) } -type updateAccountParams struct{} -type updateAccountResult struct{} +type UpdateAccountParams struct { + Username string + Password string +} +type UpdateAccountResult struct { + AccountID string +} -func (util *Util) updateAccount(params updateAccountParams) (updateAccountResult, error) { +func (util *Util) updateAccount(params *UpdateAccountParams) (*UpdateAccountResult, error) { var err error - res := updateAccountResult{} + res := &UpdateAccountResult{} + ctx := context.Background() + operParams := &mbctl.UpdateAccountParams{ + Username: params.Username, + //Password: params.Password, + } + _, err = util.oper.UpdateAccount(ctx, util.operID, operParams) + if err != nil { + return res, err + } return res, err } diff --git a/cmd/mbaseadmin/grant/util.go b/cmd/mbaseadmin/grant/grantutil.go similarity index 72% rename from cmd/mbaseadmin/grant/util.go rename to cmd/mbaseadmin/grant/grantutil.go index f06d0cb..a691655 100644 --- a/cmd/mbaseadmin/grant/util.go +++ b/cmd/mbaseadmin/grant/grantutil.go @@ -4,6 +4,9 @@ package grant import ( + "context" + "fmt" + "github.com/spf13/cobra" "github.com/spf13/viper" "mbase/app/operator" @@ -11,10 +14,11 @@ import ( type Util struct { oper *operator.Logic - subCmd *cobra.Command + operID string + subCmd *cobra.Command createGrantParams createGrantParams deleteGrantParams deleteGrantParams - commonParams CommonParams + commonParams CommonParams } func NewUtil(oper *operator.Logic) *Util { @@ -34,9 +38,10 @@ func (util *Util) GetRooCmd() *cobra.Command { func (util *Util) BuildCmds() *cobra.Command { subCmd := &cobra.Command{ - Use: "grant", - Short: "\nGrant operations", - SilenceUsage: true, + Use: "grant", + Short: "\nGrant operations", + SilenceUsage: true, + PersistentPreRunE: util.ValidateOper, } subCmd.CompletionOptions.DisableDefaultCmd = true @@ -51,9 +56,6 @@ func (util *Util) BuildCmds() *cobra.Command { util.commonParams.Username = vi.GetString("user") util.commonParams.Password = vi.GetString("pass") - - - var createGrantCmd = &cobra.Command{ Use: "create name|id password", Short: "Create grant", @@ -73,3 +75,15 @@ func (util *Util) BuildCmds() *cobra.Command { util.subCmd = subCmd return util.subCmd } + +func (util *Util) ValidateOper(cmd *cobra.Command, args []string) error { + var err error + ctx := context.Background() + authOk, operID, err := util.oper.ValidateAccount(ctx, util.commonParams.Username, util.commonParams.Password) + if !authOk { + err := fmt.Errorf("Authentification error") + return err + } + util.operID = operID + return err +} diff --git a/cmd/mbaseadmin/main.go b/cmd/mbaseadmin/main.go index eaaf76d..edddfa8 100644 --- a/cmd/mbaseadmin/main.go +++ b/cmd/mbaseadmin/main.go @@ -4,18 +4,22 @@ package main import ( + "mbase/pkg/logger" "os" ) func main() { var err error + log := logger.NewLogger("main") util := NewUtil() err = util.Build() if err != nil { + log.Errorf("Build error: %v", err) os.Exit(1) } err = util.Exec(os.Args[1:]) if err != nil { + log.Errorf("Exec error: %v", err) os.Exit(1) } } diff --git a/cmd/mbaseadmin/printr.go b/cmd/mbaseadmin/printr.go new file mode 100644 index 0000000..08b1802 --- /dev/null +++ b/cmd/mbaseadmin/printr.go @@ -0,0 +1,27 @@ +/* + * Copyright 2026 Oleg Borodin + */ +package main + +import ( + "fmt" + + "sigs.k8s.io/yaml" +) + +func printResponse(res any, err error) { + type Response struct { + Error bool `json:"error" yaml:"error"` + Message string `json:"message,omitempty" yaml:"message,omitempty"` + Result any `json:"result,omitempty" yaml:"result,omitempty"` + } + resp := Response{} + if err != nil { + resp.Error = true + resp.Message = err.Error() + } else { + resp.Result = res + } + respBytes, _ := yaml.Marshal(resp) + fmt.Printf("---\n%s\n", string(respBytes)) +} diff --git a/cmd/mbaseadmin/util.go b/cmd/mbaseadmin/util.go index 55356ce..57d3b7c 100644 --- a/cmd/mbaseadmin/util.go +++ b/cmd/mbaseadmin/util.go @@ -4,6 +4,8 @@ package main import ( + "context" + "io/ioutil" "os" "path/filepath" @@ -12,18 +14,27 @@ import ( "mbase/app/operator" "mbase/cmd/mbaseadmin/account" "mbase/cmd/mbaseadmin/grant" + "mbase/pkg/auxtool" + "mbase/pkg/descr" + "mbase/pkg/logger" "github.com/spf13/cobra" + "go.yaml.in/yaml/v4" ) type Util struct { - lg *operator.Logic + log *logger.Logger + oper *operator.Logic db *maindb.Database + state descr.Server + sfile string rootCmd *cobra.Command } func NewUtil() *Util { - return &Util{} + return &Util{ + log: logger.NewLogger("util"), + } } func (util *Util) GetRooCmd() *cobra.Command { @@ -34,6 +45,9 @@ func (util *Util) Build() error { var err error conf := config.NewConfig() + + util.sfile = filepath.Join(conf.DataDir, "mbase.yaml") + err = conf.ReadFile() if err != nil { return err @@ -42,11 +56,29 @@ func (util *Util) Build() error { if err != nil { return err } - + err = conf.Normalize() + if err != nil { + return err + } + err = conf.Validate() + if err != nil { + return err + } + //util.log.Infof("Create %s dir", conf.DataDir) + err = os.MkdirAll(conf.DataDir, 0750) + if err != nil { + return err + } + // Load state + err = util.LoadState() + if err != nil { + //return err + } db, err := maindb.NewDatabase(conf.DataDir) if err != nil { return err } + err = db.OpenDatabase() if err != nil { return err @@ -55,11 +87,24 @@ func (util *Util) Build() error { logicConfig := &operator.LogicConfig{ Database: util.db, } - util.lg, err = operator.NewLogic(logicConfig) + util.oper, err = operator.NewLogic(logicConfig) if err != nil { return err } + if !util.state.DatabaseInitialized { + // Create schema + util.log.Infof("Init database") + err = util.db.InitDatabase() + if err != nil { + return err + } + util.state.DatabaseInitialized = true + err = util.SaveState() + if err != nil { + return err + } + } execName := filepath.Base(os.Args[0]) rootCmd := &cobra.Command{ Use: execName, @@ -68,12 +113,19 @@ func (util *Util) Build() error { } rootCmd.CompletionOptions.DisableDefaultCmd = true - accountUtil := account.NewUtil(util.lg) + accountUtil := account.NewUtil(util.oper) rootCmd.AddCommand(accountUtil.BuildCmds()) - grantUtil := grant.NewUtil(util.lg) + grantUtil := grant.NewUtil(util.oper) rootCmd.AddCommand(grantUtil.BuildCmds()) + var deleteGrantCmd = &cobra.Command{ + Use: "seed", + Short: "Create initial account", + Run: util.SeedAccount, + } + rootCmd.AddCommand(deleteGrantCmd) + util.rootCmd = rootCmd return err } @@ -84,3 +136,64 @@ func (util *Util) Exec(args []string) error { err = util.rootCmd.Execute() return err } + +func (util *Util) LoadState() error { + var err error + _, err = os.Stat(util.sfile) + if os.IsNotExist(err) { + err = nil + return err + } + file, err := os.Open(util.sfile) + if err != nil { + return err + } + defer file.Close() + stateBytes, err := ioutil.ReadAll(file) + if err != nil { + return err + } + err = yaml.Unmarshal(stateBytes, &util.state) + if err != nil { + return err + } + return err +} + +func (util *Util) SaveState() error { + var err error + + file, err := os.OpenFile(util.sfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640) + if err != nil { + return err + } + defer file.Close() + util.state.UpdatedAt = auxtool.TimeNow() + if util.state.CreatedAt == "" { + util.state.CreatedAt = util.state.UpdatedAt + } + stateBytes, err := yaml.Marshal(util.state) + if err != nil { + return err + } + _, err = file.Write(stateBytes) + if err != nil { + return err + } + return err +} + +func (util *Util) SeedAccount(cmd *cobra.Command, args []string) { + err := util.seedAccount() + printResponse(nil, err) +} + +func (util *Util) seedAccount() error { + // Seed accounts + ctx := context.Background() + _, err := util.oper.SeedAccount(ctx) + if err != nil { + return err + } + return err +} diff --git a/cmd/mbasectl/attic/main.go b/cmd/mbasectl/attic/main.go index a6da0e9..3164b5a 100644 --- a/cmd/mbasectl/attic/main.go +++ b/cmd/mbasectl/attic/main.go @@ -22,7 +22,7 @@ import ( const ( defaultHostname = "localhost" - rcFilename = ".certmanager.yaml" + rcFilename = ".mbase.yaml" defaultPort uint32 = client.DefaultPort getHelloCmd = "getHello" diff --git a/proto/mbctl.proto b/proto/mbctl.proto index c52a1c6..7910faa 100644 --- a/proto/mbctl.proto +++ b/proto/mbctl.proto @@ -25,7 +25,6 @@ message getDumpResult { string dump = 1; } - message restoreDumpParams { string dump = 1; bool deleteAllRecords = 2;