generated from Scorify/check-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
219 lines (186 loc) · 6.18 KB
/
main.go
File metadata and controls
219 lines (186 loc) · 6.18 KB
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
package http
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"regexp"
"slices"
"strconv"
"strings"
"github.com/scorify/schema"
)
type Schema struct {
URL string `key:"url"`
Verb string `key:"verb" default:"GET" enum:"GET,POST,PUT,DELETE,PATCH,HEAD,OPTIONS,CONNECT,TRACE"`
ExpectedOutput string `key:"expected_output"`
MatchType string `key:"match_type" default:"statusCode" enum:"statusCode,substringMatch,exactMatch,regexMatch"`
Insecure bool `key:"insecure"`
Headers string `key:"headers"`
Body string `key:"body"`
ContentType string `key:"content_type" default:"empty" enum:"text/plain,application/json,application/x-www-form-urlencoded,empty"`
}
func Validate(config string) error {
conf := Schema{}
err := schema.Unmarshal([]byte(config), &conf)
if err != nil {
return err
}
if conf.URL == "" {
return fmt.Errorf("url must be provided; got: %v", conf.URL)
}
if !slices.Contains([]string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT", "TRACE"}, conf.Verb) {
return fmt.Errorf("invalid command provided: %v", conf.Verb)
}
if !slices.Contains([]string{"statusCode", "substringMatch", "exactMatch", "regexMatch"}, conf.MatchType) {
return fmt.Errorf("invalid match type provided: %v", conf.MatchType)
}
if conf.ExpectedOutput == "" {
return fmt.Errorf("expected_output must be provided; got: %v", conf.ExpectedOutput)
}
if conf.MatchType == "regexMatch" {
if _, err := regexp.Compile(conf.ExpectedOutput); err != nil {
return fmt.Errorf("invalid regex pattern provided: %v; %q", conf.ExpectedOutput, err)
}
}
if conf.MatchType == "statusCode" {
status_code, err := strconv.Atoi(conf.ExpectedOutput)
if err != nil {
return fmt.Errorf("invalid status code provided: %v; %q", conf.ExpectedOutput, err)
}
if status_code < 100 || status_code > 599 {
return fmt.Errorf("invalid status code provided: %d", status_code)
}
}
if conf.Headers != "" {
for _, raw := range strings.Split(conf.Headers, ";") {
if raw == "" {
return fmt.Errorf("header format must be \"header:value;header:value\" ; got: %v", conf.Headers)
}
parts := strings.SplitN(raw, ":", 2)
if len(parts) != 2 || strings.TrimSpace(parts[0]) == "" || strings.TrimSpace(parts[1]) == "" {
return fmt.Errorf("header format must be \"header:value;header:value\" ; got: %v", conf.Headers)
}
}
}
if conf.ContentType == "empty" && conf.Body != "" {
return fmt.Errorf("body must not be provided when using empty Content-Type; got: %v", conf.Body)
}
if conf.ContentType != "empty" && conf.Body == "" {
return fmt.Errorf("body must be provided when using non-empty Content-Type; got: %v", conf.Body)
}
if !slices.Contains([]string{"text/plain", "application/json", "application/x-www-form-urlencoded", "empty"}, conf.ContentType) {
return fmt.Errorf("invalid content type provided: %v", conf.ContentType)
}
return nil
}
func Run(ctx context.Context, config string) error {
conf := Schema{}
err := schema.Unmarshal([]byte(config), &conf)
if err != nil {
return err
}
var requestType string
switch conf.Verb {
case "GET":
requestType = http.MethodGet
case "POST":
requestType = http.MethodPost
case "PUT":
requestType = http.MethodPut
case "DELETE":
requestType = http.MethodDelete
case "PATCH":
requestType = http.MethodPatch
case "HEAD":
requestType = http.MethodHead
case "OPTIONS":
requestType = http.MethodOptions
case "CONNECT":
requestType = http.MethodConnect
case "TRACE":
requestType = http.MethodTrace
default:
return fmt.Errorf("provided invalid command/http verb: %q", conf.Verb)
}
var req *http.Request
if conf.ContentType == "empty" {
req, err = http.NewRequestWithContext(ctx, requestType, conf.URL, nil)
} else {
req, err = http.NewRequestWithContext(ctx, requestType, conf.URL, bytes.NewBufferString(conf.Body))
}
if err != nil {
return fmt.Errorf("encountered error while creating request: %v", err.Error())
}
if conf.ContentType != "empty" {
req.Header.Set("Content-Type", conf.ContentType)
}
if conf.Headers != "" {
for _, raw := range strings.Split(conf.Headers, ";") {
raw = strings.TrimSpace(raw)
if raw == "" {
continue
}
parts := strings.SplitN(raw, ":", 2)
if len(parts) != 2 {
continue
}
key, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
if key != "" && value != "" {
req.Header.Add(key, value)
}
}
}
tls_config := &tls.Config{InsecureSkipVerify: conf.Insecure}
http_transport := &http.Transport{TLSClientConfig: tls_config}
client := &http.Client{Transport: http_transport}
defer http_transport.CloseIdleConnections()
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("encountered error while making request: %v", err.Error())
}
defer resp.Body.Close()
switch conf.MatchType {
case "statusCode":
status_code, err := strconv.Atoi(conf.ExpectedOutput)
if err != nil {
return fmt.Errorf("invalid status code provided: %v; %q", conf.ExpectedOutput, err)
}
if resp.StatusCode != status_code {
return fmt.Errorf("expected status code: %d; got: %d", status_code, resp.StatusCode)
}
case "substringMatch":
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("encountered error while reading response body: %v", err)
}
if !strings.Contains(string(body), conf.ExpectedOutput) {
return fmt.Errorf("expected output not found in response body")
}
case "exactMatch":
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("encountered error while reading response body: %v", err)
}
if string(body) != conf.ExpectedOutput {
return fmt.Errorf("expected output not found in response body")
}
case "regexMatch":
pattern, err := regexp.Compile(conf.ExpectedOutput)
if err != nil {
return fmt.Errorf("invalid regex pattern provided: %v; %q", conf.ExpectedOutput, err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("encountered error while reading response body: %v", err)
}
if !pattern.Match(body) {
return fmt.Errorf("expected output not found in response body")
}
default:
return fmt.Errorf("invalid match type provided: %v", conf.MatchType)
}
return nil
}