backend

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: AGPL-3.0-or-later Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeDropboxRemote = "dropbox"
	TypeFileSystem    = "filesystem"
	TypeWebserver     = "webserver"
	TypeSandstorm     = "sandstorm" // Uses webserver + WebserverBackend code
)

Variables

View Source
var (
	RANDOM_TAG_ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789"
	RANDOM_TAG_LENGTH   = 9

	ErrBackendExists = errors.New("Backend already exists")
)
View Source
var (
	ErrConfigExists = errors.New("Backend config already exists")
)
View Source
var (
	ErrMakerNotFound = errors.New("Backend maker func not found")
)
View Source
var (
	ErrNilConfig = errors.New("Backend Config is nil")
)
View Source
var (
	ErrNilCustom = errors.New("Field 'Custom' cannot be nil!")
)
View Source
var (
	ErrNoDefaultBackend = errors.New("backend: no default backend set;" +
		" specify a backend explicitly or set a default to use")
)
View Source
var (
	ErrWrongBackendType = errors.New("backend: wrong Backend type")
)
View Source
var (
	HttpGetTimeout = 300 * time.Second
)

Functions

func ConfigNameFromPath

func ConfigNameFromPath(fullpath string) string

func ConfigPathFromName

func ConfigPathFromName(backendPath, backendName string) string

func CreateFileRow

func CreateFileRow(bk Backend, pairs types.TagPairs, filename string, plaintags []string) (*types.Row, error)

func CreateJSONRow

func CreateJSONRow(bk Backend, pairs types.TagPairs, obj interface{}, plaintags []string) (*types.Row, error)

func CreateRow

func CreateRow(bk Backend, pairs types.TagPairs, rowData []byte, plaintags []string) (*types.Row, error)

func CreateTag

func CreateTag(bk Backend, plaintag string) (*types.TagPair, error)

CreateTag uses NewTagPair to create a new TagPair, then saves said TagPair in backend.

func CreateTagsFromPlain

func CreateTagsFromPlain(bk Backend, plaintags []string, pairs types.TagPairs) (newPairs types.TagPairs, err error)

CreateTagsFromPlain concurrently creates new TagPairs for each plaintag that doesn't already have a corresponding PlainTag in pairs. (Be sure that pairs contains the latest TagPairs contained in backend.)

func DeleteRows

func DeleteRows(bk Backend, pairs types.TagPairs, plaintags cryptag.PlainTags) error

func DropboxConfigToMap

func DropboxConfigToMap(cfg DropboxConfig) map[string]interface{}

func GetMaker

func GetMaker(bkType string) (maker, error)

GetMaker returns a Backend maker function that will make a Backend of the specified type. If no such Backend has been registered, ErrMakerNotFound is returned.

func IsDefaultBackendSet

func IsDefaultBackendSet(backendPath string) (bool, error)

Checks if a default backend is set. Returns true if so, false otherwise.

func ListRowsFromPlainTags

func ListRowsFromPlainTags(bk Backend, pairs types.TagPairs, plaintags cryptag.PlainTags) (types.Rows, error)

func NewTagPair

func NewTagPair(key *[32]byte, plaintag string) (*types.TagPair, error)

NewTagPair creates a (cryptographically secure pseudorandom) RandomTag that corresponds to the given PlainTag, generates a new nonce, encrypts the PlainTag, then creates and returns the newly allocated TagPair.

func PopulateRowBeforeSave

func PopulateRowBeforeSave(bk Backend, row *types.Row, pairs types.TagPairs) (newPairs types.TagPairs, err error)

PopulateRowBeforeSave creates a new TagPair for each plaintag unique to row, sets row.RandomTags, and sets row.Encrypted. row is now ready to be saved to a Backend.

func RegisterMaker

func RegisterMaker(bkType string, f maker) error

RegisterMaker registers a new Backend maker function by type. Suitable for placing in an init() function in the file where a new Backend type has been defined.

func RowsFromPlainTags

func RowsFromPlainTags(bk Backend, pairs types.TagPairs, plaintags cryptag.PlainTags) (types.Rows, error)

func SandstormWebKeyToMap

func SandstormWebKeyToMap(webkey string) map[string]interface{}

func Save

func Save(bk Backend) error

Save turns bk into a *Config then saves it to disk.

func SetDefaultBackend

func SetDefaultBackend(backendPath, newDefault string) error

SetDefaultBackend sets the default Backend to newDefault by creating a symlink from (newDefault).json to default.json

func UpdateFileRow

