|
| 1 | +// Copyright 2019 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package plugin |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + "github.com/pingcap/parser/auth" |
| 20 | + "github.com/pingcap/tidb/sessionctx/variable" |
| 21 | +) |
| 22 | + |
| 23 | +// GeneralEvent presents TiDB generate event. |
| 24 | +type GeneralEvent byte |
| 25 | + |
| 26 | +const ( |
| 27 | + // Log presents log event. |
| 28 | + Log GeneralEvent = iota |
| 29 | + // Error presents error event. |
| 30 | + Error |
| 31 | + // Result presents result event. |
| 32 | + Result |
| 33 | + // Status presents status event. |
| 34 | + Status |
| 35 | +) |
| 36 | + |
| 37 | +// ConnectionEvent presents TiDB connection event. |
| 38 | +type ConnectionEvent byte |
| 39 | + |
| 40 | +const ( |
| 41 | + // Connected presents new connection establish event(finish auth). |
| 42 | + Connected ConnectionEvent = iota |
| 43 | + // Disconnect presents disconnect event. |
| 44 | + Disconnect |
| 45 | + // ChangeUser presents change user. |
| 46 | + ChangeUser |
| 47 | + // PreAuth presents event before start auth. |
| 48 | + PreAuth |
| 49 | +) |
| 50 | + |
| 51 | +func (c ConnectionEvent) String() string { |
| 52 | + switch c { |
| 53 | + case Connected: |
| 54 | + return "Connected" |
| 55 | + case Disconnect: |
| 56 | + return "Disconnect" |
| 57 | + case ChangeUser: |
| 58 | + return "ChangeUser" |
| 59 | + case PreAuth: |
| 60 | + return "PreAuth" |
| 61 | + } |
| 62 | + return "" |
| 63 | +} |
| 64 | + |
| 65 | +// ParseEvent presents events happen around parser. |
| 66 | +type ParseEvent byte |
| 67 | + |
| 68 | +const ( |
| 69 | + // PreParse presents event before parse. |
| 70 | + PreParse ParseEvent = 1 + iota |
| 71 | + // PostParse presents event after parse. |
| 72 | + PostParse |
| 73 | +) |
| 74 | + |
| 75 | +// AuditManifest presents a sub-manifest that every audit plugin must provide. |
| 76 | +type AuditManifest struct { |
| 77 | + Manifest |
| 78 | + // OnConnectionEvent will be called when TiDB receive or disconnect from client. |
| 79 | + // return error will ignore and close current connection. |
| 80 | + OnConnectionEvent func(ctx context.Context, identity *auth.UserIdentity, event ConnectionEvent, info *variable.ConnectionInfo) error |
| 81 | + // OnGeneralEvent will be called during TiDB execution. |
| 82 | + OnGeneralEvent func(ctx context.Context, sctx *variable.SessionVars, event GeneralEvent, cmd string) |
| 83 | + // OnGlobalVariableEvent will be called when Change GlobalVariable. |
| 84 | + OnGlobalVariableEvent func(ctx context.Context, sctx *variable.SessionVars, varName, varValue string) |
| 85 | + // OnParseEvent will be called around parse logic. |
| 86 | + OnParseEvent func(ctx context.Context, sctx *variable.SessionVars, event ParseEvent) error |
| 87 | +} |
0 commit comments