vaultstore

package module
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2025 License: AGPL-3.0 Imports: 28 Imported by: 0

README ΒΆ

Vault Store

Tests Status Go Report Card PkgGoDev

Vault - a secure value storage (data-at-rest) implementation for Go.

Scope

VaultStore is specifically designed as a data store component for securely storing and retrieving secrets. It is not an API or a complete secrets management system. Features such as user management, access control, and API endpoints are intentionally beyond the scope of this project.

VaultStore is meant to be integrated into your application as a library, providing the data storage layer for your secrets management needs. The application using VaultStore is responsible for implementing any additional layers such as API endpoints, user management, or access control if needed.

Documentation

Features

  • Secure storage of sensitive data
  • Token-based access to secrets
  • Password protection for stored values
  • Flexible query interface for retrieving records
  • Soft delete functionality for data recovery
  • Support for multiple database backends

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). You can find a copy of the license at https://www.gnu.org/licenses/agpl-3.0.en.html

For commercial use, please use my contact page to obtain a commercial license.

Installation

go get -u github.com/gouniverse/valuestore

Technical Details

For database schema, record structure, and other technical information, please see the Technical Reference.

Setup

vault, err := NewStore(NewStoreOptions{
	VaultTableName:     "my_vault",
	DB:                 databaseInstance,
	AutomigrateEnabled: true,
})

Usage

Here are some basic examples of using VaultStore. For comprehensive documentation, see the Usage Guide.

// Create a token
token, err := vault.TokenCreate("my_value", "my_password", 20)

// Check if a token exists
exists, err := vault.TokenExists(token)

// Read a value using a token
value, err := vault.TokenRead(token, "my_password")

// Update a token's value
err := vault.TokenUpdate(token, "new_value", "my_password")

// Hard delete a token
err := vault.TokenDelete(token)

// Soft delete a token
err := vault.TokenSoftDelete(token)

🌏 Development in the Cloud

Click any of the buttons below to start a new development environment to contribute to the codebase without having to install anything on your machine:

Open in GitHub Codespaces Open in Gitpod

Changelog

For a detailed version history and changes, please see the Changelog.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const COLUMN_CREATED_AT = "created_at"
View Source
const COLUMN_ID = "id"
View Source
const COLUMN_SOFT_DELETED_AT = "soft_deleted_at"
View Source
const COLUMN_UPDATED_AT = "updated_at"
View Source
const COLUMN_VAULT_TOKEN = "vault_token"
View Source
const COLUMN_VAULT_VALUE = "vault_value"
View Source
const TOKEN_PREFIX = "tk_"

Variables ΒΆ

This section is empty.

Functions ΒΆ

func IsToken ΒΆ added in v0.22.0

func IsToken(s string) bool

Types ΒΆ

type NewStoreOptions ΒΆ

type NewStoreOptions struct {
	VaultTableName     string
	DB                 *sql.DB
	DbDriverName       string
	AutomigrateEnabled bool
	DebugEnabled       bool
}

NewStoreOptions define the options for creating a new session store

type RecordInterface ΒΆ added in v0.25.0

type RecordInterface interface {
	Data() map[string]string
	DataChanged() map[string]string

	// Getters
	GetCreatedAt() string
	GetSoftDeletedAt() string
	GetID() string
	GetToken() string
	GetUpdatedAt() string
	GetValue() string

	// Setters
	SetCreatedAt(createdAt string) RecordInterface
	SetSoftDeletedAt(softDeletedAt string) RecordInterface
	SetID(id string) RecordInterface
	SetToken(token string) RecordInterface
	SetUpdatedAt(updatedAt string) RecordInterface
	SetValue(value string) RecordInterface
}

RecordInterface defines the methods that a Record must implement

func NewRecord ΒΆ

func NewRecord() RecordInterface

func NewRecordFromExistingData ΒΆ

func NewRecordFromExistingData(data map[string]string) RecordInterface

type RecordQueryInterface ΒΆ added in v0.25.0

