-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.go
More file actions
289 lines (251 loc) · 7.71 KB
/
installer.go
File metadata and controls
289 lines (251 loc) · 7.71 KB
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
package plugin
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/GoCodeAlone/workflow/dynamic"
)
// PluginInstaller handles installing plugins from remote or local sources.
type PluginInstaller struct {
remoteReg *RemoteRegistry
localReg *LocalRegistry
loader *dynamic.Loader
installDir string
}
// NewPluginInstaller creates a new plugin installer.
func NewPluginInstaller(remoteReg *RemoteRegistry, localReg *LocalRegistry, loader *dynamic.Loader, installDir string) *PluginInstaller {
return &PluginInstaller{
remoteReg: remoteReg,
localReg: localReg,
loader: loader,
installDir: installDir,
}
}
// Install downloads and installs a plugin from the remote registry.
func (i *PluginInstaller) Install(ctx context.Context, name, version string) error {
if i.IsInstalled(name) {
return nil // already installed
}
if i.remoteReg == nil {
return fmt.Errorf("no remote registry configured")
}
// Get manifest from remote
manifest, err := i.remoteReg.GetManifest(ctx, name, version)
if err != nil {
return fmt.Errorf("get manifest for %s@%s: %w", name, version, err)
}
// Validate install directory to prevent directory traversal
absInstallDir, err := filepath.Abs(i.installDir)
if err != nil {
return fmt.Errorf("resolve install directory: %w", err)
}
pluginDir := filepath.Join(absInstallDir, name)
absPluginDir, err := filepath.Abs(pluginDir)
if err != nil {
return fmt.Errorf("resolve plugin directory: %w", err)
}
if !strings.HasPrefix(absPluginDir, absInstallDir+string(os.PathSeparator)) {
return fmt.Errorf("invalid plugin name %q", name)
}
if err := os.MkdirAll(pluginDir, 0750); err != nil {
return fmt.Errorf("create plugin dir: %w", err)
}
// Download archive from remote registry
reader, err := i.remoteReg.Download(ctx, name, version)
if err != nil {
os.RemoveAll(pluginDir) // cleanup on failure
return fmt.Errorf("download plugin %s@%s: %w", name, version, err)
}
defer reader.Close()
// Save archive to disk
archivePath := filepath.Join(pluginDir, fmt.Sprintf("%s-%s.tar.gz", name, version))
f, err := os.Create(archivePath)
if err != nil {
os.RemoveAll(pluginDir)
return fmt.Errorf("create archive file: %w", err)
}
if _, err := io.Copy(f, reader); err != nil {
f.Close()
os.RemoveAll(pluginDir)
return fmt.Errorf("save archive: %w", err)
}
f.Close()
// Extract archive — failure is non-fatal if we have the manifest
_ = extractTarGz(archivePath, pluginDir)
// Save manifest
manifestPath := filepath.Join(pluginDir, "plugin.json")
if err := SaveManifest(manifestPath, manifest); err != nil {
os.RemoveAll(pluginDir)
return fmt.Errorf("save manifest: %w", err)
}
// Register in local registry (without a component -- loaded separately)
if i.localReg != nil {
if err := i.localReg.Register(manifest, nil, pluginDir); err != nil {
return fmt.Errorf("register installed plugin: %w", err)
}
}
return nil
}
// InstallFromBundle installs a plugin from a local bundle directory.
// The bundle directory must contain a plugin.json manifest.
func (i *PluginInstaller) InstallFromBundle(bundlePath string) error {
// Read plugin manifest
manifestPath := filepath.Join(bundlePath, "plugin.json")
manifest, err := LoadManifest(manifestPath)
if err != nil {
return fmt.Errorf("load bundle manifest: %w", err)
}
info, err := os.Stat(bundlePath)
if err != nil {
return fmt.Errorf("stat bundle: %w", err)
}
if !info.IsDir() {
return fmt.Errorf("bundle path must be a directory")
}
destDir := filepath.Join(i.installDir, manifest.Name)
// Validate destination to prevent directory traversal
absInstallDir, err := filepath.Abs(i.installDir)
if err != nil {
return fmt.Errorf("resolve install directory: %w", err)
}
absDestDir, err := filepath.Abs(destDir)
if err != nil {
return fmt.Errorf("resolve destination directory: %w", err)
}
if !strings.HasPrefix(absDestDir, absInstallDir+string(os.PathSeparator)) {
return fmt.Errorf("invalid plugin name %q", manifest.Name)
}
if err := copyDir(bundlePath, destDir); err != nil {
return fmt.Errorf("copy plugin bundle: %w", err)
}
// Register in local registry
if i.localReg != nil {
// Attempt to load component via dynamic loader
var comp *dynamic.DynamicComponent
if i.loader != nil {
sourceFiles, _ := filepath.Glob(filepath.Join(destDir, "*.go"))
for _, sf := range sourceFiles {
base := filepath.Base(sf)
if strings.HasSuffix(base, "_test.go") {
continue
}
c, loadErr := i.loader.LoadFromFile(manifest.Name, sf)
if loadErr == nil {
comp = c
break
}
}
}
if err := i.localReg.Register(manifest, comp, destDir); err != nil {
return fmt.Errorf("register plugin %q: %w", manifest.Name, err)
}
}
return nil
}
// IsInstalled checks if a plugin is installed locally.
func (i *PluginInstaller) IsInstalled(name string) bool {
pluginDir := filepath.Join(i.installDir, name)
manifestPath := filepath.Join(pluginDir, "plugin.json")
_, err := os.Stat(manifestPath)
return err == nil
}
// Uninstall removes an installed plugin.
func (i *PluginInstaller) Uninstall(name string) error {
pluginDir := filepath.Join(i.installDir, name)
if _, err := os.Stat(pluginDir); os.IsNotExist(err) {
return fmt.Errorf("plugin %s not installed", name)
}
// Unregister from local registry
if i.localReg != nil {
_ = i.localReg.Unregister(name) // best-effort
}
return os.RemoveAll(pluginDir)
}
// ScanInstalled loads all previously installed plugins from the install directory.
func (i *PluginInstaller) ScanInstalled() ([]*PluginEntry, error) {
if i.localReg == nil {
return nil, nil
}
if _, err := os.Stat(i.installDir); os.IsNotExist(err) {
return nil, nil
}
return i.localReg.ScanDirectory(i.installDir, i.loader)
}
// InstallDir returns the configured plugin installation directory.
func (i *PluginInstaller) InstallDir() string {
return i.installDir
}
func copyDir(src, dst string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
rel, err := filepath.Rel(src, path)
if err != nil {
return err
}
destPath := filepath.Join(dst, rel)
if info.IsDir() {
return os.MkdirAll(destPath, 0750)
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
return os.WriteFile(destPath, data, info.Mode())
})
}
// extractTarGz extracts a .tar.gz archive into a destination directory.
func extractTarGz(archivePath, destDir string) error {
f, err := os.Open(archivePath)
if err != nil {
return err
}
defer f.Close()
gzr, err := gzip.NewReader(f)
if err != nil {
return err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
target := filepath.Join(destDir, header.Name) //nolint:gosec // G305: path traversal validated below
// Prevent path traversal (CWE-22)
if !strings.HasPrefix(filepath.Clean(target), filepath.Clean(destDir)+string(os.PathSeparator)) {
return fmt.Errorf("illegal file path in archive: %s", header.Name)
}
switch header.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(target, 0750); err != nil { //nolint:gosec // G703: target validated against destDir above
return err
}
case tar.TypeReg:
if err := os.MkdirAll(filepath.Dir(target), 0750); err != nil { //nolint:gosec // G703: target validated against destDir above
return err
}
out, err := os.Create(target) //nolint:gosec // G703: target validated against destDir above
if err != nil {
return err
}
// Limit copy to 100MB to prevent decompression bombs
if _, err := io.Copy(out, io.LimitReader(tr, 100*1024*1024)); err != nil {
out.Close()
return err
}
out.Close()
}
}
return nil
}