-
Notifications
You must be signed in to change notification settings - Fork 18
/
agent.go
387 lines (315 loc) · 11.4 KB
/
agent.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package main
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/florianl/go-nflog/v2"
)
const (
StepSecurityLogCorrelationPrefix = "Step Security Job Correlation ID:"
StepSecurityAnnotationPrefix = "StepSecurity Harden Runner:"
EgressPolicyAudit = "audit"
EgressPolicyBlock = "block"
)
type DNSServer interface {
ListenAndServe() error
}
type Command interface {
Run() error
}
type AgentNflogger interface {
Open(config *nflog.Config) (AgentNfLog, error)
}
type AgentNfLog struct {
NfLogger
}
type NfLogger interface {
Close() error
Register(ctx context.Context, fn nflog.HookFunc) error
}
type Firewall struct {
IPTables IPTables
}
type IPTables interface {
Append(table, chain string, rulespec ...string) error
Insert(table, chain string, pos int, rulespec ...string) error
Exists(table, chain string, rulespec ...string) (bool, error)
ClearChain(table, chain string) error
}
// Run the agent
// TODO: move all inputs into a struct
func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
dockerDNSServer DNSServer, iptables *Firewall, nflog AgentNflogger,
cmd Command, resolvdConfigPath, dockerDaemonConfigPath, tempDir string) error {
// Passed to each go routine, if anyone fails, the program fails
errc := make(chan error)
config := &config{}
if err := config.init(configFilePath); err != nil {
writeStatus(fmt.Sprintf("Error reading config file %v", err))
return err
}
apiclient := &ApiClient{Client: &http.Client{Timeout: 3 * time.Second}, APIURL: config.APIURL, DisableTelemetry: config.DisableTelemetry, EgressPolicy: config.EgressPolicy, OneTimeKey: config.OneTimeKey}
config.OneTimeKey = ""
// TODO: pass in an iowriter/ use log library
WriteLog(fmt.Sprintf("read config \n %+v", config))
WriteLog("\n")
WriteLog(fmt.Sprintf("%s %s", StepSecurityLogCorrelationPrefix, config.CorrelationId))
WriteLog("\n")
// if this is a private repo
if config.Private {
isActive := apiclient.getSubscriptionStatus(config.Repo)
if !isActive {
config.EgressPolicy = EgressPolicyAudit
config.DisableSudo = false
apiclient.DisableTelemetry = true
config.DisableFileMonitoring = true
WriteAnnotation("StepSecurity Harden Runner is disabled. A subscription is required for private repositories. Please start a free trial at https://www.stepsecurity.io")
}
}
Cache := InitCache(config.EgressPolicy)
allowedEndpoints, wildcardEndpoints := addImplicitEndpoints(config.Endpoints, config.DisableTelemetry)
// Start DNS servers and get confirmation
dnsProxy := DNSProxy{
Cache: &Cache,
CorrelationId: config.CorrelationId,
Repo: config.Repo,
ApiClient: apiclient,
EgressPolicy: config.EgressPolicy,
AllowedEndpoints: allowedEndpoints,
WildCardEndpoints: wildcardEndpoints,
ReverseIPLookup: make(map[string]string),
Iptables: iptables,
}
go startDNSServer(&dnsProxy, hostDNSServer, errc)
go startDNSServer(&dnsProxy, dockerDNSServer, errc) // this is for the docker bridge
// start proc mon
if cmd == nil {
procMon := &ProcessMonitor{CorrelationId: config.CorrelationId, Repo: config.Repo,
ApiClient: apiclient, WorkingDirectory: config.WorkingDirectory, DisableFileMonitoring: config.DisableFileMonitoring, DNSProxy: &dnsProxy}
go procMon.MonitorProcesses(errc)
WriteLog("started process monitor")
}
dnsConfig := DnsConfig{}
sudo := Sudo{}
var ipAddressEndpoints []ipAddressEndpoint
// hydrate dns cache
if config.EgressPolicy == EgressPolicyBlock {
for domainName, endpoints := range allowedEndpoints {
// this will cause domain, IP mapping to be cached
ipAddress, err := dnsProxy.getIPByDomain(domainName)
if err != nil {
WriteLog(fmt.Sprintf("Error resolving allowed domain %v", err))
WriteAnnotation(fmt.Sprintf("%s Reverting agent since allowed endpoint %s could not be resolved", StepSecurityAnnotationPrefix, strings.Trim(domainName, ".")))
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig, sudo)
return err
}
for _, endpoint := range endpoints {
// create list of ip address to be added to firewall
ipAddressEndpoints = append(ipAddressEndpoints, ipAddressEndpoint{ipAddress: ipAddress, port: fmt.Sprintf("%d", endpoint.port)})
}
}
}
// Change DNS config on host, causes processes to use agent's DNS proxy
if err := dnsConfig.SetDNSServer(cmd, resolvdConfigPath, tempDir); err != nil {
WriteLog(fmt.Sprintf("Error setting DNS server %v", err))
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig, sudo)
return err
}
WriteLog("\n")
WriteLog("updated resolved")
// Change DNS for docker, causes process in containers to use agent's DNS proxy
if err := dnsConfig.SetDockerDNSServer(cmd, dockerDaemonConfigPath, tempDir); err != nil {
WriteLog(fmt.Sprintf("Error setting DNS server for docker %v", err))
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig, sudo)
return err
}
WriteLog("\n")
WriteLog("set docker config\n")
if config.EgressPolicy == EgressPolicyAudit {
netMonitor := NetworkMonitor{
CorrelationId: config.CorrelationId,
Repo: config.Repo,
ApiClient: apiclient,
Status: "Allowed",
}
// Start network monitor
go netMonitor.MonitorNetwork(ctx, nflog, errc) // listens for NFLOG messages
WriteLog("before audit rules")
// Add logging to firewall, including NFLOG rules
if err := AddAuditRules(iptables); err != nil {
WriteLog(fmt.Sprintf("Error adding firewall rules %v", err))
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig, sudo)
return err
}
WriteLog("added audit rules")
} else if config.EgressPolicy == EgressPolicyBlock {
WriteLog("\n")
WriteLog(fmt.Sprintf("Allowed domains:%v", config.Endpoints))
WriteLog("\n")
netMonitor := NetworkMonitor{
CorrelationId: config.CorrelationId,
Repo: config.Repo,
ApiClient: apiclient,
Status: "Dropped",
}
// Start network monitor
go netMonitor.MonitorNetwork(ctx, nflog, errc) // listens for NFLOG messages
if err := addBlockRulesForGitHubHostedRunner(iptables, ipAddressEndpoints); err != nil {
WriteLog(fmt.Sprintf("Error setting firewall for allowed domains %v", err))
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig, sudo)
return err
}
go refreshDNSEntries(ctx, iptables, allowedEndpoints, &dnsProxy)
}
if config.DisableSudo {
err := sudo.disableSudo(tempDir)
if err != nil {
WriteAnnotation(fmt.Sprintf("%s Unable to disable sudo %v", StepSecurityAnnotationPrefix, err))
} else {
WriteLog("disabled sudo")
}
}
WriteLog("done")
// Write the status file
writeStatus("Initialized")
for {
select {
case <-ctx.Done():
return nil
case e := <-errc:
WriteLog(fmt.Sprintf("Error in Initialization %v", e))
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig, sudo)
return e
}
}
}
func refreshDNSEntries(ctx context.Context, iptables *Firewall, allowedEndpoints map[string][]Endpoint, dnsProxy *DNSProxy) {
ticker := time.NewTicker(30 * time.Second)
go func() {
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
WriteLog("\n")
WriteLog("Refreshing DNS entries")
for domainName, endpoints := range allowedEndpoints {
element, found := dnsProxy.Cache.Get(domainName)
if found {
var err error
var answer *Answer
// check if DNS TTL is close to expiry
if time.Now().Unix()+10-element.TimeAdded > int64(element.Value.TTL) {
// resolve domain name
answer, err = dnsProxy.ResolveDomain(domainName)
if err != nil {
// log and continue
WriteLog(fmt.Sprintf("domain could not be resolved: %s, %v", domainName, err))
continue
}
for _, endpoint := range endpoints {
// add endpoint to firewall
err = InsertAllowRule(iptables, answer.Data, fmt.Sprintf("%d", endpoint.port))
if err != nil {
break
}
}
if err != nil {
// log and continue
WriteLog(fmt.Sprintf("failed to insert new ipaddress in firewall: %s, %v", domainName, err))
continue
}
// add to cache with new TTL
dnsProxy.Cache.Set(domainName, answer, false)
WriteLog(fmt.Sprintf("domain resolved: %s, ip address: %s, TTL: %d", domainName, answer.Data, answer.TTL))
}
}
}
}
}
}()
}
func addImplicitEndpoints(endpoints map[string][]Endpoint, disableTelemetry bool) (map[string][]Endpoint, map[string][]Endpoint) {
normalEndpoints := make(map[string][]Endpoint)
wildcardEndpoints := make(map[string][]Endpoint)
implicitEndpoints := []Endpoint{
{domainName: "*.actions.githubusercontent.com.", port: 443}, // GitHub
{domainName: "codeload.github.com", port: 443}, // GitHub
{domainName: "actions-results-receiver-production.githubapp.com", port: 443}, // GitHub
{domainName: "productionresultssa*.blob.core.windows.net.", port: 443}, // GitHub
}
for key, val := range endpoints {
if isWildcardDomain(key) {
wildcardEndpoints[key] = append(wildcardEndpoints[key], val...)
} else {
normalEndpoints[key] = append(normalEndpoints[key], val...)
}
}
for _, endpoint := range implicitEndpoints {
if isWildcardDomain(endpoint.domainName) {
wildcardEndpoints[endpoint.domainName] = append(wildcardEndpoints[endpoint.domainName], endpoint)
} else {
normalEndpoints[endpoint.domainName] = append(normalEndpoints[endpoint.domainName], endpoint)
}
}
stepsecurity := Endpoint{domainName: "agent.api.stepsecurity.io", port: 443} // Should be implicit based on user feedback
if !disableTelemetry {
// allowing only if disable_telemetry is set to false
normalEndpoints[stepsecurity.domainName] = append(normalEndpoints[stepsecurity.domainName], stepsecurity)
}
return normalEndpoints, wildcardEndpoints
}
func RevertChanges(iptables *Firewall, nflog AgentNflogger,
cmd Command, resolvdConfigPath, dockerDaemonConfigPath string, dnsConfig DnsConfig, sudo Sudo) {
err := RevertFirewallChanges(iptables)
if err != nil {
WriteLog(fmt.Sprintf("Error in RevertChanges %v", err))
}
err = dnsConfig.RevertDNSServer(cmd, resolvdConfigPath)
if err != nil {
WriteLog(fmt.Sprintf("Error in reverting DNS server changes %v", err))
}
err = dnsConfig.RevertDockerDNSServer(cmd, dockerDaemonConfigPath)
if err != nil {
WriteLog(fmt.Sprintf("Error in reverting docker DNS server changes %v", err))
}
err = sudo.revertDisableSudo()
if err != nil {
WriteLog(fmt.Sprintf("Error in reverting sudo changes %v", err))
}
WriteLog("Reverted changes")
}
func writeStatus(message string) error {
dir := "/home/agent"
if _, err := os.Stat(dir); os.IsNotExist(err) {
_ = os.Mkdir(dir, 0644)
}
f, err := os.OpenFile("/home/agent/agent.status",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
f.WriteString(message)
return nil
}
func writeDone() error {
dir := "/home/agent"
if _, err := os.Stat(dir); os.IsNotExist(err) {
_ = os.Mkdir(dir, 0644)
}
f, err := os.OpenFile("/home/agent/done.json",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
f.WriteString("done.json")
return nil
}
func isWildcardDomain(domain string) bool {
return strings.ContainsAny(domain, "*")
}