working commit
This commit is contained in:
+2
-1
@@ -43,7 +43,8 @@ mbasectl$(EXEEXT): $(mbasectl_SOURCES) $(EXTRA_mbased_SOURCES)
|
|||||||
|
|
||||||
mbaseadmin_SOURCES = \
|
mbaseadmin_SOURCES = \
|
||||||
cmd/mbaseadmin/main.go \
|
cmd/mbaseadmin/main.go \
|
||||||
cmd/mbaseadmin/util.go
|
cmd/mbaseadmin/util.go \
|
||||||
|
cmd/mbaseadmin/printr.go
|
||||||
|
|
||||||
EXTRA_mbaseadmin_SOURCES = \
|
EXTRA_mbaseadmin_SOURCES = \
|
||||||
cmd/mbaseadmin/account/create.go \
|
cmd/mbaseadmin/account/create.go \
|
||||||
|
|||||||
+2
-1
@@ -348,7 +348,8 @@ EXTRA_mbasectl_SOURCES = \
|
|||||||
|
|
||||||
mbaseadmin_SOURCES = \
|
mbaseadmin_SOURCES = \
|
||||||
cmd/mbaseadmin/main.go \
|
cmd/mbaseadmin/main.go \
|
||||||
cmd/mbaseadmin/util.go
|
cmd/mbaseadmin/util.go \
|
||||||
|
cmd/mbaseadmin/printr.go
|
||||||
|
|
||||||
EXTRA_mbaseadmin_SOURCES = \
|
EXTRA_mbaseadmin_SOURCES = \
|
||||||
cmd/mbaseadmin/account/create.go \
|
cmd/mbaseadmin/account/create.go \
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
const (
|
const (
|
||||||
confdirPath = "/home/ziggi/Projects/grpcbase/etc/mbase"
|
confdirPath = "/home/ziggi/Projects/gmbase/etc/mbase"
|
||||||
rundirPath = "/home/ziggi/Projects/grpcbase/tmp/run"
|
rundirPath = "/home/ziggi/Projects/gmbase/tmp/run"
|
||||||
logdirPath = "/home/ziggi/Projects/grpcbase/tmp/log"
|
logdirPath = "/home/ziggi/Projects/gmbase/tmp/log"
|
||||||
datadirPath = "/home/ziggi/Projects/grpcbase/tmp/data"
|
datadirPath = "/home/ziggi/Projects/gmbase/tmp/data"
|
||||||
packageVersion = "0.0.1"
|
packageVersion = "0.0.1"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ func (hand *Handler) Authentificate(ctx context.Context) (string, error) {
|
|||||||
meta, _ := metadata.FromIncomingContext(ctx)
|
meta, _ := metadata.FromIncomingContext(ctx)
|
||||||
usernameArr := meta["username"]
|
usernameArr := meta["username"]
|
||||||
passwordArr := meta["password"]
|
passwordArr := meta["password"]
|
||||||
|
|
||||||
if len(usernameArr) == 0 || len(passwordArr) == 0 {
|
if len(usernameArr) == 0 || len(passwordArr) == 0 {
|
||||||
err := status.Errorf(codes.PermissionDenied, "Empty auth data")
|
err := status.Errorf(codes.PermissionDenied, "Empty auth data")
|
||||||
return accountID, err
|
return accountID, err
|
||||||
}
|
}
|
||||||
username := meta["username"][0]
|
username := meta["username"][0]
|
||||||
password := meta["password"][0]
|
password := meta["password"][0]
|
||||||
|
//hand.log.Debugf("Auth pair: [%s:%s]", username, password)
|
||||||
validated, accountID, err := hand.oper.ValidateAccount(ctx, username, password)
|
validated, accountID, err := hand.oper.ValidateAccount(ctx, username, password)
|
||||||
if !validated {
|
if !validated {
|
||||||
err := status.Errorf(codes.PermissionDenied, "Wrong auth data")
|
err := status.Errorf(codes.PermissionDenied, "Wrong auth data")
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func NewDatabase(datapath string) (*Database, error) {
|
|||||||
db := &Database{
|
db := &Database{
|
||||||
datapath: datapath,
|
datapath: datapath,
|
||||||
}
|
}
|
||||||
db.log = logger.NewLogger("database")
|
db.log = logger.NewLogger("maindb")
|
||||||
return db, err
|
return db, err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,12 +86,10 @@ func (svc *Service) Run() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
interceptors := []grpc.UnaryServerInterceptor{
|
interceptors := []grpc.UnaryServerInterceptor{
|
||||||
svc.accessInterceptor,
|
svc.accessInterceptor,
|
||||||
svc.logInterceptor,
|
svc.logInterceptor,
|
||||||
}
|
}
|
||||||
|
|
||||||
gsrvOpts := []grpc.ServerOption{
|
gsrvOpts := []grpc.ServerOption{
|
||||||
grpc.Creds(tlsCredentials),
|
grpc.Creds(tlsCredentials),
|
||||||
grpc.ChainUnaryInterceptor(interceptors...),
|
grpc.ChainUnaryInterceptor(interceptors...),
|
||||||
@@ -114,7 +112,6 @@ func (svc *Service) accessInterceptor(ctx context.Context, req any, info *grpc.U
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
addressEnabled, _ := svc.nacl.AddressIsEnabled(host)
|
addressEnabled, _ := svc.nacl.AddressIsEnabled(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -13,10 +13,6 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
defaultHostname = "localhost:1025"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Util struct {
|
type Util struct {
|
||||||
oper *operator.Operator
|
oper *operator.Operator
|
||||||
subCmd *cobra.Command
|
subCmd *cobra.Command
|
||||||
@@ -84,8 +80,8 @@ func (util *Util) BuildCmds() *cobra.Command {
|
|||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: util.UpdateAccount,
|
Run: util.UpdateAccount,
|
||||||
}
|
}
|
||||||
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.Password, "newpass", "N", util.updateAccountParams.Password, "New password")
|
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.NewPassword, "newpass", "N", util.updateAccountParams.NewPassword, "New password")
|
||||||
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.Username, "newname", "M", util.updateAccountParams.Username, "New username")
|
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newname", "M", util.updateAccountParams.NewUsername, "New username")
|
||||||
updateAccountsCmd.MarkFlagsOneRequired("newpass", "newname")
|
updateAccountsCmd.MarkFlagsOneRequired("newpass", "newname")
|
||||||
subCmd.AddCommand(updateAccountsCmd)
|
subCmd.AddCommand(updateAccountsCmd)
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,9 @@ func (util *Util) UpdateAccount(cmd *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UpdateAccountParams struct {
|
type UpdateAccountParams struct {
|
||||||
Username string
|
Username string
|
||||||
Password string
|
NewPassword string
|
||||||
|
NewUsername string
|
||||||
}
|
}
|
||||||
type UpdateAccountResult struct {
|
type UpdateAccountResult struct {
|
||||||
AccountID string
|
AccountID string
|
||||||
@@ -30,8 +31,9 @@ func (util *Util) updateAccount(params *UpdateAccountParams) (*UpdateAccountResu
|
|||||||
res := &UpdateAccountResult{}
|
res := &UpdateAccountResult{}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
operParams := &mbctl.UpdateAccountParams{
|
operParams := &mbctl.UpdateAccountParams{
|
||||||
Username: params.Username,
|
Username: params.Username,
|
||||||
//Password: params.Password,
|
NewPassword: params.NewPassword,
|
||||||
|
NewUsername: params.NewUsername,
|
||||||
}
|
}
|
||||||
_, err = util.oper.UpdateAccount(ctx, util.operID, operParams)
|
_, err = util.oper.UpdateAccount(ctx, util.operID, operParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -22,22 +22,19 @@ type DeleteGrantParams struct {
|
|||||||
Username string
|
Username string
|
||||||
Operation string
|
Operation string
|
||||||
}
|
}
|
||||||
type DeleteGrantResult struct {
|
type DeleteGrantResult struct{}
|
||||||
GrantID string `json:"grantId"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (util *Util) deleteGrant(params *DeleteGrantParams) (*DeleteGrantResult, error) {
|
func (util *Util) deleteGrant(params *DeleteGrantParams) (*DeleteGrantResult, error) {
|
||||||
var err error
|
var err error
|
||||||
res := &DeleteGrantResult{}
|
res := &DeleteGrantResult{}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
operParams := &mbctl.SetGrantParams{
|
operParams := &mbctl.DeleteGrantParams{
|
||||||
Username: params.Username,
|
Username: params.Username,
|
||||||
Operation: params.Operation,
|
Operation: params.Operation,
|
||||||
}
|
}
|
||||||
operRes, err := util.oper.SetGrant(ctx, util.operID, operParams)
|
_, err = util.oper.DeleteGrant(ctx, util.operID, operParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
res.GrantID = operRes.GrantID
|
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ func (util *Util) BuildCmds() *cobra.Command {
|
|||||||
util.commonParams.Password = vi.GetString("pass")
|
util.commonParams.Password = vi.GetString("pass")
|
||||||
|
|
||||||
var setGrantCmd = &cobra.Command{
|
var setGrantCmd = &cobra.Command{
|
||||||
Use: "set name|id password",
|
Use: "set username grant",
|
||||||
Short: "Set grant for account",
|
Short: "Set grant for account",
|
||||||
Args: cobra.ExactArgs(2),
|
Args: cobra.ExactArgs(2),
|
||||||
Run: util.SetGrant,
|
Run: util.SetGrant,
|
||||||
@@ -65,9 +65,9 @@ func (util *Util) BuildCmds() *cobra.Command {
|
|||||||
subCmd.AddCommand(setGrantCmd)
|
subCmd.AddCommand(setGrantCmd)
|
||||||
|
|
||||||
var deleteGrantCmd = &cobra.Command{
|
var deleteGrantCmd = &cobra.Command{
|
||||||
Use: "delete name|id grant",
|
Use: "delete username|id grant",
|
||||||
Short: "Delete grant for account",
|
Short: "Delete grant for account",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(2),
|
||||||
Run: util.DeleteGrant,
|
Run: util.DeleteGrant,
|
||||||
}
|
}
|
||||||
subCmd.AddCommand(deleteGrantCmd)
|
subCmd.AddCommand(deleteGrantCmd)
|
||||||
|
|||||||
@@ -4,20 +4,47 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mbase/pkg/client"
|
||||||
|
"mbase/pkg/mbctl"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (util *Util) CreateAccount(cmd *cobra.Command, args []string) {
|
func (util *Util) CreateAccount(cmd *cobra.Command, args []string) {
|
||||||
//util.createAccountParams.Filename = args[0]
|
util.createAccountParams.Username = args[0]
|
||||||
|
util.createAccountParams.Password = args[1]
|
||||||
res, err := util.createAccount(util.createAccountParams)
|
res, err := util.createAccount(util.createAccountParams)
|
||||||
printResponse(res, err)
|
printResponse(res, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type createAccountParams struct{}
|
type CreateAccountParams struct {
|
||||||
type createAccountResult struct{}
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
type CreateAccountResult struct {
|
||||||
|
AccountID string
|
||||||
|
}
|
||||||
|
|
||||||
func (util *Util) createAccount(params createAccountParams) (createAccountResult, error) {
|
func (util *Util) createAccount(params CreateAccountParams) (CreateAccountResult, error) {
|
||||||
var err error
|
var err error
|
||||||
res := createAccountResult{}
|
res := CreateAccountResult{}
|
||||||
|
|
||||||
|
grpcConn, cli, err := client.NewClient(&util.access)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
defer grpcConn.Close()
|
||||||
|
operParams := &mbctl.CreateAccountParams{
|
||||||
|
Username: params.Username,
|
||||||
|
Password: params.Password,
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
operRes, err := cli.CreateAccount(ctx, operParams)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
res.AccountID = operRes.AccountID
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,20 +4,41 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mbase/pkg/client"
|
||||||
|
"mbase/pkg/mbctl"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (util *Util) DeleteAccount(cmd *cobra.Command, args []string) {
|
func (util *Util) DeleteAccount(cmd *cobra.Command, args []string) {
|
||||||
//util.deleteAccountParams.Filename = args[0]
|
util.deleteAccountParams.Username = args[0]
|
||||||
res, err := util.deleteAccount(util.deleteAccountParams)
|
res, err := util.deleteAccount(util.deleteAccountParams)
|
||||||
printResponse(res, err)
|
printResponse(res, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type deleteAccountParams struct{}
|
type DeleteAccountParams struct {
|
||||||
type deleteAccountResult 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
|
var err error
|
||||||
res := deleteAccountResult{}
|
res := DeleteAccountResult{}
|
||||||
|
|
||||||
|
grpcConn, cli, err := client.NewClient(&util.access)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
defer grpcConn.Close()
|
||||||
|
operParams := &mbctl.DeleteAccountParams{
|
||||||
|
Username: params.Username,
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
_, err = cli.DeleteAccount(ctx, operParams)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,20 +4,40 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mbase/pkg/client"
|
||||||
|
"mbase/pkg/mbctl"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (util *Util) ListAccounts(cmd *cobra.Command, args []string) {
|
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)
|
printResponse(res, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type listAccountsParams struct{}
|
type ListAccountsParams struct {
|
||||||
type listAccountsResult 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
|
var err error
|
||||||
res := listAccountsResult{}
|
res := ListAccountsResult{}
|
||||||
|
|
||||||
|
grpcConn, cli, err := client.NewClient(&util.access)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
defer grpcConn.Close()
|
||||||
|
operParams := &mbctl.ListAccountsParams{}
|
||||||
|
ctx := context.Background()
|
||||||
|
operRes, err := cli.ListAccounts(ctx, operParams)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
res.Accounts = operRes.Accounts
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,20 +4,45 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mbase/pkg/client"
|
||||||
|
"mbase/pkg/mbctl"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (util *Util) UpdateAccount(cmd *cobra.Command, args []string) {
|
func (util *Util) UpdateAccount(cmd *cobra.Command, args []string) {
|
||||||
//util.updateAccountParams.Filename = args[0]
|
util.updateAccountParams.Username = args[0]
|
||||||
res, err := util.updateAccount(util.updateAccountParams)
|
res, err := util.updateAccount(util.updateAccountParams)
|
||||||
printResponse(res, err)
|
printResponse(res, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type updateAccountParams struct{}
|
type UpdateAccountParams struct {
|
||||||
type updateAccountResult struct{}
|
Username string
|
||||||
|
NewUsername string
|
||||||
|
NewPassword string
|
||||||
|
}
|
||||||
|
type UpdateAccountResult struct{}
|
||||||
|
|
||||||
func (util *Util) updateAccount(params updateAccountParams) (updateAccountResult, error) {
|
func (util *Util) updateAccount(params UpdateAccountParams) (UpdateAccountResult, error) {
|
||||||
var err error
|
var err error
|
||||||
res := updateAccountResult{}
|
res := UpdateAccountResult{}
|
||||||
|
|
||||||
|
grpcConn, cli, err := client.NewClient(&util.access)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
defer grpcConn.Close()
|
||||||
|
operParams := &mbctl.UpdateAccountParams{
|
||||||
|
Username: params.Username,
|
||||||
|
NewPassword: params.NewPassword,
|
||||||
|
NewUsername: params.NewUsername,
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
_, err = cli.UpdateAccount(ctx, operParams)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,64 +4,109 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"mbase/pkg/client"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Util struct {
|
type Util struct {
|
||||||
rootCmd *cobra.Command
|
subCmd *cobra.Command
|
||||||
createAccountParams createAccountParams
|
createAccountParams CreateAccountParams
|
||||||
listAccountsParams listAccountsParams
|
listAccountsParams ListAccountsParams
|
||||||
updateAccountParams updateAccountParams
|
updateAccountParams UpdateAccountParams
|
||||||
deleteAccountParams deleteAccountParams
|
deleteAccountParams DeleteAccountParams
|
||||||
|
commonParams CommonParams
|
||||||
|
access client.Access
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUtil() *Util {
|
func NewUtil() *Util {
|
||||||
return &Util{}
|
return &Util{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultHostname = "localhost"
|
||||||
|
defaultPort = client.DefaultPort
|
||||||
|
)
|
||||||
|
|
||||||
|
type CommonParams struct {
|
||||||
|
Hostname string
|
||||||
|
Port uint32
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
func (util *Util) GetRooCmd() *cobra.Command {
|
func (util *Util) GetRooCmd() *cobra.Command {
|
||||||
return util.rootCmd
|
return util.subCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (util *Util) BuildCmds() *cobra.Command {
|
func (util *Util) BuildCmds() *cobra.Command {
|
||||||
rootCmd := &cobra.Command{
|
subCmd := &cobra.Command{
|
||||||
Use: "account",
|
Use: "account",
|
||||||
Short: "\nAccount operations",
|
Short: "\nAccount operations",
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
}
|
}
|
||||||
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
subCmd.CompletionOptions.DisableDefaultCmd = true
|
||||||
|
subCmd.PersistentFlags().StringVarP(&util.commonParams.Hostname, "host", "H", defaultHostname, "Service hostname")
|
||||||
|
subCmd.PersistentFlags().Uint32VarP(&util.commonParams.Port, "port", "I", defaultPort, "Service port")
|
||||||
|
subCmd.PersistentFlags().StringVarP(&util.commonParams.Username, "user", "U", util.commonParams.Username, "Access username")
|
||||||
|
subCmd.PersistentFlags().StringVarP(&util.commonParams.Password, "pass", "P", util.commonParams.Password, "Access password")
|
||||||
|
subCmd.MarkFlagsRequiredTogether("user", "pass")
|
||||||
|
|
||||||
|
vi := viper.New()
|
||||||
|
vi.SetEnvPrefix("mstore")
|
||||||
|
vi.BindEnv("user")
|
||||||
|
vi.BindEnv("pass")
|
||||||
|
util.commonParams.Username = vi.GetString("user")
|
||||||
|
util.commonParams.Password = vi.GetString("pass")
|
||||||
|
|
||||||
var createAccountCmd = &cobra.Command{
|
var createAccountCmd = &cobra.Command{
|
||||||
Use: "create name password ",
|
Use: "create name password ",
|
||||||
Short: "Create account",
|
Short: "Create account",
|
||||||
Args: cobra.ExactArgs(2),
|
Args: cobra.ExactArgs(2),
|
||||||
Run: util.CreateAccount,
|
Run: util.CreateAccount,
|
||||||
|
PreRun: util.SetAccess,
|
||||||
}
|
}
|
||||||
rootCmd.AddCommand(createAccountCmd)
|
subCmd.AddCommand(createAccountCmd)
|
||||||
|
|
||||||
var listAccountsCmd = &cobra.Command{
|
var listAccountsCmd = &cobra.Command{
|
||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "List accounts",
|
Short: "List accounts",
|
||||||
Run: util.ListAccounts,
|
Run: util.ListAccounts,
|
||||||
|
PreRun: util.SetAccess,
|
||||||
}
|
}
|
||||||
rootCmd.AddCommand(listAccountsCmd)
|
subCmd.AddCommand(listAccountsCmd)
|
||||||
|
|
||||||
var updateAccountsCmd = &cobra.Command{
|
var updateAccountsCmd = &cobra.Command{
|
||||||
Use: "update name|id",
|
Use: "update name|id",
|
||||||
Short: "Update account",
|
Short: "Update account",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: util.UpdateAccount,
|
Run: util.UpdateAccount,
|
||||||
|
PreRun: util.SetAccess,
|
||||||
}
|
}
|
||||||
rootCmd.AddCommand(updateAccountsCmd)
|
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.NewPassword, "newpass", "N", util.updateAccountParams.NewPassword, "New password")
|
||||||
|
updateAccountsCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newname", "M", util.updateAccountParams.NewUsername, "New username")
|
||||||
|
updateAccountsCmd.MarkFlagsOneRequired("newpass", "newname")
|
||||||
|
subCmd.AddCommand(updateAccountsCmd)
|
||||||
|
|
||||||
var deleteAccountCmd = &cobra.Command{
|
var deleteAccountCmd = &cobra.Command{
|
||||||
Use: "delete name|id",
|
Use: "delete name|id",
|
||||||
Short: "Delete account",
|
Short: "Delete account",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: util.DeleteAccount,
|
Run: util.DeleteAccount,
|
||||||
|
PreRun: util.SetAccess,
|
||||||
}
|
}
|
||||||
rootCmd.AddCommand(deleteAccountCmd)
|
subCmd.AddCommand(deleteAccountCmd)
|
||||||
|
|
||||||
util.rootCmd = rootCmd
|
util.subCmd = subCmd
|
||||||
return util.rootCmd
|
return util.subCmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) SetAccess(cmd *cobra.Command, args []string) {
|
||||||
|
util.access = client.Access{
|
||||||
|
Hostname: util.commonParams.Hostname,
|
||||||
|
Port: util.commonParams.Port,
|
||||||
|
Username: util.commonParams.Username,
|
||||||
|
Password: util.commonParams.Password,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mbase/pkg/mbctl"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (util *Util) CreateAccount(ctx context.Context, operID string) (*mbctl.CreateAccountResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &mbctl.CreateAccountResult{}
|
||||||
|
|
||||||
|
params := &mbctl.CreateAccountParams{
|
||||||
|
Username: util.username,
|
||||||
|
Password: util.password,
|
||||||
|
}
|
||||||
|
res, err = util.lg.CreateAccount(ctx, operID, params)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) DeleteAccount(ctx context.Context, operID string) (*mbctl.DeleteAccountResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &mbctl.DeleteAccountResult{}
|
||||||
|
params := &mbctl.DeleteAccountParams{
|
||||||
|
Username: util.username,
|
||||||
|
AccountID: util.accountID,
|
||||||
|
}
|
||||||
|
res, err = util.lg.DeleteAccount(ctx, operID, params)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) ListAccounts(ctx context.Context, operID string) (*mbctl.ListAccountsResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &mbctl.ListAccountsResult{}
|
||||||
|
params := &mbctl.ListAccountsParams{}
|
||||||
|
res, err = util.lg.ListAccounts(ctx, operID, params)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) UpdateAccount(ctx context.Context, operID string) (*mbctl.UpdateAccountResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &mbctl.UpdateAccountResult{}
|
||||||
|
params := &mbctl.UpdateAccountParams{
|
||||||
|
Username: util.username,
|
||||||
|
AccountID: util.accountID,
|
||||||
|
NewUsername: util.newUsername,
|
||||||
|
NewPassword: util.newPassword,
|
||||||
|
}
|
||||||
|
res, err = util.lg.UpdateAccount(ctx, operID, params)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mbase/pkg/mbctl"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (util *Util) SetGrant(ctx context.Context, operID string) (*mbctl.SetGrantResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &mbctl.SetGrantResult{}
|
||||||
|
params := &mbctl.SetGrantParams{
|
||||||
|
Username: util.username,
|
||||||
|
AccountID: util.accountID,
|
||||||
|
Operation: util.operation,
|
||||||
|
}
|
||||||
|
res, err = util.lg.SetGrant(ctx, operID, params)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) DeleteGrant(ctx context.Context, operID string) (*mbctl.DeleteGrantResult, error) {
|
||||||
|
var err error
|
||||||
|
res := &mbctl.DeleteGrantResult{}
|
||||||
|
params := &mbctl.DeleteGrantParams{
|
||||||
|
Username: util.username,
|
||||||
|
AccountID: util.accountID,
|
||||||
|
Operation: util.operation,
|
||||||
|
}
|
||||||
|
res, err = util.lg.DeleteGrant(ctx, operID, params)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,467 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Oleg Borodin <borodin@unix7.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mbase/app/config"
|
||||||
|
"mbase/app/maindb"
|
||||||
|
"mbase/pkg/descr"
|
||||||
|
"mbase/app/logic"
|
||||||
|
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
rcFilename = ".certmanager.yaml"
|
||||||
|
|
||||||
|
helpCmd = "help"
|
||||||
|
|
||||||
|
createAccountCmd = "createAccount"
|
||||||
|
updateAccountCmd = "updateAccount"
|
||||||
|
deleteAccountCmd = "deleteAccount"
|
||||||
|
listAccountsCmd = "listAccounts"
|
||||||
|
|
||||||
|
setGrantCmd = "setGrant"
|
||||||
|
deleteGrantCmd = "deleteGrant"
|
||||||
|
|
||||||
|
initDatabaseCmd = "initDatabase"
|
||||||
|
seedAccountCmd = "seedAccount"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var err error
|
||||||
|
util := NewUtil()
|
||||||
|
err = util.Build()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Build error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
err = util.Exec()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Exec error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Util struct {
|
||||||
|
subCmd string
|
||||||
|
cmdTimeout int64
|
||||||
|
|
||||||
|
conf *config.Config
|
||||||
|
lg *logic.Logic
|
||||||
|
db *maindb.Database
|
||||||
|
state descr.Server
|
||||||
|
sfile string
|
||||||
|
|
||||||
|
accessUsername string
|
||||||
|
accessPassword string
|
||||||
|
accountID int64
|
||||||
|
username string
|
||||||
|
password string
|
||||||
|
disable bool
|
||||||
|
newUsername string
|
||||||
|
newPassword string
|
||||||
|
operation string
|
||||||
|
quiet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUtil() *Util {
|
||||||
|
var util Util
|
||||||
|
util.cmdTimeout = 120
|
||||||
|
return &util
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) GetOpt() error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
exeName := filepath.Base(os.Args[0])
|
||||||
|
|
||||||
|
flag.Int64Var(&util.cmdTimeout, "timeout", util.cmdTimeout, "command execution timeout")
|
||||||
|
flag.StringVar(&util.accessUsername, "user", util.accessUsername, "access login")
|
||||||
|
flag.StringVar(&util.accessPassword, "pass", util.accessPassword, "access password")
|
||||||
|
flag.BoolVar(&util.quiet, "quiet", util.quiet, "don't print result")
|
||||||
|
|
||||||
|
help := func() {
|
||||||
|
fmt.Println("")
|
||||||
|
fmt.Printf("Usage: %s [option] command [command option]\n", exeName)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf(" %s, %s, %s, %s,\n",
|
||||||
|
createAccountCmd,
|
||||||
|
deleteAccountCmd,
|
||||||
|
listAccountsCmd,
|
||||||
|
updateAccountCmd)
|
||||||
|
fmt.Printf(" %s, %s\n",
|
||||||
|
setGrantCmd,
|
||||||
|
deleteGrantCmd)
|
||||||
|
fmt.Printf(" %s, %s\n",
|
||||||
|
initDatabaseCmd,
|
||||||
|
seedAccountCmd)
|
||||||
|
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("Global options:\n")
|
||||||
|
flag.PrintDefaults()
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
flag.Usage = help
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
args := flag.Args()
|
||||||
|
|
||||||
|
var subCmd string
|
||||||
|
var subArgs []string
|
||||||
|
if len(args) > 0 {
|
||||||
|
subCmd = args[0]
|
||||||
|
subArgs = args[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
util.subCmd = subCmd
|
||||||
|
|
||||||
|
switch subCmd {
|
||||||
|
case helpCmd:
|
||||||
|
help()
|
||||||
|
return errors.New("Unknown command")
|
||||||
|
|
||||||
|
case createAccountCmd:
|
||||||
|
flagSet := flag.NewFlagSet(createAccountCmd, flag.ExitOnError)
|
||||||
|
|
||||||
|
flagSet.StringVar(&util.username, "username", util.username, "user name")
|
||||||
|
flagSet.StringVar(&util.password, "password", util.password, "user password")
|
||||||
|
|
||||||
|
flagSet.Usage = func() {
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("The command options: none\n")
|
||||||
|
flagSet.PrintDefaults()
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
flagSet.Parse(subArgs)
|
||||||
|
util.subCmd = subCmd
|
||||||
|
|
||||||
|
case deleteAccountCmd:
|
||||||
|
flagSet := flag.NewFlagSet(deleteAccountCmd, flag.ExitOnError)
|
||||||
|
|
||||||
|
flagSet.StringVar(&util.username, "username", util.username, "user name")
|
||||||
|
flagSet.Int64Var(&util.accountID, "accountId", util.accountID, "account ID")
|
||||||
|
|
||||||
|
flagSet.Usage = func() {
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("The command options: none\n")
|
||||||
|
flagSet.PrintDefaults()
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
flagSet.Parse(subArgs)
|
||||||
|
util.subCmd = subCmd
|
||||||
|
|
||||||
|
case listAccountsCmd:
|
||||||
|
flagSet := flag.NewFlagSet(listAccountsCmd, flag.ExitOnError)
|
||||||
|
|
||||||
|
flagSet.Usage = func() {
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("The command options: none\n")
|
||||||
|
flagSet.PrintDefaults()
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
flagSet.Parse(subArgs)
|
||||||
|
util.subCmd = subCmd
|
||||||
|
|
||||||
|
case updateAccountCmd:
|
||||||
|
flagSet := flag.NewFlagSet(updateAccountCmd, flag.ExitOnError)
|
||||||
|
|
||||||
|
flagSet.StringVar(&util.username, "username", util.username, "user name")
|
||||||
|
flagSet.Int64Var(&util.accountID, "accountId", util.accountID, "account ID")
|
||||||
|
|
||||||
|
flagSet.StringVar(&util.newUsername, "newUsername", util.newUsername, "new user name")
|
||||||
|
flagSet.StringVar(&util.newPassword, "newPassword", util.newPassword, "new user password")
|
||||||
|
flagSet.BoolVar(&util.disable, "disable", util.disable, "disable account")
|
||||||
|
|
||||||
|
flagSet.Usage = func() {
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("The command options: none\n")
|
||||||
|
flagSet.PrintDefaults()
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
flagSet.Parse(subArgs)
|
||||||
|
util.subCmd = subCmd
|
||||||
|
|
||||||
|
case setGrantCmd:
|
||||||
|
flagSet := flag.NewFlagSet(setGrantCmd, flag.ExitOnError)
|
||||||
|
|
||||||
|
flagSet.StringVar(&util.username, "username", util.username, "user name")
|
||||||
|
flagSet.Int64Var(&util.accountID, "accountId", util.accountID, "account ID")
|
||||||
|
flagSet.StringVar(&util.operation, "operation", util.operation, "grant type")
|
||||||
|
|
||||||
|
flagSet.Usage = func() {
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("The command options: none\n")
|
||||||
|
flagSet.PrintDefaults()
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
flagSet.Parse(subArgs)
|
||||||
|
util.subCmd = subCmd
|
||||||
|
|
||||||
|
case deleteGrantCmd:
|
||||||
|
flagSet := flag.NewFlagSet(deleteGrantCmd, flag.ExitOnError)
|
||||||
|
|
||||||
|
flagSet.StringVar(&util.username, "username", util.username, "user name")
|
||||||
|
flagSet.Int64Var(&util.accountID, "accountId", util.accountID, "account ID")
|
||||||
|
flagSet.StringVar(&util.operation, "operation", util.operation, "grant type")
|
||||||
|
|
||||||
|
flagSet.Usage = func() {
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("The command options: none\n")
|
||||||
|
flagSet.PrintDefaults()
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
flagSet.Parse(subArgs)
|
||||||
|
util.subCmd = subCmd
|
||||||
|
|
||||||
|
case initDatabaseCmd:
|
||||||
|
flagSet := flag.NewFlagSet(initDatabaseCmd, flag.ExitOnError)
|
||||||
|
|
||||||
|
flagSet.Usage = func() {
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("The command options: none\n")
|
||||||
|
flagSet.PrintDefaults()
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
flagSet.Parse(subArgs)
|
||||||
|
util.subCmd = subCmd
|
||||||
|
case seedAccountCmd:
|
||||||
|
flagSet := flag.NewFlagSet(seedAccountCmd, flag.ExitOnError)
|
||||||
|
|
||||||
|
flagSet.Usage = func() {
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
fmt.Printf("The command options: none\n")
|
||||||
|
flagSet.PrintDefaults()
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
flagSet.Parse(subArgs)
|
||||||
|
util.subCmd = subCmd
|
||||||
|
|
||||||
|
default:
|
||||||
|
help()
|
||||||
|
return errors.New("Unknown command")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) Build() error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
util.conf = config.NewConfig()
|
||||||
|
err = util.conf.ReadFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = util.conf.ReadEnv()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
util.sfile = filepath.Join(util.conf.DataDir, "certmanager.yaml")
|
||||||
|
|
||||||
|
db, err := maindb.NewDatabase(util.conf.DataDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = db.OpenDatabase()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
util.db = db
|
||||||
|
logicConfig := &logic.LogicConfig{
|
||||||
|
Database: util.db,
|
||||||
|
}
|
||||||
|
util.lg, err = logic.NewLogic(logicConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Command string `json:"command" yaml:"command"`
|
||||||
|
Args []string `json:"args" yaml:"args"`
|
||||||
|
Error bool `json:"error" yaml:"error"`
|
||||||
|
Message string `json:"message,omitempty" yaml:"message,omitempty"`
|
||||||
|
Result any `json:"result,omitempty" yaml:"result,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (util *Util) Exec() error {
|
||||||
|
var err error
|
||||||
|
err = util.GetOpt()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var timeout = time.Duration(util.cmdTimeout) * time.Second
|
||||||
|
ctx, close := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer close()
|
||||||
|
|
||||||
|
var res any
|
||||||
|
|
||||||
|
switch util.subCmd {
|
||||||
|
case initDatabaseCmd:
|
||||||
|
res, err = util.InitDatabase(ctx)
|
||||||
|
case seedAccountCmd:
|
||||||
|
res, err = util.SeedAccount(ctx)
|
||||||
|
default:
|
||||||
|
authOk, operID, localErr := util.lg.ValidateAcount(ctx, util.accessUsername, util.accessPassword)
|
||||||
|
if err != nil {
|
||||||
|
err = localErr
|
||||||
|
goto exit
|
||||||
|
}
|
||||||
|
if !authOk {
|
||||||
|
err = fmt.Errorf("Incorrect username or password")
|
||||||
|
goto exit
|
||||||
|
}
|
||||||
|
switch util.subCmd {
|
||||||
|
case createAccountCmd:
|
||||||
|
res, err = util.CreateAccount(ctx, operID)
|
||||||
|
case updateAccountCmd:
|
||||||
|
res, err = util.UpdateAccount(ctx, operID)
|
||||||
|
case listAccountsCmd:
|
||||||
|
res, err = util.ListAccounts(ctx, operID)
|
||||||
|
case deleteAccountCmd:
|
||||||
|
res, err = util.DeleteAccount(ctx, operID)
|
||||||
|
case setGrantCmd:
|
||||||
|
res, err = util.SetGrant(ctx, operID)
|
||||||
|
case deleteGrantCmd:
|
||||||
|
res, err = util.DeleteGrant(ctx, operID)
|
||||||
|
default:
|
||||||
|
err = errors.New("Unknown cli command")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exit:
|
||||||
|
var resp Response
|
||||||
|
resp.Command = util.subCmd
|
||||||
|
resp.Args = os.Args
|
||||||
|
if err != nil {
|
||||||
|
resp.Error = true
|
||||||
|
resp.Message = fmt.Sprintf("%v", err)
|
||||||
|
} else {
|
||||||
|
resp.Result = res
|
||||||
|
}
|
||||||
|
|
||||||
|
if !resp.Error && !util.quiet {
|
||||||
|
respBytes, _ := yaml.Marshal(resp)
|
||||||
|
fmt.Println(string(respBytes))
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type InitDatabaseRes struct{}
|
||||||
|
|
||||||
|
func (util *Util) InitDatabase(ctx context.Context) (InitDatabaseRes, error) {
|
||||||
|
res := InitDatabaseRes{}
|
||||||
|
// Initialize database
|
||||||
|
|
||||||
|
// Load state
|
||||||
|
err := util.LoadState()
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
if !util.state.DatabaseInitialized {
|
||||||
|
if util.db == nil {
|
||||||
|
err = fmt.Errorf("Nil db object")
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
err := util.db.InitDatabase()
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
util.state.DatabaseInitialized = true
|
||||||
|
err = util.SaveState()
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type SeedAccountRes struct{}
|
||||||
|
|
||||||
|
func (util *Util) SeedAccount(ctx context.Context) (SeedAccountRes, error) {
|
||||||
|
// Seed accounts
|
||||||
|
res := SeedAccountRes{}
|
||||||
|
_, err := util.lg.SeedAccount(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
return res, 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 = time.Now().Format(time.RFC3339)
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"mbase/cmd/mbaseadmin/account"
|
"mbase/cmd/mbasectl/account"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
package auxid
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math/rand"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
idMtx sync.Mutex
|
|
||||||
lastID int64
|
|
||||||
)
|
|
||||||
|
|
||||||
func GenID() int64 {
|
|
||||||
// 53 bit limit for js
|
|
||||||
// See https://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript
|
|
||||||
idMtx.Lock()
|
|
||||||
defer idMtx.Unlock()
|
|
||||||
for {
|
|
||||||
id := (time.Now().UnixNano() / 1000) // - 10000000000000
|
|
||||||
if id != lastID {
|
|
||||||
lastID = id
|
|
||||||
return id
|
|
||||||
}
|
|
||||||
time.Sleep(1 * time.Microsecond)
|
|
||||||
}
|
|
||||||
//10467328383814
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user