2022-05-25 16:15:27 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
2022-05-25 16:15:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-06-23 13:52:47 +00:00
|
|
|
signBearerJSONFlag = commonflags.JSON
|
2022-05-25 16:15:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var signBearerCmd = &cobra.Command{
|
|
|
|
Use: "bearer-token",
|
|
|
|
Short: "Sign bearer token to use it in requests",
|
|
|
|
Run: signBearerToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initSignBearerCmd() {
|
|
|
|
commonflags.InitWithoutRPC(signBearerCmd)
|
|
|
|
|
|
|
|
flags := signBearerCmd.Flags()
|
|
|
|
|
|
|
|
flags.String(signFromFlag, "", "File with JSON or binary encoded bearer token to sign")
|
|
|
|
_ = signBearerCmd.MarkFlagFilename(signFromFlag)
|
|
|
|
_ = signBearerCmd.MarkFlagRequired(signFromFlag)
|
|
|
|
|
|
|
|
flags.String(signToFlag, "", "File to dump signed bearer token (default: binary encoded)")
|
|
|
|
flags.Bool(signBearerJSONFlag, false, "Dump bearer token in JSON encoding")
|
|
|
|
}
|
|
|
|
|
|
|
|
func signBearerToken(cmd *cobra.Command, _ []string) {
|
|
|
|
btok := common.ReadBearerToken(cmd, signFromFlag)
|
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
|
|
|
|
err := btok.Sign(*pk)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", err)
|
2022-05-25 16:15:27 +00:00
|
|
|
|
|
|
|
to := cmd.Flag(signToFlag).Value.String()
|
|
|
|
jsonFlag, _ := cmd.Flags().GetBool(signBearerJSONFlag)
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
if jsonFlag || len(to) == 0 {
|
|
|
|
data, err = btok.MarshalJSON()
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't JSON encode bearer token: %w", err)
|
2022-05-25 16:15:27 +00:00
|
|
|
} else {
|
|
|
|
data = btok.Marshal()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(to) == 0 {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrettyPrintJSON(cmd, btok, "bearer token")
|
2022-05-25 16:15:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-31 11:56:55 +00:00
|
|
|
err = os.WriteFile(to, data, 0o644)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't write signed bearer token to file: %w", err)
|
2022-05-25 16:15:27 +00:00
|
|
|
|
|
|
|
cmd.Printf("signed bearer token was successfully dumped to %s\n", to)
|
|
|
|
}
|