/
registry.go
304 lines (254 loc) · 7.97 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package install
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/BurntSushi/toml"
"github.com/blang/semver/v4"
plugin "github.com/gideaworx/terraform-exporter-plugin-go"
"github.com/gideaworx/terraform-exporter-plugin-registry/registry"
localreg "github.com/gideaworx/terraform-exporter/registry"
"github.com/gideaworx/terraform-exporter/runner"
)
func (i *Command) registryInstall() error {
reg := i.r.Get(i.Registry)
if reg == nil {
return fmt.Errorf("no plugin %q found", i.Registry)
}
if err := reg.LazyLoad(); err != nil {
return err
}
var plugin registry.Plugin
var version registry.PluginVersion
found := false
for _, p := range reg.Plugins {
if p.Name == i.PluginName {
plugin = p
if strings.EqualFold(strings.TrimSpace(i.PluginVersion), "") {
found = true
break
}
for _, v := range p.Versions {
if v.Version == i.PluginVersion {
version = v
found = true
break
}
}
}
}
if !found {
return fmt.Errorf("could not find version %q of plugin %q in registry %q", i.PluginVersion, i.PluginName, i.Registry)
}
if strings.EqualFold(strings.TrimSpace(i.PluginVersion), "") {
sort.Slice(plugin.Versions, func(i, j int) bool {
si, _ := semver.ParseTolerant(plugin.Versions[i].Version)
sj, _ := semver.ParseTolerant(plugin.Versions[j].Version)
return si.GT(sj)
})
version = plugin.Versions[0]
}
exe := i.getPluginExecutable(version)
if exe.Locator == "" {
return fmt.Errorf("plugin %s, version %s is not compatible with architecture %s/%s", plugin.Name, version.Version, runtime.GOOS, runtime.GOARCH)
}
pluginDir := filepath.Join(i.pluginHomeDir, i.PluginName)
if err := os.MkdirAll(pluginDir, 0777); err != nil {
return err
}
var installer func(string, registry.PluginExecutable, *localreg.PluginRegistry, string) (runner.BillOfMaterials, error)
switch exe.Type {
case registry.Native:
installer = i.installNative
case registry.NodeJS:
installer = i.installNPM
case registry.Python:
installer = i.installPyPI
// case registry.Java:
// return i.installMaven(exe, reg, version.Version)
default:
return fmt.Errorf("unknown type %s", exe.Type)
}
bom, err := installer(pluginDir, exe, reg, version.Version)
if err != nil {
return err
}
bomFile, err := os.Create(filepath.Join(pluginDir, "export-plugin.bom"))
if err != nil {
return err
}
defer bomFile.Close()
return toml.NewEncoder(bomFile).Encode(bom)
}
func (i *Command) getPluginExecutable(v registry.PluginVersion) registry.PluginExecutable {
targetArch := registry.TargetArchitecture(fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
if exe, ok := v.DownloadInfo[targetArch]; ok {
return exe
}
if maExe, ok := v.DownloadInfo[registry.MultiArch]; ok {
return maExe
}
// if we're on darwin/arm64, we can try darwin/amd64 and let Rosetta take the wheel
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
return v.DownloadInfo[registry.DarwinAmd64]
}
return registry.PluginExecutable{}
}
func (i *Command) installNative(pluginDir string, exe registry.PluginExecutable, reg *localreg.PluginRegistry, version string) (runner.BillOfMaterials, error) {
targetFile, err := os.OpenFile(filepath.Join(pluginDir, "export-plugin"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0777)
if err != nil {
return runner.BillOfMaterials{}, err
}
defer targetFile.Close()
hasher := sha256.New()
writer := io.MultiWriter(targetFile, hasher)
resp, err := runner.GetHTTPClient().Get(exe.Locator)
if err != nil {
return runner.BillOfMaterials{}, err
}
defer resp.Body.Close()
if _, err = io.Copy(writer, resp.Body); err != nil {
return runner.BillOfMaterials{}, err
}
targetFile.Close()
calculatedSHASum := hex.EncodeToString(hasher.Sum(nil))
if exe.Info.Checksum != "" && exe.Info.Checksum != calculatedSHASum {
return runner.BillOfMaterials{}, fmt.Errorf("calculated sha256 checksum %q does not match provided checksum %q", calculatedSHASum, exe.Info.Checksum)
}
integrity := &runner.PluginIntegrity{
Checksum: calculatedSHASum,
Algorithm: "sha256",
}
info, err := runner.LoadPluginInfo(i.PluginName, integrity)
if err != nil {
return runner.BillOfMaterials{}, err
}
bom := runner.BillOfMaterials{
Name: i.PluginName,
Version: plugin.FromString(version),
Type: runner.Native,
Source: runner.PluginSource{
Type: "registry",
Name: i.Registry,
URL: reg.URL.String(),
},
Integrity: integrity,
Provides: info.Provides,
}
return bom, nil
}
func (i *Command) installPyPI(pluginDir string, exe registry.PluginExecutable, reg *localreg.PluginRegistry, version string) (runner.BillOfMaterials, error) {
var (
pythonPath string
err error
)
if pythonPath, err = exec.LookPath("python3"); err != nil {
if pythonPath, err = exec.LookPath("python"); err != nil {
return runner.BillOfMaterials{}, errors.New(`could not find "python" or "python3" on the system PATH`)
}
}
ensurePipCmd := exec.Command(pythonPath, "-m", "ensurepip")
ensurePipCmd.Stdout = i.out
ensurePipCmd.Stderr = i.err
if err = ensurePipCmd.Run(); err != nil {
return runner.BillOfMaterials{}, fmt.Errorf("could not ensure pip is installed: %w", err)
}
pipArgs := append([]string{"-m", "pip", "install", "--target", pluginDir, fmt.Sprintf("%s==%s", exe.Locator, version)}, exe.Info.ExtraArgs...)
pipInstaller := exec.Command(pythonPath, pipArgs...)
pipInstaller.Stdout = i.out
pipInstaller.Stderr = i.err
if err := pipInstaller.Run(); err != nil {
return runner.BillOfMaterials{}, err
}
if err = os.Symlink(filepath.Join(pluginDir, "bin", "export-plugin"), filepath.Join(pluginDir, "export-plugin")); err != nil {
return runner.BillOfMaterials{}, err
}
hasher := sha256.New()
contents, err := os.ReadFile(filepath.Join(pluginDir, "bin", "export-plugin"))
if err != nil {
return runner.BillOfMaterials{}, err
}
checksum := hasher.Sum(contents)
integrity := &runner.PluginIntegrity{
Checksum: hex.EncodeToString(checksum),
Algorithm: "sha256",
}
info, err := runner.LoadPluginInfo(i.PluginName, integrity)
if err != nil {
return runner.BillOfMaterials{}, err
}
return runner.BillOfMaterials{
Name: i.PluginName,
Version: info.Version,
Type: runner.Python,
Source: runner.PluginSource{
Type: "registry",
Name: i.Registry,
URL: reg.URL.String(),
},
Integrity: integrity,
Provides: info.Provides,
}, nil
}
func (i *Command) installNPM(pluginDir string, exe registry.PluginExecutable, reg *localreg.PluginRegistry, version string) (runner.BillOfMaterials, error) {
var (
npmPath string
bom runner.BillOfMaterials
err error
)
if _, err = exec.LookPath("node"); err != nil {
return bom, err
}
if npmPath, err = exec.LookPath("npm"); err != nil {
return bom, err
}
nmDir := filepath.Join(pluginDir, "node_modules")
if err = os.MkdirAll(nmDir, 0o755); err != nil {
return bom, err
}
npmArgs := append([]string{"install", "--save", fmt.Sprintf("%s@%s", exe.Locator, version)}, exe.Info.ExtraArgs...)
cmd := exec.Command(npmPath, npmArgs...)
cmd.Stdout = i.out
cmd.Stderr = i.err
cmd.Dir = filepath.Dir(nmDir)
if err := cmd.Run(); err != nil {
return bom, err
}
if err = os.Symlink(filepath.Join(nmDir, ".bin", "export-plugin"), filepath.Join(pluginDir, "export-plugin")); err != nil {
return bom, err
}
hasher := sha256.New()
contents, err := os.ReadFile(filepath.Join(nmDir, ".bin", "export-plugin"))
if err != nil {
return runner.BillOfMaterials{}, err
}
checksum := hasher.Sum(contents)
integrity := &runner.PluginIntegrity{
Checksum: hex.EncodeToString(checksum),
Algorithm: "sha256",
}
info, err := runner.LoadPluginInfo(i.PluginName, integrity)
if err != nil {
return runner.BillOfMaterials{}, err
}
return runner.BillOfMaterials{
Name: i.PluginName,
Version: info.Version,
Type: runner.NodeJS,
Source: runner.PluginSource{
Type: "registry",
Name: i.Registry,
URL: reg.URL.String(),
},
Integrity: integrity,
Provides: info.Provides,
}, nil
}