55 "fmt"
66 "net/http"
77 "os"
8+ "sync"
89 "time"
910
1011 "github.com/florianl/go-nflog/v2"
@@ -46,6 +47,8 @@ type IPTables interface {
4647 ClearChain (table , chain string ) error
4748}
4849
50+ var fileMutex sync.Mutex
51+
4952// Run the agent
5053// TODO: move all inputs into a struct
5154func Run (ctx context.Context , configFilePath string , hostDNSServer DNSServer ,
@@ -90,6 +93,23 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
9093
9194 dnsConfig := DnsConfig {}
9295
96+ var ipAddressEndpoints []ipAddressEndpoint
97+ if config .EgressPolicy == EgressPolicyBlock {
98+ endpoints := addImplicitEndpoints (config .Endpoints )
99+ for _ , endpoint := range endpoints {
100+ // this will cause domain, IP mapping to be cached
101+ ipAddress , err := dnsProxy .getIPByDomain (endpoint .domainName )
102+ if err != nil {
103+ writeLog (fmt .Sprintf ("Error resolving allowed domain %v" , err ))
104+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
105+ return err
106+ }
107+
108+ // create list of ip address to be added to firewall
109+ ipAddressEndpoints = append (ipAddressEndpoints , ipAddressEndpoint {ipAddress : ipAddress , port : fmt .Sprintf ("%d" , endpoint .port )})
110+ }
111+ }
112+
93113 // Change DNS config on host, causes processes to use agent's DNS proxy
94114 if err := dnsConfig .SetDNSServer (cmd , resolvdConfigPath , tempDir ); err != nil {
95115 writeLog (fmt .Sprintf ("Error setting DNS server %v" , err ))
@@ -130,7 +150,6 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
130150
131151 writeLog ("added audit rules" )
132152 } else if config .EgressPolicy == EgressPolicyBlock {
133- var ipAddressEndpoints []ipAddressEndpoint
134153
135154 writeLog (fmt .Sprintf ("Allowed domains:%v" , config .Endpoints ))
136155
@@ -143,19 +162,6 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
143162
144163 // Start network monitor
145164 go netMonitor .MonitorNetwork (nflog , errc ) // listens for NFLOG messages
146- endpoints := addImplicitEndpoints (config .Endpoints )
147- for _ , endpoint := range endpoints {
148- // this will cause domain, IP mapping to be cached
149- ipAddress , err := dnsProxy .getIPByDomain (endpoint .domainName )
150- if err != nil {
151- writeLog (fmt .Sprintf ("Error resolving allowed domain %v" , err ))
152- RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
153- return err
154- }
155-
156- // create list of ip address to be added to firewall
157- ipAddressEndpoints = append (ipAddressEndpoints , ipAddressEndpoint {ipAddress : ipAddress , port : fmt .Sprintf ("%d" , endpoint .port )})
158- }
159165
160166 if err := addBlockRulesForGitHubHostedRunner (ipAddressEndpoints ); err != nil {
161167 writeLog (fmt .Sprintf ("Error setting firewall for allowed domains %v" , err ))
@@ -212,6 +218,9 @@ func RevertChanges(iptables *Firewall, nflog AgentNflogger,
212218}
213219
214220func writeLog (message string ) {
221+ fileMutex .Lock ()
222+ defer fileMutex .Unlock ()
223+
215224 f , _ := os .OpenFile ("/home/agent/agent.log" ,
216225 os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
217226
0 commit comments