Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin: setup connection info in session when audit plugin be enabled #10923

Merged
merged 3 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1434,10 +1434,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 @@ -385,6 +385,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about saving it to clientConn?
It's not used in sessionVars anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiancaiamao clientConn is not easy to obtain in the later process phase(e.g. write audit log after a query finished - - ), it will be used in audit plugin's OnGeneralEvent

}

// ConnectionInfo present connection used by audit.
Expand Down