-
Notifications
You must be signed in to change notification settings - Fork 90
/
table.go
125 lines (107 loc) · 3.55 KB
/
table.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
package provider
import (
"crypto/sha256"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"plugin"
"strings"
)
type CloudProvider interface {
ParseCmd(flags *flag.FlagSet)
LoadEnv()
NewProvider() (Provider, error)
}
var providerTable map[string]CloudProvider = make(map[string]CloudProvider)
func getFileNameAndSha256sum(providerPath string) (string, string, error) {
file, err := os.Open(providerPath)
if err != nil {
return "", "", err
}
defer file.Close()
// Get the base filename without the directory path
filename := filepath.Base(providerPath)
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
return filename, "", err
}
sum := hash.Sum(nil)
return filename, fmt.Sprintf("%x", sum), nil
}
func hasExecutePermission(providerPath string) (bool, error) {
// Get the parent directory of the specified file path
dir := filepath.Dir(providerPath)
// Stat the directory to get its file info
dirInfo, err := os.Stat(dir)
if err != nil {
return false, err
}
// Check if the directory has execute permission for the current user
mode := dirInfo.Mode()
executePermission := mode&os.ModeDir != 0 && mode&0100 != 0
return executePermission, nil
}
// LoadCloudProvider loads cloud provider external plugin from the given path CLOUD_PROVIDER_EXTERNAL_PLUGIN_PATH
// The values of 1) ${CLOUD_PROVIDER}, 2) the filename of the cloud provider external plugin
// and 3) the provider defined within the external plugin must all match
func LoadCloudProvider(name string) {
if os.Getenv("ENABLE_CLOUD_PROVIDER_EXTERNAL_PLUGIN") != "true" {
logger.Printf("Cloud provider external plugin loading is disabled, skipping plugin loading")
return
}
externalPluginPath := os.Getenv("CLOUD_PROVIDER_EXTERNAL_PLUGIN_PATH")
executePermission, err := hasExecutePermission(externalPluginPath)
if err != nil {
logger.Printf("Failed to retrieve file information for the parent directory of CLOUD_PROVIDER_EXTERNAL_PLUGIN_PATH %s", err)
return
}
if !executePermission {
logger.Printf("The parent directory of the external plugin %s lacks execute permissions", filepath.Dir(externalPluginPath))
return
}
cloudProviderPluginHash := os.Getenv("CLOUD_PROVIDER_EXTERNAL_PLUGIN_HASH")
if externalPluginPath == "" {
logger.Printf("Env CLOUD_PROVIDER_EXTERNAL_PLUGIN_PATH is not set")
return
}
if cloudProviderPluginHash == "" {
logger.Printf("Env CLOUD_PROVIDER_EXTERNAL_PLUGIN_HASH is not set")
return
}
filename, realPluginHash, err := getFileNameAndSha256sum(externalPluginPath)
if !strings.EqualFold(filename, name+".so") {
logger.Printf("Filename of the external plugin: %s, is not match with CLOUD_PROVIDER: %s", filename, name)
return
}
logger.Printf("Loading external plugin %s from %s", name, externalPluginPath)
if err != nil {
logger.Printf("Failed to calculate the SHA256 checksum of the external plugin %s", err)
return
}
if cloudProviderPluginHash != realPluginHash {
logger.Printf("The sha256sum of the external plugin: %s doesn't match the one from configmap: %s", realPluginHash, cloudProviderPluginHash)
return
}
_, err = plugin.Open(externalPluginPath)
if err != nil {
logger.Printf("Failed to open the external plugin %s", err)
} else {
logger.Printf("Successfully opened the external plugin %s", externalPluginPath)
}
}
func Get(name string) CloudProvider {
LoadCloudProvider(name)
return providerTable[name]
}
func AddCloudProvider(name string, cloud CloudProvider) {
providerTable[name] = cloud
}
func List() []string {
var list []string
for name := range providerTable {
list = append(list, name)
}
return list
}