2023-10-31 08:55:42 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2024-01-12 14:44:27 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
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-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"
|
2024-11-18 10:33:47 +00:00
|
|
|
policyengine "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
2023-10-31 08:55:42 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var listRulesCmd = &cobra.Command{
|
|
|
|
Use: "list-rules",
|
|
|
|
Short: "List local overrides",
|
|
|
|
Long: "List local APE overrides of the node",
|
|
|
|
Run: listRules,
|
|
|
|
}
|
|
|
|
|
2024-11-18 10:33:47 +00:00
|
|
|
var engineToControlSvcType = map[policyengine.TargetType]control.ChainTarget_TargetType{
|
|
|
|
policyengine.Namespace: control.ChainTarget_NAMESPACE,
|
|
|
|
policyengine.Container: control.ChainTarget_CONTAINER,
|
|
|
|
policyengine.User: control.ChainTarget_USER,
|
|
|
|
policyengine.Group: control.ChainTarget_GROUP,
|
|
|
|
}
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-11-18 10:33:47 +00:00
|
|
|
func parseTarget(cmd *cobra.Command) *control.ChainTarget {
|
|
|
|
target := apeCmd.ParseTarget(cmd)
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-11-18 10:33:47 +00:00
|
|
|
typ, ok := engineToControlSvcType[target.Type]
|
|
|
|
if !ok {
|
|
|
|
commonCmd.ExitOnErr(cmd, "%w", fmt.Errorf("unknown type '%c", target.Type))
|
|
|
|
}
|
2024-03-11 14:55:50 +00:00
|
|
|
|
2024-11-18 10:33:47 +00:00
|
|
|
return &control.ChainTarget{
|
|
|
|
Name: target.Name,
|
|
|
|
Type: typ,
|
2024-01-12 14:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func listRules(cmd *cobra.Command, _ []string) {
|
|
|
|
pk := key.Get(cmd)
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2024-02-29 11:22:10 +00:00
|
|
|
target := parseTarget(cmd)
|
2023-10-31 08:55:42 +00:00
|
|
|
req := &control.ListChainLocalOverridesRequest{
|
|
|
|
Body: &control.ListChainLocalOverridesRequest_Body{
|
2024-02-29 11:22:10 +00:00
|
|
|
Target: target,
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
signRequest(cmd, pk, req)
|
|
|
|
|
|
|
|
cli := getClient(cmd, pk)
|
|
|
|
|
|
|
|
var resp *control.ListChainLocalOverridesResponse
|
|
|
|
var err error
|
|
|
|
err = cli.ExecRaw(func(client *client.Client) error {
|
|
|
|
resp, err = control.ListChainLocalOverrides(client, req)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
|
|
|
|
|
|
|
verifyResponse(cmd, resp.GetSignature(), resp.GetBody())
|
|
|
|
|
|
|
|
chains := resp.GetBody().GetChains()
|
|
|
|
if len(chains) == 0 {
|
2024-02-29 11:22:10 +00:00
|
|
|
cmd.Printf("Local overrides are not defined for the %s.\n", strings.ToLower(target.GetType().String()))
|
2023-10-31 08:55:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range chains {
|
2023-11-16 07:58:55 +00:00
|
|
|
var chain apechain.Chain
|
2023-10-31 08:55:42 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "decode error: %w", chain.DecodeBytes(c))
|
2024-11-18 09:37:55 +00:00
|
|
|
apeCmd.PrintHumanReadableAPEChain(cmd, &chain)
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initControlListRulesCmd() {
|
|
|
|
initControlFlags(listRulesCmd)
|
|
|
|
|
|
|
|
ff := listRulesCmd.Flags()
|
2024-11-18 10:33:47 +00:00
|
|
|
ff.String(apeCmd.TargetNameFlag, "", apeCmd.TargetNameFlagDesc)
|
|
|
|
ff.String(apeCmd.TargetTypeFlag, "", apeCmd.TargetTypeFlagDesc)
|
|
|
|
_ = listRulesCmd.MarkFlagRequired(apeCmd.TargetTypeFlag)
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|