driver

package
v1.2112.0-pre4 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2021 License: MPL-2.0 Imports: 17 Imported by: 4

Documentation

Index

Constants

View Source
const (
	SQLTypeDML = "dml"
	SQLTypeDDL = "ddl"
)
View Source
const (
	DriverTypeMySQL      = "mysql"
	DriverTypePostgreSQL = "PostgreSQL"
)

Variables

View Source
var ErrNodesCountExceedOne = errors.New("after parse, nodes count exceed one")

Functions

func AllDrivers

func AllDrivers() []string

func AllRules

func AllRules() map[string][]*Rule

func InitPlugins

func InitPlugins(pluginDir string) error

InitPlugins init plugins at plugins directory. It should be called on host process.

func Register

func Register(name string, h handler, rs []*Rule)

Register like sql.Register.

Register makes a database driver available by the provided driver name. Driver's initialize handler and audit rules register by Register.

func ServePlugin

func ServePlugin(r Registerer, newDriver func(cfg *Config) Driver)

ServePlugin start plugin process service. It should be called on plugin process.

Types

type AuditResult

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

func NewInspectResults

func NewInspectResults() *AuditResult

func (*AuditResult) Add

func (rs *AuditResult) Add(level RuleLevel, message string, args ...interface{})

func (*AuditResult) Level

func (rs *AuditResult) Level() RuleLevel

Level find highest Level in result

func (*AuditResult) Message

func (rs *AuditResult) Message() string

type Config

type Config struct {
	DSN   *DSN
	Rules []*Rule
}

Config define the configuration for driver.

func NewConfig

func NewConfig(dsn *DSN, rules []*Rule) (*Config, error)

NewConfig return a config for driver.

1. dsn is nil, rules is not nil. Use drive to do Offline Audit. 2. dsn is not nil, rule is nil. Use drive to communicate with database only. 3. dsn is not nil, rule is not nil. Most common usecase.

type DSN added in v1.2111.0

type DSN struct {
	Host     string
	Port     string
	User     string
	Password string

	// DatabaseName is the default database to connect.
	DatabaseName string
}

DSN provide necessary information to connect to database.

type Driver

type Driver interface {
	Close(ctx context.Context)
	Ping(ctx context.Context) error
	Exec(ctx context.Context, query string) (driver.Result, error)
	Tx(ctx context.Context, queries ...string) ([]driver.Result, error)

	// Schemas export all supported schemas.
	//
	// For example, performance_schema/performance_schema... which in MySQL is not allowed for auditing.
	Schemas(ctx context.Context) ([]string, error)

	// Parse parse sqlText to Node array.
	//
	// sqlText may be single SQL or batch SQLs.
	Parse(ctx context.Context, sqlText string) ([]Node, error)

	// Audit sql with rules. sql is single SQL text.
	//
	// Multi Audit call may be in one context.
	// For example:
	//		driver, _ := NewDriver(..., ..., ...)
	// 		driver.Audit(..., "CREATE TABLE t1(id int)")
	// 		driver.Audit(..., "SELECT * FROM t1 WHERE id = 1")
	//      ...
	// driver should keep SQL context during it's lifecycle.
	Audit(ctx context.Context, sql string) (*AuditResult, error)

	// GenRollbackSQL generate sql's rollback SQL.
	GenRollbackSQL(ctx context.Context, sql string) (string, string, error)
}

Driver is a interface that must be implemented by a database.

It's implementation maybe on the same process or over gRPC(by go-plugin).

Driver is responsible for two primary things: 1. provides handle to communicate with database 2. audit SQL with rules

func NewDriver

func NewDriver(log *logrus.Entry, dbType string, cfg *Config) (Driver, error)

NewDriver return a new instantiated Driver.

type DriverNotSupportedError added in v1.2112.0

type DriverNotSupportedError struct {
	DriverTyp string
}

func (*DriverNotSupportedError) Error added in v1.2112.0

func (e *DriverNotSupportedError) Error() string

type Node

type Node struct {
	// Text is the raw SQL text of Node.
	Text string

	// Type is type of SQL, such as DML/DDL/DCL.
	Type string

	// Fingerprint is fingerprint of Node's raw SQL.
	Fingerprint string
}

Node is a interface which unify SQL ast tree. It produce by Driver.Parse.

type Registerer added in v1.2111.0

type Registerer interface {
	// Name returns plugin name.
	Name() string

	// Rules returns all rules that plugin supported.
	Rules() []*Rule
}

Registerer is the interface that all SQLe plugins must support.

type Rule added in v1.2111.0

type Rule struct {
	Name string
	Desc string

	// Category is the category of the rule. Such as "Naming Conventions"...
	// Rules will be displayed on the SQLE rule list page by category.
	Category string
	Level    RuleLevel
	Params   RuleParams
}

type RuleLevel added in v1.2111.0

type RuleLevel string
const (
	RuleLevelNormal RuleLevel = "normal"
	RuleLevelNotice RuleLevel = "notice"
	RuleLevelWarn   RuleLevel = "warn"
	RuleLevelError  RuleLevel = "error"
)

type RuleParam added in v1.2112.0

type RuleParam struct {
	Key   string        `json:"key"`
	Value string        `json:"value"`
	Desc  string        `json:"desc"`
	Type  RuleParamType `json:"type"`
}

func (*RuleParam) Bool added in v1.2112.0

func (r *RuleParam) Bool() bool

func (*RuleParam) Int added in v1.2112.0

func (r *RuleParam) Int() int

func (*RuleParam) String added in v1.2112.0

func (r *RuleParam) String() string

type RuleParamType added in v1.2112.0

type RuleParamType string
const (
	RuleParamTypeString RuleParamType = "string"
	RuleParamTypeInt    RuleParamType = "int"
	RuleParamTypeBool   RuleParamType = "bool"
)

type RuleParams added in v1.2112.0

type RuleParams []*RuleParam

func (*RuleParams) GetParam added in v1.2112.0

func (r *RuleParams) GetParam(key string) *RuleParam

func (*RuleParams) SetParamValue added in v1.2112.0

func (r *RuleParams) SetParamValue(key, value string) error

Directories

Path Synopsis
Package proto is a generated protocol buffer package.
Package proto is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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