Skip to content

Commit 97975cb

Browse files
committed
Add annotation file
1 parent c8f4501 commit 97975cb

File tree

9 files changed

+76
-45
lines changed

9 files changed

+76
-45
lines changed

agent.go

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"fmt"
66
"net/http"
77
"os"
8-
"sync"
9-
"time"
108

119
"github.com/florianl/go-nflog/v2"
1210
)
@@ -47,8 +45,6 @@ type IPTables interface {
4745
ClearChain(table, chain string) error
4846
}
4947

50-
var fileMutex sync.Mutex
51-
5248
// Run the agent
5349
// TODO: move all inputs into a struct
5450
func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
@@ -67,15 +63,15 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
6763
apiclient := &ApiClient{Client: &http.Client{}, APIURL: config.APIURL}
6864

6965
// TODO: pass in an iowriter/ use log library
70-
writeLog(fmt.Sprintf("read config %v", config))
66+
WriteLog(fmt.Sprintf("read config %v", config))
7167

72-
writeLog(fmt.Sprintf("%s %s", StepSecurityLogCorrelationPrefix, config.CorrelationId))
68+
WriteLog(fmt.Sprintf("%s %s", StepSecurityLogCorrelationPrefix, config.CorrelationId))
7369

7470
// TODO: fix the cache and time
7571
Cache := InitCache(10 * 60 * 1000000000) // 10 * 60 seconds
7672

7773
allowedEndpoints := addImplicitEndpoints(config.Endpoints)
78-
74+
7975
// Start DNS servers and get confirmation
8076
dnsProxy := DNSProxy{
8177
Cache: &Cache,
@@ -93,20 +89,20 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
9389
if cmd == nil {
9490
procMon := &ProcessMonitor{CorrelationId: config.CorrelationId, Repo: config.Repo, ApiClient: apiclient, WorkingDirectory: config.WorkingDirectory}
9591
go procMon.MonitorProcesses(errc)
96-
writeLog("started process monitor")
92+
WriteLog("started process monitor")
9793
}
9894

9995
dnsConfig := DnsConfig{}
10096

10197
var ipAddressEndpoints []ipAddressEndpoint
102-
98+
10399
// hydrate dns cache
104100
if config.EgressPolicy == EgressPolicyBlock {
105101
for _, endpoint := range allowedEndpoints {
106102
// this will cause domain, IP mapping to be cached
107103
ipAddress, err := dnsProxy.getIPByDomain(endpoint.domainName)
108104
if err != nil {
109-
writeLog(fmt.Sprintf("Error resolving allowed domain %v", err))
105+
WriteLog(fmt.Sprintf("Error resolving allowed domain %v", err))
110106
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig)
111107
return err
112108
}
@@ -118,21 +114,21 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
118114

119115
// Change DNS config on host, causes processes to use agent's DNS proxy
120116
if err := dnsConfig.SetDNSServer(cmd, resolvdConfigPath, tempDir); err != nil {
121-
writeLog(fmt.Sprintf("Error setting DNS server %v", err))
117+
WriteLog(fmt.Sprintf("Error setting DNS server %v", err))
122118
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig)
123119
return err
124120
}
125121

126-
writeLog("updated resolved")
122+
WriteLog("updated resolved")
127123

128124
// Change DNS for docker, causes process in containers to use agent's DNS proxy
129125
if err := dnsConfig.SetDockerDNSServer(cmd, dockerDaemonConfigPath, tempDir); err != nil {
130-
writeLog(fmt.Sprintf("Error setting DNS server for docker %v", err))
126+
WriteLog(fmt.Sprintf("Error setting DNS server for docker %v", err))
131127
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig)
132128
return err
133129
}
134130

135-
writeLog("set docker config")
131+
WriteLog("set docker config")
136132

