5
5
"fmt"
6
6
"net/http"
7
7
"os"
8
- "sync"
9
- "time"
10
8
11
9
"github.com/florianl/go-nflog/v2"
12
10
)
@@ -47,8 +45,6 @@ type IPTables interface {
47
45
ClearChain (table , chain string ) error
48
46
}
49
47
50
- var fileMutex sync.Mutex
51
-
52
48
// Run the agent
53
49
// TODO: move all inputs into a struct
54
50
func Run (ctx context.Context , configFilePath string , hostDNSServer DNSServer ,
@@ -67,15 +63,15 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
67
63
apiclient := & ApiClient {Client : & http.Client {}, APIURL : config .APIURL }
68
64
69
65
// TODO: pass in an iowriter/ use log library
70
- writeLog (fmt .Sprintf ("read config %v" , config ))
66
+ WriteLog (fmt .Sprintf ("read config %v" , config ))
71
67
72
- writeLog (fmt .Sprintf ("%s %s" , StepSecurityLogCorrelationPrefix , config .CorrelationId ))
68
+ WriteLog (fmt .Sprintf ("%s %s" , StepSecurityLogCorrelationPrefix , config .CorrelationId ))
73
69
74
70
// TODO: fix the cache and time
75
71
Cache := InitCache (10 * 60 * 1000000000 ) // 10 * 60 seconds
76
72
77
73
allowedEndpoints := addImplicitEndpoints (config .Endpoints )
78
-
74
+
79
75
// Start DNS servers and get confirmation
80
76
dnsProxy := DNSProxy {
81
77
Cache : & Cache ,
@@ -93,20 +89,20 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
93
89
if cmd == nil {
94
90
procMon := & ProcessMonitor {CorrelationId : config .CorrelationId , Repo : config .Repo , ApiClient : apiclient , WorkingDirectory : config .WorkingDirectory }
95
91
go procMon .MonitorProcesses (errc )
96
- writeLog ("started process monitor" )
92
+ WriteLog ("started process monitor" )
97
93
}
98
94
99
95
dnsConfig := DnsConfig {}
100
96
101
97
var ipAddressEndpoints []ipAddressEndpoint
102
-
98
+
103
99
// hydrate dns cache
104
100
if config .EgressPolicy == EgressPolicyBlock {
105
101
for _ , endpoint := range allowedEndpoints {
106
102
// this will cause domain, IP mapping to be cached
107
103
ipAddress , err := dnsProxy .getIPByDomain (endpoint .domainName )
108
104
if err != nil {
109
- writeLog (fmt .Sprintf ("Error resolving allowed domain %v" , err ))
105
+ WriteLog (fmt .Sprintf ("Error resolving allowed domain %v" , err ))
110
106
RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
111
107
return err
112
108
}
@@ -118,21 +114,21 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
118
114
119
115
// Change DNS config on host, causes processes to use agent's DNS proxy
120
116
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 ))
122
118
RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
123
119
return err
124
120
}
125
121
126
- writeLog ("updated resolved" )
122
+ WriteLog ("updated resolved" )
127
123
128
124
// Change DNS for docker, causes process in containers to use agent's DNS proxy
129
125
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 ))
131
127
RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
132
128
return err
133
129
}
134
130
135
- writeLog ("set docker config" )
131
+ WriteLog ("set docker config" )
136
132
137
133
if config .EgressPolicy == EgressPolicyAudit {
138
134
netMonitor := NetworkMonitor {
@@ -145,19 +141,19 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
145
141
// Start network monitor
146
142
go netMonitor .MonitorNetwork (nflog , errc ) // listens for NFLOG messages
147
143
//writeLog("started net monitor")
148
- writeLog ("before audit rules" )
144
+ WriteLog ("before audit rules" )
149
145
150
146
// Add logging to firewall, including NFLOG rules
151
147
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 ))
153
149
RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
154
150
return err
155
151
}
156
152
157
- writeLog ("added audit rules" )
153
+ WriteLog ("added audit rules" )
158
154
} else if config .EgressPolicy == EgressPolicyBlock {
159
155
160
- writeLog (fmt .Sprintf ("Allowed domains:%v" , config .Endpoints ))
156
+ WriteLog (fmt .Sprintf ("Allowed domains:%v" , config .Endpoints ))
161
157
162
158
netMonitor := NetworkMonitor {
163
159
CorrelationId : config .CorrelationId ,
@@ -170,13 +166,13 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
170
166
go netMonitor .MonitorNetwork (nflog , errc ) // listens for NFLOG messages
171
167
172
168
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 ))
174
170
RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
175
171
return err
176
172
}
177
173
}
178
174
179
- writeLog ("done" )
175
+ WriteLog ("done" )
180
176
181
177
// Write the status file
182
178
writeStatus ("Initialized" )
@@ -186,7 +182,7 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
186
182
case <- ctx .Done ():
187
183
return nil
188
184
case e := <- errc :
189
- writeLog (fmt .Sprintf ("Error in Initialization %v" , e ))
185
+ WriteLog (fmt .Sprintf ("Error in Initialization %v" , e ))
190
186
RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
191
187
return e
192
188
@@ -210,29 +206,17 @@ func RevertChanges(iptables *Firewall, nflog AgentNflogger,
210
206
cmd Command , resolvdConfigPath , dockerDaemonConfigPath string , dnsConfig DnsConfig ) {
211
207
err := RevertFirewallChanges (iptables )
212
208
if err != nil {
213
- writeLog (fmt .Sprintf ("Error in RevertChanges %v" , err ))
209
+ WriteLog (fmt .Sprintf ("Error in RevertChanges %v" , err ))
214
210
}
215
211
err = dnsConfig .RevertDNSServer (cmd , resolvdConfigPath )
216
212
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 ))
218
214
}
219
215
err = dnsConfig .RevertDockerDNSServer (cmd , dockerDaemonConfigPath )
220
216
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 ))
222
218
}
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" )
236
220
}
237
221
238
222
func writeStatus (message string ) {
0 commit comments