forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
185 lines (156 loc) · 4.88 KB
/
registry.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
/*
Copyright IBM Corp, SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package library
import (
"fmt"
"os"
"plugin"
"reflect"
"sync"
"github.com/hyperledger/fabric/core/handlers/auth"
"github.com/hyperledger/fabric/core/handlers/decoration"
)
// Registry defines an object that looks up
// handlers by name
type Registry interface {
// Lookup returns a handler with a given
// registered name, or nil if does not exist
Lookup(HandlerType) interface{}
}
// HandlerType defines custom handlers that can filter and mutate
// objects passing within the peer
type HandlerType int
const (
// Auth handler - reject or forward proposals from clients
Auth HandlerType = iota
// Decoration handler - append or mutate the chaincode input
// passed to the chaincode
Decoration
authPluginFactory = "NewFilter"
decoratorPluginFactory = "NewDecorator"
)
type registry struct {
filters []auth.Filter
decorators []decoration.Decorator
}
var once sync.Once
var reg registry
// Config configures the factory methods
// and plugins for the registry
type Config struct {
AuthFilters []*HandlerConfig `mapstructure:"authFilters" yaml:"authFilters"`
Decorators []*HandlerConfig `mapstructure:"decorators" yaml:"decorators"`
}
// HandlerConfig defines configuration for a plugin or compiled handler
type HandlerConfig struct {
Name string `mapstructure:"name" yaml:"name"`
Library string `mapstructure:"library" yaml:"library"`
}
// InitRegistry creates the (only) instance
// of the registry
func InitRegistry(c Config) Registry {
once.Do(func() {
reg = registry{}
reg.loadHandlers(c)
})
return ®
}
// loadHandlers loads the configured handlers
func (r *registry) loadHandlers(c Config) {
for _, config := range c.AuthFilters {
r.evaluateModeAndLoad(config, Auth)
}
for _, config := range c.Decorators {
r.evaluateModeAndLoad(config, Decoration)
}
}
// evaluateModeAndLoad if a library path is provided, load the shared object
func (r *registry) evaluateModeAndLoad(c *HandlerConfig, handlerType HandlerType) {
if c.Library != "" {
r.loadPlugin(c.Library, handlerType)
} else {
r.loadCompiled(c.Name, handlerType)
}
}
// loadCompiled loads a statically compiled handler
func (r *registry) loadCompiled(handlerFactory string, handlerType HandlerType) {
registryMD := reflect.ValueOf(&HandlerLibrary{})
o := registryMD.MethodByName(handlerFactory)
if !o.IsValid() {
panic(fmt.Errorf("Method %s isn't a method of HandlerLibrary", handlerFactory))
}
inst := o.Call(nil)[0].Interface()
if handlerType == Auth {
r.filters = append(r.filters, inst.(auth.Filter))
} else if handlerType == Decoration {
r.decorators = append(r.decorators, inst.(decoration.Decorator))
}
}
// loadPlugin loads a pluggagle handler
func (r *registry) loadPlugin(pluginPath string, handlerType HandlerType) {
if _, err := os.Stat(pluginPath); err != nil {
panic(fmt.Errorf("Could not find plugin at path %s: %s", pluginPath, err))
}
p, err := plugin.Open(pluginPath)
if err != nil {
panic(fmt.Errorf("Error opening plugin at path %s: %s", pluginPath, err))
}
if handlerType == Auth {
r.initAuthPlugin(p)
} else if handlerType == Decoration {
r.initDecoratorPlugin(p)
}
}
// initAuthPlugin constructs an auth filter from the given plugin
func (r *registry) initAuthPlugin(p *plugin.Plugin) {
constructorSymbol, err := p.Lookup(authPluginFactory)
if err != nil {
panicWithLookupError(authPluginFactory, err)
}
constructor, ok := constructorSymbol.(func() auth.Filter)
if !ok {
panicWithDefinitionError(authPluginFactory)
}
filter := constructor()
if filter != nil {
r.filters = append(r.filters, filter)
}
}
// initDecoratorPlugin constructs a decorator from the given plugin
func (r *registry) initDecoratorPlugin(p *plugin.Plugin) {
constructorSymbol, err := p.Lookup(decoratorPluginFactory)
if err != nil {
panicWithLookupError(decoratorPluginFactory, err)
}
constructor, ok := constructorSymbol.(func() decoration.Decorator)
if !ok {
panicWithDefinitionError(decoratorPluginFactory)
}
decorator := constructor()
if decorator != nil {
r.decorators = append(r.decorators, constructor())
}
}
// panicWithLookupError panics when a handler constructor lookup fails
func panicWithLookupError(factory string, err error) {
panic(fmt.Errorf("Filter must contain constructor with name %s. Error from lookup: %s",
factory, err))
}
// panicWithDefinitionError panics when a handler constructor does not match
// the expected function definition
func panicWithDefinitionError(factory string) {
panic(fmt.Errorf("Constructor method %s does not match expected definition",
authPluginFactory))
}
// Lookup returns a list of handlers with the given
// given type, or nil if none exist
func (r *registry) Lookup(handlerType HandlerType) interface{} {
if handlerType == Auth {
return r.filters
} else if handlerType == Decoration {
return r.decorators
}
return nil
}