A sample implementation of a Wasm authentication filter for Envoy Proxy written in Go.
This project demonstrates how to use Envoy Proxy's Wasm filter capabilities to implement HTTP request authentication.
[Client] → [Envoy Proxy + Wasm Filter] → [Backend Service]
- Envoy Proxy: Acts as a reverse proxy and executes the Wasm filter
- Wasm Filter: Authentication filter implemented in Go
- Backend Service: Simple HTTP server
- Bearer token authentication
- Skip authentication for health check endpoints
- Add user information headers upon successful authentication
- Add filter identification headers to responses
- Docker
- Docker Compose
- Go 1.24 or later
- Clone the repository
git clone https://github.com/keitaj/envoy-wasm-sample.git
cd envoy-wasm-sample- Build the Wasm filter and start services
make runThis will:
- Build the Go Wasm filter
- Build Docker images
- Start Envoy and Backend services
- Health check (no authentication required)
curl -i http://localhost:10000/health- Access without authentication (401 error)
curl -i http://localhost:10000/api/data- Access with invalid token (401 error)
curl -i -H "Authorization: Bearer invalid-token" http://localhost:10000/api/data- Access with valid user token (success)
curl -i -H "Authorization: Bearer secret-token-123" http://localhost:10000/api/data- Access with valid admin token (success)
curl -i -H "Authorization: Bearer admin-token-456" http://localhost:10000/api/datamake test-
Request Reception
- HTTP request from client arrives at Envoy
- Wasm filter's
OnHttpRequestHeadersis called
-
Path Check
/healthendpoint skips authentication- Other paths undergo authentication
-
Authentication Process
- Check for Authorization header
- Validate Bearer token
- Valid tokens:
secret-token-123: user roleadmin-token-456: admin role
-
On Successful Authentication
- Add user information to
x-auth-userheader - Forward request to backend service
- Add user information to
-
On Authentication Failure
- Return 401 Unauthorized response
- Return error message in JSON format
All responses include the x-wasm-filter: go-auth header, confirming that the Wasm filter is functioning.
The Wasm filter outputs the following logs:
- On plugin start: "plugin started"
- On request processing: path information
- On authentication success/failure: detailed information
These logs can be viewed in Envoy's logs:
docker logs envoy.
├── Makefile # Build, run, and test command definitions
├── docker-compose.yaml # Docker Compose configuration
├── envoy.yaml # Envoy configuration file
├── filter.wasm # Built Wasm file (auto-generated)
├── backend/ # Backend service
│ ├── Dockerfile
│ └── main.go
└── wasm-filter/ # Wasm filter source code
├── go.mod
├── go.sum
└── main.go
- Check Envoy logs
docker logs envoy- Verify Go version (1.24 or later required)
go version- Confirm Wasm file is built correctly
ls -la filter.wasmStop services and remove build files:
make clean- Envoy: v1.34-latest
- Go: 1.24 (wasip1/wasm target)
- proxy-wasm-go-sdk: Wasm plugin development SDK for Envoy
The Wasm filter sets up the VM context in the init() function and creates an HTTP context for each request to handle processing.