middleware

package
v0.0.0-...-028b8ad Latest Latest
Warning

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

Go to latest
Published: May 16, 2019 License: MIT Imports: 16 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PolicyAwareHandler

func PolicyAwareHandler(policy ComputationPolicy) http.HandlerFunc

PolicyAwareHandler returns a http.Handler based on the passed ComputationPolicy. It also performs some basic logging of requests received.

Types

type ColumnTransform

type ColumnTransform func(interface{}) (transformedValue interface{}, excludeRow bool, err error)

type ComputationLevel

type ComputationLevel int

ComputationLevel specifies whether a handler for a http request can compute no globalResult, just provide the raw data or compute a full globalResult

const (
	NoComputation ComputationLevel = iota
	RawData       ComputationLevel = iota
	CanCompute    ComputationLevel = iota
)

func ComputationLevelFromString

func ComputationLevelFromString(level string) (ComputationLevel, error)

ComputationLevelFromString converts a string to the relevant ComputationLevel

func (ComputationLevel) ToString

func (c ComputationLevel) ToString() string

ToString converts from a ComputationLevel to the relevant string

type ComputationPolicy

type ComputationPolicy interface {
	Register(string, ComputationLevel, http.Handler)
	UnregisterAll(string)
	UnregisterOne(string, ComputationLevel)
	Resolve(string, ProcessingLocation) (ComputationLevel, http.Handler)
}

ComputationPolicy stores the computation capabilities of a node

type DataPolicy

type DataPolicy interface {
	// resolve takes an identifier for an entity and returns the TableOperatoinsg for the entity
	Resolve(string) (*TableOperations, error)
	LastUpdated() time.Time
}

DataPolicy allow us to get a function which must be applied to data before returning for a given identifier

type DataTransforms

type DataTransforms map[*PrivacyGroup]*TableOperations

transforms is a map from privacyGroups to TableOperations

type DynamicComputationPolicy

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

DynamicComputationPolicy holds a set of computation capabilities for paths, these must be set manually

func NewDynamicComputationPolicy

func NewDynamicComputationPolicy() *DynamicComputationPolicy

NewDynamicComputationPolicy returns a pointer to a DynamicComputationPolicy with an empty, initialised internal map

func (*DynamicComputationPolicy) Activate

func (p *DynamicComputationPolicy) Activate(path string, level ComputationLevel) error

Activate marks a handler for a specific request path and computation level as active and hence it will appear as registered

func (*DynamicComputationPolicy) Deactivate

func (p *DynamicComputationPolicy) Deactivate(path string, level ComputationLevel) error

Deactivate marks the handler for a specific request path and computation level as deactivated which means it will appear not be registered but can easily be re-activated with a call to Activate

func (*DynamicComputationPolicy) Register

func (p *DynamicComputationPolicy) Register(path string, level ComputationLevel, handler http.Handler)

Register adds a capability for a path at a specific ComputationLevel

func (*DynamicComputationPolicy) Resolve

func (p *DynamicComputationPolicy) Resolve(path string, preferredLocation ProcessingLocation) (ComputationLevel, http.Handler)

Resolve takes a path and preferred processing location and returns a handler and the computation level which that handler provides. It does this based on the capabilities for this path registered with the StaticComputationPolicy. The preferred processing location is used to break ties when we can offer full computation and raw data.

func (*DynamicComputationPolicy) UnregisterAll

func (p *DynamicComputationPolicy) UnregisterAll(path string)

UnregisterAll removes all capabilities for a path

func (*DynamicComputationPolicy) UnregisterOne

func (p *DynamicComputationPolicy) UnregisterOne(path string, level ComputationLevel)

UnregisterOne removes a capability for a path at a specific computation level

type MySQLPrivateDatabase

type MySQLPrivateDatabase struct {
	DataPolicy  DataPolicy
	CacheTables bool
	// contains filtered or unexported fields
}

MySQLPrivateDatabase is a wrapper around a MySQL database which implements the PrivateRelationalDatabase interface, it supports DataPolicies which specify transforms for columns and excluded columns on a per PrivacyGroup basis

