-
Notifications
You must be signed in to change notification settings - Fork 469
/
cipher_manager.go
executable file
·57 lines (49 loc) · 1.29 KB
/
cipher_manager.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
package security
import (
"errors"
"fmt"
"os"
"github.com/go-chassis/foundation/security"
"github.com/go-chassis/go-chassis/pkg/goplugin"
"github.com/go-mesh/openlogging"
)
const pluginSuffix = ".so"
//CipherPlugins is a map
var cipherPlugins map[string]func() security.Cipher
//InstallCipherPlugin is a function
func InstallCipherPlugin(name string, f func() security.Cipher) {
cipherPlugins[name] = f
}
//GetCipherNewFunc is a function
func GetCipherNewFunc(name string) (func() security.Cipher, error) {
if f, ok := cipherPlugins[name]; ok {
return f, nil
}
openlogging.GetLogger().Debugf("try to load cipher [%s] from go plugin", name)
f, err := loadCipherFromPlugin(name)
if err == nil {
cipherPlugins[name] = f
return f, nil
}
if !os.IsNotExist(err) {
return nil, err
}
return nil, fmt.Errorf("unknown cipher plugin [%s]", name)
}
func loadCipherFromPlugin(name string) (func() security.Cipher, error) {
c, err := goplugin.LookUpSymbolFromPlugin(name+pluginSuffix, "Cipher")
if err != nil {
return nil, err
}
customCipher, ok := c.(security.Cipher)
if !ok {
return nil, errors.New("symbol from plugin is not type Cipher")
}
f := func() security.Cipher {
return customCipher
}
return f, nil
}
func init() {
cipherPlugins = make(map[string]func() security.Cipher)
}