2024-02-01 07:20:17 +00:00
|
|
|
package ape
|
2023-12-20 12:44:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
|
2024-02-02 12:04:48 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/commonflags"
|
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"
|
2023-12-20 12:44:12 +00:00
|
|
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
2024-11-23 12:08:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2023-12-20 12:44:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-11-18 11:11:21 +00:00
|
|
|
jsonFlag = "json"
|
|
|
|
jsonFlagDesc = "Output rule chains in JSON format"
|
|
|
|
addrAdminFlag = "addr"
|
|
|
|
addrAdminDesc = "The address of the admins wallet"
|
2023-12-20 12:44:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
addRuleChainCmd = &cobra.Command{
|
|
|
|
Use: "add-rule-chain",
|
|
|
|
Short: "Add rule chain",
|
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
2024-02-02 12:04:48 +00:00
|
|
|
_ = viper.BindPFlag(commonflags.EndpointFlag, cmd.Flags().Lookup(commonflags.EndpointFlag))
|
|
|
|
_ = viper.BindPFlag(commonflags.AlphabetWalletsFlag, cmd.Flags().Lookup(commonflags.AlphabetWalletsFlag))
|
2023-12-20 12:44:12 +00:00
|
|
|
},
|
|
|
|
Run: addRuleChain,
|
|
|
|
}
|
|
|
|
|
|
|
|
removeRuleChainCmd = &cobra.Command{
|
|
|
|
Use: "rm-rule-chain",
|
|
|
|
Short: "Remove rule chain",
|
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
2024-02-02 12:04:48 +00:00
|
|
|
_ = viper.BindPFlag(commonflags.EndpointFlag, cmd.Flags().Lookup(commonflags.EndpointFlag))
|
|
|
|
_ = viper.BindPFlag(commonflags.AlphabetWalletsFlag, cmd.Flags().Lookup(commonflags.AlphabetWalletsFlag))
|
2023-12-20 12:44:12 +00:00
|
|
|
},
|
|
|
|
Run: removeRuleChain,
|
|
|
|
}
|
|
|
|
|
|
|
|
listRuleChainsCmd = &cobra.Command{
|
|
|
|
Use: "list-rule-chains",
|
|
|
|
Short: "List rule chains",
|
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
2024-02-02 12:04:48 +00:00
|
|
|
_ = viper.BindPFlag(commonflags.EndpointFlag, cmd.Flags().Lookup(commonflags.EndpointFlag))
|
2023-12-20 12:44:12 +00:00
|
|
|
},
|
|
|
|
Run: listRuleChains,
|
|
|
|
}
|
2023-12-21 13:33:37 +00:00
|
|
|
|
|
|
|
setAdminCmd = &cobra.Command{
|
|
|
|
Use: "set-admin",
|
|
|
|
Short: "Set admin",
|
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
2024-02-02 12:04:48 +00:00
|
|
|
_ = viper.BindPFlag(commonflags.EndpointFlag, cmd.Flags().Lookup(commonflags.EndpointFlag))
|
|
|
|
_ = viper.BindPFlag(commonflags.AlphabetWalletsFlag, cmd.Flags().Lookup(commonflags.AlphabetWalletsFlag))
|
2023-12-21 13:33:37 +00:00
|
|
|
},
|
|
|
|
Run: setAdmin,
|
|
|
|
}
|
|
|
|
|
|
|
|
getAdminCmd = &cobra.Command{
|
|
|
|
Use: "get-admin",
|
|
|
|
Short: "Get admin",
|
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
2024-02-02 12:04:48 +00:00
|
|
|
_ = viper.BindPFlag(commonflags.EndpointFlag, cmd.Flags().Lookup(commonflags.EndpointFlag))
|
2023-12-21 13:33:37 +00:00
|
|
|
},
|
|
|
|
Run: getAdmin,
|
|
|
|
}
|
2024-03-11 08:22:56 +00:00
|
|
|
|
|
|
|
listTargetsCmd = &cobra.Command{
|
|
|
|
Use: "list-targets",
|
|
|
|
Short: "List targets",
|
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
_ = viper.BindPFlag(commonflags.EndpointFlag, cmd.Flags().Lookup(commonflags.EndpointFlag))
|
|
|
|
},
|
|
|
|
Run: listTargets,
|
|
|
|
}
|
2023-12-20 12:44:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func initAddRuleChainCmd() {
|
2024-02-01 07:20:17 +00:00
|
|
|
Cmd.AddCommand(addRuleChainCmd)
|
2023-12-20 12:44:12 +00:00
|
|
|
|
2024-02-02 12:04:48 +00:00
|
|
|
addRuleChainCmd.Flags().StringP(commonflags.EndpointFlag, commonflags.EndpointFlagShort, "", commonflags.EndpointFlagDesc)
|
|
|
|
addRuleChainCmd.Flags().String(commonflags.AlphabetWalletsFlag, "", commonflags.AlphabetWalletsFlagDesc)
|
2023-12-20 12:44:12 +00:00
|
|
|
|
2024-11-18 11:11:21 +00:00
|
|
|
addRuleChainCmd.Flags().String(apeCmd.TargetTypeFlag, "", apeCmd.TargetTypeFlagDesc)
|
|
|
|
_ = addRuleChainCmd.MarkFlagRequired(apeCmd.TargetTypeFlag)
|
|
|
|
addRuleChainCmd.Flags().String(apeCmd.TargetNameFlag, "", apeCmd.TargetTypeFlagDesc)
|
|
|
|
_ = addRuleChainCmd.MarkFlagRequired(apeCmd.TargetNameFlag)
|
|
|
|
|
|
|
|
addRuleChainCmd.Flags().String(apeCmd.ChainIDFlag, "", apeCmd.ChainIDFlagDesc)
|
|
|
|
_ = addRuleChainCmd.MarkFlagRequired(apeCmd.ChainIDFlag)
|
|
|
|
addRuleChainCmd.Flags().StringArray(apeCmd.RuleFlag, []string{}, apeCmd.RuleFlagDesc)
|
|
|
|
addRuleChainCmd.Flags().String(apeCmd.PathFlag, "", apeCmd.PathFlagDesc)
|
|
|
|
addRuleChainCmd.Flags().String(apeCmd.ChainNameFlag, apeCmd.Ingress, apeCmd.ChainNameFlagDesc)
|
|
|
|
addRuleChainCmd.MarkFlagsMutuallyExclusive(apeCmd.RuleFlag, apeCmd.PathFlag)
|
2023-12-20 12:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initRemoveRuleChainCmd() {
|
2024-02-01 07:20:17 +00:00
|
|
|
Cmd.AddCommand(removeRuleChainCmd)
|
2023-12-20 12:44:12 +00:00
|
|
|
|
2024-02-02 12:04:48 +00:00
|
|
|
removeRuleChainCmd.Flags().StringP(commonflags.EndpointFlag, commonflags.EndpointFlagShort, "", commonflags.EndpointFlagDesc)
|
|
|
|
removeRuleChainCmd.Flags().String(commonflags.AlphabetWalletsFlag, "", commonflags.AlphabetWalletsFlagDesc)
|
2023-12-20 12:44:12 +00:00
|
|
|
|
2024-11-18 11:11:21 +00:00
|
|
|
removeRuleChainCmd.Flags().String(apeCmd.TargetTypeFlag, "", apeCmd.TargetTypeFlagDesc)
|
|
|
|
_ = removeRuleChainCmd.MarkFlagRequired(apeCmd.TargetTypeFlag)
|
|
|
|
removeRuleChainCmd.Flags().String(apeCmd.TargetNameFlag, "", apeCmd.TargetNameFlagDesc)
|
|
|
|
_ = removeRuleChainCmd.MarkFlagRequired(apeCmd.TargetNameFlag)
|
|
|
|
removeRuleChainCmd.Flags().String(apeCmd.ChainIDFlag, "", apeCmd.ChainIDFlagDesc)
|
|
|
|
removeRuleChainCmd.Flags().String(apeCmd.ChainNameFlag, apeCmd.Ingress, apeCmd.ChainNameFlagDesc)
|
2024-03-07 11:33:07 +00:00
|
|
|
removeRuleChainCmd.Flags().Bool(commonflags.AllFlag, false, "Remove all chains for target")
|
2024-11-18 11:11:21 +00:00
|
|
|
removeRuleChainCmd.MarkFlagsMutuallyExclusive(commonflags.AllFlag, apeCmd.ChainIDFlag)
|
2023-12-20 12:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initListRuleChainsCmd() {
|
2024-02-01 07:20:17 +00:00
|
|
|
Cmd.AddCommand(listRuleChainsCmd)
|
2023-12-20 12:44:12 +00:00
|
|
|
|
2024-02-02 12:04:48 +00:00
|
|
|
listRuleChainsCmd.Flags().StringP(commonflags.EndpointFlag, commonflags.EndpointFlagShort, "", commonflags.EndpointFlagDesc)
|
2024-11-18 11:11:21 +00:00
|
|
|
listRuleChainsCmd.Flags().StringP(apeCmd.TargetTypeFlag, "t", "", apeCmd.TargetTypeFlagDesc)
|
|
|
|
_ = listRuleChainsCmd.MarkFlagRequired(apeCmd.TargetTypeFlag)
|
|
|
|
listRuleChainsCmd.Flags().String(apeCmd.TargetNameFlag, "", apeCmd.TargetNameFlagDesc)
|
2023-12-20 12:44:12 +00:00
|
|
|
listRuleChainsCmd.Flags().Bool(jsonFlag, false, jsonFlagDesc)
|
2024-11-18 11:11:21 +00:00
|
|
|
listRuleChainsCmd.Flags().String(apeCmd.ChainNameFlag, apeCmd.Ingress, apeCmd.ChainNameFlagDesc)
|
2023-12-20 12:44:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 13:33:37 +00:00
|
|
|
func initSetAdminCmd() {
|
2024-02-01 07:20:17 +00:00
|
|
|
Cmd.AddCommand(setAdminCmd)
|
2023-12-21 13:33:37 +00:00
|
|
|
|
2024-02-02 12:04:48 +00:00
|
|
|
setAdminCmd.Flags().StringP(commonflags.EndpointFlag, commonflags.EndpointFlagShort, "", commonflags.EndpointFlagDesc)
|
|
|
|
setAdminCmd.Flags().String(commonflags.AlphabetWalletsFlag, "", commonflags.AlphabetWalletsFlagDesc)
|
2023-12-21 13:33:37 +00:00
|
|
|
setAdminCmd.Flags().String(addrAdminFlag, "", addrAdminDesc)
|
|
|
|
_ = setAdminCmd.MarkFlagRequired(addrAdminFlag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initGetAdminCmd() {
|
2024-02-01 07:20:17 +00:00
|
|
|
Cmd.AddCommand(getAdminCmd)
|
2023-12-21 13:33:37 +00:00
|
|
|
|
2024-02-02 12:04:48 +00:00
|
|
|
getAdminCmd.Flags().StringP(commonflags.EndpointFlag, commonflags.EndpointFlagShort, "", commonflags.EndpointFlagDesc)
|
2023-12-21 13:33:37 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 08:22:56 +00:00
|
|
|
func initListTargetsCmd() {
|
|
|
|
Cmd.AddCommand(listTargetsCmd)
|
|
|
|
|
|
|
|
listTargetsCmd.Flags().StringP(commonflags.EndpointFlag, commonflags.EndpointFlagShort, "", commonflags.EndpointFlagDesc)
|
2024-11-18 11:11:21 +00:00
|
|
|
listTargetsCmd.Flags().StringP(apeCmd.TargetTypeFlag, "t", "", apeCmd.TargetTypeFlagDesc)
|
|
|
|
_ = listTargetsCmd.MarkFlagRequired(apeCmd.TargetTypeFlag)
|
2024-03-11 08:22:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 12:44:12 +00:00
|
|
|
func addRuleChain(cmd *cobra.Command, _ []string) {
|
2024-11-18 11:11:21 +00:00
|
|
|
chain := apeCmd.ParseChain(cmd)
|
2023-12-20 12:44:12 +00:00
|
|
|
target := parseTarget(cmd)
|
|
|
|
pci, ac := newPolicyContractInterface(cmd)
|
2024-11-18 11:11:21 +00:00
|
|
|
h, vub, err := pci.AddMorphRuleChain(apeCmd.ParseChainName(cmd), target, chain)
|
2023-12-20 12:44:12 +00:00
|
|
|
cmd.Println("Waiting for transaction to persist...")
|
|
|
|
_, err = ac.Wait(h, vub, err)
|
|
|
|
commonCmd.ExitOnErr(cmd, "add rule chain error: %w", err)
|
|
|
|
cmd.Println("Rule chain added successfully")
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeRuleChain(cmd *cobra.Command, _ []string) {
|
|
|
|
target := parseTarget(cmd)
|
|
|
|
pci, ac := newPolicyContractInterface(cmd)
|
2024-03-07 11:33:07 +00:00
|
|
|
removeAll, _ := cmd.Flags().GetBool(commonflags.AllFlag)
|
|
|
|
if removeAll {
|
2024-11-18 11:11:21 +00:00
|
|
|
h, vub, err := pci.RemoveMorphRuleChainsByTarget(apeCmd.ParseChainName(cmd), target)
|
2024-03-07 11:33:07 +00:00
|
|
|
cmd.Println("Waiting for transaction to persist...")
|
|
|
|
_, err = ac.Wait(h, vub, err)
|
|
|
|
commonCmd.ExitOnErr(cmd, "remove rule chain error: %w", err)
|
|
|
|
cmd.Println("All chains for target removed successfully")
|
|
|
|
} else {
|
2024-11-18 11:11:21 +00:00
|
|
|
chainID := apeCmd.ParseChainID(cmd)
|
|
|
|
h, vub, err := pci.RemoveMorphRuleChain(apeCmd.ParseChainName(cmd), target, chainID)
|
2024-03-07 11:33:07 +00:00
|
|
|
cmd.Println("Waiting for transaction to persist...")
|
|
|
|
_, err = ac.Wait(h, vub, err)
|
|
|
|
commonCmd.ExitOnErr(cmd, "remove rule chain error: %w", err)
|
|
|
|
cmd.Println("Rule chain removed successfully")
|
|
|
|
}
|
2023-12-20 12:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func listRuleChains(cmd *cobra.Command, _ []string) {
|
|
|
|
target := parseTarget(cmd)
|
2024-02-26 16:23:54 +00:00
|
|
|
pci, _ := newPolicyContractReaderInterface(cmd)
|
2024-11-18 11:11:21 +00:00
|
|
|
chains, err := pci.ListMorphRuleChains(apeCmd.ParseChainName(cmd), target)
|
2023-12-20 12:44:12 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "list rule chains error: %w", err)
|
|
|
|
if len(chains) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON, _ := cmd.Flags().GetBool(jsonFlag)
|
|
|
|
if toJSON {
|
|
|
|
prettyJSONFormat(cmd, chains)
|
|
|
|
} else {
|
|
|
|
for _, c := range chains {
|
2024-11-18 09:37:55 +00:00
|
|
|
apeCmd.PrintHumanReadableAPEChain(cmd, c)
|
2023-12-20 12:44:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-21 13:33:37 +00:00
|
|
|
func setAdmin(cmd *cobra.Command, _ []string) {
|
|
|
|
s, _ := cmd.Flags().GetString(addrAdminFlag)
|
2024-11-23 12:08:50 +00:00
|
|
|
addr, err := address.StringToUint160(s)
|
2023-12-21 13:33:37 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't decode admin addr: %w", err)
|
|
|
|
pci, ac := newPolicyContractInterface(cmd)
|
|
|
|
h, vub, err := pci.SetAdmin(addr)
|
|
|
|
cmd.Println("Waiting for transaction to persist...")
|
|
|
|
_, err = ac.Wait(h, vub, err)
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't set admin: %w", err)
|
|
|
|
cmd.Println("Admin set successfully")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAdmin(cmd *cobra.Command, _ []string) {
|
2024-02-26 16:23:54 +00:00
|
|
|
pci, _ := newPolicyContractReaderInterface(cmd)
|
2023-12-21 13:33:37 +00:00
|
|
|
addr, err := pci.GetAdmin()
|
|
|
|
commonCmd.ExitOnErr(cmd, "unable to get admin: %w", err)
|
2024-11-23 12:08:50 +00:00
|
|
|
cmd.Println(address.Uint160ToString(addr))
|
2023-12-21 13:33:37 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 08:22:56 +00:00
|
|
|
func listTargets(cmd *cobra.Command, _ []string) {
|
2024-11-18 11:11:21 +00:00
|
|
|
typ := apeCmd.ParseTargetType(cmd)
|
2024-03-11 08:22:56 +00:00
|
|
|
pci, inv := newPolicyContractReaderInterface(cmd)
|
|
|
|
|
|
|
|
sid, it, err := pci.ListTargetsIterator(typ)
|
|
|
|
commonCmd.ExitOnErr(cmd, "list targets error: %w", err)
|
|
|
|
items, err := inv.TraverseIterator(sid, &it, 0)
|
|
|
|
for err == nil && len(items) != 0 {
|
|
|
|
for _, item := range items {
|
|
|
|
bts, err := item.TryBytes()
|
|
|
|
commonCmd.ExitOnErr(cmd, "list targets error: %w", err)
|
|
|
|
if len(bts) == 0 {
|
|
|
|
cmd.Println("(no name)")
|
|
|
|
} else {
|
|
|
|
cmd.Println(string(bts))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
items, err = inv.TraverseIterator(sid, &it, 0)
|
|
|
|
commonCmd.ExitOnErr(cmd, "unable to list targets: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 12:44:12 +00:00
|
|
|
func prettyJSONFormat(cmd *cobra.Command, chains []*apechain.Chain) {
|
|
|
|
wr := bytes.NewBufferString("")
|
|
|
|
data, err := json.Marshal(chains)
|
|
|
|
if err == nil {
|
|
|
|
err = json.Indent(wr, data, "", " ")
|
|
|
|
}
|
|
|
|
commonCmd.ExitOnErr(cmd, "print rule chain error: %w", err)
|
|
|
|
cmd.Println(wr)
|
|
|
|
}
|