func (*MySQLPrivateDatabase) Close

func (mspd *MySQLPrivateDatabase) Close() error

Close closes the connection to the MySQL database

func (*MySQLPrivateDatabase) Connect

func (mspd *MySQLPrivateDatabase) Connect(user, password, databaseName, uri string, port int) error

Connect opens the connection to the MySQL database

func (*MySQLPrivateDatabase) Exec

func (mspd *MySQLPrivateDatabase) Exec(query string, requestPolicy *RequestPolicy, args ...interface{}) (sql.Result, error)

Exec takes a query string and a RequestPolicy and resolves the DataPolicy from the MySQLPrivateDatabase with the request policy to give a globalResult to the query on transformed versions of the actual database tables

func (*MySQLPrivateDatabase) ExecContext

func (mspd *MySQLPrivateDatabase) ExecContext(ctx context.Context, query string, requestPolicy *RequestPolicy, args ...interface{}) (sql.Result, error)

ExecContext takes a query string and a RequestPolicy and resolves the DataPolicy from the MySQLPrivateDatabase with the request policy to give a globalResult to the query on transformed versions of the actual database tables

func (*MySQLPrivateDatabase) Ping

func (mspd *MySQLPrivateDatabase) Ping() error

Ping verifies a connection to the database is still alive, establishing a connection if necessary.

func (*MySQLPrivateDatabase) PingContext

func (mspd *MySQLPrivateDatabase) PingContext(ctx context.Context) error

PingContext verifies a connection to the database is still alive, establishing a connection if necessary.

func (*MySQLPrivateDatabase) Query

func (mspd *MySQLPrivateDatabase) Query(query string, requestPolicy *RequestPolicy, args ...interface{}) (*sql.Rows, error)

Query takes a query string and a RequestPolicy and resolves the DataPolicy from the MySQLPrivateDatabase with the request policy to give a globalResult to the query on transformed versions of the actual database tables

func (*MySQLPrivateDatabase) QueryContext

func (mspd *MySQLPrivateDatabase) QueryContext(ctx context.Context, query string, requestPolicy *RequestPolicy, args ...interface{}) (*sql.Rows, error)

QueryContext takes a query string and a RequestPolicy and resolves the DataPolicy from the MySQLPrivateDatabase with the request policy to give a globalResult to the query on transformed versions of the actual database tables

func (*MySQLPrivateDatabase) QueryRow

func (mspd *MySQLPrivateDatabase) QueryRow(query string, requestPolicy *RequestPolicy, args ...interface{}) (*sql.Row, error)

QueryRow takes a query string and a RequestPolicy and resolves the DataPolicy from the MySQLPrivateDatabase with the // request policy to give a globalResult to the query on transformed versions of the actual database tables

func (*MySQLPrivateDatabase) QueryRowContext

func (mspd *MySQLPrivateDatabase) QueryRowContext(ctx context.Context, query string, requestPolicy *RequestPolicy, args ...interface{}) (*sql.Row, error)

QueryRowContext takes a query string and a RequestPolicy and resolves the DataPolicy from the MySQLPrivateDatabase with the request policy to give a globalResult to the query on transformed versions of the actual database tables

func (*MySQLPrivateDatabase) SetConnMaxLifetime

func (mspd *MySQLPrivateDatabase) SetConnMaxLifetime(d time.Duration)

SetConnMaxLifetime sets the maximum amount of time a connection may be reused.

Expired connections may be closed lazily before reuse.

If d <= 0, connections are reused forever.

func (*MySQLPrivateDatabase) SetMaxIdleConns

func (mspd *MySQLPrivateDatabase) SetMaxIdleConns(n int)

SetMaxIdleConns sets the maximum number of connections in the idle connection pool.

If MaxOpenConns is greater than 0 but less than the new MaxIdleConns, then the new MaxIdleConns will be reduced to match the MaxOpenConns limit.

If n <= 0, no idle connections are retained.

The default max idle connections is currently 2. This may change in a future release.

