-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
137 lines (115 loc) · 3.64 KB
/
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
/*
Copyright (c) 42Crunch Ltd. All rights reserved.
Licensed under the GNU Affero General Public License version 3. See LICENSE.txt in the project root for license information.
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strings"
"github.com/google/uuid"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
)
type JobRequest struct {
Token string
Name string
ExpirationTime int64
PlatformService string
ScandImage string
Env map[string]string
}
type Job struct {
Name string
ExpirationTime int64
ScandImage string
Env []v1.EnvVar
}
func sendResponse(w http.ResponseWriter, response map[string]interface{}) {
result, err := json.Marshal(response)
if err != nil {
log.Printf("ERROR, failed to marshal response: %s", err)
writeErrorMsg(err, w, http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(result))
}
func writeErrorMsg(err error, w http.ResponseWriter, status int) {
json, _ := json.Marshal(map[string]string{
"error": fmt.Sprint(err),
})
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write([]byte(json))
}
func readJobRequest(r *http.Request) (*Job, error) {
// set default values which can be overriden by the request
job := JobRequest{
Name: fmt.Sprintf("scand-%s", uuid.New().String()),
ExpirationTime: expirationTimeInt,
PlatformService: platformService,
ScandImage: scandImage,
}
err := json.NewDecoder(r.Body).Decode(&job)
if err != nil {
log.Println("ERROR, failed to launch a job, can't decode the request:", err)
return nil, errors.New("invalid request")
}
if !isValidJobName(job.Name) {
log.Println("ERROR, failed to launch a job, invalid job name:", job.Name)
return nil, errors.New("invalid job name")
}
if !isValidUUID(job.Token) {
log.Println("ERROR, failed to launch a job, no token provided or invalid token:", job.Token)
return nil, errors.New("no token provided or invalid token")
}
if !isValidHostnameAndPort(job.PlatformService) {
log.Println("ERROR, failed to launch a job, invalid platform host:", job.PlatformService)
return nil, errors.New("invalid platform host")
}
if !isValidScandImage(job.ScandImage) {
log.Println("ERROR, failed to launch a job, invalid scand image:", job.ScandImage)
return nil, errors.New("invalid scand image")
}
var envVars []v1.EnvVar
envVars = append(envVars, newEnvVar("SCAN_TOKEN", job.Token))
envVars = append(envVars, newEnvVar("PLATFORM_SERVICE", job.PlatformService))
if job.Env != nil {
for name, value := range job.Env {
nameUpper := strings.ToUpper(name)
if strings.HasPrefix(nameUpper, "SECURITY_" ) || strings.HasPrefix(nameUpper, "SCAN42C_") || strings.HasPrefix(nameUpper, "HTTPS_") || strings.HasPrefix(nameUpper, "HTTP_"){
envVars = append(envVars, newEnvVar(name, value))
} else {
log.Println("ERROR, invalid env variable in the request, must start with 'SECURITY_, SCAN42C_, or set HTTP proxies' ", name)
return nil, errors.New("invalid environment variable name, must start with 'SECURITY_, SCAN42C_, or set HTTP proxies'")
}
}
}
return &Job{
Name: job.Name,
ExpirationTime: job.ExpirationTime,
ScandImage: job.ScandImage,
Env: envVars,
}, nil
}
func newEnvVar(name string, value string) v1.EnvVar {
return v1.EnvVar{
Name: name,
Value: value,
}
}
func getJobStatus(job *batchv1.Job) string {
if job.Status.Active > 0 {
return "active"
} else if job.Status.Succeeded > 0 {
return "succeeded"
} else if job.Status.Failed > 0 {
return "failed"
} else {
return "unknown"
}
}