Files
mstore/cmd/mstorectl/grantcmd.go
T
2026-02-15 11:54:32 +02:00

288 lines
8.7 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package main
import (
"context"
"regexp"
"strings"
"time"
"github.com/spf13/cobra"
"mstore/app/descr"
"mstore/pkg/client"
)
func (util *GrantUtil) CreateGrantCmds() *cobra.Command {
var subCmd = &cobra.Command{
Use: "grants",
Short: "Grant operation",
Aliases: []string{"grant"},
}
const defaultTimeout uint64 = 10
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",
Short: "Create grant",
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",
Short: "Get detail grant info",
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",
Short: "Update grant parameters",
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",
Short: "Delete grant",
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",
Short: "list user grants",
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)
return subCmd
}
type GrantUtil struct {
createGrantParams CreateGrantParams
updateGrantParams UpdateGrantParams
getGrantParams GetGrantParams
deleteGrantParams DeleteGrantParams
listGrantsParams ListGrantsParams
commonGrantParams CommonGrantParams
}
type CommonGrantParams struct {
Username string
Password string
Hostname string
Timeout uint64
}
// CreateGrant
type CreateGrantParams struct {
AccountID string
Right string
Pattern string
}
type CreateGrantResult struct {
GrantID string `json:"grantId"`
}
func (util *GrantUtil) CreateGrant(cmd *cobra.Command, args []string) {
res, err := util.createGrant(&util.commonGrantParams, &util.createGrantParams)
printResponse(res, err)
}
func (util *GrantUtil) createGrant(common *CommonGrantParams, params *CreateGrantParams) (*CreateGrantResult, error) {
var err error
res := &CreateGrantResult{}
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
return res, err
}
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)
if err != nil {
return res, err
}
res.GrantID = accountID
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) {
res, err := util.updateGrant(&util.commonGrantParams, &util.updateGrantParams)
printResponse(res, err)
}
func (util *GrantUtil) updateGrant(common *CommonGrantParams, params *UpdateGrantParams) (*UpdateGrantResult, error) {
var err error
res := &UpdateGrantResult{}
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
return res, err
}
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
id := strings.ToLower(params.GrantID)
err = client.NewClient().UpdateGrant(ctx, hostname, id, params.Pattern)
if err != nil {
return res, err
}
return res, err
}
// GetGrant
type GetGrantParams struct {
GrantID string
}
type GetGrantResult struct {
Grant *descr.Grant `json:"grant,omitempty"`
}
func (util *GrantUtil) GetGrant(cmd *cobra.Command, args []string) {
res, err := util.getGrant(&util.commonGrantParams, &util.getGrantParams)
printResponse(res, err)
}
func (util *GrantUtil) getGrant(common *CommonGrantParams, params *GetGrantParams) (*GetGrantResult, error) {
var err error
res := &GetGrantResult{}
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
return res, err
}
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
opRes := &descr.Grant{}
id := strings.ToLower(params.GrantID)
opRes, err = client.NewClient().GetGrant(ctx, hostname, id)
if err != nil {
return res, err
}
res.Grant = opRes
return res, err
}
// DeleteGrant
type DeleteGrantParams struct {
GrantID string
}
type DeleteGrantResult struct{}
func (util *GrantUtil) DeleteGrant(cmd *cobra.Command, args []string) {
res, err := util.deleteGrant(&util.commonGrantParams, &util.deleteGrantParams)
printResponse(res, err)
}
func (util *GrantUtil) deleteGrant(common *CommonGrantParams, params *DeleteGrantParams) (*DeleteGrantResult, error) {
var err error
res := &DeleteGrantResult{}
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
return res, err
}
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
id := strings.ToLower(params.GrantID)
err = client.NewClient().DeleteGrant(ctx, hostname, id)
if err != nil {
return res, err
}
return res, err
}
// ListGrants
type ListGrantsParams struct {
Detail bool
AccountID string
}
type ListGrantsResult struct {
Grants []descr.Grant `json:"grants,omitempty"`
Rights []string `json:"rights,omitempty"`
}
func (util *GrantUtil) ListGrants(cmd *cobra.Command, args []string) {
res, err := util.listGrants(&util.commonGrantParams, &util.listGrantsParams)
printResponse(res, err)
}
func (util *GrantUtil) listGrants(common *CommonGrantParams, params *ListGrantsParams) (*ListGrantsResult, error) {
var err error
res := &ListGrantsResult{}
hostname, err := packUserinfo(common.Hostname, common.Username, common.Password)
if err != nil {
return res, err
}
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
grants := make([]descr.Grant, 0)
re := regexp.MustCompile(uuidRegex)
id := strings.ToLower(params.AccountID)
if re.MatchString(id) {
grants, err = client.NewClient().ListGrantsByAccountID(ctx, hostname, params.AccountID)
} else {
grants, err = client.NewClient().ListGrantsByUsername(ctx, hostname, params.AccountID)
}
if err != nil {
return res, err
}
if params.Detail {
res.Grants = grants
} else {
res.Rights = make([]string, 0)
for _, item := range grants {
res.Rights = append(res.Rights, item.Right)
}
}
return res, err
}