2022-05-25 16:15:27 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2022-05-31 17:02:35 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/json"
|
2022-05-18 15:20:08 +00:00
|
|
|
"errors"
|
2022-05-25 16:15:27 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"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-sdk-go/session"
|
2022-05-25 16:15:27 +00:00
|
|
|
"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) {
|
2022-05-18 15:20:08 +00:00
|
|
|
fPath, err := cmd.Flags().GetString(signFromFlag)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", err)
|
2022-05-18 15:20:08 +00:00
|
|
|
|
|
|
|
if fPath == "" {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", errors.New("missing session token flag"))
|
2022-05-18 15:20:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:02:35 +00:00
|
|
|
type iTokenSession interface {
|
|
|
|
json.Marshaler
|
2022-10-20 09:40:33 +00:00
|
|
|
common.BinaryOrJSON
|
2022-05-31 17:02:35 +00:00
|
|
|
Sign(ecdsa.PrivateKey) error
|
|
|
|
}
|
|
|
|
var errLast error
|
|
|
|
var stok iTokenSession
|
|
|
|
|
|
|
|
for _, el := range [...]iTokenSession{
|
|
|
|
new(session.Object),
|
|
|
|
new(session.Container),
|
|
|
|
} {
|
2022-12-27 09:36:30 +00:00
|
|
|
errLast = common.ReadBinaryOrJSON(cmd, el, fPath)
|
2022-05-31 17:02:35 +00:00
|
|
|
if errLast == nil {
|
|
|
|
stok = el
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "decode session: %v", errLast)
|
2022-05-18 15:20:08 +00:00
|
|
|
|
2022-05-25 16:15:27 +00:00
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
err = stok.Sign(*pk)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't sign token: %w", err)
|
2022-05-25 16:15:27 +00:00
|
|
|
|
|
|
|
data, err := stok.MarshalJSON()
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't encode session token: %w", err)
|
2022-05-25 16:15:27 +00:00
|
|
|
|
|
|
|
to := cmd.Flag(signToFlag).Value.String()
|
|
|
|
if len(to) == 0 {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrettyPrintJSON(cmd, stok, "session token")
|
2022-05-25 16:15:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(to, data, 0644)
|
|
|
|
if err != nil {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("can't write signed session token to %s: %w", to, err))
|
2022-05-25 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("signed session token saved in %s\n", to)
|
|
|
|
}
|