2022-03-20 19:55:47 +00:00
|
|
|
package persistent
|
|
|
|
|
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
2022-03-21 11:53:49 +00:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2022-03-20 19:55:47 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/session/storage"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-03-20 19:55:47 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TokenStore is a wrapper around persistent K:V db that
|
|
|
|
// allows creating (storing), retrieving and expiring
|
|
|
|
// (removing) session tokens.
|
|
|
|
type TokenStore struct {
|
|
|
|
db *bbolt.DB
|
|
|
|
|
2022-09-28 07:41:01 +00:00
|
|
|
l *logger.Logger
|
2022-03-21 11:53:49 +00:00
|
|
|
|
|
|
|
// optional AES-256 algorithm
|
|
|
|
// encryption in Galois/Counter
|
|
|
|
// Mode
|
|
|
|
gcm cipher.AEAD
|
2022-03-20 19:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sessionsBucket = []byte("sessions")
|
|
|
|
|
|
|
|
// NewTokenStore creates, initializes and returns a new TokenStore instance.
|
|
|
|
//
|
|
|
|
// The elements of the instance are stored in bolt DB.
|
|
|
|
func NewTokenStore(path string, opts ...Option) (*TokenStore, error) {
|
|
|
|
cfg := defaultCfg()
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(cfg)
|
|
|
|
}
|
|
|
|
|
2023-10-31 11:56:55 +00:00
|
|
|
db, err := bbolt.Open(path, 0o600,
|
2022-03-20 19:55:47 +00:00
|
|
|
&bbolt.Options{
|
|
|
|
Timeout: cfg.timeout,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't open bbolt at %s: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucketIfNotExists(sessionsBucket)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
_ = db.Close()
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("could not init session bucket: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:53:49 +00:00
|
|
|
ts := &TokenStore{db: db, l: cfg.l}
|
|
|
|
|
|
|
|
// enable encryption if it
|
|
|
|
// was configured so
|
|
|
|
if cfg.privateKey != nil {
|
2022-03-27 15:55:24 +00:00
|
|
|
rawKey := make([]byte, (cfg.privateKey.Curve.Params().N.BitLen()+7)/8)
|
|
|
|
cfg.privateKey.D.FillBytes(rawKey)
|
2022-03-21 11:53:49 +00:00
|
|
|
|
2022-03-27 15:55:24 +00:00
|
|
|
c, err := aes.NewCipher(rawKey)
|
2022-03-21 11:53:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create cipher block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not wrapp cipher block in Galois Counter Mode: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts.gcm = gcm
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts, nil
|
2022-03-20 19:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns private token corresponding to the given identifiers.
|
|
|
|
//
|
|
|
|
// Returns nil is there is no element in storage.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (s *TokenStore) Get(ownerID user.ID, tokenID []byte) (t *storage.PrivateToken) {
|
2022-05-17 13:59:46 +00:00
|
|
|
err := s.db.View(func(tx *bbolt.Tx) error {
|
2022-03-20 19:55:47 +00:00
|
|
|
rootBucket := tx.Bucket(sessionsBucket)
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
ownerBucket := rootBucket.Bucket(ownerID.WalletBytes())
|
2022-03-20 19:55:47 +00:00
|
|
|
if ownerBucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rawToken := ownerBucket.Get(tokenID)
|
|
|
|
if rawToken == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
var err error
|
|
|
|
|
2022-03-21 11:53:49 +00:00
|
|
|
t, err = s.unpackToken(rawToken)
|
2023-08-14 11:06:17 +00:00
|
|
|
return err
|
2022-03-20 19:55:47 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.l.Error(context.Background(), logs.PersistentCouldNotGetSessionFromPersistentStorage,
|
2022-03-20 19:55:47 +00:00
|
|
|
zap.Error(err),
|
|
|
|
zap.Stringer("ownerID", ownerID),
|
|
|
|
zap.String("tokenID", hex.EncodeToString(tokenID)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveOld removes all tokens expired since provided epoch.
|
|
|
|
func (s *TokenStore) RemoveOld(epoch uint64) {
|
|
|
|
err := s.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
rootBucket := tx.Bucket(sessionsBucket)
|
|
|
|
|
|
|
|
// iterating over ownerIDs
|
|
|
|
return iterateNestedBuckets(rootBucket, func(b *bbolt.Bucket) error {
|
|
|
|
c := b.Cursor()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// iterating over fixed ownerID's tokens
|
|
|
|
for k, v := c.First(); k != nil; k, v = c.Next() {
|
|
|
|
if epochFromToken(v) <= epoch {
|
|
|
|
err = c.Delete()
|
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.l.Error(context.Background(), logs.PersistentCouldNotDeleteSToken,
|
2022-03-20 19:55:47 +00:00
|
|
|
zap.String("token_id", hex.EncodeToString(k)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.l.Error(context.Background(), logs.PersistentCouldNotCleanUpExpiredTokens,
|
2022-03-20 19:55:47 +00:00
|
|
|
zap.Uint64("epoch", epoch),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes database connection.
|
|
|
|
func (s *TokenStore) Close() error {
|
|
|
|
return s.db.Close()
|
|
|
|
}
|