func UpdateFileRow(bk Backend, pairs types.TagPairs, prevIDTag string, newFilename string) (*types.Row, error)

UpdateFileRow finds the Row uniquely picked out by prevIDTag then creates a new Row consisting of the contents of newFilename and the tags from the Row being updated (after replacing id:..., created:..., and filename:..., and adding a origversionrow:... tag).

func UpdateKey

func UpdateKey(bk Backend, newKey interface{}) error

newKey can be of type *[32]byte, []byte (with length 32), or a string to be parsed with keyutil.Parse.

func UpdateRow

func UpdateRow(bk Backend, pairs types.TagPairs, prevIDTag string, newData []byte) (*types.Row, error)

UpdateRow creates a new version of the Row whose ID tag is prevIDTag, but replaces the "id:..." and "created:..." tags. It then adds an "origversionrow:..." tag if one does not already exist which points to the ID tag of the original Row being versioned here.

func UpdateRowAdvanced

func UpdateRowAdvanced(bk Backend, pairs types.TagPairs, oldRow *types.Row, newData []byte, newishTags []string) (*types.Row, error)

UpdateRowAdvanced creates a new version of oldRow but with updated tags that begin with newishTags, remove the "id:...", "created:...", and "all" tags, and will add an "origversionrow:..." tag that points to the ID tag of the original Row being versioned here (or keep the existing "origversionrow:..." tag).

If the plaintags that the new, updated row should have doesn't require any pre-processing, newishTags can simply be oldRow.PlainTags(). (You may want your pre-processing step to add tags like `prevversionrow:...` or user-specified tags.)

func WebserverConfigToMap

func WebserverConfigToMap(cfg WebserverConfig) map[string]interface{}

Types

type Backend

type Backend interface {
	Name() string
	Key() *[32]byte

	AllTagPairs(oldPairs types.TagPairs) (types.TagPairs, error)
	TagPairsFromRandomTags(randtags cryptag.RandomTags) (types.TagPairs, error)
	SaveTagPair(pair *types.TagPair) error

	ListRows(randtags cryptag.RandomTags) (types.Rows, error)
	RowsFromRandomTags(randtags cryptag.RandomTags) (types.Rows, error)
	SaveRow(row *types.Row) error
	DeleteRows(randtags cryptag.RandomTags) error

	ToConfig() (*Config, error)
}

Backend is an interface that represents a type of storage location for data, such as a filesystem or remote API.

func Create

func Create(bkType, bkName string, args []string) (Backend, error)

Create persists a new Backend Config to disk. DEPRECATED; use CreateFromConfig instead.

func CreateFromConfig

func CreateFromConfig(bkPath string, cfg *Config) (Backend, error)

CreateFromConfig persists a new Backend Config to disk using cfg, then returns a new Backend based on cfg.

func LoadBackend

func LoadBackend(backendPath, backendName string) (Backend, error)

func New

func New(cfg *Config) (Backend, error)

New makes a Backend based on cfg. Does not persist new *Config to disk.

func ReadBackends

func ReadBackends(backendPath, bkPattern string) ([]Backend, error)

ReadBackends reads all the Backend Configs at backendPath whose names match the pattern bkPattern, turns them into Backends, then returns all the successfully created Backends.

type Config

type Config struct {
	Name     string
	Type     string // Should be one of: backend.Type*
	New      bool   `json:"-"`
	Key      *[32]byte
	Local    bool
	DataPath string // Used by backend.FileSystem, other local backends

	Custom map[string]interface{} `json:",omitempty"` // Used by Dropbox, Webserver, other backends
}

