From e1c73fd43d2b82af42060127ed9e1519e8998d8f Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 21 Feb 2022 15:02:23 +0300 Subject: [PATCH] [#146] client: Customize private key in object HEAD/DELETE We should provide the ability to customize private of object HEAD / DELETE ops. Implement `UseKey` method on `PrmObjectHead` / `PrmObjectDelete` types. Sign requests with the specified key if called. Signed-off-by: Leonard Lyubich --- client/common.go | 5 +++++ client/object_delete.go | 19 ++++++++++++++++++- client/object_get.go | 18 +++++++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/client/common.go b/client/common.go index f5dbedc1..db713970 100644 --- a/client/common.go +++ b/client/common.go @@ -262,6 +262,11 @@ func (x *contextCall) processCall() bool { // initializes static cross-call parameters inherited from client. func (c *Client) initCallContext(ctx *contextCall) { ctx.key = *c.opts.key + c.initCallContextWithoutKey(ctx) +} + +// initializes static cross-call parameters inherited from client except private key. +func (c *Client) initCallContextWithoutKey(ctx *contextCall) { ctx.resolveAPIFailures = c.opts.parseNeoFSErrors ctx.callbackResp = c.opts.cbRespInfo ctx.netMagic = c.opts.netMagic diff --git a/client/object_delete.go b/client/object_delete.go index 93bae432..d3e13b59 100644 --- a/client/object_delete.go +++ b/client/object_delete.go @@ -2,6 +2,7 @@ package client import ( "context" + "crypto/ecdsa" v2object "github.com/nspcc-dev/neofs-api-go/v2/object" v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs" @@ -21,6 +22,9 @@ type PrmObjectDelete struct { body v2object.DeleteRequestBody addr v2refs.Address + + keySet bool + key ecdsa.PrivateKey } // WithinSession specifies session within which object should be read. @@ -54,6 +58,13 @@ func (x *PrmObjectDelete) ByID(id oid.ID) { x.addr.SetObjectID(id.ToV2()) } +// UseKey specifies private key to sign the requests. +// If key is not provided, then Client default key is used. +func (x *PrmObjectDelete) UseKey(key ecdsa.PrivateKey) { + x.keySet = true + x.key = key +} + // ResObjectDelete groups resulting values of ObjectDelete operation. type ResObjectDelete struct { statusRes @@ -115,7 +126,13 @@ func (c *Client) ObjectDelete(ctx context.Context, prm PrmObjectDelete) (*ResObj res ResObjectDelete ) - c.initCallContext(&cc) + if prm.keySet { + c.initCallContextWithoutKey(&cc) + cc.key = prm.key + } else { + c.initCallContext(&cc) + } + cc.req = &req cc.statusRes = &res cc.call = func() (responseV2, error) { diff --git a/client/object_get.go b/client/object_get.go index 373cddaf..5f67c915 100644 --- a/client/object_get.go +++ b/client/object_get.go @@ -383,6 +383,16 @@ func GetFullObject(ctx context.Context, c *Client, idCnr cid.ID, idObj oid.ID) ( // PrmObjectHead groups parameters of ObjectHead operation. type PrmObjectHead struct { prmObjectRead + + keySet bool + key ecdsa.PrivateKey +} + +// UseKey specifies private key to sign the requests. +// If key is not provided, then Client default key is used. +func (x *PrmObjectHead) UseKey(key ecdsa.PrivateKey) { + x.keySet = true + x.key = key } // ResObjectHead groups resulting values of ObjectHead operation. @@ -482,7 +492,13 @@ func (c *Client) ObjectHead(ctx context.Context, prm PrmObjectHead) (*ResObjectH res.idObj = prm.obj - c.initCallContext(&cc) + if prm.keySet { + c.initCallContextWithoutKey(&cc) + cc.key = prm.key + } else { + c.initCallContext(&cc) + } + cc.req = &req cc.statusRes = &res cc.call = func() (responseV2, error) {