Skip to content

Commit a8677ec

Browse files
authored
tests: adds E2E test to ensure functionality (#2)
* tests: adds E2E test. * tests: fixes envoy config. * tests: fixes wasm filter location. * chore: fixes cache. * chore: changes cache version. * fix: fixes cache by not using a container in build step. This is to mitigate the problem where cache action does not mount the cache folder as per in https://stackoverflow.com/questions/66653352/github-action-cache-question-with-containers. * fix: attempts to use env var rightly. * fix: git installation. * chore: uses sudo for dpkg. * chore: removes usage of scheduler. * chore: attempts to parse wasm-tools as suggested by @mathetake * chore: attempts to fix build. * chore: validate and parse wasm generated. * chore: drops scheduler. * chore: attempts to fix build removing logging. * feat: get extension working. * chore: adds label in docker image. * fix: checks out the code for using the Dockerfile. * fix: ls -la build folder. * chore: removes ls and adds very basic dockerignore.
1 parent fa82b56 commit a8677ec

16 files changed

+334
-212
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.git
2+
*.md
3+
LICENSE

.dockerignore.Dockerfile.server-test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
LICENSE
3+
**/.git

.github/workflows/ci.yaml

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths-ignore:
7+
- "**/*.md"
8+
- "LICENSE"
9+
pull_request:
10+
11+
env:
12+
GO_VERSION: 1.18
13+
TINYGO_VERSION: 0.23.0
14+
15+
jobs:
16+
test:
17+
runs-on: ubuntu-20.04
18+
steps:
19+
# Set fetch-depth: 0 to fetch commit history and tags for use in version calculation
20+
- name: Check out code
21+
uses: actions/[email protected]
22+
with:
23+
fetch-depth: 0
24+
submodules: true
25+
26+
- name: Install Go
27+
uses: actions/setup-go@v1
28+
with:
29+
go-version: ${{ env.GO_VERSION }}
30+
31+
- name: Run tests
32+
shell: bash
33+
run: make test
34+
35+
build:
36+
runs-on: ubuntu-20.04
37+
needs: test
38+
steps:
39+
# submodule needs .git folder, which is missing without installing a newer git command
40+
# https://github.com/actions/checkout/issues/335
41+
- name: "Install latest `git`"
42+
run: |
43+
sudo apt purge git -y
44+
sudo apt-get update && sudo apt-get install -y software-properties-common make
45+
sudo add-apt-repository ppa:git-core/ppa -y
46+
# apt update fails to fetch some repo due to cert failure. Skip them.
47+
sudo apt update || true; sudo apt install -y --no-install-recommends git
48+
49+
- name: Check out code
50+
uses: actions/[email protected]
51+
with:
52+
fetch-depth: 0
53+
submodules: true
54+
55+
- name: Install Go
56+
uses: actions/setup-go@v3
57+
with:
58+
go-version: ${{ env.GO_VERSION }}
59+
60+
- name: Install TinyGo
61+
run: |
62+
wget https://github.com/tinygo-org/tinygo/releases/download/v${TINYGO_VERSION}/tinygo_${TINYGO_VERSION}_amd64.deb
63+
sudo dpkg -i tinygo_${TINYGO_VERSION}_amd64.deb
64+
export PATH=$PATH:/usr/local/bin
65+
66+
- name: "Cache generated .wasm file"
67+
uses: actions/cache@v2
68+
with:
69+
path: |
70+
build/main.wasm
71+
key: ${{ runner.os }}-cache-build-${{ github.sha }}
72+
73+
- name: Build WASM filter
74+
shell: bash
75+
run: make build
76+
77+
- name: Install WASM
78+
uses: actions-rs/toolchain@v1
79+
with:
80+
toolchain: stable
81+
82+
- name: Install Cargo
83+
uses: actions-rs/cargo@v1
84+
with:
85+
command: install
86+
args: wasm-tools
87+
88+
- name: Validate WASM output
89+
shell: bash
90+
run: wasm-tools validate build/main.wasm
91+
92+
e2e-test:
93+
runs-on: ubuntu-20.04
94+
needs: build
95+
steps:
96+
- name: "Checkout"
97+
uses: actions/checkout@v2
98+
with:
99+
fetch-depth: 0
100+
101+
- name: "Install func-e"
102+
shell: bash
103+
run: curl https://func-e.io/install.sh | bash -s -- -b /usr/local/bin
104+
105+
- name: "Restore the wasm files cache"
106+
uses: actions/cache@v2
107+
with:
108+
path: |
109+
build/main.wasm
110+
key: ${{ runner.os }}-cache-build-${{ github.sha }}
111+
112+
- name: "Verify build"
113+
shell: bash
114+
run: test -f build/main.wasm
115+
116+
- name: "Spin up server and envoy"
117+
shell: bash
118+
run: |
119+
func-e run -c e2e/envoy-config.yaml --log-level info --component-log-level wasm:debug &
120+
121+
- name: "Run tests"
122+
shell: bash
123+
run: |
124+
./e2e/tests.sh
125+
126+
package:
127+
runs-on: ubuntu-20.04
128+
needs: e2e-test
129+
steps:
130+
- name: "Checkout"
131+
uses: actions/checkout@v2
132+
with:
133+
fetch-depth: 0
134+
135+
- name: "Restore the wasm files cache"
136+
uses: actions/cache@v2
137+
with:
138+
path: |
139+
build/main.wasm
140+
key: ${{ runner.os }}-cache-build-${{ github.sha }}
141+
142+
- name: Set up QEMU
143+
uses: docker/setup-qemu-action@v2
144+
145+
- name: Set up Docker Buildx
146+
uses: docker/setup-buildx-action@v2
147+
148+
- name: Login to DockerHub
149+
uses: docker/login-action@v2
150+
with:
151+
username: ${{ secrets.DOCKERHUB_USERNAME }}
152+
password: ${{ secrets.DOCKERHUB_TOKEN }}
153+
154+
- name: Build and push
155+
uses: docker/build-push-action@v3
156+
with:
157+
context: .
158+
push: ${{ github.event_name == 'push' }}
159+
tags: jcchavezs/coraza-wasm-filter:latest

.github/workflows/test.yaml

Lines changed: 0 additions & 63 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
build
1+
.vscode
2+
build

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM scratch
2+
3+
LABEL org.opencontainers.image.source=https://github.com/jcchavezs/coraza-wasm-filter
4+
5+
COPY build/main.wasm /plugin.wasm

Dockerfile.server-test

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
FROM ubuntu as func-e-downloader
2+
3+
RUN apt update && apt -y install curl
4+
5+
RUN curl https://func-e.io/install.sh | bash -s -- -b /usr/local/bin
6+
7+
FROM tinygo/tinygo as build-stage
8+
9+
RUN apt-get install -y build-essential curl
10+
11+
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
12+
13+
RUN ~/.cargo/bin/cargo install wasm-tools
14+
15+
WORKDIR /usr/src/wasm-filter
16+
17+
COPY coraza coraza
18+
COPY go.mod go.mod
19+
COPY go.sum go.sum
20+
21+
RUN go mod download
22+
23+
COPY main.go main.go
24+
COPY Makefile Makefile
25+
26+
RUN make build
27+
28+
RUN ~/.cargo/bin/wasm-tools validate build/main.wasm
29+
RUN ~/.cargo/bin/wasm-tools dump build/main.wasm > build/main.wasm.dump
30+
31+
FROM func-e-downloader as run-stage
32+
33+
WORKDIR /usr/bin/wasm-filter
34+
35+
COPY --from=build-stage /usr/src/wasm-filter/build ./build
36+
COPY e2e/envoy-config.yaml envoy-config.yaml
37+
38+
ENTRYPOINT ["/usr/local/bin/func-e", "run", "-c envoy-config.yaml", "--log-level info", "--component-log-level wasm:debug"]

Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1+
ARTIFACT_NAME="coraza-wasm-filter"
2+
IMAGE_NAME=$(ARTIFACT_NAME):latest
3+
CONTAINER_NAME=$(ARTIFACT_NAME)-build
4+
15
.PHONY: build
26
build:
37
mkdir -p ./build
4-
tinygo build -o build/main.wasm -scheduler=asyncify -target=wasi ./main.go
8+
tinygo build -o build/main.wasm -scheduler=none -target=wasi ./main.go
59

610
test:
7-
go test -tags=proxytest ./...
11+
go test -tags="proxytest tinygo" ./...
12+
13+
server-test-build:
14+
docker build --progress=plain -t $(IMAGE_NAME) -f Dockerfile.server-test .
15+
16+
server-test-wasm-dump: server-test-build
17+
@docker rm -f $(CONTAINER_NAME) || true
18+
@docker create -ti --name $(CONTAINER_NAME) $(IMAGE_NAME) bash
19+
docker cp $(CONTAINER_NAME):/usr/bin/wasm-filter/build ./
20+
@docker rm -f $(CONTAINER_NAME)
21+
22+
server-test-run: server-test-build
23+
docker run -p 8001:8001 $(IMAGE_NAME)

e2e/envoy-config.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
static_resources:
2+
listeners:
3+
- address:
4+
socket_address:
5+
address: 0.0.0.0
6+
port_value: 8001
7+
filter_chains:
8+
- filters:
9+
- name: envoy.filters.network.http_connection_manager
10+
typed_config:
11+
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
12+
stat_prefix: ingress_http
13+
codec_type: auto
14+
route_config:
15+
virtual_hosts:
16+
- name: local_route
17+
domains:
18+
- "*"
19+
routes:
20+
- match: { prefix: "/" }
21+
direct_response:
22+
status: 200
23+
http_filters:
24+
- name: envoy.filters.http.wasm
25+
typed_config:
26+
"@type": type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
27+
config:
28+
name: "coraza-filter"
29+
root_id: ""
30+
configuration:
31+
"@type": "type.googleapis.com/google.protobuf.StringValue"
32+
value: |
33+
{
34+
"rules":"SecDebugLogLevel 5 \nSecDebugLog modsec.log \nSecRuleEngine On \nSecRule REQUEST_URI \"@streq /admin\" \"id:101,phase:1,t:lowercase,deny\""
35+
}
36+
vm_config:
37+
runtime: "envoy.wasm.runtime.v8"
38+
vm_id: "my_vm_id"
39+
code:
40+
local:
41+
filename: "build/main.wasm"
42+
- name: envoy.filters.http.router
43+
typed_config:
44+
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

0 commit comments

Comments
 (0)