From 5395988efcd3c5b801cc745d266c214312d9b330 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 17 Dec 2020 12:51:03 +0300 Subject: [PATCH] [#208] pkg/client: Add function to get container and verify ID GetContainer method reads container structure by identifier from the network. In some cases it is required to additionally check the correspondence of the container structure to the identifier as a hash from the binary representation. To do this, a new function GetVerifiedContainerStructure is defined to execute the check after receiving the container. Signed-off-by: Leonard Lyubich --- pkg/client/container.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/client/container.go b/pkg/client/container.go index 3bd61e2..d28bec7 100644 --- a/pkg/client/container.go +++ b/pkg/client/container.go @@ -64,6 +64,23 @@ func (c Client) GetContainer(ctx context.Context, id *container.ID, opts ...Call } } +// GetVerifiedContainerStructure is a wrapper over Client.GetContainer method +// which checks if the structure of the resulting container matches its identifier. +// +// Returns container.ErrIDMismatch if container does not match the identifier. +func GetVerifiedContainerStructure(ctx context.Context, c *Client, id *container.ID, opts ...CallOption) (*container.Container, error) { + cnr, err := c.GetContainer(ctx, id, opts...) + if err != nil { + return nil, err + } + + if !container.CalculateID(cnr).Equal(id) { + return nil, container.ErrIDMismatch + } + + return cnr, nil +} + func (c Client) ListContainers(ctx context.Context, owner *owner.ID, opts ...CallOption) ([]*container.ID, error) { switch c.remoteNode.Version.Major() { case 2: