/
packages.go
317 lines (265 loc) · 7.56 KB
/
packages.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
305
306
307
308
309
310
311
312
313
314
315
316
317
package api
import (
"archive/zip"
"context"
"encoding/json"
"fmt"
graphql "github.com/cli/shurcooL-graphql"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
)
// Packages is a API client for working with Humio packages.
type Packages struct {
client *Client
}
// Packages constructs a Packages API client.
func (c *Client) Packages() *Packages { return &Packages{client: c} }
// ValidationResponse contain the results of a package validation.
type ValidationResponse struct {
InstallationErrors []string `json:"installationErrors"`
ParseErrors []string `json:"parseErrors"`
}
// IsValid returns true if there are no errors in the package
func (resp *ValidationResponse) IsValid() bool {
return (len(resp.InstallationErrors) == 0) && (len(resp.ParseErrors) == 0)
}
// InstalledPackage contain the details of an installed package
type InstalledPackage struct {
ID string
InstalledBy *struct {
Username string
Timestamp string
}
UpdatedBy *struct {
Username string
Timestamp string
}
Source string
AvailableUpdate string
}
func isDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fileInfo.IsDir(), err
}
// Validate checks a package declaration validity against a Humio
// server.
func (p *Packages) Validate(viewName string, absPath string) (*ValidationResponse, error) {
var zipFilePath string
var err error
isDir, err := isDirectory(absPath)
if err != nil {
return nil, err
}
if isDir {
zipFilePath, err = createTempZipFromFolder(absPath)
} else {
zipFilePath = absPath
}
if err != nil {
return nil, err
}
urlPath := "api/v1/packages/analyze?view=" + url.QueryEscape(viewName)
// #nosec G304
fileReader, err := os.Open(zipFilePath)
if err != nil {
return nil, err
}
// #nosec G307
defer fileReader.Close()
response, err := p.client.HTTPRequestContext(context.Background(), "POST", urlPath, fileReader, ZIPContentType)
if err != nil {
return nil, err
}
if response.StatusCode >= 400 {
return nil, fmt.Errorf("bad response. %s", response.Status)
}
var report ValidationResponse
decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&report)
if err != nil {
return nil, err
}
return &report, nil
}
// ListInstalled returns a list of installed packages
func (p *Packages) ListInstalled(viewName string) ([]InstalledPackage, error) {
var query struct {
Repository struct {
InstalledPackages []InstalledPackage
} `graphql:"searchDomain(name: $repositoryName)"`
}
variables := map[string]interface{}{
"repositoryName": graphql.String(viewName),
}
err := p.client.Query(&query, variables)
return query.Repository.InstalledPackages, err
}
// InstallArchive installs a local package (zip file).
func (p *Packages) InstallArchive(viewName string, pathToZip string) (*ValidationResponse, error) {
// #nosec G304
fileReader, err := os.Open(pathToZip)
if err != nil {
return nil, err
}
// #nosec G307
defer fileReader.Close()
urlPath := "api/v1/packages/install?view=" + url.QueryEscape(viewName) + "&overwrite=true"
response, err := p.client.HTTPRequestContext(context.Background(), "POST", urlPath, fileReader, ZIPContentType)
if err != nil {
return nil, err
}
if response.StatusCode >= 400 {
return nil, detailedInstallationError(response)
}
var report ValidationResponse
decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&report)
if err != nil {
return nil, err
}
return &report, nil
}
func detailedInstallationError(response *http.Response) error {
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(response.Body)
body, err := io.ReadAll(response.Body) // response body is []byte
if err != nil {
return fmt.Errorf("the package could not be installed")
}
var result InstallationErrors
if err := json.Unmarshal(body, &result); err != nil {
return fmt.Errorf("the package could not be installed and the reason returned could not be parsed")
}
allErrors := append(result.ParseErrors, result.InstallationErrors...)
formattedErrors := make([]string, len(allErrors))
for i, v := range allErrors {
formattedErrors[i] = "\n - " + v
}
return fmt.Errorf("%s", strings.Join(formattedErrors, ""))
}
type InstallationErrors struct {
InstallationErrors []string `json:"installationErrors"`
ParseErrors []string `json:"parseErrors"`
ResponseType string `json:"responseType"`
}
type (
// VersionedPackageSpecifier is the ID and version of a package, e.g foo/bar@2.0.1
VersionedPackageSpecifier string
// UnversionedPackageSpecifier is the ID of a package, e.g foo/bar
UnversionedPackageSpecifier string
)
// UninstallPackage uninstalls a package by name.
func (p *Packages) UninstallPackage(viewName string, packageID string) error {
var mutation struct {
UninstallPackage struct {
// We have to make a selection, so just take __typename
Typename graphql.String `graphql:"__typename"`
} `graphql:"uninstallPackage(packageId: $packageId, viewName: $viewName)"`
}
variables := map[string]interface{}{
"packageId": UnversionedPackageSpecifier(packageID),
"viewName": graphql.String(viewName),
}
return p.client.Mutate(&mutation, variables)
}
// CreateArchive creates a archive by bundling the files in packageDirPath in a zip file.
func (p *Packages) CreateArchive(packageDirPath string, targetFileName string) error {
// #nosec G304
outFile, err := os.Create(targetFileName)
if err != nil {
return err
}
// #nosec G307
defer outFile.Close()
return createZipFromFolder(packageDirPath, outFile)
}
// InstallFromDirectory installs a package from a directory containing the package files.
func (p *Packages) InstallFromDirectory(packageDirPath string, targetRepoOrView string) (*ValidationResponse, error) {
zipFilePath, err := createTempZipFromFolder(packageDirPath)
if err != nil {
return nil, err
}
// #nosec G304
zipFile, err := os.Open(zipFilePath)
if err != nil {
return nil, err
}
// #nosec G307
defer zipFile.Close()
defer os.Remove(zipFile.Name())
return p.InstallArchive(targetRepoOrView, zipFilePath)
}
func createTempZipFromFolder(baseFolder string) (string, error) {
tempDir := os.TempDir()
zipFile, err := os.CreateTemp(tempDir, "humio-package.*.zip")
if err != nil {
return "", err
}
// #nosec G307
defer zipFile.Close()
err = createZipFromFolder(baseFolder, zipFile)
if err != nil {
return "", err
}
return zipFile.Name(), nil
}
func createZipFromFolder(baseFolder string, outFile *os.File) error {
// Create a new zip archive.
w := zip.NewWriter(outFile)
// Add some files to the archive.
err := addFiles(w, baseFolder, "")
if err != nil {
return err
}
return w.Close()
}
func isValidFolderOrFile(name string) bool {
return !strings.HasPrefix(name, "_") && !strings.HasPrefix(name, ".")
}
func addFiles(w *zip.Writer, basePath string, baseInZip string) error {
// Open the Directory
files, err := os.ReadDir(basePath)
if err != nil {
return err
}
for _, file := range files {
if !isValidFolderOrFile(file.Name()) {
continue
}
if !file.IsDir() {
// #nosec G304
src, err := os.Open(filepath.Join(basePath, file.Name()))
if err != nil {
return err
}
// Add some files to the archive.
dst, err := w.Create(path.Join(baseInZip, file.Name()))
if err != nil {
_ = src.Close()
return err
}
_, err = io.Copy(dst, src)
_ = src.Close()
if err != nil {
return err
}
} else if file.IsDir() {
// Drill down
newBase := path.Join(basePath, file.Name())
err := addFiles(w, newBase, path.Join(baseInZip, file.Name()))
if err != nil {
return err
}
}
}
return nil
}