This step by step guide runs on top of the Kong Docker Stack.
We will see how the Kong Admin API was used under the hood in this Approov Demo.
To setup Kong to check the Approov Tokens, we will need to create a Kong consumer and two Kong services, one for the Approov Token, and another one for the Approov Token Binding, and enable their respective plugins.
Before you start please ensure the Kong Docker Stack is up and running:
./kong up
Please follow these instructions to set it up.
./kong-admin help
A bash script to wrap repetitive tasks in the Kong Admin API.
---> MUST BE USED FROM THE ROOT OF THE PROJECT <---
SIGNATURE:
./kong [options] <command> [arguments]
COMMANDS:
approov:demo-setup Configures the Approov demo in one go.
$ ./kong-admin consumer:new shapes-mobile-app
consumer:new Creates a new consumer.
$ ./kong-admin consumer:new shapes-mobile-app
consumer:add-base64url-secret Adds the base64url safe encoded secret.
$ ./kong-admin consumer:add-base64url-secret <consumer-name> <key-id-for-the-secret>
$ ./kong-admin consumer:add-base64url-secret shapes-mobile-app approov
service:new Creates a new Kong service.
$ ./kong-admin <service-name> <url-to-forward-requests>
$ ./kong-admin service:new approov-token https://example.com
service:add-route-by-path Adds a route to the service for the given path.
$ ./kong-admin <service-name> <domain-service-is-listening-on> <route-path>
$ ./kong-admin service:add-route-by-path approov-token domain.com /v1/shapes
service:add-all-routes Adds all routes to the service.
$ ./kong-admin <service-name> <domain-service-is-listening-on>
$ ./kong-admin service:add-route-by-path approov-token domain.com
service:enable-jwt-plugin Enables the Kong JWT plugin for the given service.
$ ./kong-admin service:enable-jwt-plugin <service-name>
$ ./kong-admin service:enable-jwt-plugin approov-token
service:enable-approov-token-binding-plugin Enables and configures the plugin to check the Approov Token Binding.
$ ./kong-admin service:enable-approov-token-binding-plugin <service-name>
$ ./kong-admin service:enable-approov-token-binding-plugin approov-token-binding
In order to add the security layer for the Approov Token Service we need to create a consumer for it which will hold the secret to be used later by the Kong JWT plugin to verify the signature for the Approov-Token.
./kong-admin consumer:new <consumer-name>
./kong-admin consumer:new shapes-mobile-app
We can confirm that the consumer application was added by visiting http://localhost:8001/consumers.
./kong-admin consumer:add-base64url-secret <consumer-name> <key-id-for-the-secret>
./kong-admin consumer:add-base64url-secret \
shapes-mobile-app \
approov
To confirm the Approov Secret was added as the JWT credential we can visit http://localhost:8001/consumers/shapes-mobile-app/jwt.
In order to check the Approov token we will need to create a Kong service, add routes to it, enable the Kong JWT plugin to validate the Approov-Token, and finally we will need to add the shapes-mobile-app consumer to this service.
./kong-admin <service-name> <url-to-forward-requests>
./kong-admin service:new \
approov-token \
https://shapes.demo.approov.io
We can now visit http://localhost:8001/services/ to confirm the response for the above command.
./kong-admin <service-name> <domain-service-is-listening-on> <route-path>
./kong-admin service:add-route-by-path \
approov-token \
localhost \
/v1/shapes
We can confirm the response for the request by visiting http://localhost:8001/services/approov-token/routes.
NOTE: To match all routes
/*:./kong-admin service:add-all-routes \ approov-token \ localhost
To test the service we will send a request to the Kong API Gateway which will be proxied to the backend based on the Host header provided in the request.
curl -i -X GET --url http://localhost:8000/v1/shapes
{"shape": "Triangle"}Now that we know Kong is forwarding correctly our requests, it's time to add the security layer.
The Approov Token service was created to match all routes for /v1/shapes/*, therefore any matching request will be checked for the existence of an Approov-Token which is correctly signed and has not expired. On a successful check the request will be forwarded and if it fails the request is immediately terminated.
./kong-admin service:enable-jwt-plugin <service-name>
./kong-admin service:enable-jwt-plugin approov-token
We can visit http://localhost:8001/services/approov-token/plugins to confirm the above response.
This will be very similar to what we have done for the Approov Token Service, therefore I will give the sequence of commands without explaining them, except when we do something extra.
./kong-admin service:new \
approov-token-binding \
https://shapes.demo.approov.io
./kong-admin service:add-route-by-path \
approov-token-binding \
localhost \
/v1/forms
./kong-admin service:enable-jwt-plugin approov-token-binding
This plugin will ensure that we have a valid token binding.
This Approov Demo is configured to bind the Authorization token with the Approov-Token.
So in order to have a valid token binding, the SHA256 hash of the base64 string for the Authorization token must match with the value in the pay claim for the Approov-Token.
In other words we are binding who is in the request with what made the request.
./kong-admin service:enable-approov-token-binding-plugin <service-name>
./kong-admin service:enable-approov-token-binding-plugin approov-token-binding
We can visit http://localhost:8001/services/approov-token-binding/plugins to confirm the above response.