Skip to content

Commit 446d358

Browse files
authored
chore: restrict worker network traffic (#179)
1 parent 8be11fe commit 446d358

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

.github/workflows/nodejs.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
branches: [ master ]
77
jobs:
88
test:
9-
runs-on: ubuntu-latest
9+
runs-on: ubuntu-20.04
1010
timeout-minutes: 60
1111
steps:
1212
- uses: microsoft/playwright-github-action@v1
@@ -45,7 +45,7 @@ jobs:
4545
if: ${{ failure() }}
4646
uses: mxschmitt/action-tmate@v3
4747
lint:
48-
runs-on: ubuntu-latest
48+
runs-on: ubuntu-20.04
4949
strategy:
5050
fail-fast: false
5151
matrix:
@@ -67,7 +67,7 @@ jobs:
6767
run: npm run build
6868
build:
6969
#needs: test
70-
runs-on: ubuntu-latest
70+
runs-on: ubuntu-20.04
7171
if: github.event_name == 'push'
7272
strategy:
7373
fail-fast: false

control-service/workers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,16 @@ func (w *Worker) createPod() error {
143143
ObjectMeta: metav1.ObjectMeta{
144144
GenerateName: "worker-",
145145
Labels: map[string]string{
146-
"pod-name": "nginx",
146+
"role": "worker",
147147
},
148148
},
149149
Spec: v1.PodSpec{
150150
RestartPolicy: v1.RestartPolicy(v1.RestartPolicyNever),
151151
Containers: []v1.Container{
152152
{
153-
Name: "worker",
154-
Image: "ghcr.io/mxschmitt/try-playwright/worker:latest",
153+
Name: "worker",
154+
Image: "ghcr.io/mxschmitt/try-playwright/worker:latest",
155+
ImagePullPolicy: v1.PullIfNotPresent,
155156
Env: []v1.EnvVar{
156157
{
157158
Name: "WORKER_ID",

e2e/try-playwright.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,25 @@ const playwright = require("playwright");
177177
await page.click("text='Run'")
178178
await page.waitForSelector("text='Error: foobar!'")
179179
})
180+
it("should prevent access to the control microservice from inside the worker", async ({ page }) => {
181+
await page.goto(ROOT_URL);
182+
await page.waitForTimeout(200)
183+
await page.evaluate(() => {
184+
// @ts-ignore
185+
window.monacoEditorModel.setValue(`// @ts-check
186+
const playwright = require('playwright');
187+
188+
(async () => {
189+
const browser = await playwright.chromium.launch();
190+
const page = await browser.newPage();
191+
await page.goto('http://control:8080/service/control/health');
192+
await browser.close();
193+
})();`)
194+
})
195+
await page.waitForTimeout(200)
196+
await Promise.all([
197+
page.waitForSelector("text=ERR_CONNECTION_REFUSED"),
198+
page.click("text='Run'")
199+
])
200+
})
180201
})

k8/control-deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ spec:
2929
value: https://c4698982912c457ba9c9a2a815a8bb25@o359550.ingest.sentry.io/5479806
3030
image: ghcr.io/mxschmitt/try-playwright/control-service:latest
3131
name: control
32-
imagePullPolicy: Always
32+
imagePullPolicy: IfNotPresent
3333
restartPolicy: Always

k8/file-deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ spec:
1313
metadata:
1414
labels:
1515
io.kompose.service: file
16+
reachable-by-worker: "true"
1617
spec:
1718
containers:
1819
- env:

k8/worker-networkpolicy.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: NetworkPolicy
3+
metadata:
4+
name: deny-worker-traffic
5+
spec:
6+
podSelector:
7+
matchLabels:
8+
role: worker
9+
egress:
10+
- to:
11+
- ipBlock:
12+
cidr: 0.0.0.0/0
13+
except:
14+
- 10.0.0.0/8
15+
- 192.168.0.0/16
16+
- 172.16.0.0/20
17+
- podSelector:
18+
matchLabels:
19+
reachable-by-worker: "true"
20+
- to:
21+
ports:
22+
- protocol: TCP
23+
port: 53
24+
- protocol: UDP
25+
port: 53

0 commit comments

Comments
 (0)