libstore

package module
v0.0.0-...-069751c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2024 License: MIT Imports: 20 Imported by: 0

README

libstore

libstore is a Go package offering a flexible key-value storage interface with support for multiple backends. The driving idea behind the design is to ensure the ability to migrate data from one backend to another and to chain backends.

Features

  • In-Memory (InMemoryOps): Fast, ephemeral storage for testing or caching.
  • PostgreSQL (dbOps): Persistent, versioned storage.
  • AWS S3 (S3Ops): Scalable cloud-based storage.
  • Encryption (CryptStore): Encrypts data before storage and decrypts on retrieval, ideal for client-side encryption with S3.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TranslateToError

func TranslateToError(code int, message string) error

Types

type CryptStore

type CryptStore struct {
	// contains filtered or unexported fields
}

func (CryptStore) Create

func (m CryptStore) Create(ctx context.Context, key string) error

Create implements libstore.Ops.

func (CryptStore) Delete

func (m CryptStore) Delete(ctx context.Context, key string) error

Delete implements libstore.Ops.

func (CryptStore) List

func (m CryptStore) List(ctx context.Context) ([]string, error)

List implements libstore.Ops.

func (CryptStore) Put

func (m CryptStore) Put(ctx context.Context, key string, entry []byte) error

Put implements libstore.Ops.

func (CryptStore) Read

func (m CryptStore) Read(ctx context.Context, key string) ([]byte, error)

Read implements libstore.Ops.

func (CryptStore) ReadAll

func (m CryptStore) ReadAll(ctx context.Context, key string) ([][]byte, error)

ReadAll implements libstore.Ops.

type DecryptionError

type DecryptionError string

func (DecryptionError) Error

func (e DecryptionError) Error() string

type EntryError

type EntryError string

func (EntryError) Error

func (e EntryError) Error() string

type Error

type Error struct {
	Code    ErrorCode `json:"code"`
	Message string    `json:"message"`
}

func NewError

func NewError(err error) *Error

func (*Error) Error

func (e *Error) Error() string

type ErrorCode

type ErrorCode int
const (
	ErrUnknown ErrorCode = iota
	ErrLocation
	ErrKey
	ErrEntry
	ErrOpsInternal
	ErrKeyNotFound
)

type InMemoryOps

type InMemoryOps struct {
	// contains filtered or unexported fields
}

InMemoryOps is an in-memory implementation of the Ops interface.

func NewInMemoryOps

func NewInMemoryOps() *InMemoryOps

NewInMemoryOps creates a new InMemoryOps instance.

func (*InMemoryOps) Create

func (ops *InMemoryOps) Create(ctx context.Context, key string) error

Create creates a new key in the store.

func (*InMemoryOps) Delete

func (ops *InMemoryOps) Delete(ctx context.Context, key string) error

Delete deletes the key and all its associated entries.

func (*InMemoryOps) List

func (ops *InMemoryOps) List(ctx context.Context) ([]string, error)

List lists all keys in the store.

func (*InMemoryOps) Put

func (ops *InMemoryOps) Put(ctx context.Context, key string, entry []byte) error

Put replaces all entries associated with the key with a single entry.

func (*InMemoryOps) Read

func (ops *InMemoryOps) Read(ctx context.Context, key string) ([]byte, error)

ReadLast reads the last entry associated with the key.

func (*InMemoryOps) ReadAll

func (ops *InMemoryOps) ReadAll(ctx context.Context, key string) ([][]byte, error)

ReadWhole reads the entire content associated with the key.

type KeyError

type KeyError string

func (KeyError) Error

func (e KeyError) Error() string

type KeyNotFoundError

type KeyNotFoundError string

func (KeyNotFoundError) Error

func (e KeyNotFoundError) Error() string

type LocationError

type LocationError string

func (LocationError) Error

func (e LocationError) Error() string

type Ops

type Ops interface {
	// Create creates a new key.
	// It returns an error if the key already exists or if there is an issue creating the key.
	Create(ctx context.Context, key string) error
	// ReadAll reads the entire content of the given key.
	// It returns the content as a byte slice or an error if the content cannot be read.
	ReadAll(ctx context.Context, key string) ([][]byte, error)
	// Read reads the last entry of the given key.
	// It returns the last entry or an error if the file cannot be read.
	Read(ctx context.Context, key string) ([]byte, error)
	// Put replaces an entry to the file with the given key.
	// It returns an error if the file cannot be opened or written to.
	Put(ctx context.Context, key string, entry []byte) error
	// Delete deletes the given key and associated content.
	// It returns an error if the key or associated content cannot be deleted.
	Delete(ctx context.Context, key string) error
	// List lists all keys in the bucket-scope.
	// It returns a slice of key names or an error if the bucket-scope cannot be read.
	List(ctx context.Context) ([]string, error)
}

