-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugins.go
167 lines (141 loc) · 3.78 KB
/
plugins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package pggateway
import (
"fmt"
"sync"
"github.com/c653labs/pgproto"
)
var authPlugins = make(map[string]authPluginInitializer)
var loggingPlugins = make(map[string]loggingPluginInitializer)
type authPluginInitializer func(ConfigMap) (AuthenticationPlugin, error)
type loggingPluginInitializer func(ConfigMap) (LoggingPlugin, error)
type Plugin interface{}
type AuthenticationPlugin interface {
Plugin
Authenticate(*Session, *pgproto.StartupMessage) (bool, error)
}
type LoggingContext map[string]interface{}
type LoggingPlugin interface {
Plugin
LogInfo(LoggingContext, string, ...interface{})
LogDebug(LoggingContext, string, ...interface{})
LogError(LoggingContext, string, ...interface{})
LogFatal(LoggingContext, string, ...interface{})
LogWarn(LoggingContext, string, ...interface{})
}
func RegisterAuthPlugin(name string, init authPluginInitializer) {
authPlugins[name] = init
}
func RegisterLoggingPlugin(name string, init func(ConfigMap) (LoggingPlugin, error)) {
loggingPlugins[name] = init
}
type loggingMessage struct {
level string
context LoggingContext
msg string
args []interface{}
}
type PluginRegistry struct {
authPlugins map[string]AuthenticationPlugin
loggingPlugins map[string]LoggingPlugin
logMutex *sync.Mutex
}
func NewPluginRegistry(auth map[string]ConfigMap, logging map[string]ConfigMap) (*PluginRegistry, error) {
r := &PluginRegistry{
authPlugins: make(map[string]AuthenticationPlugin),
loggingPlugins: make(map[string]LoggingPlugin),
logMutex: &sync.Mutex{},
}
for name, config := range auth {
init, ok := authPlugins[name]
if !ok {
return nil, fmt.Errorf("could not find authentication plugin: %s", name)
}
p, err := init(config)
if err != nil {
return nil, err
}
r.authPlugins[name] = p
}
for name, config := range logging {
init, ok := loggingPlugins[name]
if !ok {
return nil, fmt.Errorf("could not find logging plugin: %s", name)
}
p, err := init(config)
if err != nil {
return nil, err
}
r.loggingPlugins[name] = p
}
return r, nil
}
func (r *PluginRegistry) handleLog(msg loggingMessage) {
r.logMutex.Lock()
for _, p := range r.loggingPlugins {
switch msg.level {
case "info":
p.LogInfo(msg.context, msg.msg, msg.args...)
case "debug":
p.LogDebug(msg.context, msg.msg, msg.args...)
case "warn":
p.LogWarn(msg.context, msg.msg, msg.args...)
case "error":
p.LogError(msg.context, msg.msg, msg.args...)
case "fatal":
p.LogFatal(msg.context, msg.msg, msg.args...)
}
}
r.logMutex.Unlock()
}
func (r *PluginRegistry) Authenticate(sess *Session, startup *pgproto.StartupMessage) (bool, error) {
for _, p := range r.authPlugins {
success, err := p.Authenticate(sess, startup)
if err != nil {
return false, err
}
if success {
return true, nil
}
}
return false, nil
}
func (r *PluginRegistry) LogInfo(context LoggingContext, msg string, args ...interface{}) {
r.handleLog(loggingMessage{
level: "info",
context: context,
msg: msg,
args: args,
})
}
func (r *PluginRegistry) LogError(context LoggingContext, msg string, args ...interface{}) {
r.handleLog(loggingMessage{
level: "error",
context: context,
msg: msg,
args: args,
})
}
func (r *PluginRegistry) LogWarn(context LoggingContext, msg string, args ...interface{}) {
r.handleLog(loggingMessage{
level: "warn",
context: context,
msg: msg,
args: args,
})
}
func (r *PluginRegistry) LogDebug(context LoggingContext, msg string, args ...interface{}) {
r.handleLog(loggingMessage{
level: "debug",
context: context,
msg: msg,
args: args,
})
}
func (r *PluginRegistry) LogFatal(context LoggingContext, msg string, args ...interface{}) {
r.handleLog(loggingMessage{
level: "fatal",
context: context,
msg: msg,
args: args,
})
}