2021-06-14 13:39:25 +00:00
|
|
|
package tokens
|
2021-05-25 19:59:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-02-08 16:54:04 +00:00
|
|
|
"fmt"
|
2021-05-25 19:59:21 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2022-12-14 16:58:00 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/cache"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
|
|
|
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/user"
|
2021-06-24 15:21:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
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 {
|
2022-05-25 17:25:43 +00:00
|
|
|
GetBox(context.Context, oid.Address) (*accessbox.Box, error)
|
2022-06-27 09:37:03 +00:00
|
|
|
Put(context.Context, cid.ID, user.ID, *accessbox.AccessBox, uint64, ...*keys.PublicKey) (oid.Address, error)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cred struct {
|
2022-12-20 08:38:58 +00:00
|
|
|
key *keys.PrivateKey
|
|
|
|
frostFS FrostFS
|
|
|
|
cache *cache.AccessBoxCache
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
// PrmObjectCreate groups parameters of objects created by credential tool.
|
|
|
|
type PrmObjectCreate struct {
|
2022-12-20 08:38:58 +00:00
|
|
|
// FrostFS identifier of the object creator.
|
2022-04-25 09:57:58 +00:00
|
|
|
Creator user.ID
|
2022-03-01 19:02:24 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// FrostFS container to store the object.
|
2022-03-01 19:02:24 +00:00
|
|
|
Container cid.ID
|
|
|
|
|
2022-09-07 06:59:24 +00:00
|
|
|
// File path.
|
|
|
|
Filepath string
|
2022-03-01 19:02:24 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// Last FrostFS epoch of the object lifetime.
|
2022-03-01 19:02:24 +00:00
|
|
|
ExpirationEpoch uint64
|
|
|
|
|
|
|
|
// Object payload.
|
|
|
|
Payload []byte
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// FrostFS represents virtual connection to FrostFS network.
|
|
|
|
type FrostFS interface {
|
2022-03-01 19:02:24 +00:00
|
|
|
// CreateObject creates and saves a parameterized object in the specified
|
2022-12-20 08:38:58 +00:00
|
|
|
// FrostFS container from a specific user. It sets 'Timestamp' attribute to the current time.
|
2022-04-13 16:56:58 +00:00
|
|
|
// It returns the ID of the saved object.
|
2022-03-01 19:02:24 +00:00
|
|
|
//
|
2022-04-13 16:56:58 +00:00
|
|
|
// It returns exactly one non-nil value. It returns any error encountered which
|
|
|
|
// prevented the object from being created.
|
2022-06-27 09:33:36 +00:00
|
|
|
CreateObject(context.Context, PrmObjectCreate) (oid.ID, error)
|
2022-03-01 19:02:24 +00:00
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// ReadObjectPayload reads payload of the object from FrostFS network by address
|
2022-03-01 19:02:24 +00:00
|
|
|
// into memory.
|
|
|
|
//
|
2022-04-13 16:56:58 +00:00
|
|
|
// It returns exactly one non-nil value. It returns any error encountered which
|
|
|
|
// prevented the object payload from being read.
|
2022-05-25 17:25:43 +00:00
|
|
|
ReadObjectPayload(context.Context, oid.Address) ([]byte, error)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
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 _ = New
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// New creates a new Credentials instance using the given cli and key.
|
2022-12-20 08:38:58 +00:00
|
|
|
func New(frostFS FrostFS, key *keys.PrivateKey, config *cache.Config) Credentials {
|
|
|
|
return &cred{frostFS: frostFS, key: key, cache: cache.NewAccessBoxCache(config)}
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
func (c *cred) GetBox(ctx context.Context, addr oid.Address) (*accessbox.Box, error) {
|
2022-02-08 16:54:04 +00:00
|
|
|
cachedBox := c.cache.Get(addr)
|
2021-09-08 07:08:56 +00:00
|
|
|
if cachedBox != nil {
|
|
|
|
return cachedBox, nil
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
box, err := c.getAccessBox(ctx, addr)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("get access box: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 07:08:56 +00:00
|
|
|
cachedBox, err = box.GetBox(c.key)
|
2021-07-16 12:35:07 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("get box: %w", err)
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
if err = c.cache.Put(addr, cachedBox); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("put box into cache: %w", err)
|
2021-09-08 07:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cachedBox, nil
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
func (c *cred) getAccessBox(ctx context.Context, addr oid.Address) (*accessbox.AccessBox, error) {
|
2022-12-20 08:38:58 +00:00
|
|
|
data, err := c.frostFS.ReadObjectPayload(ctx, addr)
|
2022-02-08 16:54:04 +00:00
|
|
|
if err != nil {
|
2022-03-01 19:02:24 +00:00
|
|
|
return nil, fmt.Errorf("read payload: %w", err)
|
2021-05-26 16:48:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
// decode access box
|
|
|
|
var box accessbox.AccessBox
|
|
|
|
if err = box.Unmarshal(data); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("unmarhal access box: %w", err)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
return &box, nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:37:03 +00:00
|
|
|
func (c *cred) Put(ctx context.Context, idCnr cid.ID, issuer user.ID, box *accessbox.AccessBox, expiration uint64, keys ...*keys.PublicKey) (oid.Address, error) {
|
2021-05-25 19:59:21 +00:00
|
|
|
if len(keys) == 0 {
|
2022-06-27 09:37:03 +00:00
|
|
|
return oid.Address{}, ErrEmptyPublicKeys
|
2021-06-14 13:39:25 +00:00
|
|
|
} else if box == nil {
|
2022-06-27 09:37:03 +00:00
|
|
|
return oid.Address{}, ErrEmptyBearerToken
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
data, err := box.Marshal()
|
|
|
|
if err != nil {
|
2022-06-27 09:37:03 +00:00
|
|
|
return oid.Address{}, fmt.Errorf("marshall box: %w", err)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
idObj, err := c.frostFS.CreateObject(ctx, PrmObjectCreate{
|
2022-04-25 09:57:58 +00:00
|
|
|
Creator: issuer,
|
2022-05-25 17:25:43 +00:00
|
|
|
Container: idCnr,
|
2022-09-07 06:59:24 +00:00
|
|
|
Filepath: strconv.FormatInt(time.Now().Unix(), 10) + "_access.box",
|
2022-03-01 19:02:24 +00:00
|
|
|
ExpirationEpoch: expiration,
|
|
|
|
Payload: data,
|
|
|
|
})
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
2022-06-27 09:37:03 +00:00
|
|
|
return oid.Address{}, fmt.Errorf("create object: %w", err)
|
2021-05-26 16:48:27 +00:00
|
|
|
}
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
var addr oid.Address
|
2022-06-27 09:33:36 +00:00
|
|
|
addr.SetObject(idObj)
|
2022-05-25 17:25:43 +00:00
|
|
|
addr.SetContainer(idCnr)
|
|
|
|
|
2022-06-27 09:37:03 +00:00
|
|
|
return addr, nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|