From e1ca9b1b4c22f5d2f72b84a6edec93317a044feb 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: Wed, 18 Feb 2026 16:28:02 +0200 Subject: [PATCH] working commit --- app/operator/account.go | 24 +--------- app/operator/grant.go | 28 +++++++++++- cmd/mstorectl/accountcmd.go | 87 +++++++++++++++---------------------- cmd/mstorectl/grantcmd.go | 74 ++++++++++++++++--------------- cmd/mstorectl/main.go | 6 +-- go.mod | 13 +++++- go.sum | 23 ++++++++++ pkg/client/grant.go | 45 +++++++++++++++++-- 8 files changed, 183 insertions(+), 117 deletions(-) diff --git a/app/operator/account.go b/app/operator/account.go index 8f90fa0..5b8b15e 100644 --- a/app/operator/account.go +++ b/app/operator/account.go @@ -121,17 +121,7 @@ func (oper *Operator) GetAccount(ctx context.Context, params *GetAccountParams) if err != nil { return res, err } - for _, grantDescrs := range grantDescrs { - grantShorts := descr.Grant{ - Right: grantDescrs.Right, - Pattern: grantDescrs.Pattern, - CreatedAt: grantDescrs.CreatedAt, - UpdatedAt: grantDescrs.UpdatedAt, - CreatedBy: grantDescrs.CreatedBy, - UpdatedBy: grantDescrs.UpdatedBy, - } - accountShort.Grants = append(accountShort.Grants, grantShorts) - } + accountShort.Grants = grantDescrs res.Account = accountShort return res, err @@ -289,17 +279,7 @@ func (oper *Operator) ListAccounts(ctx context.Context, params *ListAccountsPara if err != nil { return res, err } - for _, grantDescrs := range grantDescrs { - grantShorts := descr.Grant{ - Right: grantDescrs.Right, - Pattern: grantDescrs.Pattern, - CreatedAt: grantDescrs.CreatedAt, - UpdatedAt: grantDescrs.UpdatedAt, - CreatedBy: grantDescrs.CreatedBy, - UpdatedBy: grantDescrs.UpdatedBy, - } - accountShort.Grants = append(accountShort.Grants, grantShorts) - } + accountShort.Grants = grantDescrs res.Accounts = append(res.Accounts, accountShort) } return res, err diff --git a/app/operator/grant.go b/app/operator/grant.go index f86d9c2..0be8e7e 100644 --- a/app/operator/grant.go +++ b/app/operator/grant.go @@ -13,6 +13,7 @@ import ( // CreateGrant type CreateGrantParams struct { AccountID string `json:"accountID"` + Username string `json:"username"` Right string `json:"operation"` Pattern string `json:"pattern"` } @@ -43,6 +44,31 @@ func (oper *Operator) CreateGrant(ctx context.Context, operID string, params *Cr return res, err } + var accountDescr *descr.Account + var accountExists bool + switch { + case params.AccountID != "": + accountExists, accountDescr, err = oper.mdb.GetAccountByID(ctx, params.AccountID) + if err != nil { + return res, err + } + if !accountExists { + err := fmt.Errorf("Account with ID %s dont exists", params.AccountID) + return res, err + } + case params.Username != "": + accountExists, accountDescr, err = oper.mdb.GetAccountByUsername(ctx, params.Username) + if err != nil { + return res, err + } + if !accountExists { + err := fmt.Errorf("Account with name %s dont exists", params.Username) + return res, err + } + default: + err := fmt.Errorf("Empty username and accountId parameter") + return res, err + } grantExists, _, err := oper.mdb.GetGrantByAccoundIDRightPattern(ctx, params.AccountID, params.Right, params.Pattern) if err != nil { return res, err @@ -55,7 +81,7 @@ func (oper *Operator) CreateGrant(ctx context.Context, operID string, params *Cr now := auxtool.TimeNow() grantDescr := &descr.Grant{ ID: auxuuid.NewUUID(), - AccountID: params.AccountID, + AccountID: accountDescr.ID, Right: params.Right, Pattern: params.Pattern, CreatedAt: now, diff --git a/cmd/mstorectl/accountcmd.go b/cmd/mstorectl/accountcmd.go index d073368..6a9527d 100644 --- a/cmd/mstorectl/accountcmd.go +++ b/cmd/mstorectl/accountcmd.go @@ -16,7 +16,6 @@ import ( "time" "github.com/spf13/cobra" - "github.com/spf13/viper" "mstore/app/descr" "mstore/pkg/client" @@ -55,75 +54,59 @@ func (util *AccountUtil) CreateAccountCmds() *cobra.Command { subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Password, "pass", "p", "", "Password") subCmd.PersistentFlags().StringVarP(&util.commonAccountParams.Hostname, "host", "x", defaultHostname, "Hostname") subCmd.PersistentFlags().Uint64VarP(&util.commonAccountParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") - subCmd.MarkPersistentFlagRequired("host") subCmd.MarkFlagsRequiredTogether("user", "pass") // CreateAccount var createAccountCmd = &cobra.Command{ - Use: "create", + Use: "create host username password", Short: "Create user account", + Args: cobra.ExactArgs(3), Run: util.CreateAccount, } - createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewUsername, "newuser", "U", "", "New account username") - createAccountCmd.Flags().StringVarP(&util.createAccountParams.NewPassword, "newpass", "P", "", "New account password") - createAccountCmd.MarkFlagsRequiredTogether("newuser", "newpass") subCmd.AddCommand(createAccountCmd) // GetAccount var getAccountCmd = &cobra.Command{ - Use: "get", + Use: "get hostname accountId|username", Short: "Get account info", + Args: cobra.ExactArgs(2), Run: util.GetAccount, } - getAccountCmd.Flags().StringVarP(&util.getAccountParams.AccountID, "id", "I", "", "Account ID or name") - getAccountCmd.Flags().StringVarP(&util.getAccountParams.AccountID, "name", "n", "", "Account ID or name") - getAccountCmd.MarkFlagsOneRequired("id", "name") subCmd.AddCommand(getAccountCmd) // UpdateAccount var updateAccountCmd = &cobra.Command{ - Use: "update", + Use: "update hostname username|accounId", Short: "Update account parameters", + Args: cobra.ExactArgs(2), Run: util.UpdateAccount, } - updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "id", "I", "", "Account ID or username") - updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "name", "n", "", "Account ID or username") updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewUsername, "newname", "N", "", "New username") updateAccountCmd.Flags().StringVarP(&util.updateAccountParams.NewPassword, "newpass", "P", "", "New password") - updateAccountCmd.MarkFlagsOneRequired("id", "name") updateAccountCmd.MarkFlagsOneRequired("newname", "newpass") subCmd.AddCommand(updateAccountCmd) // DeleteAccount var deleteAccountCmd = &cobra.Command{ - Use: "delete", + Use: "delete hostname username|accountId", Short: "Delete account", + Args: cobra.ExactArgs(2), Run: util.DeleteAccount, } - deleteAccountCmd.Flags().StringVarP(&util.deleteAccountParams.AccountID, "id", "I", "", "Account ID") deleteAccountCmd.Flags().StringVarP(&util.updateAccountParams.AccountID, "name", "n", "", "Account ID or username") - deleteAccountCmd.MarkFlagsOneRequired("id", "name") subCmd.AddCommand(deleteAccountCmd) // ListAccount var listAccountsCmd = &cobra.Command{ - Use: "list", + Use: "list hostname", Short: "list accounts", + Args: cobra.ExactArgs(1), Run: util.ListAccounts, } listAccountsCmd.Flags().BoolVarP(&util.listAccountsParams.Detail, "detail", "d", false, "Show detail information") listAccountsCmd.Flags().StringVarP(&util.listAccountsParams.Regex, "regex", "r", "", "Output regexp for usernames") - listAccountsCmd.MarkFlagRequired("host") subCmd.AddCommand(listAccountsCmd) - viper.BindPFlags(subCmd.Flags()) - viper.SetEnvPrefix("MSTORE") - - // Bind environment variables - viper.BindEnv("user") - viper.BindEnv("pass") - viper.BindEnv("host") - return subCmd } @@ -133,11 +116,14 @@ type CreateAccountParams struct { NewPassword string } type CreateAccountResult struct { - AccountID string `yaml:"accountId"` - GrantIDs []string `yaml:"grantsIds,omitempty"` + AccountID string `yaml:"accountId"` + Grants map[string]string `yaml:"grantsIds,omitempty"` } func (util *AccountUtil) CreateAccount(cmd *cobra.Command, args []string) { + util.commonAccountParams.Hostname = args[0] + util.createAccountParams.NewUsername = args[1] + util.createAccountParams.NewPassword = args[2] res, err := util.createAccount(&util.commonAccountParams, &util.createAccountParams) printResponse(res, err) } @@ -145,7 +131,7 @@ func (util *AccountUtil) CreateAccount(cmd *cobra.Command, args []string) { func (util *AccountUtil) createAccount(common *CommonAccountParams, params *CreateAccountParams) (*CreateAccountResult, error) { var err error res := &CreateAccountResult{ - GrantIDs: make([]string, 0), + Grants: make(map[string]string, 0), } hostname, err := packUserinfo(common.Hostname, common.Username, common.Password) if err != nil { @@ -167,11 +153,11 @@ func (util *AccountUtil) createAccount(common *CommonAccountParams, params *Crea descr.RightReadImages, } for _, right := range fullRights { - id, err := client.NewClient().CreateGrant(ctx, hostname, accountID, right, ".*") + id, err := client.NewClient().CreateGrantByAccountID(ctx, hostname, accountID, right, ".*") if err != nil { return res, err } - res.GrantIDs = append(res.GrantIDs, id) + res.Grants[id] = right } res.AccountID = accountID return res, err @@ -179,9 +165,6 @@ func (util *AccountUtil) createAccount(common *CommonAccountParams, params *Crea // UpdateAccount type UpdateAccountParams struct { - Hostname string - Username string - Password string Timeout uint64 AccountID string NewUsername string @@ -192,6 +175,8 @@ type UpdateAccountResult struct { } func (util *AccountUtil) UpdateAccount(cmd *cobra.Command, args []string) { + util.commonAccountParams.Hostname = args[0] + util.updateAccountParams.AccountID = args[1] res, err := util.updateAccount(&util.commonAccountParams, &util.updateAccountParams) printResponse(res, err) } @@ -220,14 +205,13 @@ func (util *AccountUtil) updateAccount(common *CommonAccountParams, params *Upda // Get file type GetAccountParams struct { - Hostname string - Username string - Password string Timeout uint64 AccountID string } func (util *AccountUtil) GetAccount(cmd *cobra.Command, args []string) { + util.commonAccountParams.Hostname = args[0] + util.getAccountParams.AccountID = args[1] res, err := util.getAccount(&util.commonAccountParams, &util.getAccountParams) printResponse(res, err) } @@ -263,9 +247,6 @@ func (util *AccountUtil) getAccount(common *CommonAccountParams, params *GetAcco // DeleteAccount type DeleteAccountParams struct { - Hostname string - Username string - Password string AccountID string Timeout uint64 } @@ -273,6 +254,8 @@ type DeleteAccountParams struct { type DeleteAccountResult struct{} func (util *AccountUtil) DeleteAccount(cmd *cobra.Command, args []string) { + util.commonAccountParams.Hostname = args[0] + util.deleteAccountParams.AccountID = args[1] res, err := util.deleteAccount(&util.commonAccountParams, &util.deleteAccountParams) printResponse(res, err) } @@ -300,17 +283,15 @@ func (util *AccountUtil) deleteAccount(common *CommonAccountParams, params *Dele // ListAccounts type ListAccountsParams struct { - Hostname string - Username string - Password string - Timeout uint64 - Detail bool - Regex string + Timeout uint64 + Detail bool + Regex string } type Userinfo struct { - Username string `yaml:"username,omitempty"` - Rights []string `yaml:"rights,omitempty"` + Username string `yaml:"username,omitempty"` + AccountID string `yaml:"accountId,omitempty"` + Rights map[string]string `yaml:"rights,omitempty"` } type ListAccountsResult struct { @@ -319,6 +300,7 @@ type ListAccountsResult struct { } func (util *AccountUtil) ListAccounts(cmd *cobra.Command, args []string) { + util.commonAccountParams.Hostname = args[0] res, err := util.listAccounts(&util.commonAccountParams, &util.listAccountsParams) printResponse(res, err) } @@ -356,11 +338,12 @@ func (util *AccountUtil) listAccounts(common *CommonAccountParams, params *ListA res.Users = make([]Userinfo, 0) for _, account := range outAccounts { userinfo := Userinfo{ - Username: account.Username, - Rights: make([]string, 0), + Username: account.Username, + AccountID: account.ID, + Rights: make(map[string]string, 0), } for _, grant := range account.Grants { - userinfo.Rights = append(userinfo.Rights, grant.Right) + userinfo.Rights[grant.ID] = grant.Right } res.Users = append(res.Users, userinfo) } diff --git a/cmd/mstorectl/grantcmd.go b/cmd/mstorectl/grantcmd.go index f03e605..b341855 100644 --- a/cmd/mstorectl/grantcmd.go +++ b/cmd/mstorectl/grantcmd.go @@ -31,69 +31,53 @@ func (util *GrantUtil) CreateGrantCmds() *cobra.Command { subCmd.PersistentFlags().StringVarP(&util.commonGrantParams.Username, "user", "u", "", "Username") subCmd.PersistentFlags().StringVarP(&util.commonGrantParams.Password, "pass", "p", "", "Password") - subCmd.PersistentFlags().StringVarP(&util.commonGrantParams.Hostname, "host", "x", defaultHostname, "Hostname") subCmd.PersistentFlags().Uint64VarP(&util.commonGrantParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout") - subCmd.MarkPersistentFlagRequired("host") - subCmd.MarkFlagsRequiredTogether("user", "pass") // CreateGrant var createGrantCmd = &cobra.Command{ - Use: "create", + Use: "create hostname[:port] username|accountId rigth pattern", Short: "Create grant", + Args: cobra.ExactArgs(4), Run: util.CreateGrant, } - createGrantCmd.Flags().StringVarP(&util.createGrantParams.Right, "newuser", "U", "", "New account username") - createGrantCmd.Flags().StringVarP(&util.createGrantParams.Pattern, "newpass", "P", "", "New account password") - createGrantCmd.MarkFlagsRequiredTogether("newuser", "newpass") subCmd.AddCommand(createGrantCmd) // GetGrant var getGrantCmd = &cobra.Command{ - Use: "get", + Use: "get hostname[:port] grantId", Short: "Get detail grant info", + Args: cobra.ExactArgs(2), Run: util.GetGrant, } - getGrantCmd.Flags().StringVarP(&util.getGrantParams.GrantID, "id", "I", "", "Grant ID or name") - getGrantCmd.Flags().StringVarP(&util.getGrantParams.GrantID, "name", "n", "", "Grant ID or name") - getGrantCmd.MarkFlagsOneRequired("id", "name") subCmd.AddCommand(getGrantCmd) // UpdateGrant var updateGrantCmd = &cobra.Command{ - Use: "update", + Use: "update hostname[:port] gruntId newPattern", Short: "Update grant parameters", - Run: util.UpdateGrant, + Args: cobra.ExactArgs(3), + Run: util.UpdateGrant, } - updateGrantCmd.Flags().StringVarP(&util.updateGrantParams.GrantID, "id", "I", "", "Grant ID or username") - updateGrantCmd.Flags().StringVarP(&util.updateGrantParams.GrantID, "name", "n", "", "Grant ID or username") - updateGrantCmd.Flags().StringVarP(&util.updateGrantParams.Right, "rigth", "U", "", "Rigth") - updateGrantCmd.Flags().StringVarP(&util.updateGrantParams.Pattern, "pattern", "P", "", "New pattern") - updateGrantCmd.MarkFlagsOneRequired("id", "name") - updateGrantCmd.MarkFlagRequired("rigth") - updateGrantCmd.MarkFlagRequired("pattern") subCmd.AddCommand(updateGrantCmd) // DeleteGrant var deleteGrantCmd = &cobra.Command{ - Use: "delete", + Use: "delete hostname[:port] gruntId ", Short: "Delete grant", - Run: util.DeleteGrant, + Args: cobra.ExactArgs(2), + + Run: util.DeleteGrant, } - deleteGrantCmd.Flags().StringVarP(&util.deleteGrantParams.GrantID, "id", "I", "", "Grant ID") - deleteGrantCmd.MarkFlagRequired("id") subCmd.AddCommand(deleteGrantCmd) // ListGrants var listGrantsCmd = &cobra.Command{ - Use: "list", + Use: "list hostname[:port] accountId|username", Short: "list user grants", + Args: cobra.ExactArgs(2), Run: util.ListGrants, } listGrantsCmd.Flags().BoolVarP(&util.listGrantsParams.Detail, "detail", "d", false, "Show detail information") - listGrantsCmd.Flags().StringVarP(&util.listGrantsParams.AccountID, "id", "I", "", "User account ID") - listGrantsCmd.Flags().StringVarP(&util.listGrantsParams.AccountID, "name", "n", "", "User account ID") - listGrantsCmd.MarkFlagRequired("host") - listGrantsCmd.MarkFlagsOneRequired("id", "name") subCmd.AddCommand(listGrantsCmd) @@ -127,6 +111,10 @@ type CreateGrantResult struct { } func (util *GrantUtil) CreateGrant(cmd *cobra.Command, args []string) { + util.commonGrantParams.Hostname = args[0] + util.createGrantParams.AccountID = args[1] + util.createGrantParams.Right = args[2] + util.createGrantParams.Pattern = args[3] res, err := util.createGrant(&util.commonGrantParams, &util.createGrantParams) printResponse(res, err) } @@ -140,23 +128,32 @@ func (util *GrantUtil) createGrant(common *CommonGrantParams, params *CreateGran } timeout := time.Duration(common.Timeout) * time.Second ctx, _ := context.WithTimeout(context.Background(), timeout) - accountID, err := client.NewClient().CreateGrant(ctx, hostname, params.AccountID, params.Right, params.Pattern) + re := regexp.MustCompile(uuidRegex) + id := strings.ToLower(params.AccountID) + var operRes string + if re.MatchString(id) { + operRes, err = client.NewClient().CreateGrantByAccountID(ctx, hostname, params.AccountID, params.Right, params.Pattern) + } else { + operRes, err = client.NewClient().CreateGrantByUsername(ctx, hostname, params.AccountID, params.Right, params.Pattern) + } if err != nil { return res, err } - res.GrantID = accountID + res.GrantID = operRes return res, err } // UpdateGrant type UpdateGrantParams struct { GrantID string - Right string Pattern string } type UpdateGrantResult struct{} func (util *GrantUtil) UpdateGrant(cmd *cobra.Command, args []string) { + util.commonGrantParams.Hostname = args[0] + util.updateGrantParams.GrantID = args[1] + util.updateGrantParams.Pattern = args[2] res, err := util.updateGrant(&util.commonGrantParams, &util.updateGrantParams) printResponse(res, err) } @@ -188,6 +185,9 @@ type GetGrantResult struct { } func (util *GrantUtil) GetGrant(cmd *cobra.Command, args []string) { + util.commonGrantParams.Hostname = args[0] + util.getGrantParams.GrantID = args[1] + res, err := util.getGrant(&util.commonGrantParams, &util.getGrantParams) printResponse(res, err) } @@ -220,6 +220,8 @@ type DeleteGrantParams struct { type DeleteGrantResult struct{} func (util *GrantUtil) DeleteGrant(cmd *cobra.Command, args []string) { + util.commonGrantParams.Hostname = args[0] + util.deleteGrantParams.GrantID = args[1] res, err := util.deleteGrant(&util.commonGrantParams, &util.deleteGrantParams) printResponse(res, err) } @@ -248,10 +250,12 @@ type ListGrantsParams struct { type ListGrantsResult struct { Grants []descr.Grant `yaml:"grants,omitempty"` - Rights []string `yaml:"rights,omitempty"` + Rights map[string]string `yaml:"rights,omitempty"` } func (util *GrantUtil) ListGrants(cmd *cobra.Command, args []string) { + util.commonGrantParams.Hostname = args[0] + util.listGrantsParams.AccountID = args[1] res, err := util.listGrants(&util.commonGrantParams, &util.listGrantsParams) printResponse(res, err) } @@ -278,9 +282,9 @@ func (util *GrantUtil) listGrants(common *CommonGrantParams, params *ListGrantsP if params.Detail { res.Grants = grants } else { - res.Rights = make([]string, 0) + res.Rights = make(map[string]string, 0) for _, item := range grants { - res.Rights = append(res.Rights, item.Right) + res.Rights[item.ID] = item.Right } } return res, err diff --git a/cmd/mstorectl/main.go b/cmd/mstorectl/main.go index 25a6bed..061c0d9 100644 --- a/cmd/mstorectl/main.go +++ b/cmd/mstorectl/main.go @@ -47,9 +47,9 @@ func (util *Util) Build() error { var err error execName := filepath.Base(os.Args[0]) rootCmd := cobra.Command{ - Use: execName, - Short: "\nA brief description the command", - SilenceUsage: true, + Use: execName, + Short: "\nA brief description the command", + //SilenceUsage: true, } rootCmd.CompletionOptions.DisableDefaultCmd = true diff --git a/go.mod b/go.mod index 5d8efdf..98eda47 100644 --- a/go.mod +++ b/go.mod @@ -20,14 +20,25 @@ require ( github.com/docker/cli v29.0.3+incompatible // indirect github.com/docker/distribution v2.8.3+incompatible // indirect github.com/docker/docker-credential-helpers v0.9.3 // indirect + github.com/fsnotify/fsnotify v1.9.0 // indirect + github.com/go-viper/mapstructure/v2 v2.4.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/klauspost/compress v1.18.1 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/sagikazarmark/locafero v0.11.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect - github.com/spf13/pflag v1.0.9 // indirect + github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect + github.com/spf13/afero v1.15.0 // indirect + github.com/spf13/cast v1.10.0 // indirect + github.com/spf13/pflag v1.0.10 // indirect + github.com/spf13/viper v1.21.0 // indirect + github.com/subosito/gotenv v1.6.0 // indirect github.com/vbatts/tar-split v0.12.2 // indirect + go.yaml.in/yaml/v3 v3.0.4 // indirect golang.org/x/sync v0.18.0 // indirect golang.org/x/sys v0.38.0 // indirect + golang.org/x/text v0.28.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index d169018..0e0cde0 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,12 @@ github.com/docker/distribution v2.8.3+incompatible h1:AtKxIZ36LoNK51+Z6RpzLpddBi github.com/docker/distribution v2.8.3+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= github.com/docker/docker-credential-helpers v0.9.3 h1:gAm/VtF9wgqJMoxzT3Gj5p4AqIjCBS4wrsOh9yRqcz8= github.com/docker/docker-credential-helpers v0.9.3/go.mod h1:x+4Gbw9aGmChi3qTLZj8Dfn0TD20M/fuWy0E5+WDeCo= +github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= +github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= +github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs= +github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/go-containerregistry v0.20.7 h1:24VGNpS0IwrOZ2ms2P1QE3Xa5X9p4phx0aUgzYzHW6I= @@ -37,23 +41,40 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8 github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040= github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc= +github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw= +github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U= +github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I= +github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg= +github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY= +github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= +github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU= +github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/vbatts/tar-split v0.12.2 h1:w/Y6tjxpeiFMR47yzZPlPj/FcPLpXbTUi/9H7d3CPa4= github.com/vbatts/tar-split v0.12.2/go.mod h1:eF6B6i6ftWQcDqEn3/iGFRFRo8cBIMSJVOpnNdfTMFA= +go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= go.yaml.in/yaml/v4 v4.0.0-rc.4 h1:UP4+v6fFrBIb1l934bDl//mmnoIZEDK0idg1+AIvX5U= go.yaml.in/yaml/v4 v4.0.0-rc.4/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0= @@ -62,6 +83,8 @@ golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng= +golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/client/grant.go b/pkg/client/grant.go index 5afebc0..cb7b626 100644 --- a/pkg/client/grant.go +++ b/pkg/client/grant.go @@ -19,7 +19,7 @@ import ( "mstore/app/operator" ) -func (cli *Client) CreateGrant(ctx context.Context, hosturi, accountID, right, pattern string) (string, error) { +func (cli *Client) CreateGrantByAccountID(ctx context.Context, hosturi, accountID, right, pattern string) (string, error) { var err error var res string @@ -54,6 +54,43 @@ func (cli *Client) CreateGrant(ctx context.Context, hosturi, accountID, right, p return res, err } + +func (cli *Client) CreateGrantByUsername(ctx context.Context, hosturi, username, right, pattern string) (string, error) { + var err error + var res string + + apiuri, err := setApiPath(hosturi, "/v3/api/grant/create") + if err != nil { + return res, err + } + operParams := operator.CreateGrantParams{ + Username: username, + Right: right, + Pattern: pattern, + } + paramsJson, err := json.Marshal(operParams) + if err != nil { + return res, err + } + respBytes, err := doHTTPCall(ctx, apiuri, paramsJson) + if err != nil { + return res, err + } + + operRes := handler.NewResponse[operator.CreateGrantResult]() + err = json.Unmarshal(respBytes, operRes) + if err != nil { + return res, err + } + if operRes.Error { + err = fmt.Errorf("%s", operRes.Message) + return res, err + } + res = operRes.Result.GrantID + return res, err +} + + func (cli *Client) GetGrant(ctx context.Context, hosturi, id string) (*descr.Grant, error) { var err error res := &descr.Grant{} @@ -87,6 +124,8 @@ func (cli *Client) GetGrant(ctx context.Context, hosturi, id string) (*descr.Gra return res, err } + + func (cli *Client) UpdateGrant(ctx context.Context, hosturi, grantID, newPattern string) error { var err error @@ -153,7 +192,7 @@ func (cli *Client) ListGrantsByAccountID(ctx context.Context, hosturi, accountID var err error res := make([]descr.Grant, 0) - apipath, err := setApiPath(hosturi, "/v3/api/grant/delete") + apipath, err := setApiPath(hosturi, "/v3/api/grants/list") if err != nil { return res, err } @@ -186,7 +225,7 @@ func (cli *Client) ListGrantsByUsername(ctx context.Context, hosturi, username s var err error res := make([]descr.Grant, 0) - apipath, err := setApiPath(hosturi, "/v3/api/grant/delete") + apipath, err := setApiPath(hosturi, "/v3/api/grants/list") if err != nil { return res, err }