[#1216] neofs-cli: Allow to use session token from file

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-28 13:42:18 +03:00 committed by LeL
parent a95bdb1811
commit a254fd6bc8

View file

@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@ -289,6 +290,23 @@ func init() {
initObjectHashCmd()
initObjectRangeCmd()
initCommandObjectLock()
for _, cmd := range []*cobra.Command{
objectPutCmd,
objectDelCmd,
objectGetCmd,
objectSearchCmd,
objectHeadCmd,
objectRangeCmd,
cmdObjectLock,
} {
cmd.Flags().StringVar(
&sessionTokenPath,
sessionTokenFlag,
"",
"path to a JSON-encoded container session token",
)
}
}
type clientKeySession interface {
@ -320,8 +338,20 @@ func prepareSessionPrmWithOwner(
cli, err := getSDKClient(key)
exitOnErr(cmd, errf("create API client: %w", err))
sessionToken, err := sessionCli.CreateSession(cli, ownerID, sessionTokenLifetime)
exitOnErr(cmd, err)
var sessionToken *session.Token
if tokenPath, _ := cmd.Flags().GetString(sessionTokenFlag); len(tokenPath) != 0 {
data, err := ioutil.ReadFile(tokenPath)
exitOnErr(cmd, errf("can't read session token: %w", err))
sessionToken = session.NewToken()
if err := sessionToken.Unmarshal(data); err != nil {
err = sessionToken.UnmarshalJSON(data)
exitOnErr(cmd, errf("can't unmarshal session token: %w", err))
}
} else {
sessionToken, err = sessionCli.CreateSession(cli, ownerID, sessionTokenLifetime)
exitOnErr(cmd, err)
}
for i := range prms {
objectContext := session.NewObjectContext()