Config represents the configuration info for a Backend. Configs store things like which type of Backend this is the config for (e.g., filesystem or webserver), the base URL of the Backend (if it's remote), API tokens, etc.

func ReadConfig

func ReadConfig(backendPath, backendName string) (*Config, error)

ReadConfig reads, parses, and unmarshals the Backend Config file with the name backendName and returns it.

func ReadConfigs

func ReadConfigs(backendPath, bkPattern string) ([]*Config, error)

ReadConfigs reads, parses, and unmarshals the Backend Config files located in backendPath (defaults to cryptag.BackendPath) and that matches the pattern bkPattern.

func (*Config) Backup

func (conf *Config) Backup(backendsDir string) error

Backup creates a backup of this Config to (backendsDir)/(conf.Name).json-$timestamp

func (*Config) Canonicalize

func (conf *Config) Canonicalize() error

Canonicalize makes changes to this Config to ensure that it is valid and returns an error when this cannot be ensured and no correction can be safely made (e.g., if conf.Name is empty). Useful while ensuring correctness of a new-created Config and right before saving one.

func (*Config) GetPath

func (conf *Config) GetPath() string

func (*Config) GetType

func (conf *Config) GetType() string

GetType returns the type of conf. Preferable to using .Type directly because this detects legacy Backend Configs with type TypeFileSystem, TypeWebserver, and TypeDropboxRemote.

func (*Config) Save

func (conf *Config) Save(backendsDir string) error

Save persists this config to disk. Returns error if a Config already exists with the same name.

func (*Config) Update

func (conf *Config) Update(backendsDir string) error

Update persists this config to disk. Returns error if there is no existing Config with the same name to update.

type DropboxConfig

type DropboxConfig struct {
	AppKey      string
	AppSecret   string
	AccessToken string
	BasePath    string // e.g., "/cryptag_folder_in_dropbox_root"
}

func DropboxConfigFromMap

func DropboxConfigFromMap(m map[string]interface{}) (DropboxConfig, error)

func (*DropboxConfig) Valid

func (dc *DropboxConfig) Valid() error

type DropboxRemote

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

DropboxRemote represents a Dropbox folder that is being used to store data in. Implements backend.Backend.

func DropboxRemoteFromConfig

func DropboxRemoteFromConfig(conf *Config) (*DropboxRemote, error)

DropboxRemoteFromConfig turns conf into a DropboxRemote Backend.

func LoadDropboxRemote

func LoadDropboxRemote(backendPath, backendName string) (*DropboxRemote, error)

LoadDropboxRemote tries to load a DropboxRemote Backend named backendName. For legacy reasons, if backendName is empty, uses "dropbox"-$(hostname).

func NewDropboxRemote

func NewDropboxRemote(key []byte, name string, cfg DropboxConfig) (*DropboxRemote, error)

NewDropboxRemote creates a new DropboxRemote using the given attributes and returns it.

For legacy reasons, will save this backend to disk if len(key) == 0 or name == "".

func (*DropboxRemote) AllTagPairs

func (db *DropboxRemote) AllTagPairs(oldPairs types.TagPairs) (types.TagPairs, error)

func (*DropboxRemote) DeleteRows

func (db *DropboxRemote) DeleteRows(randTags cryptag.RandomTags) error

func (*DropboxRemote) GetTagCursor

func (db *DropboxRemote) GetTagCursor() string

GetTagCursor gets the cursor for the remote tags directory used for incremental TagPair fetching.

func (*DropboxRemote) Key

func (db *DropboxRemote) Key() *[32]byte

func (*DropboxRemote) ListRows

func (db *DropboxRemote) ListRows(randtags cryptag.RandomTags) (types.Rows, error)

func (*DropboxRemote) Name

func (db *DropboxRemote) Name() string

func (*DropboxRemote) RowsFromRandomTags

func (db *DropboxRemote) RowsFromRandomTags(randtags cryptag.RandomTags) (types.Rows, error)

func (*DropboxRemote) SaveRow

func (db *DropboxRemote) SaveRow(row *types.Row) error

func (*DropboxRemote) SaveTagPair

func (db *DropboxRemote) SaveTagPair(pair *types.TagPair) error

func (*DropboxRemote) SetHTTPClient

func (db *DropboxRemote) SetHTTPClient(c *http.Client)

SetHTTPClient sets the underlying HTTP client used. Probably most useful for using a custom client that does proxied requests, perhaps through Tor.

func (*DropboxRemote) SetTagCursor

func (db *DropboxRemote) SetTagCursor(cursor string)

SetTagCursor sets the cursor for the remote tags directory incremental TagPair fetching so it can be used for incremental TagPair fetching.

func (*DropboxRemote) TagPairsFromRandomTags

func (db *DropboxRemote) TagPairsFromRandomTags(randtags cryptag.RandomTags) (types.TagPairs, error)

func (*DropboxRemote) ToConfig

func (db *DropboxRemote) ToConfig() (*Config, error)

ToConfig converts db to a Backend Config. If db.key is nil, returns error.

func (*DropboxRemote) UseTor

func (db *DropboxRemote) UseTor() error

UseTor sets db's HTTP client to one that uses Tor.

type FileSystem

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

func LoadOrCreateDefaultFileSystemBackend

func LoadOrCreateDefaultFileSystemBackend(backendPath, backendName string) (*FileSystem, error)

LoadOrCreateDefaultFileSystemBackend calls LoadOrCreateFileSystem then, if the returned FileSystem was just created anew, makes it the default Backend.

func LoadOrCreateFileSystem

func LoadOrCreateFileSystem(backendPath, backendName string) (*FileSystem, error)

func NewFileSystem

func NewFileSystem(conf *Config) (*FileSystem, error)

func (*FileSystem) AllTagPairs

func (fs *FileSystem) AllTagPairs(oldPairs types.TagPairs) (types.TagPairs, error)

func (*FileSystem) DeleteRows

func (fs *FileSystem) DeleteRows(randTags cryptag.RandomTags) error

func (*FileSystem) Key

func (fs *FileSystem) Key() *[32]byte

func (*FileSystem) ListRows

func (fs *FileSystem) ListRows(randtags cryptag.RandomTags) (types.Rows, error)

func (*FileSystem) Name

func (fs *FileSystem) Name() string

func (*FileSystem) RowsFromRandomTags

func (fs *FileSystem) RowsFromRandomTags(randtags cryptag.RandomTags) (types.Rows, error)

func (*FileSystem) SaveRow

func (fs *FileSystem) SaveRow(row *types.Row) error

func (*FileSystem) SaveTagPair

func (fs *FileSystem) SaveTagPair(pair *types.TagPair) error

func (*FileSystem) TagPairsFromRandomTags

func (fs *FileSystem) TagPairsFromRandomTags(randtags cryptag.RandomTags) (types.TagPairs, error)

func (*FileSystem) ToConfig

func (fs *FileSystem) ToConfig() (*Config, error)

type WebserverBackend

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

func CreateSandstormWebserver

func CreateSandstormWebserver(key []byte, bkName, webkey string) (*WebserverBackend, error)

func CreateWebserver

func CreateWebserver(key []byte, backendName, baseURL, authToken string) (*WebserverBackend, error)

CreateWebserver is a high-level function that creates a new WebserverBackend and saves its config to disk. (Useful for config init and more.)

func LoadWebserverBackend

func LoadWebserverBackend(backendPath, backendName string) (*WebserverBackend, error)

func NewWebserverBackend

func NewWebserverBackend(key []byte, serverName, serverBaseUrl, authToken string) (*WebserverBackend, error)

func SandstormFromConfig

func SandstormFromConfig(cfg *Config) (*WebserverBackend, error)

func WebserverFromConfig

func WebserverFromConfig(conf *Config) (*WebserverBackend, error)

func (*WebserverBackend) AllTagPairs

func (wb *WebserverBackend) AllTagPairs(oldPairs types.TagPairs) (types.TagPairs, error)

func (*WebserverBackend) DeleteRows

func (wb *WebserverBackend) DeleteRows(randtags cryptag.RandomTags) error

func (*WebserverBackend) Key

func (wb *WebserverBackend) Key() *[32]byte

func (*WebserverBackend) ListRows

func (wb *WebserverBackend) ListRows(randtags cryptag.RandomTags) (types.Rows, error)

func (*WebserverBackend) Name

func (wb *WebserverBackend) Name() string

func (*WebserverBackend) RowsFromRandomTags

func (wb *WebserverBackend) RowsFromRandomTags(randtags cryptag.RandomTags) (types.Rows, error)

func (*WebserverBackend) SaveRow

func (wb *WebserverBackend) SaveRow(row *types.Row) error

func (*WebserverBackend) SaveTagPair

func (wb *WebserverBackend) SaveTagPair(pair *types.TagPair) error

func (*WebserverBackend) SetHTTPClient

func (wb *WebserverBackend) SetHTTPClient(client *http.Client)

SetHTTPClient sets the underlying HTTP client used. Useful for using a custom client that does proxied requests, perhaps through Tor.

func (*WebserverBackend) TagPairsFromRandomTags

func (wb *WebserverBackend) TagPairsFromRandomTags(randtags cryptag.RandomTags) (types.TagPairs, error)

func (*WebserverBackend) ToConfig

func (wb *WebserverBackend) ToConfig() (*Config, error)

func (*WebserverBackend) UseTor

func (wb *WebserverBackend) UseTor() error

UseTor sets wb's HTTP client to one that uses Tor and records that Tor should be used.

type WebserverConfig

type WebserverConfig struct {
	AuthToken string
	BaseURL   string
}

func WebserverConfigFromMap

func WebserverConfigFromMap(m map[string]interface{}) (WebserverConfig, error)

func (*WebserverConfig) Valid

func (wc *WebserverConfig) Valid() error

Jump to

Keyboard shortcuts

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