2022-03-28 09:09:49 +00:00
|
|
|
package bearer
|
2022-03-23 12:47:34 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-23 13:22:09 +00:00
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
2022-03-23 12:47:34 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2022-03-23 13:22:09 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2022-03-23 12:47:34 +00:00
|
|
|
|
2022-03-28 09:36:04 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
2022-05-18 09:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
2022-03-23 13:22:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2022-05-12 07:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/bearer"
|
2022-03-23 13:22:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
2022-03-23 12:47:34 +00:00
|
|
|
eaclSDK "github.com/nspcc-dev/neofs-sdk-go/eacl"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2022-03-23 12:47:34 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
eaclFlag = "eacl"
|
|
|
|
issuedAtFlag = "issued-at"
|
|
|
|
notValidBeforeFlag = "not-valid-before"
|
|
|
|
expireAtFlag = "expire-at"
|
|
|
|
ownerFlag = "owner"
|
|
|
|
outFlag = "out"
|
2022-06-23 13:52:47 +00:00
|
|
|
jsonFlag = commonflags.JSON
|
2022-03-23 12:47:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var createCmd = &cobra.Command{
|
|
|
|
Use: "create",
|
|
|
|
Short: "Create bearer token",
|
2022-03-23 13:22:09 +00:00
|
|
|
Long: `Create bearer token.
|
|
|
|
|
|
|
|
All epoch flags can be specified relative to the current epoch with the +n syntax.
|
2022-05-18 09:22:02 +00:00
|
|
|
In this case --` + commonflags.RPC + ` flag should be specified and the epoch in bearer token
|
2022-03-23 13:22:09 +00:00
|
|
|
is set to current epoch + n.
|
|
|
|
`,
|
|
|
|
RunE: createToken,
|
2022-03-23 12:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
createCmd.Flags().StringP(eaclFlag, "e", "", "path to the extended ACL table")
|
2022-03-23 13:22:09 +00:00
|
|
|
createCmd.Flags().StringP(issuedAtFlag, "i", "", "epoch to issue token at")
|
|
|
|
createCmd.Flags().StringP(notValidBeforeFlag, "n", "", "not valid before epoch")
|
|
|
|
createCmd.Flags().StringP(expireAtFlag, "x", "", "expiration epoch")
|
2022-03-23 12:47:34 +00:00
|
|
|
createCmd.Flags().StringP(ownerFlag, "o", "", "token owner")
|
|
|
|
createCmd.Flags().String(outFlag, "", "file to write 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-23 12:47:34 +00:00
|
|
|
|
|
|
|
_ = cobra.MarkFlagFilename(createCmd.Flags(), eaclFlag)
|
|
|
|
|
|
|
|
_ = cobra.MarkFlagRequired(createCmd.Flags(), issuedAtFlag)
|
|
|
|
_ = cobra.MarkFlagRequired(createCmd.Flags(), notValidBeforeFlag)
|
|
|
|
_ = cobra.MarkFlagRequired(createCmd.Flags(), expireAtFlag)
|
|
|
|
_ = cobra.MarkFlagRequired(createCmd.Flags(), ownerFlag)
|
|
|
|
_ = cobra.MarkFlagRequired(createCmd.Flags(), outFlag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createToken(cmd *cobra.Command, _ []string) error {
|
2022-03-23 13:22:09 +00:00
|
|
|
iat, iatRelative, err := parseEpoch(cmd, issuedAtFlag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
exp, expRelative, err := parseEpoch(cmd, expireAtFlag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nvb, nvbRelative, err := parseEpoch(cmd, notValidBeforeFlag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if iatRelative || expRelative || nvbRelative {
|
2022-05-18 09:22:02 +00:00
|
|
|
endpoint, _ := cmd.Flags().GetString(commonflags.RPC)
|
2022-03-23 13:22:09 +00:00
|
|
|
currEpoch, err := getCurrentEpoch(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if iatRelative {
|
|
|
|
iat += currEpoch
|
|
|
|
}
|
|
|
|
if expRelative {
|
|
|
|
exp += currEpoch
|
|
|
|
}
|
|
|
|
if nvbRelative {
|
|
|
|
nvb += currEpoch
|
|
|
|
}
|
|
|
|
}
|
2022-03-23 12:47:34 +00:00
|
|
|
if exp < nvb {
|
|
|
|
return fmt.Errorf("expiration epoch is less than not-valid-before epoch: %d < %d", exp, nvb)
|
|
|
|
}
|
|
|
|
|
|
|
|
ownerStr, _ := cmd.Flags().GetString(ownerFlag)
|
2022-05-17 13:59:46 +00:00
|
|
|
|
|
|
|
var ownerID user.ID
|
|
|
|
if err := ownerID.DecodeString(ownerStr); err != nil {
|
2022-03-23 12:47:34 +00:00
|
|
|
return fmt.Errorf("can't parse recipient: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-12 07:22:02 +00:00
|
|
|
var b bearer.Token
|
2022-06-03 14:12:55 +00:00
|
|
|
b.SetExp(exp)
|
|
|
|
b.SetNbf(nvb)
|
|
|
|
b.SetIat(iat)
|
|
|
|
b.ForUser(ownerID)
|
2022-03-23 12:47:34 +00:00
|
|
|
|
|
|
|
eaclPath, _ := cmd.Flags().GetString(eaclFlag)
|
|
|
|
if eaclPath != "" {
|
|
|
|
table := eaclSDK.NewTable()
|
|
|
|
raw, err := ioutil.ReadFile(eaclPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't read extended ACL file: %w", err)
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(raw, table); err != nil {
|
|
|
|
return fmt.Errorf("can't parse extended ACL: %w", err)
|
|
|
|
}
|
2022-05-12 07:22:02 +00:00
|
|
|
b.SetEACLTable(*table)
|
2022-03-23 12:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
|
|
|
|
toJSON, _ := cmd.Flags().GetBool(jsonFlag)
|
|
|
|
if toJSON {
|
|
|
|
data, err = json.Marshal(b)
|
2022-05-12 07:22:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't mashal token to JSON: %w", err)
|
|
|
|
}
|
2022-03-23 12:47:34 +00:00
|
|
|
} else {
|
2022-05-12 07:22:02 +00:00
|
|
|
data = b.Marshal()
|
2022-03-23 12:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out, _ := cmd.Flags().GetString(outFlag)
|
|
|
|
if err := ioutil.WriteFile(out, data, 0644); err != nil {
|
|
|
|
return fmt.Errorf("can't write token to file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-23 13:22:09 +00:00
|
|
|
|
|
|
|
// parseEpoch parses epoch argument. Second return value is true if
|
|
|
|
// the specified epoch is relative, and false otherwise.
|
|
|
|
func parseEpoch(cmd *cobra.Command, flag string) (uint64, bool, error) {
|
|
|
|
s, _ := cmd.Flags().GetString(flag)
|
|
|
|
if len(s) == 0 {
|
|
|
|
return 0, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
relative := s[0] == '+'
|
|
|
|
if relative {
|
|
|
|
s = s[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
epoch, err := strconv.ParseUint(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, relative, fmt.Errorf("can't parse epoch for %s argument: %w", flag, err)
|
|
|
|
}
|
|
|
|
return epoch, relative, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCurrentEpoch returns current epoch.
|
|
|
|
func getCurrentEpoch(endpoint string) (uint64, error) {
|
2022-03-28 09:36:04 +00:00
|
|
|
var addr network.Address
|
2022-03-23 13:22:09 +00:00
|
|
|
|
|
|
|
if err := addr.FromString(endpoint); err != nil {
|
|
|
|
return 0, fmt.Errorf("can't parse RPC endpoint: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("can't generate key to sign query: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-28 09:36:04 +00:00
|
|
|
c, err := internalclient.GetSDKClient(key, addr)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2022-03-23 13:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ni, err := c.NetworkInfo(ctx, client.PrmNetworkInfo{})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ni.Info().CurrentEpoch(), nil
|
|
|
|
}
|