type RecordQueryInterface interface {
	Validate() error

	GetColumns() []string
	SetColumns(columns []string) RecordQueryInterface

	IsIDSet() bool
	GetID() string
	SetID(id string) RecordQueryInterface

	IsIDInSet() bool
	GetIDIn() []string
	SetIDIn(idIn []string) RecordQueryInterface

	IsTokenSet() bool
	GetToken() string
	SetToken(token string) RecordQueryInterface

	IsTokenInSet() bool
	GetTokenIn() []string
	SetTokenIn(tokenIn []string) RecordQueryInterface

	IsOffsetSet() bool
	GetOffset() int
	SetOffset(offset int) RecordQueryInterface

	IsOrderBySet() bool
	GetOrderBy() string
	SetOrderBy(orderBy string) RecordQueryInterface

	IsLimitSet() bool
	GetLimit() int
	SetLimit(limit int) RecordQueryInterface

	IsCountOnlySet() bool
	GetCountOnly() bool
	SetCountOnly(countOnly bool) RecordQueryInterface

	IsSortOrderSet() bool
	GetSortOrder() string
	SetSortOrder(sortOrder string) RecordQueryInterface

	IsSoftDeletedIncludeSet() bool
	GetSoftDeletedInclude() bool
	SetSoftDeletedInclude(softDeletedInclude bool) RecordQueryInterface
	// contains filtered or unexported methods
}

func RecordQuery ΒΆ added in v0.25.0

func RecordQuery() RecordQueryInterface

RecordQuery creates a new record query

type Store ΒΆ

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

Store defines a session store

func NewStore ΒΆ

func NewStore(opts NewStoreOptions) (*Store, error)

NewStore creates a new entity store

func (*Store) AutoMigrate ΒΆ

func (st *Store) AutoMigrate() error

AutoMigrate auto migrate

func (*Store) EnableDebug ΒΆ

func (st *Store) EnableDebug(debug bool)

EnableDebug - enables the debug option

func (*Store) GetDbDriverName ΒΆ added in v0.25.0

func (st *Store) GetDbDriverName() string

func (*Store) GetVaultTableName ΒΆ added in v0.25.0

func (st *Store) GetVaultTableName() string

func (*Store) RecordCount ΒΆ

func (store *Store) RecordCount(ctx context.Context, query RecordQueryInterface) (int64, error)

func (*Store) RecordCreate ΒΆ

func (store *Store) RecordCreate(ctx context.Context, record RecordInterface) error

func (*Store) RecordDeleteByID ΒΆ

func (store *Store) RecordDeleteByID(ctx context.Context, recordID string) error

func (*Store) RecordDeleteByToken ΒΆ

func (store *Store) RecordDeleteByToken(ctx context.Context, token string) error

func (*Store) RecordFindByID ΒΆ

func (st *Store) RecordFindByID(ctx context.Context, id string) (RecordInterface, error)

FindByID finds an entry by ID

func (*Store) RecordFindByToken ΒΆ

func (st *Store) RecordFindByToken(ctx context.Context, token string) (RecordInterface, error)

RecordFindByToken finds a record entity by token

If the supplied token is empty, an error is returned ΒΆ

Parameters: - ctx: The context - token: The token to find

Returns: - record: The record found - err: An error if something went wrong

func (*Store) RecordList ΒΆ

func (store *Store) RecordList(ctx context.Context, query RecordQueryInterface) ([]RecordInterface, error)

func (*Store) RecordSoftDelete ΒΆ added in v0.25.0

func (store *Store) RecordSoftDelete(ctx context.Context, record RecordInterface) error

RecordSoftDelete soft deletes a record by setting the soft_deleted_at column to the current time

func (*Store) RecordSoftDeleteByID ΒΆ added in v0.25.0

func (store *Store) RecordSoftDeleteByID(ctx context.Context, recordID string) error

RecordSoftDeleteByID soft deletes a record by ID by setting the soft_deleted_at column to the current time

func (*Store) RecordSoftDeleteByToken ΒΆ added in v0.25.0

func (store *Store) RecordSoftDeleteByToken(ctx context.Context, token string) error

RecordSoftDeleteByToken soft deletes a record by token by setting the soft_deleted_at column to the current time

func (*Store) RecordUpdate ΒΆ

func (store *Store) RecordUpdate(ctx context.Context, record RecordInterface) error

func (*Store) SqlCreateTable ΒΆ

func (store *Store) SqlCreateTable() string

SqlCreateTable returns a SQL string for creating the setting table

