-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathagent_test.go
287 lines (230 loc) · 12.8 KB
/
agent_test.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
package main
import (
"context"
"fmt"
"net/http"
"os"
"path"
"testing"
"time"
"github.com/florianl/go-nflog/v2"
"github.com/jarcoal/httpmock"
)
type mockDNSServer struct {
DNSServer
}
type mockDNSServerWithError struct {
DNSServer
}
func (m *mockDNSServer) ListenAndServe() error {
return nil
}
func (m *mockDNSServerWithError) ListenAndServe() error {
return fmt.Errorf("error")
}
type MockIPTables struct {
IPTables
}
func (m *MockIPTables) Append(table, chain string, rulespec ...string) error {
return nil
}
func (m *MockIPTables) Exists(table, chain string, rulespec ...string) (bool, error) {
return false, nil
}
func (m *MockIPTables) Insert(table, chain string, post int, rulespec ...string) error {
return nil
}
func (m *MockIPTables) ClearChain(table, chain string) error {
return nil
}
type MockAgentNflogger struct {
AgentNflogger
}
func (m *MockAgentNflogger) Open(config *nflog.Config) (AgentNfLog, error) {
return AgentNfLog{&MockNfLogger{}}, nil
}
type MockAgentNfloggerWithErr struct {
AgentNflogger
}
func (m *MockAgentNfloggerWithErr) Open(config *nflog.Config) (AgentNfLog, error) {
return AgentNfLog{&MockNfLogger{}}, fmt.Errorf("failed to open nflog")
}
type MockNfLogger struct {
NfLogger
}
func (m *MockNfLogger) Close() error {
return nil
}
func (m *MockNfLogger) Register(ctx context.Context, fn nflog.HookFunc) error {
return nil
}
type MockCommand struct {
Command
}
type MockCommandWithError struct {
Command
}
func (m *MockCommand) Run() error {
return nil
}
func (m *MockCommandWithError) Run() error {
return fmt.Errorf("failed to run command")
}
func deleteTempFile(path string) {
os.Remove(path)
}
func getContext(seconds int) context.Context {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
time.AfterFunc(time.Duration(seconds)*time.Second, cancel)
return ctx
}
func TestRun(t *testing.T) {
type args struct {
ctxCancelDuration int
configFilePath string
hostDNSServer DNSServer
dockerDNSServer DNSServer
iptables *Firewall
nflog AgentNflogger
cmd Command
resolvdConfigPath string
dockerDaemonConfigPath string
ciTestOnly bool
}
httpmock.Activate()
httpmock.RegisterResponder("GET", "https://dns.google/resolve?name=domain1.com.&type=a",
httpmock.NewStringResponder(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain1.com.","type":1}],"Answer":[{"name":"domain1.com.","type":1,"TTL":30,"data":"67.67.67.67"}]}`))
httpmock.RegisterResponder("GET", "https://dns.google/resolve?name=domain2.com.&type=a",
httpmock.ResponderFromMultipleResponses(
[]*http.Response{
httpmock.NewStringResponse(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}`),
httpmock.NewStringResponse(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}`),
httpmock.NewStringResponse(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"70.70.70.70"}]}`),
httpmock.NewStringResponse(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}`),
httpmock.NewStringResponse(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain2.com.","type":1}],"Answer":[{"name":"domain2.com.","type":1,"TTL":30,"data":"70.70.70.70"}]}`),
},
t.Log))
httpmock.RegisterResponder("GET", "https://cloudflare-dns.com/dns-query?name=actions-results-receiver-production.githubapp.com.&type=A",
httpmock.NewStringResponder(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"requesteddomain.com.","type":1}],"Answer":[{"name":"requesteddomain.com.","type":1,"TTL":300,"data":"69.69.69.69"}]}`))
httpmock.RegisterResponder("GET", "https://cloudflare-dns.com/dns-query?name=artifactcache.actions.githubusercontent.com.&type=A",
httpmock.NewStringResponder(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"requesteddomain.com.","type":1}],"Answer":[{"name":"requesteddomain.com.","type":1,"TTL":300,"data":"69.69.69.69"}]}`))
httpmock.RegisterResponder("GET", "https://cloudflare-dns.com/dns-query?name=pipelines.actions.githubusercontent.com.&type=A",
httpmock.NewStringResponder(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"requesteddomain.com.","type":1}],"Answer":[{"name":"requesteddomain.com.","type":1,"TTL":300,"data":"69.69.69.69"}]}`))
httpmock.RegisterResponder("GET", "https://cloudflare-dns.com/dns-query?name=token.actions.githubusercontent.com.&type=A",
httpmock.NewStringResponder(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"requesteddomain.com.","type":1}],"Answer":[{"name":"requesteddomain.com.","type":1,"TTL":300,"data":"69.69.69.69"}]}`))
httpmock.RegisterResponder("GET", "https://cloudflare-dns.com/dns-query?name=codeload.github.com.&type=A",
httpmock.NewStringResponder(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"requesteddomain.com.","type":1}],"Answer":[{"name":"requesteddomain.com.","type":1,"TTL":300,"data":"69.69.69.69"}]}`))
httpmock.RegisterResponder("GET", "https://cloudflare-dns.com/dns-query?name=vstsmms.actions.githubusercontent.com.&type=A",
httpmock.NewStringResponder(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"requesteddomain.com.","type":1}],"Answer":[{"name":"requesteddomain.com.","type":1,"TTL":300,"data":"69.69.69.69"}]}`))
httpmock.RegisterResponder("GET", "https://cloudflare-dns.com/dns-query?name=vstoken.actions.githubusercontent.com.&type=A",
httpmock.NewStringResponder(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"requesteddomain.com.","type":1}],"Answer":[{"name":"requesteddomain.com.","type":1,"TTL":300,"data":"69.69.69.69"}]}`))
httpmock.RegisterResponder("GET", "https://apiurl/v1/github/owner/repo/actions/subscription",
httpmock.NewStringResponder(403, ""))
tests := []struct {
name string
args args
wantErr bool
}{
{name: "success egress audit", args: args{ctxCancelDuration: 2, configFilePath: "./testfiles/agent.json", hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServer{},
iptables: &Firewall{&MockIPTables{}}, nflog: &MockAgentNflogger{}, cmd: &MockCommand{}, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}")}, wantErr: false},
{name: "success egress blocked", args: args{ctxCancelDuration: 2, configFilePath: "./testfiles/agent-allowed-endpoints.json",
hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServer{},
iptables: &Firewall{&MockIPTables{}}, nflog: &MockAgentNflogger{}, cmd: &MockCommand{}, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}")}, wantErr: false},
// ctx will cancel after 35 seconds
// DNS refresh will be done after 30 seconds
{name: "success egress blocked DNS refresh", args: args{ctxCancelDuration: 35, configFilePath: "./testfiles/agent-allowed-endpoints.json",
hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServer{},
iptables: &Firewall{&MockIPTables{}}, nflog: &MockAgentNflogger{}, cmd: &MockCommand{}, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}")}, wantErr: false},
{name: "dns failure", args: args{ctxCancelDuration: 5, configFilePath: "./testfiles/agent.json", hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServerWithError{},
iptables: &Firewall{&MockIPTables{}}, nflog: &MockAgentNflogger{}, cmd: &MockCommand{}, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}")}, wantErr: true},
{name: "cmd failure", args: args{ctxCancelDuration: 5, configFilePath: "./testfiles/agent.json", hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServer{},
iptables: &Firewall{&MockIPTables{}}, nflog: &MockAgentNflogger{}, cmd: &MockCommandWithError{}, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}")}, wantErr: true},
{name: "nflog failure", args: args{ctxCancelDuration: 5, configFilePath: "./testfiles/agent.json", hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServer{},
iptables: &Firewall{&MockIPTables{}}, nflog: &MockAgentNfloggerWithErr{}, cmd: &MockCommand{}, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}")}, wantErr: true},
// CI only tests
{name: "success monitor process CI Test", args: args{ctxCancelDuration: 2, configFilePath: "./testfiles/agent.json", hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServer{},
iptables: &Firewall{&MockIPTables{}}, nflog: &MockAgentNflogger{}, cmd: nil, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}"), ciTestOnly: true}, wantErr: false},
{name: "success allowed endpoints CI Test", args: args{ctxCancelDuration: 2, configFilePath: "./testfiles/agent-allowed-endpoints.json",
hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServer{},
iptables: nil, nflog: &MockAgentNflogger{}, cmd: &MockCommand{}, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}"), ciTestOnly: true}, wantErr: false},
{name: "success allowed endpoints DNS refresh CI Test", args: args{ctxCancelDuration: 60, configFilePath: "./testfiles/agent-allowed-endpoints.json",
hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServer{},
iptables: nil, nflog: &MockAgentNflogger{}, cmd: &MockCommand{}, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}"), ciTestOnly: true}, wantErr: false},
{name: "success disable sudo", args: args{ctxCancelDuration: 35, configFilePath: "./testfiles/agent-disable-sudo.json",
hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServer{},
iptables: &Firewall{&MockIPTables{}}, nflog: &MockAgentNflogger{}, cmd: &MockCommand{}, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}"), ciTestOnly: true}, wantErr: false},
{name: "private repo no subscription", args: args{ctxCancelDuration: 2, configFilePath: "./testfiles/agent-private-repo.json", hostDNSServer: &mockDNSServer{}, dockerDNSServer: &mockDNSServer{},
iptables: &Firewall{&MockIPTables{}}, nflog: &MockAgentNflogger{}, cmd: &MockCommand{}, resolvdConfigPath: createTempFileWithContents(""),
dockerDaemonConfigPath: createTempFileWithContents("{}")}, wantErr: false},
}
_, ciTest := os.LookupEnv("CI")
fmt.Printf("ci-test: %t\n", ciTest)
for _, tt := range tests {
if !tt.args.ciTestOnly || ciTest {
t.Run(tt.name, func(t *testing.T) {
tempDir := os.TempDir()
if err := Run(getContext(tt.args.ctxCancelDuration), tt.args.configFilePath, tt.args.hostDNSServer, tt.args.dockerDNSServer,
tt.args.iptables, tt.args.nflog, tt.args.cmd, tt.args.resolvdConfigPath, tt.args.dockerDaemonConfigPath, tempDir); (err != nil) != tt.wantErr {
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
}
deleteTempFile(path.Join(tempDir, "resolved.conf"))
deleteTempFile(path.Join(tempDir, "daemon.json"))
if tt.args.ciTestOnly {
fmt.Printf("Reverting firewall changes\n")
RevertFirewallChanges(nil)
}
})
}
}
}
func Test_writeDone(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{name: "writing to done.json", wantErr: false},
}
_, ciTest := os.LookupEnv("CI")
if ciTest {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := writeDone(); (err != nil) != tt.wantErr {
t.Errorf("writeDone() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
}
func Test_writeStatus(t *testing.T) {
type args struct {
message string
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "writing status", args: args{message: "this is test status"}, wantErr: false},
}
_, ciTest := os.LookupEnv("CI")
if ciTest {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := writeStatus(tt.args.message); (err != nil) != tt.wantErr {
t.Errorf("writeStatus() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
}