A misconfigured authorization policy can unintentionally deny valid traffic or allow unauthorized access. In trial mode, an ASM authorization policy evaluates requests and logs the outcome without enforcing rules. This lets you verify that a policy is correct and reliable without affecting your production environment.
Prerequisites
-
A cluster has been added to an ASM instance that is version 1.14 or later. For instructions on how to upgrade an instance, see Upgrade an ASM instance.
-
A sample namespace named foo is created, and automatic injection is enabled. For more information, see Manage global namespaces.
Background information
Authorization policies in Service Mesh (ASM) provide access control for workloads at the namespace and workload levels. Because authorization policies are a traffic management feature, a misconfiguration can unexpectedly block normal business traffic or allow traffic that should be blocked. This poses a significant challenge for mesh administrators. To address this, ASM provides a trial mode (called dry-run mode in Istio) for authorization policies. When you enable trial mode, the policy only logs its evaluation outcome instead of blocking or allowing traffic. Mesh administrators can use these logs to verify whether the policy behaves as expected. After you tune the policy so that it performs as expected, disable trial mode to enforce it.
This example deploys two test applications, sleep and httpbin. The overall workflow is as follows: From the sleep application, use curl to access the httpbin application and verify connectivity. Then, configure an authorization policy in ASM to deny specific requests and enable its trial mode. Next, send requests that match the policy's deny conditions. Because trial mode is enabled, the requests are not denied, but the sidecar logs the trial run. After you confirm from the logs that the policy works as intended, disable trial mode to enforce the policy.
Step 1: Deploy test applications and verify connectivity
-
Create a file named
sleep.yamlwith the following content: -
Connect to the cluster by using kubectl and deploy the
sleepapplication in thefoonamespace.kubectl apply -f sleep.yaml -n foo -
Create a file named
httpbin.yamlwith the following content: -
Deploy the
httpbinapplication in thefoonamespace.kubectl apply -f httpbin.yaml -n foo -
Test the connectivity between the
sleepandhttpbinapplications.for i in {1..20}; do kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl http://httpbin.foo:8000/headers -s -o /dev/null -w "%{http_code}\n"; doneExpected output:
200 200 200 ...The
200status code returned for all 20 requests indicates that thesleeppod can connect to thehttpbinapplication.
Step 2: Create a policy and enable trial mode
-
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 , and then click Create.
-
On the Create page, configure the following parameters, and then click Create.
-
Name: test
-
Policy Type: Deny
-
Select Enable Trial Mode.
-
Click the Workload tab. Set Namespace to foo, set the scope to Service, set Workload to httpbin, and set the matching label to
app:httpbin. -
In the Request Matching Rules section, turn on the HTTP Paths switch, and enter /headers.
-
Step 3: Observe the policy effect
-
Send requests from the
sleepapplication to thehttpbinapplication again.for i in {1..20}; do kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl http://httpbin.foo:8000/headers -s -o /dev/null -w "%{http_code}\n"; doneExpected output:
200 200 200 ...The requests are still successful because the authorization policy is in trial mode.
-
Set the Role-Based Access Control (RBAC) logging level of the
httpbinapplication's sidecar todebug.kubectl exec "$(kubectl get pod -l app=httpbin -n foo -o jsonpath={.items..metadata.name})" -c istio-proxy -n foo -- curl -X POST 127.0.0.1:15000/logging?rbac=debugExpected output:
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0active loggers: ... rbac: debug ... 100 1028 0 1028 0 0 1003k 0 --:--:-- --:--:-- --:--:-- 1003k -
Filter the sidecar logs of the
httpbinapplication for the authorization policy's dry-run output.kubectl logs "$(kubectl -n foo -l app=httpbin get pods -o jsonpath={.items..metadata.name})" -c istio-proxy -n foo | grep "shadow denied"The output contains dry-run logs similar to the following:
2023-12-20T03:58:47.107915Z debug envoy rbac external/envoy/source/extensions/filters/http/rbac/rbac_filter.cc:130 shadow denied, matched policy ns[foo]-policy[test]-rule[0] thread=32 2023-12-20T03:58:48.800098Z debug envoy rbac external/envoy/source/extensions/filters/http/rbac/rbac_filter.cc:130 shadow denied, matched policy ns[foo]-policy[test]-rule[0] thread=33 2023-12-20T03:58:50.420179Z debug envoy rbac external/envoy/source/extensions/filters/http/rbac/rbac_filter.cc:130 shadow denied, matched policy ns[foo]-policy[test]-rule[0] thread=32
Step 4: Disable trial mode
After verifying from the logs that the authorization policy behaves as expected, disable trial mode to enforce the policy.
-
Log on to the ASM console. In the left-side navigation pane, choose .
-
On the Authorization Policy page, find the authorization policy that you created in Step 2, turn off the switch in the Trial Mode column. Then click OK in the Confirm dialog.
-
Send the requests again.
for i in {1..20}; do kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl http://httpbin.foo:8000/headers -s -o /dev/null -w "%{http_code}\n"; doneExpected output:
403 403 403 ...The output shows that the requests are denied with a
403status code, which indicates that the authorization policy is enforced. -
After the test is complete, restore the sidecar's logging level to
warning.kubectl exec "$(kubectl get pod -l app=httpbin -n foo -o jsonpath={.items..metadata.name})" -c istio-proxy -n foo -- curl -X POST 127.0.0.1:15000/logging?rbac=warningExpected output:
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0active loggers: ... rbac: warning ... 100 1028 0 1028 0 0 1003k 0 --:--:-- --:--:-- --:--:-- 1003k
Related documents
-
To control access and manage permissions for workloads in a cluster, use an authorization policy to set access conditions, such as the request path, method, and client IP address. This ensures that only requests that meet these conditions can access the workloads. For more information, see Configure an access policy for a workload.
-
For fine-grained control over HTTP or TCP traffic between services, set authorization policies. For more information, see Configure an authorization policy for HTTP traffic and Configure an authorization policy for TCP traffic.
-
To control access to services outside the mesh, see Control access from services in a mesh to an external website and Control access from services in a mesh to an external database.
-
Customize the content of ASM gateway access logs to promptly identify potential security issues. For more information, see Generate and collect access logs of an ASM gateway.
-
The mesh audit feature lets you record or trace the routine operations of different users. For more information, see Use KubeAPI operation audit. This feature also lets you configure audit alerts for operations on mesh resources, which promptly notify an alert contact when critical resources are modified.