Skip to content

Commit

Permalink
plugin: setup connection info in session when audit plugin be enabled (
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored Jun 27, 2019
1 parent cf5f42b commit 98274cc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
15 changes: 15 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,21 @@ func ForeachPlugin(kind Kind, fn func(plugin *Plugin) error) error {
return nil
}

// IsEnable checks plugin's enable state.
func IsEnable(kind Kind) bool {
plugins := pluginGlobal.plugins()
if plugins == nil {
return false
}
for i := range plugins.plugins[kind] {
p := &plugins.plugins[kind][i]
if p.State == Ready {
return true
}
}
return false
}

// GetAll finds and returns all plugins.
func GetAll() map[Kind][]Plugin {
plugins := pluginGlobal.plugins()
Expand Down
6 changes: 5 additions & 1 deletion server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1449,10 +1449,14 @@ func (cc *clientConn) handleChangeUser(ctx context.Context, data []byte) error {
return err
}

if plugin.IsEnable(plugin.Audit) {
cc.ctx.GetSessionVars().ConnectionInfo = cc.connectInfo()
}

err = plugin.ForeachPlugin(plugin.Audit, func(p *plugin.Plugin) error {
authPlugin := plugin.DeclareAuditManifest(p.Manifest)
if authPlugin.OnConnectionEvent != nil {
connInfo := cc.connectInfo()
connInfo := cc.ctx.GetSessionVars().ConnectionInfo
err = authPlugin.OnConnectionEvent(context.Background(), &auth.UserIdentity{Hostname: connInfo.Host}, plugin.ChangeUser, connInfo)
if err != nil {
return err
Expand Down
13 changes: 8 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,14 @@ func (s *Server) onConn(conn *clientConn) {
s.rwlock.Unlock()
metrics.ConnGauge.Set(float64(connections))

if plugin.IsEnable(plugin.Audit) {
conn.ctx.GetSessionVars().ConnectionInfo = conn.connectInfo()
}
err := plugin.ForeachPlugin(plugin.Audit, func(p *plugin.Plugin) error {
authPlugin := plugin.DeclareAuditManifest(p.Manifest)
if authPlugin.OnConnectionEvent != nil {
connInfo := conn.connectInfo()
return authPlugin.OnConnectionEvent(context.Background(), conn.ctx.GetSessionVars().User, plugin.Connected, connInfo)
sessionVars := conn.ctx.GetSessionVars()
return authPlugin.OnConnectionEvent(context.Background(), sessionVars.User, plugin.Connected, sessionVars.ConnectionInfo)
}
return nil
})
Expand All @@ -440,9 +443,9 @@ func (s *Server) onConn(conn *clientConn) {
err = plugin.ForeachPlugin(plugin.Audit, func(p *plugin.Plugin) error {
authPlugin := plugin.DeclareAuditManifest(p.Manifest)
if authPlugin.OnConnectionEvent != nil {
connInfo := conn.connectInfo()
connInfo.Duration = float64(time.Since(connectedTime)) / float64(time.Millisecond)
err := authPlugin.OnConnectionEvent(context.Background(), conn.ctx.GetSessionVars().User, plugin.Disconnect, connInfo)
sessionVars := conn.ctx.GetSessionVars()
sessionVars.ConnectionInfo.Duration = float64(time.Since(connectedTime)) / float64(time.Millisecond)
err := authPlugin.OnConnectionEvent(context.Background(), sessionVars.User, plugin.Disconnect, sessionVars.ConnectionInfo)
if err != nil {
logutil.BgLogger().Warn("do connection event failed", zap.String("plugin", authPlugin.Name), zap.Error(err))
}
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ type SessionVars struct {

// Killed is a flag to indicate that this query is killed.
Killed uint32

// ConnectionInfo indicates current connection info used by current session, only be lazy assigned by plugin.
ConnectionInfo *ConnectionInfo
}

// ConnectionInfo present connection used by audit.
Expand Down

0 comments on commit 98274cc

Please sign in to comment.