common

package
v0.0.0-...-982e07a Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package common contains all query handlers for AcraCensor: blacklist handler, which allows everything and forbids specific query/pattern/table; whitelist handler, which allows query/pattern/table and restricts/forbids everything else; ignore handler, which allows to ignore any query; and querycapture module that logs every unique query to the QueryCapture log.

https://github.com/cossacklabs/acra/wiki/AcraCensor

Index

Constants

View Source
const (
	// UnionPlaceholder is used when matching %%UNION%% pattern
	UnionPlaceholder = "%%UNION%%"
	// UnionReplacer is used when matching %%UNION%% pattern
	UnionReplacer = "SELECT 254775710223443243272234290 UNION SELECT 486264166657867240626457666"
	// SelectPlaceholder is used when matching %%SELECT%% pattern
	SelectPlaceholder = "%%SELECT%%"
	// SelectReplacer is used when matching %%SELECT%% pattern
	SelectReplacer = "SELECT 253768160274445518137315681"
	// InsertPlaceholder is used when matching %%INSERT%% pattern
	InsertPlaceholder = "%%INSERT%%"
	// InsertReplacer is used when matching %%INSERT%% pattern
	InsertReplacer = "INSERT INTO table_150624360841713829746677497 (column_454716724) VALUES (value_151516596)"
	// UpdatePlaceholder is used when matching %%UPDATE%% pattern
	UpdatePlaceholder = "%%UPDATE%%"
	// UpdateReplacer is used when matching %%UPDATE%% pattern
	UpdateReplacer = "UPDATE table_795749362101944825892661393 SET column_148943040 = 577742781 WHERE row_788570922 = 840343494"
	// DeletePlaceholder is used when matching %%DELETE%% pattern
	DeletePlaceholder = "%%DELETE%%"
	// DeleteReplacer is used when matching %%DELETE%% pattern
	DeleteReplacer = "DELETE FROM table_359557854899217835429634591"
	// BeginPlaceholder is used when matching BEGIN pattern
	BeginPlaceholder = "%%BEGIN%%"
	// BeginReplacer is used when matching BEGIN pattern
	BeginReplacer = "BEGIN"
	// CommitPlaceholder is used when matching COMMIT pattern
	CommitPlaceholder = "%%COMMIT%%"
	// CommitReplacer is used when matching COMMIT pattern
	CommitReplacer = "COMMIT"
	// RollbackPlaceholder is used when matching ROLLBACK pattern
	RollbackPlaceholder = "%%ROLLBACK%%"
	// RollbackReplacer is used when matching ROLLBACK pattern
	RollbackReplacer = "ROLLBACK"

	// WherePlaceholder is used when matching %%WHERE%% pattern
	WherePlaceholder = "%%WHERE%%"
	// WhereReplacer is used when matching %%WHERE%% pattern
	WhereReplacer = "where value = where_651453831047102383248696721"
	// ValuePlaceholder is used when matching %%VALUE%% pattern
	ValuePlaceholder = "%%VALUE%%"
	// ValueReplacer is used when matching %%VALUE%% pattern
	ValueReplacer = "'value_877452131373673274532373116'"
	// SubqueryPlaceholder is used when matching %%SUBQUERY%% pattern
	SubqueryPlaceholder = "%%SUBQUERY%%"
	// SubqueryReplacer is used when matching %%SUBQUERY%% pattern
	SubqueryReplacer = "SELECT 'subquery_820753242875385807714016705'"
	// ListOfValuesPlaceholder is used when matching %%LIST_OF_VALUES%% pattern
	ListOfValuesPlaceholder = "%%LIST_OF_VALUES%%"
	// ListOfValuesReplacer is used when matching %%LIST_OF_VALUES%% pattern
	ListOfValuesReplacer = "'list_of_values_980254824737236160411017007'"
	// ColumnPlaceholder is used when matching %%COLUMN%% pattern
	ColumnPlaceholder = "%%COLUMN%%"
	// ColumnReplacer is used when matching %%COLUMN%% pattern
	ColumnReplacer = "column_443112402399486586659464580"
)
View Source
const LogQueryLength = 100

LogQueryLength is maximum query length for logging to syslog.

Variables

View Source
var (
	ErrDenyByQueryError                = errors.New("deny by query")
	ErrDenyByTableError                = errors.New("deny by table")
	ErrDenyByPatternError              = errors.New("deny by pattern")
	ErrPatternSyntaxError              = errors.New("fail to parse specified pattern")
	ErrPatternCheckError               = errors.New("failed to check specified pattern match")
	ErrCantReadQueriesFromStorageError = errors.New("can't read queries from storage")
	ErrUnexpectedTypeError             = errors.New("should never appear")
	ErrDenyAllError                    = errors.New("deny all queries error")
	ErrCensorConfigurationError        = errors.New("configuration error")
)

Errors returned by censor

View Source
var ColumnPatternStatement = sqlparser.NewColIdent(ColumnReplacer)

ColumnPatternStatement is used while comparison with %%COLUMN%% pattern

View Source
var DefaultSerializationTimeout = defaultSerializationTimeout

DefaultSerializationTimeout defines a default ticker' timeout

View Source
var DefaultWriteQueryChannelSize = defaultWriteQueryChannelSize

