Use ASM Security Policies to Connect to an External OPA Execution Engine
Service Mesh (ASM) abstracts Istio native security resources into ASM security policies, which centralize the configuration of common security scenarios. ASM integrates the Open Policy Agent (OPA) plugin, so access control policies defined with OPA deliver fine-grained access control for your applications.
Prerequisites
A managed Kubernetes cluster is added to an ASM instance that runs version 1.15.3.25 or later. For instructions, see Add a cluster to an ASM instance and Upgrade an ASM instance.
The httpbin application is deployed and accessible. For instructions, see Deploy the HTTPBin application.
Automatic sidecar injection is enabled for the default namespace. For instructions, see Configure sidecar injection policies.
How it works
By default, ASM deploys OPA in sidecar mode. After you enable OPA and restart the application Pod, ASM injects an OPA container into the application Pod. All requests from the application container are then validated by the OPA sidecar. Sidecar mode keeps all access inside the Pod, which delivers lower latency and suits latency-sensitive workloads.
However, sidecar mode has drawbacks, such as resource consumption, Pod restarts for application onboarding, and limited flexibility. Centralized OPA complements sidecar mode in these aspects. It provides benefits such as lower resource consumption, application onboarding without a restart, and the freedom to decidewhich requests reaching the application are evaluated against OPA policies. The following sections describe how to deploy a centralized OPA authorization service and connect it to your applications.

Step 1: Deploy OPA
Create the asm-opa.yaml file with the following content.
The YAML file deploys an OPA Service, an OPA Deployment, and a Secret.
Kind
Description
Deployment
Replace the region in the image
registry-vpc.cn-hangzhou.aliyuncs.com/acs/opa:0.46.1-istio-3-staticwith the region of your cluster.This OPA engine enables logging
--set=decision_logs.console=trueby default to simplify debugging.
Secret
The Secret defines the OPA policy. The policy rules are as follows:
If the request path is
health, the request is allowed.If the request method is
HEAD, the request is allowed.If the username is
alice, the request is allowed.NoteThe username comes from the
Authorizationheader of the request, in the formatAuthorization: Basic ${Base64 encoding of the username:password string}.
Use the kubeconfig file of the Container Service for Kubernetes (ACK) cluster to deploy OPA.
kubectl apply -f asm-opa.yaml
Step 2: Use an ASM security policy to connect to OPA
-
Log on to the ASM console. In the left-side navigation pane, choose .
-
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose .
On the ASMSecurityPolicy page, click Create. In the Create ASMSecurityPolicy dialog box, click the Custom Authorization Service card, and then click OK.
Configure the custom authorization information.
On the Create Custom Authorization Service page, configure the information of the OPA authorization service that you deployed in Step 1, and then click Next.
Configure the following parameters: Set ASM Security Policy Name to
opa. Set Protocol to GRPC. Set Service Address toasm-opa.default.svc.cluster.local. Set Service Port to9191. Set Timeout to10seconds.In the Workload and Match Rules wizard, click Add Workload Group. Then, in the New Workload Group dialog box, enter a Workload Group Name and click Add Workload.
In the Add Workload dialog box, select Workload Scope, set Namespaces to default, and set Workload Type to Service. In the Select workloads area, select httpbin, click the
icon, and then click OK.In the New Workload Group dialog box, go to the Match Rule List area. Match Mode: Select The selected request must be authenticated. Matching Rules: Select Custom Matching Rules. Turn on the Path switch and set it to
/status/*, and then click OK.In Workload Group Name, enter
httpbin. In Workload List, add a workload with the type Sidecar, the workload httpbin, and the labelapp:httpbin.In the Workload and Match Rules wizard, click Submit.
After the policy is created and submitted, the page displays The ASM security policy is created. The page also shows the Istio resources that are created: ASMExtensionProvider (resource name
grpcextauth-asmsecuritypolicy-opa) and AuthorizationPolicy (resource nameopa-ap-wg-httpbin-default-service-httpbin). Click View YAML to view the resource details, and then click Done.
Step 3: Test access to the httpbin application
Access the
/path.curl ${ASM_GATEWAY_IP}/ -I -X GETExpected output:
HTTP/1.1 200 OK server: istio-envoy date: Tue, 25 Jul 2023 08:30:58 GMT content-type: text/html; charset=utf-8 content-length: 9593 access-control-allow-origin: * access-control-allow-credentials: true x-envoy-upstream-service-time: 2The expected output indicates that this path does not require authentication and the request is allowed.
Access the
/status/201path without valid credentials.curl ${ASM_GATEWAY_IP}/status/201 -I -X GETExpected output:
HTTP/1.1 403 Forbidden date: Tue, 25 Jul 2023 08:31:18 GMT server: istio-envoy content-length: 0 x-envoy-upstream-service-time: 1The expected output indicates that the request is denied because it carries no valid credentials.
Access the
/status/201path with valid credentials.curl ${ASM_GATEWAY_IP}/status/201 -I -X GET --user alice:testpasswordExpected output:
HTTP/1.1 201 Created server: istio-envoy date: Tue, 25 Jul 2023 08:31:38 GMT content-type: text/html; charset=utf-8 access-control-allow-origin: * access-control-allow-credentials: true content-length: 0 x-envoy-upstream-service-time: 3The expected output indicates that the request is allowed because it carries valid credentials.
Step 4: Update the OPA policy
Call the HTTP API of the OPA engine to update the OPA policy.
Update the policy so that only the bob user can access HTTP and the previous alice user cannot.
kubectl exec deployment/httpbin -c istio-proxy -- curl asm-opa:8181/v1/policies/policy/policy.rego -XPUT --data-binary 'package asm.authz import future.keywords import input.attributes.request.http as http_request import input.parsed_path default allow := false allow if { parsed_path[0] == "health" } allow if { http_request.method == "HEAD" } allow if { user_name == "bob" } user_name := parsed if { [_, encoded] := split(http_request.headers.authorization, " ") [parsed, _] := split(base64url.decode(encoded), ":") }'Access httpbin as the bob user.
curl ${ASM_GATEWAY_IP}/status/201 -I -X GET --user bob:testpasswordExpected output:
HTTP/1.1 201 Created server: istio-envoy date: Tue, 25 Jul 2023 08:32:16 GMT content-type: text/html; charset=utf-8 access-control-allow-origin: * access-control-allow-credentials: true content-length: 0 x-envoy-upstream-service-time: 3The expected output indicates that access from the bob user succeeds.
Access httpbin as the alice user.
curl ${ASM_GATEWAY_IP}/status/201 -I -X GET --user alice:testpasswordExpected output:
HTTP/1.1 403 Forbidden date: Tue, 25 Jul 2023 08:32:49 GMT server: istio-envoy content-length: 0 x-envoy-upstream-service-time: 1The expected output indicates that access from the alice user is denied.