Skip to content

Commit f78b9cd

Browse files
Merge pull request #67 from step-security/feature-56
Revert changes on failure
2 parents 165ef71 + a985f44 commit f78b9cd

17 files changed

+296
-267
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
# Learn more about CodeQL language support at https://git.io/codeql-language-support
3838

3939
steps:
40-
- uses: step-security/harden-runner@7206db2ec98c5538323a6d70e51f965d55c11c87
40+
- uses: step-security/harden-runner@917f7d59f22e82a5ddcaef409923426fd7aa6327
4141
- name: Checkout repository
4242
uses: actions/checkout@629c2de402a417ea7690ca6ce3f33229e27606a5
4343

.github/workflows/int.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
contents: read
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: step-security/harden-runner@7206db2ec98c5538323a6d70e51f965d55c11c87
16+
- uses: step-security/harden-runner@917f7d59f22e82a5ddcaef409923426fd7aa6327
1717
with:
1818
allowed-endpoints:
1919
api.github.com:443

.github/workflows/release.yml

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,38 @@ name: Release
22

33
on:
44
push:
5-
branches:
6-
- main
5+
tags:
6+
- '*'
77

88
permissions: read-all
99

1010
jobs:
1111
release:
1212
permissions:
13-
contents: read
13+
contents: write
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: step-security/harden-runner@7206db2ec98c5538323a6d70e51f965d55c11c87
16+
- uses: step-security/harden-runner@917f7d59f22e82a5ddcaef409923426fd7aa6327
1717
with:
18-
allowed-endpoints:
19-
api.github.com:443
20-
beta.api.stepsecurity.io:443
21-
github.com:443
22-
int.api.stepsecurity.io:443
23-
pipelines.actions.githubusercontent.com:443
24-
proxy.golang.org:443
25-
step-security-agent.s3.us-west-2.amazonaws.com:443
26-
storage.googleapis.com:443
27-
sts.us-west-2.amazonaws.com:443
18+
allowed-endpoints:
19+
agent.api.stepsecurity.io:443
20+
api.github.com:443
21+
github.com:443
22+
goreleaser.com:443
23+
objects.githubusercontent.com:443
24+
proxy.golang.org:443
25+
storage.googleapis.com:443
26+
uploads.github.com:443
2827
- name: Checkout
2928
uses: actions/checkout@629c2de402a417ea7690ca6ce3f33229e27606a5
3029
- name: Set up Go
3130
uses: actions/setup-go@37335c7bb261b353407cff977110895fa0b4f7d8
3231
with:
33-
go-version: 1.17
34-
- run: go build -ldflags="-s -w" -o ./agent
35-
- name: Configure aws credentials
36-
uses: aws-actions/configure-aws-credentials@ea7b857d8a33dc2fb4ef5a724500044281b49a5e
32+
go-version: 1.17
33+
- uses: goreleaser/goreleaser-action@5df302e5e9e4c66310a6b6493a8865b12c555af2
3734
with:
38-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
39-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
40-
aws-region: us-west-2
41-
- run: aws s3 cp ./agent s3://step-security-agent/refs/heads/int/agent --acl public-read
42-
- name: Integration test
43-
uses: docker://ghcr.io/step-security/integration-test/int:latest
35+
distribution: goreleaser
36+
version: latest
37+
args: release --rm-dist
4438
env:
45-
PAT: ${{ secrets.PAT }}
46-
- run: aws s3 cp ./agent s3://step-security-agent/refs/heads/agent/agent --acl public-read
39+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/scorecard-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
security-events: write
1919

2020
steps:
21-
- uses: step-security/harden-runner@7206db2ec98c5538323a6d70e51f965d55c11c87
21+
- uses: step-security/harden-runner@917f7d59f22e82a5ddcaef409923426fd7aa6327
2222
- name: "Checkout code"
2323
uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579
2424

.github/workflows/test.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,12 @@ jobs:
1010
contents: read
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: step-security/harden-runner@7206db2ec98c5538323a6d70e51f965d55c11c87
14-
with:
15-
allowed-endpoints:
16-
beta.api.stepsecurity.io:443
17-
codecov.io:443
18-
github.com:443
19-
proxy.golang.org:443
20-
storage.googleapis.com:443
2113
- name: Checkout
2214
uses: actions/checkout@629c2de402a417ea7690ca6ce3f33229e27606a5
2315
- name: Set up Go
2416
uses: actions/setup-go@37335c7bb261b353407cff977110895fa0b4f7d8
2517
with:
2618
go-version: 1.17
2719
- name: Run coverage
28-
run: sudo go test -coverprofile=coverage.txt -covermode=atomic
20+
run: sudo CI=true go test -race -coverprofile=coverage.txt -covermode=atomic
2921
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b

.goreleaser.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# .goreleaser.yml
2+
builds:
3+
# You can have multiple builds defined as a yaml list
4+
-
5+
# GOOS list to build for.
6+
# For more info refer to: https://golang.org/doc/install/source#environment
7+
# Defaults are darwin and linux.
8+
goos:
9+
- linux
10+
11+
# GOARCH to build for.
12+
# For more info refer to: https://golang.org/doc/install/source#environment
13+
# Defaults are 386, amd64 and arm64.
14+
goarch:
15+
- amd64
16+
17+
# Optionally override the matrix generation and specify only the final list of targets.
18+
# Format is `{goos}_{goarch}` with optionally a suffix with `_{goarm}` or `_{gomips}`.
19+
# This overrides `goos`, `goarch`, `goarm`, `gomips` and `ignores`.
20+
targets:
21+
- linux_amd64

agent.go

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
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
4847
func 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+
183201
func 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

Comments
 (0)