2023-10-31 08:55:42 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/modules/util"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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>",
|
|
|
|
Example: `allow Object.Get *
|
|
|
|
deny Object.Get EbxzAdz5LB4uqxuz6crWKAumBNtZyK2rKsqQP7TdZvwr/*
|
|
|
|
deny:QuotaLimitReached Object.Put Object.Resource:Department=HR *
|
|
|
|
`,
|
|
|
|
Run: addRule,
|
|
|
|
}
|
|
|
|
|
|
|
|
func prettyJSONFormat(cmd *cobra.Command, serializedChain []byte) string {
|
|
|
|
wr := bytes.NewBufferString("")
|
|
|
|
err := json.Indent(wr, serializedChain, "", " ")
|
|
|
|
commonCmd.ExitOnErr(cmd, "%w", err)
|
|
|
|
return wr.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addRule(cmd *cobra.Command, _ []string) {
|
|
|
|
pk := key.Get(cmd)
|
|
|
|
|
2023-11-21 14:01:14 +00:00
|
|
|
chainID, _ := cmd.Flags().GetString(chainIDFlag)
|
|
|
|
|
2023-10-31 08:55:42 +00:00
|
|
|
var cnr cid.ID
|
|
|
|
cidStr, _ := cmd.Flags().GetString(commonflags.CIDFlag)
|
|
|
|
commonCmd.ExitOnErr(cmd, "can't decode container ID: %w", cnr.DecodeString(cidStr))
|
|
|
|
|
|
|
|
rawCID := make([]byte, sha256.Size)
|
|
|
|
cnr.Encode(rawCID)
|
|
|
|
|
|
|
|
rule, _ := cmd.Flags().GetString(ruleFlag)
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
chain := new(apechain.Chain)
|
2023-10-31 08:55:42 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "parser error: %w", util.ParseAPEChain(chain, []string{rule}))
|
2023-11-21 14:01:14 +00:00
|
|
|
chain.ID = apechain.ID(chainID)
|
2023-10-31 08:55:42 +00:00
|
|
|
serializedChain := chain.Bytes()
|
|
|
|
|
|
|
|
cmd.Println("Container ID: " + cidStr)
|
|
|
|
cmd.Println("Parsed chain:\n" + prettyJSONFormat(cmd, serializedChain))
|
|
|
|
|
|
|
|
req := &control.AddChainLocalOverrideRequest{
|
|
|
|
Body: &control.AddChainLocalOverrideRequest_Body{
|
2023-12-05 09:11:28 +00:00
|
|
|
Target: &control.ChainTarget{
|
|
|
|
Type: control.ChainTarget_CONTAINER,
|
2023-12-13 12:25:47 +00:00
|
|
|
Name: cidStr,
|
2023-12-05 09:11:28 +00:00
|
|
|
},
|
|
|
|
Chain: serializedChain,
|
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())
|
|
|
|
|
|
|
|
cmd.Println("Rule has been added. Chain id: ", resp.GetBody().GetChainId())
|
|
|
|
}
|
|
|
|
|
|
|
|
func initControlAddRuleCmd() {
|
|
|
|
initControlFlags(addRuleCmd)
|
|
|
|
|
|
|
|
ff := addRuleCmd.Flags()
|
|
|
|
ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
|
|
|
ff.String(ruleFlag, "", "Rule statement")
|
2023-11-21 14:01:14 +00:00
|
|
|
ff.String(chainIDFlag, "", "Assign ID to the parsed chain")
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|