2024-02-01 07:20:17 +00:00
|
|
|
package ape
|
2023-12-20 12:44:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
|
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"
|
2023-12-21 13:33:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2023-12-20 12:44:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
namespaceTarget = "namespace"
|
|
|
|
containerTarget = "container"
|
|
|
|
jsonFlag = "json"
|
|
|
|
jsonFlagDesc = "Output rule chains in JSON format"
|
|
|
|
chainIDFlag = "chain-id"
|
|
|
|
chainIDDesc = "Rule chain ID"
|
|
|
|
ruleFlag = "rule"
|
|
|
|
ruleFlagDesc = "Rule chain in text format"
|
|
|
|
ruleJSONFlag = "rule-json"
|
|
|
|
ruleJSONFlagDesc = "Chain rule in JSON format or path to the file"
|
|
|
|
targetNameFlag = "target-name"
|
|
|
|
targetNameDesc = "Resource name in APE resource name format"
|
|
|
|
targetTypeFlag = "target-type"
|
|
|
|
targetTypeDesc = "Resource type(container/namespace)"
|
2023-12-21 13:33:37 +00:00
|
|
|
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-01-31 14:26:26 +00:00
|
|
|
_ = viper.BindPFlag(morphUtil.EndpointFlag, cmd.Flags().Lookup(morphUtil.EndpointFlag))
|
2024-02-01 06:51:24 +00:00
|
|
|
_ = viper.BindPFlag(morphUtil.AlphabetWalletsFlag, cmd.Flags().Lookup(morphUtil.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-01-31 14:26:26 +00:00
|
|
|
_ = viper.BindPFlag(morphUtil.EndpointFlag, cmd.Flags().Lookup(morphUtil.EndpointFlag))
|
2024-02-01 06:51:24 +00:00
|
|
|
_ = viper.BindPFlag(morphUtil.AlphabetWalletsFlag, cmd.Flags().Lookup(morphUtil.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-01-31 14:26:26 +00:00
|
|
|
_ = viper.BindPFlag(morphUtil.EndpointFlag, cmd.Flags().Lookup(morphUtil.EndpointFlag))
|
2024-02-01 06:51:24 +00:00
|
|
|
_ = viper.BindPFlag(morphUtil.AlphabetWalletsFlag, cmd.Flags().Lookup(morphUtil.AlphabetWalletsFlag))
|
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-01-31 14:26:26 +00:00
|
|
|
_ = viper.BindPFlag(morphUtil.EndpointFlag, cmd.Flags().Lookup(morphUtil.EndpointFlag))
|
2024-02-01 06:51:24 +00:00
|
|
|
_ = viper.BindPFlag(morphUtil.AlphabetWalletsFlag, cmd.Flags().Lookup(morphUtil.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-01-31 14:26:26 +00:00
|
|
|
_ = viper.BindPFlag(morphUtil.EndpointFlag, cmd.Flags().Lookup(morphUtil.EndpointFlag))
|
2024-02-01 06:51:24 +00:00
|
|
|
_ = viper.BindPFlag(morphUtil.AlphabetWalletsFlag, cmd.Flags().Lookup(morphUtil.AlphabetWalletsFlag))
|
2023-12-21 13:33:37 +00:00
|
|
|
},
|
|
|
|
Run: getAdmin,
|
|
|
|
}
|
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-01-31 14:26:26 +00:00
|
|
|
addRuleChainCmd.Flags().StringP(morphUtil.EndpointFlag, morphUtil.EndpointFlagShort, "", morphUtil.EndpointFlagDesc)
|
2024-02-01 06:51:24 +00:00
|
|
|
addRuleChainCmd.Flags().String(morphUtil.AlphabetWalletsFlag, "", morphUtil.AlphabetWalletsFlagDesc)
|
2023-12-20 12:44:12 +00:00
|
|
|
|
|
|
|
addRuleChainCmd.Flags().String(targetTypeFlag, "", targetTypeDesc)
|
|
|
|
_ = addRuleChainCmd.MarkFlagRequired(targetTypeFlag)
|
|
|
|
addRuleChainCmd.Flags().String(targetNameFlag, "", targetNameDesc)
|
|
|
|
_ = addRuleChainCmd.MarkFlagRequired(targetNameFlag)
|
|
|
|
|
|
|
|
addRuleChainCmd.Flags().String(chainIDFlag, "", chainIDDesc)
|
|
|
|
_ = addRuleChainCmd.MarkFlagRequired(chainIDFlag)
|
|
|
|
addRuleChainCmd.Flags().String(ruleFlag, "", ruleFlagDesc)
|
|
|
|
addRuleChainCmd.Flags().String(ruleJSONFlag, "", ruleJSONFlagDesc)
|
|
|
|
|
|
|
|
addRuleChainCmd.MarkFlagsMutuallyExclusive(ruleFlag, ruleJSONFlag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initRemoveRuleChainCmd() {
|
2024-02-01 07:20:17 +00:00
|
|
|
Cmd.AddCommand(removeRuleChainCmd)
|
2023-12-20 12:44:12 +00:00
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
removeRuleChainCmd.Flags().StringP(morphUtil.EndpointFlag, morphUtil.EndpointFlagShort, "", morphUtil.EndpointFlagDesc)
|
2024-02-01 06:51:24 +00:00
|
|
|
removeRuleChainCmd.Flags().String(morphUtil.AlphabetWalletsFlag, "", morphUtil.AlphabetWalletsFlagDesc)
|
2023-12-20 12:44:12 +00:00
|
|
|
|
|
|
|
removeRuleChainCmd.Flags().String(targetTypeFlag, "", targetTypeDesc)
|
|
|
|
_ = removeRuleChainCmd.MarkFlagRequired(targetTypeFlag)
|
|
|
|
removeRuleChainCmd.Flags().String(targetNameFlag, "", targetNameDesc)
|
|
|
|
_ = removeRuleChainCmd.MarkFlagRequired(targetNameFlag)
|
|
|
|
removeRuleChainCmd.Flags().String(chainIDFlag, "", chainIDDesc)
|
|
|
|
_ = removeRuleChainCmd.MarkFlagRequired(chainIDFlag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initListRuleChainsCmd() {
|
2024-02-01 07:20:17 +00:00
|
|
|
Cmd.AddCommand(listRuleChainsCmd)
|
2023-12-20 12:44:12 +00:00
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
listRuleChainsCmd.Flags().StringP(morphUtil.EndpointFlag, morphUtil.EndpointFlagShort, "", morphUtil.EndpointFlagDesc)
|
2024-02-01 06:51:24 +00:00
|
|
|
listRuleChainsCmd.Flags().String(morphUtil.AlphabetWalletsFlag, "", morphUtil.AlphabetWalletsFlagDesc)
|
2023-12-20 12:44:12 +00:00
|
|
|
listRuleChainsCmd.Flags().StringP(targetTypeFlag, "t", "", targetTypeDesc)
|
|
|
|
_ = listRuleChainsCmd.MarkFlagRequired(targetTypeFlag)
|
|
|
|
listRuleChainsCmd.Flags().String(targetNameFlag, "", targetNameDesc)
|
|
|
|
_ = listRuleChainsCmd.MarkFlagRequired(targetNameFlag)
|
|
|
|
listRuleChainsCmd.Flags().Bool(jsonFlag, false, jsonFlagDesc)
|
|
|
|
}
|
|
|
|
|
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-01-31 14:26:26 +00:00
|
|
|
setAdminCmd.Flags().StringP(morphUtil.EndpointFlag, morphUtil.EndpointFlagShort, "", morphUtil.EndpointFlagDesc)
|
2024-02-01 06:51:24 +00:00
|
|
|
setAdminCmd.Flags().String(morphUtil.AlphabetWalletsFlag, "", morphUtil.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-01-31 14:26:26 +00:00
|
|
|
getAdminCmd.Flags().StringP(morphUtil.EndpointFlag, morphUtil.EndpointFlagShort, "", morphUtil.EndpointFlagDesc)
|
2024-02-01 06:51:24 +00:00
|
|
|
getAdminCmd.Flags().String(morphUtil.AlphabetWalletsFlag, "", morphUtil.AlphabetWalletsFlagDesc)
|
2023-12-21 13:33:37 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 12:44:12 +00:00
|
|
|
func addRuleChain(cmd *cobra.Command, _ []string) {
|
|
|
|
chain := parseChain(cmd)
|
|
|
|
target := parseTarget(cmd)
|
|
|
|
pci, ac := newPolicyContractInterface(cmd)
|
|
|
|
h, vub, err := pci.AddMorphRuleChain(apechain.Ingress, target, chain)
|
|
|
|
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) {
|
|
|
|
chainID := parseChainID(cmd)
|
|
|
|
target := parseTarget(cmd)
|
|
|
|
pci, ac := newPolicyContractInterface(cmd)
|
|
|
|
h, vub, err := pci.RemoveMorphRuleChain(apechain.Ingress, target, chainID)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func listRuleChains(cmd *cobra.Command, _ []string) {
|
|
|
|
target := parseTarget(cmd)
|
|
|
|
pci, _ := newPolicyContractInterface(cmd)
|
|
|
|
chains, err := pci.ListMorphRuleChains(apechain.Ingress, target)
|
|
|
|
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 {
|
|
|
|
parseutil.PrintHumanReadableAPEChain(cmd, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-21 13:33:37 +00:00
|
|
|
func setAdmin(cmd *cobra.Command, _ []string) {
|
|
|
|
s, _ := cmd.Flags().GetString(addrAdminFlag)
|
|
|
|
addr, err := util.Uint160DecodeStringLE(s)
|
|
|
|
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) {
|
|
|
|
pci, _ := newPolicyContractInterface(cmd)
|
|
|
|
addr, err := pci.GetAdmin()
|
|
|
|
commonCmd.ExitOnErr(cmd, "unable to get admin: %w", err)
|
|
|
|
cmd.Println(addr.StringLE())
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|