@@ -65,15 +65,27 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
65
65
return err
66
66
}
67
67
68
- apiclient := & ApiClient {Client : & http.Client {}, APIURL : config .APIURL , DisableTelemetry : config .DisableTelemetry , EgressPolicy : config .EgressPolicy }
68
+ apiclient := & ApiClient {Client : & http.Client {Timeout : 3 * time . Second }, APIURL : config .APIURL , DisableTelemetry : config .DisableTelemetry , EgressPolicy : config .EgressPolicy }
69
69
70
70
// TODO: pass in an iowriter/ use log library
71
- WriteLog (fmt .Sprintf ("read config \n %v" , config ))
71
+ WriteLog (fmt .Sprintf ("read config \n %+ v" , config ))
72
72
WriteLog ("\n " )
73
73
74
74
WriteLog (fmt .Sprintf ("%s %s" , StepSecurityLogCorrelationPrefix , config .CorrelationId ))
75
75
WriteLog ("\n " )
76
76
77
+ // if this is a private repo
78
+ if config .Private {
79
+ isActive := apiclient .getSubscriptionStatus (config .Repo )
80
+ if ! isActive {
81
+ config .EgressPolicy = EgressPolicyAudit
82
+ config .DisableSudo = false
83
+ apiclient .DisableTelemetry = true
84
+ config .DisableFileMonitoring = true
85
+ WriteAnnotation ("StepSecurity Harden Runner disabled. A subscription is required for private repositories. Please start a free trial at https://stepsecurity.io" )
86
+ }
87
+ }
88
+
77
89
Cache := InitCache (config .EgressPolicy )
78
90
79
91
allowedEndpoints := addImplicitEndpoints (config .Endpoints , config .DisableTelemetry )
@@ -95,13 +107,13 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
95
107
// start proc mon
96
108
if cmd == nil {
97
109
procMon := & ProcessMonitor {CorrelationId : config .CorrelationId , Repo : config .Repo ,
98
- ApiClient : apiclient , WorkingDirectory : config .WorkingDirectory , DNSProxy : & dnsProxy }
110
+ ApiClient : apiclient , WorkingDirectory : config .WorkingDirectory , DisableFileMonitoring : config . DisableFileMonitoring , DNSProxy : & dnsProxy }
99
111
go procMon .MonitorProcesses (errc )
100
112
WriteLog ("started process monitor" )
101
113
}
102
114
103
115
dnsConfig := DnsConfig {}
104
-
116
+ sudo := Sudo {}
105
117
var ipAddressEndpoints []ipAddressEndpoint
106
118
107
119
// hydrate dns cache
@@ -112,7 +124,7 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
112
124
if err != nil {
113
125
WriteLog (fmt .Sprintf ("Error resolving allowed domain %v" , err ))
114
126
WriteAnnotation (fmt .Sprintf ("%s Reverting agent since allowed endpoint %s could not be resolved" , StepSecurityAnnotationPrefix , strings .Trim (domainName , "." )))
115
- RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
127
+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig , sudo )
116
128
return err
117
129
}
118
130
for _ , endpoint := range endpoints {
@@ -126,7 +138,7 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
126
138
// Change DNS config on host, causes processes to use agent's DNS proxy
127
139
if err := dnsConfig .SetDNSServer (cmd , resolvdConfigPath , tempDir ); err != nil {
128
140
WriteLog (fmt .Sprintf ("Error setting DNS server %v" , err ))
129
- RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
141
+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig , sudo )
130
142
return err
131
143
}
132
144
@@ -136,7 +148,7 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
136
148
// Change DNS for docker, causes process in containers to use agent's DNS proxy
137
149
if err := dnsConfig .SetDockerDNSServer (cmd , dockerDaemonConfigPath , tempDir ); err != nil {
138
150
WriteLog (fmt .Sprintf ("Error setting DNS server for docker %v" , err ))
139
- RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
151
+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig , sudo )
140
152
return err
141
153
}
142
154
@@ -159,7 +171,7 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
159
171
// Add logging to firewall, including NFLOG rules
160
172
if err := AddAuditRules (iptables ); err != nil {
161
173
WriteLog (fmt .Sprintf ("Error adding firewall rules %v" , err ))
162
- RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
174
+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig , sudo )
163
175
return err
164
176
}
165
177
@@ -182,13 +194,22 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
182
194
183
195
if err := addBlockRulesForGitHubHostedRunner (iptables , ipAddressEndpoints ); err != nil {
184
196
WriteLog (fmt .Sprintf ("Error setting firewall for allowed domains %v" , err ))
185
- RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
197
+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig , sudo )
186
198
return err
187
199
}
188
200
189
201
go refreshDNSEntries (ctx , iptables , allowedEndpoints , & dnsProxy )
190
202
}
191
203
204
+ if config .DisableSudo {
205
+ err := sudo .disableSudo (tempDir )
206
+ if err != nil {
207
+ WriteAnnotation (fmt .Sprintf ("%s Unable to disable sudo %v" , StepSecurityAnnotationPrefix , err ))
208
+ } else {
209
+ WriteLog ("disabled sudo" )
210
+ }
211
+ }
212
+
192
213
WriteLog ("done" )
193
214
194
215
// Write the status file
@@ -200,7 +221,7 @@ func Run(ctx context.Context, configFilePath string, hostDNSServer DNSServer,
200
221
return nil
201
222
case e := <- errc :
202
223
WriteLog (fmt .Sprintf ("Error in Initialization %v" , e ))
203
- RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig )
224
+ RevertChanges (iptables , nflog , cmd , resolvdConfigPath , dockerDaemonConfigPath , dnsConfig , sudo )
204
225
return e
205
226
206
227
}
@@ -284,7 +305,7 @@ func addImplicitEndpoints(endpoints map[string][]Endpoint, disableTelemetry bool
284
305
}
285
306
286
307
func RevertChanges (iptables * Firewall , nflog AgentNflogger ,
287
- cmd Command , resolvdConfigPath , dockerDaemonConfigPath string , dnsConfig DnsConfig ) {
308
+ cmd Command , resolvdConfigPath , dockerDaemonConfigPath string , dnsConfig DnsConfig , sudo Sudo ) {
288
309
err := RevertFirewallChanges (iptables )
289
310
if err != nil {
290
311
WriteLog (fmt .Sprintf ("Error in RevertChanges %v" , err ))
@@ -297,6 +318,10 @@ func RevertChanges(iptables *Firewall, nflog AgentNflogger,
297
318
if err != nil {
298
319
WriteLog (fmt .Sprintf ("Error in reverting docker DNS server changes %v" , err ))
299
320
}
321
+ err = sudo .revertDisableSudo ()
322
+ if err != nil {
323
+ WriteLog (fmt .Sprintf ("Error in reverting sudo changes %v" , err ))
324
+ }
300
325
WriteLog ("Reverted changes" )
301
326
}
302
327
0 commit comments