Activity: Implement Least Privilege with Authorization Policies
Module 2
As it stands, the microservices in the travel-agency namespace can currently communicate freely. While this makes the development cycle easier, it poses security risks especially for services like discounts which applies proprietary pricing logic and handles sensitive data.

In this section, you’ll apply an Istio AuthorizationPolicy to enforce a least privilege model, allowing access to discounts only from the allowed backend services (hotels, cars, flights and insurance services), and not from the travels REST API.
Review current access of discounts service
-
Run the following command in the upper terminal to verify that you are indeed able to access the
discountsservice directly from all the services in thetravel-agencyname space.
This command calls thediscountsservice directly from the pods of the services.oc rsh -n travel-agency $(oc get pod -l app=travels -n travel-agency -o jsonpath='{.items[0].metadata.name}') \ curl http://discounts.travel-agency.svc.cluster.local:8000/discounts/hello-from-travels oc rsh -n travel-agency $(oc get pod -l app=flights -n travel-agency -o jsonpath='{.items[0].metadata.name}') \ curl http://discounts.travel-agency.svc.cluster.local:8000/discounts/hello-from-flights oc rsh -n travel-agency $(oc get pod -l app=insurances -n travel-agency -o jsonpath='{.items[0].metadata.name}') \ curl http://discounts.travel-agency.svc.cluster.local:8000/discounts/hello-from-insurances oc rsh -n travel-agency $(oc get pod -l app=cars -n travel-agency -o jsonpath='{.items[0].metadata.name}') \ curl http://discounts.travel-agency.svc.cluster.local:8000/discounts/hello-from-cars oc rsh -n travel-agency $(oc get pod -l app=hotels -n travel-agency -o jsonpath='{.items[0].metadata.name}') \ curl http://discounts.travel-agency.svc.cluster.local:8000/discounts/hello-from-hotels -
The output verifies access of
discountsservice directly fromtravelsREST API, and other services such ascarsandhotels.{"user":"hello-from-travels","discount":0.05} {"user":"hello-from-flights","discount":0.05} {"user":"hello-from-insurances","discount":0.05} {"user":"hello-from-cars","discount":0.05} {"user":"hello-from-hotels","discount":0.05}
Restrict access to discounts service
Your goal is to restrict access to the discounts service so that only specific backend services can access it, while preventing access from the travels REST API. This is achieved using an Istio Custom Resource (CR) AuthorizationPolicy.
-
Click the (+) button on the top navigation bar of {ocp_cluster_openshift_cluster_console_url}[OpenShift Console^, window="console"] to create a new resource. Log in with username
adminand password{ocp_cluster_openshift_cluster_admin_password}if prompted.
-
In the YAML editor, copy the following AuthorizationPolicy CR, and click Create.
apiVersion: security.istio.io/v1 kind: AuthorizationPolicy metadata: name: allow-only-trusted-services namespace: travel-agency spec: selector: matchLabels: app: discounts version: v1 action: ALLOW rules: - from: - source: principals: - cluster.local/ns/travel-agency/sa/discount-access-saA brief description of the above
AuthorizationPolicy-
the selector specifies the workload which is the
discountsservice in this case -
the action ALLOWs traffic based on the rules set
-
the rules allows requests from workloads using the service account named
discount-access-sa
The
discount-access-saservice account has already been created and added to the allowed backend services as part of the workshop set up. -
Test the AuthorizationPolicy
-
Run the following command in the upper terminal to ensure the
travelsREST API is no longer able to access thediscountsservice.oc rsh -n travel-agency $(oc get pod -l app=travels -n travel-agency -o jsonpath='{.items[0].metadata.name}') \ curl http://discounts.travel-agency.svc.cluster.local:8000/discounts/hello-from-travels && echo -
You should no longer be able to access
discountsservice but instead get anaccess deniedresponse.RBAC: access deniedIt may take a few seconds for the AuthorizationPolicy to take effect. Please retry if you still see a success response. -
Verify that the allowed services can still access the
discountsservice by running the following command in the upper terminal.oc rsh -n travel-agency $(oc get pod -l app=flights -n travel-agency -o jsonpath='{.items[0].metadata.name}') \ curl http://discounts.travel-agency.svc.cluster.local:8000/discounts/hello-from-flights oc rsh -n travel-agency $(oc get pod -l app=insurances -n travel-agency -o jsonpath='{.items[0].metadata.name}') \ curl http://discounts.travel-agency.svc.cluster.local:8000/discounts/hello-from-insurances oc rsh -n travel-agency $(oc get pod -l app=cars -n travel-agency -o jsonpath='{.items[0].metadata.name}') \ curl http://discounts.travel-agency.svc.cluster.local:8000/discounts/hello-from-cars oc rsh -n travel-agency $(oc get pod -l app=hotels -n travel-agency -o jsonpath='{.items[0].metadata.name}') \ curl http://discounts.travel-agency.svc.cluster.local:8000/discounts/hello-from-hotelsOutput:
{"user":"hello-from-flights","discount":0.05} {"user":"hello-from-insurances","discount":0.05} {"user":"hello-from-cars","discount":0.05} {"user":"hello-from-hotels","discount":0.05}