|
| 1 | +# JSON Payload validation |
| 2 | + |
| 3 | +This wasm plugin checks whether the request has JSON payload and has required keys in it. |
| 4 | +If not, the wasm plugin ceases the further process of the request and returns 403 immediately. |
| 5 | + |
| 6 | +## Run it via Envoy |
| 7 | + |
| 8 | +`envoy.yaml` is the example envoy config file that you can use for running the wasm plugin |
| 9 | +with standalone Envoy. |
| 10 | + |
| 11 | +Envoy listens on `localhost:18000`, responding to any requests with static content "hello from server". |
| 12 | +However, the wasm plugin also runs to validate the requests' payload. |
| 13 | + |
| 14 | +```bash |
| 15 | +make -C ../.. run name=json_validation |
| 16 | +``` |
| 17 | + |
| 18 | +The plugin intercepts the request and makes Envoy return 403 instead of the static content |
| 19 | +if the request has no JSON payload or the payload JSON doesn't have "id" or "token" keys. |
| 20 | + |
| 21 | +```console |
| 22 | +# Returns the normal response when the request has the required keys, id and token. |
| 23 | +curl -X POST localhost:18000 -H 'Content-Type: application/json' --data '{"id": "xxx", "token": "xxx"}' |
| 24 | +hello from the server |
| 25 | + |
| 26 | +# Returns 403 when the request has missing required keys. |
| 27 | +curl -v -X POST localhost:18000 -H 'Content-Type: application/json' --data '"required_keys_missing"' |
| 28 | +Note: Unnecessary use of -X or --request, POST is already inferred. |
| 29 | +* Trying 127.0.0.1:18000... |
| 30 | +* TCP_NODELAY set |
| 31 | +* Connected to localhost (127.0.0.1) port 18000 (#0) |
| 32 | +> POST / HTTP/1.1 |
| 33 | +> Host: localhost:18000 |
| 34 | +> User-Agent: curl/7.68.0 |
| 35 | +> Accept: */* |
| 36 | +> Content-Type: application/json |
| 37 | +> Content-Length: 23 |
| 38 | +> |
| 39 | +* upload completely sent off: 23 out of 23 bytes |
| 40 | +* Mark bundle as not supporting multiuse |
| 41 | +< HTTP/1.1 403 Forbidden |
| 42 | +< content-length: 15 |
| 43 | +< content-type: text/plain |
| 44 | +< date: Tue, 01 Mar 2022 19:22:24 GMT |
| 45 | +< server: envoy |
| 46 | +< |
| 47 | +* Connection #0 to host localhost left intact |
| 48 | +invalid payload |
| 49 | +``` |
| 50 | + |
| 51 | +### Run it via Istio |
| 52 | + |
| 53 | +This example details deploying to a kind cluster running the Istio httpbin sample app. |
| 54 | + |
| 55 | +```console |
| 56 | +# Create a test cluster |
| 57 | +kind create cluster |
| 58 | + |
| 59 | +# Install Istio and the httpbin sample app |
| 60 | + |
| 61 | +istioctl install --set profile=demo -y |
| 62 | +kubectl label namespace default istio-injection=enabled |
| 63 | +kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.12/samples/httpbin/httpbin.yaml |
| 64 | +kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.12/samples/httpbin/httpbin-gateway.yaml |
| 65 | +``` |
| 66 | + |
| 67 | +For Istio 1.12 and later the easiest way is to use a WasmPlugin resource. For older Istio |
| 68 | +versions an EnvoyFilter is needed. |
| 69 | + |
| 70 | +#### Install using WasmPlugin resource |
| 71 | + |
| 72 | +Build and push the wasm module to your container registry, then apply the WasmPlugin. |
| 73 | + |
| 74 | +```console |
| 75 | +export HUB=your_registry # e.g. docker.io/tetrate |
| 76 | +make -C ../.. build.example name=json_validation |
| 77 | +docker build . -t ${HUB}/json-validation:v1 |
| 78 | +docker push ${HUB}/json-validation:v1 |
| 79 | + |
| 80 | +sed "s|YOUR_CONTAINER_REGISTRY|$HUB|" wasmplugin.yaml | kubectl apply -f - |
| 81 | +``` |
| 82 | + |
| 83 | +#### Install using EnvoyFilter |
| 84 | + |
| 85 | +To use an EnvoyFilter, create a config map containing the compiled wasm plugin, mount the config |
| 86 | +map into the gateway pod, and then configure Envoy via an EnvoyFilter to load the wasm plugin from |
| 87 | +a local file. |
| 88 | + |
| 89 | +```console |
| 90 | +# Create the config map |
| 91 | +kubectl -n istio-system create configmap wasm-plugins --from-file=main.wasm |
| 92 | + |
| 93 | +# Patch the gateway deployment to mount the config map |
| 94 | +kubectl -n istio-system patch deployment istio-ingressgateway --patch-file=gatewaydeploymentpatch.yaml |
| 95 | + |
| 96 | +# Create the EnvoyFilter |
| 97 | +kubectl apply -f envoyfilter.yaml |
| 98 | +``` |
| 99 | + |
| 100 | +#### Test the plugin |
| 101 | + |
| 102 | +Expose the ingress gateway on port 8080 on your local machine via. |
| 103 | + |
| 104 | +```console |
| 105 | +kubectl port-forward -n istio-system svc/istio-ingressgateway 8080:80 |
| 106 | +``` |
| 107 | + |
| 108 | +Requests without the required payload will fail: |
| 109 | + |
| 110 | +```console |
| 111 | +% curl -i http://localhost:8080/post -H 'Content-Type: application/json' --data '{"id": "xxx", "not_token": "xxx"}' |
| 112 | +HTTP/1.1 403 Forbidden |
| 113 | +content-length: 15 |
| 114 | +content-type: text/plain |
| 115 | +date: Tue, 15 Mar 2022 23:05:52 GMT |
| 116 | +server: istio-envoy |
| 117 | + |
| 118 | +invalid payload |
| 119 | +``` |
| 120 | + |
| 121 | +But those with the payload will proceed: |
| 122 | + |
| 123 | +```console |
| 124 | +% curl -i http://localhost:8080/post -H 'Content-Type: application/json' --data '{"id": "xxx", "token": "xxx"}' |
| 125 | +HTTP/1.1 200 OK |
| 126 | +server: istio-envoy |
| 127 | +date: Tue, 15 Mar 2022 23:06:29 GMT |
| 128 | +content-type: application/json |
| 129 | +content-length: 884 |
| 130 | +access-control-allow-origin: * |
| 131 | +access-control-allow-credentials: true |
| 132 | +x-envoy-upstream-service-time: 3 |
| 133 | + |
| 134 | +{ |
| 135 | + "args": {}, |
| 136 | + "data": "{\"id\": \"xxx\", \"token\": \"xxx\"}", |
| 137 | + "files": {}, |
| 138 | + "form": {}, |
| 139 | + "headers": { |
| 140 | + "Accept": "*/*", |
| 141 | + "Content-Length": "29", |
| 142 | + "Content-Type": "application/json", |
| 143 | + "Host": "localhost:8080", |
| 144 | + "User-Agent": "curl/7.64.1", |
| 145 | + "X-B3-Parentspanid": "99a94908edd26592", |
| 146 | + "X-B3-Sampled": "1", |
| 147 | + "X-B3-Spanid": "e12fc7fd9aa74838", |
| 148 | + "X-B3-Traceid": "2b7375cda8bc98a299a94908edd26592", |
| 149 | + "X-Envoy-Attempt-Count": "1", |
| 150 | + "X-Envoy-Internal": "true", |
| 151 | + "X-Forwarded-Client-Cert": "By=spiffe://cluster.local/ns/default/sa/httpbin;Hash=5703a66dcdbc8cafc8c29e1ebee1174f4bc81234d8dc1ccc20fb9e3c26b320e1;Subject=\"\";URI=spiffe://cluster.local/ns/istio-system/sa/istio-ingressgateway-service-account" |
| 152 | + }, |
| 153 | + "json": { |
| 154 | + "id": "xxx", |
| 155 | + "token": "xxx" |
| 156 | + }, |
| 157 | + "origin": "10.244.0.9", |
| 158 | + "url": "http://localhost:8080/post" |
| 159 | +} |
| 160 | +``` |
0 commit comments