-
Notifications
You must be signed in to change notification settings - Fork 4
/
artifact.go
351 lines (311 loc) · 9.98 KB
/
artifact.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package limacharlie
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
"cloud.google.com/go/storage"
"golang.org/x/sync/errgroup"
)
type ArtifactRuleName = string
type ArtifactRule struct {
By string `json:"by"`
LastUpdated uint64 `json:"updated"`
IsIgnoreCert bool `json:"is_ignore_cert"`
IsDeleteAfter bool `json:"is_delete_after"`
DaysRetentions uint `json:"days_retention"`
Patterns []string `json:"patterns"`
Filters ArtifactRuleFilter `json:"filters"`
}
type ArtifactRuleFilter struct {
Tags []string `json:"tags"`
Platforms []string `json:"platforms"`
}
type ArtifactRulesByName = map[ArtifactRuleName]ArtifactRule
type artifactExportResp struct {
Payload string `json:"payload,omitempty"`
Export string `json:"export,omitempty"`
}
var maxUploadFilePartSize = int64(1024 * 1024)
const concurrentUploads = 10
func (org Organization) artifact(responseData interface{}, action string, req Dict) error {
reqData := req
reqData["action"] = action
return org.client.serviceRequest(responseData, "logging", reqData, false)
}
func (org Organization) ArtifactsRules() (ArtifactRulesByName, error) {
resp := ArtifactRulesByName{}
if err := org.artifact(&resp, "list_rules", Dict{}); err != nil {
return ArtifactRulesByName{}, err
}
return resp, nil
}
func (org Organization) ArtifactRuleAdd(ruleName ArtifactRuleName, rule ArtifactRule) error {
resp := Dict{}
if err := org.artifact(&resp, "add_rule", Dict{
"name": ruleName,
"patterns": rule.Patterns,
"is_delete_after": rule.IsDeleteAfter,
"is_ignore_cert": rule.IsIgnoreCert,
"days_retention": rule.DaysRetentions,
"tags": rule.Filters.Tags,
"platforms": rule.Filters.Platforms,
}); err != nil {
return err
}
return nil
}
func (org Organization) ArtifactRuleDelete(ruleName ArtifactRuleName) error {
resp := Dict{}
if err := org.artifact(&resp, "remove_rule", Dict{"name": ruleName}); err != nil {
return err
}
return nil
}
func (org Organization) CreateArtifactFromBytes(name string, fileData []byte, fileType string, artifactId string, nDaysRetention int, ingestionKey string) error {
return org.UploadArtifact(bytes.NewBuffer(fileData), int64(len(fileData)), fileType, name, artifactId, "", nDaysRetention, ingestionKey)
}
func (org Organization) CreateArtifactFromFile(name string, fileName string, fileType string, artifactId string, nDaysRetention int, ingestionKey string) error {
fileInfo, err := os.Stat(fileName)
if err != nil {
return err
}
// If it's a file, read its content
file, err := os.Open(fileName)
if err != nil {
return err
}
return org.UploadArtifact(file, fileInfo.Size(), fileType, name, artifactId, fileName, nDaysRetention, ingestionKey)
}
func (org Organization) UploadArtifact(data io.Reader, size int64, hint string, source string, artifactId string, originalPath string, nDaysRetention int, ingestionKey string) error {
// Assemble headers
headers := map[string]string{}
headers["lc-source"] = source
headers["lc-hint"] = hint
if artifactId != "" {
headers["lc-payload-id"] = artifactId
} else {
headers["lc-payload-id"] = uuid.New().String()
}
if originalPath != "" {
absolutePath, _ := filepath.Abs(originalPath)
headers["lc-path"] = base64.StdEncoding.EncodeToString([]byte(absolutePath))
}
headers["lc-retention-days"] = fmt.Sprintf("%d", nDaysRetention)
// Get artifacts URL
urls, err := org.GetURLs()
if err != nil {
return fmt.Errorf("failed resolving org URLs: %v", err)
}
uploadUrl, ok := urls["artifacts"]
if !ok {
return errors.New("artifacts URL not found in org URLs")
}
reqUrl := fmt.Sprintf("https://%s/ingest", uploadUrl)
// Build request
combined := fmt.Sprintf("%s:%s", org.GetOID(), ingestionKey)
creds := base64.StdEncoding.EncodeToString([]byte(combined))
c := &http.Client{}
defer c.CloseIdleConnections()
partId := 0
endOffset := int64(0)
eg := errgroup.Group{}
eg.SetLimit(concurrentUploads)
for {
// Read from the data in chunks of MAX_UPLOAD_PART_SIZE so we can
// upload in parts if the file is too big.
chunk := make([]byte, maxUploadFilePartSize)
n, err := data.Read(chunk)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
chunk = chunk[:n]
// If this is the last chunk, set the part to "done".
endOffset += int64(n)
if endOffset > size {
return fmt.Errorf("got more data (%d bytes) than expected (%d bytes)", endOffset, size)
}
if size <= maxUploadFilePartSize {
// If the file is small enough, we can upload it in one go.
} else if endOffset != size {
headers["lc-part"] = fmt.Sprintf("%d", partId)
} else {
headers["lc-part"] = "done"
// If this is the last part, we must ensure that all the
// other parts were done uploading since LC triggers the
// processing of the artifact upon receiving the last part.
if err := eg.Wait(); err != nil {
return err
}
// Now reset the error group so we can keep the same
// code path as usual.
eg = errgroup.Group{}
}
// Prepare the request.
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(chunk))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", creds))
// Add the dynamic headers.
for k, v := range headers {
req.Header.Set(k, v)
}
eg.Go(func() error {
// Send the request.
httpResp, err := c.Do(req)
if err != nil {
return err
}
// Check if the API liked it.
if httpResp.StatusCode != 200 {
// Read the first 1KB of the response as it can contain details.
body := make([]byte, 1024)
httpResp.Body.Read(body)
return fmt.Errorf("failed to POST artifact, http status: %d (%s)", httpResp.StatusCode, string(body))
}
return nil
})
partId++
}
return eg.Wait()
}
func (org Organization) ExportArtifact(artifactID string, deadline time.Time) (io.ReadCloser, error) {
resp := artifactExportResp{}
var request restRequest
request = makeDefaultRequest(&resp)
if err := org.client.reliableRequest(http.MethodPost, fmt.Sprintf("insight/%s/artifacts/originals/%s", org.GetOID(), artifactID), request); err != nil {
return nil, err
}
if resp.Payload != "" {
b64Dec, err := base64.StdEncoding.DecodeString(resp.Payload)
if err != nil {
return nil, err
}
gzR, err := gzip.NewReader(bytes.NewBuffer(b64Dec))
if err != nil {
return nil, err
}
return gzR, nil
}
// If the data contains an inline payload, just return it.
if resp.Payload != "" {
return io.NopCloser(base64.NewDecoder(base64.StdEncoding, strings.NewReader(resp.Payload))), nil
}
c := http.Client{
Timeout: 30 * time.Second,
}
defer c.CloseIdleConnections()
var httpResp *http.Response
for !time.Now().After(deadline) {
req, err := http.NewRequest(http.MethodGet, resp.Export, &bytes.Buffer{})
if err != nil {
return nil, err
}
httpResp, err = c.Do(req)
if err != nil {
return nil, err
}
if httpResp.StatusCode == 200 {
break
}
httpResp.Body.Close()
if httpResp.StatusCode == 404 {
time.Sleep(5 * time.Second)
continue
}
// Read all the data from the body in case it includes
// a relevant error to return.
body, _ := io.ReadAll(httpResp.Body)
return nil, fmt.Errorf("unexpected status getting data (%d): %s", httpResp.StatusCode, string(body))
}
return httpResp.Body, nil
}
func (org Organization) ExportArtifactThroughGCS(ctx context.Context, artifactID string, deadline time.Time, bucketName string, writeCreds string, readClient *storage.Client) (io.ReadCloser, error) {
resp := artifactExportResp{}
request := makeDefaultRequest(&resp).withFormData(Dict{
"dest_bucket": bucketName,
"svc_creds": writeCreds,
})
if err := org.client.reliableRequest(http.MethodPost, fmt.Sprintf("insight/%s/artifacts/originals/%s", org.GetOID(), artifactID), request); err != nil {
return nil, err
}
if resp.Payload != "" {
b64Dec, err := base64.StdEncoding.DecodeString(resp.Payload)
if err != nil {
return nil, err
}
gzR, err := gzip.NewReader(bytes.NewBuffer(b64Dec))
if err != nil {
return nil, err
}
return gzR, nil
}
u, err := url.Parse(resp.Export)
if err != nil {
return nil, fmt.Errorf("failed to parse export URL: %v", err)
}
bucket := readClient.Bucket(bucketName)
objPath := strings.SplitN(strings.TrimLeft(u.Path, "/"), "/", 2)[1]
var r io.ReadCloser
for !time.Now().After(deadline) {
r, err = bucket.Object(objPath).NewReader(ctx)
if errors.Is(err, storage.ErrObjectNotExist) {
time.Sleep(5 * time.Second)
continue
}
if err != nil {
return nil, fmt.Errorf("failed to get object reader: %v", err)
}
break
}
return r, nil
}
func (org Organization) ExportArtifactToGCS(ctx context.Context, artifactID string, deadline time.Time, bucketName string, writeCreds string, readClient *storage.Client) (string, error) {
resp := artifactExportResp{}
request := makeDefaultRequest(&resp).withFormData(Dict{
"dest_bucket": bucketName,
"svc_creds": writeCreds,
})
if err := org.client.reliableRequest(http.MethodPost, fmt.Sprintf("insight/%s/artifacts/originals/%s", org.GetOID(), artifactID), request); err != nil {
return "", err
}
if resp.Payload != "" {
// This should not happen as LC will always use the bucket if present in the request.
return "", errors.New("payload is embedded in the response, not in GCS")
}
u, err := url.Parse(resp.Export)
if err != nil {
return "", fmt.Errorf("failed to parse export URL: %v", err)
}
bucket := readClient.Bucket(bucketName)
objPath := strings.SplitN(strings.TrimLeft(u.Path, "/"), "/", 2)[1]
var r io.ReadCloser
for !time.Now().After(deadline) {
r, err = bucket.Object(objPath).NewReader(ctx)
if errors.Is(err, storage.ErrObjectNotExist) {
time.Sleep(5 * time.Second)
continue
}
if err != nil {
return "", fmt.Errorf("failed to get object reader: %v", err)
}
r.Close()
break
}
return objPath, nil
}