Documentation ¶
Index ¶
- Constants
- Variables
- func AllAdditionalParams() map[string]params.Params
- func AllDrivers() []string
- func AllRules() map[string][]*Rule
- func GetQueryDriverNames() []string
- func InitPlugins(pluginDir string) error
- func NewAnalysisDriverPlugin(newDriver func(cfg *DSN) AnalysisDriver) *analysisDriverPlugin
- func NewAuditDriverPlugin(r Registerer, newDriver func(cfg *Config) Driver) *auditDriverPlugin
- func NewQueryDriverPlugin(newDriver func(cfg *DSN) SQLQueryDriver) *queryDriverPlugin
- func RegisterAnalysisDriver(name string)
- func RegisterAuditDriver(name string, rs []*Rule, ap params.Params)
- func RegisterDriverFromClient(client *PluginClient) error
- func RegisterDriverManger(client *PluginClient, pluginName string, handler newDriverManagerHandler)
- func RegisterSQLQueryDriver(name string)
- func RuleLevelLessOrEqual(a, b string) bool
- func ServePlugin(r Registerer, newDriver func(cfg *Config) Driver)
- type AnalysisDriver
- type AnalysisInfoHead
- type AnalysisInfoInTableFormat
- type AuditResult
- type ColumnsInfo
- type Config
- type DSN
- type Driver
- type DriverManager
- type DriverNotSupportedError
- type ErrorType
- type ExplainClassicResult
- type ExplainConf
- type ExplainResult
- type GetTableMetaBySQLConf
- type GetTableMetaBySQLResult
- type GetTableMetaByTableNameConf
- type GetTableMetaByTableNameResult
- type IndexesInfo
- type ListTablesInSchemaConf
- type ListTablesInSchemaResult
- type Node
- type PluginClient
- type PluginDriverManager
- type PluginServer
- func (p *PluginServer) AddAnalysisDriverPlugin(plugin goPlugin.Plugin)
- func (p *PluginServer) AddDriverPlugin(plugin goPlugin.Plugin)
- func (p *PluginServer) AddPlugin(pluginName string, pluginVersion int, plugin goPlugin.Plugin)
- func (p *PluginServer) AddQueryDriverPlugin(plugin goPlugin.Plugin)
- func (p *PluginServer) Serve()
- type QueryConf
- type QueryPrepareConf
- type QueryPrepareResult
- type QueryResult
- type QueryResultRow
- type QueryResultValue
- type Registerer
- type Rule
- type RuleLevel
- type SQLQueryDriver
- type Table
- type TableMetaItem
- type TableMetaItemBySQL
Constants ¶
const ( SQLTypeDML = "dml" SQLTypeDDL = "ddl" )
const ( DriverTypeMySQL = "MySQL" DriverTypePostgreSQL = "PostgreSQL" DriverTypeTiDB = "TiDB" )
const ( ErrorTypeNotQuery = "not query" ErrorTypeNotError = "not error" )
const (
DefaultPluginVersion = 1
)
const (
PluginNameAnalysisDriver = "analysis-driver"
)
const (
PluginNameAuditDriver = "audit-driver"
)
const (
PluginNameQueryDriver = "query-driver"
)
Variables ¶
var ErrNodesCountExceedOne = errors.New("after parse, nodes count exceed one")
var ErrSQLIsNotSupported = errors.New("SQL is not supported")
var SQLEGRPCDialOptions = []grpc.DialOption{}
var SQLEGrpcServer = goPlugin.DefaultGRPCServer
Functions ¶
func AllAdditionalParams ¶ added in v1.2203.0
func AllDrivers ¶
func AllDrivers() []string
func GetQueryDriverNames ¶ added in v1.2206.0
func GetQueryDriverNames() []string
QueryDriverName = InstanceType
func InitPlugins ¶
InitPlugins init plugins at plugins directory. It should be called on host process.
func NewAnalysisDriverPlugin ¶ added in v1.2207.0
func NewAnalysisDriverPlugin(newDriver func(cfg *DSN) AnalysisDriver) *analysisDriverPlugin
func NewAuditDriverPlugin ¶ added in v1.2206.0
func NewAuditDriverPlugin(r Registerer, newDriver func(cfg *Config) Driver) *auditDriverPlugin
func NewQueryDriverPlugin ¶ added in v1.2206.0
func NewQueryDriverPlugin(newDriver func(cfg *DSN) SQLQueryDriver) *queryDriverPlugin
func RegisterAnalysisDriver ¶ added in v1.2206.0
func RegisterAnalysisDriver(name string)
RegisterAnalysisDriver like sql.RegisterAuditDriver.
RegisterAnalysisDriver makes a database driver available by the provided driver name. RegisterAnalysisDriver's initialize handler registered by RegisterAnalysisDriver.
func RegisterAuditDriver ¶ added in v1.2206.0
RegisterAuditDriver like sql.RegisterAuditDriver.
RegisterAuditDriver makes a database driver available by the provided driver name. Driver's initialize handler and audit rules register by RegisterAuditDriver.
func RegisterDriverFromClient ¶ added in v1.2206.0
func RegisterDriverFromClient(client *PluginClient) error
func RegisterDriverManger ¶ added in v1.2207.0
func RegisterDriverManger(client *PluginClient, pluginName string, handler newDriverManagerHandler)
func RegisterSQLQueryDriver ¶ added in v1.2206.0
func RegisterSQLQueryDriver(name string)
RegisterSQLQueryDriver like sql.RegisterAuditDriver.
RegisterSQLQueryDriver makes a database driver available by the provided driver name. SQLQueryDriver's initialize handler and audit rules register by RegisterSQLQueryDriver.
func RuleLevelLessOrEqual ¶ added in v1.2204.0
RuleLevelLessOrEqual return level a <= level b
func ServePlugin ¶
func ServePlugin(r Registerer, newDriver func(cfg *Config) Driver)
ServePlugin start plugin process service. It should be called on plugin process. Deprecated: Use PluginServer.AddDriverPlugin and PluginServer.Serve instead.
Types ¶
type AnalysisDriver ¶ added in v1.2206.0
type AnalysisDriver interface { ListTablesInSchema(ctx context.Context, conf *ListTablesInSchemaConf) (*ListTablesInSchemaResult, error) GetTableMetaByTableName(ctx context.Context, conf *GetTableMetaByTableNameConf) (*GetTableMetaByTableNameResult, error) GetTableMetaBySQL(ctx context.Context, conf *GetTableMetaBySQLConf) (*GetTableMetaBySQLResult, error) Explain(ctx context.Context, conf *ExplainConf) (*ExplainResult, error) }
AnalysisDriver is a driver for SQL analysis and getting table metadata
type AnalysisInfoHead ¶ added in v1.2206.0
type AnalysisInfoInTableFormat ¶ added in v1.2206.0
type AnalysisInfoInTableFormat struct { Columns []AnalysisInfoHead Rows [][]string }
AnalysisInfoInTableFormat the field Columns represents the column name of a table the field Rows represents the data of the table their relationship is as follows
| Columns[0] | Columns[1] | Columns[2] | | Rows[0][0] | Rows[0][1] | Rows[0][2] | | Rows[1][0] | Rows[1][1] | Rows[1][2] |
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
func (*AuditResult) SortByLevel ¶ added in v1.2203.0
func (rs *AuditResult) SortByLevel()
type ColumnsInfo ¶ added in v1.2206.0
type ColumnsInfo struct {
AnalysisInfoInTableFormat
}
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 AdditionalParams params.Params // 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 DriverManager ¶ added in v1.2207.0
type DriverManager interface { GetAuditDriver() (Driver, error) GetSQLQueryDriver() (SQLQueryDriver, error) GetAnalysisDriver() (AnalysisDriver, error) // Close invoke grpc.Close of audit plugin to inform plugin process to recycle their resource // resource of all drivers should be recycle in this function Close(ctx context.Context) }
func NewDriverManger ¶ added in v1.2207.0
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 ExplainClassicResult ¶ added in v1.2206.0
type ExplainClassicResult struct {
AnalysisInfoInTableFormat
}
type ExplainConf ¶ added in v1.2206.0
type ExplainConf struct { // this SQL should be a single SQL Sql string }
type ExplainResult ¶ added in v1.2206.0
type ExplainResult struct {
ClassicResult ExplainClassicResult
}
type GetTableMetaBySQLConf ¶ added in v1.2206.0
type GetTableMetaBySQLConf struct { // this SQL should be a single SQL Sql string }
type GetTableMetaBySQLResult ¶ added in v1.2206.0
type GetTableMetaBySQLResult struct {
TableMetas []TableMetaItemBySQL
}
type GetTableMetaByTableNameConf ¶ added in v1.2206.0
type GetTableMetaByTableNameResult ¶ added in v1.2206.0
type GetTableMetaByTableNameResult struct {
TableMeta TableMetaItem
}
type IndexesInfo ¶ added in v1.2206.0
type IndexesInfo struct {
AnalysisInfoInTableFormat
}
type ListTablesInSchemaConf ¶ added in v1.2206.0
type ListTablesInSchemaConf struct {
Schema string
}
type ListTablesInSchemaResult ¶ added in v1.2206.0
type ListTablesInSchemaResult struct {
Tables []Table
}
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 PluginClient ¶ added in v1.2206.0
type PluginClient struct {
// contains filtered or unexported fields
}
func (*PluginClient) Client ¶ added in v1.2206.0
func (p *PluginClient) Client() (goPlugin.ClientProtocol, error)
func (*PluginClient) Kill ¶ added in v1.2206.0
func (p *PluginClient) Kill()
func (*PluginClient) RegisterDrivers ¶ added in v1.2207.0
func (p *PluginClient) RegisterDrivers(c *PluginClient) (pluginName string, err error)
type PluginDriverManager ¶ added in v1.2207.0
type PluginDriverManager struct {
// contains filtered or unexported fields
}
func (*PluginDriverManager) Close ¶ added in v1.2207.0
func (d *PluginDriverManager) Close(ctx context.Context)
func (*PluginDriverManager) GetAnalysisDriver ¶ added in v1.2207.0
func (d *PluginDriverManager) GetAnalysisDriver() (AnalysisDriver, error)
func (*PluginDriverManager) GetAuditDriver ¶ added in v1.2207.0
func (d *PluginDriverManager) GetAuditDriver() (Driver, error)
func (*PluginDriverManager) GetSQLQueryDriver ¶ added in v1.2207.0
func (d *PluginDriverManager) GetSQLQueryDriver() (SQLQueryDriver, error)
type PluginServer ¶ added in v1.2206.0
type PluginServer struct {
// contains filtered or unexported fields
}
func NewPlugin ¶ added in v1.2206.0
func NewPlugin() *PluginServer
func (*PluginServer) AddAnalysisDriverPlugin ¶ added in v1.2207.0
func (p *PluginServer) AddAnalysisDriverPlugin(plugin goPlugin.Plugin)
func (*PluginServer) AddDriverPlugin ¶ added in v1.2206.0
func (p *PluginServer) AddDriverPlugin(plugin goPlugin.Plugin)
func (*PluginServer) AddPlugin ¶ added in v1.2206.0
func (p *PluginServer) AddPlugin(pluginName string, pluginVersion int, plugin goPlugin.Plugin)
func (*PluginServer) AddQueryDriverPlugin ¶ added in v1.2206.0
func (p *PluginServer) AddQueryDriverPlugin(plugin goPlugin.Plugin)
func (*PluginServer) Serve ¶ added in v1.2206.0
func (p *PluginServer) Serve()
type QueryPrepareConf ¶ added in v1.2206.0
type QueryPrepareResult ¶ added in v1.2206.0
type QueryResult ¶ added in v1.2206.0
type QueryResult struct { Column params.Params Rows []*QueryResultRow }
The data location in Values should be consistent with that in Column
type QueryResultRow ¶ added in v1.2206.0
type QueryResultRow struct {
Values []*QueryResultValue
}
type QueryResultValue ¶ added in v1.2206.0
type QueryResultValue struct {
Value string
}
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 // AdditionalParams returns all additional params that plugin supported. AdditionalParams() params.Params }
Registerer is the interface that all SQLe plugins must support.
type RuleLevel ¶ added in v1.2111.0
type RuleLevel string
func (RuleLevel) LessOrEqual ¶ added in v1.2201.0
func (RuleLevel) MoreOrEqual ¶ added in v1.2203.0
type SQLQueryDriver ¶ added in v1.2206.0
type SQLQueryDriver interface { QueryPrepare(ctx context.Context, sql string, conf *QueryPrepareConf) (*QueryPrepareResult, error) Query(ctx context.Context, sql string, conf *QueryConf) (*QueryResult, error) }
SQLQueryDriver is a SQL rewrite and execute driver
type TableMetaItem ¶ added in v1.2206.0
type TableMetaItem struct { Name string Schema string ColumnsInfo ColumnsInfo IndexesInfo IndexesInfo CreateTableSQL string }
type TableMetaItemBySQL ¶ added in v1.2206.0
type TableMetaItemBySQL struct { Name string Schema string ColumnsInfo ColumnsInfo IndexesInfo IndexesInfo CreateTableSQL string Message string }