2025-07-21 17:16:10 +03:00
|
|
|
package storage
|
2025-07-06 22:59:08 +03:00
|
|
|
|
|
|
|
import (
|
2025-07-27 19:02:05 +03:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
|
|
|
|
"code.uint32.ru/tiny/storage/internal/errinternal"
|
2025-07-21 17:16:10 +03:00
|
|
|
"code.uint32.ru/tiny/storage/internal/filesystem"
|
|
|
|
"code.uint32.ru/tiny/storage/internal/natsobj"
|
|
|
|
"code.uint32.ru/tiny/storage/internal/vault"
|
2025-07-06 22:59:08 +03:00
|
|
|
)
|
|
|
|
|
2025-07-27 19:02:05 +03:00
|
|
|
var (
|
|
|
|
// ErrInvalidKey is returned when key validation
|
|
|
|
// for particular implementation of Storage fails.
|
|
|
|
ErrInvalidKey = errinternal.ErrInvalidKey
|
|
|
|
// ErrNotFound is returned when object is not found
|
|
|
|
// in Storage.
|
|
|
|
ErrNotFound = errinternal.ErrNotFound
|
|
|
|
)
|
|
|
|
|
2025-07-06 22:59:08 +03:00
|
|
|
var (
|
|
|
|
_ Storage = (*natsobj.Storage)(nil)
|
|
|
|
_ Storage = (*filesystem.Storage)(nil)
|
2025-07-21 17:16:10 +03:00
|
|
|
_ Storage = (*vault.Storage)(nil)
|
2025-07-06 22:59:08 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Storage is a very basic object store.
|
|
|
|
type Storage interface {
|
2025-07-21 17:16:10 +03:00
|
|
|
// Save puts object with name 'key' into the store.
|
|
|
|
// If a key already exists it gets overwritten.
|
2025-07-06 22:59:08 +03:00
|
|
|
Save(key string, data []byte) error
|
2025-07-27 19:02:05 +03:00
|
|
|
// Load returns contents of object named 'key' or
|
|
|
|
// ErrNotFound.
|
2025-07-06 22:59:08 +03:00
|
|
|
Load(key string) ([]byte, error)
|
2025-07-21 17:16:10 +03:00
|
|
|
// Delete removes object named 'key' from the store.
|
|
|
|
// If key does not exist Delete returns nil.
|
2025-07-06 22:59:08 +03:00
|
|
|
Delete(key string) error
|
|
|
|
}
|
|
|
|
|
2025-07-27 19:02:05 +03:00
|
|
|
// NewNats wraps the provided ObjectStore with Storage interface.
|
|
|
|
func NewNats(store nats.ObjectStore) Storage {
|
|
|
|
return natsobj.New(store)
|
2025-07-06 22:59:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFS established a key/value within the directory 'path'
|
2025-07-27 19:02:05 +03:00
|
|
|
// and uses it as underlying physical store.
|
|
|
|
// Note that the implementation requires keys to be at least
|
|
|
|
// 3 characters long.
|
|
|
|
// Key "abcd" will be stored in /path/a/b/abcd.
|
2025-07-06 22:59:08 +03:00
|
|
|
func NewFS(path string) (Storage, error) {
|
2025-07-27 19:02:05 +03:00
|
|
|
return filesystem.New(path)
|
2025-07-06 22:59:08 +03:00
|
|
|
}
|
2025-07-21 17:16:10 +03:00
|
|
|
|
2025-07-27 19:02:05 +03:00
|
|
|
// NewVault uses provided Vault client to store objects.
|
2025-08-10 13:02:38 +03:00
|
|
|
// All objects are stored as a single secret, a JSON object
|
|
|
|
// where key are keys and values are base64 encoded bytes of
|
|
|
|
// saved object.
|
|
|
|
// If secret specified by path does not exist it will be created
|
|
|
|
// on first call to Storage methods.
|
|
|
|
// Note that a secret in vault gets updated (a new version of secret is created)
|
|
|
|
// on every save/delete operation.
|
2025-07-27 19:02:05 +03:00
|
|
|
func NewVault(client *api.Client, path string) Storage {
|
|
|
|
return vault.New(client, path)
|
2025-07-21 17:16:10 +03:00
|
|
|
}
|