2023-10-31 08:55:42 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2024-03-11 14:55:50 +00:00
|
|
|
"errors"
|
2024-01-12 14:44:27 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-10-31 08:55:42 +00:00
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
2024-01-25 17:25:23 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/util"
|
2023-10-31 08:55:42 +00:00
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
|
2024-11-07 14:32:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/client"
|
2023-10-31 08:55:42 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-11-16 07:58:55 +00:00
|
|
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
2024-01-12 14:44:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
2023-10-31 08:55:42 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var listRulesCmd = &cobra.Command{
|
|
|
|
Use: "list-rules",
|
|
|
|
Short: "List local overrides",
|
|
|
|
Long: "List local APE overrides of the node",
|
|
|
|
Run: listRules,
|
|
|
|
}
|
|
|
|
|
2024-01-12 14:44:27 +00:00
|
|
|
const (
|
|
|
|
defaultNamespace = "root"
|
|
|
|
namespaceTarget = "namespace"
|
|
|
|
containerTarget = "container"
|
2024-04-15 07:47:45 +00:00
|
|
|
userTarget = "user"
|
|
|
|
groupTarget = "group"
|
2024-01-12 14:44:27 +00:00
|
|
|
)
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-01-12 14:44:27 +00:00
|
|
|
const (
|
|
|
|
targetNameFlag = "target-name"
|
|
|
|
targetNameDesc = "Resource name in APE resource name format"
|
|
|
|
targetTypeFlag = "target-type"
|
|
|
|
targetTypeDesc = "Resource type(container/namespace)"
|
|
|
|
)
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-03-11 14:55:50 +00:00
|
|
|
var (
|
|
|
|
errSettingDefaultValueWasDeclined = errors.New("setting default value was declined")
|
|
|
|
errUnknownTargetType = errors.New("unknown target type")
|
|
|
|
)
|
|
|
|
|
2024-01-12 14:44:27 +00:00
|
|
|
func parseTarget(cmd *cobra.Command) *control.ChainTarget {
|
|
|
|
typ, _ := cmd.Flags().GetString(targetTypeFlag)
|
|
|
|
name, _ := cmd.Flags().GetString(targetNameFlag)
|
|
|
|
switch typ {
|
|
|
|
case namespaceTarget:
|
|
|
|
if name == "" {
|
|
|
|
ln, err := input.ReadLine(fmt.Sprintf("Target name is not set. Confirm to use %s namespace (n|Y)> ", defaultNamespace))
|
|
|
|
commonCmd.ExitOnErr(cmd, "read line error: %w", err)
|
|
|
|
ln = strings.ToLower(ln)
|
|
|
|
if len(ln) > 0 && (ln[0] == 'n') {
|
2024-03-11 14:55:50 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "read namespace error: %w", errSettingDefaultValueWasDeclined)
|
2024-01-12 14:44:27 +00:00
|
|
|
}
|
|
|
|
name = defaultNamespace
|
|
|
|
}
|
|
|
|
return &control.ChainTarget{
|
|
|
|
Name: name,
|
|
|
|
Type: control.ChainTarget_NAMESPACE,
|
|
|
|
}
|
|
|
|
case containerTarget:
|
|
|
|
var cnr cid.ID
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't decode container ID: %w", cnr.DecodeString(name))
|
|
|
|
return &control.ChainTarget{
|
|
|
|
Name: name,
|
|
|
|
Type: control.ChainTarget_CONTAINER,
|
|
|
|
}
|
2024-04-15 07:47:45 +00:00
|
|
|
case userTarget:
|
|
|
|
return &control.ChainTarget{
|
|
|
|
Name: name,
|
|
|
|
Type: control.ChainTarget_USER,
|
|
|
|
}
|
|
|
|
case groupTarget:
|
|
|
|
return &control.ChainTarget{
|
|
|
|
Name: name,
|
|
|
|
Type: control.ChainTarget_GROUP,
|
|
|
|
}
|
2024-01-12 14:44:27 +00:00
|
|
|
default:
|
2024-03-11 14:55:50 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "read target type error: %w", errUnknownTargetType)
|
2024-01-12 14:44:27 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listRules(cmd *cobra.Command, _ []string) {
|
|
|
|
pk := key.Get(cmd)
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-02-29 11:22:10 +00:00
|
|
|
target := parseTarget(cmd)
|
2023-10-31 08:55:42 +00:00
|
|
|
req := &control.ListChainLocalOverridesRequest{
|
|
|
|
Body: &control.ListChainLocalOverridesRequest_Body{
|
2024-02-29 11:22:10 +00:00
|
|
|
Target: target,
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
signRequest(cmd, pk, req)
|
|
|
|
|
|
|
|
cli := getClient(cmd, pk)
|
|
|
|
|
|
|
|
var resp *control.ListChainLocalOverridesResponse
|
|
|
|
var err error
|
|
|
|
err = cli.ExecRaw(func(client *client.Client) error {
|
|
|
|
resp, err = control.ListChainLocalOverrides(client, req)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
|
|
|
|
|
|
|
verifyResponse(cmd, resp.GetSignature(), resp.GetBody())
|
|
|
|
|
|
|
|
chains := resp.GetBody().GetChains()
|
|
|
|
if len(chains) == 0 {
|
2024-02-29 11:22:10 +00:00
|
|
|
cmd.Printf("Local overrides are not defined for the %s.\n", strings.ToLower(target.GetType().String()))
|
2023-10-31 08:55:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range chains {
|
2023-11-16 07:58:55 +00:00
|
|
|
var chain apechain.Chain
|
2023-10-31 08:55:42 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "decode error: %w", chain.DecodeBytes(c))
|
2024-01-25 17:25:23 +00:00
|
|
|
util.PrintHumanReadableAPEChain(cmd, &chain)
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initControlListRulesCmd() {
|
|
|
|
initControlFlags(listRulesCmd)
|
|
|
|
|
|
|
|
ff := listRulesCmd.Flags()
|
2024-01-12 14:44:27 +00:00
|
|
|
ff.String(targetNameFlag, "", targetNameDesc)
|
|
|
|
ff.String(targetTypeFlag, "", targetTypeDesc)
|
|
|
|
_ = listRulesCmd.MarkFlagRequired(targetTypeFlag)
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|