/
http.go
332 lines (286 loc) · 7.91 KB
/
http.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
package types
import (
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/ssh"
)
const maxBuildContextSize = 1024 * 1024 * 100 // 100MB
func parseContextFile(r *http.Request) (multipart.File, error) {
invalidFileErr := &Error{
Code: http.StatusBadRequest,
Message: "Failed to parse build context file",
Suggestion: "Make sure the build context is a valid zipped multipart form file called 'context.zip'",
}
if err := r.ParseMultipartForm(maxBuildContextSize); err != nil {
invalidFileErr.Err = fmt.Errorf("failed to parse multipart form: %w", err)
return nil, invalidFileErr
}
// Only allowed to upload a single file called context.zip for now
form := r.MultipartForm
file, exists := form.File["context"]
if !exists {
invalidFileErr.Message = "No build context file found"
return nil, invalidFileErr
}
if len(file) != 1 {
invalidFileErr.Message = "More than one build context file found"
return nil, invalidFileErr
}
if file[0].Filename != "context.zip" {
invalidFileErr.Message = "Invalid build context file name"
return nil, invalidFileErr
}
part, err := file[0].Open()
if err != nil {
invalidFileErr.Err = fmt.Errorf("failed to open file: %w", err)
return nil, invalidFileErr
}
return part, nil
}
type BuildsCreateParams struct {
Builder string `json:"builder"`
Name *string `json:"name,omitempty"`
BuildContext io.ReadCloser `json:"-"`
}
func (i *BuildsCreateParams) Bind(r *http.Request) error {
jsonStr := r.FormValue("params")
if err := json.Unmarshal([]byte(jsonStr), i); err != nil {
return &Error{
Code: http.StatusBadRequest,
Message: "Failed to parse request body",
Suggestion: "Make sure the request body is valid JSON",
Err: err,
}
}
if i.Builder != "docker" {
return &Error{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("Invalid builder: %s", i.Builder),
Suggestion: "Valid builders are: docker",
}
}
// Validate build context in Multipart Form
part, err := parseContextFile(r)
if err != nil {
return err
}
i.BuildContext = part
return nil
}
type BuildsCreateResponse struct {
BuildID string `json:"buildID"`
}
type BuildsGetResponse struct {
Build
UsedBySessions []Exec `json:"usedBySessions,omitempty"`
Logs *[]LogEntry `json:"logs,omitempty"`
}
type NodeTypesListResponse struct {
NodeTypes []NodeType `json:"nodeTypes"`
}
type VolumeAttachParams struct {
VolumeRef string `json:"volumeRef"`
MountPath string `json:"mountPath"`
}
type ExecCreateParams struct {
Name string `json:"name,omitempty"`
Provider Provider `json:"provider"`
Spec HardwareSpec `json:"hardwareSpec,omitempty"`
SSHKeyName string `json:"sshKeyName"`
SSHPublicKey string `json:"sshPublicKey"`
Region *string `json:"region,omitempty"`
Image *string `json:"image"`
Command []string `json:"command"`
CommitID *string `json:"commitID,omitempty"`
GitURL *string `json:"gitURL,omitempty"`
Source *SourceContext `json:"source,omitempty"`
Volumes []VolumeAttachParams `json:"volumes,omitempty"`
InternalPort int32 `json:"internal_port"`
}
func (s *ExecCreateParams) Bind(r *http.Request) error {
// Check if header is set, if yes and set to json, parse body as json
if r.Header.Get("Content-Type") == "application/json" {
log.Info().Msg("Content-Type is application/json")
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(s); err != nil {
return &Error{
Code: http.StatusBadRequest,
Message: "Failed to parse request body",
Suggestion: "Make sure the request body is valid JSON",
Err: err,
}
}
return nil
}
jsonStr := r.FormValue("params")
if err := json.Unmarshal([]byte(jsonStr), s); err != nil {
return &Error{
Code: http.StatusBadRequest,
Message: "Failed to parse request body",
Suggestion: "Make sure the request body is a valid multipart form or valid JSON",
Err: err,
}
}
if s.Provider == "" {
return &Error{
Code: http.StatusBadRequest,
Message: "Invalid request body: field 'provider' is required",
}
}
if s.SSHPublicKey == "" || s.SSHKeyName == "" {
return &Error{
Code: http.StatusBadRequest,
Message: "Invalid request body: both Public Key and Key Name are required",
}
}
if s.Image != nil {
if *s.Image == "" {
return &Error{
Code: http.StatusBadRequest,
Message: "Invalid request body: field 'image' cannot be an empty string",
}
}
}
if len(s.Volumes) > 0 {
for _, v := range s.Volumes {
if v.VolumeRef == "" {
return &Error{
Code: http.StatusBadRequest,
Message: "Invalid request body: field 'volumeRef' cannot be an empty string",
}
}
if v.MountPath == "" || v.MountPath == "/" {
return &Error{
Code: http.StatusBadRequest,
Message: "Invalid request body: field 'mountPath' cannot be an empty string or '/'",
}
}
}
}
if s.Source != nil {
part, err := parseContextFile(r)
if err != nil {
return err
}
s.Source.Context = part
}
return nil
}
type ProviderConnectParams struct {
Provider Provider `json:"provider"`
ProviderToken string `json:"providerToken,omitempty"`
}
func (p *ProviderConnectParams) Bind(r *http.Request) error {
if p.Provider == "" {
return &Error{
Code: http.StatusBadRequest,
Message: "Invalid request body: field 'provider' is required",
}
}
return nil
}
type ProvidersListResponse struct {
Providers []Provider `json:"providers"`
}
type ExecGetResponse struct {
Exec Exec `json:"session"`
}
type ExecsListResponse struct {
Execs []Exec `json:"sessions"`
}
type ExecTerminateResponse struct {
Success bool `json:"success"`
}
type SSHKeyAddParams struct {
Name *string `json:"name"`
PublicKey string `json:"publicKey"`
}
func (s *SSHKeyAddParams) Bind(r *http.Request) error {
if _, _, _, _, err := ssh.ParseAuthorizedKey([]byte(s.PublicKey)); err != nil {
return &Error{
Code: http.StatusBadRequest,
Message: "Invalid SSH public key",
}
}
return nil
}
type SSHKeyGenerateParams struct {
Name *string `json:"name"`
}
func (s *SSHKeyGenerateParams) Bind(r *http.Request) error {
return nil
}
type SSHKeyResponse struct {
Name string `json:"name"`
PublicKey string `json:"publicKey"`
PrivateKey string `json:"privateKey"`
}
type SSHKeyListResponse struct {
Keys []SSHKey `json:"keys"`
}
type VolumeCreateRequest struct {
Size int `json:"size"`
Name string `json:"name"`
Provider Provider `json:"provider"`
}
func (p *VolumeCreateRequest) Bind(r *http.Request) error {
if p.Name == "" {
return &Error{
Code: http.StatusBadRequest,
Message: "Name is required",
}
}
if p.Size <= 0 {
return &Error{
Code: http.StatusBadRequest,
Message: "Size is required",
}
}
if p.Provider == LambdaLabsProvider {
return &Error{
Code: http.StatusBadRequest,
Message: "Lambda Labs not implemented",
}
}
switch p.Provider {
case LambdaLabsProvider:
return &Error{
Code: http.StatusBadRequest,
Message: "Lambda Labs not implemented",
}
case UnweaveProvider, AWSProvider:
default:
return &Error{
Code: http.StatusBadRequest,
Message: "Invalid provider",
Suggestion: "Valid providers are: " + UnweaveProvider.String(),
}
}
return nil
}
type VolumesListResponse struct {
Volumes []Volume `json:"volumes"`
}
type VolumeResizeRequest struct {
IDOrName string `json:"idOrName"`
Size int `json:"size"`
}
func (p *VolumeResizeRequest) Bind(r *http.Request) error {
if p.IDOrName == "" {
return &Error{
Code: http.StatusBadRequest,
Message: "Name is required",
}
}
if p.Size <= 0 {
return &Error{
Code: http.StatusBadRequest,
Message: "A new size is required",
}
}
return nil
}