forked from krakend/krakend-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
274 lines (227 loc) · 6.69 KB
/
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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package krakend
import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
cmd "github.com/krakendio/krakend-cobra/v2"
"github.com/luraproject/lura/v2/logging"
proxy "github.com/luraproject/lura/v2/proxy/plugin"
client "github.com/luraproject/lura/v2/transport/http/client/plugin"
server "github.com/luraproject/lura/v2/transport/http/server/plugin"
"github.com/spf13/cobra"
)
// LoadPlugins loads and registers the plugins so they can be used if enabled at the configuration
func LoadPlugins(folder, pattern string, logger logging.Logger) {
LoadPluginsWithContext(context.Background(), folder, pattern, logger)
}
func LoadPluginsWithContext(ctx context.Context, folder, pattern string, logger logging.Logger) {
logger.Debug("[SERVICE: Plugin Loader] Starting loading process")
n, err := client.LoadWithLogger(
folder,
pattern,
client.RegisterClient,
logger,
)
logPluginLoaderErrors(logger, "[SERVICE: Executor Plugin]", n, err)
n, err = server.LoadWithLogger(
folder,
pattern,
server.RegisterHandler,
logger,
)
logPluginLoaderErrors(logger, "[SERVICE: Handler Plugin]", n, err)
n, err = proxy.LoadWithLoggerAndContext(
ctx,
folder,
pattern,
proxy.RegisterModifier,
logger,
)
logPluginLoaderErrors(logger, "[SERVICE: Modifier Plugin]", n, err)
logger.Debug("[SERVICE: Plugin Loader] Loading process completed")
}
func logPluginLoaderErrors(logger logging.Logger, tag string, n int, err error) {
if err != nil {
if mErrs, ok := err.(pluginLoaderErr); ok {
for _, err := range mErrs.Errs() {
logger.Debug(tag, err.Error())
}
} else {
logger.Debug(tag, err.Error())
}
}
if n > 0 {
logger.Info(tag, "Total plugins loaded:", n)
}
}
type pluginLoader struct{}
func (pluginLoader) Load(folder, pattern string, logger logging.Logger) {
LoadPlugins(folder, pattern, logger)
}
func (pluginLoader) LoadWithContext(ctx context.Context, folder, pattern string, logger logging.Logger) {
LoadPluginsWithContext(ctx, folder, pattern, logger)
}
type pluginLoaderErr interface {
Errs() []error
}
var (
serverExpected bool
clientExpected bool
modifierExpected bool
testPluginCmd = &cobra.Command{
Use: "test-plugin [flags] [artifacts]",
Short: "Tests that one or more plugins are loadable into KrakenD.",
Run: testPluginFunc,
Example: "krakend test-plugin -scm ./plugins/my_plugin.so ./plugins/my_other_plugin.so",
}
serverExpectedFlag cmd.FlagBuilder
clientExpectedFlag cmd.FlagBuilder
modifierExpectedFlag cmd.FlagBuilder
reLogErrorPlugins = regexp.MustCompile(`(?m)plugin \#\d+ \(.*\): (.*)`)
)
func init() {
serverExpectedFlag = cmd.BoolFlagBuilder(&serverExpected, "server", "s", false, "The artifact should contain a Server Plugin.")
clientExpectedFlag = cmd.BoolFlagBuilder(&clientExpected, "client", "c", false, "The artifact should contain a Client Plugin.")
modifierExpectedFlag = cmd.BoolFlagBuilder(&modifierExpected, "modifier", "m", false, "The artifact should contain a Req/Resp Modifier Plugin.")
}
func NewTestPluginCmd() cmd.Command {
return cmd.NewCommand(testPluginCmd, serverExpectedFlag, clientExpectedFlag, modifierExpectedFlag)
}
func testPluginFunc(ccmd *cobra.Command, args []string) {
if len(args) == 0 {
ccmd.Println("At least one plugin is required.")
os.Exit(1)
}
if !serverExpected && !clientExpected && !modifierExpected {
ccmd.Println("You must declare the expected type of the plugin.")
os.Exit(1)
}
start := time.Now()
ctx, cancel := context.WithCancel(context.Background())
var failed int
globalOK := true
for _, pluginPath := range args {
f, err := os.Open(pluginPath)
if os.IsNotExist(err) {
ccmd.Println(fmt.Sprintf("[KO] Unable to open the plugin %s.", pluginPath))
failed++
globalOK = false
continue
}
f.Close()
name := filepath.Base(pluginPath)
folder := filepath.Dir(pluginPath)
ok := true
if serverExpected {
ok = checkHandlerPlugin(ccmd, folder, name) && ok
}
if modifierExpected {
ok = checkModifierPlugin(ctx, ccmd, folder, name) && ok
}
if clientExpected {
ok = checkClientPlugin(ccmd, folder, name) && ok
}
if !ok {
failed++
}
globalOK = globalOK && ok
}
cancel()
if !globalOK {
ccmd.Println(fmt.Sprintf("[KO] %d tested plugin(s) in %s.\n%d plugin(s) failed.", len(args), time.Since(start), failed))
os.Exit(1)
}
ccmd.Println(fmt.Sprintf("[OK] %d tested plugin(s) in %s", len(args), time.Since(start)))
}
func checkClientPlugin(ccmd *cobra.Command, folder, name string) bool {
_, err := client.LoadWithLogger(
folder,
name,
client.RegisterClient,
logging.NoOp,
)
if err == nil {
ccmd.Println(fmt.Sprintf("[OK] CLIENT\t%s", name))
return true
}
var msg string
if mErrs, ok := err.(pluginLoaderErr); ok {
for _, err := range mErrs.Errs() {
msg += err.Error()
}
} else {
msg = err.Error()
}
if strings.Contains(msg, "symbol ClientRegisterer not found") {
ccmd.Println(fmt.Sprintf("[KO] CLIENT\t%s: The plugin does not contain a ClientRegisterer.", name))
return false
}
for _, match := range reLogErrorPlugins.FindAllStringSubmatch(msg, -1) {
msg = match[1]
}
ccmd.Println(fmt.Sprintf("[KO] CLIENT\t%s: %s", name, msg))
return false
}
func checkHandlerPlugin(ccmd *cobra.Command, folder, name string) bool {
_, err := server.LoadWithLogger(
folder,
name,
server.RegisterHandler,
logging.NoOp,
)
if err == nil {
ccmd.Println(fmt.Sprintf("[OK] SERVER\t%s", name))
return true
}
var msg string
if mErrs, ok := err.(pluginLoaderErr); ok {
for _, err := range mErrs.Errs() {
msg += err.Error()
}
} else {
msg = err.Error()
}
if strings.Contains(msg, "symbol HandlerRegisterer not found") {
ccmd.Println(fmt.Sprintf("[KO] SERVER\t%s: The plugin does not contain a HandlerRegisterer.", name))
return false
}
for _, match := range reLogErrorPlugins.FindAllStringSubmatch(msg, -1) {
msg = match[1]
}
ccmd.Println(fmt.Sprintf("[KO] SERVER\t%s: %s", name, msg))
return false
}
func checkModifierPlugin(ctx context.Context, ccmd *cobra.Command, folder, name string) bool {
_, err := proxy.LoadWithLoggerAndContext(
ctx,
folder,
name,
proxy.RegisterModifier,
logging.NoOp,
)
if err == nil {
ccmd.Println(fmt.Sprintf("[OK] MODIFIER\t%s", name))
return true
}
var msg string
if mErrs, ok := err.(pluginLoaderErr); ok {
for _, err := range mErrs.Errs() {
msg += err.Error()
}
} else {
msg = err.Error()
}
if strings.Contains(msg, "symbol ModifierRegisterer not found") {
ccmd.Println(fmt.Sprintf("[KO] MODIFIER\t%s: The plugin does not contain a ModifierRegisterer.", name))
return false
}
for _, match := range reLogErrorPlugins.FindAllStringSubmatch(msg, -1) {
msg = match[1]
}
ccmd.Println(fmt.Sprintf("[KO] MODIFIER\t%s: %s", name, msg))
return false
}