/
component_data.go
203 lines (183 loc) · 5.16 KB
/
component_data.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
package api
import (
"bytes"
"io"
"net/http"
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
)
type ComponentDataService struct {
client *Client
}
const URL_TYPE_DEFAULT = "Default"
const URL_TYPE_SAST_TABLES = "SastTables"
var URL_TYPES = []string{URL_TYPE_DEFAULT, URL_TYPE_SAST_TABLES}
type ComponentDataInitialRequest struct {
Name string `json:"name"`
Tags []string `json:"tags"`
SupportedMethods []string `json:"supportedMethods"`
Documents []*DocumentSpec `json:"documents"`
UrlType string `json:"urlType"`
}
type DocumentSpec struct {
Name string `json:"name"`
Size int64 `json:"size"`
}
type ComponentDataInitialResponseRaw struct {
Data *ComponentDataInitialResponse `json:"data,omitempty"`
}
type ComponentDataInitialResponse struct {
Guid string `json:"guid,omitempty"`
UploadMethods []*ComponentDataUploadMethod `json:"uploadMethods,omitempty"`
}
type ComponentDataUploadMethod struct {
Method string `json:"method,omitempty"`
Info map[string]string `json:"info,omitempty"`
}
type ComponentDataCompleteRequest struct {
UploadGuid string `json:"uploadGuid"`
UrlType string `json:"urlType"`
}
type ComponentDataCompleteResponseRaw struct {
Data *ComponentDataCompleteResponse `json:"data,omitempty"`
}
type ComponentDataCompleteResponse struct {
Guid string `json:"guid,omitempty"`
}
func (svc *ComponentDataService) UploadFiles(
name string, tags []string, paths []string) (string, error) {
return svc.doUploadFiles(name, tags, paths, URL_TYPE_DEFAULT)
}
func (svc *ComponentDataService) UploadSastTables(
name string, paths []string) (string, error) {
return svc.doUploadFiles(name, []string{"sast"}, paths, URL_TYPE_SAST_TABLES)
}
func (svc *ComponentDataService) doUploadFiles(
name string, tags []string, paths []string, urlType string) (string, error) {
var hasValidType = false
for _, validType := range URL_TYPES {
if urlType == validType {
hasValidType = true
break
}
}
if !hasValidType {
return "", errors.Errorf("Invalid URL type: (%s)", urlType)
}
initialRequest, err := buildComponentDataInitialRequest(name, tags, paths, urlType)
if err != nil {
return "", err
}
var initialResponse ComponentDataInitialResponseRaw
err = doWithExponentialBackoffWaiting(func() error {
return svc.client.RequestEncoderDecoder(http.MethodPost,
apiV2ComponentDataRequest,
initialRequest,
&initialResponse,
)
})
if err != nil {
return "", err
}
var chosenMethod *ComponentDataUploadMethod
for _, method := range initialResponse.Data.UploadMethods {
if method.Method == "AwsS3" {
chosenMethod = method
}
}
if chosenMethod == nil {
return "", errors.New("couldn't find a supported upload method in the upload request response")
}
for _, path := range paths {
err = doWithExponentialBackoffWaiting(func() error {
return svc.putFileToS3(path, chosenMethod.Info)
})
if err != nil {
return "", err
}
}
completeRequest := ComponentDataCompleteRequest{
UploadGuid: initialResponse.Data.Guid,
UrlType: urlType,
}
var completeResponse ComponentDataCompleteResponseRaw
err = doWithExponentialBackoffWaiting(func() error {
return svc.client.RequestEncoderDecoder(http.MethodPost,
apiV2ComponentDataComplete,
completeRequest,
&completeResponse,
)
})
if err != nil {
return "", err
}
if initialResponse.Data.Guid != completeResponse.Data.Guid {
return "", errors.New("expected the initial GUID and the one returned on completion to match")
}
return initialResponse.Data.Guid, nil
}
func buildComponentDataInitialRequest(
name string, tags []string, paths []string, urlType string,
) (*ComponentDataInitialRequest, error) {
documents := make([]*DocumentSpec, 0, len(paths))
for _, path := range paths {
info, err := os.Lstat(path)
if err != nil {
return nil, err
}
documents = append(documents, &DocumentSpec{
Name: filepath.Base(path),
Size: info.Size(),
})
}
return &ComponentDataInitialRequest{
Name: name,
Tags: tags,
SupportedMethods: []string{"AwsS3"},
Documents: documents,
UrlType: urlType,
}, nil
}
func (svc *ComponentDataService) putFileToS3(path string, uploadUrls map[string]string) error {
contents, err := os.ReadFile(path)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPut, uploadUrls[filepath.Base(path)], bytes.NewReader(contents))
if err != nil {
return err
}
resp, err := svc.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err == nil {
return errors.Errorf("Upload to S3 failed (%s): %s", resp.Status, body)
}
return errors.Errorf("Upload to S3 failed (%s)", resp.Status)
}
return nil
}
func doWithExponentialBackoffWaiting(f func() error) error {
return DoWithExponentialBackoff(f, func(x int) {
time.Sleep(time.Duration(x) * time.Second)
})
}
func DoWithExponentialBackoff(f func() error, wait func(x int)) error {
err := f()
if err == nil {
return nil
}
for waitTime := 2; waitTime < 60; waitTime *= 2 {
wait(waitTime)
err := f()
if err == nil {
return nil
}
}
return err
}