Documentation
¶
Index ¶
- Constants
- Variables
- func AllDrivers() []string
- func AllRules() map[string][]*Rule
- func InitPlugins(pluginDir string) error
- func Register(name string, h handler, rs []*Rule)
- func ServePlugin(r Registerer, newDriver func(cfg *Config) Driver)
- type AuditResult
- type Config
- type DSN
- type Driver
- type DriverNotSupportedError
- type Node
- type Registerer
- type Rule
- type RuleLevel
Constants ¶
const ( SQLTypeDML = "dml" SQLTypeDDL = "ddl" )
const ( DriverTypeMySQL = "mysql" DriverTypePostgreSQL = "PostgreSQL" )
Variables ¶
var ErrNodesCountExceedOne = errors.New("after parse, nodes count exceed one")
Functions ¶
func AllDrivers ¶
func AllDrivers() []string
func InitPlugins ¶
InitPlugins init plugins at plugins directory. It should be called on host process.
func Register ¶
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) HasResult ¶ added in v1.2201.0
func (rs *AuditResult) HasResult() bool
func (*AuditResult) Level ¶
func (rs *AuditResult) Level() RuleLevel
Level find highest Level in result
func (*AuditResult) Message ¶
func (rs *AuditResult) Message() string
type Config ¶
Config define the configuration for driver.
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
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 RuleLevel ¶ added in v1.2111.0
type RuleLevel string