2023-10-31 08:55:42 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2023-12-21 12:55:09 +00:00
|
|
|
"encoding/hex"
|
2024-02-16 09:36:42 +00:00
|
|
|
"errors"
|
2023-10-31 08:55:42 +00:00
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
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-10-31 08:55:42 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
|
2024-11-18 09:54:02 +00:00
|
|
|
parseutil "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/ape"
|
2024-11-07 14:32:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/client"
|
2023-11-16 07:58:55 +00:00
|
|
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
2023-10-31 08:55:42 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ruleFlag = "rule"
|
2024-02-16 09:36:42 +00:00
|
|
|
pathFlag = "path"
|
2023-10-31 08:55:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var addRuleCmd = &cobra.Command{
|
|
|
|
Use: "add-rule",
|
|
|
|
Short: "Add local override",
|
|
|
|
Long: "Add local APE rule to a node with following format:\n<action>[:action_detail] <operation> [<condition1> ...] <resource>",
|
2024-01-25 17:25:23 +00:00
|
|
|
Example: `control add-rule --endpoint ... -w ... --address ... --chain-id ChainID --cid ... --rule "allow Object.Get *"
|
|
|
|
--rule "deny Object.Get EbxzAdz5LB4uqxuz6crWKAumBNtZyK2rKsqQP7TdZvwr/*"
|
2024-05-14 09:23:26 +00:00
|
|
|
--rule "deny:QuotaLimitReached Object.Put ResourceCondition:Department=HR *"
|
2024-02-16 09:36:42 +00:00
|
|
|
|
|
|
|
control add-rule --endpoint ... -w ... --address ... --chain-id ChainID --cid ... --path some_chain.json
|
2023-10-31 08:55:42 +00:00
|
|
|
`,
|
|
|
|
Run: addRule,
|
|
|
|
}
|
|
|
|
|
2024-02-16 09:36:42 +00:00
|
|
|
func parseChain(cmd *cobra.Command) *apechain.Chain {
|
2023-11-21 14:01:14 +00:00
|
|
|
chainID, _ := cmd.Flags().GetString(chainIDFlag)
|
2023-12-21 12:55:09 +00:00
|
|
|
hexEncoded, _ := cmd.Flags().GetBool(chainIDHexFlag)
|
|
|
|
|
|
|
|
chainIDRaw := []byte(chainID)
|
|
|
|
|
|
|
|
if hexEncoded {
|
|
|
|
var err error
|
|
|
|
chainIDRaw, err = hex.DecodeString(chainID)
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't decode chain ID as hex: %w", err)
|
|
|
|
}
|
2023-11-21 14:01:14 +00:00
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
chain := new(apechain.Chain)
|
2023-12-21 12:55:09 +00:00
|
|
|
chain.ID = apechain.ID(chainIDRaw)
|
2024-02-16 09:36:42 +00:00
|
|
|
|
|
|
|
if rules, _ := cmd.Flags().GetStringArray(ruleFlag); len(rules) > 0 {
|
2024-11-18 09:54:02 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "parser error: %w", parseutil.ParseAPEChain(chain, rules))
|
2024-02-16 09:36:42 +00:00
|
|
|
} else if encPath, _ := cmd.Flags().GetString(pathFlag); encPath != "" {
|
2024-11-18 09:54:02 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "decode binary or json error: %w", parseutil.ParseAPEChainBinaryOrJSON(chain, encPath))
|
2024-02-16 09:36:42 +00:00
|
|
|
} else {
|
|
|
|
commonCmd.ExitOnErr(cmd, "parser error", errors.New("rule is not passed"))
|
|
|
|
}
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-01-25 17:25:23 +00:00
|
|
|
cmd.Println("Parsed chain:")
|
2024-11-18 09:37:55 +00:00
|
|
|
apeCmd.PrintHumanReadableAPEChain(cmd, chain)
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-02-16 09:36:42 +00:00
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
|
|
|
func addRule(cmd *cobra.Command, _ []string) {
|
|
|
|
pk := key.Get(cmd)
|
|
|
|
|
|
|
|
target := parseTarget(cmd)
|
|
|
|
|
|
|
|
parsed := parseChain(cmd)
|
|
|
|
|
2023-10-31 08:55:42 +00:00
|
|
|
req := &control.AddChainLocalOverrideRequest{
|
|
|
|
Body: &control.AddChainLocalOverrideRequest_Body{
|
2024-01-12 14:44:27 +00:00
|
|
|
Target: target,
|
2024-02-16 09:36:42 +00:00
|
|
|
Chain: parsed.Bytes(),
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
signRequest(cmd, pk, req)
|
|
|
|
|
|
|
|
cli := getClient(cmd, pk)
|
|
|
|
|
|
|
|
var resp *control.AddChainLocalOverrideResponse
|
|
|
|
var err error
|
|
|
|
err = cli.ExecRaw(func(client *client.Client) error {
|
|
|
|
resp, err = control.AddChainLocalOverride(client, req)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
|
|
|
|
|
|
|
verifyResponse(cmd, resp.GetSignature(), resp.GetBody())
|
2024-01-25 17:25:23 +00:00
|
|
|
cmd.Println("\nRule has been added.")
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initControlAddRuleCmd() {
|
|
|
|
initControlFlags(addRuleCmd)
|
|
|
|
|
|
|
|
ff := addRuleCmd.Flags()
|
2024-01-25 17:25:23 +00:00
|
|
|
ff.StringArray(ruleFlag, []string{}, "Rule statement")
|
2024-02-16 09:36:42 +00:00
|
|
|
ff.String(pathFlag, "", "Path to encoded chain in JSON or binary format")
|
2023-11-21 14:01:14 +00:00
|
|
|
ff.String(chainIDFlag, "", "Assign ID to the parsed chain")
|
2024-01-12 14:44:27 +00:00
|
|
|
ff.String(targetNameFlag, "", targetNameDesc)
|
|
|
|
ff.String(targetTypeFlag, "", targetTypeDesc)
|
|
|
|
_ = addRuleCmd.MarkFlagRequired(targetTypeFlag)
|
2023-12-21 12:55:09 +00:00
|
|
|
ff.Bool(chainIDHexFlag, false, "Flag to parse chain ID as hex")
|
2024-02-16 09:36:42 +00:00
|
|
|
|
|
|
|
addRuleCmd.MarkFlagsMutuallyExclusive(pathFlag, ruleFlag)
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|