8bba490c30
There is a need to support NeoFS-binary sessions along with JSON ones in NeoFS CLI. Provide generic `common.ReadBinaryOrJSON` functions which tries to decode NeoFS-binary structure and falls back to JSON format. Use this function in all places with token reading. Signed-off-by: Leonard Lyubich <ctulhurider@gmail.com>
84 lines
2 KiB
Go
84 lines
2 KiB
Go
package util
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var signSessionCmd = &cobra.Command{
|
|
Use: "session-token",
|
|
Short: "Sign session token to use it in requests",
|
|
Run: signSessionToken,
|
|
}
|
|
|
|
func initSignSessionCmd() {
|
|
commonflags.InitWithoutRPC(signSessionCmd)
|
|
|
|
flags := signSessionCmd.Flags()
|
|
|
|
flags.String(signFromFlag, "", "File with JSON encoded session token to sign")
|
|
_ = signSessionCmd.MarkFlagFilename(signFromFlag)
|
|
_ = signSessionCmd.MarkFlagRequired(signFromFlag)
|
|
|
|
flags.String(signToFlag, "", "File to save signed session token (optional)")
|
|
}
|
|
|
|
func signSessionToken(cmd *cobra.Command, _ []string) {
|
|
fPath, err := cmd.Flags().GetString(signFromFlag)
|
|
common.ExitOnErr(cmd, "", err)
|
|
|
|
if fPath == "" {
|
|
common.ExitOnErr(cmd, "", errors.New("missing session token flag"))
|
|
}
|
|
|
|
type iTokenSession interface {
|
|
json.Marshaler
|
|
common.BinaryOrJSON
|
|
Sign(ecdsa.PrivateKey) error
|
|
}
|
|
var errLast error
|
|
var stok iTokenSession
|
|
|
|
for _, el := range [...]iTokenSession{
|
|
new(session.Object),
|
|
new(session.Container),
|
|
} {
|
|
errLast = common.ReadBinaryOrJSON(el, fPath)
|
|
if errLast == nil {
|
|
stok = el
|
|
break
|
|
}
|
|
}
|
|
|
|
common.ExitOnErr(cmd, "decode session: %v", errLast)
|
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
err = stok.Sign(*pk)
|
|
common.ExitOnErr(cmd, "can't sign token: %w", err)
|
|
|
|
data, err := stok.MarshalJSON()
|
|
common.ExitOnErr(cmd, "can't encode session token: %w", err)
|
|
|
|
to := cmd.Flag(signToFlag).Value.String()
|
|
if len(to) == 0 {
|
|
prettyPrintJSON(cmd, data)
|
|
return
|
|
}
|
|
|
|
err = os.WriteFile(to, data, 0644)
|
|
if err != nil {
|
|
common.ExitOnErr(cmd, "", fmt.Errorf("can't write signed session token to %s: %w", to, err))
|
|
}
|
|
|
|
cmd.Printf("signed session token saved in %s\n", to)
|
|
}
|