-
Notifications
You must be signed in to change notification settings - Fork 162
/
custom_plugin.go
131 lines (100 loc) · 3.53 KB
/
custom_plugin.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
package backends
import (
"fmt"
"plugin"
log "github.com/sirupsen/logrus"
)
type CustomPlugin struct {
plugin *plugin.Plugin
init func(map[string]string, log.Level) error
getName func() string
getUser func(username, password, clientid string) (bool, error)
getSuperuser func(username string) (bool, error)
checkAcl func(username, topic, clientid string, acc int32) (bool, error)
halt func()
}
func NewCustomPlugin(authOpts map[string]string, logLevel log.Level) (*CustomPlugin, error) {
plug, err := plugin.Open(authOpts["plugin_path"])
if err != nil {
return nil, fmt.Errorf("could not init custom plugin: %s", err)
}
customPlugin := &CustomPlugin{
plugin: plug,
}
// Damn, this is gonna be tedious, freaking error handling!
plInit, err := plug.Lookup("Init")
if err != nil {
return nil, fmt.Errorf("couldn't find func Init in plugin: %s", err)
}
initFunc := plInit.(func(authOpts map[string]string, logLevel log.Level) error)
err = initFunc(authOpts, logLevel)
if err != nil {
return nil, fmt.Errorf("couldn't init plugin: %s", err)
}
customPlugin.init = initFunc
plName, err := plug.Lookup("GetName")
if err != nil {
return nil, fmt.Errorf("couldn't find func GetName in plugin: %s", err)
}
nameFunc := plName.(func() string)
customPlugin.getName = nameFunc
plGetUser, err := plug.Lookup("GetUser")
if err != nil {
return nil, fmt.Errorf("couldn't find func GetUser in plugin: %s", err)
}
getUserFunc, ok := plGetUser.(func(username, password, clientid string) (bool, error))
if !ok {
// Here and in other places, we do this for backwards compatibility in case the custom plugin so was created before error was returned.
tmp := plGetUser.(func(username, password, clientid string) bool)
getUserFunc = func(username, password, clientid string) (bool, error) {
return tmp(username, password, clientid), nil
}
}
customPlugin.getUser = getUserFunc
plGetSuperuser, err := plug.Lookup("GetSuperuser")
if err != nil {
return nil, fmt.Errorf("couldn't find func GetSuperuser in plugin: %s", err)
}
getSuperuserFunc, ok := plGetSuperuser.(func(username string) (bool, error))
if !ok {
tmp := plGetSuperuser.(func(username string) bool)
getSuperuserFunc = func(username string) (bool, error) {
return tmp(username), nil
}
}
customPlugin.getSuperuser = getSuperuserFunc
plCheckAcl, err := plug.Lookup("CheckAcl")
if err != nil {
return nil, fmt.Errorf("couldn't find func CheckAcl in plugin: %s", err)
}
checkAclFunc, ok := plCheckAcl.(func(username, topic, clientid string, acc int32) (bool, error))
if !ok {
tmp := plCheckAcl.(func(username, topic, clientid string, acc int32) bool)
checkAclFunc = func(username, topic, clientid string, acc int32) (bool, error) {
return tmp(username, topic, clientid, acc), nil
}
}
customPlugin.checkAcl = checkAclFunc
plHalt, err := plug.Lookup("Halt")
if err != nil {
return nil, fmt.Errorf("couldn't find func Halt in plugin: %s", err)
}
haltFunc := plHalt.(func())
customPlugin.halt = haltFunc
return customPlugin, nil
}
func (o *CustomPlugin) GetUser(username, password, clientid string) (bool, error) {
return o.getUser(username, password, clientid)
}
func (o *CustomPlugin) GetSuperuser(username string) (bool, error) {
return o.getSuperuser(username)
}
func (o *CustomPlugin) CheckAcl(username, topic, clientid string, acc int32) (bool, error) {
return o.checkAcl(username, topic, clientid, acc)
}
func (o *CustomPlugin) GetName() string {
return o.getName()
}
func (o *CustomPlugin) Halt() {
o.halt()
}