Ingress Access Control
This task shows you how to enforce IP-based access control on an Istio ingress gateway using an authorization policy.
Istio includes beta support for the Kubernetes Gateway API and intends to make it the default API for traffic management in the future. The following instructions allow you to choose to use either the Gateway API or the Istio configuration API when configuring traffic management in the mesh. Follow instructions under either the Gateway API or Istio classic tab, according to your preference.
Note that the Kubernetes Gateway API CRDs do not come installed by default on most Kubernetes clusters, so make sure they are installed before using the Gateway API:
$ kubectl get crd gateways.gateway.networking.k8s.io &> /dev/null || \{ kubectl kustomize "github.com/kubernetes-sigs/gateway-api/config/crd?ref=v0.6.2" | kubectl apply -f -; }
Before you begin
Before you begin this task, do the following:
Read the Istio authorization concepts.
Install Istio using the Istio installation guide.
Deploy a workload,
httpbin, in namespacefoowith sidecar injection enabled:$ kubectl create ns foo$ kubectl label namespace foo istio-injection=enabled$ kubectl apply -f @samples/httpbin/httpbin.yaml@ -n foo
Expose
httpbinthrough an ingress gateway:
Configure the gateway:
$ kubectl apply -f @samples/httpbin/httpbin-gateway.yaml@ -n foo
Turn on RBAC debugging in Envoy for the ingress gateway:
$ kubectl get pods -n istio-system -o name -l istio=ingressgateway | sed 's|pod/||' | while read -r pod; do istioctl proxy-config log "$pod" -n istio-system --level rbac:debug; done
Follow the instructions in Determining the ingress IP and ports to define the INGRESS_PORT and INGRESS_HOST environment variables.
Create the gateway:
$ kubectl apply -f @samples/httpbin/gateway-api/httpbin-gateway.yaml@ -n foo$ kubectl wait --for=condition=programmed gtw -n foo httpbin-gateway
Turn on RBAC debugging in Envoy for the ingress gateway:
$ kubectl get pods -n foo -o name -l istio.io/gateway-name=httpbin-gateway | sed 's|pod/||' | while read -r pod; do istioctl proxy-config log "$pod" -n foo --level rbac:debug; done
Set the INGRESS_PORT and INGRESS_HOST environment variables:
$ export INGRESS_HOST=$(kubectl get gtw httpbin-gateway -n foo -o jsonpath='{.status.addresses[0].value}')$ export INGRESS_PORT=$(kubectl get gtw httpbin-gateway -n foo -o jsonpath='{.spec.listeners[?(@.name=="http")].port}')
Verify that the
httpbinworkload and ingress gateway are working as expected using this command:$ curl "$INGRESS_HOST:$INGRESS_PORT"/headers -s -o /dev/null -w "%{http_code}\n"200
If you don’t see the expected output, retry after a few seconds. Caching and propagation overhead can cause a delay.
Getting traffic into Kubernetes and Istio
All methods of getting traffic into Kubernetes involve opening a port on all worker nodes. The main features that accomplish this are the NodePort service and the LoadBalancer service. Even the Kubernetes Ingress resource must be backed by an Ingress controller that will create either a NodePort or a LoadBalancer service.
A
NodePortjust opens up a port in the range 30000-32767 on each worker node and uses a label selector to identify which Pods to send the traffic to. You have to manually create some kind of load balancer in front of your worker nodes or use Round-Robin DNS.A
LoadBalanceris just like aNodePort, except it also creates an environment specific external load balancer to handle distributing traffic to the worker nodes. For example, in AWS EKS, theLoadBalancerservice will create a Classic ELB with your worker nodes as targets. If your Kubernetes environment does not have aLoadBalancerimplementation, then it will just behave like aNodePort. An Istio ingress gateway creates aLoadBalancerservice.
What if the Pod that is handling traffic from the NodePort or LoadBalancer isn’t running on the worker node that received the traffic? Kubernetes has its own internal proxy called kube-proxy that receives the packets and forwards them to the correct node.
Source IP address of the original client
If a packet goes through an external proxy load balancer and/or kube-proxy, then the original source IP address of the client is lost. The following subsections describe some strategies for preserving the original client IP for logging or security purpose for different load balancer types:
For reference, here are the types of load balancers created by Istio with a LoadBalancer service on popular managed Kubernetes environments:
| Cloud Provider | Load Balancer Name | Load Balancer Type |
|---|---|---|
| AWS EKS | Classic Elastic Load Balancer | TCP Proxy |
| GCP GKE | TCP/UDP Network Load Balancer | Network |
| Azure AKS | Azure Load Balancer | Network |
| IBM IKS/ROKS | Network Load Balancer | Network |
| DO DOKS | Load Balancer | Network |
You can instruct AWS EKS to create a Network Load Balancer with an annotation on the gateway service:
apiVersion: install.istio.io/v1alpha1kind: IstioOperatorspec:meshConfig:accessLogEncoding: JSONaccessLogFile: /dev/stdoutcomponents:ingressGateways:- enabled: truek8s:hpaSpec:maxReplicas: 10minReplicas: 5serviceAnnotations:service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
apiVersion: gateway.networking.k8s.io/v1beta1kind: Gatewaymetadata:name: httpbin-gatewayannotations:service.beta.kubernetes.io/aws-load-balancer-type: "nlb"spec:gatewayClassName: istio...
TCP/UDP Proxy Load Balancer
If you are using a TCP/UDP Proxy external load balancer (AWS Classic ELB), it can use the Proxy Protocol to embed the original client IP address in the packet data. Both the external load balancer and the Istio ingress gateway must support the proxy protocol for it to work. In Istio, you can enable it with an EnvoyFilter like below:
apiVersion: networking.istio.io/v1alpha3kind: EnvoyFiltermetadata:name: proxy-protocolnamespace: istio-systemspec:configPatches:- applyTo: LISTENER_FILTERpatch:operation: INSERT_FIRSTvalue:name: proxy_protocoltyped_config:"@type": "type.googleapis.com/envoy.extensions.filters.listener.proxy_protocol.v3.ProxyProtocol"workloadSelector:labels:istio: ingressgateway
apiVersion: networking.istio.io/v1alpha3kind: EnvoyFiltermetadata:name: proxy-protocolnamespace: foospec:configPatches:- applyTo: LISTENER_FILTERpatch:operation: INSERT_FIRSTvalue:name: proxy_protocoltyped_config:"@type": "type.googleapis.com/envoy.extensions.filters.listener.proxy_protocol.v3.ProxyProtocol"workloadSelector:labels:istio.io/gateway-name: httpbin-gateway
Here is a sample configuration that shows how to make an ingress gateway on AWS EKS support the Proxy Protocol:
apiVersion: install.istio.io/v1alpha1kind: IstioOperatorspec:meshConfig:accessLogEncoding: JSONaccessLogFile: /dev/stdoutcomponents:ingressGateways:- enabled: truename: istio-ingressgatewayk8s:hpaSpec:maxReplicas: 10minReplicas: 5serviceAnnotations:service.beta.kubernetes.io/aws-load-balancer-proxy-protocol: "*"...
apiVersion: gateway.networking.k8s.io/v1beta1kind: Gatewaymetadata:name: httpbin-gatewayannotations:service.beta.kubernetes.io/aws-load-balancer-proxy-protocol: "*"spec:gatewayClassName: istio...---apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata:name: httpbin-gatewayspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: httpbin-gateway-istiominReplicas: 5maxReplicas: 10
Network Load Balancer
If you are using a TCP/UDP network load balancer that preserves the client IP address (AWS Network Load Balancer, GCP External Network Load Balancer, Azure Load Balancer) or you are using Round-Robin DNS, then you can use the externalTrafficPolicy: Local setting to also preserve the client IP inside Kubernetes by bypassing kube-proxy and preventing it from sending traffic to other nodes.
For production deployments it is strongly recommended to deploy an ingress gateway pod to multiple nodes if you enable externalTrafficPolicy: Local. Otherwise, this creates a situation where only nodes with an active ingress gateway pod will be able to accept and distribute incoming NLB traffic to the rest of the cluster, creating potential ingress traffic bottlenecks and reduced internal load balancing capability, or even complete loss of ingress traffic to the cluster if the subset of nodes with ingress gateway pods go down. See Source IP for Services with Type=NodePort for more information.
Update the ingress gateway to set externalTrafficPolicy: Local to preserve the original client source IP on the ingress gateway using the following command:
$ kubectl patch svc istio-ingressgateway -n istio-system -p '{"spec":{"externalTrafficPolicy":"Local"}}'
$ kubectl patch svc httpbin-gateway-istio -n foo -p '{"spec":{"externalTrafficPolicy":"Local"}}'
HTTP/HTTPS Load Balancer
If you are using an HTTP/HTTPS external load balancer (AWS ALB, GCP ), it can put the original client IP address in the X-Forwarded-For header. Istio can extract the client IP address from this header with some configuration. See Configuring Gateway Network Topology. Quick example if using a single load balancer in front of Kubernetes:
apiVersion: install.istio.io/v1alpha1kind: IstioOperatorspec:meshConfig:accessLogEncoding: JSONaccessLogFile: /dev/stdoutdefaultConfig:gatewayTopology:numTrustedProxies: 1
IP-based allow list and deny list
When to use ipBlocks vs. remoteIpBlocks: If you are using the X-Forwarded-For HTTP header or the Proxy Protocol to determine the original client IP address, then you should use remoteIpBlocks in your AuthorizationPolicy. If you are using externalTrafficPolicy: Local, then you should use ipBlocks in your AuthorizationPolicy.
| Load Balancer Type | Source of Client IP | ipBlocks vs. remoteIpBlocks |
|---|---|---|
| TCP Proxy | Proxy Protocol | remoteIpBlocks |
| Network | packet source address | ipBlocks |
| HTTP/HTTPS | X-Forwarded-For | remoteIpBlocks |
- The following command creates the authorization policy,
ingress-policy, for the Istio ingress gateway. The following policy sets theactionfield toALLOWto allow the IP addresses specified in theipBlocksto access the ingress gateway. IP addresses not in the list will be denied. TheipBlockssupports both single IP address and CIDR notation.
ipBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: istio-systemspec:selector:matchLabels:app: istio-ingressgatewayaction: ALLOWrules:- from:- source:ipBlocks: ["1.2.3.4", "5.6.7.0/24"]EOF
remoteIpBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: istio-systemspec:selector:matchLabels:app: istio-ingressgatewayaction: ALLOWrules:- from:- source:remoteIpBlocks: ["1.2.3.4", "5.6.7.0/24"]EOF
ipBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: foospec:selector:matchLabels:istio.io/gateway-name: httpbin-gatewayaction: ALLOWrules:- from:- source:ipBlocks: ["1.2.3.4", "5.6.7.0/24"]EOF
remoteIpBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: foospec:selector:matchLabels:istio.io/gateway-name: httpbin-gatewayaction: ALLOWrules:- from:- source:remoteIpBlocks: ["1.2.3.4", "5.6.7.0/24"]EOF
Verify that a request to the ingress gateway is denied:
$ curl "$INGRESS_HOST:$INGRESS_PORT"/headers -s -o /dev/null -w "%{http_code}\n"403
Assign your original client IP address to an env variable. If you don’t know it, you can an find it in the Envoy logs using the following command:
ipBlocks:
$ CLIENT_IP=$(kubectl get pods -n istio-system -o name -l istio=ingressgateway | sed 's|pod/||' | while read -r pod; do kubectl logs "$pod" -n istio-system | grep remoteIP; done | tail -1 | awk -F, '{print $3}' | awk -F: '{print $2}' | sed 's/ //') && echo "$CLIENT_IP"192.168.10.15
remoteIpBlocks:
$ CLIENT_IP=$(kubectl get pods -n istio-system -o name -l istio=ingressgateway | sed 's|pod/||' | while read -r pod; do kubectl logs "$pod" -n istio-system | grep remoteIP; done | tail -1 | awk -F, '{print $4}' | awk -F: '{print $2}' | sed 's/ //') && echo "$CLIENT_IP"192.168.10.15
ipBlocks:
$ CLIENT_IP=$(kubectl get pods -n foo -o name -l istio.io/gateway-name=httpbin-gateway | sed 's|pod/||' | while read -r pod; do kubectl logs "$pod" -n foo | grep remoteIP; done | tail -1 | awk -F, '{print $3}' | awk -F: '{print $2}' | sed 's/ //') && echo "$CLIENT_IP"192.168.10.15
remoteIpBlocks:
$ CLIENT_IP=$(kubectl get pods -n foo -o name -l istio.io/gateway-name=httpbin-gateway | sed 's|pod/||' | while read -r pod; do kubectl logs "$pod" -n foo | grep remoteIP; done | tail -1 | awk -F, '{print $4}' | awk -F: '{print $2}' | sed 's/ //') && echo "$CLIENT_IP"192.168.10.15
- Update the
ingress-policyto include your client IP address:
ipBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: istio-systemspec:selector:matchLabels:app: istio-ingressgatewayaction: ALLOWrules:- from:- source:ipBlocks: ["1.2.3.4", "5.6.7.0/24", "$CLIENT_IP"]EOF
remoteIpBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: istio-systemspec:selector:matchLabels:app: istio-ingressgatewayaction: ALLOWrules:- from:- source:remoteIpBlocks: ["1.2.3.4", "5.6.7.0/24", "$CLIENT_IP"]EOF
ipBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: foospec:selector:matchLabels:istio.io/gateway-name: httpbin-gatewayaction: ALLOWrules:- from:- source:ipBlocks: ["1.2.3.4", "5.6.7.0/24", "$CLIENT_IP"]EOF
remoteIpBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: foospec:selector:matchLabels:istio.io/gateway-name: httpbin-gatewayaction: ALLOWrules:- from:- source:remoteIpBlocks: ["1.2.3.4", "5.6.7.0/24", "$CLIENT_IP"]EOF
Verify that a request to the ingress gateway is allowed:
$ curl "$INGRESS_HOST:$INGRESS_PORT"/headers -s -o /dev/null -w "%{http_code}\n"200
Update the
ingress-policyauthorization policy to set theactionkey toDENYso that the IP addresses specified in theipBlocksare not allowed to access the ingress gateway:
ipBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: istio-systemspec:selector:matchLabels:app: istio-ingressgatewayaction: DENYrules:- from:- source:ipBlocks: ["$CLIENT_IP"]EOF
remoteIpBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: istio-systemspec:selector:matchLabels:app: istio-ingressgatewayaction: DENYrules:- from:- source:remoteIpBlocks: ["$CLIENT_IP"]EOF
ipBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: foospec:selector:matchLabels:istio.io/gateway-name: httpbin-gatewayaction: DENYrules:- from:- source:ipBlocks: ["$CLIENT_IP"]EOF
remoteIpBlocks:
$ kubectl apply -f - <<EOFapiVersion: security.istio.io/v1kind: AuthorizationPolicymetadata:name: ingress-policynamespace: foospec:selector:matchLabels:istio.io/gateway-name: httpbin-gatewayaction: DENYrules:- from:- source:remoteIpBlocks: ["$CLIENT_IP"]EOF
Verify that a request to the ingress gateway is denied:
$ curl "$INGRESS_HOST:$INGRESS_PORT"/headers -s -o /dev/null -w "%{http_code}\n"403
You could use an online proxy service to access the ingress gateway using a different client IP to verify the request is allowed.
If you are not getting the responses you expect, view the ingress gateway logs which should show RBAC debugging information:
$ kubectl get pods -n istio-system -o name -l istio=ingressgateway | sed 's|pod/||' | while read -r pod; do kubectl logs "$pod" -n istio-system; done
$ kubectl get pods -n foo -o name -l istio.io/gateway-name=httpbin-gateway | sed 's|pod/||' | while read -r pod; do kubectl logs "$pod" -n foo; done
Clean up
- Remove the authorization policy:
$ kubectl delete authorizationpolicy ingress-policy -n istio-system
$ kubectl delete authorizationpolicy ingress-policy -n foo
Remove the namespace
foo:$ kubectl delete namespace foo