2022-03-28 10:32:18 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-15 07:29:30 +00:00
|
|
|
"os"
|
2022-03-28 10:32:18 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
internalclient "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd "github.com/TrueCloudLab/frostfs-node/cmd/internal/common"
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/network"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
frostfsecdsa "github.com/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/session"
|
2022-05-18 15:20:08 +00:00
|
|
|
"github.com/google/uuid"
|
2022-03-28 10:32:18 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-05-18 10:38:45 +00:00
|
|
|
"github.com/spf13/viper"
|
2022-03-28 10:32:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-07-19 12:45:58 +00:00
|
|
|
outFlag = "out"
|
|
|
|
jsonFlag = commonflags.JSON
|
2022-03-28 10:32:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultLifetime = 10
|
|
|
|
|
|
|
|
var createCmd = &cobra.Command{
|
|
|
|
Use: "create",
|
|
|
|
Short: "Create session token",
|
2022-07-19 12:53:26 +00:00
|
|
|
Run: createSession,
|
2022-05-18 10:38:45 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
_ = viper.BindPFlag(commonflags.WalletPath, cmd.Flags().Lookup(commonflags.WalletPath))
|
|
|
|
_ = viper.BindPFlag(commonflags.Account, cmd.Flags().Lookup(commonflags.Account))
|
|
|
|
},
|
2022-03-28 10:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2022-10-11 14:02:03 +00:00
|
|
|
createCmd.Flags().Uint64P(commonflags.Lifetime, "l", defaultLifetime, "Number of epochs for token to stay valid")
|
2022-05-18 09:22:02 +00:00
|
|
|
createCmd.Flags().StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage)
|
|
|
|
createCmd.Flags().StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage)
|
2022-10-11 14:02:03 +00:00
|
|
|
createCmd.Flags().String(outFlag, "", "File to write session token to")
|
|
|
|
createCmd.Flags().Bool(jsonFlag, false, "Output token in JSON")
|
2022-05-18 09:22:02 +00:00
|
|
|
createCmd.Flags().StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage)
|
2022-03-28 10:32:18 +00:00
|
|
|
|
2022-05-18 09:22:02 +00:00
|
|
|
_ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.WalletPath)
|
2022-03-28 10:32:18 +00:00
|
|
|
_ = cobra.MarkFlagRequired(createCmd.Flags(), outFlag)
|
2022-05-18 09:22:02 +00:00
|
|
|
_ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.RPC)
|
2022-03-28 10:32:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 12:53:26 +00:00
|
|
|
func createSession(cmd *cobra.Command, _ []string) {
|
2022-05-24 08:22:23 +00:00
|
|
|
privKey := key.Get(cmd)
|
2022-03-28 10:32:18 +00:00
|
|
|
|
|
|
|
var netAddr network.Address
|
2022-05-18 09:22:02 +00:00
|
|
|
addrStr, _ := cmd.Flags().GetString(commonflags.RPC)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't parse endpoint: %w", netAddr.FromString(addrStr))
|
2022-03-28 10:32:18 +00:00
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
c, err := internalclient.GetSDKClient(cmd, privKey, netAddr)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't create client: %w", err)
|
2022-03-28 10:32:18 +00:00
|
|
|
|
|
|
|
lifetime := uint64(defaultLifetime)
|
2022-07-19 12:45:58 +00:00
|
|
|
if lfArg, _ := cmd.Flags().GetUint64(commonflags.Lifetime); lfArg != 0 {
|
2022-03-28 10:32:18 +00:00
|
|
|
lifetime = lfArg
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
var tok session.Object
|
2022-05-17 13:59:46 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
err = CreateSession(&tok, c, lifetime)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't create session: %w", err)
|
2022-03-28 10:32:18 +00:00
|
|
|
|
|
|
|
var data []byte
|
|
|
|
|
|
|
|
if toJSON, _ := cmd.Flags().GetBool(jsonFlag); toJSON {
|
|
|
|
data, err = tok.MarshalJSON()
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't decode session token JSON: %w", err)
|
2022-03-28 10:32:18 +00:00
|
|
|
} else {
|
2022-05-18 15:20:08 +00:00
|
|
|
data = tok.Marshal()
|
2022-03-28 10:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filename, _ := cmd.Flags().GetString(outFlag)
|
2022-08-15 07:29:30 +00:00
|
|
|
err = os.WriteFile(filename, data, 0644)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't write token to file: %w", err)
|
2022-03-28 10:32:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 16:29:25 +00:00
|
|
|
// CreateSession opens a new communication with FrostFS storage node using client connection.
|
2022-05-18 15:20:08 +00:00
|
|
|
// The session is expected to be maintained by the storage node during the given
|
|
|
|
// number of epochs.
|
|
|
|
//
|
|
|
|
// Fills ID, lifetime and session key.
|
|
|
|
func CreateSession(dst *session.Object, c *client.Client, lifetime uint64) error {
|
2022-03-28 10:32:18 +00:00
|
|
|
var netInfoPrm internalclient.NetworkInfoPrm
|
|
|
|
netInfoPrm.SetClient(c)
|
|
|
|
|
|
|
|
ni, err := internalclient.NetworkInfo(netInfoPrm)
|
|
|
|
if err != nil {
|
2022-05-18 15:20:08 +00:00
|
|
|
return fmt.Errorf("can't fetch network info: %w", err)
|
2022-03-28 10:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cur := ni.NetworkInfo().CurrentEpoch()
|
|
|
|
exp := cur + lifetime
|
|
|
|
|
|
|
|
var sessionPrm internalclient.CreateSessionPrm
|
|
|
|
sessionPrm.SetClient(c)
|
|
|
|
sessionPrm.SetExp(exp)
|
|
|
|
|
|
|
|
sessionRes, err := internalclient.CreateSession(sessionPrm)
|
|
|
|
if err != nil {
|
2022-05-18 15:20:08 +00:00
|
|
|
return fmt.Errorf("can't open session: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
binIDSession := sessionRes.ID()
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
var keySession frostfsecdsa.PublicKey
|
2022-05-18 15:20:08 +00:00
|
|
|
|
|
|
|
err = keySession.Decode(sessionRes.SessionKey())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode public session key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var idSession uuid.UUID
|
|
|
|
|
|
|
|
err = idSession.UnmarshalBinary(binIDSession)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode session ID: %w", err)
|
2022-03-28 10:32:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
dst.SetID(idSession)
|
|
|
|
dst.SetNbf(cur)
|
|
|
|
dst.SetIat(cur)
|
|
|
|
dst.SetExp(exp)
|
|
|
|
dst.SetAuthKey(&keySession)
|
2022-03-28 10:32:18 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
return nil
|
2022-03-28 10:32:18 +00:00
|
|
|
}
|