You are viewing archived v1.23 Service Mesh documentation.
Available versions
Cloud Service Mesh latest
Cloud Service Mesh 1.26 archive
Cloud Service Mesh 1.24 archive
Cloud Service Mesh 1.24 archive
Cloud Service Mesh 1.23 archive
Cloud Service Mesh 1.22 archive
Cloud Service Mesh 1.21 archive
Cloud Service Mesh 1.20 archive
Anthos Service Mesh 1.19 archive
Cloud Service Mesh by example: Authorization Stay organized with collections Save and categorize content based on your preferences.
In this tutorial, you will learn what authorization is, and how to enable itwith Cloud Service Mesh on a sample application to learn how to enableauthorization policies to your microservices. You will create anAuthorizationPolicy toDENY access to a microservice, then create anAuthorizationPolicy toALLOW specific access to a microservice.
What is authorization?
Authentication verifies an identity -- is this service who they say they are?Authorization verifies the permission - is this service allowed to do that?Identity is fundamental to this idea. With Cloud Service Mesh,AuthorizationPolicies allow for workload-to-workload communication in yourmesh to be controlled for improved security and access.
In a microservice architecture, where calls are made across network boundaries,IP-based firewall rules are often not adequate to secure access betweenworkloads. With Cloud Service Mesh, you can set authorization rules to:
- Control access to workloads within your mesh, either workload-to-workload orend-user-to-workload
- Broadly or granularly define policies depending on your needs.
To see an in-depth explanation on configuring policies and best practices, seeAuthorization with Cloud Service Mesh.
Costs
This tutorial uses the following billable components of Google Cloud:
When you finish this tutorial, you can avoid ongoing costs by deleting theresources you created. For more information, seeClean up.
Before you begin
Provision Cloud Service Mesh on a GKE cluster. There arevarious supported setup methods:
Clone the repo:
git clone https://github.com/GoogleCloudPlatform/anthos-service-mesh-samplescd anthos-service-mesh-samples
Deploy an ingress gateway
Set the current context for
Note: Usekubectlto the cluster:--regioninstead of--zone, if the cluster is a regionalcluster.gcloud container clusters get-credentialsCLUSTER_NAME \--project=PROJECT_ID \--zone=CLUSTER_LOCATIONCreate a namespace for your ingress gateway:
kubectl create namespace asm-ingressEnable the namespace for injection. The steps depend on yourcontrol plane implementation.
Managed (TD)
Apply the default injection label to the namespace:
kubectllabelnamespaceasm-ingress\istio.io/rev-istio-injection=enabled--overwriteManaged (Istiod)
Recommended: Run the following command to apply the default injection label to the namespace:
kubectllabelnamespaceasm-ingress\istio.io/rev-istio-injection=enabled--overwriteIf you are an existing user with the Managed Istiod control plane:We recommend that you use default injection, but revision-based injection issupported. Use the following instructions:
Run the following command to locate the available release channels:
kubectl-nistio-systemgetcontrolplanerevisionThe output is similar to the following:
Note: If two control plane revisions appear in the earlier list, remove one. Having multiple control plane channels in the cluster is not supported.NAME AGEasm-managed-rapid 6d7hIn the output, the value under the
NAMEcolumn is the revision label that corresponds to the availablerelease channel for the Cloud Service Mesh version.Apply the revision label to the namespace:
kubectllabelnamespaceasm-ingress\istio-injection-istio.io/rev=REVISION_LABEL--overwrite
In-cluster
Recommended: Run the following command to apply the default injection label to the namespace:
kubectllabelnamespaceasm-ingress\istio.io/rev-istio-injection=enabled--overwriteWe recommend that you use default injection, but revision-based injection is supported:Use the following instructions:
Use the following command to locate the revision label on
istiod:kubectlgetdeploy-nistio-system-lapp=istiod-o\jsonpath={.items[*].metadata.labels.'istio\.io\/rev'}'{"\n"}'Apply the revision label to the namespace. In the following command,
REVISION_LABELis the value of theistiodrevisionlabel that you noted in the previous step.kubectllabelnamespaceasm-ingress\istio-injection-istio.io/rev=REVISION_LABEL--overwrite
Deploy the example gateway in the
anthos-service-mesh-samplesrepo:kubectl apply -n asm-ingress \-f docs/shared/asm-ingress-gatewayExpected output:
serviceaccount/asm-ingressgateway configuredservice/asm-ingressgateway configureddeployment.apps/asm-ingressgateway configuredgateway.networking.istio.io/asm-ingressgateway configured
Deploy the Online Boutique sample application
If you haven't, set the current context for
kubectlto the cluster:gcloud container clusters get-credentialsCLUSTER_NAME \--project=PROJECT_ID \--zone=CLUSTER_LOCATIONCreate the namespace for the sample application:
kubectl create namespace onlineboutiqueLabel the
onlineboutiquenamespace to automatically inject Envoy proxies. Follow the stepsto enable automatic sidecar injection.Deploy the sample app, the
VirtualServicefor the frontend, and service accounts for the workloads. For this tutorial, you will deployOnline Boutique, a microservice demo app.kubectl apply \-n onlineboutique \-f docs/shared/online-boutique/virtual-service.yamlkubectl apply \-n onlineboutique \-f docs/shared/online-boutique/service-accounts
View your services
View the pods in the
onlineboutiquenamespace:kubectl get pods -n onlineboutiqueExpected output:
NAME READY STATUS RESTARTS AGEadservice-85598d856b-m84m6 2/2 Running 0 2m7scartservice-c77f6b866-m67vd 2/2 Running 0 2m8scheckoutservice-654c47f4b6-hqtqr 2/2 Running 0 2m10scurrencyservice-59bc889674-jhk8z 2/2 Running 0 2m8semailservice-5b9fff7cb8-8nqwz 2/2 Running 0 2m10sfrontend-77b88cc7cb-mr4rp 2/2 Running 0 2m9sloadgenerator-6958f5bc8b-55q7w 2/2 Running 0 2m8spaymentservice-68dd9755bb-2jmb7 2/2 Running 0 2m9sproductcatalogservice-84f95c95ff-c5kl6 2/2 Running 0 114srecommendationservice-64dc9dfbc8-xfs2t 2/2 Running 0 2m9sredis-cart-5b569cd47-cc2qd 2/2 Running 0 2m7sshippingservice-5488d5b6cb-lfhtt 2/2 Running 0 2m7sAll of the pods for your application should be up and running, with a
2/2in theREADYcolumn. This indicates that the pods have an Envoy sidecar proxy injected successfully. If it does not show2/2after a couple of minutes, visit theTroubleshooting guide.Get the external IP, and set it to a variable:
kubectl get services -n asm-ingressexport FRONTEND_IP=$(kubectl --namespace asm-ingress \get service --output jsonpath='{.items[0].status.loadBalancer.ingress[0].ip}' \)You see output similar to the following:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEasm-ingressgateway LoadBalancer 10.19.247.233 35.239.7.64 80:31380/TCP,443:31390/TCP,31400:31400/TCP 27mVisit the
EXTERNAL-IPaddress in your web browser. You should expect to seethe Online Boutique shop in your browser.
DenyAll Authorization for a workload
This section adds anAuthorizationPolicy to deny all incoming traffic to thecurrency service.AuthorizationPolicies work by transformingAuthorizationPolicies into Envoy-readable configs, and applying the configs toyour sidecar proxies. This enables the Envoy proxy to authorize or deny incomingrequests to a service.
Apply an
AuthorizationPolicyto thecurrencyservice. Notice the match on the labelcurrencyservicein the YAML file.kubectl apply -f docs/authorization/currency-deny-all.yaml -n onlineboutiqueapiVersion:security.istio.io/v1beta1kind:AuthorizationPolicymetadata:name:currency-policyspec:selector:matchLabels:app:currencyserviceTry accessing your gateway's
EXTERNAL-IPto view Online Boutique in the webbrowser. You should see an authorization error (500 Internal Service Error)fromcurrency service.
Observe your sidecar proxy logs
To see what is occurring in the sidecar proxy, you can review the logs in thepod.
Get the name of your
currencyservicepod:CURRENCY_POD=$(kubectl get pod -n onlineboutique |grep currency|awk '{print $1}')Set the Envoy proxy to allow for trace level logs. By default, blocked authorization calls are not logged:
kubectl debug --image istio/base --target istio-proxy -it $CURRENCY_POD -n onlineboutique -- curl -X POST "http://localhost:15000/logging?level=trace"Expected output:
none {:.devsite-disable-click-to-copy}active loggers:admin: tracealternate_protocols_cache: trace...tracing: traceupstream: traceudp: tracewasm: traceUse
curlto send traffic to yourEXTERNAL_IPto generate logs:for i in {0..10}; docurl -s -I $FRONTEND_IP ; doneView the role-based access control (RBAC) related logs in your istio-proxy:
kubectl logs -n onlineboutique $CURRENCY_POD -c istio-proxy | grep -m5 rbacExpected output:
2022-07-08T14:19:20.442920Z debug envoy rbac checking request: requestedServerName: outbound_.7000_._.currencyservice.onlineboutique.svc.cluster.local, sourceIP: 10.8.8.5:34080, directRemoteIP: 10.8.8.5:34080, remoteIP: 10.8.8.5:34080,localAddress: 10.8.0.6:7000, ssl: uriSanPeerCertificate: spiffe://christineskim-tf-asm.svc.id.goog/ns/onlineboutique/sa/default, dnsSanPeerCertificate: , subjectPeerCertificate: OU=istio_v1_cloud_workload,O=Google LLC,L=Mountain View,ST=California,C=US, headers: ':method', 'POST'2022-07-08T14:19:20.442944Z debug envoy rbac enforced denied, matched policy none2022-07-08T14:19:20.442965Z debug envoy http [C73987][S13078781800499437460] Sending local reply with details rbac_access_denied_matched_policy[none] ```
You should see anenforced denied message in the logs, showing thatcurrencyservice is set to block inbound requests.
Allow Restricted Access
Instead of aDENYALL policy, you can set access to be allowed for certainworkloads. This will be relevant in a microservice architecture where you wantto ensure that only authorized services can communicate with each other.
In this section, you will enable thefrontend andcheckout service theability to communicate with thecurrency service.
- In the following file, see that a specific
source.principal(client) isallowed to accesscurrencyservice:
apiVersion:security.istio.io/v1beta1kind:AuthorizationPolicymetadata:name:currency-policyspec:selector:matchLabels:app:currencyservicerules:-from:-source:principals:["cluster.local/ns/onlineboutique/sa/frontend"]-from:-source:principals:["cluster.local/ns/onlineboutique/sa/checkoutservice"]Apply the policy:
kubectl apply -f docs/authorization/currency-allow-frontend-checkout.yaml -n onlineboutiqueVisit the
EXTERNAL-IPin your web browser, you should now be able to accessOnline Boutique.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
To avoid incurring continuing charges to your Google Cloud account forthe resources used in this tutorial, you can eitherdelete the project or deletethe individual resources.
Delete the project
Caution: Deleting a project has the following effects:- Everything in the project is deleted. If you used an existing project for this tutorial, when you delete it, you also delete any other work you've done in the project.
- Custom project IDs are lost. When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an appspot.com URL, delete selected resources inside the project instead of deleting the whole project.
In Cloud Shell, delete the project:
gcloud projects deletePROJECT_IDDelete the resources
If you want to keep your cluster and remove the Online Boutique sample:
Delete the application namespaces:
kubectl delete namespace onlineboutiqueExpected output:
namespace "onlineboutique" deletedDelete the Ingress Gateway namespace:
kubectl delete namespace asm-ingressExpected output:
amespace "asm-ingress" deleted
If you want to prevent additional charges, delete the cluster:
gcloud container clusters deleteCLUSTER_NAME \--project=PROJECT_ID \--zone=CLUSTER_LOCATION
What's next
- For a general guide on configuring
PeerAuthenticationpolicies, seeConfiguring transport security. - Explore your mesh's security dashboard withMonitor mesh security.
- Deep dive with authorization policies withConfigure authorization policy advanced features.
- Familiarize yourself withCloud Service Mesh security best practices.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.