Ops defines the interface for data operations.

func NewCryptStoreCBC

func NewCryptStoreCBC(ops Ops, encyptionKey []byte, integrityKey []byte, calculateMAC func() hash.Hash, rand io.Reader) (Ops, error)

NewCryptStoreCBC initializes a new CryptStore instance using CBC-HMAC encryption.

Parameters:

  • ops: An instance of Ops that defines the underlying storage operations.
  • encryptionKey: A byte slice representing the encryption key used for the CBC encryption.
  • integrityKey: A byte slice representing the key used for HMAC integrity checks.
  • calculateMAC: A function returning a new hash.Hash used for generating the MAC.
  • rand: An io.Reader used as a source of randomness, typically crypto/rand.Reader.

Returns:

  • An Ops instance that wraps the provided storage operations with CBC-HMAC encryption.
  • An error if the encryption or decryption setup fails.

The function sets up an encryptor and decryptor using the specified keys and MAC function. It then returns a CryptStore that applies these operations on the provided Ops.

func NewCryptStoreGCM

func NewCryptStoreGCM(ops Ops, encyptionKey []byte, rand io.Reader) (Ops, error)

NewCryptStoreGCM initializes a new CryptStore instance using GCM encryption.

Parameters:

  • ops: An instance of Ops that defines the underlying storage operations.
  • encryptionKey: A byte slice representing the encryption key used for GCM encryption.
  • rand: An io.Reader used as a source of randomness, typically crypto/rand.Reader.

Returns:

  • An Ops instance that wraps the provided storage operations with GCM encryption.
  • An error if the encryption or decryption setup fails.

The function sets up an encryptor and decryptor using the specified encryption key. It then returns a CryptStore that applies these operations on the provided Ops.

func NewDBOps

func NewDBOps(ctx context.Context, conn string) (Ops, error)

NewDBOps initializes a new dbOps instance with a connection to a PostgreSQL database.

Parameters:

  • ctx: Context for managing request lifecycles.
  • conn: Connection string for connecting to the PostgreSQL database.

Returns:

  • An initialized dbOps instance that implements the Ops interface.
  • An error if the database connection fails or if the table cannot be created.

The function opens a connection to the PostgreSQL database using the provided connection string, and ensures that the necessary table ('FILES') exists by creating it if it does not.

Note: The function returns an OpsInternalError if any step of the initialization fails.

func NewFileOps

func NewFileOps(location string) (Ops, error)

NewFileOps initializes a new Ops instance with an OS filesystem-based implementation. It returns an error if the provided location is invalid.

type OpsInternalError

type OpsInternalError string

func (OpsInternalError) Error

func (e OpsInternalError) Error() string

type S3Ops

type S3Ops struct {
	// contains filtered or unexported fields
}

S3Ops provides operations for AWS S3 bucket interactions.

func NewS3Ops

func NewS3Ops(ctx context.Context, bucket string) (*S3Ops, error)

NewS3Ops initializes an S3Ops instance with AWS S3 client authorization.

Parameters:

  • ctx: Context for managing request lifecycles.
  • bucket: The name of the AWS S3 bucket to interact with.

Returns:

  • A pointer to an initialized S3Ops instance.
  • An error if the AWS configuration cannot be loaded or if the specified bucket cannot be accessed.

Environment Variables:

  • AWS_ACCESS_KEY_ID: AWS access key ID.
  • AWS_SECRET_ACCESS_KEY: AWS secret access key.
  • AWS_REGION: AWS region.

Note: These environment variables are required for the AWS SDK to authenticate and perform operations on the S3 bucket.

func (*S3Ops) Create

func (s *S3Ops) Create(ctx context.Context, key string) error

Create creates a new key in S3.

func (*S3Ops) Delete

func (s *S3Ops) Delete(ctx context.Context, key string) error

Delete deletes the given key and associated content.

func (*S3Ops) List

func (s *S3Ops) List(ctx context.Context) ([]string, error)

List lists all keys in the bucket-scope.

func (*S3Ops) Put

func (s *S3Ops) Put(ctx context.Context, key string, entry []byte) error

Put replaces an entry to the file with the given key.

func (*S3Ops) Read

func (s *S3Ops) Read(ctx context.Context, key string) ([]byte, error)

ReadLast reads the last entry of the given key.

func (*S3Ops) ReadAll

func (s *S3Ops) ReadAll(ctx context.Context, key string) ([][]byte, error)

ReadAll reads the entire content of the given key.

type TimestampError

type TimestampError string

func (TimestampError) Error

func (e TimestampError) Error() string

type ValidationError

type ValidationError string

func (ValidationError) Error

func (e ValidationError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL