To apply fine-grained control over TCP traffic between services, configure an authorization policy to manage the access permissions between services. Only authorized requests reach a specific service, which improves the security and reliability of the service.
Prerequisites
-
Istio resources are defined. For more information, see Use Istio resources to route traffic based on version.
-
The
foonamespace is created and sidecar proxy injection is enabled. For more information, see Create a namespace and Enable automatic injection.
Step 1: Deploy the sample applications
Deploy tcp-echo as the target TCP service that receives requests and sleep as the client TCP service that sends requests.
-
Deploy the target TCP service.
-
Create a
tcp-echo.yamlfile with the following content:When the
tcp-echoservice receives a request, it adds thehelloprefix to the request content and returns the result. For example, if you sendworldtotcp-echo, the service returnshello world. -
Use kubectl to connect to the cluster and run the following command to deploy the
tcp-echoservice to thefoonamespace.For more information about how to use kubectl to connect to a cluster, see Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.
kubectl apply -f tcp-echo.yaml -n foo
-
-
Deploy the client TCP service.
-
Create a
sleep.yamlfile with the following content: -
Run the following command to deploy the
sleepservice to thefoonamespace.kubectl apply -f sleep.yaml -n foo
-
Step 2: Verify the connection before authorization
Before you create the authorization policy, confirm that the sleep service can reach both ports of the tcp-echo service. These results are the baseline for the verification in Step 4.
-
Run the following command to verify that the
sleepservice can access thetcp-echoservice on port 9001.kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- sh -c 'echo "port 9001" | nc tcp-echo 9001' | grep "hello" && echo 'connection succeeded' || echo 'connection rejected'Expected output:
hello port 9001 connection succeededThe output confirms that the
sleepservice can access thetcp-echoservice on port 9001. -
Run the following command to verify that the
sleepservice can access thetcp-echoservice on port 9000.kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- sh -c 'echo "port 9000" | nc tcp-echo 9000' | grep "hello" && echo 'connection succeeded' || echo 'connection rejected'Expected output:
hello port 9000 connection succeededThe output confirms that the
sleepservice can access thetcp-echoservice on port 9000.
Step 3: Configure the authorization policy
Configure an authorization policy that allows access to the tcp-echo service on port 9000. The policy takes effect on the entire foo namespace, and its rules allow access only on port 9000. Access on port 9001 is therefore denied after the policy is created. Verify this in Step 4.
-
Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.
-
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Mesh Security Center > AuthorizationPolicy.
-
-
Create an authorization policy by using one of the following methods.
Method 1: Create the authorization policy by using YAML
-
On the AuthorizationPolicy page, click Create from YAML.
-
On the Create page, set Namespaces to
foo, select any Scenario Template, paste the following YAML template into the YAML area, and then click Create.kind: AuthorizationPolicy apiVersion: security.istio.io/v1beta1 metadata: name: demo namespace: foo spec: action: ALLOW rules: - to: - operation: ports: - '9000'
Method 2: Create the authorization policy in the console UI
ImportantWhen you configure a TCP authorization policy, do not turn on the Methods switch. This setting applies only to HTTP requests and does not create a valid
ALLOWrule for TCP traffic. The Service Mesh ignores invalidALLOWrules, which causes requests to be denied andconnection rejectedto be returned.-
On the AuthorizationPolicy page, click Create.
-
On the Create page, configure the following settings, and then click Create.
Parameter Description Name In this example, enter demo.Policy Type Select ALLOW. Namespaces On the Workload Scope tab, select the foonamespace.Effective Scope Select Namespace Scope. Request Matching Rules In the Add Request Target section, turn on the Ports switch and set the value to 9000.
-
Step 4: Verify the connection after authorization
After the authorization policy is created, run the same two connection tests as in Step 2 and compare the results.
-
Run the following command to verify that access to the
tcp-echoservice on port 9001 is denied.kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- sh -c 'echo "port 9001" | nc tcp-echo 9001' | grep "hello" && echo 'connection succeeded' || echo 'connection rejected'Expected output:
connection rejectedThe
connection rejectedoutput confirms that thesleepservice can no longer access thetcp-echoservice on port 9001. -
Run the following command to verify that access to the
tcp-echoservice on port 9000 is still allowed.kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- sh -c 'echo "port 9000" | nc tcp-echo 9000' | grep "hello" && echo 'connection succeeded' || echo 'connection rejected'Expected output:
hello port 9000 connection succeededPort 9001 is denied and port 9000 is still reachable, which confirms that the authorization policy takes effect as expected.
A misconfigured authorization policy can cause requests to be unexpectedly denied or allowed. If either command does not return the expected output, use the trial mode of an ASM authorization policy to determine from the logs whether the policy produces the expected result. Then turn off the trial mode to make the authorization policy take effect. For more information, see Use trial mode for an ASM authorization policy.
References
-
To control HTTP traffic between services at a fine granularity, see Configure an authorization policy for HTTP traffic.
-
To control access to services outside the mesh, see Control access from services in the mesh to external websites and Control access from services in the mesh to external databases.
-
Customize the content of ASM gateway access logs to identify potential security issues promptly. For more information, see Generate and collect ASM gateway access logs.
-
Enable mesh audit to record and trace the daily operations of different users. Additionally, configure audit alerts for mesh resource operations, which send alert notifications to the alert contacts when critical resources change. For more information, see Use KubeAPI operation audit and Configure audit alerts for mesh resource operations.