forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whitelist.go
62 lines (50 loc) · 1.16 KB
/
whitelist.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
// Package whitelist is a micro plugin for whitelisting service requests
package whitelist
import (
"net/http"
"strings"
"github.com/micro/cli"
"github.com/micro/go-micro/client"
"github.com/micro/micro/plugin"
)
type whitelist struct {
services map[string]bool
}
func (w *whitelist) Flags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "rpc_whitelist",
Usage: "Comma separated whitelist of allowed services for RPC calls",
EnvVar: "RPC_WHITELIST",
},
}
}
func (w *whitelist) Commands() []cli.Command {
return nil
}
func (w *whitelist) Handler() plugin.Handler {
return func(h http.Handler) http.Handler {
return h
}
}
func (w *whitelist) Init(ctx *cli.Context) error {
if whitelist := ctx.String("rpc_whitelist"); len(whitelist) > 0 {
client.DefaultClient = newClient(strings.Split(whitelist, ",")...)
}
return nil
}
func (w *whitelist) String() string {
return "whitelist"
}
func NewPlugin() plugin.Plugin {
return NewRPCWhitelist()
}
func NewRPCWhitelist(services ...string) plugin.Plugin {
list := make(map[string]bool)
for _, service := range services {
list[service] = true
}
return &whitelist{
services: list,
}
}