firewalldb

package
v0.12.4-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DBFilename is the default filename of the rules' database.
	DBFilename = "rules.db"

	// DefaultRulesDBTimeout is the default maximum time we wait for the
	// db bbolt database to be opened. If the database is already
	// opened by another process, the unique lock cannot be obtained. With
	// the timeout we error out after the given time instead of just
	// blocking for forever.
	DefaultRulesDBTimeout = 5 * time.Second
)
View Source
const Subsystem = "FWDB"

Variables

View Source
var (

	// ErrDBReversion is returned when detecting an attempt to revert to a
	// prior database version.
	ErrDBReversion = errors.New("cannot revert to prior version")
)
View Source
var (

	// ErrNoSuchKeyFound is returned when there is no key-value pair found
	// for the given key.
	ErrNoSuchKeyFound = fmt.Errorf("no such key found")
)

Functions

func DecodeChannelPoint

func DecodeChannelPoint(cp string) (string, uint32, error)

func HideBytes

func HideBytes(tx PrivacyMapTx, realBytes []byte) ([]byte, error)

func HideChanPoint

func HideChanPoint(tx PrivacyMapTx, txid string, index uint32) (string,
	uint32, error)

func HideChanPointStr

func HideChanPointStr(tx PrivacyMapTx, cp string) (string, error)

func HideString

func HideString(tx PrivacyMapTx, real string) (string, error)

func HideUint64

func HideUint64(tx PrivacyMapTx, real uint64) (uint64, error)

func NewPseudoChanPoint

func NewPseudoChanPoint() (string, error)

func NewPseudoStr

func NewPseudoStr(n int) (string, error)

func NewPseudoUint32

func NewPseudoUint32() uint32

func NewPseudoUint64

func NewPseudoUint64() (uint64, string)

func RevealBytes

func RevealBytes(tx PrivacyMapTx, pseudoBytes []byte) ([]byte, error)

func RevealChanPoint

func RevealChanPoint(tx PrivacyMapTx, txid string, index uint32) (string,
	uint32, error)

func RevealString

func RevealString(tx PrivacyMapTx, pseudo string) (string, error)

func RevealUint64

func RevealUint64(tx PrivacyMapTx, pseudo uint64) (uint64, error)

func SerializeAction

func SerializeAction(w io.Writer, action *Action) error

SerializeAction binary serializes the given action to the writer using the tlv format.

func StrToUint64

func StrToUint64(s string) (uint64, error)

func Uint64ToStr

func Uint64ToStr(i uint64) string

func UseLogger

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

Types

type Action

type Action struct {
	// SessionID is the ID of the session that this action belongs to.
	// Note that this is not serialized on persistence since the action is
	// already stored under a bucket identified by the session ID.
	SessionID session.ID

	// ActorName is the name of the entity who performed the Action.
	ActorName string

	// FeatureName is the name of the feature that the Action is being
	// performed by.
	FeatureName string

	// Trigger is the meta info detailing what caused this action to be
	// executed.
	Trigger string

	// Intent is the meta info detailing what the intended outcome of this
	// action will be.
	Intent string

	// StructuredJsonData is extra, structured, info that the Autopilot can
	// send to Litd serialised as a json string.
	StructuredJsonData string

	// RPCMethod is the URI that was called.
	RPCMethod string

	// RPCParams is the method parameters of the request in JSON form.
	RPCParamsJson []byte

	// AttemptedAt is the time at which this action was created.
	AttemptedAt time.Time

	// State represents the state of the Action.
	State ActionState

	// ErrorReason is the human-readable reason for why the action failed.
	// It will only be set if State is ActionStateError.
	ErrorReason string
}

Action represents an RPC call made through the firewall.

func DeserializeAction

func DeserializeAction(r io.Reader, sessionID session.ID) (*Action, error)

DeserializeAction deserializes an action from the given reader, expecting the data to be encoded in the tlv format.

type ActionLocator

type ActionLocator struct {
	SessionID session.ID
	ActionID  uint64
}

ActionLocator helps us find an action in the database.

type ActionReadDBGetter

type ActionReadDBGetter interface {
	GetActionsReadDB(groupID session.ID, featureName string) ActionsReadDB
}

ActionReadDBGetter represents a function that can be used to construct an ActionsReadDB.

type ActionState

type ActionState uint8

ActionState represents the state of an action.

const (
	// ActionStateUnknown means that the action's state was never
	// initialised. This should never be the case.
	ActionStateUnknown ActionState = 0

	// ActionStateInit represents that an Action has been created but that
	// is still in the pending state.
	ActionStateInit ActionState = 1

	// ActionStateDone represents that an Action has been executed
	// successfully.
	ActionStateDone ActionState = 2

	// ActionStateError represents that an Action did not complete
	// successfully.
	ActionStateError ActionState = 3
)

type ActionsDB

