forked from Consensys/quorum
/
settings.go
290 lines (261 loc) · 8.35 KB
/
settings.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package plugin
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"github.com/ethereum/go-ethereum/plugin/account"
"github.com/ethereum/go-ethereum/plugin/helloworld"
"github.com/ethereum/go-ethereum/plugin/security"
"github.com/ethereum/go-ethereum/rpc"
"github.com/hashicorp/go-plugin"
"github.com/naoina/toml"
)
const (
HelloWorldPluginInterfaceName = PluginInterfaceName("helloworld") // lower-case always
SecurityPluginInterfaceName = PluginInterfaceName("security")
AccountPluginInterfaceName = PluginInterfaceName("account")
)
var (
// define additional plugins being supported here
pluginProviders = map[PluginInterfaceName]pluginProvider{
HelloWorldPluginInterfaceName: {
apiProviderFunc: func(ns string, pm *PluginManager) ([]rpc.API, error) {
template := new(HelloWorldPluginTemplate)
if err := pm.GetPluginTemplate(HelloWorldPluginInterfaceName, template); err != nil {
return nil, err
}
service, err := template.Get()
if err != nil {
return nil, err
}
return []rpc.API{{
Namespace: ns,
Version: "1.0.0",
Service: service,
Public: true,
}}, nil
},
pluginSet: plugin.PluginSet{
helloworld.ConnectorName: &helloworld.PluginConnector{},
},
},
SecurityPluginInterfaceName: {
pluginSet: plugin.PluginSet{
security.TLSConfigurationConnectorName: &security.TLSConfigurationSourcePluginConnector{},
security.AuthenticationConnectorName: &security.AuthenticationManagerPluginConnector{},
},
},
AccountPluginInterfaceName: {
apiProviderFunc: func(ns string, pm *PluginManager) ([]rpc.API, error) {
f := new(ReloadableAccountServiceFactory)
if err := pm.GetPluginTemplate(AccountPluginInterfaceName, f); err != nil {
return nil, err
}
service, err := f.Create()
if err != nil {
return nil, err
}
return []rpc.API{{
Namespace: ns,
Version: "1.0.0",
Service: account.NewCreator(service),
Public: true,
}}, nil
},
pluginSet: plugin.PluginSet{
account.ConnectorName: &account.PluginConnector{},
},
},
}
// this is the place holder for future solution of the plugin central
quorumPluginCentralConfiguration = &PluginCentralConfiguration{
CertFingerprint: "",
BaseURL: "https://dl.bintray.com/quorumengineering/quorum-plugins",
PublicKeyURI: "/.pgp/" + DefaultPublicKeyFile,
InsecureSkipTLSVerify: false,
}
)
type pluginProvider struct {
// this allows exposing plugin interfaces to geth RPC API automatically.
// nil value implies that plugin won't expose its methods to geth RPC API
apiProviderFunc rpcAPIProviderFunc
// contains connectors being registered to the plugin library
pluginSet plugin.PluginSet
}
type rpcAPIProviderFunc func(ns string, pm *PluginManager) ([]rpc.API, error)
type Version string
// This is to describe a plugin
//
// Information is used to discover the plugin binary and verify its integrity
// before forking a process running the plugin
type PluginDefinition struct {
Name string `json:"name" toml:""`
// the semver version of the plugin
Version Version `json:"version" toml:""`
// plugin configuration in a form of map/slice/string
Config interface{} `json:"config,omitempty" toml:",omitempty"`
}
func ReadMultiFormatConfig(config interface{}) ([]byte, error) {
if config == nil {
return []byte{}, nil
}
switch k := reflect.TypeOf(config).Kind(); k {
case reflect.Map, reflect.Slice:
return json.Marshal(config)
case reflect.String:
configStr := config.(string)
u, err := url.Parse(configStr)
if err != nil { // just return as is
return []byte(configStr), nil
}
switch s := u.Scheme; s {
case "file":
return ioutil.ReadFile(filepath.Join(u.Host, u.Path))
case "env": // config string in an env variable
varName := u.Host
isFile := u.Query().Get("type") == "file"
if v, ok := os.LookupEnv(varName); ok {
if isFile {
return ioutil.ReadFile(v)
} else {
return []byte(v), nil
}
} else {
return nil, fmt.Errorf("env variable %s not found", varName)
}
default:
return []byte(configStr), nil
}
default:
return nil, fmt.Errorf("unsupported type of config [%s]", k)
}
}
// return remote folder storing the plugin distribution file and signature file
//
// e.g.: my-plugin/v1.0.0/darwin-amd64
func (m *PluginDefinition) RemotePath() string {
return fmt.Sprintf("%s/v%s/%s-%s", m.Name, m.Version, runtime.GOOS, runtime.GOARCH)
}
// return plugin name and version
func (m *PluginDefinition) FullName() string {
return fmt.Sprintf("%s-%s", m.Name, m.Version)
}
// return plugin distribution file name
func (m *PluginDefinition) DistFileName() string {
return fmt.Sprintf("%s.zip", m.FullName())
}
// return plugin distribution signature file name
func (m *PluginDefinition) SignatureFileName() string {
return fmt.Sprintf("%s.sha256sum.asc", m.DistFileName())
}
// must be always be lowercase when define constants
// as when unmarshaling from config, value will be case-lowered
type PluginInterfaceName string
// When this is used as a key in map. This function is not invoked.
func (p *PluginInterfaceName) UnmarshalJSON(data []byte) error {
var v string
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*p = PluginInterfaceName(strings.ToLower(v))
return nil
}
func (p *PluginInterfaceName) UnmarshalTOML(data []byte) error {
var v string
if err := toml.Unmarshal(data, &v); err != nil {
return err
}
*p = PluginInterfaceName(strings.ToLower(v))
return nil
}
func (p *PluginInterfaceName) UnmarshalText(data []byte) error {
*p = PluginInterfaceName(strings.ToLower(string(data)))
return nil
}
func (p PluginInterfaceName) String() string {
return string(p)
}
// this defines plugins used in the geth node
type Settings struct {
BaseDir EnvironmentAwaredValue `json:"baseDir" toml:""`
CentralConfig *PluginCentralConfiguration `json:"central" toml:"Central"`
Providers map[PluginInterfaceName]PluginDefinition `json:"providers" toml:""`
}
func (s *Settings) GetPluginDefinition(name PluginInterfaceName) (*PluginDefinition, bool) {
m, ok := s.Providers[name]
return &m, ok
}
func (s *Settings) SetDefaults() {
if s.CentralConfig == nil {
s.CentralConfig = quorumPluginCentralConfiguration
}
}
// CheckSettingsAreSupported validates Settings by ensuring that only supportedPlugins are defined.
// It is not required for all supportedPlugins to be defined.
// An error containing plugin details is returned if one or more unsupported plugins are defined.
func (s *Settings) CheckSettingsAreSupported(supportedPlugins []PluginInterfaceName) error {
errList := []PluginInterfaceName{}
for name := range s.Providers {
isValid := false
for _, supportedPlugin := range supportedPlugins {
if supportedPlugin == name {
isValid = true
break
}
}
if !isValid {
errList = append(errList, name)
}
}
if len(errList) != 0 {
return fmt.Errorf("unsupported plugins configured: %v", errList)
}
return nil
}
type PluginCentralConfiguration struct {
// To implement certificate pinning while communicating with PluginCentral
// if it's empty, we skip cert pinning logic
CertFingerprint string `json:"certFingerprint" toml:""`
BaseURL string `json:"baseURL" toml:""`
PublicKeyURI string `json:"publicKeyURI" toml:""`
InsecureSkipTLSVerify bool `json:"insecureSkipTLSVerify" toml:""`
}
// support URI format with 'env' scheme during JSON/TOML/TEXT unmarshalling
// e.g.: env://FOO_VAR means read a string value from FOO_VAR environment variable
type EnvironmentAwaredValue string
func (d *EnvironmentAwaredValue) UnmarshalJSON(data []byte) error {
return d.unmarshal(data)
}
func (d *EnvironmentAwaredValue) UnmarshalTOML(data []byte) error {
return d.unmarshal(data)
}
func (d *EnvironmentAwaredValue) UnmarshalText(data []byte) error {
return d.unmarshal(data)
}
func (d *EnvironmentAwaredValue) unmarshal(data []byte) error {
v := string(data)
isString := strings.HasPrefix(v, "\"") && strings.HasSuffix(v, "\"")
if !isString {
return fmt.Errorf("not a string")
}
v = strings.TrimFunc(v, func(r rune) bool {
return r == '"'
})
if u, err := url.Parse(v); err == nil {
switch u.Scheme {
case "env":
v = os.Getenv(u.Host)
}
}
*d = EnvironmentAwaredValue(v)
return nil
}
func (d EnvironmentAwaredValue) String() string {
return string(d)
}