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"
|
2024-08-30 12:05:32 +00:00
|
|
|
"strings"
|
2021-05-25 19:59:21 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/cache"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
2024-02-06 13:44:49 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-03-07 14:38:08 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2024-01-19 07:00:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 14:38:08 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-06-24 15:21:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2024-02-06 13:44:49 +00:00
|
|
|
"go.uber.org/zap"
|
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 {
|
2024-08-30 12:05:32 +00:00
|
|
|
GetBox(context.Context, cid.ID, string) (*accessbox.Box, []object.Attribute, error)
|
|
|
|
Put(context.Context, CredentialsParam) (oid.Address, error)
|
|
|
|
Update(context.Context, CredentialsParam) (oid.Address, error)
|
2024-01-19 07:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CredentialsParam struct {
|
2024-08-30 12:05:32 +00:00
|
|
|
Container cid.ID
|
|
|
|
AccessKeyID string
|
2024-01-19 07:00:04 +00:00
|
|
|
AccessBox *accessbox.AccessBox
|
|
|
|
Expiration uint64
|
|
|
|
Keys keys.PublicKeys
|
|
|
|
CustomAttributes []object.Attribute
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cred struct {
|
2024-02-06 13:44:49 +00:00
|
|
|
key *keys.PrivateKey
|
|
|
|
frostFS FrostFS
|
|
|
|
cache *cache.AccessBoxCache
|
|
|
|
removingCheckDuration time.Duration
|
|
|
|
log *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
Config struct {
|
|
|
|
FrostFS FrostFS
|
|
|
|
Key *keys.PrivateKey
|
|
|
|
CacheConfig *cache.Config
|
|
|
|
RemovingCheckAfterDurations time.Duration
|
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 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
|
|
|
|
2023-06-13 09:35:40 +00:00
|
|
|
// Optional.
|
|
|
|
// If provided cred object will be created using crdt approach.
|
2024-08-30 12:05:32 +00:00
|
|
|
NewVersionForAccessKeyID string
|
|
|
|
|
|
|
|
// Optional.
|
|
|
|
// If provided cred object will contain specific crdt name attribute for first accessbox object version.
|
|
|
|
// If NewVersionForAccessKeyID is provided this field isn't used.
|
|
|
|
CustomAccessKey string
|
2023-06-13 09:35:40 +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
|
2024-01-19 07:00:04 +00:00
|
|
|
|
|
|
|
// CustomAttributes are additional user provided attributes for box object.
|
|
|
|
CustomAttributes []object.Attribute
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
// PrmGetCredsObject groups parameters of getting credential object.
|
|
|
|
type PrmGetCredsObject struct {
|
|
|
|
// FrostFS container to get the object.
|
|
|
|
Container cid.ID
|
|
|
|
|
|
|
|
// S3 access key id.
|
|
|
|
AccessKeyID string
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrCustomAccessKeyIDNotFound = errors.New("custom AccessKeyId not found")
|
|
|
|
|
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
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
// GetCredsObject gets the credential object from FrostFS network.
|
2023-06-13 09:35:40 +00:00
|
|
|
// It uses search by system name and select using CRDT 2PSet. In case of absence CRDT header
|
|
|
|
// it heads object by address.
|
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 payload from being read.
|
2024-08-30 12:05:32 +00:00
|
|
|
// Returns ErrCustomAccessKeyIDNotFound if provided AccessKey is custom, and it was not found.
|
2024-07-15 12:47:19 +00:00
|
|
|
// Object must contain full payload.
|
2024-08-30 12:05:32 +00:00
|
|
|
GetCredsObject(context.Context, PrmGetCredsObject) (*object.Object, 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")
|
|
|
|
)
|
|
|
|
|
2024-02-06 13:44:49 +00:00
|
|
|
var _ Credentials = (*cred)(nil)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// New creates a new Credentials instance using the given cli and key.
|
2024-02-06 13:44:49 +00:00
|
|
|
func New(cfg Config) Credentials {
|
|
|
|
return &cred{
|
|
|
|
frostFS: cfg.FrostFS,
|
|
|
|
key: cfg.Key,
|
|
|
|
cache: cache.NewAccessBoxCache(cfg.CacheConfig),
|
|
|
|
removingCheckDuration: cfg.RemovingCheckAfterDurations,
|
|
|
|
log: cfg.CacheConfig.Logger,
|
|
|
|
}
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
func (c *cred) GetBox(ctx context.Context, cnrID cid.ID, accessKeyID string) (*accessbox.Box, []object.Attribute, error) {
|
|
|
|
isCustomSecret := isCustom(accessKeyID)
|
|
|
|
cachedBoxValue := c.cache.Get(accessKeyID)
|
2024-02-06 13:44:49 +00:00
|
|
|
if cachedBoxValue != nil {
|
2024-08-30 12:05:32 +00:00
|
|
|
return c.checkIfCredentialsAreRemoved(ctx, cnrID, accessKeyID, cachedBoxValue, isCustomSecret)
|
2021-09-08 07:08:56 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
box, attrs, err := c.getAccessBox(ctx, cnrID, accessKeyID)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
2024-04-16 08:20:35 +00:00
|
|
|
return nil, nil, fmt.Errorf("get access box: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
cachedBox, err := box.GetBox(c.key, isCustomSecret)
|
2021-07-16 12:35:07 +00:00
|
|
|
if err != nil {
|
2024-04-16 08:20:35 +00:00
|
|
|
return nil, nil, fmt.Errorf("get gate box: %w", err)
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
c.putBoxToCache(accessKeyID, cachedBox, attrs)
|
2021-09-08 07:08:56 +00:00
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
return cachedBox, attrs, nil
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
func (c *cred) checkIfCredentialsAreRemoved(ctx context.Context, cnrID cid.ID, accessKeyID string, cachedBoxValue *cache.AccessBoxCacheValue, isCustomSecret bool) (*accessbox.Box, []object.Attribute, error) {
|
2024-02-09 06:40:40 +00:00
|
|
|
if time.Since(cachedBoxValue.PutTime) < c.removingCheckDuration {
|
2024-04-16 08:20:35 +00:00
|
|
|
return cachedBoxValue.Box, cachedBoxValue.Attributes, nil
|
2024-02-09 06:40:40 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
box, attrs, err := c.getAccessBox(ctx, cnrID, accessKeyID)
|
2024-02-09 06:40:40 +00:00
|
|
|
if err != nil {
|
|
|
|
if client.IsErrObjectAlreadyRemoved(err) {
|
2024-08-30 12:05:32 +00:00
|
|
|
c.cache.Delete(accessKeyID)
|
2024-04-16 08:20:35 +00:00
|
|
|
return nil, nil, fmt.Errorf("get access box: %w", err)
|
2024-02-09 06:40:40 +00:00
|
|
|
}
|
2024-04-16 08:20:35 +00:00
|
|
|
return cachedBoxValue.Box, cachedBoxValue.Attributes, nil
|
2024-02-09 06:40:40 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
cachedBox, err := box.GetBox(c.key, isCustomSecret)
|
2024-02-09 06:40:40 +00:00
|
|
|
if err != nil {
|
2024-08-30 12:05:32 +00:00
|
|
|
c.cache.Delete(accessKeyID)
|
2024-04-16 08:20:35 +00:00
|
|
|
return nil, nil, fmt.Errorf("get gate box: %w", err)
|
2024-02-09 06:40:40 +00:00
|
|
|
}
|
|
|
|
// we need this to reset PutTime
|
|
|
|
// to don't check for removing each time after removingCheckDuration interval
|
2024-08-30 12:05:32 +00:00
|
|
|
c.putBoxToCache(accessKeyID, cachedBox, attrs)
|
2024-02-09 06:40:40 +00:00
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
return cachedBoxValue.Box, attrs, nil
|
2024-02-09 06:40:40 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
func (c *cred) putBoxToCache(accessKeyID string, box *accessbox.Box, attrs []object.Attribute) {
|
|
|
|
if err := c.cache.Put(accessKeyID, box, attrs); err != nil {
|
|
|
|
c.log.Warn(logs.CouldntPutAccessBoxIntoCache, zap.String("accessKeyID", accessKeyID))
|
2024-02-06 13:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
func (c *cred) getAccessBox(ctx context.Context, cnrID cid.ID, accessKeyID string) (*accessbox.AccessBox, []object.Attribute, error) {
|
|
|
|
prm := PrmGetCredsObject{
|
|
|
|
Container: cnrID,
|
|
|
|
AccessKeyID: accessKeyID,
|
|
|
|
}
|
|
|
|
obj, err := c.frostFS.GetCredsObject(ctx, prm)
|
2022-02-08 16:54:04 +00:00
|
|
|
if err != nil {
|
2024-04-16 08:20:35 +00:00
|
|
|
return nil, nil, fmt.Errorf("read payload and attributes: %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
|
2024-04-16 08:20:35 +00:00
|
|
|
if err = box.Unmarshal(obj.Payload()); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unmarhal access box: %w", err)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
return &box, obj.Attributes(), nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
func (c *cred) Put(ctx context.Context, prm CredentialsParam) (oid.Address, error) {
|
|
|
|
if prm.AccessKeyID != "" {
|
|
|
|
c.log.Info(logs.CheckCustomAccessKeyIDUniqueness, zap.String("access_key_id", prm.AccessKeyID))
|
|
|
|
credsPrm := PrmGetCredsObject{
|
|
|
|
Container: prm.Container,
|
|
|
|
AccessKeyID: prm.AccessKeyID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := c.frostFS.GetCredsObject(ctx, credsPrm); err == nil {
|
|
|
|
return oid.Address{}, fmt.Errorf("access key id '%s' already exists", prm.AccessKeyID)
|
|
|
|
} else if !errors.Is(err, ErrCustomAccessKeyIDNotFound) {
|
|
|
|
return oid.Address{}, fmt.Errorf("check AccessKeyID uniqueness: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.createObject(ctx, prm, false)
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
func (c *cred) Update(ctx context.Context, prm CredentialsParam) (oid.Address, error) {
|
|
|
|
return c.createObject(ctx, prm, true)
|
2023-06-13 09:35:40 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
func (c *cred) createObject(ctx context.Context, prm CredentialsParam, update bool) (oid.Address, error) {
|
2024-01-19 07:00:04 +00:00
|
|
|
if len(prm.Keys) == 0 {
|
2022-06-27 09:37:03 +00:00
|
|
|
return oid.Address{}, ErrEmptyPublicKeys
|
2024-01-19 07:00:04 +00:00
|
|
|
} else if prm.AccessBox == nil {
|
2022-06-27 09:37:03 +00:00
|
|
|
return oid.Address{}, ErrEmptyBearerToken
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2024-01-19 07:00:04 +00:00
|
|
|
data, err := prm.AccessBox.Marshal()
|
2021-06-14 13:39:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
var newVersionFor string
|
|
|
|
if update {
|
|
|
|
newVersionFor = prm.AccessKeyID
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
idObj, err := c.frostFS.CreateObject(ctx, PrmObjectCreate{
|
2024-08-30 12:05:32 +00:00
|
|
|
Container: prm.Container,
|
|
|
|
Filepath: strconv.FormatInt(time.Now().Unix(), 10) + "_access.box",
|
|
|
|
ExpirationEpoch: prm.Expiration,
|
|
|
|
CustomAccessKey: prm.AccessKeyID,
|
|
|
|
NewVersionForAccessKeyID: newVersionFor,
|
|
|
|
Payload: data,
|
|
|
|
CustomAttributes: prm.CustomAttributes,
|
2022-03-01 19:02:24 +00:00
|
|
|
})
|
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)
|
2024-08-30 12:05:32 +00:00
|
|
|
addr.SetContainer(prm.Container)
|
2022-05-25 17:25:43 +00:00
|
|
|
|
2022-06-27 09:37:03 +00:00
|
|
|
return addr, nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
2024-08-30 12:05:32 +00:00
|
|
|
|
|
|
|
func isCustom(accessKeyID string) bool {
|
|
|
|
return (&oid.Address{}).DecodeString(strings.ReplaceAll(accessKeyID, "0", "/")) != nil
|
|
|
|
}
|