type ActionsDB interface {
	// ListActions returns a  list of past Action items.
	ListActions(ctx context.Context) ([]*RuleAction, error)
}

ActionsDB represents a DB backend that contains Action entries that can be queried. It allows us to abstract away the details of the data storage method.

type ActionsReadDB

type ActionsReadDB interface {
	GroupActionsDB() ActionsDB
	GroupFeatureActionsDB() ActionsDB
}

ActionsReadDB is an abstraction gives a caller access to either a group specific or group and feature specific rules.ActionDB.

type ActionsWriteDB

type ActionsWriteDB interface {
	AddAction(sessionID session.ID, action *Action) (uint64, error)
	SetActionState(al *ActionLocator, state ActionState,
		errReason string) error
}

ActionsWriteDB is an abstraction over the Actions DB that will allow a caller to add new actions as well as change the values of an existing action.

type DB

type DB struct {
	*bbolt.DB
	// contains filtered or unexported fields
}

DB is a bolt-backed persistent store.

func NewDB

func NewDB(dir, fileName string, sessionIDIndex session.IDToGroupIndex) (*DB,
	error)

NewDB creates a new bolt database that can be found at the given directory.

func (*DB) AddAction

func (db *DB) AddAction(sessionID session.ID, action *Action) (uint64, error)

AddAction serialises and adds an Action to the DB under the given sessionID.

func (*DB) GetActionsReadDB

func (db *DB) GetActionsReadDB(groupID session.ID,
	featureName string) ActionsReadDB

GetActionsReadDB is a method on DB that constructs an ActionsReadDB.

func (*DB) GetKVStores

func (db *DB) GetKVStores(rule string, groupID session.ID,
	feature string) KVStores

GetKVStores constructs a new rules.KVStores backed by a bbolt db.

func (*DB) ListActions

func (db *DB) ListActions(filterFn ListActionsFilterFn,
	query *ListActionsQuery) ([]*Action, uint64, uint64, error)

ListActions returns a list of Actions that pass the filterFn requirements. The indexOffset and maxNum params can be used to control the number of actions returned. The return values are the list of actions, the last index and the total count (iff query.CountTotal is set).

func (*DB) ListGroupActions

func (db *DB) ListGroupActions(groupID session.ID,
	filterFn ListActionsFilterFn) ([]*Action, error)

ListGroupActions returns a list of the given session group's Actions that pass the filterFn requirements.

TODO: update to allow for pagination.

func (*DB) ListSessionActions

func (db *DB) ListSessionActions(sessionID session.ID,
	filterFn ListActionsFilterFn, query *ListActionsQuery) ([]*Action,
	uint64, uint64, error)

ListSessionActions returns a list of the given session's Actions that pass the filterFn requirements.

func (*DB) PrivacyDB

func (db *DB) PrivacyDB(groupID session.ID) PrivacyMapDB

PrivacyDB constructs a PrivacyMapDB that will be indexed under the given group ID key.

func (*DB) SetActionState

func (db *DB) SetActionState(al *ActionLocator, state ActionState,
	errorReason string) error

SetActionState finds the action specified by the ActionLocator and sets its state to the given state.

type KVStore

type KVStore interface {
	// Get fetches the value under the given key from the underlying kv
	// store. If no value is found, nil is returned.
	Get(ctx context.Context, key string) ([]byte, error)

	// Set sets the given key-value pair in the underlying kv store.
	Set(ctx context.Context, key string, value []byte) error

	// Del deletes the value under the given key in the underlying kv store.
	Del(ctx context.Context, key string) error
}

KVStore is in interface representing a key value store. It allows us to abstract away the details of the data storage method.

type KVStoreTx

type KVStoreTx interface {
	// Global returns a persisted global, rule-name indexed, kv store. A
	// rule with a given name will have access to this store independent of
	// group ID or feature.
	Global() KVStore

	// Local returns a persisted local kv store for the rule. Depending on
	// how the implementation is initialised, this will either be under the
	// group ID namespace or the group ID _and_ feature name namespace.
	Local() KVStore

	// GlobalTemp is similar to the Global store except that its contents
	// is cleared upon restart of the database.
	GlobalTemp() KVStore

	// LocalTemp is similar to the Local store except that its contents is
	// cleared upon restart of the database.
	LocalTemp() KVStore
}

KVStoreTx represents a database transaction that can be used for both read and writes of the various different key value stores offered for the rule.

type KVStores

type KVStores interface {
	// Update opens a database read/write transaction and executes the
	// function f with the transaction passed as a parameter. After f exits,
	// if f did not error, the transaction is committed. Otherwise, if f did
	// error, the transaction is rolled back. If the rollback fails, the
	// original error returned by f is still returned. If the commit fails,
	// the commit error is returned.
	Update(f func(tx KVStoreTx) error) error

	// View opens a database read transaction and executes the function f
	// with the transaction passed as a parameter. After f exits, the
	// transaction is rolled back. If f errors, its error is returned, not a
	// rollback error (if any occur).
	View(f func(tx KVStoreTx) error) error
}

