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"
|
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
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
2024-11-18 09:37:55 +00:00
|
|
|
apeCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common/ape"
|
2024-11-18 11:11:21 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-12-20 12:44:12 +00:00
|
|
|
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-11-18 11:11:21 +00:00
|
|
|
var errUnknownTargetType = errors.New("unknown target type")
|
2024-03-11 14:55:50 +00:00
|
|
|
|
2023-12-20 12:44:12 +00:00
|
|
|
func parseTarget(cmd *cobra.Command) policyengine.Target {
|
2024-11-18 11:11:21 +00:00
|
|
|
typ := apeCmd.ParseTargetType(cmd)
|
|
|
|
name, _ := cmd.Flags().GetString(apeCmd.TargetNameFlag)
|
2024-03-11 08:22:56 +00:00
|
|
|
switch typ {
|
2024-11-18 11:11:21 +00:00
|
|
|
case policyengine.Namespace:
|
|
|
|
if name == "root" {
|
|
|
|
name = ""
|
|
|
|
}
|
|
|
|
return policyengine.NamespaceTarget(name)
|
|
|
|
case policyengine.Container:
|
|
|
|
var cnr cid.ID
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't decode container ID: %w", cnr.DecodeString(name))
|
|
|
|
return policyengine.ContainerTarget(name)
|
|
|
|
case policyengine.User:
|
|
|
|
return policyengine.UserTarget(name)
|
|
|
|
case policyengine.Group:
|
|
|
|
return policyengine.GroupTarget(name)
|
|
|
|
default:
|
|
|
|
commonCmd.ExitOnErr(cmd, "read target type error: %w", errUnknownTargetType)
|
2024-02-13 13:22:08 +00:00
|
|
|
}
|
2024-11-18 11:11:21 +00:00
|
|
|
panic("unreachable")
|
2024-02-13 13:22:08 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
|
2024-11-22 06:59:43 +00:00
|
|
|
ch, err := helper.NNSResolveHash(inv, nnsCs.Hash, helper.DomainOf(constants.PolicyContract))
|
2024-02-26 16:23:54 +00:00
|
|
|
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-10-30 14:39:02 +00:00
|
|
|
ac, err := helper.NewLocalActor(cmd, c, constants.ConsensusAccountName)
|
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
|
|
|
|
}
|