137133
if config.EgressPolicy == EgressPolicyAudit {
138134
netMonitor := NetworkMonitor{
@@ -145,19 +141,19 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
145141
// Start network monitor
146142
go netMonitor.MonitorNetwork(nflog, errc) // listens for NFLOG messages
147143
//writeLog("started net monitor")
148-
writeLog("before audit rules")
144+
WriteLog("before audit rules")
149145

150146
// Add logging to firewall, including NFLOG rules
151147
if err := AddAuditRules(iptables); err != nil {
152-
writeLog(fmt.Sprintf("Error adding firewall rules %v", err))
148+
WriteLog(fmt.Sprintf("Error adding firewall rules %v", err))
153149
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig)
154150
return err
155151
}
156152

157-
writeLog("added audit rules")
153+
WriteLog("added audit rules")
158154
} else if config.EgressPolicy == EgressPolicyBlock {
159155

160-
writeLog(fmt.Sprintf("Allowed domains:%v", config.Endpoints))
156+
WriteLog(fmt.Sprintf("Allowed domains:%v", config.Endpoints))
161157

162158
netMonitor := NetworkMonitor{
163159
CorrelationId: config.CorrelationId,
@@ -170,13 +166,13 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
170166
go netMonitor.MonitorNetwork(nflog, errc) // listens for NFLOG messages
171167

172168
if err := addBlockRulesForGitHubHostedRunner(ipAddressEndpoints); err != nil {
173-
writeLog(fmt.Sprintf("Error setting firewall for allowed domains %v", err))
169+
WriteLog(fmt.Sprintf("Error setting firewall for allowed domains %v", err))
174170
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig)
175171
return err
176172
}
177173
}
178174

179-
writeLog("done")
175+
WriteLog("done")
180176

181177
// Write the status file
182178
writeStatus("Initialized")
@@ -186,7 +182,7 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
186182
case <-ctx.Done():
187183
return nil
188184
case e := <-errc:
189-
writeLog(fmt.Sprintf("Error in Initialization %v", e))
185+
WriteLog(fmt.Sprintf("Error in Initialization %v", e))
190186
RevertChanges(iptables, nflog, cmd, resolvdConfigPath, dockerDaemonConfigPath, dnsConfig)
191187
return e
192188

