2024-10-15 09:31:18 +00:00
|
|
|
package frostfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2024-10-15 12:42:46 +00:00
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
2024-10-16 09:05:26 +00:00
|
|
|
"errors"
|
2024-10-15 09:31:18 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
status "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
containerid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
objectid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2024-10-15 13:52:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/base58"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2024-10-15 09:31:18 +00:00
|
|
|
)
|
|
|
|
|
2024-10-15 13:52:53 +00:00
|
|
|
// Storage provides barebones API client for a single FrostFS container.
|
2024-10-15 09:31:18 +00:00
|
|
|
type Storage struct {
|
|
|
|
endpoint string
|
|
|
|
container containerid.ID
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
user user.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
const storageRequestTimeout = 10 * time.Second
|
|
|
|
|
2024-10-15 13:52:53 +00:00
|
|
|
// Open FrostFS client for working with a single storage container.
|
2024-10-15 09:31:18 +00:00
|
|
|
func Open(endpoint, containerID string, key *ecdsa.PrivateKey) (*Storage, error) {
|
|
|
|
var container containerid.ID
|
|
|
|
err := container.DecodeString(containerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid container ID: %w", err)
|
|
|
|
}
|
|
|
|
var owner user.ID
|
|
|
|
user.IDFromKey(&owner, key.PublicKey)
|
|
|
|
return &Storage{
|
|
|
|
endpoint: endpoint,
|
|
|
|
key: key,
|
|
|
|
user: owner,
|
|
|
|
container: container,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save byte slice to FrostFS object
|
|
|
|
//
|
|
|
|
// Large objects must not be created this way. Depending on FrostFS network configuration,
|
|
|
|
// attempting to send byte slices larger than ~64MB will always return an error.
|
|
|
|
func (s *Storage) Save(ctx context.Context, data []byte, attr ...string) (oid string, err error) {
|
|
|
|
attributes, err := keyval2attrs(attr...)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("invalid attributes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := object.New()
|
|
|
|
object.InitCreation(obj, object.RequiredFields{
|
|
|
|
Container: s.container,
|
|
|
|
Owner: s.user,
|
|
|
|
})
|
|
|
|
obj.SetAttributes(attributes...)
|
|
|
|
obj.SetPayload(data)
|
|
|
|
obj.SetPayloadSize(uint64(len(data)))
|
|
|
|
object.CalculateAndSetPayloadChecksum(obj)
|
|
|
|
err = object.CalculateAndSetID(obj)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("object ID: %w", err)
|
|
|
|
}
|
|
|
|
err = object.CalculateAndSetSignature(*s.key, obj)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("signing object: %w", err)
|
|
|
|
}
|
2024-10-16 09:05:26 +00:00
|
|
|
if !obj.VerifyIDSignature() {
|
|
|
|
return "", errors.New("signing object: invalid signature was generated")
|
|
|
|
}
|
2024-10-15 09:31:18 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, storageRequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := s.dial(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("connecting to storage node: %w", err)
|
|
|
|
}
|
|
|
|
put := client.PrmObjectPutSingle{
|
|
|
|
Key: s.key,
|
|
|
|
Object: obj,
|
|
|
|
}
|
|
|
|
res, err := c.ObjectPutSingle(ctx, put)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("sending object to storage: %w", err)
|
|
|
|
}
|
|
|
|
stat := res.Status()
|
|
|
|
if !status.IsSuccessful(stat) {
|
|
|
|
return "", fmt.Errorf("saving object to storage: %w", stat.(error))
|
|
|
|
}
|
|
|
|
id, _ := obj.ID()
|
2024-10-15 13:52:53 +00:00
|
|
|
return id.String(), nil
|
2024-10-15 09:31:18 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 13:52:53 +00:00
|
|
|
// Delete object from container.
|
2024-10-15 09:31:18 +00:00
|
|
|
func (s *Storage) Delete(ctx context.Context, oid string) error {
|
|
|
|
var obj objectid.ID
|
|
|
|
err := obj.DecodeString(oid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid object id: %w", err)
|
|
|
|
}
|
|
|
|
c, err := s.dial(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("connecting to storage node: %w", err)
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, storageRequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
res, err := c.ObjectDelete(ctx, client.PrmObjectDelete{
|
|
|
|
ContainerID: &s.container,
|
|
|
|
ObjectID: &obj,
|
|
|
|
Key: s.key,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete request: %w", err)
|
|
|
|
}
|
|
|
|
stat := res.Status()
|
|
|
|
if !status.IsSuccessful(stat) {
|
|
|
|
return fmt.Errorf("delete object: %w", stat.(error))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-15 13:52:53 +00:00
|
|
|
// Open new connection to FrostFS storage node.
|
2024-10-15 09:31:18 +00:00
|
|
|
func (s *Storage) dial(ctx context.Context) (*client.Client, error) {
|
2024-10-15 13:52:53 +00:00
|
|
|
var w client.PrmInit
|
|
|
|
w.Key = *s.key
|
2024-10-15 09:31:18 +00:00
|
|
|
|
|
|
|
var c client.Client
|
2024-10-15 13:52:53 +00:00
|
|
|
c.Init(w)
|
2024-10-15 09:31:18 +00:00
|
|
|
|
|
|
|
var endpoint client.PrmDial
|
|
|
|
endpoint.Endpoint = s.endpoint
|
|
|
|
|
|
|
|
err := c.Dial(ctx, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
2024-10-15 13:52:53 +00:00
|
|
|
// Compose object attributes slice.
|
2024-10-15 09:31:18 +00:00
|
|
|
func keyval2attrs(attr ...string) ([]object.Attribute, error) {
|
|
|
|
if len(attr)%2 != 0 {
|
|
|
|
return nil, fmt.Errorf("odd number of key-value strings: %d (must be even)", len(attr))
|
|
|
|
}
|
|
|
|
attributes := make([]object.Attribute, 0, len(attr)/2)
|
|
|
|
var current *object.Attribute
|
|
|
|
for index, text := range attr {
|
|
|
|
if index%2 == 0 {
|
|
|
|
current = object.NewAttribute()
|
|
|
|
current.SetKey(text)
|
|
|
|
} else {
|
|
|
|
current.SetValue(text)
|
|
|
|
attributes = append(attributes, *current)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attributes, nil
|
|
|
|
}
|
2024-10-15 12:42:46 +00:00
|
|
|
|
2024-10-15 13:52:53 +00:00
|
|
|
// Load private key from wallet file.
|
2024-10-16 09:05:26 +00:00
|
|
|
func getKey(walletPath, walletAccount, walletPassword string) (*ecdsa.PrivateKey, error) { //nolint:gocyclo
|
2024-10-15 12:42:46 +00:00
|
|
|
if walletPath == "" {
|
2024-10-16 09:05:26 +00:00
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
2024-10-15 12:42:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("generating ephemeral key: %w", err)
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
2024-10-16 09:05:26 +00:00
|
|
|
|
|
|
|
// This function intentionally omits calls to w.Close() and account.Close()
|
|
|
|
// because that would destroy the underlying ecdsa.PrivateKey.
|
2024-10-15 12:42:46 +00:00
|
|
|
w, err := wallet.NewWalletFromFile(walletPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(w.Accounts) == 0 {
|
|
|
|
return nil, fmt.Errorf("no accounts in wallet: %s", walletPath)
|
|
|
|
}
|
|
|
|
account := w.Accounts[0]
|
|
|
|
if walletAccount != "" {
|
2024-10-15 13:52:53 +00:00
|
|
|
decode, err := base58.CheckDecode(walletAccount) //nolint:govet // err shadow declaration
|
2024-10-15 12:42:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid account address: %w", err)
|
|
|
|
}
|
2024-10-16 09:05:26 +00:00
|
|
|
if len(decode) != 21 {
|
|
|
|
return nil, fmt.Errorf("invalid account address length: %d bytes", len(decode))
|
|
|
|
}
|
|
|
|
if decode[0] != 0x35 {
|
|
|
|
return nil, fmt.Errorf("invalid account address first byte: %s -> %#x", walletAccount, decode[0])
|
|
|
|
}
|
2024-10-15 12:42:46 +00:00
|
|
|
hash, err := util.Uint160DecodeBytesBE(decode[1:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid account hash: %w", err)
|
|
|
|
}
|
|
|
|
account = w.GetAccount(hash)
|
|
|
|
if account == nil {
|
|
|
|
return nil, fmt.Errorf("account not found: %s", walletAccount)
|
|
|
|
}
|
|
|
|
}
|
2024-10-16 09:05:26 +00:00
|
|
|
if account.PrivateKey() == nil {
|
|
|
|
err = account.Decrypt(walletPassword, w.Scrypt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decrypt wallet: %w", err)
|
|
|
|
}
|
2024-10-15 12:42:46 +00:00
|
|
|
}
|
|
|
|
key := account.PrivateKey().PrivateKey
|
|
|
|
return &key, nil
|
|
|
|
}
|