2020-07-24 13:54:03 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2022-05-12 16:37:46 +00:00
|
|
|
"crypto/sha256"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2022-01-31 13:34:01 +00:00
|
|
|
"strings"
|
2021-05-18 08:12:51 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
containerContract "git.frostfs.info/TrueCloudLab/frostfs-contract/container"
|
|
|
|
containercore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
2024-11-07 14:32:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
2023-03-07 13:38:26 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
type containerSource Client
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
func (x *containerSource) Get(cnr cid.ID) (*containercore.Container, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
return Get((*Client)(x), cnr)
|
2021-05-25 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// AsContainerSource provides container Source interface
|
|
|
|
// from Wrapper instance.
|
2023-04-03 13:11:56 +00:00
|
|
|
func AsContainerSource(w *Client) containercore.Source {
|
2022-01-31 13:34:01 +00:00
|
|
|
return (*containerSource)(w)
|
2021-05-25 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 09:05:33 +00:00
|
|
|
type getContainer interface {
|
|
|
|
Get(cid []byte) (*containercore.Container, error)
|
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// Get marshals container ID, and passes it to Wrapper's Get method.
|
2023-04-26 09:05:33 +00:00
|
|
|
func Get(c getContainer, cnr cid.ID) (*containercore.Container, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
binCnr := make([]byte, sha256.Size)
|
|
|
|
cnr.Encode(binCnr)
|
|
|
|
|
|
|
|
return c.Get(binCnr)
|
2021-05-25 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// Get reads the container from FrostFS system by binary identifier
|
2022-01-31 13:34:01 +00:00
|
|
|
// through Container contract call.
|
|
|
|
//
|
|
|
|
// If an empty slice is returned for the requested identifier,
|
|
|
|
// storage.ErrNotFound error is returned.
|
2022-06-22 10:55:31 +00:00
|
|
|
func (c *Client) Get(cid []byte) (*containercore.Container, error) {
|
2022-01-31 13:34:01 +00:00
|
|
|
prm := client.TestInvokePrm{}
|
|
|
|
prm.SetMethod(getMethod)
|
|
|
|
prm.SetArgs(cid)
|
|
|
|
|
|
|
|
res, err := c.client.TestInvoke(prm)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2022-02-08 08:25:11 +00:00
|
|
|
if strings.Contains(err.Error(), containerContract.NotFoundError) {
|
2023-08-04 11:14:07 +00:00
|
|
|
return nil, new(apistatus.ContainerNotFound)
|
2022-01-31 13:34:01 +00:00
|
|
|
}
|
2024-12-04 08:17:13 +00:00
|
|
|
return nil, fmt.Errorf("test invoke (%s): %w", getMethod, err)
|
2022-01-31 13:34:01 +00:00
|
|
|
} else if ln := len(res); ln != 1 {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected stack item count (%s): %d", getMethod, ln)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
arr, err := client.ArrayFromStackItem(res[0])
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("get item array of container (%s): %w", getMethod, err)
|
2021-05-25 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(arr) != 4 {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected container stack item count (%s): %d", getMethod, len(arr))
|
2021-05-25 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cnrBytes, err := client.BytesFromStackItem(arr[0])
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("get byte array of container (%s): %w", getMethod, err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
sigBytes, err := client.BytesFromStackItem(arr[1])
|
2021-05-25 16:08:24 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("get byte array of container signature (%s): %w", getMethod, err)
|
2021-05-25 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub, err := client.BytesFromStackItem(arr[2])
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("get byte array of public key (%s): %w", getMethod, err)
|
2021-05-25 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
tokBytes, err := client.BytesFromStackItem(arr[3])
|
2021-05-25 16:08:24 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("get byte array of session token (%s): %w", getMethod, err)
|
2021-05-25 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
var cnr containercore.Container
|
|
|
|
|
|
|
|
if err := cnr.Value.Unmarshal(cnrBytes); err != nil {
|
2022-01-31 13:34:01 +00:00
|
|
|
// use other major version if there any
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("unmarshal container: %w", err)
|
2022-01-31 13:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(tokBytes) > 0 {
|
2022-06-22 10:55:31 +00:00
|
|
|
cnr.Session = new(session.Container)
|
2022-01-31 13:34:01 +00:00
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
err = cnr.Session.Unmarshal(tokBytes)
|
2022-01-31 13:34:01 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("unmarshal session token: %w", err)
|
2022-01-31 13:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 13:18:39 +00:00
|
|
|
// TODO(@cthulhu-rider): #468 implement and use another approach to avoid conversion
|
2022-05-16 13:15:31 +00:00
|
|
|
var sigV2 refs.Signature
|
|
|
|
sigV2.SetKey(pub)
|
|
|
|
sigV2.SetSign(sigBytes)
|
|
|
|
sigV2.SetScheme(refs.ECDSA_RFC6979_SHA256)
|
|
|
|
|
2022-07-22 14:04:37 +00:00
|
|
|
err = cnr.Signature.ReadFromV2(sigV2)
|
|
|
|
return &cnr, err
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|