Skip to content

Commit

Permalink
refactor: AuditLog filter PreHandle support more stmtnode type (#198)
Browse files Browse the repository at this point in the history
* refactor: AuditLog filter PreHandle support more stmtnode type
  • Loading branch information
dk-lockdown committed Jul 14, 2022
1 parent 3a72572 commit bb408a5
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions pkg/filter/audit_log/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@ func (f *_filter) PreHandle(ctx context.Context, conn proto.Connection) error {
var (
commandTypeStr string
args strings.Builder
stmt ast.Node
stmtNode ast.StmtNode
)
args.WriteByte('[')
switch commandType {
case constant.ComQuery:
commandTypeStr = "COM_QUERY"
stmt = proto.QueryStmt(ctx)
stmtNode = proto.QueryStmt(ctx)
case constant.ComStmtExecute:
commandTypeStr = "COM_STMT_EXECUTE"
statement := proto.PrepareStmt(ctx)
stmt = statement.StmtNode
stmtNode = statement.StmtNode
for i := 0; i < len(statement.BindVars); i++ {
parameterID := fmt.Sprintf("v%d", i+1)
param := statement.BindVars[parameterID]
Expand All @@ -145,18 +145,8 @@ func (f *_filter) PreHandle(ctx context.Context, conn proto.Connection) error {
}
args.WriteByte(']')

var command string
switch stmt.(type) {
case *ast.DeleteStmt:
command = "DELETE"
case *ast.InsertStmt:
command = "INSERT"
case *ast.UpdateStmt:
command = "UPDATE"
case *ast.SelectStmt:
command = "SELECT"
default:
}
command := misc.GetStmtLabel(stmtNode)
command = strings.ToUpper(command)

if _, err := f.log.Write([]byte(fmt.Sprintf("%s,%s,%s,%v,%s,%s,%s,%s,0\n", carbon.Now(), userName, remoteAddr, connectionID,
commandTypeStr, command, sqlText, args.String()))); err != nil {
Expand Down Expand Up @@ -210,6 +200,7 @@ func (f *_filter) PostHandle(ctx context.Context, result proto.Result, conn prot
args.WriteByte(']')

command := misc.GetStmtLabel(stmtNode)
command = strings.ToUpper(command)

affected, err := result.RowsAffected()
if err != nil {
Expand Down

0 comments on commit bb408a5

Please sign in to comment.