Skip to content

Commit

Permalink
chore: restrict worker network traffic (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Mar 16, 2021
1 parent 8be11fe commit 446d358
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
branches: [ master ]
jobs:
test:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
timeout-minutes: 60
steps:
- uses: microsoft/playwright-github-action@v1
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
lint:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
Expand All @@ -67,7 +67,7 @@ jobs:
run: npm run build
build:
#needs: test
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
if: github.event_name == 'push'
strategy:
fail-fast: false
Expand Down
7 changes: 4 additions & 3 deletions control-service/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,16 @@ func (w *Worker) createPod() error {
ObjectMeta: metav1.ObjectMeta{
GenerateName: "worker-",
Labels: map[string]string{
"pod-name": "nginx",
"role": "worker",
},
},
Spec: v1.PodSpec{
RestartPolicy: v1.RestartPolicy(v1.RestartPolicyNever),
Containers: []v1.Container{
{
Name: "worker",
Image: "ghcr.io/mxschmitt/try-playwright/worker:latest",
Name: "worker",
Image: "ghcr.io/mxschmitt/try-playwright/worker:latest",
ImagePullPolicy: v1.PullIfNotPresent,
Env: []v1.EnvVar{
{
Name: "WORKER_ID",
Expand Down
21 changes: 21 additions & 0 deletions e2e/try-playwright.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,25 @@ const playwright = require("playwright");
await page.click("text='Run'")
await page.waitForSelector("text='Error: foobar!'")
})
it("should prevent access to the control microservice from inside the worker", async ({ page }) => {
await page.goto(ROOT_URL);
await page.waitForTimeout(200)
await page.evaluate(() => {
// @ts-ignore
window.monacoEditorModel.setValue(`// @ts-check
const playwright = require('playwright');
(async () => {
const browser = await playwright.chromium.launch();
const page = await browser.newPage();
await page.goto('http://control:8080/service/control/health');
await browser.close();
})();`)
})
await page.waitForTimeout(200)
await Promise.all([
page.waitForSelector("text=ERR_CONNECTION_REFUSED"),
page.click("text='Run'")
])
})
})
2 changes: 1 addition & 1 deletion k8/control-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ spec:
value: https://[email protected]/5479806
image: ghcr.io/mxschmitt/try-playwright/control-service:latest
name: control
imagePullPolicy: Always
imagePullPolicy: IfNotPresent
restartPolicy: Always
1 change: 1 addition & 0 deletions k8/file-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ spec:
metadata:
labels:
io.kompose.service: file
reachable-by-worker: "true"
spec:
containers:
- env:
Expand Down
25 changes: 25 additions & 0 deletions k8/worker-networkpolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-worker-traffic
spec:
podSelector:
matchLabels:
role: worker
egress:
- to:
- ipBlock:
cidr: 0.0.0.0/0
except:
- 10.0.0.0/8
- 192.168.0.0/16
- 172.16.0.0/20
- podSelector:
matchLabels:
reachable-by-worker: "true"
- to:
ports:
- protocol: TCP
port: 53
- protocol: UDP
port: 53

0 comments on commit 446d358

Please sign in to comment.