2022-05-25 16:15:27 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-05-18 15:20:08 +00:00
|
|
|
"encoding/json"
|
2022-10-20 09:40:33 +00:00
|
|
|
"errors"
|
2022-05-31 17:00:41 +00:00
|
|
|
"fmt"
|
2022-05-25 16:15:27 +00:00
|
|
|
"os"
|
|
|
|
|
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/bearer"
|
2022-05-25 16:15:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadBearerToken reads bearer token from the path provided in a specified flag.
|
|
|
|
func ReadBearerToken(cmd *cobra.Command, flagname string) *bearer.Token {
|
|
|
|
path, err := cmd.Flags().GetString(flagname)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", err)
|
2022-05-25 16:15:27 +00:00
|
|
|
|
|
|
|
if len(path) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
PrintVerbose(cmd, "Reading bearer token from file [%s]...", path)
|
2022-05-25 16:15:27 +00:00
|
|
|
|
|
|
|
var tok bearer.Token
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
err = ReadBinaryOrJSON(cmd, &tok, path)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "invalid bearer token: %v", err)
|
2022-05-25 16:15:27 +00:00
|
|
|
|
|
|
|
return &tok
|
|
|
|
}
|
|
|
|
|
2022-10-20 09:40:33 +00:00
|
|
|
// BinaryOrJSON is an interface of entities which provide json.Unmarshaler
|
2023-02-03 16:29:25 +00:00
|
|
|
// and FrostFS binary decoder.
|
2022-10-20 09:40:33 +00:00
|
|
|
type BinaryOrJSON interface {
|
|
|
|
Unmarshal([]byte) error
|
|
|
|
json.Unmarshaler
|
2022-05-31 17:00:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 09:40:33 +00:00
|
|
|
// ReadBinaryOrJSON reads file data using provided path and decodes
|
|
|
|
// BinaryOrJSON from the data.
|
2022-12-27 09:36:30 +00:00
|
|
|
func ReadBinaryOrJSON(cmd *cobra.Command, dst BinaryOrJSON, fPath string) error {
|
|
|
|
PrintVerbose(cmd, "Reading file [%s]...", fPath)
|
2022-10-20 09:40:33 +00:00
|
|
|
|
2022-05-25 16:15:27 +00:00
|
|
|
// try to read session token from file
|
2022-05-18 15:20:08 +00:00
|
|
|
data, err := os.ReadFile(fPath)
|
2022-05-31 17:00:41 +00:00
|
|
|
if err != nil {
|
2022-10-20 09:40:33 +00:00
|
|
|
return fmt.Errorf("read file <%s>: %w", fPath, err)
|
2022-05-31 17:00:41 +00:00
|
|
|
}
|
2022-05-25 16:15:27 +00:00
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
PrintVerbose(cmd, "Trying to decode binary...")
|
2022-10-20 09:40:33 +00:00
|
|
|
|
|
|
|
err = dst.Unmarshal(data)
|
2022-05-31 17:00:41 +00:00
|
|
|
if err != nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
PrintVerbose(cmd, "Failed to decode binary: %v", err)
|
2022-10-20 09:40:33 +00:00
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
PrintVerbose(cmd, "Trying to decode JSON...")
|
2022-10-20 09:40:33 +00:00
|
|
|
|
|
|
|
err = dst.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
2022-12-27 09:36:30 +00:00
|
|
|
PrintVerbose(cmd, "Failed to decode JSON: %v", err)
|
2022-10-20 09:40:33 +00:00
|
|
|
return errors.New("invalid format")
|
|
|
|
}
|
2022-05-31 17:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-05-25 16:15:27 +00:00
|
|
|
}
|