@@ -82,17 +82,27 @@ kubectl apply -f examples/toystore/httproute.yaml
8282
8383#### Try the API unprotected
8484
85+ Export the gateway hostname and port:
86+
87+ ``` sh
88+ export INGRESS_HOST=$( kubectl get gtw istio-ingressgateway -n istio-system -o jsonpath=' {.status.addresses[0].value}' )
89+ export INGRESS_PORT=$( kubectl get gtw istio-ingressgateway -n istio-system -o jsonpath=' {.spec.listeners[?(@.name=="http")].port}' )
90+ export GATEWAY_URL=$INGRESS_HOST :$INGRESS_PORT
91+ ```
92+
8593``` sh
86- curl -H ' Host: api.toystore.com' http://localhost:9080 /toy -i
94+ curl -H ' Host: api.toystore.com' http://$GATEWAY_URL /toy -i
8795# HTTP/1.1 200 OK
8896```
8997
9098It should return ` 200 OK ` .
9199
92- > ** Note** : If the command above fails to hit the Toy Store API on your environment, try forwarding requests to the service:
100+ > ** Note** : If the command above fails to hit the Toy Store API on your environment, try forwarding requests to the service and accessing over localhost :
93101>
94102> ``` sh
95103> kubectl port-forward -n istio-system service/istio-ingressgateway 9080:80 2>&1 > /dev/null &
104+ > curl -H ' Host: api.toystore.com' http://localhost:9080/toy -i
105+ > # HTTP/1.1 200 OK
96106> ` ` `
97107
98108# ## ③ Deploy Keycloak
157167#### Try the API missing authentication
158168
159169``` sh
160- curl -H ' Host: api.toystore.com' http://localhost:9080 /toy -i
170+ curl -H ' Host: api.toystore.com' http://$GATEWAY_URL /toy -i
161171# HTTP/1.1 401 Unauthorized
162172# www-authenticate: Bearer realm="keycloak-users"
163173# www-authenticate: Bearer realm="k8s-service-accounts"
@@ -175,7 +185,7 @@ ACCESS_TOKEN=$(kubectl run token --attach --rm --restart=Never -q --image=curlim
175185Send a request to the API as the Keycloak-authenticated user while still missing permissions:
176186
177187``` sh
178- curl -H " Authorization: Bearer $ACCESS_TOKEN " -H ' Host: api.toystore.com' http://localhost:9080 /toy -i
188+ curl -H " Authorization: Bearer $ACCESS_TOKEN " -H ' Host: api.toystore.com' http://$GATEWAY_URL /toy -i
179189# HTTP/1.1 403 Forbidden
180190```
181191
@@ -199,7 +209,7 @@ SA_TOKEN=$(kubectl create token client-app-1)
199209Send a request to the API as the service account while still missing permissions:
200210
201211``` sh
202- curl -H " Authorization: Bearer $SA_TOKEN " -H ' Host: api.toystore.com' http://localhost:9080 /toy -i
212+ curl -H " Authorization: Bearer $SA_TOKEN " -H ' Host: api.toystore.com' http://$GATEWAY_URL /toy -i
203213# HTTP/1.1 403 Forbidden
204214```
205215
@@ -281,24 +291,24 @@ EOF
281291Send requests to the API as the Keycloak-authenticated user:
282292
283293``` sh
284- curl -H " Authorization: Bearer $ACCESS_TOKEN " -H ' Host: api.toystore.com' http://localhost:9080 /toy -i
294+ curl -H " Authorization: Bearer $ACCESS_TOKEN " -H ' Host: api.toystore.com' http://$GATEWAY_URL /toy -i
285295# HTTP/1.1 200 OK
286296```
287297
288298``` sh
289- curl -H " Authorization: Bearer $ACCESS_TOKEN " -H ' Host: api.toystore.com' -X POST http://localhost:9080 /admin/toy -i
299+ curl -H " Authorization: Bearer $ACCESS_TOKEN " -H ' Host: api.toystore.com' -X POST http://$GATEWAY_URL /admin/toy -i
290300# HTTP/1.1 200 OK
291301```
292302
293303Send requests to the API as the Kubernetes service account:
294304
295305``` sh
296- curl -H " Authorization: Bearer $SA_TOKEN " -H ' Host: api.toystore.com' http://localhost:9080 /toy -i
306+ curl -H " Authorization: Bearer $SA_TOKEN " -H ' Host: api.toystore.com' http://$GATEWAY_URL /toy -i
297307# HTTP/1.1 200 OK
298308```
299309
300310``` sh
301- curl -H " Authorization: Bearer $SA_TOKEN " -H ' Host: api.toystore.com' -X POST http://localhost:9080 /admin/toy -i
311+ curl -H " Authorization: Bearer $SA_TOKEN " -H ' Host: api.toystore.com' -X POST http://$GATEWAY_URL /admin/toy -i
302312# HTTP/1.1 403 Forbidden
303313```
304314
@@ -339,13 +349,13 @@ Each user should be entitled to a maximum of 5 requests every 10 seconds.
339349Send requests as the Keycloak-authenticated user:
340350
341351``` sh
342- while : ; do curl --write-out ' %{http_code}\n' --silent --output /dev/null -H " Authorization: Bearer $ACCESS_TOKEN " -H ' Host: api.toystore.com' http://localhost:9080 /toy | egrep --color " \b(429)\b|$" ; sleep 1; done
352+ while : ; do curl --write-out ' %{http_code}\n' --silent --output /dev/null -H " Authorization: Bearer $ACCESS_TOKEN " -H ' Host: api.toystore.com' http://$GATEWAY_URL /toy | grep -E --color " \b(429)\b|$" ; sleep 1; done
343353```
344354
345355Send requests as the Kubernetes service account:
346356
347357``` sh
348- while : ; do curl --write-out ' %{http_code}\n' --silent --output /dev/null -H " Authorization: Bearer $SA_TOKEN " -H ' Host: api.toystore.com' http://localhost:9080 /toy | egrep --color " \b(429)\b|$" ; sleep 1; done
358+ while : ; do curl --write-out ' %{http_code}\n' --silent --output /dev/null -H " Authorization: Bearer $SA_TOKEN " -H ' Host: api.toystore.com' http://$GATEWAY_URL /toy | grep -E --color " \b(429)\b|$" ; sleep 1; done
349359```
350360
351361## Cleanup
0 commit comments