db

package
v0.0.0-...-fb8553b Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2017 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//QueryCounter state
	QueryCounter = uint64(0)

	//AbnormalCounter state
	AbnormalCounter = uint64(0)
)
View Source
var (
	// DBCon boltdb
	DBCon *bolt.DB
)

Functions

func Contains

func Contains(target interface{}, obj interface{}) (bool, error)

Contains check if target contains obj

func RandString

func RandString(n int) string

RandString generate a random string of a fixed length https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang

func SContains

func SContains(target []string, obj string) (bool, error)

SContains string array contains

Types

type BASE

type BASE interface {
	InitialDB(string, time.Duration, time.Duration) error
	RecordQueryAction(sql.QueryAction) error
	Abnormals() int
	RecordAbnormal(sql.QueryContext, string) error
	Patterns() int
	CheckPattern([]byte) error
	AddPattern([]byte, sql.QueryContext) error
	PutPattern([]byte, []byte) error
	DeletePattern([]byte) error
	Purge() error
	CheckQuery(sql.QueryContext, bool, bool) bool
	CheckPermission(sql.QueryContext, bool, bool) bool
	UpdateState() error
	SyncAndClose() error
}

BASE interface should get implemented with every added store database(Boltdb, MySQL, Postgre & etc.) structure

func GenerateLocalDB

func GenerateLocalDB(dbName string, dbID string) BASE

GenerateLocalDB generate local db

type BoltDB

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

BoltDB local db

func (*BoltDB) Abnormals

func (m *BoltDB) Abnormals() (count int)

Abnormals list Abnormals

func (*BoltDB) AddPattern

func (m *BoltDB) AddPattern(pattern []byte, context sql.QueryContext) error

AddPattern add

func (*BoltDB) CheckPattern

func (m *BoltDB) CheckPattern(pattern []byte) error

CheckPattern check if pattern exist

func (*BoltDB) CheckPermission

func (m *BoltDB) CheckPermission(sql.QueryContext, bool, bool) bool

CheckPermission check if has permission

func (*BoltDB) CheckQuery

func (m *BoltDB) CheckQuery(context sql.QueryContext, checkUser bool, checkSource bool) bool

CheckQuery check if Query exist

func (*BoltDB) DeletePattern

func (m *BoltDB) DeletePattern(pattern []byte) error

DeletePattern delete pattern

func (*BoltDB) InitialDB

func (m *BoltDB) InitialDB(str string, syncInterval time.Duration, timeout time.Duration) error

InitialDB local databases

func (*BoltDB) Patterns

func (m *BoltDB) Patterns() (count int)

Patterns list Patterns

func (*BoltDB) Purge

func (m *BoltDB) Purge() error

Purge local database

func (*BoltDB) PutPattern

func (m *BoltDB) PutPattern(pattern []byte, query []byte) error

PutPattern put pattern

func (*BoltDB) RecordAbnormal

func (m *BoltDB) RecordAbnormal(context sql.QueryContext, abType string) error

RecordAbnormal record abnormal query

func (*BoltDB) RecordQueryAction

func (m *BoltDB) RecordQueryAction(context sql.QueryAction) error

RecordQueryAction record query and action

func (*BoltDB) SyncAndClose

func (m *BoltDB) SyncAndClose() error

SyncAndClose local databases

func (*BoltDB) UpdateState

func (m *BoltDB) UpdateState() error

UpdateState update

type MySQL

type MySQL struct {
	UUID string
	// contains filtered or unexported fields
}

MySQL local db

func (*MySQL) Abnormals

func (m *MySQL) Abnormals() (count int)

Abnormals list abnormals

func (*MySQL) AddPattern

func (m *MySQL) AddPattern(pattern []byte, context sql.QueryContext) error

AddPattern add

func (*MySQL) CheckPattern

func (m *MySQL) CheckPattern(pattern []byte) error

CheckPattern check if pattern exists

func (*MySQL) CheckPermission

func (m *MySQL) CheckPermission(context sql.QueryContext, q bool, v bool) bool

CheckPermission check if has permission

func (*MySQL) CheckQuery

func (m *MySQL) CheckQuery(context sql.QueryContext, checkUser bool, checkSource bool) bool

CheckQuery check query

func (*MySQL) DeletePattern

func (m *MySQL) DeletePattern(pattern []byte) error

DeletePattern delete pattern

func (*MySQL) InitialDB

func (m *MySQL) InitialDB(str string, syncInterval time.Duration, timeout time.Duration) error

InitialDB local databases