@@ -210,29 +206,17 @@ func RevertChanges(iptables *Firewall, nflog AgentNflogger,
210206
cmd Command, resolvdConfigPath, dockerDaemonConfigPath string, dnsConfig DnsConfig) {
211207
err := RevertFirewallChanges(iptables)
212208
if err != nil {
213-
writeLog(fmt.Sprintf("Error in RevertChanges %v", err))
209+
WriteLog(fmt.Sprintf("Error in RevertChanges %v", err))
214210
}
215211
err = dnsConfig.RevertDNSServer(cmd, resolvdConfigPath)
216212
if err != nil {
217-
writeLog(fmt.Sprintf("Error in reverting DNS server changes %v", err))
213+
WriteLog(fmt.Sprintf("Error in reverting DNS server changes %v", err))
218214
}
219215
err = dnsConfig.RevertDockerDNSServer(cmd, dockerDaemonConfigPath)
220216
if err != nil {
221-
writeLog(fmt.Sprintf("Error in reverting docker DNS server changes %v", err))
217+
WriteLog(fmt.Sprintf("Error in reverting docker DNS server changes %v", err))
222218
}
223-
writeLog("Reverted changes")
224-
}
225-
226-
func writeLog(message string) {
227-
fileMutex.Lock()
228-
defer fileMutex.Unlock()
229-
230-
f, _ := os.OpenFile("/home/agent/agent.log",
231-
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
232-
233-
defer f.Close()
234-
235-
f.WriteString(fmt.Sprintf("%s:%s\n", time.Now().String(), message))
219+
WriteLog("Reverted changes")
236220
}
237221

238222
func writeStatus(message string) {

annotation.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"sync"
7+
)
8+
9+
var annotationMutex sync.Mutex
10+
11+
const AnnotationError = "error"
12+
13+
func WriteAnnotation(annotationType, message string) {
14+
annotationMutex.Lock()
15+
defer annotationMutex.Unlock()
16+
17+
f, _ := os.OpenFile("/home/agent/annotation.log",
18+
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
19+
20+
defer f.Close()
21+
22+
f.WriteString(fmt.Sprintf("%s:%s\n", annotationType, message))
23+
}

dnsproxy.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ func (proxy *DNSProxy) getIPByDomain(domain string) (string, error) {
9696

9797
if proxy.EgressPolicy == EgressPolicyBlock {
9898
if !proxy.isAllowedDomain(domain) {
99-
go writeLog(fmt.Sprintf("domain not allowed: %s", domain))
99+
go WriteLog(fmt.Sprintf("domain not allowed: %s", domain))
100+
go WriteAnnotation(AnnotationError, fmt.Sprintf("DNS resolution for domain %s was blocked", domain))
100101
return "", fmt.Errorf("domain not allowed %s", domain)
101102
}
102103
}
@@ -127,7 +128,7 @@ func (proxy *DNSProxy) getIPByDomain(domain string) (string, error) {
127128
if answer.Type == 1 {
128129
proxy.Cache.Set(domain, answer.Data)
129130

130-
go writeLog(fmt.Sprintf("domain resolved: %s, ip address: %s", domain, answer.Data))
131+
go WriteLog(fmt.Sprintf("domain resolved: %s, ip address: %s", domain, answer.Data))
131132

132133
go proxy.ApiClient.sendDNSRecord(proxy.CorrelationId, proxy.Repo, domain, answer.Data)
133134

eventhandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (eventHandler *EventHandler) handleFileEvent(event *Event) {
3939
}
4040

4141
if strings.Contains(event.FileName, "post_event.json") {
42-
writeLog("post_event called")
42+
WriteLog("post_event called")
4343

4444
// send done signal to post step
4545
writeDone()

log.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"sync"
7+
"time"
8+
)
9+
10+
var logMutex sync.Mutex
11+
12+
func WriteLog(message string) {
13+
logMutex.Lock()
14+
defer logMutex.Unlock()
15+
16+
f, _ := os.OpenFile("/home/agent/agent.log",
17+
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
18+
19+
defer f.Close()
20+
21+
f.WriteString(fmt.Sprintf("%s:%s\n", time.Now().String(), message))
22+
}

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ func main() {
3434
case syscall.SIGHUP:
3535
c.init(agentConfigFilePath)
3636
case os.Interrupt:
37-
writeLog("got os.kill")
37+
WriteLog("got os.kill")
3838
cancel()
3939
os.Exit(1)
4040
}
4141
case <-ctx.Done():
42-
writeLog("called ctx.Done()")
42+
WriteLog("called ctx.Done()")
4343
os.Exit(1)
4444
}
4545
}

netmon.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ func (netMonitor *NetworkMonitor) handlePacket(attrs nflog.Attribute) {
9797
ipv4.DstIP.String(), port, netMonitor.Status, timestamp, Tool{Name: Unknown, SHA256: Unknown})
9898

9999
if netMonitor.Status == "Dropped" {
100-
go writeLog(fmt.Sprintf("ip address dropped: %s", ipv4.DstIP.String()))
100+
go WriteLog(fmt.Sprintf("ip address dropped: %s", ipv4.DstIP.String()))
101+
go WriteAnnotation(AnnotationError, fmt.Sprintf("Traffic to IP Address %s was blocked", ipv4.DstIP.String()))
101102
}
102103
}
103104
}

procmon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (p *ProcessMonitor) PrepareEvent(sequence int, eventMap map[string]interfac
9494
argCountStr := fmt.Sprintf("%v", argc)
9595
argCount, err := strconv.Atoi(argCountStr)
9696
if err != nil {
97-
writeLog(fmt.Sprintf("could not parse argc:%v", argc))
97+
WriteLog(fmt.Sprintf("could not parse argc:%v", argc))
9898
}
9999
for i := 0; i < argCount; i++ {
100100
p.Events[sequence].ProcessArguments = append(p.Events[sequence].ProcessArguments, fmt.Sprintf("%v", eventMap[fmt.Sprintf("a%d", i)]))

procmon_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func (p *ProcessMonitor) MonitorProcesses(errc chan error) {
11-
writeLog("Monitor Processes called")
11+
WriteLog("Monitor Processes called")
1212
}
1313

1414
func getParentProcessId(pid string) (int, error) {

0 commit comments

Comments
 (0)