Skip to content

Commit 550787a

Browse files
Merge pull request #705 from Tharsanan1/jwt-auth-it
Add JWT authentication integration tests
2 parents a2b4e19 + 3a81ca9 commit 550787a

10 files changed

Lines changed: 894 additions & 0 deletions

File tree

.github/copilot-instructions.md

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
# Copilot Coding Agent Instructions
2+
3+
This document provides instructions for the GitHub Copilot coding agent when working on this repository.
4+
5+
## Project Structure
6+
7+
- `gateway/` - Gateway components (controller, policy-engine, router)
8+
- `gateway/gateway-controller/` - Gateway controller service
9+
- `gateway/policy-engine/` - Policy engine service
10+
- `gateway/router/` - Envoy-based router
11+
- `gateway/it/` - Integration tests
12+
- `kubernetes/` - Kubernetes operator and Helm charts
13+
- `kubernetes/gateway-operator/` - Gateway Kubernetes operator
14+
15+
## Running Integration Tests
16+
17+
### When modifying code in `./gateway/**` (excluding `./gateway/it/`)
18+
19+
If you change any code in the gateway components (gateway-controller, policy-engine, router, etc.), you MUST:
20+
21+
1. **Rebuild the Docker images** to include your changes:
22+
```bash
23+
cd gateway && make build-local
24+
```
25+
26+
2. **Modify the test configuration** to use the non-coverage images. Edit `gateway/it/docker-compose.test.yaml` line 25 and remove the `-coverage` suffix from the gateway-controller image name:
27+
- Change: `ghcr.io/wso2/api-platform/gateway-controller-coverage:<version>`
28+
- To: `ghcr.io/wso2/api-platform/gateway-controller:<version>`
29+
30+
**Important:** Keep the existing version tag (e.g., `0.3.0-SNAPSHOT`) as-is. The version in the file is the current version - do not hardcode a different version.
31+
32+
3. **Run the integration tests**:
33+
```bash
34+
cd gateway/it && make test
35+
```
36+
37+
4. **Revert the gateway-controller image name before committing**. The `-coverage` suffix removal was only for local testing. Before creating a PR, restore the `-coverage` suffix of `gateway/it/docker-compose.test.yaml`:
38+
- Change: `ghcr.io/wso2/api-platform/gateway-controller:<version>`
39+
- Back to: `ghcr.io/wso2/api-platform/gateway-controller-coverage:<version>`
40+
41+
**Important:** Do NOT include this image name change in your PR. The CI pipeline requires the coverage image. Other legitimate changes to docker-compose.test.yaml should still be committed.
42+
43+
### When modifying only `./gateway/it/**` (integration test files only)
44+
45+
If you are only modifying integration test files (test cases, test utilities, etc.) and not the gateway component source code:
46+
47+
1. **Modify the test configuration** to use the non-coverage images. Edit `gateway/it/docker-compose.test.yaml` line 25 and remove the `-coverage` suffix from the gateway-controller image name:
48+
- Change: `ghcr.io/wso2/api-platform/gateway-controller-coverage:<version>`
49+
- To: `ghcr.io/wso2/api-platform/gateway-controller:<version>`
50+
51+
**Important:** Keep the existing version tag as-is. Do not change the version number.
52+
53+
2. **Run the integration tests**:
54+
```bash
55+
cd gateway/it && make test
56+
```
57+
58+
3. **Revert the gateway-controller image name before committing**. Restore the `-coverage` suffix of `gateway/it/docker-compose.test.yaml`:
59+
- Change: `ghcr.io/wso2/api-platform/gateway-controller:<version>`
60+
- Back to: `ghcr.io/wso2/api-platform/gateway-controller-coverage:<version>`
61+
62+
**Important:** Do NOT include this image name change in your PR. Other legitimate changes to docker-compose.test.yaml should still be committed.
63+
64+
Note: The images are pre-built during the setup phase, so you can run tests directly without rebuilding.
65+
66+
## Important Notes
67+
68+
- The `copilot-setup-steps.yml` workflow pre-builds gateway images using `make build-local`
69+
- The docker-compose file by default references coverage images (for CI coverage reporting)
70+
- You must switch to non-coverage images when running tests in the Copilot environment
71+
- **Version tags:** Always use whatever version tag is currently in the docker-compose.test.yaml file. The version may change over time (e.g., `0.3.0-SNAPSHOT`, `0.4.0-SNAPSHOT`, etc.) - never hardcode a specific version
72+
73+
## Build Commands Reference
74+
75+
| Command | Description | Working Directory |
76+
|---------|-------------|-------------------|
77+
| `make build-local` | Build all gateway Docker images locally | `gateway/` |
78+
| `make test` | Run integration tests | `gateway/it/` |
79+
80+
## Policy Configuration in Integration Tests
81+
82+
When writing integration tests for policies, you MUST understand how policy parameters work:
83+
84+
### Policy Parameter Types
85+
86+
Policies have two types of parameters defined in their `policy-definition.yaml` files (located in `gateway/policies/<policy-name>/v<version>/policy-definition.yaml`):
87+
88+
1. **`parameters`** (User Parameters): User-defined parameters that are specified in the API configuration when deploying an API. These are set by API developers via the REST API or API YAML.
89+
90+
2. **`systemParameters`** (System Parameters): Gateway admin-defined parameters that are configured in the gateway configuration file. These are NOT set in the API definition.
91+
92+
### How System Parameters Work
93+
94+
System parameters use the `"wso2/defaultValue"` attribute to reference values from the gateway config file using a JSON path syntax:
95+
96+
```yaml
97+
# Example from a policy-definition.yaml
98+
systemParameters:
99+
type: object
100+
properties:
101+
keyManagers:
102+
type: array
103+
description: List of key manager definitions
104+
"wso2/defaultValue": "${config.policy_configurations.jwtauth_v010.keymanagers}"
105+
jwksCacheTtl:
106+
type: string
107+
"wso2/defaultValue": "${config.policy_configurations.jwtauth_v010.jwkscachettl}"
108+
```
109+
110+
The `"wso2/defaultValue"` path `${config.policy_configurations.jwtauth_v010.keymanagers}` tells the gateway to fetch the value from:
111+
112+
```yaml
113+
# In gateway/it/test-config.yaml
114+
policy_configurations:
115+
jwtauth_v010:
116+
keyManagers:
117+
- name: test-jwks
118+
issuer: http://mock-jwks.default.svc.cluster.local:8080/token
119+
jwks:
120+
remote:
121+
uri: http://mock-jwks:8080/jwks
122+
```
123+
124+
### Setting Up Policy Tests
125+
126+
When creating integration tests for a policy:
127+
128+
1. **Check the policy definition** at `gateway/policies/<policy-name>/v<version>/policy-definition.yaml`
129+
130+
2. **Identify systemParameters**: Look for properties with `"wso2/defaultValue"` - these need gateway config entries
131+
132+
3. **Add system parameter values to test-config.yaml**: Add the required configuration under `policy_configurations` in `gateway/it/test-config.yaml` following the JSON path from `"wso2/defaultValue"`:
133+
- Path: `${config.policy_configurations.<policy_id>.<param_name>}`
134+
- Location: `gateway/it/test-config.yaml` → `policy_configurations` → `<policy_id>` → `<param_name>`
135+
- Note: The `<policy_id>` in the path typically follows the pattern `<policyname>_v<version>` with underscores (e.g., `jwtauth_v010` for `jwt-auth` v0.1.0)
136+
137+
4. **Use regular parameters in API definitions**: In your feature file's API YAML, only specify user `params` (not system parameters):
138+
139+
```yaml
140+
# Example API definition in feature file
141+
operations:
142+
- method: GET
143+
path: /protected
144+
policies:
145+
- name: jwt-auth
146+
version: v0.1.0
147+
params: # These are user parameters
148+
issuers:
149+
- test-jwks
150+
audiences:
151+
- my-api
152+
requiredScopes:
153+
- read
154+
```
155+
156+
### Example: Adding JWT Auth Policy Test
157+
158+
1. **Policy definition** (`gateway/policies/jwt-auth/v0.1.0/policy-definition.yaml`) shows:
159+
- `parameters`: `issuers`, `audiences`, `requiredScopes`, etc. (user-defined)
160+
- `systemParameters`: `keyManagers`, `jwksCacheTtl`, etc. with `"wso2/defaultValue"` references
161+
162+
2. **Add to test-config.yaml**:
163+
```yaml
164+
policy_configurations:
165+
jwtauth_v010:
166+
keyManagers:
167+
- name: test-jwks
168+
issuer: http://mock-jwks:8080/token
169+
jwks:
170+
remote:
171+
uri: http://mock-jwks:8080/jwks
172+
jwkscachettl: "5m"
173+
```
174+
175+
3. **In your feature file API definition**, only include user params:
176+
```yaml
177+
policies:
178+
- name: jwt-auth
179+
version: v0.1.0
180+
params:
181+
issuers:
182+
- test-jwks
183+
```
184+
185+
### Key Points
186+
187+
- **NEVER** put systemParameters in the API definition - they come from the gateway config
188+
- **ALWAYS** check the policy-definition.yaml for `"wso2/defaultValue"` to know what config entries are needed
189+
- The config path uses the format: `policy_configurations.<policy_id>.<lowercase_param_name>`
190+
- If a test fails with missing system parameters, check that `gateway/it/test-config.yaml` has the required `policy_configurations` entries
191+
192+
## Unit Tests
193+
194+
To run unit tests for individual components:
195+
196+
```bash
197+
# Gateway Controller
198+
cd gateway/gateway-controller && go test ./...
199+
200+
# Policy Engine
201+
cd gateway/policy-engine && go test ./...
202+
203+
# Router
204+
cd gateway/router && go test ./...
205+
206+
# Gateway Operator
207+
cd kubernetes/gateway-operator && go test ./...
208+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: "Copilot Setup Steps"
2+
3+
# Automatically run the setup steps when they are changed to allow for easy validation, and
4+
# allow manual testing through the repository's "Actions" tab
5+
on:
6+
workflow_dispatch:
7+
push:
8+
paths:
9+
- .github/workflows/copilot-setup-steps.yml
10+
pull_request:
11+
paths:
12+
- .github/workflows/copilot-setup-steps.yml
13+
14+
jobs:
15+
# The job MUST be called `copilot-setup-steps` or it will not be picked up by Copilot.
16+
copilot-setup-steps:
17+
runs-on: ubuntu-24.04
18+
19+
# Set the permissions to the lowest permissions possible needed for your steps.
20+
# Copilot will be given its own token for its operations.
21+
permissions:
22+
contents: read
23+
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@v4
27+
28+
- name: Set up Go
29+
uses: actions/setup-go@v5
30+
with:
31+
go-version: '1.25'
32+
cache: true
33+
cache-dependency-path: |
34+
gateway/**/go.sum
35+
kubernetes/**/go.sum
36+
37+
- name: Set up Docker Buildx
38+
uses: docker/setup-buildx-action@v3
39+
40+
- name: Install Helm
41+
uses: azure/setup-helm@v4
42+
with:
43+
version: 'latest'
44+
45+
- name: Install kubectl
46+
uses: azure/setup-kubectl@v4
47+
with:
48+
version: 'latest'
49+
50+
- name: Download Go dependencies for gateway-controller
51+
run: go mod download
52+
working-directory: gateway/gateway-controller
53+
54+
- name: Download Go dependencies for policy-engine
55+
run: go mod download
56+
working-directory: gateway/policy-engine
57+
58+
- name: Download Go dependencies for router
59+
run: go mod download
60+
working-directory: gateway/router
61+
62+
- name: Download Go dependencies for gateway-operator
63+
run: go mod download
64+
working-directory: kubernetes/gateway-operator
65+
66+
- name: Build Gateway images
67+
run: make build-local
68+
working-directory: gateway

.github/workflows/gateway-integration-test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ jobs:
2929
cd gateway
3030
make build-coverage
3131
32+
- name: Build mock-jwks image
33+
run: |
34+
cd tests/mock-servers/mock-jwks
35+
docker build -t ghcr.io/wso2/api-platform/mock-jwks:latest .
36+
3237
- name: Run integration tests
3338
run: |
3439
cd gateway

gateway/it/docker-compose.test.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ services:
122122
networks:
123123
- it-gateway-network
124124

125+
# Mock JWKS server for JWT authentication testing
126+
mock-jwks:
127+
container_name: it-mock-jwks
128+
image: ghcr.io/wso2/api-platform/mock-jwks:latest
129+
build:
130+
context: ../../tests/mock-servers/mock-jwks
131+
dockerfile: Dockerfile
132+
ports:
133+
- "8082:8080"
134+
networks:
135+
- it-gateway-network
136+
125137
volumes:
126138
controller-data-tests:
127139
driver: local

0 commit comments

Comments
 (0)