generated from compscore/check-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (133 loc) · 3.77 KB
/
main.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
package web
import (
"context"
"fmt"
"io"
"net/http"
"regexp"
"strings"
)
type optionsStruct struct {
// check for status code match
StatusCode int `compscore:"status_code"`
// check for substring match in body
SubstringMatch bool `compscore:"substring_match"`
// check for regex match in body
RegexMatch bool `compscore:"regex_match"`
// check for exact match in body
Match bool `compscore:"match"`
}
func (o *optionsStruct) Unmarshal(options map[string]interface{}) error {
statusCodeInterface, ok := options["status_code"]
if ok {
statusCode, ok := statusCodeInterface.(int)
if !ok {
return fmt.Errorf("status code must be a string")
}
o.StatusCode = statusCode
}
_, ok = options["substring_match"]
if ok {
o.SubstringMatch = true
}
_, ok = options["regex_match"]
if ok {
o.RegexMatch = true
}
_, ok = options["match"]
if ok {
o.Match = true
}
return nil
}
func (o *optionsStruct) Compare(expectedOutput string, response *http.Response) error {
if o.StatusCode != 0 && o.StatusCode != response.StatusCode {
return fmt.Errorf("status code mismatch: expected \"%d\", got \"%d\"", o.StatusCode, response.StatusCode)
}
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("encountered error while reading response body: %v", err.Error())
}
body := string(bodyBytes)
if o.SubstringMatch && !strings.Contains(body, expectedOutput) {
return fmt.Errorf("substring match mismatch: expected \"%s\"", expectedOutput)
}
if o.Match && expectedOutput != body {
return fmt.Errorf("match mismatch: expected \"%s\", got \"%s\"", expectedOutput, body)
}
if o.RegexMatch {
pattern, err := regexp.Compile(expectedOutput)
if err != nil {
return fmt.Errorf("invalid regex pattern: \"%s\"", expectedOutput)
}
if !pattern.MatchString(body) {
return fmt.Errorf("regex match mismatch: expected \"%s\"", expectedOutput)
}
}
return nil
}
func Run(ctx context.Context, target string, command string, expectedOutput string, username string, password string, options map[string]interface{}) (bool, string) {
var requestType string
switch strings.ToUpper(command) {
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 false, "provided invalid command/http verb: " + command
}
req, err := http.NewRequestWithContext(ctx, requestType, target, nil)
if err != nil {
return false, fmt.Sprintf("encounted error while creating request: %v", err.Error())
}
if username != "" && password != "" {
req.SetBasicAuth(username, password)
} else if password != "" && username == "" {
req.Header.Add("Authorization", password)
}
errChan := make(chan error)
go func() {
defer close(errChan)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
errChan <- fmt.Errorf("encounted error while making request: %v", err.Error())
return
}
defer resp.Body.Close()
var o optionsStruct
err = o.Unmarshal(options)
if err != nil {
errChan <- fmt.Errorf("encounted error while parsing expected output: %v", err.Error())
return
}
err = o.Compare(expectedOutput, resp)
if err != nil {
errChan <- fmt.Errorf("encounted error while comparing expected output: %v", err.Error())
return
}
}()
select {
case <-ctx.Done():
return false, fmt.Sprintf("Timeout exceeded; err %v", ctx.Err())
case err := <-errChan:
if err != nil {
return false, fmt.Sprintf("Encountered error: %s", err)
}
return true, ""
}
}