func (*MySQLPrivateDatabase) SetMaxOpenConns

func (mspd *MySQLPrivateDatabase) SetMaxOpenConns(n int)

SetMaxOpenConns sets the maximum number of open connections to the database.

If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than MaxIdleConns, then MaxIdleConns will be reduced to match the new MaxOpenConns limit.

If n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited).

func (*MySQLPrivateDatabase) Stats

func (mspd *MySQLPrivateDatabase) Stats() sql.DBStats

Stats returns database statistics

type PamRequest

type PamRequest struct {
	Policy      *RequestPolicy
	HttpRequest *http.Request
}

PamRequest contains a RequestPolicy and a http request

func BuildPamRequest

func BuildPamRequest(req *http.Request) (PamRequest, error)

BuildPamRequest takes a pointer to a http request and returns a PamRequest with policy values taken from the parameters of the passed request

func (*PamRequest) AddParam

func (r *PamRequest) AddParam(key, value string)

AddParam adds a parameter to the contained http request with the same semantics as adding to the URL.Values of the http request

func (*PamRequest) DelParam

func (r *PamRequest) DelParam(key string)

AddParam deletes a parameter from the contained http request with the same semantics as adding to the URL.Values of the http request

func (*PamRequest) GetParam

func (r *PamRequest) GetParam(key string) string

AddParam gets a parameter from the contained http request with the same semantics as adding to the URL.Values of the http request

func (*PamRequest) SetParam

func (r *PamRequest) SetParam(key, value string)

AddParam sets a parameter on the contained http request with the same semantics as adding to the URL.Values of the http request

type PamResponse

type PamResponse struct {
	ComputationLevel ComputationLevel
	HttpResponse     *http.Response
}

PamResponse contains a http response and its associated computation level

func BuildPamResponse

func BuildPamResponse(resp *http.Response) (PamResponse, error)

BuildPamResponse takes a pointer to a http response and returns a PamResponse with the ComputationLevel taken from the response header

type PolicyAwareClient

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

PolicyAwareClient wraps a http client with a ComputationPolicy

func MakePolicyAwareClient

func MakePolicyAwareClient(policy ComputationPolicy) PolicyAwareClient

MakePolicyAwareClient returns an PolicyAwareClient with initialised fields

func (PolicyAwareClient) Send

Send takes a PamRequest and martials the RequestPolicy into the http request parameters before sending it using the contained http client. If the ComputationPolicy has a local handler for the requested path, and the preferred location is local, and all of the data required for a globalResult is contained within the request then the request will instead be handled locally.

type PrivacyGroup

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

PrivacyGroup a struct which contain a data structure of RequesterID's which we can Add to and Remove from

func NewPrivacyGroup

func NewPrivacyGroup(name string) *PrivacyGroup

func (*PrivacyGroup) Add

func (pg *PrivacyGroup) Add(id string)

func (*PrivacyGroup) AddMany

func (pg *PrivacyGroup) AddMany(ids []string)

func (*PrivacyGroup) Name

func (pg *PrivacyGroup) Name() string

func (*PrivacyGroup) Remove

func (pg *PrivacyGroup) Remove(id string) error

type PrivateRelationalDatabase

type PrivateRelationalDatabase interface {
	Connect(user, password, databaseName, uri string, port int) error
	Close() error
	Query(query string, requestPolicy *RequestPolicy, args ...interface{}) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, requestPolicy *RequestPolicy, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, requestPolicy *RequestPolicy, args ...interface{}) (*sql.Row, error)
	QueryRowContext(ctx context.Context, query string, requestPolicy *RequestPolicy, args ...interface{}) (*sql.Row, error)
	Exec(ctx context.Context, query string, requestPolicy *RequestPolicy, args ...interface{}) (sql.Result, error)
	ExecContext(ctx context.Context, query string, requestPolicy *RequestPolicy, args ...interface{}) (sql.Result, error)
	Stats() sql.DBStats
	SetConnMaxLifetime(d time.Duration)
	SetMaxOpenConns(n int)
	SetMaxIdleConns(n int)
	Ping() error
	PingContext(ctx context.Context) error
}

