[#26] Add extnded ACL table converter in CLI

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-22 18:59:37 +03:00 committed by Alex Vanin
parent c4e9e258fd
commit b93c146824

View file

@ -29,19 +29,38 @@ var (
Short: "sign bearer token to use it in requests",
RunE: signBearerToken,
}
convertCmd = &cobra.Command{
Use: "convert",
Short: "convert representation of NeoFS structures",
}
convertEACLCmd = &cobra.Command{
Use: "eacl",
Short: "convert representation of extended ACL table",
RunE: convertEACLTable,
}
)
func init() {
rootCmd.AddCommand(utilCmd)
utilCmd.AddCommand(signCmd)
utilCmd.AddCommand(convertCmd)
signCmd.AddCommand(signBearerCmd)
signBearerCmd.Flags().String("from", "", "File with JSON or binary encoded bearer token to sign")
_ = signBearerCmd.MarkFlagFilename("from")
_ = signBearerCmd.MarkFlagRequired("from")
signBearerCmd.Flags().String("to", "", "File to dump signed bearer token")
signBearerCmd.Flags().String("to", "", "File to dump signed bearer token (default: binary encoded)")
signBearerCmd.Flags().Bool("json", false, "Dump bearer token in JSON encoding")
convertCmd.AddCommand(convertEACLCmd)
convertEACLCmd.Flags().String("from", "", "File with JSON or binary encoded extended ACL table")
_ = convertEACLCmd.MarkFlagFilename("from")
_ = convertEACLCmd.MarkFlagRequired("from")
convertEACLCmd.Flags().String("to", "", "File to dump extended ACL table (default: binary encoded)")
convertEACLCmd.Flags().Bool("json", false, "Dump extended ACL table in JSON encoding")
}
func signBearerToken(cmd *cobra.Command, _ []string) error {
@ -97,6 +116,45 @@ func signBearerToken(cmd *cobra.Command, _ []string) error {
return nil
}
func convertEACLTable(cmd *cobra.Command, _ []string) error {
pathFrom := cmd.Flag("from").Value.String()
to := cmd.Flag("to").Value.String()
jsonFlag, _ := cmd.Flags().GetBool("json")
table, err := parseEACL(pathFrom)
if err != nil {
return err
}
var data []byte
if jsonFlag || len(to) == 0 {
data = v2ACL.TableToJSON(table.ToV2())
if len(data) == 0 {
return errors.New("can't JSON encode extended ACL table")
}
} else {
data, err = table.ToV2().StableMarshal(nil)
if err != nil {
return errors.New("can't binary encode extended ACL table")
}
}
if len(to) == 0 {
prettyPrintJSON(cmd, data)
return nil
}
err = ioutil.WriteFile(to, data, 0644)
if err != nil {
return fmt.Errorf("can't write exteded ACL table to file: %w", err)
}
cmd.Printf("extended ACL table was successfully dumped to %s\n", to)
return nil
}
func completeBearerToken(btok *token.BearerToken) error {
if v2 := btok.ToV2(); v2 != nil {
// set eACL table version, because it usually omitted