Authorization policies control which services can communicate over TCP in your mesh. By restricting access to specific ports, you make sure that only authorized traffic reaches a service -- reducing your attack surface without changing application code.
This walkthrough covers deploying sample TCP services, creating an ALLOW authorization policy that permits traffic on a single port, and verifying the result.
How it works
An AuthorizationPolicy resource defines access rules at the namespace or workload level. For TCP traffic, the policy matches on Layer 4 (L4) attributes only. HTTP-specific fields (Layer 7) are not supported.
HTTP-only fields such as methods, paths, and headers do not apply to TCP traffic. If an ALLOW rule includes HTTP-only fields, ASM ignores that rule. Because no valid ALLOW rule remains, all TCP traffic to the workload is denied.
Do not mix HTTP-specific fields into TCP authorization policies.
Prerequisites
Before you begin, make sure that you have:
A cluster added to the ASM instance
Istio resources defined for traffic routing. See Use Istio resources to route traffic to different versions of a service
The
foonamespace created with sidecar proxy injection enabled. See Create a namespacekubectl connected to your cluster. See Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster
Deploy sample applications
Deploy two applications in the foo namespace: tcp-echo (receives TCP requests) and sleep (sends TCP requests).
Deploy tcp-echo
The tcp-echo service listens on ports 9000 and 9001. When it receives a message, it prepends hello and returns the result. For example, sending world returns hello world.
Create a file named
tcp-echo.yamlwith the following content:Apply the manifest:
kubectl apply -f tcp-echo.yaml -n foo
Deploy sleep
The sleep service acts as the TCP client for testing.
Create a file named
sleep.yamlwith the following content:Apply the manifest:
kubectl apply -f sleep.yaml -n foo
Verify connectivity before the policy
Before creating the authorization policy, confirm that sleep can reach tcp-echo on both ports.
Test 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 succeededTest 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 succeededBoth connections succeed because no authorization policy is in place.
Create an authorization policy
Create an ALLOW policy that permits TCP traffic to tcp-echo only on port 9000. Traffic on all other ports is denied.
To test a policy before enforcing it, use trial mode. Trial mode logs policy decisions without blocking traffic, so you can verify expected behavior first.
Use one of the following methods.
Use kubectl
Apply the authorization policy directly from the command line:
kubectl apply -f - <<EOF
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: demo
namespace: foo
spec:
action: ALLOW
rules:
- to:
- operation:
ports:
- "9000"
EOFUse the ASM console (YAML)
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.
Click Create from YAML.
Select foo from the Namespace drop-down list, paste the following YAML into the editor, and click Create:
kind: AuthorizationPolicy apiVersion: security.istio.io/v1beta1 metadata: name: demo namespace: foo spec: action: ALLOW rules: - to: - operation: ports: - "9000"
Use the ASM console (GUI)
Follow steps 1-4 in the YAML method above to navigate to the AuthorizationPolicy page.
Click Create.
Set the following parameters, then click Create:
Parameter Value Name demoPolicy Type ALLOW Namespace (on the Workload Scope tab) foo Effective Scope Namespace Scope Request Matching Rules Turn on Ports in the Add Request Target section and enter 9000
Do not turn on the Methods switch in the Add Request Target section. The Methods field applies only to HTTP requests. For TCP requests, an ALLOW rule that includes Methods is invalid and silently ignored by ASM. With no valid ALLOW rule remaining, all TCP traffic to the workload is denied.
Verify the authorization policy
After the policy is created, test connectivity again to confirm that only port 9000 is accessible.
Test port 9001 (expected: 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 rejectedPort 9001 is now blocked because the ALLOW policy only permits port 9000.
Test port 9000 (expected: 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 9000 remains accessible as specified in the policy.
What's next
Run an ASM authorization policy in trial mode -- Test policies without blocking live traffic
Configure authorization policies for HTTP requests -- Apply fine-grained access control for HTTP services, with support for method, path, and header matching
Control access to external websites -- Restrict outbound traffic from mesh services to external endpoints
Control access to external databases -- Restrict outbound traffic from mesh services to external databases
Configure access logs for the ASM gateway -- Customize gateway access logs to detect security anomalies
Use the KubeAPI operation audit feature in ASM -- Track operational changes across users
Configure audit alerts for ASM resources -- Get notified when mesh resources change