2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-06-22 16:21:18 +00:00
|
|
|
"crypto/ecdsa"
|
2020-10-15 10:40:58 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2020-08-04 14:46:12 +00:00
|
|
|
"fmt"
|
2020-10-15 10:40:58 +00:00
|
|
|
"io"
|
|
|
|
"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
|
|
|
|
2021-02-21 09:51:33 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2021-10-28 14:48:46 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/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"
|
|
|
|
)
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
const bearerTokenFlag = "bearer"
|
|
|
|
|
2022-02-07 09:33:27 +00:00
|
|
|
const sessionTokenLifetime = 10 // in epochs
|
|
|
|
|
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`,
|
2021-09-27 15:36:14 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
// bind exactly that cmd's flags to
|
|
|
|
// the viper before execution
|
|
|
|
bindCommonFlags(cmd)
|
2021-10-07 10:37:57 +00:00
|
|
|
bindAPIFlags(cmd)
|
2021-09-27 15:36:14 +00:00
|
|
|
},
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
objectPutCmd = &cobra.Command{
|
|
|
|
Use: "put",
|
|
|
|
Short: "Put object to NeoFS",
|
|
|
|
Long: "Put object to NeoFS",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: putObject,
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
objectGetCmd = &cobra.Command{
|
|
|
|
Use: "get",
|
|
|
|
Short: "Get object from NeoFS",
|
|
|
|
Long: "Get object from NeoFS",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: getObject,
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
objectDelCmd = &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Aliases: []string{"del"},
|
|
|
|
Short: "Delete object from NeoFS",
|
|
|
|
Long: "Delete object from NeoFS",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: deleteObject,
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 22:03:46 +00:00
|
|
|
searchFilters []string
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
objectSearchCmd = &cobra.Command{
|
|
|
|
Use: "search",
|
|
|
|
Short: "Search object",
|
|
|
|
Long: "Search object",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: searchObject,
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
objectHeadCmd = &cobra.Command{
|
|
|
|
Use: "head",
|
|
|
|
Short: "Get object header",
|
|
|
|
Long: "Get object header",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: getObjectHeader,
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
objectHashCmd = &cobra.Command{
|
|
|
|
Use: "hash",
|
|
|
|
Short: "Get object hash",
|
|
|
|
Long: "Get object hash",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: getObjectHash,
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
2020-11-20 08:56:21 +00:00
|
|
|
|
|
|
|
objectRangeCmd = &cobra.Command{
|
|
|
|
Use: getRangeCmdUse,
|
|
|
|
Short: getRangeCmdShortDesc,
|
|
|
|
Long: getRangeCmdLongDesc,
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: getObjectRange,
|
2020-11-20 08:56:21 +00:00
|
|
|
}
|
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"
|
|
|
|
)
|
|
|
|
|
2021-02-21 09:51:33 +00:00
|
|
|
const putExpiresOnFlag = "expires-on"
|
|
|
|
|
|
|
|
var putExpiredOn uint64
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
func initObjectPutCmd() {
|
|
|
|
initCommonFlags(objectPutCmd)
|
|
|
|
|
|
|
|
flags := objectPutCmd.Flags()
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
flags.String("file", "", "File with object payload")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectPutCmd.MarkFlagFilename("file")
|
|
|
|
_ = objectPutCmd.MarkFlagRequired("file")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.String("cid", "", "Container ID")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectPutCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.String("attributes", "", "User attributes in form of Key1=Value1,Key2=Value2")
|
|
|
|
flags.Bool("disable-filename", false, "Do not set well-known filename attribute")
|
|
|
|
flags.Bool("disable-timestamp", false, "Do not set well-known timestamp attribute")
|
|
|
|
flags.Uint64VarP(&putExpiredOn, putExpiresOnFlag, "e", 0, "Last epoch in the life of the object")
|
|
|
|
}
|
|
|
|
|
|
|
|
func initObjectDeleteCmd() {
|
|
|
|
initCommonFlags(objectDelCmd)
|
|
|
|
|
|
|
|
flags := objectDelCmd.Flags()
|
|
|
|
|
|
|
|
flags.String("cid", "", "Container ID")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectDelCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.String("oid", "", "Object ID")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectDelCmd.MarkFlagRequired("oid")
|
2021-09-27 15:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initObjectGetCmd() {
|
|
|
|
initCommonFlags(objectGetCmd)
|
2020-10-15 10:40:58 +00:00
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
flags := objectGetCmd.Flags()
|
|
|
|
|
|
|
|
flags.String("cid", "", "Container ID")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectGetCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.String("oid", "", "Object ID")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectGetCmd.MarkFlagRequired("oid")
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
flags.String("file", "", "File to write object payload to. Default: stdout.")
|
|
|
|
flags.String("header", "", "File to write header to. Default: stdout.")
|
|
|
|
flags.Bool(rawFlag, false, rawFlagDesc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initObjectSearchCmd() {
|
|
|
|
initCommonFlags(objectSearchCmd)
|
|
|
|
|
|
|
|
flags := objectSearchCmd.Flags()
|
|
|
|
|
|
|
|
flags.String("cid", "", "Container ID")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectSearchCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.StringSliceVarP(&searchFilters, "filters", "f", nil,
|
2021-02-10 22:07:48 +00:00
|
|
|
"Repeated filter expressions or files with protobuf JSON")
|
2020-10-15 10:40:58 +00:00
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
flags.Bool("root", false, "Search for user objects")
|
|
|
|
flags.Bool("phy", false, "Search physically stored objects")
|
|
|
|
flags.String(searchOIDFlag, "", "Search object by identifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
func initObjectHeadCmd() {
|
|
|
|
initCommonFlags(objectHeadCmd)
|
|
|
|
|
|
|
|
flags := objectHeadCmd.Flags()
|
|
|
|
|
|
|
|
flags.String("cid", "", "Container ID")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectHeadCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.String("oid", "", "Object ID")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectHeadCmd.MarkFlagRequired("oid")
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
flags.String("file", "", "File to write header to. Default: stdout.")
|
|
|
|
flags.Bool("main-only", false, "Return only main fields")
|
|
|
|
flags.Bool("json", false, "Marshal output in JSON")
|
|
|
|
flags.Bool("proto", false, "Marshal output in Protobuf")
|
|
|
|
flags.Bool(rawFlag, false, rawFlagDesc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initObjectHashCmd() {
|
|
|
|
initCommonFlags(objectHashCmd)
|
|
|
|
|
|
|
|
flags := objectHashCmd.Flags()
|
|
|
|
|
|
|
|
flags.String("cid", "", "Container ID")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectHashCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.String("oid", "", "Object ID")
|
2020-10-15 10:40:58 +00:00
|
|
|
_ = objectHashCmd.MarkFlagRequired("oid")
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
flags.String("range", "", "Range to take hash from in the form offset1:length1,...")
|
|
|
|
flags.String("type", hashSha256, "Hash type. Either 'sha256' or 'tz'")
|
|
|
|
flags.String(getRangeHashSaltFlag, "", "Salt in hex format")
|
|
|
|
}
|
|
|
|
|
|
|
|
func initObjectRangeCmd() {
|
|
|
|
initCommonFlags(objectRangeCmd)
|
|
|
|
|
|
|
|
flags := objectRangeCmd.Flags()
|
|
|
|
|
|
|
|
flags.String("cid", "", "Container ID")
|
2020-11-20 08:56:21 +00:00
|
|
|
_ = objectRangeCmd.MarkFlagRequired("cid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.String("oid", "", "Object ID")
|
2020-11-20 08:56:21 +00:00
|
|
|
_ = objectRangeCmd.MarkFlagRequired("oid")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
flags.String("range", "", "Range to take data from in the form offset:length")
|
|
|
|
flags.String("file", "", "File to write object payload to. Default: stdout.")
|
|
|
|
flags.Bool(rawFlag, false, rawFlagDesc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
objectChildCommands := []*cobra.Command{
|
|
|
|
objectPutCmd,
|
|
|
|
objectDelCmd,
|
|
|
|
objectGetCmd,
|
|
|
|
objectSearchCmd,
|
|
|
|
objectHeadCmd,
|
|
|
|
objectHashCmd,
|
|
|
|
objectRangeCmd,
|
2022-02-16 15:56:16 +00:00
|
|
|
cmdObjectLock,
|
2021-09-27 15:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rootCmd.AddCommand(objectCmd)
|
|
|
|
objectCmd.AddCommand(objectChildCommands...)
|
|
|
|
|
|
|
|
for _, objCommand := range objectChildCommands {
|
|
|
|
flags := objCommand.Flags()
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
flags.String(bearerTokenFlag, "", "File with signed JSON or binary encoded bearer token")
|
2021-09-27 15:36:14 +00:00
|
|
|
flags.StringSliceVarP(&xHeaders, xHeadersKey, xHeadersShorthand, xHeadersDefault, xHeadersUsage)
|
|
|
|
flags.Uint32P(ttl, ttlShorthand, ttlDefault, ttlUsage)
|
|
|
|
}
|
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")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
initObjectPutCmd()
|
|
|
|
initObjectDeleteCmd()
|
|
|
|
initObjectGetCmd()
|
|
|
|
initObjectSearchCmd()
|
|
|
|
initObjectHeadCmd()
|
|
|
|
initObjectHashCmd()
|
|
|
|
initObjectRangeCmd()
|
2022-02-16 15:56:16 +00:00
|
|
|
initCommandObjectLock()
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
2020-10-15 10:40:58 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
type clientKeySession interface {
|
|
|
|
clientWithKey
|
|
|
|
SetSessionToken(*session.Token)
|
|
|
|
}
|
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
func prepareSessionPrm(cmd *cobra.Command, addr *addressSDK.Address, prms ...clientKeySession) {
|
2021-10-28 14:48:46 +00:00
|
|
|
key, err := getKey()
|
|
|
|
exitOnErr(cmd, errf("get private key: %w", err))
|
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
prepareSessionPrmWithKey(cmd, addr, key, prms...)
|
2021-10-28 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
func prepareSessionPrmWithKey(cmd *cobra.Command, addr *addressSDK.Address, key *ecdsa.PrivateKey, prms ...clientKeySession) {
|
2021-10-28 14:48:46 +00:00
|
|
|
ownerID, err := getOwnerID(key)
|
|
|
|
exitOnErr(cmd, errf("owner ID from key: %w", err))
|
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
prepareSessionPrmWithOwner(cmd, addr, key, ownerID, prms...)
|
2021-10-28 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func prepareSessionPrmWithOwner(
|
|
|
|
cmd *cobra.Command,
|
2022-03-03 08:29:14 +00:00
|
|
|
addr *addressSDK.Address,
|
2021-10-28 14:48:46 +00:00
|
|
|
key *ecdsa.PrivateKey,
|
|
|
|
ownerID *owner.ID,
|
|
|
|
prms ...clientKeySession,
|
|
|
|
) {
|
2022-02-07 09:33:27 +00:00
|
|
|
var (
|
|
|
|
sessionPrm internalclient.CreateSessionPrm
|
|
|
|
netInfoPrm internalclient.NetworkInfoPrm
|
|
|
|
)
|
2021-10-28 14:48:46 +00:00
|
|
|
|
2022-02-07 09:33:27 +00:00
|
|
|
cws := make([]clientWithKey, 2, len(prms)+2)
|
2021-10-28 14:48:46 +00:00
|
|
|
cws[0] = &sessionPrm
|
2022-02-07 09:33:27 +00:00
|
|
|
cws[1] = &netInfoPrm
|
2021-10-28 14:48:46 +00:00
|
|
|
|
|
|
|
for i := range prms {
|
|
|
|
cws = append(cws, prms[i])
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
2021-10-28 14:48:46 +00:00
|
|
|
|
|
|
|
prepareAPIClientWithKey(cmd, key, cws...)
|
|
|
|
|
2022-02-07 09:33:27 +00:00
|
|
|
ni, err := internalclient.NetworkInfo(netInfoPrm)
|
|
|
|
exitOnErr(cmd, errf("read network info: %w", err))
|
|
|
|
|
2022-03-03 08:17:06 +00:00
|
|
|
cur := ni.NetworkInfo().CurrentEpoch()
|
|
|
|
exp := cur + sessionTokenLifetime
|
|
|
|
sessionPrm.SetExp(exp)
|
2022-02-07 09:33:27 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
sessionRes, err := internalclient.CreateSession(sessionPrm)
|
|
|
|
exitOnErr(cmd, errf("open session: %w", err))
|
|
|
|
|
|
|
|
for i := range prms {
|
2022-03-03 08:17:06 +00:00
|
|
|
objectContext := session.NewObjectContext()
|
|
|
|
switch prms[i].(type) {
|
|
|
|
case *internalclient.GetObjectPrm:
|
|
|
|
objectContext.ForGet()
|
|
|
|
case *internalclient.HeadObjectPrm:
|
|
|
|
objectContext.ForHead()
|
|
|
|
case *internalclient.PutObjectPrm:
|
|
|
|
objectContext.ForPut()
|
|
|
|
case *internalclient.DeleteObjectPrm:
|
|
|
|
objectContext.ForDelete()
|
|
|
|
case *internalclient.SearchObjectsPrm:
|
|
|
|
objectContext.ForSearch()
|
|
|
|
case *internalclient.PayloadRangePrm:
|
|
|
|
objectContext.ForRange()
|
|
|
|
case *internalclient.HashPayloadRangesPrm:
|
|
|
|
objectContext.ForRangeHash()
|
|
|
|
default:
|
|
|
|
panic("invalid client parameter type")
|
|
|
|
}
|
2022-03-03 08:29:14 +00:00
|
|
|
objectContext.ApplyTo(addr)
|
2022-03-03 08:17:06 +00:00
|
|
|
|
|
|
|
tok := session.NewToken()
|
|
|
|
tok.SetID(sessionRes.ID())
|
|
|
|
tok.SetSessionKey(sessionRes.SessionKey())
|
|
|
|
tok.SetOwnerID(ownerID)
|
|
|
|
tok.SetContext(objectContext)
|
|
|
|
tok.SetExp(exp)
|
|
|
|
tok.SetIat(cur)
|
|
|
|
tok.SetNbf(cur)
|
|
|
|
|
|
|
|
err = tok.Sign(key)
|
|
|
|
exitOnErr(cmd, errf("session token signing: %w", err))
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
prms[i].SetSessionToken(tok)
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
2021-10-28 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type objectPrm interface {
|
|
|
|
bearerPrm
|
|
|
|
SetTTL(uint32)
|
2021-11-10 07:08:33 +00:00
|
|
|
SetXHeaders([]*session.XHeader)
|
2021-10-28 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func prepareObjectPrm(cmd *cobra.Command, prms ...objectPrm) {
|
|
|
|
for i := range prms {
|
|
|
|
prepareBearerPrm(cmd, prms[i])
|
|
|
|
|
|
|
|
prms[i].SetTTL(getTTL())
|
|
|
|
prms[i].SetXHeaders(parseXHeaders())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareObjectPrmRaw(cmd *cobra.Command, prm interface {
|
|
|
|
objectPrm
|
|
|
|
SetRawFlag(bool)
|
|
|
|
}) {
|
|
|
|
prepareObjectPrm(cmd, prm)
|
|
|
|
|
|
|
|
raw, _ := cmd.Flags().GetBool(rawFlag)
|
|
|
|
prm.SetRawFlag(raw)
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func putObject(cmd *cobra.Command, _ []string) {
|
2021-06-22 16:21:18 +00:00
|
|
|
key, err := getKey()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("can't fetch private key: %w", err))
|
2021-06-22 16:21:18 +00:00
|
|
|
|
|
|
|
ownerID, err := getOwnerID(key)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
cid, err := getCID(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
|
|
|
|
filename := cmd.Flag("file").Value.String()
|
|
|
|
f, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm)
|
|
|
|
if err != nil {
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, fmt.Errorf("can't open file '%s': %w", filename, err))
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
attrs, err := parseObjectAttrs(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("can't parse object attributes: %w", err))
|
2020-10-15 10:40:58 +00:00
|
|
|
|
2021-02-21 09:51:33 +00:00
|
|
|
expiresOn, _ := cmd.Flags().GetUint64(putExpiresOnFlag)
|
|
|
|
if expiresOn > 0 {
|
|
|
|
var expAttr *object.Attribute
|
|
|
|
|
|
|
|
for _, a := range attrs {
|
|
|
|
if a.Key() == objectV2.SysAttributeExpEpoch {
|
|
|
|
expAttr = a
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if expAttr == nil {
|
|
|
|
expAttr = object.NewAttribute()
|
|
|
|
expAttr.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
attrs = append(attrs, expAttr)
|
|
|
|
}
|
|
|
|
|
|
|
|
expAttr.SetValue(strconv.FormatUint(expiresOn, 10))
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := object.New()
|
2020-10-15 10:40:58 +00:00
|
|
|
obj.SetContainerID(cid)
|
|
|
|
obj.SetOwnerID(ownerID)
|
|
|
|
obj.SetAttributes(attrs...)
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.PutObjectPrm
|
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
sessionObjectCtxAddress := addressSDK.NewAddress()
|
|
|
|
sessionObjectCtxAddress.SetContainerID(cid)
|
|
|
|
prepareSessionPrmWithOwner(cmd, sessionObjectCtxAddress, key, ownerID, &prm)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrm(cmd, &prm)
|
2022-03-03 14:19:05 +00:00
|
|
|
prm.SetHeader(obj)
|
2021-10-28 14:48:46 +00:00
|
|
|
prm.SetPayloadReader(f)
|
|
|
|
|
|
|
|
res, err := internalclient.PutObject(prm)
|
2021-09-01 16:33:48 +00:00
|
|
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
2020-10-15 10:40:58 +00:00
|
|
|
|
|
|
|
cmd.Printf("[%s] Object successfully stored\n", filename)
|
2021-10-28 14:48:46 +00:00
|
|
|
cmd.Printf(" ID: %s\n CID: %s\n", res.ID(), cid)
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func deleteObject(cmd *cobra.Command, _ []string) {
|
2020-10-15 10:40:58 +00:00
|
|
|
objAddr, err := getObjectAddress(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.DeleteObjectPrm
|
2020-12-11 08:23:23 +00:00
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
prepareSessionPrm(cmd, objAddr, &prm)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrm(cmd, &prm)
|
|
|
|
prm.SetAddress(objAddr)
|
|
|
|
|
|
|
|
res, err := internalclient.DeleteObject(prm)
|
2021-09-01 16:33:48 +00:00
|
|
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
2020-10-15 10:40:58 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
tombstoneAddr := res.TombstoneAddress()
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func getObject(cmd *cobra.Command, _ []string) {
|
2020-10-15 10:40:58 +00:00
|
|
|
objAddr, err := getObjectAddress(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
|
|
|
|
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 {
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, fmt.Errorf("can't open file '%s': %w", filename, err))
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
2021-07-06 12:27:54 +00:00
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
defer f.Close()
|
2021-07-06 12:27:54 +00:00
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
out = f
|
|
|
|
}
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.GetObjectPrm
|
2020-12-14 08:56:22 +00:00
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
prepareSessionPrm(cmd, objAddr, &prm)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrmRaw(cmd, &prm)
|
|
|
|
prm.SetAddress(objAddr)
|
|
|
|
prm.SetPayloadWriter(out)
|
2020-12-14 08:56:22 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
res, err := internalclient.GetObject(prm)
|
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 {
|
2021-06-20 23:02:56 +00:00
|
|
|
return
|
2020-12-14 08:56:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:33:48 +00:00
|
|
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 != "" {
|
2021-10-28 14:48:46 +00:00
|
|
|
err = saveAndPrintHeader(cmd, res.Header(), hdrFile)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func getObjectHeader(cmd *cobra.Command, _ []string) {
|
2020-10-15 10:40:58 +00:00
|
|
|
objAddr, err := getObjectAddress(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
mainOnly, _ := cmd.Flags().GetBool("main-only")
|
2020-12-14 08:56:22 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.HeadObjectPrm
|
2020-12-14 08:56:22 +00:00
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
prepareSessionPrm(cmd, objAddr, &prm)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrmRaw(cmd, &prm)
|
|
|
|
prm.SetAddress(objAddr)
|
|
|
|
prm.SetMainOnlyFlag(mainOnly)
|
|
|
|
|
|
|
|
res, err := internalclient.HeadObject(prm)
|
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 {
|
2021-06-20 23:02:56 +00:00
|
|
|
return
|
2020-12-14 08:56:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:33:48 +00:00
|
|
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
err = saveAndPrintHeader(cmd, res.Header(), cmd.Flag("file").Value.String())
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func searchObject(cmd *cobra.Command, _ []string) {
|
2020-10-15 10:40:58 +00:00
|
|
|
cid, err := getCID(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
|
|
|
|
sf, err := parseSearchFilters(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.SearchObjectsPrm
|
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
sessionObjectCtxAddress := addressSDK.NewAddress()
|
|
|
|
sessionObjectCtxAddress.SetContainerID(cid)
|
|
|
|
prepareSessionPrm(cmd, sessionObjectCtxAddress, &prm)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrm(cmd, &prm)
|
|
|
|
prm.SetContainerID(cid)
|
|
|
|
prm.SetFilters(sf)
|
|
|
|
|
|
|
|
res, err := internalclient.SearchObjects(prm)
|
2021-09-01 16:33:48 +00:00
|
|
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
2021-10-28 14:48:46 +00:00
|
|
|
|
|
|
|
ids := res.IDList()
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
cmd.Printf("Found %d objects.\n", len(ids))
|
|
|
|
for _, id := range ids {
|
|
|
|
cmd.Println(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func getObjectHash(cmd *cobra.Command, _ []string) {
|
2020-10-15 10:40:58 +00:00
|
|
|
objAddr, err := getObjectAddress(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
ranges, err := getRangeList(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
typ, err := getHashType(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-10-15 10:40:58 +00:00
|
|
|
|
2021-01-11 14:43:38 +00:00
|
|
|
strSalt := strings.TrimPrefix(cmd.Flag(getRangeHashSaltFlag).Value.String(), "0x")
|
|
|
|
|
|
|
|
salt, err := hex.DecodeString(strSalt)
|
2021-09-01 16:33:48 +00:00
|
|
|
exitOnErr(cmd, errf("could not decode salt: %w", err))
|
2021-01-11 13:51:29 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var (
|
|
|
|
hashPrm internalclient.HashPayloadRangesPrm
|
|
|
|
headPrm internalclient.HeadObjectPrm
|
|
|
|
|
|
|
|
sesPrms = []clientKeySession{&hashPrm}
|
|
|
|
objPrms = []objectPrm{&hashPrm}
|
|
|
|
)
|
|
|
|
|
|
|
|
fullHash := len(ranges) == 0
|
|
|
|
if fullHash {
|
|
|
|
sesPrms = append(sesPrms, &headPrm)
|
|
|
|
objPrms = append(objPrms, &headPrm)
|
|
|
|
}
|
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
prepareSessionPrm(cmd, objAddr, sesPrms...)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrm(cmd, objPrms...)
|
|
|
|
|
|
|
|
tz := typ == hashTz
|
|
|
|
|
|
|
|
if fullHash {
|
|
|
|
headPrm.SetAddress(objAddr)
|
|
|
|
|
|
|
|
// get hash of full payload through HEAD (may be user can do it through dedicated command?)
|
|
|
|
res, err := internalclient.HeadObject(headPrm)
|
2021-09-01 16:33:48 +00:00
|
|
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
2021-10-28 14:48:46 +00:00
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
var cs *checksum.Checksum
|
2021-10-28 14:48:46 +00:00
|
|
|
|
|
|
|
if tz {
|
|
|
|
cs = res.Header().PayloadHomomorphicHash()
|
|
|
|
} else {
|
|
|
|
cs = res.Header().PayloadChecksum()
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
2021-10-28 14:48:46 +00:00
|
|
|
|
|
|
|
cmd.Println(hex.EncodeToString(cs.Sum()))
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
return
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
hashPrm.SetAddress(objAddr)
|
|
|
|
hashPrm.SetSalt(salt)
|
|
|
|
hashPrm.SetRanges(ranges)
|
2021-01-11 13:51:29 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
if tz {
|
|
|
|
hashPrm.TZ()
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := internalclient.HashPayloadRanges(hashPrm)
|
|
|
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
|
|
|
|
|
|
|
hs := res.HashList()
|
|
|
|
|
|
|
|
for i := range hs {
|
|
|
|
cmd.Printf("Offset=%d (Length=%d)\t: %s\n", ranges[i].GetOffset(), ranges[i].GetLength(),
|
|
|
|
hex.EncodeToString(hs[i]))
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:18 +00:00
|
|
|
func getOwnerID(key *ecdsa.PrivateKey) (*owner.ID, error) {
|
2022-01-21 12:15:10 +00:00
|
|
|
return owner.NewIDFromPublicKey(&key.PublicKey), nil
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 22:03:46 +00:00
|
|
|
var searchUnaryOpVocabulary = map[string]object.SearchMatchType{
|
|
|
|
"NOPRESENT": object.MatchNotPresent,
|
|
|
|
}
|
|
|
|
|
|
|
|
var searchBinaryOpVocabulary = map[string]object.SearchMatchType{
|
2021-08-20 04:18:59 +00:00
|
|
|
"EQ": object.MatchStringEqual,
|
|
|
|
"NE": object.MatchStringNotEqual,
|
|
|
|
"COMMON_PREFIX": object.MatchCommonPrefix,
|
2021-02-10 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 10:40:58 +00:00
|
|
|
func parseSearchFilters(cmd *cobra.Command) (object.SearchFilters, error) {
|
|
|
|
var fs object.SearchFilters
|
2021-02-10 22:03:46 +00:00
|
|
|
|
|
|
|
for i := range searchFilters {
|
|
|
|
words := strings.Fields(searchFilters[i])
|
|
|
|
|
|
|
|
switch len(words) {
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid field number: %d", len(words))
|
2021-02-10 22:07:48 +00:00
|
|
|
case 1:
|
2021-06-28 14:01:31 +00:00
|
|
|
data, err := os.ReadFile(words[0])
|
2021-02-10 22:07:48 +00:00
|
|
|
if err != nil {
|
2021-09-01 16:33:48 +00:00
|
|
|
return nil, fmt.Errorf("could not read attributes filter from file: %w", err)
|
2021-02-10 22:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subFs := object.NewSearchFilters()
|
|
|
|
|
|
|
|
if err := subFs.UnmarshalJSON(data); err != nil {
|
2021-09-01 16:33:48 +00:00
|
|
|
return nil, fmt.Errorf("could not unmarshal attributes filter from file: %w", err)
|
2021-02-10 22:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fs = append(fs, subFs...)
|
2021-02-10 22:03:46 +00:00
|
|
|
case 2:
|
|
|
|
m, ok := searchUnaryOpVocabulary[words[1]]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unsupported unary op: %s", words[1])
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
2021-02-10 22:03:46 +00:00
|
|
|
|
|
|
|
fs.AddFilter(words[0], "", m)
|
|
|
|
case 3:
|
|
|
|
m, ok := searchBinaryOpVocabulary[words[1]]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unsupported binary op: %s", words[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.AddFilter(words[0], words[2], m)
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 != "" {
|
2022-01-26 12:11:13 +00:00
|
|
|
id := oidSDK.NewID()
|
2020-11-20 08:08:40 +00:00
|
|
|
if err := id.Parse(oid); err != nil {
|
2021-09-01 16:33:48 +00:00
|
|
|
return nil, fmt.Errorf("could not parse object ID: %w", err)
|
2020-11-20 08:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func getCID(cmd *cobra.Command) (*cid.ID, error) {
|
|
|
|
id := cid.New()
|
2021-09-01 16:33:48 +00:00
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
err := id.Parse(cmd.Flag("cid").Value.String())
|
2021-09-01 16:33:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse container ID: %w", err)
|
|
|
|
}
|
2020-10-19 11:51:47 +00:00
|
|
|
|
2021-09-01 16:33:48 +00:00
|
|
|
return id, nil
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func getOID(cmd *cobra.Command) (*oidSDK.ID, error) {
|
|
|
|
oid := oidSDK.NewID()
|
2021-09-01 16:33:48 +00:00
|
|
|
|
2020-10-19 11:51:47 +00:00
|
|
|
err := oid.Parse(cmd.Flag("oid").Value.String())
|
2021-09-01 16:33:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse object ID: %w", err)
|
|
|
|
}
|
2020-10-19 11:51:47 +00:00
|
|
|
|
2021-09-01 16:33:48 +00:00
|
|
|
return oid, nil
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func getObjectAddress(cmd *cobra.Command) (*addressSDK.Address, error) {
|
2020-10-15 10:40:58 +00:00
|
|
|
cid, err := getCID(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
oid, err := getOID(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
objAddr := addressSDK.NewAddress()
|
2020-10-15 10:40:58 +00:00
|
|
|
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 {
|
2021-09-01 16:33:48 +00:00
|
|
|
return fmt.Errorf("could not marshal header: %w", err)
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
if len(bs) != 0 {
|
|
|
|
if filename == "" {
|
|
|
|
cmd.Println(string(bs))
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-01 16:33:48 +00:00
|
|
|
err = os.WriteFile(filename, bs, os.ModePerm)
|
2020-10-15 10:40:58 +00:00
|
|
|
if err != nil {
|
2021-09-01 16:33:48 +00:00
|
|
|
return fmt.Errorf("could not write header to file: %w", err)
|
2020-10-15 10:40:58 +00:00
|
|
|
}
|
|
|
|
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()))
|
2022-02-15 05:54:42 +00:00
|
|
|
cmd.Printf("Type: %s\n", obj.Type())
|
2020-10-15 10:40:58 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:01:31 +00:00
|
|
|
data, err := os.ReadFile(path)
|
2020-10-22 15:08:08 +00:00
|
|
|
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
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func getObjectRange(cmd *cobra.Command, _ []string) {
|
2020-11-20 08:56:21 +00:00
|
|
|
objAddr, err := getObjectAddress(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-11-20 08:56:21 +00:00
|
|
|
|
|
|
|
ranges, err := getRangeList(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
|
|
|
|
|
|
|
if len(ranges) != 1 {
|
|
|
|
exitOnErr(cmd, fmt.Errorf("exactly one range must be specified, got: %d", len(ranges)))
|
2020-11-20 08:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, fmt.Errorf("can't open file '%s': %w", filename, err))
|
2020-11-20 08:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
out = f
|
|
|
|
}
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.PayloadRangePrm
|
2020-11-20 08:56:21 +00:00
|
|
|
|
2022-03-03 08:29:14 +00:00
|
|
|
prepareSessionPrm(cmd, objAddr, &prm)
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareObjectPrmRaw(cmd, &prm)
|
|
|
|
prm.SetAddress(objAddr)
|
|
|
|
prm.SetRange(ranges[0])
|
|
|
|
prm.SetPayloadWriter(out)
|
2020-11-20 08:56:21 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
_, err = internalclient.PayloadRange(prm)
|
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 {
|
2021-06-20 23:02:56 +00:00
|
|
|
return
|
2020-12-14 08:56:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, fmt.Errorf("can't get object payload range: %w", err))
|
2020-11-20 08:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if filename != "" {
|
|
|
|
cmd.Printf("[%s] Payload successfully saved\n", filename)
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|