@@ -3,7 +3,6 @@ package main
33import (
44 "context"
55 "fmt"
6- "io"
76 "net/http"
87 "os"
98 "time"
@@ -47,7 +46,7 @@ type IPTables interface {
4746// TODO: move all inputs into a struct
4847func Run (ctx context.Context , configFilePath string , hostDNSServer DNSServer ,
4948 dockerDNSServer DNSServer , iptables * Firewall , nflog AgentNflogger ,
50- cmd Command , resolvdConfigPath , dockerDaemonConfigPath string , stdout io. Writer ) error {
49+ cmd Command , resolvdConfigPath , dockerDaemonConfigPath , tempDir string ) error {
5150
5251 // Passed to each go routine, if anyone fails, the program fails
5352 errc := make (chan error )
@@ -79,6 +78,32 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
7978 go startDNSServer (dnsProxy , hostDNSServer , errc )
8079 go startDNSServer (dnsProxy , dockerDNSServer , errc ) // this is for the docker bridge
8180
81+ if cmd == nil {
82+ procMon := & ProcessMonitor {CorrelationId : config .CorrelationId , Repo : config .Repo , ApiClient : apiclient , WorkingDirectory : config .WorkingDirectory }
83+ go procMon .MonitorProcesses (errc )
84+ writeLog ("started p monitor" )
85+ }
86+
87+ dnsConfig := DnsConfig {}
88+
89+ // Change DNS config on host, causes processes to use agent's DNS proxy
90+ if err := dnsConfig .SetDNSServer (cmd , resolvdConfigPath , tempDir ); err != nil {
91+ writeLog (fmt .Sprintf ("Error setting DNS server %v" , err ))
92+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
93+ return err
94+ }
95+
96+ writeLog ("updated resolved" )
97+
98+ // Change DNS for docker, causes process in containers to use agent's DNS proxy
99+ if err := dnsConfig .SetDockerDNSServer (cmd , dockerDaemonConfigPath , tempDir ); err != nil {
100+ writeLog (fmt .Sprintf ("Error setting DNS server for docker %v" , err ))
101+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
102+ return err
103+ }
104+
105+ writeLog ("set docker config" )
106+
82107 if len (config .Endpoints ) == 0 {
83108 netMonitor := NetworkMonitor {
84109 CorrelationId : config .CorrelationId ,
@@ -93,37 +118,15 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
93118 writeLog ("before audit rules" )
94119
95120 // Add logging to firewall, including NFLOG rules
96- if err := addAuditRules (iptables ); err != nil {
121+ if err := AddAuditRules (iptables ); err != nil {
97122 writeLog (fmt .Sprintf ("Error adding firewall rules %v" , err ))
123+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
98124 return err
99125 }
100126
101127 writeLog ("added audit rules" )
102128 }
103129
104- // TODO: If something did not work, revert settings
105- if cmd == nil {
106- procMon := & ProcessMonitor {CorrelationId : config .CorrelationId , Repo : config .Repo , ApiClient : apiclient , WorkingDirectory : config .WorkingDirectory }
107- go procMon .MonitorProcesses (errc )
108- writeLog ("started p monitor" )
109- }
110-
111- // Change DNS config on host, causes processes to use agent's DNS proxy
112- if err := setDNSServer (cmd , resolvdConfigPath ); err != nil {
113- writeLog (fmt .Sprintf ("Error setting DNS server %v" , err ))
114- return err
115- }
116-
117- writeLog ("updated resolved" )
118-
119- // Change DNS for docker, causes process in containers to use agent's DNS proxy
120- if err := setDockerDNSServer (cmd , dockerDaemonConfigPath ); err != nil {
121- writeLog (fmt .Sprintf ("Error setting DNS server for docker %v" , err ))
122- return err
123- }
124-
125- writeLog ("set docker config" )
126-
127130 // If allowed endpoints set, resolve them, and add to firewall
128131 if len (config .Endpoints ) > 0 {
129132 var ipAddressEndpoints []ipAddressEndpoint
@@ -145,6 +148,7 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
145148 ipAddress , err := dnsProxy .getIPByDomain (endpoint .domainName )
146149 if err != nil {
147150 writeLog (fmt .Sprintf ("Error resolving allowed domain %v" , err ))
151+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
148152 return err
149153 }
150154
@@ -154,15 +158,11 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
154158
155159 if err := addBlockRulesForGitHubHostedRunner (ipAddressEndpoints ); err != nil {
156160 writeLog (fmt .Sprintf ("Error setting firewall for allowed domains %v" , err ))
161+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
157162 return err
158163 }
159164 }
160165
161- // Ask API to monitor the run
162- go apiclient .monitorRun (config .Repo , config .RunId )
163-
164- writeLog ("called monitor run" )
165-
166166 writeLog ("done" )
167167
168168 // Write the status file
@@ -173,13 +173,31 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
173173 case <- ctx .Done ():
174174 return nil
175175 case e := <- errc :
176- writeLog (e .Error ())
176+ writeLog (fmt .Sprintf ("Error in Initialization %v" , e ))
177+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
177178 return e
178179
179180 }
180181 }
181182}
182183
184+ func RevertChanges (iptables * Firewall , nflog AgentNflogger ,
185+ cmd Command , resolvdConfigPath , dockerDaemonConfigPath string , dnsConfig DnsConfig ) {
186+ err := RevertFirewallChanges (iptables )
187+ if err != nil {
188+ writeLog (fmt .Sprintf ("Error in RevertChanges %v" , err ))
189+ }
190+ err = dnsConfig .RevertDNSServer (cmd , resolvdConfigPath )
191+ if err != nil {
192+ writeLog (fmt .Sprintf ("Error in reverting DNS server changes %v" , err ))
193+ }
194+ err = dnsConfig .RevertDockerDNSServer (cmd , dockerDaemonConfigPath )
195+ if err != nil {
196+ writeLog (fmt .Sprintf ("Error in reverting docker DNS server changes %v" , err ))
197+ }
198+ writeLog ("Reverted changes" )
199+ }
200+
183201func writeLog (message string ) {
184202 f , _ := os .OpenFile ("/home/agent/agent.log" ,
185203 os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
0 commit comments