PrivateRelationalDatabase wraps an SQL database and edits queries so that they operate over tables adjusted to match privacy policies

type ProcessingLocation

type ProcessingLocation string

ProcessingLocation refers to either local or remote computation over data

const (
	// Specified if the request should ideally be executed locally and never leave the device
	Local ProcessingLocation = "local"
	// Specified if the returned data would ideally be unprocessed
	Remote ProcessingLocation = "remote"
)

func ProcessingLocationFromString

func ProcessingLocationFromString(loc string) (ProcessingLocation, error)

ProcessingLocationFromString converts from a string to the relevant ProcessingLocation

func (ProcessingLocation) ToString

func (p ProcessingLocation) ToString() string

ToString coverts from a ProcessingLocation to the relevant string

type RequestPolicy

type RequestPolicy struct {
	RequesterID                 string
	PreferredProcessingLocation ProcessingLocation
	HasAllRequiredData          bool
}

RequestPolicy stores the preferred location for processing of a request (and the identity of the requester?)

func BuildRequestPolicy

func BuildRequestPolicy(req *http.Request) (*RequestPolicy, error)

BuildRequestPolicy takes a http request and extracts the values for a RequestPolicy from its parameters

func (*RequestPolicy) AddToParams

func (p *RequestPolicy) AddToParams(params *url.Values)

AddToParams adds each of its fields as a parameter in the passed Values struct

type StaticComputationPolicy

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

StaticComputationPolicy holds a map from http request paths to computation capabilities which dictate which handlers can be used for the request. A handler can be specified for returning a full globalResult (CanCompute) or just the raw data (RawData)

func NewStaticComputationPolicy

func NewStaticComputationPolicy() *StaticComputationPolicy

NewStaticComputationPolicy returns a pointer to an initialised StaticComputationPolicy

func (*StaticComputationPolicy) Register

func (p *StaticComputationPolicy) Register(path string, level ComputationLevel, handler http.Handler)

Register adds a capability for a path at a specific ComputationLevel

func (*StaticComputationPolicy) Resolve

func (p *StaticComputationPolicy) Resolve(path string, preferredLocation ProcessingLocation) (ComputationLevel, http.Handler)

Resolve takes a path and preferred processing location and returns a handler and the computation level which that handler provides. It does this based on the capabilities for this path registered with the StaticComputationPolicy. The preferred processing location is used to break ties when we can offer full computation and raw data.

func (*StaticComputationPolicy) UnregisterAll

func (p *StaticComputationPolicy) UnregisterAll(path string)

UnregisterAll removes all capabilities for a path

func (*StaticComputationPolicy) UnregisterOne

func (p *StaticComputationPolicy) UnregisterOne(path string, level ComputationLevel)

UnregisterOne removes a capability for a path at a specific computation level

type StaticDataPolicy

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

StaticDataPolicy implements the DataPolicy interface and contains a list of privacyGroups and DataTransforms for them

func NewStaticDataPolicy

func NewStaticDataPolicy(privacyGroups []*PrivacyGroup, transforms DataTransforms) *StaticDataPolicy

NewStaticDataPolicy returns a pointer to a StaticDataPolicy with initialised fields

func (StaticDataPolicy) LastUpdated

func (sdp StaticDataPolicy) LastUpdated() time.Time

func (*StaticDataPolicy) Resolve

func (sdp *StaticDataPolicy) Resolve(entityID string) (*TableOperations, error)

Resolve takes an entity ID and returns a pointer to the relevant TableOperations struct based on the privacyGroups that the entity ID is in and the associated transforms stored in the StaticDataPolicy

type TableOperations

type TableOperations struct {
	TableTransforms map[string]TableTransform
	ExcludedCols    map[string][]string
}

TableOperations contains functions to apply to tables before sending to an entity and columns to exclude

func NewTableOperations

func NewTableOperations() *TableOperations

NewTableOperations returns a pointer to a TableOperations struct with initialised fields

type TableTransform

type TableTransform map[string]ColumnTransform

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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