From b2619eb999f40c0ea42a09c1fc9843016ae8aef7 Mon Sep 17 00:00:00 2001 From: David Garvey Date: Fri, 4 Oct 2024 16:51:19 +0200 Subject: [PATCH] Add JSON message validation stream processor example --- ...-oas-02715dce83cb47ce59487abe573222f7.json | 40 ++++++++++++++++++- .../validated-pubsub/validated-pubsub-1.sh | 6 +++ .../validated-pubsub/validated-pubsub-2.sh | 6 +++ .../validated-pubsub/validated-pubsub-3.sh | 6 +++ .../validated-pubsub/validated-pubsub-4.sh | 6 +++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100755 deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-1.sh create mode 100755 deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-2.sh create mode 100755 deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-3.sh create mode 100755 deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-4.sh diff --git a/deployments/tyk/data/tyk-dashboard/1/apis/api-oas-02715dce83cb47ce59487abe573222f7.json b/deployments/tyk/data/tyk-dashboard/1/apis/api-oas-02715dce83cb47ce59487abe573222f7.json index 9c65ed31..5c7a8043 100644 --- a/deployments/tyk/data/tyk-dashboard/1/apis/api-oas-02715dce83cb47ce59487abe573222f7.json +++ b/deployments/tyk/data/tyk-dashboard/1/apis/api-oas-02715dce83cb47ce59487abe573222f7.json @@ -13,7 +13,7 @@ ], "x-tyk-api-gateway": { "info": { - "dbId": "66fe500b1da0b00001da209c", + "dbId": "66ffabd12a4a5100016d007e", "id": "02715dce83cb47ce59487abe573222f7", "name": "Streams - Simple PubSub", "orgId": "5e9d9544a1dcd60001d0ed20", @@ -59,6 +59,44 @@ "ws_path": "/ws" } } + }, + "Validated Pub": { + "input": { + "http_server": { + "allowed_verbs": [ + "POST" + ], + "path": "/validated-pub", + "timeout": "5s" + } + }, + "output": { + "http_server": { + "stream_path": "/validated-sse" + } + }, + "pipeline": { + "processors": [ + { + "json_schema": { + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\n \"type\": \"string\",\n \"minLength\": 5\n },\n \"age\": {\n \"type\": \"integer\",\n \"minimum\": 19\n }\n },\n \"required\": [\"name\", \"age\"],\n \"additionalProperties\": false\n\n}\n" + } + }, + { + "catch": [ + { + "log": { + "level": "ERROR", + "message": "JSON schema validated failed due to: ${!error()}" + } + }, + { + "mapping": "root = deleted()" + } + ] + } + ] + } } } } diff --git a/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-1.sh b/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-1.sh new file mode 100755 index 00000000..12916d81 --- /dev/null +++ b/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-1.sh @@ -0,0 +1,6 @@ +echo "Streams - Validated PubSub" +echo "Part 1: Listening for validated server-sent events" +echo "This script connects to a server-sent event endpoint, and waits to receive messages." +echo "Use script 3 to send messages." + +curl http://tyk-gateway.localhost:8080/streams-simple-pubsub/validated-sse diff --git a/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-2.sh b/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-2.sh new file mode 100755 index 00000000..30db2b9d --- /dev/null +++ b/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-2.sh @@ -0,0 +1,6 @@ +echo "Streams - Validated PubSub" +echo "Part 2: Publishing a valid message" +echo "This script publishes a valid message to the clients connected in script 1." +echo "Check the first terminal to see the messages received." + +curl http://tyk-gateway.localhost:8080/streams-simple-pubsub/validated-pub -d '{ "name": "Tyk Streams", "age": 21 }' diff --git a/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-3.sh b/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-3.sh new file mode 100755 index 00000000..505a3c72 --- /dev/null +++ b/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-3.sh @@ -0,0 +1,6 @@ +echo "Streams - Validated PubSub" +echo "Part 3: Publishing an invalid message" +echo "This script publishes an invalid message, which the gateway prevents subscribers from receiving." +echo "Check the first terminal to see that the message is not received, and the fourth terminal to see the logged error." + +curl http://tyk-gateway.localhost:8080/streams-simple-pubsub/validated-pub -d '{ "name": "Tyk", "age": 3 }' diff --git a/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-4.sh b/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-4.sh new file mode 100755 index 00000000..87163d0b --- /dev/null +++ b/deployments/tyk/scripts/examples/streams/validated-pubsub/validated-pubsub-4.sh @@ -0,0 +1,6 @@ +echo "Streams - Validated PubSub" +echo "Part 4: Displaying errors from the application log" +echo "This script watches the Tyk Gateway application log for 'Schema validation failed' messages.". +echo "It will display the error message generated by the invalid request sent by script 3." + +docker logs -f tyk-demo-tyk-gateway-1 2>&1 | grep -E "@service=benthos.*path=root.pipeline.processors.1.catch.0"