2024-02-01 07:20:17 +00:00
|
|
|
package ape
|
2023-12-20 12:44:12 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-11 14:55:50 +00:00
|
|
|
"errors"
|
2024-02-13 13:22:08 +00:00
|
|
|
"strings"
|
2023-12-20 12:44:12 +00:00
|
|
|
|
2024-02-02 12:26:57 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/constants"
|
2024-02-02 12:36:14 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/helper"
|
2023-12-20 12:44:12 +00:00
|
|
|
parseutil "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/util"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
policyengine "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
|
|
|
morph "git.frostfs.info/TrueCloudLab/policy-engine/pkg/morph/policy"
|
2024-02-26 16:23:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
2023-12-20 12:44:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2024-02-13 13:22:08 +00:00
|
|
|
const (
|
|
|
|
ingress = "ingress"
|
|
|
|
s3 = "s3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mChainName = map[string]apechain.Name{
|
|
|
|
ingress: apechain.Ingress,
|
|
|
|
s3: apechain.S3,
|
|
|
|
}
|
|
|
|
|
2024-03-11 14:55:50 +00:00
|
|
|
var (
|
|
|
|
errUnknownTargetType = errors.New("unknown target type")
|
|
|
|
errChainIDCannotBeEmpty = errors.New("chain id cannot be empty")
|
|
|
|
errRuleIsNotParsed = errors.New("rule is not passed")
|
|
|
|
errUnsupportedChainName = errors.New("unsupported chain name")
|
|
|
|
)
|
|
|
|
|
2023-12-20 12:44:12 +00:00
|
|
|
func parseTarget(cmd *cobra.Command) policyengine.Target {
|
|
|
|
name, _ := cmd.Flags().GetString(targetNameFlag)
|
2024-03-11 08:22:56 +00:00
|
|
|
typ, err := parseTargetType(cmd)
|
|
|
|
commonCmd.ExitOnErr(cmd, "read target type error: %w", err)
|
2023-12-20 12:44:12 +00:00
|
|
|
|
|
|
|
return policyengine.Target{
|
|
|
|
Name: name,
|
2024-03-11 08:22:56 +00:00
|
|
|
Type: typ,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTargetType(cmd *cobra.Command) (policyengine.TargetType, error) {
|
|
|
|
typ, _ := cmd.Flags().GetString(targetTypeFlag)
|
|
|
|
switch typ {
|
|
|
|
case namespaceTarget:
|
|
|
|
return policyengine.Namespace, nil
|
|
|
|
case containerTarget:
|
|
|
|
return policyengine.Container, nil
|
2024-04-16 10:19:16 +00:00
|
|
|
case userTarget:
|
|
|
|
return policyengine.User, nil
|
|
|
|
case groupTarget:
|
|
|
|
return policyengine.Group, nil
|
2023-12-20 12:44:12 +00:00
|
|
|
}
|
2024-03-11 08:22:56 +00:00
|
|
|
return -1, errUnknownTargetType
|
2023-12-20 12:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseChainID(cmd *cobra.Command) apechain.ID {
|
|
|
|
chainID, _ := cmd.Flags().GetString(chainIDFlag)
|
|
|
|
if chainID == "" {
|
|
|
|
commonCmd.ExitOnErr(cmd, "read chain id error: %w",
|
2024-03-11 14:55:50 +00:00
|
|
|
errChainIDCannotBeEmpty)
|
2023-12-20 12:44:12 +00:00
|
|
|
}
|
|
|
|
return apechain.ID(chainID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseChain(cmd *cobra.Command) *apechain.Chain {
|
|
|
|
chain := new(apechain.Chain)
|
|
|
|
|
2024-02-16 09:59:48 +00:00
|
|
|
if rules, _ := cmd.Flags().GetStringArray(ruleFlag); len(rules) > 0 {
|
|
|
|
commonCmd.ExitOnErr(cmd, "parser error: %w", parseutil.ParseAPEChain(chain, rules))
|
|
|
|
} else if encPath, _ := cmd.Flags().GetString(pathFlag); encPath != "" {
|
|
|
|
commonCmd.ExitOnErr(cmd, "decode binary or json error: %w", parseutil.ParseAPEChainBinaryOrJSON(chain, encPath))
|
2023-12-20 12:44:12 +00:00
|
|
|
} else {
|
2024-03-11 14:55:50 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "parser error: %w", errRuleIsNotParsed)
|
2023-12-20 12:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chain.ID = parseChainID(cmd)
|
|
|
|
|
2024-02-16 09:59:48 +00:00
|
|
|
cmd.Println("Parsed chain:")
|
|
|
|
parseutil.PrintHumanReadableAPEChain(cmd, chain)
|
|
|
|
|
2023-12-20 12:44:12 +00:00
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
2024-02-13 13:22:08 +00:00
|
|
|
func parseChainName(cmd *cobra.Command) apechain.Name {
|
|
|
|
chainName, _ := cmd.Flags().GetString(chainNameFlag)
|
|
|
|
apeChainName, ok := mChainName[strings.ToLower(chainName)]
|
|
|
|
if !ok {
|
2024-03-11 14:55:50 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", errUnsupportedChainName)
|
2024-02-13 13:22:08 +00:00
|
|
|
}
|
|
|
|
return apeChainName
|
|
|
|
}
|
|
|
|
|
2024-04-26 14:27:56 +00:00
|
|
|
// invokerAdapter adapats invoker.Invoker to ContractStorageInvoker interface.
|
|
|
|
type invokerAdapter struct {
|
|
|
|
*invoker.Invoker
|
|
|
|
rpcActor invoker.RPCInvoke
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *invokerAdapter) GetRPCInvoker() invoker.RPCInvoke {
|
|
|
|
return n.rpcActor
|
|
|
|
}
|
|
|
|
|
2024-02-26 16:23:54 +00:00
|
|
|
func newPolicyContractReaderInterface(cmd *cobra.Command) (*morph.ContractStorageReader, *invoker.Invoker) {
|
|
|
|
c, err := helper.GetN3Client(viper.GetViper())
|
|
|
|
commonCmd.ExitOnErr(cmd, "unable to create NEO rpc client: %w", err)
|
|
|
|
|
|
|
|
inv := invoker.New(c, nil)
|
|
|
|
var ch util.Uint160
|
|
|
|
r := management.NewReader(inv)
|
2024-06-28 13:35:18 +00:00
|
|
|
nnsCs, err := helper.GetContractByID(r, 1)
|
2024-02-26 16:23:54 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't get NNS contract state: %w", err)
|
|
|
|
|
|
|
|
ch, err = helper.NNSResolveHash(inv, nnsCs.Hash, helper.DomainOf(constants.PolicyContract))
|
|
|
|
commonCmd.ExitOnErr(cmd, "unable to resolve policy contract hash: %w", err)
|
|
|
|
|
2024-04-26 14:27:56 +00:00
|
|
|
invokerAdapter := &invokerAdapter{
|
|
|
|
Invoker: inv,
|
|
|
|
rpcActor: c,
|
|
|
|
}
|
|
|
|
|
|
|
|
return morph.NewContractStorageReader(invokerAdapter, ch), inv
|
2024-02-26 16:23:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 08:36:36 +00:00
|
|
|
func newPolicyContractInterface(cmd *cobra.Command) (*morph.ContractStorage, *helper.LocalActor) {
|
|
|
|
c, err := helper.GetN3Client(viper.GetViper())
|
2023-12-20 12:44:12 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "unable to create NEO rpc client: %w", err)
|
|
|
|
|
2024-02-20 08:36:36 +00:00
|
|
|
ac, err := helper.NewLocalActor(cmd, c)
|
2023-12-20 12:44:12 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't create actor: %w", err)
|
|
|
|
|
|
|
|
var ch util.Uint160
|
2024-02-20 08:36:36 +00:00
|
|
|
r := management.NewReader(ac.Invoker)
|
2024-06-28 13:35:18 +00:00
|
|
|
nnsCs, err := helper.GetContractByID(r, 1)
|
2023-12-20 12:44:12 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't get NNS contract state: %w", err)
|
|
|
|
|
2024-02-20 08:36:36 +00:00
|
|
|
ch, err = helper.NNSResolveHash(ac.Invoker, nnsCs.Hash, helper.DomainOf(constants.PolicyContract))
|
2023-12-20 12:44:12 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "unable to resolve policy contract hash: %w", err)
|
|
|
|
|
|
|
|
return morph.NewContractStorage(ac, ch), ac
|
|
|
|
}
|