2023-12-20 12:44:12 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/config"
|
2024-01-31 14:26:26 +00:00
|
|
|
morphUtil "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util"
|
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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func parseTarget(cmd *cobra.Command) policyengine.Target {
|
|
|
|
var targetType policyengine.TargetType
|
|
|
|
typ, _ := cmd.Flags().GetString(targetTypeFlag)
|
|
|
|
switch typ {
|
|
|
|
case namespaceTarget:
|
|
|
|
targetType = policyengine.Namespace
|
|
|
|
case containerTarget:
|
|
|
|
targetType = policyengine.Container
|
|
|
|
default:
|
|
|
|
commonCmd.ExitOnErr(cmd, "read target type error: %w", fmt.Errorf("unknown target type"))
|
|
|
|
}
|
|
|
|
name, _ := cmd.Flags().GetString(targetNameFlag)
|
|
|
|
|
|
|
|
return policyengine.Target{
|
|
|
|
Name: name,
|
|
|
|
Type: targetType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseChainID(cmd *cobra.Command) apechain.ID {
|
|
|
|
chainID, _ := cmd.Flags().GetString(chainIDFlag)
|
|
|
|
if chainID == "" {
|
|
|
|
commonCmd.ExitOnErr(cmd, "read chain id error: %w",
|
|
|
|
fmt.Errorf("chain id cannot be empty"))
|
|
|
|
}
|
|
|
|
return apechain.ID(chainID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseChain(cmd *cobra.Command) *apechain.Chain {
|
|
|
|
chain := new(apechain.Chain)
|
|
|
|
|
|
|
|
if ruleStmt, _ := cmd.Flags().GetString(ruleFlag); ruleStmt != "" {
|
|
|
|
parseErr := parseutil.ParseAPEChain(chain, []string{ruleStmt})
|
|
|
|
commonCmd.ExitOnErr(cmd, "ape chain parser error: %w", parseErr)
|
|
|
|
} else if ruleJSON, _ := cmd.Flags().GetString(ruleJSONFlag); ruleJSON != "" {
|
|
|
|
var rule []byte
|
|
|
|
if _, err := os.Stat(ruleJSON); err == nil {
|
|
|
|
rule, err = os.ReadFile(ruleJSON)
|
|
|
|
commonCmd.ExitOnErr(cmd, "read file error: %w", err)
|
|
|
|
} else {
|
|
|
|
rule = []byte(ruleJSON)
|
|
|
|
if !json.Valid(rule) {
|
|
|
|
commonCmd.ExitOnErr(cmd, "read raw rule error: %w",
|
|
|
|
fmt.Errorf("invalid JSON"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := chain.DecodeBytes(rule)
|
|
|
|
commonCmd.ExitOnErr(cmd, "chain decode error: %w", err)
|
|
|
|
} else {
|
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("rule is not passed"))
|
|
|
|
}
|
|
|
|
|
|
|
|
chain.ID = parseChainID(cmd)
|
|
|
|
|
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPolicyContractInterface(cmd *cobra.Command) (*morph.ContractStorage, *actor.Actor) {
|
|
|
|
v := viper.GetViper()
|
2024-01-31 14:26:26 +00:00
|
|
|
c, err := morphUtil.GetN3Client(v)
|
2023-12-20 12:44:12 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "unable to create NEO rpc client: %w", err)
|
|
|
|
|
|
|
|
walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag))
|
2024-01-31 14:26:26 +00:00
|
|
|
wallets, err := morphUtil.GetAlphabetWallets(v, walletDir)
|
2023-12-20 12:44:12 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "unable to get alphabet wallets: %w", err)
|
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
committeeAcc, err := morphUtil.GetWalletAccount(wallets[0], committeeAccountName)
|
2023-12-20 12:44:12 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't find committee account: %w", err)
|
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
ac, err := morphUtil.NewActor(c, committeeAcc)
|
2023-12-20 12:44:12 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't create actor: %w", err)
|
|
|
|
|
|
|
|
inv := &ac.Invoker
|
|
|
|
var ch util.Uint160
|
|
|
|
r := management.NewReader(inv)
|
|
|
|
nnsCs, err := r.GetContractByID(1)
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't get NNS contract state: %w", err)
|
|
|
|
|
2024-02-01 06:43:51 +00:00
|
|
|
ch, err = morphUtil.NNSResolveHash(inv, nnsCs.Hash, domainOf(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
|
|
|
|
}
|