2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-15 10:40:58 +00:00
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2020-08-04 14:46:12 +00:00
|
|
|
"fmt"
|
2020-10-15 10:40:58 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"os"
|
2020-11-05 13:58:30 +00:00
|
|
|
"path/filepath"
|
2020-10-15 10:40:58 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-11-03 14:07:07 +00:00
|
|
|
"time"
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2020-08-04 14:46:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2020-11-20 08:56:21 +00:00
|
|
|
const (
|
|
|
|
getRangeCmdUse = "range"
|
|
|
|
|
|
|
|
getRangeCmdShortDesc = "Get payload range data of an object"
|
|
|
|
|
|
|
|
getRangeCmdLongDesc = "Get payload range data of an object"
|
|
|
|
)
|
|
|
|
|
2021-01-11 13:51:29 +00:00
|
|
|
const (
|
|
|
|
getRangeHashSaltFlag = "salt"
|
|
|
|
)
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
var (
|
|
|
|
// objectCmd represents the object command
|
|
|
|
objectCmd = &cobra.Command{
|
|
|
|
Use: "object",
|
|
|
|
Short: "Operations with Objects",
|
|
|
|
Long: `Operations with Objects`,
|
|
|
|
}
|
|
|
|
|
|
|
|
objectPutCmd = &cobra.Command{
|
|
|
|
Use: "put",
|
|
|
|
Short: "Put object to NeoFS",
|
|
|
|
Long: "Put object to NeoFS",
|
|
|
|
RunE: putObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
objectGetCmd = &cobra.Command{
|
|
|
|
Use: "get",
|
|
|
|
Short: "Get object from NeoFS",
|
|
|
|
Long: "Get object from NeoFS",
|
|
|
|
RunE: getObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
objectDelCmd = &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Aliases: []string{"del"},
|
|
|
|
Short: "Delete object from NeoFS",
|
|
|
|
Long: "Delete object from NeoFS",
|
|
|
|
RunE: deleteObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
objectSearchCmd = &cobra.Command{
|
|
|
|
Use: "search",
|
|
|
|
Short: "Search object",
|
|
|
|
Long: "Search object",
|
|
|
|
RunE: searchObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
objectHeadCmd = &cobra.Command{
|
|
|
|
Use: "head",
|
|
|
|
Short: "Get object header",
|
|
|
|
Long: "Get object header",
|
|
|
|
RunE: getObjectHeader,
|
|
|
|
}
|
|
|
|
|
|
|
|
objectHashCmd = &cobra.Command{
|
|
|
|
Use: "hash",
|
|
|
|
Short: "Get object hash",
|
|
|
|
Long: "Get object hash",
|
|
|
|
RunE: getObjectHash,
|
|
|
|
}
|
2020-11-20 08:56:21 +00:00
|
|
|
|
|
|
|
objectRangeCmd = &cobra.Command{
|
|
|
|
Use: getRangeCmdUse,
|
|
|
|
Short: getRangeCmdShortDesc,
|
|
|
|
Long: getRangeCmdLongDesc,
|
|
|
|
RunE: getObjectRange,
|
|
|
|
}
|
2020-10-15 10:40:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
hashSha256 = "sha256"
|
|
|
|
hashTz = "tz"
|
|
|
|
rangeSep = ":"
|
|
|
|
)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-11-20 08:08:40 +00:00
|
|
|
const searchOIDFlag = "oid"
|
|
|
|
|
2020-12-14 08:56:22 +00:00
|
|
|
const (
|
|
|
|
rawFlag = "raw"
|
|
|
|
rawFlagDesc = "Set raw request option"
|
|
|
|
)
|
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(objectCmd)
|
2020-10-22 15:08:08 +00:00
|
|
|
objectCmd.PersistentFlags().String("bearer", "", "File with signed JSON or binary encoded bearer token")
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
objectCmd.AddCommand(objectPutCmd)
|
|
|
|
objectPutCmd.Flags().String("file", "", "File with object payload")
|
|
|
|
_ = objectPutCmd.MarkFlagFilename("file")
|
|
|
|
_ = objectPutCmd.MarkFlagRequired("file")
|
|
|
|
objectPutCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = objectPutCmd.MarkFlagRequired("cid")
|
|
|
|
objectPutCmd.Flags().String("attributes", "", "User attributes in form of Key1=Value1,Key2=Value2")
|
2020-11-03 14:07:07 +00:00
|
|
|
objectPutCmd.Flags().Bool("disable-filename", false, "Do not set well-known filename attribute")
|
|
|
|
objectPutCmd.Flags().Bool("disable-timestamp", false, "Do not set well-known timestamp attribute")
|
2020-10-15 10:40:58 +00:00
|
|
|
|
|
|
|
objectCmd.AddCommand(objectDelCmd)
|
|
|
|
objectDelCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = objectDelCmd.MarkFlagRequired("cid")
|
|
|
|
objectDelCmd.Flags().String("oid", "", "Object ID")
|
|
|
|
_ = objectDelCmd.MarkFlagRequired("oid")
|
|
|
|
|
|
|
|
objectCmd.AddCommand(objectGetCmd)
|
|
|
|
objectGetCmd.Flags().String("file", "", "File to write object payload to. Default: stdout.")
|
|
|
|
objectGetCmd.Flags().String("header", "", "File to write header to. Default: stdout.")
|
|
|
|
objectGetCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = objectGetCmd.MarkFlagRequired("cid")
|
|
|
|
objectGetCmd.Flags().String("oid", "", "Object ID")
|
|
|
|
_ = objectGetCmd.MarkFlagRequired("oid")
|
2020-12-14 08:56:22 +00:00
|
|
|
objectGetCmd.Flags().Bool(rawFlag, false, rawFlagDesc)
|
2020-10-15 10:40:58 +00:00
|
|
|
|
|
|
|
objectCmd.AddCommand(objectSearchCmd)
|
|
|
|
objectSearchCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = objectSearchCmd.MarkFlagRequired("cid")
|
|
|
|
objectSearchCmd.Flags().String("filters", "", "Filters in the form hdrName=value,...")
|
|
|
|
objectSearchCmd.Flags().Bool("root", false, "Search for user objects")
|
2020-11-10 12:57:25 +00:00
|
|
|
objectSearchCmd.Flags().Bool("phy", false, "Search physically stored objects")
|
2020-11-20 08:08:40 +00:00
|
|
|
objectSearchCmd.Flags().String(searchOIDFlag, "", "Search object by identifier")
|
2020-10-15 10:40:58 +00:00
|
|
|
|
|
|
|
objectCmd.AddCommand(objectHeadCmd)
|
|
|
|
objectHeadCmd.Flags().String("file", "", "File to write header to. Default: stdout.")
|
|
|
|
objectHeadCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = objectHeadCmd.MarkFlagRequired("cid")
|
|
|
|
objectHeadCmd.Flags().String("oid", "", "Object ID")
|
|
|
|
_ = objectHeadCmd.MarkFlagRequired("oid")
|
|
|
|
objectHeadCmd.Flags().Bool("main-only", false, "Return only main fields")
|
|
|
|
objectHeadCmd.Flags().Bool("json", false, "Marshal output in JSON")
|
|
|
|
objectHeadCmd.Flags().Bool("proto", false, "Marshal output in Protobuf")
|
2020-12-14 08:56:22 +00:00
|
|
|
objectHeadCmd.Flags().Bool(rawFlag, false, rawFlagDesc)
|
2020-10-15 10:40:58 +00:00
|
|
|
|
|
|
|
objectCmd.AddCommand(objectHashCmd)
|
|
|
|
objectHashCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = objectHashCmd.MarkFlagRequired("cid")
|
|
|
|
objectHashCmd.Flags().String("oid", "", "Object ID")
|
|
|
|
_ = objectHashCmd.MarkFlagRequired("oid")
|
|
|
|
objectHashCmd.Flags().String("range", "", "Range to take hash from in the form offset1:length1,...")
|
|
|
|
objectHashCmd.Flags().String("type", hashSha256, "Hash type. Either 'sha256' or 'tz'")
|
2021-01-11 13:51:29 +00:00
|
|
|
objectHashCmd.Flags().String(getRangeHashSaltFlag, "", "Salt in hex format")
|
2020-10-15 10:40:58 +00:00
|
|
|
|
2020-11-20 08:56:21 +00:00
|
|
|
objectCmd.AddCommand(objectRangeCmd)
|
|
|
|
objectRangeCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = objectRangeCmd.MarkFlagRequired("cid")
|
|
|
|
objectRangeCmd.Flags().String("oid", "", "Object ID")
|
|
|
|
_ = objectRangeCmd.MarkFlagRequired("oid")
|
|
|
|
objectRangeCmd.Flags().String("range", "", "Range to take data from in the form offset:length")
|
|
|
|
objectRangeCmd.Flags().String("file", "", "File to write object payload to. Default: stdout.")
|
2020-12-14 08:56:22 +00:00
|
|
|
objectRangeCmd.Flags().Bool(rawFlag, false, rawFlagDesc)
|
2020-11-20 08:56:21 +00:00
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
|
|
// and all subcommands, e.g.:
|
|
|
|
// objectCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
|
|
// is called directly, e.g.:
|
|
|
|
// objectCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
}
|
2020-10-15 10:40:58 +00:00
|
|
|
|
|
|
|
func initSession(ctx context.Context) (*client.Client, *token.SessionToken, error) {
|
|
|
|
cli, err := getSDKClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("can't create client: %w", err)
|
|
|
|
}
|
|
|
|
tok, err := cli.CreateSession(ctx, math.MaxUint64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("can't create session: %w", err)
|
|
|
|
}
|
|
|
|
return cli, tok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func putObject(cmd *cobra.Command, _ []string) error {
|
|
|
|
ownerID, err := getOwnerID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cid, err := getCID(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := cmd.Flag("file").Value.String()
|
|
|
|
f, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't open file '%s': %w", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attrs, err := parseObjectAttrs(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't parse object attributes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := object.NewRaw()
|
|
|
|
obj.SetContainerID(cid)
|
|
|
|
obj.SetOwnerID(ownerID)
|
|
|
|
obj.SetAttributes(attrs...)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
cli, tok, err := initSession(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 15:08:08 +00:00
|
|
|
btok, err := getBearerToken(cmd, "bearer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-15 10:40:58 +00:00
|
|
|
oid, err := cli.PutObject(ctx,
|
|
|
|
new(client.PutObjectParams).
|
|
|
|
WithObject(obj.Object()).
|
|
|
|
WithPayloadReader(f),
|
2020-12-24 11:25:36 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
|
|
|
client.WithBearer(btok),
|
|
|
|
)...,
|
|
|
|
)
|
2020-10-15 10:40:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't put object: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("[%s] Object successfully stored\n", filename)
|
|
|
|
cmd.Printf(" ID: %s\n CID: %s\n", oid, cid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteObject(cmd *cobra.Command, _ []string) error {
|
|
|
|
objAddr, err := getObjectAddress(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
cli, tok, err := initSession(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 15:08:08 +00:00
|
|
|
btok, err := getBearerToken(cmd, "bearer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-11 08:23:23 +00:00
|
|
|
|
|
|
|
tombstoneAddr, err := client.DeleteObject(cli, ctx,
|
2020-10-15 10:40:58 +00:00
|
|
|
new(client.DeleteObjectParams).WithAddress(objAddr),
|
2020-12-24 11:25:36 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
|
|
|
client.WithBearer(btok),
|
|
|
|
)...,
|
|
|
|
)
|
2020-10-15 10:40:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Println("Object removed successfully.")
|
2020-12-11 08:23:23 +00:00
|
|
|
cmd.Printf(" ID: %s\n CID: %s\n", tombstoneAddr.ObjectID(), tombstoneAddr.ContainerID())
|
2020-10-15 10:40:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObject(cmd *cobra.Command, _ []string) error {
|
|
|
|
objAddr, err := getObjectAddress(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out io.Writer
|
|
|
|
filename := cmd.Flag("file").Value.String()
|
|
|
|
if filename == "" {
|
|
|
|
out = os.Stdout
|
|
|
|
} else {
|
|
|
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't open file '%s': %w", filename, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
out = f
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
cli, tok, err := initSession(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 15:08:08 +00:00
|
|
|
btok, err := getBearerToken(cmd, "bearer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-14 08:56:22 +00:00
|
|
|
|
|
|
|
raw, _ := cmd.Flags().GetBool(rawFlag)
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
obj, err := cli.GetObject(ctx,
|
|
|
|
new(client.GetObjectParams).
|
|
|
|
WithAddress(objAddr).
|
2020-12-14 08:56:22 +00:00
|
|
|
WithPayloadWriter(out).
|
|
|
|
WithRawFlag(raw),
|
2020-12-24 11:25:36 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
|
|
|
client.WithBearer(btok),
|
|
|
|
)...,
|
|
|
|
)
|
2020-10-15 10:40:58 +00:00
|
|
|
if err != nil {
|
2020-12-14 08:56:22 +00:00
|
|
|
if ok := printSplitInfoErr(cmd, err); ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
return fmt.Errorf("can't put object: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filename != "" {
|
|
|
|
cmd.Printf("[%s] Object successfully saved\n", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print header only if file is not streamed to stdout.
|
|
|
|
hdrFile := cmd.Flag("header").Value.String()
|
|
|
|
if filename != "" || hdrFile != "" {
|
2020-12-14 11:04:16 +00:00
|
|
|
return saveAndPrintHeader(cmd, obj, hdrFile)
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObjectHeader(cmd *cobra.Command, _ []string) error {
|
|
|
|
objAddr, err := getObjectAddress(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
cli, tok, err := initSession(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 15:08:08 +00:00
|
|
|
btok, err := getBearerToken(cmd, "bearer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-15 10:40:58 +00:00
|
|
|
ps := new(client.ObjectHeaderParams).WithAddress(objAddr)
|
|
|
|
if ok, _ := cmd.Flags().GetBool("main-only"); ok {
|
|
|
|
ps = ps.WithMainFields()
|
|
|
|
}
|
2020-12-14 08:56:22 +00:00
|
|
|
|
|
|
|
raw, _ := cmd.Flags().GetBool(rawFlag)
|
|
|
|
ps.WithRawFlag(raw)
|
|
|
|
|
2020-10-22 15:08:08 +00:00
|
|
|
obj, err := cli.GetObjectHeader(ctx, ps,
|
2020-12-24 11:25:36 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
|
|
|
client.WithBearer(btok),
|
|
|
|
)...,
|
|
|
|
)
|
2020-10-15 10:40:58 +00:00
|
|
|
if err != nil {
|
2020-12-14 08:56:22 +00:00
|
|
|
if ok := printSplitInfoErr(cmd, err); ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
return fmt.Errorf("can't put object: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-12-14 11:04:16 +00:00
|
|
|
return saveAndPrintHeader(cmd, obj, cmd.Flag("file").Value.String())
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func searchObject(cmd *cobra.Command, _ []string) error {
|
|
|
|
cid, err := getCID(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sf, err := parseSearchFilters(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
cli, tok, err := initSession(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 15:08:08 +00:00
|
|
|
btok, err := getBearerToken(cmd, "bearer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-15 10:40:58 +00:00
|
|
|
ps := new(client.SearchObjectParams).WithContainerID(cid).WithSearchFilters(sf)
|
2020-10-22 15:08:08 +00:00
|
|
|
ids, err := cli.SearchObject(ctx, ps,
|
2020-12-24 11:25:36 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
|
|
|
client.WithBearer(btok),
|
|
|
|
)...,
|
|
|
|
)
|
2020-10-15 10:40:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't put object: %w", err)
|
|
|
|
}
|
|
|
|
cmd.Printf("Found %d objects.\n", len(ids))
|
|
|
|
for _, id := range ids {
|
|
|
|
cmd.Println(id)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObjectHash(cmd *cobra.Command, _ []string) error {
|
|
|
|
objAddr, err := getObjectAddress(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ranges, err := getRangeList(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
typ, err := getHashType(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-11 14:43:38 +00:00
|
|
|
strSalt := strings.TrimPrefix(cmd.Flag(getRangeHashSaltFlag).Value.String(), "0x")
|
|
|
|
|
|
|
|
salt, err := hex.DecodeString(strSalt)
|
2021-01-11 13:51:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
cli, tok, err := initSession(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 15:08:08 +00:00
|
|
|
btok, err := getBearerToken(cmd, "bearer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-15 10:40:58 +00:00
|
|
|
if len(ranges) == 0 { // hash of full payload
|
|
|
|
obj, err := cli.GetObjectHeader(ctx,
|
|
|
|
new(client.ObjectHeaderParams).WithAddress(objAddr),
|
2020-12-24 11:25:36 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
|
|
|
client.WithBearer(btok),
|
|
|
|
)...,
|
|
|
|
)
|
2020-10-15 10:40:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get object: %w", err)
|
|
|
|
}
|
|
|
|
switch typ {
|
|
|
|
case hashSha256:
|
2020-11-16 09:43:52 +00:00
|
|
|
cmd.Println(hex.EncodeToString(obj.PayloadChecksum().Sum()))
|
2020-10-15 10:40:58 +00:00
|
|
|
case hashTz:
|
2020-11-16 09:43:52 +00:00
|
|
|
cmd.Println(hex.EncodeToString(obj.PayloadHomomorphicHash().Sum()))
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 13:51:29 +00:00
|
|
|
ps := new(client.RangeChecksumParams).
|
|
|
|
WithAddress(objAddr).
|
|
|
|
WithRangeList(ranges...).
|
|
|
|
WithSalt(salt)
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
switch typ {
|
|
|
|
case hashSha256:
|
2020-10-22 15:08:08 +00:00
|
|
|
res, err := cli.ObjectPayloadRangeSHA256(ctx, ps,
|
2020-12-24 11:25:36 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
|
|
|
client.WithBearer(btok),
|
|
|
|
)...,
|
|
|
|
)
|
2020-10-15 10:40:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range res {
|
|
|
|
cmd.Printf("Offset=%d (Length=%d)\t: %s\n", ranges[i].GetOffset(), ranges[i].GetLength(),
|
|
|
|
hex.EncodeToString(res[i][:]))
|
|
|
|
}
|
|
|
|
case hashTz:
|
2020-10-22 15:08:08 +00:00
|
|
|
res, err := cli.ObjectPayloadRangeTZ(ctx, ps,
|
2020-12-24 11:25:36 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
|
|
|
client.WithBearer(btok),
|
|
|
|
)...,
|
|
|
|
)
|
2020-10-15 10:40:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range res {
|
|
|
|
cmd.Printf("Offset=%d (Length=%d)\t: %s\n", ranges[i].GetOffset(), ranges[i].GetLength(),
|
|
|
|
hex.EncodeToString(res[i][:]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOwnerID() (*owner.ID, error) {
|
|
|
|
key, err := getKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
w, err := owner.NEO3WalletFromPublicKey(&key.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ownerID := owner.NewID()
|
|
|
|
ownerID.SetNeo3Wallet(w)
|
|
|
|
return ownerID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSearchFilters(cmd *cobra.Command) (object.SearchFilters, error) {
|
|
|
|
var fs object.SearchFilters
|
|
|
|
if raw := cmd.Flag("filters").Value.String(); len(raw) != 0 {
|
|
|
|
rawFs := strings.Split(raw, ",")
|
|
|
|
for i := range rawFs {
|
|
|
|
kv := strings.SplitN(rawFs[i], "=", 2)
|
|
|
|
if len(kv) != 2 {
|
|
|
|
return nil, fmt.Errorf("invalid filter format: %s", rawFs[i])
|
|
|
|
}
|
|
|
|
fs.AddFilter(kv[0], kv[1], object.MatchStringEqual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
root, _ := cmd.Flags().GetBool("root")
|
2020-11-10 12:57:25 +00:00
|
|
|
if root {
|
2020-10-15 10:40:58 +00:00
|
|
|
fs.AddRootFilter()
|
2020-11-10 12:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
phy, _ := cmd.Flags().GetBool("phy")
|
|
|
|
if phy {
|
|
|
|
fs.AddPhyFilter()
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 08:08:40 +00:00
|
|
|
oid, _ := cmd.Flags().GetString(searchOIDFlag)
|
|
|
|
if oid != "" {
|
|
|
|
id := object.NewID()
|
|
|
|
if err := id.Parse(oid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.AddObjectIDFilter(object.MatchStringEqual, id)
|
|
|
|
}
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
return fs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseObjectAttrs(cmd *cobra.Command) ([]*object.Attribute, error) {
|
2020-11-03 14:07:07 +00:00
|
|
|
var rawAttrs []string
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
raw := cmd.Flag("attributes").Value.String()
|
2020-11-03 14:07:07 +00:00
|
|
|
if len(raw) != 0 {
|
|
|
|
rawAttrs = strings.Split(raw, ",")
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
2020-11-03 14:07:07 +00:00
|
|
|
|
|
|
|
attrs := make([]*object.Attribute, 0, len(rawAttrs)+2) // name + timestamp attributes
|
2020-10-15 10:40:58 +00:00
|
|
|
for i := range rawAttrs {
|
|
|
|
kv := strings.SplitN(rawAttrs[i], "=", 2)
|
|
|
|
if len(kv) != 2 {
|
|
|
|
return nil, fmt.Errorf("invalid attribute format: %s", rawAttrs[i])
|
|
|
|
}
|
2020-11-03 14:07:07 +00:00
|
|
|
attr := object.NewAttribute()
|
|
|
|
attr.SetKey(kv[0])
|
|
|
|
attr.SetValue(kv[1])
|
|
|
|
attrs = append(attrs, attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
disableFilename, _ := cmd.Flags().GetBool("disable-filename")
|
|
|
|
if !disableFilename {
|
2020-11-05 13:58:30 +00:00
|
|
|
filename := filepath.Base(cmd.Flag("file").Value.String())
|
2020-11-03 14:07:07 +00:00
|
|
|
attr := object.NewAttribute()
|
|
|
|
attr.SetKey(object.AttributeFileName)
|
|
|
|
attr.SetValue(filename)
|
|
|
|
attrs = append(attrs, attr)
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
2020-11-03 14:07:07 +00:00
|
|
|
|
|
|
|
disableTime, _ := cmd.Flags().GetBool("disable-timestamp")
|
|
|
|
if !disableTime {
|
|
|
|
attr := object.NewAttribute()
|
|
|
|
attr.SetKey(object.AttributeTimestamp)
|
|
|
|
attr.SetValue(strconv.FormatInt(time.Now().Unix(), 10))
|
|
|
|
attrs = append(attrs, attr)
|
|
|
|
}
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
return attrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCID(cmd *cobra.Command) (*container.ID, error) {
|
|
|
|
cid := container.NewID()
|
2020-10-19 11:51:47 +00:00
|
|
|
err := cid.Parse(cmd.Flag("cid").Value.String())
|
|
|
|
|
|
|
|
return cid, err
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getOID(cmd *cobra.Command) (*object.ID, error) {
|
|
|
|
oid := object.NewID()
|
2020-10-19 11:51:47 +00:00
|
|
|
err := oid.Parse(cmd.Flag("oid").Value.String())
|
|
|
|
|
|
|
|
return oid, err
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getObjectAddress(cmd *cobra.Command) (*object.Address, error) {
|
|
|
|
cid, err := getCID(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
oid, err := getOID(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
objAddr := object.NewAddress()
|
|
|
|
objAddr.SetContainerID(cid)
|
|
|
|
objAddr.SetObjectID(oid)
|
|
|
|
return objAddr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRangeList(cmd *cobra.Command) ([]*object.Range, error) {
|
|
|
|
v := cmd.Flag("range").Value.String()
|
|
|
|
if len(v) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
vs := strings.Split(v, ",")
|
|
|
|
rs := make([]*object.Range, len(vs))
|
|
|
|
for i := range vs {
|
|
|
|
r := strings.Split(vs[i], rangeSep)
|
|
|
|
if len(r) != 2 {
|
|
|
|
return nil, fmt.Errorf("invalid range specifier: %s", vs[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
offset, err := strconv.ParseUint(r[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid range specifier: %s", vs[i])
|
|
|
|
}
|
|
|
|
length, err := strconv.ParseUint(r[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid range specifier: %s", vs[i])
|
|
|
|
}
|
|
|
|
rs[i] = object.NewRange()
|
|
|
|
rs[i].SetOffset(offset)
|
|
|
|
rs[i].SetLength(length)
|
|
|
|
}
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHashType(cmd *cobra.Command) (string, error) {
|
|
|
|
rawType := cmd.Flag("type").Value.String()
|
|
|
|
switch typ := strings.ToLower(rawType); typ {
|
|
|
|
case hashSha256, hashTz:
|
|
|
|
return typ, nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("invalid hash type: %s", typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 11:04:16 +00:00
|
|
|
func saveAndPrintHeader(cmd *cobra.Command, obj *object.Object, filename string) error {
|
2020-10-15 10:40:58 +00:00
|
|
|
bs, err := marshalHeader(cmd, obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(bs) != 0 {
|
|
|
|
if filename == "" {
|
|
|
|
cmd.Println(string(bs))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := ioutil.WriteFile(filename, bs, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.Printf("[%s] Header successfully saved.", filename)
|
|
|
|
}
|
|
|
|
|
2020-12-14 11:04:16 +00:00
|
|
|
return printHeader(cmd, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printHeader(cmd *cobra.Command, obj *object.Object) error {
|
2020-11-16 09:43:52 +00:00
|
|
|
cmd.Printf("ID: %s\n", obj.ID())
|
|
|
|
cmd.Printf("CID: %s\n", obj.ContainerID())
|
|
|
|
cmd.Printf("Owner: %s\n", obj.OwnerID())
|
|
|
|
cmd.Printf("CreatedAt: %d\n", obj.CreationEpoch())
|
|
|
|
cmd.Printf("Size: %d\n", obj.PayloadSize())
|
|
|
|
cmd.Printf("HomoHash: %s\n", hex.EncodeToString(obj.PayloadHomomorphicHash().Sum()))
|
|
|
|
cmd.Printf("Checksum: %s\n", hex.EncodeToString(obj.PayloadChecksum().Sum()))
|
|
|
|
switch obj.Type() {
|
2020-10-15 10:40:58 +00:00
|
|
|
case object.TypeRegular:
|
|
|
|
cmd.Println("Type: regular")
|
|
|
|
case object.TypeTombstone:
|
|
|
|
cmd.Println("Type: tombstone")
|
|
|
|
case object.TypeStorageGroup:
|
|
|
|
cmd.Println("Type: storage group")
|
|
|
|
default:
|
|
|
|
cmd.Println("Type: unknown")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Println("Attributes:")
|
2020-11-16 09:43:52 +00:00
|
|
|
for _, attr := range obj.Attributes() {
|
|
|
|
if attr.Key() == object.AttributeTimestamp {
|
2020-11-03 14:07:07 +00:00
|
|
|
cmd.Printf(" %s=%s (%s)\n",
|
2020-11-16 09:43:52 +00:00
|
|
|
attr.Key(),
|
|
|
|
attr.Value(),
|
|
|
|
prettyPrintUnixTime(attr.Value()))
|
2020-11-03 14:07:07 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-11-16 09:43:52 +00:00
|
|
|
cmd.Printf(" %s=%s\n", attr.Key(), attr.Value())
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
2020-11-24 18:19:54 +00:00
|
|
|
|
2020-12-14 11:04:16 +00:00
|
|
|
return printSplitHeader(cmd, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printSplitHeader(cmd *cobra.Command, obj *object.Object) error {
|
|
|
|
if splitID := obj.SplitID(); splitID != nil {
|
|
|
|
cmd.Printf("Split ID: %s\n", splitID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if oid := obj.ParentID(); oid != nil {
|
|
|
|
cmd.Printf("Split ParentID: %s\n", oid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prev := obj.PreviousID(); prev != nil {
|
|
|
|
cmd.Printf("Split PreviousID: %s\n", prev)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, child := range obj.Children() {
|
|
|
|
cmd.Printf("Split ChildID: %s\n", child)
|
|
|
|
}
|
|
|
|
|
|
|
|
if signature := obj.Signature(); signature != nil {
|
|
|
|
cmd.Print("Split Header Signature:\n")
|
|
|
|
cmd.Printf(" public key: %s\n", hex.EncodeToString(signature.Key()))
|
|
|
|
cmd.Printf(" signature: %s\n", hex.EncodeToString(signature.Sign()))
|
|
|
|
}
|
|
|
|
|
|
|
|
parent := obj.Parent()
|
|
|
|
if parent != nil {
|
|
|
|
cmd.Print("\nSplit Parent Header:\n")
|
|
|
|
|
|
|
|
return printHeader(cmd, parent)
|
2020-11-24 18:19:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalHeader(cmd *cobra.Command, hdr *object.Object) ([]byte, error) {
|
2021-01-11 15:17:42 +00:00
|
|
|
toJSON, _ := cmd.Flags().GetBool("json")
|
2020-10-15 10:40:58 +00:00
|
|
|
toProto, _ := cmd.Flags().GetBool("proto")
|
|
|
|
switch {
|
2021-01-11 15:17:42 +00:00
|
|
|
case toJSON && toProto:
|
2020-10-15 10:40:58 +00:00
|
|
|
return nil, errors.New("'--json' and '--proto' flags are mutually exclusive")
|
2021-01-11 15:17:42 +00:00
|
|
|
case toJSON:
|
2020-11-16 10:26:35 +00:00
|
|
|
return hdr.MarshalJSON()
|
2020-10-15 10:40:58 +00:00
|
|
|
case toProto:
|
2020-11-16 10:26:35 +00:00
|
|
|
return hdr.Marshal()
|
2020-10-15 10:40:58 +00:00
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 15:08:08 +00:00
|
|
|
|
|
|
|
func getBearerToken(cmd *cobra.Command, flagname string) (*token.BearerToken, error) {
|
|
|
|
path, err := cmd.Flags().GetString(flagname)
|
|
|
|
if err != nil || len(path) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read bearer token file: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
tok := token.NewBearerToken()
|
|
|
|
if err := tok.UnmarshalJSON(data); err != nil {
|
|
|
|
if err = tok.Unmarshal(data); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't decode bearer token: %w", err)
|
2020-10-22 15:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printVerbose("Using binary encoded bearer token")
|
|
|
|
} else {
|
|
|
|
printVerbose("Using JSON encoded bearer token")
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
return tok, nil
|
2020-10-22 15:08:08 +00:00
|
|
|
}
|
2020-11-20 08:56:21 +00:00
|
|
|
|
|
|
|
func getObjectRange(cmd *cobra.Command, _ []string) error {
|
|
|
|
objAddr, err := getObjectAddress(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ranges, err := getRangeList(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(ranges) != 1 {
|
|
|
|
return errors.New("exactly one range must be specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
var out io.Writer
|
|
|
|
|
|
|
|
filename := cmd.Flag("file").Value.String()
|
|
|
|
if filename == "" {
|
|
|
|
out = os.Stdout
|
|
|
|
} else {
|
|
|
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't open file '%s': %w", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
out = f
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
c, sessionToken, err := initSession(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bearerToken, err := getBearerToken(cmd, "bearer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-14 08:56:22 +00:00
|
|
|
raw, _ := cmd.Flags().GetBool(rawFlag)
|
|
|
|
|
2020-11-20 08:56:21 +00:00
|
|
|
_, err = c.ObjectPayloadRangeData(ctx,
|
|
|
|
new(client.RangeDataParams).
|
|
|
|
WithAddress(objAddr).
|
|
|
|
WithRange(ranges[0]).
|
2020-12-14 08:56:22 +00:00
|
|
|
WithDataWriter(out).
|
|
|
|
WithRaw(raw),
|
2020-12-24 11:25:36 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(sessionToken),
|
|
|
|
client.WithBearer(bearerToken),
|
|
|
|
)...,
|
|
|
|
)
|
2020-11-20 08:56:21 +00:00
|
|
|
if err != nil {
|
2020-12-14 08:56:22 +00:00
|
|
|
if ok := printSplitInfoErr(cmd, err); ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-20 08:56:21 +00:00
|
|
|
return fmt.Errorf("can't get object payload range: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filename != "" {
|
|
|
|
cmd.Printf("[%s] Payload successfully saved\n", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-14 08:56:22 +00:00
|
|
|
|
|
|
|
func printSplitInfoErr(cmd *cobra.Command, err error) bool {
|
|
|
|
var errSplitInfo *object.SplitInfoError
|
|
|
|
|
|
|
|
ok := errors.As(err, &errSplitInfo)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
cmd.Println("Object is complex, split information received.")
|
|
|
|
printSplitInfo(cmd, errSplitInfo.SplitInfo())
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func printSplitInfo(cmd *cobra.Command, info *object.SplitInfo) {
|
|
|
|
if splitID := info.SplitID(); splitID != nil {
|
|
|
|
cmd.Println("Split ID:", splitID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if link := info.Link(); link != nil {
|
|
|
|
cmd.Println("Linking object:", link)
|
|
|
|
}
|
|
|
|
|
|
|
|
if last := info.LastPart(); last != nil {
|
|
|
|
cmd.Println("Last object:", last)
|
|
|
|
}
|
|
|
|
}
|