Chapter 8. Override your Gateway policies for auth and rate limiting
As an application developer, you can override your existing Gateway-level policies to configure your application-level auth and rate limiting requirements.
Prerequisites
- Your Connectivity Link policies are configured as described in Chapter 7, Configure your Gateway policies and HTTP route.
8.1. Override the Gateway’s deny-all AuthPolicy Copy linkLink copied to clipboard!
You can allow authenticated access to the Toystore API by defining a new AuthPolicy that targets the HTTPRoute resource created in the previous section.
Any new HTTPRoutes will still be affected by the existing Gateway-level policy. Because you want users to now access this API, you must override that Gateway policy. For simplicity, you can use API keys to authenticate the requests, but other options such as OpenID Connect are also available.
Procedure
Ensure that your Connectivity Link system namespace is set correctly as follows:
export KUADRANT_SYSTEM_NS=$(kubectl get kuadrant -A -o jsonpath="{.items[0].metadata.namespace}")export KUADRANT_SYSTEM_NS=$(kubectl get kuadrant -A -o jsonpath="{.items[0].metadata.namespace}")Copy to Clipboard Copied! Toggle word wrap Toggle overflow Define API keys for bob and alice users as follows:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a new
AuthPolicyin a different namespace that overrides thedeny-allpolicy created earlier and accepts the API keys as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
8.2. Override the Gateway’s low-limit RateLimitPolicy for specific users Copy linkLink copied to clipboard!
The configured Gateway limits provide a good set of limits for the general case. However, as the developer of the Toystore API, you might want to only allow a certain number of requests for specific users, and a general limit for all other users.
Procedure
Create a new
RateLimitPolicyin a different namespace to override the defaultlow-limitpolicy created previously and set rate limits for specific users as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIt might take a few minutes for the
RateLimitPolicyto be applied, depending on your cluster.Check that the
RateLimitPolicyhas a status ofAcceptedandEnforcedas follows:kubectl get ratelimitpolicy -n ${KUADRANT_DEVELOPER_NS} toystore-rlp -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}{"\n"}{.status.conditions[?(@.type=="Enforced")].message}'kubectl get ratelimitpolicy -n ${KUADRANT_DEVELOPER_NS} toystore-rlp -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}{"\n"}{.status.conditions[?(@.type=="Enforced")].message}'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check that the status of the HTTPRoute is now affected by the
RateLimitPolicyin the same namespace:kubectl get httproute toystore -n ${KUADRANT_DEVELOPER_NS} -o=jsonpath='{.status.parents[0].conditions[?(@.type=="kuadrant.io/RateLimitPolicyAffected")].message}'kubectl get httproute toystore -n ${KUADRANT_DEVELOPER_NS} -o=jsonpath='{.status.parents[0].conditions[?(@.type=="kuadrant.io/RateLimitPolicyAffected")].message}'Copy to Clipboard Copied! Toggle word wrap Toggle overflow
8.3. Test the new Rate limit and Auth policies Copy linkLink copied to clipboard!
Send requests as user alice as follows:
while :; do curl -k --write-out '%{http_code}\n' --silent --output /dev/null -H 'Authorization: APIKEY IAMALICE' "https://api.$KUADRANT_ZONE_ROOT_DOMAIN/cars" | grep -E --color "\b(429)\b|$"; sleep 1; donewhile :; do curl -k --write-out '%{http_code}\n' --silent --output /dev/null -H 'Authorization: APIKEY IAMALICE' "https://api.$KUADRANT_ZONE_ROOT_DOMAIN/cars" | grep -E --color "\b(429)\b|$"; sleep 1; doneCopy to Clipboard Copied! Toggle word wrap Toggle overflow You should see HTTP status
200every second for 5 seconds, followed by HTTP status429every second for 5 seconds.Send requests as user bob as follows:
while :; do curl -k --write-out '%{http_code}\n' --silent --output /dev/null -H 'Authorization: APIKEY IAMBOB' "https://api.$KUADRANT_ZONE_ROOT_DOMAIN/cars" | grep -E --color "\b(429)\b|$"; sleep 1; donewhile :; do curl -k --write-out '%{http_code}\n' --silent --output /dev/null -H 'Authorization: APIKEY IAMBOB' "https://api.$KUADRANT_ZONE_ROOT_DOMAIN/cars" | grep -E --color "\b(429)\b|$"; sleep 1; doneCopy to Clipboard Copied! Toggle word wrap Toggle overflow You should see HTTP status
200every second for 2 seconds, followed by HTTP status429every second for 8 seconds.