2021-05-25 19:59:21 +00:00
|
|
|
package bearer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-05-26 16:48:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2021-05-25 19:59:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2021-05-26 16:48:27 +00:00
|
|
|
sdk "github.com/nspcc-dev/neofs-http-gw/neofs"
|
2021-05-25 19:59:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
2021-05-26 16:48:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/hcs"
|
2021-05-25 19:59:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-05-26 18:23:36 +00:00
|
|
|
// Credentials is a bearer token get/put interface.
|
2021-05-25 19:59:21 +00:00
|
|
|
Credentials interface {
|
|
|
|
Get(context.Context, *object.Address) (*token.BearerToken, error)
|
|
|
|
Put(context.Context, *container.ID, *token.BearerToken, ...hcs.PublicKey) (*object.Address, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
cred struct {
|
|
|
|
key hcs.PrivateKey
|
2021-05-26 16:48:27 +00:00
|
|
|
obj sdk.ClientPlant
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-05-26 18:23:36 +00:00
|
|
|
// ErrEmptyPublicKeys is returned when no HCS keys are provided.
|
|
|
|
ErrEmptyPublicKeys = errors.New("HCS public keys could not be empty")
|
|
|
|
// ErrEmptyBearerToken is returned when no bearer token is provided.
|
2021-05-25 19:59:21 +00:00
|
|
|
ErrEmptyBearerToken = errors.New("Bearer token could not be empty")
|
|
|
|
)
|
|
|
|
|
|
|
|
var bufferPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = New
|
|
|
|
|
2021-05-26 18:23:36 +00:00
|
|
|
// New creates new Credentials instance using given cli and key.
|
2021-05-26 16:48:27 +00:00
|
|
|
func New(cli sdk.ClientPlant, key hcs.PrivateKey) Credentials {
|
2021-05-25 19:59:21 +00:00
|
|
|
return &cred{obj: cli, key: key}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cred) acquireBuffer() *bytes.Buffer {
|
|
|
|
return bufferPool.Get().(*bytes.Buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cred) releaseBuffer(buf *bytes.Buffer) {
|
|
|
|
buf.Reset()
|
|
|
|
bufferPool.Put(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cred) Get(ctx context.Context, address *object.Address) (*token.BearerToken, error) {
|
|
|
|
buf := c.acquireBuffer()
|
|
|
|
defer c.releaseBuffer(buf)
|
|
|
|
|
|
|
|
box := accessbox.NewBearerBox(nil)
|
|
|
|
|
2021-05-26 16:48:27 +00:00
|
|
|
conn, tok, err := c.obj.ConnectionArtifacts()
|
|
|
|
if err != nil {
|
2021-05-25 19:59:21 +00:00
|
|
|
return nil, err
|
2021-05-26 16:48:27 +00:00
|
|
|
}
|
|
|
|
ops := new(client.GetObjectParams).WithAddress(address).WithPayloadWriter(buf)
|
|
|
|
|
|
|
|
_, err = conn.GetObject(
|
|
|
|
ctx,
|
|
|
|
ops,
|
|
|
|
client.WithSession(tok),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = accessbox.NewDecoder(buf, c.key).Decode(box)
|
|
|
|
if err != nil {
|
2021-05-25 19:59:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return box.Token(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cred) Put(ctx context.Context, cid *container.ID, tkn *token.BearerToken, keys ...hcs.PublicKey) (*object.Address, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
buf = c.acquireBuffer()
|
|
|
|
box = accessbox.NewBearerBox(tkn)
|
|
|
|
|
|
|
|
created = strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
)
|
|
|
|
|
|
|
|
defer c.releaseBuffer(buf)
|
|
|
|
|
|
|
|
if len(keys) == 0 {
|
|
|
|
return nil, ErrEmptyPublicKeys
|
|
|
|
} else if tkn == nil {
|
|
|
|
return nil, ErrEmptyBearerToken
|
|
|
|
} else if err = accessbox.NewEncoder(buf, c.key, keys...).Encode(box); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:48:27 +00:00
|
|
|
conn, tok, err := c.obj.ConnectionArtifacts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-25 19:59:21 +00:00
|
|
|
timestamp := object.NewAttribute()
|
|
|
|
timestamp.SetKey(object.AttributeTimestamp)
|
|
|
|
timestamp.SetValue(created)
|
|
|
|
|
|
|
|
filename := object.NewAttribute()
|
|
|
|
filename.SetKey(object.AttributeFileName)
|
|
|
|
filename.SetValue(created + "_access.box")
|
|
|
|
|
|
|
|
raw := object.NewRaw()
|
|
|
|
raw.SetContainerID(cid)
|
|
|
|
raw.SetOwnerID(tkn.Issuer())
|
|
|
|
raw.SetAttributes(filename, timestamp)
|
|
|
|
|
2021-05-26 16:48:27 +00:00
|
|
|
ops := new(client.PutObjectParams).WithObject(raw.Object()).WithPayloadReader(buf)
|
|
|
|
oid, err := conn.PutObject(
|
|
|
|
ctx,
|
|
|
|
ops,
|
|
|
|
client.WithSession(tok),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
address := object.NewAddress()
|
|
|
|
address.SetObjectID(oid)
|
|
|
|
address.SetContainerID(cid)
|
|
|
|
return address, nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|