func (*MySQL) Patterns

func (m *MySQL) Patterns() (count int)

Patterns list Patterns

func (*MySQL) Purge

func (m *MySQL) Purge() error

Purge local databases

func (*MySQL) PutPattern

func (m *MySQL) PutPattern(pattern []byte, query []byte) error

PutPattern put pattern

func (*MySQL) RecordAbnormal

func (m *MySQL) RecordAbnormal(context sql.QueryContext, abType string) error

RecordAbnormal record abnormal query

func (*MySQL) RecordQueryAction

func (m *MySQL) RecordQueryAction(context sql.QueryAction) error

RecordQueryAction record query and action

func (*MySQL) SyncAndClose

func (m *MySQL) SyncAndClose() error

SyncAndClose local databases

func (*MySQL) UpdateState

func (m *MySQL) UpdateState() error

UpdateState update

type Pattern

type Pattern struct {
	ID int `orm:"column(id)"`
	// pattent_key
	Key string `orm:"column(key);null;type(text)"`
	//value
	Value string `orm:"column(value);null;type(text)"`
	// Example Value
	ExampleValue string `orm:"column(example_value);null;type(text)"`
	// 启用状态, true, false
	Enable bool   `orm:"column(enable);default(true)"`
	UUID   string `orm:"column(uuid);size(36)"`
}

Pattern record trainging set

type Permission

type Permission struct {
	ID int `orm:"column(id)"`
	// 数据库
	Db string `orm:"column(db);null;size(128)"`
	// 用户
	User string `orm:"column(user);null;size(128)"`
	// 客户端
	Client string `orm:"column(client);null;size(128)"`
	// 表, "*" 表示全部
	Table string `orm:"column(table);null;size(128)"`
	// 权限, SELECT,UPDATE,DELETE,INSERT,GRANT....
	Permission string `orm:"column(permission);type(text)"`
	// 启用状态, true, false
	Enable bool   `orm:"column(enable);default(true)"`
	UUID   string `orm:"column(uuid);size(36)"`
}

Permission 权限规则

type QueryAction

type QueryAction struct {
	ID        int    `orm:"column(id)"`
	SessionID string `orm:"column(flow_id);null;size(32)"`
	FlowInfo  string `orm:"column(flow_info);null;type(text)"`
	// 实际查询语句
	Query string `orm:"column(query);null;type(text)"`
	// 查询用户
	User string `orm:"column(user);null;size(128)"`
	// 查询客户端信息
	ClientIP      string `orm:"column(client_ip);null;size(39)"`
	ClientProgram string `orm:"column(client_program);null;size(128)"`
	// server info
	ServerIP   string `orm:"column(server_ip);null;size(39)"`
	ServerPort int    `orm:"column(server_port);null"`
	// 执行的数据库和表
	Database string `orm:"column(db);null;size(128)"`
	Tables   string `orm:"column(tables);null;type(text)"`
	// 执行时间和执行耗时(ms)
	Time     time.Time `orm:"column(time);auto_now_add;type(datetime);size(6)"`
	Duration int64     `orm:"column(duration);default(0)"`
	// 执行结果
	QueryResult bool `orm:"column(query_result);default(true)"`
	// 是否违规操作
	IsAbnormal bool `orm:"column(is_abnormal);default(false)"`
	// 违规操作类型:none, pattern, permission
	AbnormalType string `orm:"column(abnormal_type);size(32);default(none)"`
	// 处理结果:none, learning, pass, drop
	Action string `orm:"column(action);size(36);defult(pass)"`
	// 告警
	IsAlarm bool `orm:"column(is_alarm);default(false)"`
	// 是否分析
	Analysed bool `orm:"column(analysed);default(false)"`
	// sql type
	SQLType string `orm:"column(sql_type);null;size(32)"`
	// dbshield or others
	Tool string `orm:"column(tool);null;size(32)"`
	// 模式
	Pattern string `orm:"column(pattern);null;type(text)"`
	// 区分不同
	UUID string `orm:"column(uuid);size(36)"`
}

QueryAction 记录所有操作

type State

type State struct {
	ID              int    `orm:"column(id)"`
	Key             string `orm:"column(key);size(5)"`
	QueryCounter    uint64 `orm:"column(QueryCounter);type(bigint unsigned)"`
	AbnormalCounter uint64 `orm:"column(AbnormalCounter);type(bigint unsigned)"`
	UUID            string `orm:"column(uuid);size(36)"`
}

State record abnormal set

Jump to

Keyboard shortcuts

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