DefaultWriteQueryChannelSize defines size of channel used for writing input queries

View Source
var DeletePatternStatement, _ = sqlParser.Parse(DeleteReplacer)

DeletePatternStatement is used while comparison with %%DELETE%% pattern

View Source
var InsertPatternStatement, _ = sqlParser.Parse(InsertReplacer)

InsertPatternStatement is used while comparison with %%INSERT%% pattern

View Source
var ListOfValuePatternStatement = sqlparser.NewStrVal([]byte(ListOfValuesReplacer[1:43]))

ListOfValuePatternStatement is used while comparison with %%LIST_OF_VALUES%% pattern replacer is used without quotes

View Source
var SelectPatternStatement, _ = sqlParser.Parse(SelectReplacer)

SelectPatternStatement is used while comparison with %%SELECT%% pattern

View Source
var SubqueryPatternStatement, _ = sqlParser.Parse(SubqueryReplacer)

SubqueryPatternStatement is used while comparison with %%SUBQUERY%% pattern

View Source
var UnionPatternStatement, _ = sqlParser.Parse(UnionReplacer)

UnionPatternStatement is used while comparison with %%UNION%% pattern

View Source
var UpdatePatternStatement, _ = sqlParser.Parse(UpdateReplacer)

UpdatePatternStatement is used while comparison with %%UPDATE%% pattern

View Source
var ValuePatternStatement = sqlparser.NewStrVal([]byte(ValueReplacer[1:34]))

ValuePatternStatement is used while comparison with %%VALUE%% pattern replacer is used without quotes

View Source
var WherePatternStatement, _ = sqlParser.Parse("SELECT * FROM table_883909268 " + WhereReplacer)

WherePatternStatement is used while comparison with %%WHERE%% pattern

Functions

func CheckExactQueriesMatch

func CheckExactQueriesMatch(normalizedQuery string, setOfQueries map[string]bool) bool

CheckExactQueriesMatch evaluates if query presents in set of queries

func CheckPatternsMatching

func CheckPatternsMatching(patterns []sqlparser.Statement, parsedQuery sqlparser.Statement) bool

CheckPatternsMatching evaluates if parsed query matches specified set of patterns

func CheckTableNamesMatch

func CheckTableNamesMatch(parsedQuery sqlparser.Statement, setOfTables map[string]bool) (bool, bool)

CheckTableNamesMatch evaluates if query contains table presented in specified set of tables

func ParsePatterns

func ParsePatterns(rawPatterns []string, parser *sqlparser.Parser) ([]sqlparser.Statement, error)

ParsePatterns replace placeholders with our values which used to match patterns and parse them with sqlparser

func TrimStringToN

func TrimStringToN(query string, n int) string

TrimStringToN trims query to N chars.

Types

type FileLogStorage

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

FileLogStorage is a file-based implementation of LogStorage interface

func NewFileLogStorage

func NewFileLogStorage(filePath string) (*FileLogStorage, error)

NewFileLogStorage is a constructor for FileLogStorage

func (*FileLogStorage) Append

func (storage *FileLogStorage) Append(p []byte) error

Append appends raw queries to the end of internal file

func (*FileLogStorage) Close

func (storage *FileLogStorage) Close() error

Close simply closes internal file

func (*FileLogStorage) ReadAll

func (storage *FileLogStorage) ReadAll() ([]byte, error)

ReadAll returns stored queries in raw form from internal file

func (*FileLogStorage) WriteAll

func (storage *FileLogStorage) WriteAll(p []byte) error

WriteAll writes raw queries that need to be stored to internal file

type LogStorage

type LogStorage interface {
	io.Closer
	ReadAll() ([]byte, error)
	WriteAll([]byte) error
	Append([]byte) error
}

LogStorage defines basic storage that should be used by QueryWriter

type QueryInfo

type QueryInfo struct {
	RawQuery string `json:"raw_query"`
}

QueryInfo defines format of exporting query into file

type QueryWriter

type QueryWriter struct {
	Queries []*QueryInfo
	// contains filtered or unexported fields
}

QueryWriter is a mechanism that provides dumping input queries in background. It can be used as separate component or as one of censor's handlers

func NewFileQueryWriter

func NewFileQueryWriter(filePath string) (*QueryWriter, error)

NewFileQueryWriter creates QueryWriter instance

func (*QueryWriter) DumpQueries

func (queryWriter *QueryWriter) DumpQueries() error

DumpQueries writes all queries into file

func (*QueryWriter) Free

func (queryWriter *QueryWriter) Free()

Free dumps all Captured queries to file, and stops background processing. QueryWriter mustn't be used after that

func (*QueryWriter) GetQueries

func (queryWriter *QueryWriter) GetQueries() []*QueryInfo

GetQueries return cached queries

func (*QueryWriter) Start

func (queryWriter *QueryWriter) Start()

Start starts background logging of input queries. Should be called in separate goroutine

func (*QueryWriter) WalkQueries

func (queryWriter *QueryWriter) WalkQueries(visitor func(query *QueryInfo) error) error

WalkQueries walks through each query and perform some action on it

func (*QueryWriter) WriteQuery

func (queryWriter *QueryWriter) WriteQuery(query string)

WriteQuery writes input query to captured queries list

Jump to

Keyboard shortcuts

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