2022-03-28 09:36:04 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2022-06-08 10:13:44 +00:00
|
|
|
"context"
|
2022-03-28 09:36:04 +00:00
|
|
|
"crypto/ecdsa"
|
2022-06-08 10:13:44 +00:00
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
2022-05-18 10:16:04 +00:00
|
|
|
"errors"
|
2022-03-28 09:36:04 +00:00
|
|
|
"fmt"
|
|
|
|
|
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"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
|
2023-05-24 11:50:19 +00:00
|
|
|
tracing "git.frostfs.info/TrueCloudLab/frostfs-observability/tracing/grpc"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2022-05-24 08:38:45 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-05-18 10:16:04 +00:00
|
|
|
"github.com/spf13/viper"
|
2023-05-24 11:50:19 +00:00
|
|
|
"google.golang.org/grpc"
|
2022-03-28 09:36:04 +00:00
|
|
|
)
|
|
|
|
|
2022-05-18 10:16:04 +00:00
|
|
|
var errInvalidEndpoint = errors.New("provided RPC endpoint is incorrect")
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
// GetSDKClientByFlag returns default frostfs-sdk-go client using the specified flag for the address.
|
2022-05-24 08:38:45 +00:00
|
|
|
// On error, outputs to stderr of cmd and exits with non-zero code.
|
|
|
|
func GetSDKClientByFlag(cmd *cobra.Command, key *ecdsa.PrivateKey, endpointFlag string) *client.Client {
|
2022-12-27 09:36:30 +00:00
|
|
|
cli, err := getSDKClientByFlag(cmd, key, endpointFlag)
|
2022-05-24 08:38:45 +00:00
|
|
|
if err != nil {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't create API client: %w", err)
|
2022-05-24 08:38:45 +00:00
|
|
|
}
|
|
|
|
return cli
|
|
|
|
}
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
func getSDKClientByFlag(cmd *cobra.Command, key *ecdsa.PrivateKey, endpointFlag string) (*client.Client, error) {
|
2022-05-18 10:16:04 +00:00
|
|
|
var addr network.Address
|
|
|
|
|
|
|
|
err := addr.FromString(viper.GetString(endpointFlag))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %w", errInvalidEndpoint, err)
|
|
|
|
}
|
2023-04-18 09:04:59 +00:00
|
|
|
return GetSDKClient(cmd.Context(), cmd, key, addr)
|
2022-05-18 10:16:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
// GetSDKClient returns default frostfs-sdk-go client.
|
2023-04-18 09:04:59 +00:00
|
|
|
func GetSDKClient(ctx context.Context, cmd *cobra.Command, key *ecdsa.PrivateKey, addr network.Address) (*client.Client, error) {
|
2023-11-15 12:28:45 +00:00
|
|
|
var c client.Client
|
|
|
|
|
|
|
|
prmInit := client.PrmInit{
|
|
|
|
Key: *key,
|
|
|
|
}
|
|
|
|
|
|
|
|
prmDial := client.PrmDial{
|
|
|
|
Endpoint: addr.URIAddr(),
|
|
|
|
GRPCDialOptions: []grpc.DialOption{
|
|
|
|
grpc.WithChainUnaryInterceptor(tracing.NewUnaryClientInteceptor()),
|
|
|
|
grpc.WithChainStreamInterceptor(tracing.NewStreamClientInterceptor()),
|
|
|
|
},
|
|
|
|
}
|
2022-10-05 07:39:47 +00:00
|
|
|
if timeout := viper.GetDuration(commonflags.Timeout); timeout > 0 {
|
|
|
|
// In CLI we can only set a timeout for the whole operation.
|
|
|
|
// By also setting stream timeout we ensure that no operation hands
|
|
|
|
// for too long.
|
2023-11-15 12:28:45 +00:00
|
|
|
prmDial.DialTimeout = timeout
|
|
|
|
prmDial.StreamTimeout = timeout
|
2022-10-25 06:05:10 +00:00
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Set request timeout to %s.", timeout)
|
2022-10-05 07:39:47 +00:00
|
|
|
}
|
2022-03-28 09:36:04 +00:00
|
|
|
|
|
|
|
c.Init(prmInit)
|
|
|
|
|
2023-04-18 09:04:59 +00:00
|
|
|
if err := c.Dial(ctx, prmDial); err != nil {
|
2022-03-28 09:36:04 +00:00
|
|
|
return nil, fmt.Errorf("can't init SDK client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
}
|
2022-06-08 10:13:44 +00:00
|
|
|
|
|
|
|
// GetCurrentEpoch returns current epoch.
|
2022-12-27 09:36:30 +00:00
|
|
|
func GetCurrentEpoch(ctx context.Context, cmd *cobra.Command, endpoint string) (uint64, error) {
|
2022-06-08 10:13:44 +00:00
|
|
|
var addr network.Address
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-04-18 09:04:59 +00:00
|
|
|
c, err := GetSDKClient(ctx, cmd, key, addr)
|
2022-06-08 10:13:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ni, err := c.NetworkInfo(ctx, client.PrmNetworkInfo{})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ni.Info().CurrentEpoch(), nil
|
|
|
|
}
|