KVStores provides an Update and View method that will allow the caller to perform atomic read and write transactions on and of the key value stores offered the KVStoreTx.

type ListActionsFilterFn

type ListActionsFilterFn func(a *Action, reversed bool) (bool, bool)

ListActionsFilterFn defines a function that can be used to determine if an action should be included in a set of results or not. The reversed parameter indicates if the actions are being traversed in reverse order or not. The first return boolean indicates if the action should be included or not and the second one indicates if the iteration should be stopped or not.

type ListActionsQuery

type ListActionsQuery struct {
	// IndexOffset is index of the action to inspect.
	IndexOffset uint64

	// MaxNum is the maximum number of actions to return. If it is set to 0,
	// then no maximum is enforced.
	MaxNum uint64

	// Reversed indicates whether the actions should be returned in reverse
	// order.
	Reversed bool

	// CountAll should be set to true if the total number of actions that
	// satisfy the query should be counted and returned. Note that this will
	// make the query slower.
	CountAll bool
}

ListActionsQuery can be used to tweak the query to ListActions and ListSessionActions.

type NewPrivacyMapDB

type NewPrivacyMapDB func(groupID session.ID) PrivacyMapDB

NewPrivacyMapDB is a function type that takes a group ID and uses it to construct a new PrivacyMapDB.

type PrivacyMapDB

type PrivacyMapDB interface {
	// Update opens a database read/write transaction and executes the
	// function f with the transaction passed as a parameter. After f exits,
	// if f did not error, the transaction is committed. Otherwise, if f did
	// error, the transaction is rolled back. If the rollback fails, the
	// original error returned by f is still returned. If the commit fails,
	// the commit error is returned.
	Update(f func(tx PrivacyMapTx) error) error

	// View opens a database read transaction and executes the function f
	// with the transaction passed as a parameter. After f exits, the
	// transaction is rolled back. If f errors, its error is returned, not a
	// rollback error (if any occur).
	View(f func(tx PrivacyMapTx) error) error
}

PrivacyMapDB provides an Update and View method that will allow the caller to perform atomic read and write transactions defined by PrivacyMapTx on the underlying DB.

type PrivacyMapPairs

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

PrivacyMapPairs is an in memory implementation of the PrivacyMapReader.

func NewPrivacyMapPairs

func NewPrivacyMapPairs(m map[string]string) *PrivacyMapPairs

NewPrivacyMapPairs constructs a new PrivacyMapPairs struct. It may be initialised with either a nil map or a pre-defined real-to-pseudo strings map.

func (*PrivacyMapPairs) Add

func (p *PrivacyMapPairs) Add(pairs map[string]string) error

Add adds the passed set of real-to-pseudo pairs to the PrivacyMapPairs structure. It will throw an error if the new pairs conflict with any of the existing pairs.

func (*PrivacyMapPairs) GetPseudo

func (p *PrivacyMapPairs) GetPseudo(real string) (string, bool)

GetPseudo returns the associated pseudo value for a given real value. If no such real value exists in the DB, then false is returned.

NOTE: this is part of the PrivacyMapReader interface.

type PrivacyMapReader

type PrivacyMapReader interface {
	// GetPseudo returns the associated pseudo value for a given real value.
	// If no such real value exists in the DB, then false is returned.
	GetPseudo(real string) (string, bool)
}

PrivacyMapReader is an interface that gives read access to a privacy map DB.

type PrivacyMapTx

type PrivacyMapTx interface {
	// NewPair persists a new real-pseudo pair.
	NewPair(real, pseudo string) error

	// PseudoToReal returns the real value associated with the given pseudo
	// value. If no such pair is found, then ErrNoSuchKeyFound is returned.
	PseudoToReal(pseudo string) (string, error)

	// RealToPseudo returns the pseudo value associated with the given real
	// value. If no such pair is found, then ErrNoSuchKeyFound is returned.
	RealToPseudo(real string) (string, error)

	// FetchAllPairs loads and returns the real-to-pseudo pairs in the form
	// of a PrivacyMapPairs struct.
	FetchAllPairs() (*PrivacyMapPairs, error)
}

PrivacyMapTx represents a db that can be used to create, store and fetch real-pseudo pairs.

type RuleAction

type RuleAction struct {
	// Method is the URI of the rpc method that was called.
	Method string

	// PerformedAt is the time at which the action was attempted.
	PerformedAt time.Time
}

RuleAction represents a method call that was performed at a certain time at a certain time.

type RulesDB

type RulesDB interface {
	GetKVStores(rule string, groupID session.ID, feature string) KVStores
}

RulesDB can be used to initialise a new rules.KVStores.

Jump to

Keyboard shortcuts

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