Merge pull request #84 from KirillovDenis/bugfix/82-pass_bearer_to_request

[#82] Using bearer token
This commit is contained in:
Roman Khimov 2021-06-16 12:58:54 +03:00 committed by GitHub
commit 473536372a
3 changed files with 26 additions and 14 deletions

View file

@ -33,9 +33,10 @@ type (
func (n *layer) containerInfo(ctx context.Context, cid *cid.ID) (*BucketInfo, error) { func (n *layer) containerInfo(ctx context.Context, cid *cid.ID) (*BucketInfo, error) {
var ( var (
err error err error
res *container.Container res *container.Container
rid = api.GetRequestID(ctx) rid = api.GetRequestID(ctx)
bearerOpt = n.BearerOpt(ctx)
info = &BucketInfo{ info = &BucketInfo{
CID: cid, CID: cid,
@ -50,7 +51,7 @@ func (n *layer) containerInfo(ctx context.Context, cid *cid.ID) (*BucketInfo, er
zap.Error(err)) zap.Error(err))
return nil, err return nil, err
} }
res, err = conn.GetContainer(ctx, cid) res, err = conn.GetContainer(ctx, cid, bearerOpt)
if err != nil { if err != nil {
n.log.Error("could not fetch container", n.log.Error("could not fetch container",
zap.Stringer("cid", cid), zap.Stringer("cid", cid),
@ -87,10 +88,11 @@ func (n *layer) containerInfo(ctx context.Context, cid *cid.ID) (*BucketInfo, er
func (n *layer) containerList(ctx context.Context) ([]*BucketInfo, error) { func (n *layer) containerList(ctx context.Context) ([]*BucketInfo, error) {
var ( var (
err error err error
own = n.Owner(ctx) own = n.Owner(ctx)
res []*cid.ID bearerOpt = n.BearerOpt(ctx)
rid = api.GetRequestID(ctx) res []*cid.ID
rid = api.GetRequestID(ctx)
) )
conn, _, err := n.pool.Connection() conn, _, err := n.pool.Connection()
@ -100,7 +102,7 @@ func (n *layer) containerList(ctx context.Context) ([]*BucketInfo, error) {
zap.Error(err)) zap.Error(err))
return nil, err return nil, err
} }
res, err = conn.ListContainers(ctx, own) res, err = conn.ListContainers(ctx, own, bearerOpt)
if err != nil { if err != nil {
n.log.Error("could not fetch container", n.log.Error("could not fetch container",
zap.String("request_id", rid), zap.String("request_id", rid),

View file

@ -113,10 +113,19 @@ func (n *layer) Owner(ctx context.Context) *owner.ID {
return n.pool.OwnerID() return n.pool.OwnerID()
} }
// BearerOpt returns client.WithBearer call option with token from context or with nil token.
func (n *layer) BearerOpt(ctx context.Context) client.CallOption {
if tkn, ok := ctx.Value(api.BearerTokenKey).(*token.BearerToken); ok && tkn != nil {
return client.WithBearer(tkn)
}
return client.WithBearer(nil)
}
// Get NeoFS Object by refs.Address (should be used by auth.Center). // Get NeoFS Object by refs.Address (should be used by auth.Center).
func (n *layer) Get(ctx context.Context, address *object.Address) (*object.Object, error) { func (n *layer) Get(ctx context.Context, address *object.Address) (*object.Object, error) {
ops := new(client.GetObjectParams).WithAddress(address) ops := new(client.GetObjectParams).WithAddress(address)
return n.pool.GetObject(ctx, ops) return n.pool.GetObject(ctx, ops, n.BearerOpt(ctx))
} }
// GetBucketInfo returns bucket info by name. // GetBucketInfo returns bucket info by name.

View file

@ -42,7 +42,7 @@ func (n *layer) objectSearch(ctx context.Context, p *findParams) ([]*object.ID,
} else if filename != "" { } else if filename != "" {
opts.AddFilter(object.AttributeFileName, filename, object.MatchStringEqual) opts.AddFilter(object.AttributeFileName, filename, object.MatchStringEqual)
} }
return n.pool.SearchObject(ctx, new(client.SearchObjectParams).WithContainerID(p.cid).WithSearchFilters(opts)) return n.pool.SearchObject(ctx, new(client.SearchObjectParams).WithContainerID(p.cid).WithSearchFilters(opts), n.BearerOpt(ctx))
} }
// objectFindID returns object id (uuid) based on it's nice name in s3. If // objectFindID returns object id (uuid) based on it's nice name in s3. If
@ -62,7 +62,7 @@ func (n *layer) objectFindID(ctx context.Context, p *findParams) (*object.ID, er
// objectHead returns all object's headers. // objectHead returns all object's headers.
func (n *layer) objectHead(ctx context.Context, address *object.Address) (*object.Object, error) { func (n *layer) objectHead(ctx context.Context, address *object.Address) (*object.Object, error) {
ops := new(client.ObjectHeaderParams).WithAddress(address).WithAllFields() ops := new(client.ObjectHeaderParams).WithAddress(address).WithAllFields()
return n.pool.GetObjectHeader(ctx, ops) return n.pool.GetObjectHeader(ctx, ops, n.BearerOpt(ctx))
} }
// objectGet and write it into provided io.Reader. // objectGet and write it into provided io.Reader.
@ -70,7 +70,7 @@ func (n *layer) objectGet(ctx context.Context, p *getParams) (*object.Object, er
// prepare length/offset writer // prepare length/offset writer
w := newWriter(p.Writer, p.offset, p.length) w := newWriter(p.Writer, p.offset, p.length)
ops := new(client.GetObjectParams).WithAddress(p.address).WithPayloadWriter(w) ops := new(client.GetObjectParams).WithAddress(p.address).WithPayloadWriter(w)
return n.pool.GetObject(ctx, ops) return n.pool.GetObject(ctx, ops, n.BearerOpt(ctx))
} }
// objectPut into NeoFS, took payload from io.Reader. // objectPut into NeoFS, took payload from io.Reader.
@ -128,6 +128,7 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
oid, err := n.pool.PutObject( oid, err := n.pool.PutObject(
ctx, ctx,
ops, ops,
n.BearerOpt(ctx),
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -150,5 +151,5 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
func (n *layer) objectDelete(ctx context.Context, address *object.Address) error { func (n *layer) objectDelete(ctx context.Context, address *object.Address) error {
dop := new(client.DeleteObjectParams) dop := new(client.DeleteObjectParams)
dop.WithAddress(address) dop.WithAddress(address)
return n.pool.DeleteObject(ctx, dop) return n.pool.DeleteObject(ctx, dop, n.BearerOpt(ctx))
} }