2022-05-23 16:26:27 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
controlSvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/server"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
2022-05-23 16:26:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-10-17 15:37:15 +00:00
|
|
|
func initControlFlags(cmd *cobra.Command) {
|
|
|
|
ff := cmd.Flags()
|
2022-11-29 10:22:17 +00:00
|
|
|
ff.StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage)
|
|
|
|
ff.StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage)
|
2022-10-17 15:37:15 +00:00
|
|
|
ff.String(controlRPC, controlRPCDefault, controlRPCUsage)
|
|
|
|
ff.DurationP(commonflags.Timeout, commonflags.TimeoutShorthand, commonflags.TimeoutDefault, commonflags.TimeoutUsage)
|
|
|
|
}
|
|
|
|
|
2022-05-23 16:26:27 +00:00
|
|
|
func signRequest(cmd *cobra.Command, pk *ecdsa.PrivateKey, req controlSvc.SignedMessage) {
|
|
|
|
err := controlSvc.SignMessage(pk, req)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "could not sign request: %w", err)
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func verifyResponse(cmd *cobra.Command,
|
|
|
|
sigControl interface {
|
|
|
|
GetKey() []byte
|
|
|
|
GetSign() []byte
|
|
|
|
},
|
|
|
|
body interface {
|
2022-05-31 17:00:41 +00:00
|
|
|
StableMarshal([]byte) []byte
|
2022-05-23 16:26:27 +00:00
|
|
|
},
|
|
|
|
) {
|
|
|
|
if sigControl == nil {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", errors.New("missing response signature"))
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 13:18:39 +00:00
|
|
|
// TODO(@cthulhu-rider): #468 use Signature message from FrostFS API to avoid conversion
|
2022-05-23 16:26:27 +00:00
|
|
|
var sigV2 refs.Signature
|
|
|
|
sigV2.SetScheme(refs.ECDSA_SHA512)
|
|
|
|
sigV2.SetKey(sigControl.GetKey())
|
|
|
|
sigV2.SetSign(sigControl.GetSign())
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
var sig frostfscrypto.Signature
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't read signature: %w", sig.ReadFromV2(sigV2))
|
2022-05-23 16:26:27 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
if !sig.Verify(body.StableMarshal(nil)) {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", errors.New("invalid response signature"))
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getClient(cmd *cobra.Command, pk *ecdsa.PrivateKey) *client.Client {
|
2022-05-24 08:38:45 +00:00
|
|
|
return internalclient.GetSDKClientByFlag(cmd, pk, controlRPC)
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|