func (*Store) TokenCreate ΒΆ

func (st *Store) TokenCreate(ctx context.Context, data string, password string, tokenLength int) (token string, err error)

TokenCreate creates a new record and returns the token

func (*Store) TokenCreateCustom ΒΆ

func (store *Store) TokenCreateCustom(ctx context.Context, token string, data string, password string) (err error)

func (*Store) TokenDelete ΒΆ

func (st *Store) TokenDelete(ctx context.Context, token string) error

TokenDelete deletes a token from the store

If the supplied token is empty, an error is returned ΒΆ

Parameters: - ctx: The context - token: The token to delete

Returns: - err: An error if something went wrong

func (*Store) TokenExists ΒΆ

func (store *Store) TokenExists(ctx context.Context, token string) (bool, error)

TokenExists checks if a token exists

If the supplied token is empty, an error is returned ΒΆ

Parameters: - ctx: The context - token: The token to check

Returns: - exists: A boolean indicating if the token exists - err: An error if something went wrong

func (*Store) TokenRead ΒΆ

func (st *Store) TokenRead(ctx context.Context, token string, password string) (value string, err error)

TokenRead retrieves the value of a token

If the token does not exist, an error is returned ΒΆ

Parameters: - ctx: The context - token: The token to retrieve - password: The password to use for decryption

Returns: - value: The value of the token - err: An error if something went wrong

func (*Store) TokenSoftDelete ΒΆ added in v0.25.0

func (st *Store) TokenSoftDelete(ctx context.Context, token string) error

TokenSoftDelete soft deletes a token from the store

Soft deleting keeps the record in the database but marks it as soft deleted and soft deleted records are not returned by default

If the supplied token is empty, an error is returned ΒΆ

Parameters: - ctx: The context - token: The token to soft delete

Returns: - err: An error if something went wrong

func (*Store) TokenUpdate ΒΆ

func (st *Store) TokenUpdate(ctx context.Context, token string, value string, password string) (err error)

TokenUpdate updates the value of a token

If the token does not exist, an error is returned ΒΆ

Parameters: - ctx: The context - token: The token to update - value: The new value - password: The password to use for encryption

Returns: - err: An error if something went wrong

func (*Store) TokensRead ΒΆ

func (st *Store) TokensRead(ctx context.Context, tokens []string, password string) (values map[string]string, err error)

TokensRead reads a list of tokens, returns a map of token to value

If a token is not found, it is not included in the map ΒΆ

Parameters: - ctx: The context - tokens: The list of tokens to read - password: The password to use for decryption

Returns: - values: A map of token to value - err: An error if something went wrong

type StoreInterface ΒΆ

type StoreInterface interface {
	AutoMigrate() error
	EnableDebug(debug bool)

	GetDbDriverName() string
	GetVaultTableName() string

	RecordCount(ctx context.Context, query RecordQueryInterface) (int64, error)
	RecordCreate(ctx context.Context, record RecordInterface) error
	RecordDeleteByID(ctx context.Context, recordID string) error
	RecordDeleteByToken(ctx context.Context, token string) error
	RecordFindByID(ctx context.Context, recordID string) (RecordInterface, error)
	RecordFindByToken(ctx context.Context, token string) (RecordInterface, error)
	RecordList(ctx context.Context, query RecordQueryInterface) ([]RecordInterface, error)
	RecordSoftDelete(ctx context.Context, record RecordInterface) error
	RecordSoftDeleteByID(ctx context.Context, recordID string) error
	RecordSoftDeleteByToken(ctx context.Context, token string) error
	RecordUpdate(ctx context.Context, record RecordInterface) error

	TokenCreate(ctx context.Context, value string, password string, tokenLength int) (token string, err error)
	TokenCreateCustom(ctx context.Context, token string, value string, password string) (err error)
	TokenDelete(ctx context.Context, token string) error
	TokenExists(ctx context.Context, token string) (bool, error)
	TokenRead(ctx context.Context, token string, password string) (string, error)
	TokenSoftDelete(ctx context.Context, token string) error
	TokenUpdate(ctx context.Context, token string, value string, password string) error
	TokensRead(ctx context.Context, tokens []string, password string) (map[string]string, error)
}

Jump to

Keyboard shortcuts

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