-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
191 lines (163 loc) · 4.63 KB
/
main.go
File metadata and controls
191 lines (163 loc) · 4.63 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
package ftp
import (
"bytes"
"context"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
"io"
"regexp"
"slices"
"github.com/jlaffaye/ftp"
"github.com/scorify/schema"
)
type Schema struct {
Target string `key:"target"`
Port int `key:"port" default:"21"`
Username string `key:"username" default:"anonymous"`
Password string `key:"password"`
File string `key:"file"`
MatchType string `key:"matchType" default:"exists" enum:"exists,substringMatch,regexMatch,exactMatch,sha256,md5,sha1"`
ExpectedOutput string `key:"expectedOutput"`
}
func Validate(config string) error {
conf := Schema{}
err := schema.Unmarshal([]byte(config), &conf)
if err != nil {
return fmt.Errorf("encountered error unmarshalling config: %v", err)
}
if conf.Target == "" {
return fmt.Errorf("target must be provided; got: %s", conf.Target)
}
if conf.Port == 0 {
return fmt.Errorf("port must be provided; got: %d", conf.Port)
}
if conf.Username == "" {
return fmt.Errorf("username must be provided; got: %s", conf.Username)
}
if !slices.Contains([]string{"exists", "substringMatch", "regexMatch", "exactMatch", "sha256", "md5", "sha1"}, conf.MatchType) {
return fmt.Errorf("matchType must be one of: exists, substringMatch, regexMatch, exactMatch, sha256, md5, sha1; got: %s", conf.MatchType)
}
if conf.ExpectedOutput == "" && conf.MatchType != "exists" {
return fmt.Errorf("expectedOutput must be provided for all comparison types except exists; got: %s", conf.ExpectedOutput)
}
return nil
}
func Run(ctx context.Context, config string) error {
conf := Schema{}
err := schema.Unmarshal([]byte(config), &conf)
if err != nil {
return fmt.Errorf("encountered error unmarshalling config: %v", err)
}
connStr := fmt.Sprintf("%s:%d", conf.Target, conf.Port)
conn, err := ftp.Dial(connStr, ftp.DialWithContext(ctx))
if err != nil {
return fmt.Errorf("encountered error creating ftp connection: %v", err)
}
defer conn.Quit()
err = login(ctx, conn, conf.Username, conf.Password)
if err != nil {
return fmt.Errorf("encountered error logging in: %v", err)
}
defer conn.Logout()
resp, err := retr(ctx, conn, conf.File)
if err != nil {
return fmt.Errorf("encountered error retrieving file: %v", err)
}
defer resp.Close()
if resp == nil {
return fmt.Errorf("response is nil")
}
bodyBytes, err := io.ReadAll(resp)
if err != nil {
return fmt.Errorf("encountered error reading response body: %v", err)
}
switch conf.MatchType {
case "exists":
return nil
case "substringMatch":
if bytes.Contains(bodyBytes, []byte(conf.ExpectedOutput)) {
return nil
}
return fmt.Errorf("response does not match substring: %s", conf.ExpectedOutput)
case "regexMatch":
pattern, err := regexp.Compile(conf.ExpectedOutput)
if err != nil {
return fmt.Errorf("encountered error compiling regex pattern: %v", err)
}
if pattern.Match(bodyBytes) {
return nil
}
return fmt.Errorf("response does not match regex pattern: %s", conf.ExpectedOutput)
case "exactMatch":
if string(bodyBytes) == conf.ExpectedOutput {
return nil
}
return fmt.Errorf("response does not match expected output")
case "sha256":
sha256 := fmt.Sprintf("%x", sha256.Sum256(bodyBytes))
if sha256 == conf.ExpectedOutput {
return nil
}
return fmt.Errorf("response does not match expected sha256: %s", conf.ExpectedOutput)
case "md5":
md5 := fmt.Sprintf("%x", md5.Sum(bodyBytes))
if md5 == conf.ExpectedOutput {
return nil
}
return fmt.Errorf("response does not match expected md5: %s", conf.ExpectedOutput)
case "sha1":
sha1 := fmt.Sprintf("%x", sha1.Sum(bodyBytes))
if sha1 == conf.ExpectedOutput {
return nil
}
return fmt.Errorf("response does not match expected sha1: %s; got %s", conf.ExpectedOutput, sha1)
default:
return fmt.Errorf("unknown match type: %s", conf.MatchType)
}
}
func login(ctx context.Context, conn *ftp.ServerConn, username string, password string) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
errChan := make(chan error, 1)
go func() {
errChan <- conn.Login(username, password)
close(errChan)
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errChan:
return err
}
}
func retr(ctx context.Context, conn *ftp.ServerConn, file string) (*ftp.Response, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
type ret struct {
resp *ftp.Response
err error
}
retChan := make(chan ret, 1)
go func() {
resp, err := conn.Retr(file)
retChan <- ret{
resp: resp,
err: err,
}
close(retChan)
}()
select {
case <-ctx.Done():
return nil, ctx.Err()
case ret := <-retChan:
return ret.resp, ret.err
}
}