diff --git a/content/en/docs/tasks/security/authorization/authz-tcp/index.md b/content/en/docs/tasks/security/authorization/authz-tcp/index.md index ff3bd07641..597810a364 100644 --- a/content/en/docs/tasks/security/authorization/authz-tcp/index.md +++ b/content/en/docs/tasks/security/authorization/authz-tcp/index.md @@ -5,6 +5,7 @@ weight: 20 keywords: [security,access-control,rbac,tcp,authorization] aliases: - /docs/tasks/security/authz-tcp/ +test: true --- This task shows you how to set up Istio authorization for TCP traffic in an Istio mesh. @@ -35,13 +36,13 @@ Deploy the example namespace and workloads using the following command: using the following command: {{< text bash >}} - $ 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' + $ 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' hello port 9000 connection succeeded {{< /text >}} {{< text bash >}} - $ 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' + $ 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' hello port 9001 connection succeeded {{< /text >}} @@ -52,8 +53,8 @@ defined in the Kubernetes service object of `tcp-echo`. Get the pod IP address and send the request with the following command: {{< text bash >}} - $ TCP_ECHO_IP=$(kubectl get pod $(kubectl get pod -l app=tcp-echo -n foo -o jsonpath={.items..metadata.name}) -n foo -o jsonpath={.status.podIP}) - $ kubectl exec $(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name}) -c sleep -n foo -- sh -c "echo \"port 9002\" | nc $TCP_ECHO_IP 9002" | grep "hello" && echo 'connection succeeded' || echo 'connection rejected' + $ TCP_ECHO_IP=$(kubectl get pod "$(kubectl get pod -l app=tcp-echo -n foo -o jsonpath={.items..metadata.name})" -n foo -o jsonpath="{.status.podIP}") + $ kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- sh -c "echo \"port 9002\" | nc $TCP_ECHO_IP 9002" | grep "hello" && echo 'connection succeeded' || echo 'connection rejected' hello port 9002 connection succeeded {{< /text >}} @@ -89,7 +90,7 @@ Run the following command to apply the policy to allow requests to port 9000 and 1. Verify that requests to port 9000 are allowed using the following command: {{< text bash >}} - $ 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' + $ 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' hello port 9000 connection succeeded {{< /text >}} @@ -97,7 +98,7 @@ Run the following command to apply the policy to allow requests to port 9000 and 1. Verify that requests to port 9001 are allowed using the following command: {{< text bash >}} - $ 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' + $ 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' hello port 9001 connection succeeded {{< /text >}} @@ -107,7 +108,7 @@ policy which also applies to the pass through filter chain, even if the port is explicitly in the `tcp-echo` Kubernetes service object. Run the following command and verify the output: {{< text bash >}} - $ kubectl exec $(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name}) -c sleep -n foo -- sh -c "echo \"port 9002\" | nc $TCP_ECHO_IP 9002" | grep "hello" && echo 'connection succeeded' || echo 'connection rejected' + $ kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- sh -c "echo \"port 9002\" | nc $TCP_ECHO_IP 9002" | grep "hello" && echo 'connection succeeded' || echo 'connection rejected' connection rejected {{< /text >}} @@ -139,7 +140,7 @@ The final result is that the request is rejected, because it does not match any Run the following command and verify the output: {{< text bash >}} - $ 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' + $ 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' connection rejected {{< /text >}} @@ -147,7 +148,7 @@ Run the following command and verify the output: ALLOW rules. Run the following command and verify the output: {{< text bash >}} - $ 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' + $ 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' connection rejected {{< /text >}} @@ -179,7 +180,7 @@ which causes Istio to ignore the entire rule. The final result is that only the field is used by Istio and the requests are denied because they match with the `ports`: {{< text bash >}} - $ 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' + $ 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' connection rejected {{< /text >}} @@ -187,7 +188,7 @@ field is used by Istio and the requests are denied because they match with the ` the `ports` in the DENY policy: {{< text bash >}} - $ 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' + $ 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' hello port 9001 connection succeeded {{< /text >}} diff --git a/content/en/docs/tasks/security/authorization/authz-tcp/snips.sh b/content/en/docs/tasks/security/authorization/authz-tcp/snips.sh new file mode 100644 index 0000000000..3c7ccd667d --- /dev/null +++ b/content/en/docs/tasks/security/authorization/authz-tcp/snips.sh @@ -0,0 +1,177 @@ +#!/bin/bash +# shellcheck disable=SC2034,SC2153,SC2155 + +# Copyright Istio Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#################################################################################################### +# WARNING: THIS IS AN AUTO-GENERATED FILE, DO NOT EDIT. PLEASE MODIFY THE ORIGINAL MARKDOWN FILE: +# docs/tasks/security/authorization/authz-tcp/index.md +#################################################################################################### + +snip_before_you_begin_1() { +kubectl create ns foo +kubectl apply -f <(istioctl kube-inject -f samples/tcp-echo/tcp-echo.yaml) -n foo +kubectl apply -f <(istioctl kube-inject -f samples/sleep/sleep.yaml) -n foo +} + +snip_before_you_begin_2() { +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' +} + +! read -r -d '' snip_before_you_begin_2_out <<\ENDSNIP +hello port 9000 +connection succeeded +ENDSNIP + +snip_before_you_begin_3() { +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' +} + +! read -r -d '' snip_before_you_begin_3_out <<\ENDSNIP +hello port 9001 +connection succeeded +ENDSNIP + +snip_before_you_begin_4() { +TCP_ECHO_IP=$(kubectl get pod "$(kubectl get pod -l app=tcp-echo -n foo -o jsonpath={.items..metadata.name})" -n foo -o jsonpath="{.status.podIP}") +kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- sh -c "echo \"port 9002\" | nc $TCP_ECHO_IP 9002" | grep "hello" && echo 'connection succeeded' || echo 'connection rejected' +} + +! read -r -d '' snip_before_you_begin_4_out <<\ENDSNIP +hello port 9002 +connection succeeded +ENDSNIP + +snip_configure_access_control_for_a_tcp_workload_1() { +kubectl apply -f - <&1) +_verify_same "$out" "$snip_before_you_begin_2_out" "snip_before_you_begin_2" + +out=$(snip_before_you_begin_3 2>&1) +_verify_same "$out" "$snip_before_you_begin_3_out" "snip_before_you_begin_3" + +out=$(snip_before_you_begin_4 2>&1) +_verify_same "$out" "$snip_before_you_begin_4_out" "snip_before_you_begin_4" + +snip_configure_access_control_for_a_tcp_workload_1 + +out=$(snip_configure_access_control_for_a_tcp_workload_2 2>&1) +_verify_same "$out" "$snip_configure_access_control_for_a_tcp_workload_2_out" "snip_configure_access_control_for_a_tcp_workload_2" + +out=$(snip_configure_access_control_for_a_tcp_workload_3 2>&1) +_verify_same "$out" "$snip_configure_access_control_for_a_tcp_workload_3_out" "snip_configure_access_control_for_a_tcp_workload_3" + +out=$(snip_configure_access_control_for_a_tcp_workload_4 2>&1) +_verify_same "$out" "$snip_configure_access_control_for_a_tcp_workload_4_out" "snip_configure_access_control_for_a_tcp_workload_4" + +snip_configure_access_control_for_a_tcp_workload_5 + +out=$(snip_configure_access_control_for_a_tcp_workload_6 2>&1) +_verify_same "$out" "$snip_configure_access_control_for_a_tcp_workload_6_out" "snip_configure_access_control_for_a_tcp_workload_6" + +out=$(snip_configure_access_control_for_a_tcp_workload_7 2>&1) +_verify_same "$out" "$snip_configure_access_control_for_a_tcp_workload_7_out" "snip_configure_access_control_for_a_tcp_workload_7" + +snip_configure_access_control_for_a_tcp_workload_8 + +out=$(snip_configure_access_control_for_a_tcp_workload_9 2>&1) +_verify_same "$out" "$snip_configure_access_control_for_a_tcp_workload_9_out" "snip_configure_access_control_for_a_tcp_workload_9" + +out=$(snip_configure_access_control_for_a_tcp_workload_10 2>&1) +_verify_same "$out" "$snip_configure_access_control_for_a_tcp_workload_10_out" "snip_configure_access_control_for_a_tcp_workload_10"