To enforce IP-based access control on an ingress gateway, you need to obtain the originating IP address of a client. For example, you can create an authorization policy to deny or allow requests to the ingress gateway by configuring an IP address blacklist or whitelist. This topic describes how to obtain the originating IP address of a client from the HTTP request header.
Prerequisites
- The cluster is added to the ASM instance.
- Sidecar injection is enabled for the specified namespace. For more information, see Enable automatic sidecar injection by using multiple methods.
Background information
In most cases, requests that contain the attributes of the client are forwarded to applications by using reverse proxies. For example, a request contains the X-Forward-For header. Istio allows you to deploy a variety of network topologies. To access the IP address of the ingress gateway, you can use the public IP address of a Server Load Balancer (SLB) instance, add the IP address of the ingress gateway to Web Application Firewall (WAF), or use an unspecified network topology. In this case, a fixed default value cannot be used to identify the originating IP address in the X-Forwarded-For header if the requests that contain the client attributes are forwarded to a specified workload. Therefore, you cannot obtain the originating IP address of the client based on the value of the X-Forwarded-For request header.
To resolve this issue, you can set the numTrustedProxies parameter to the number of trusted proxies that are deployed prior to the ingress gateway. The ingress gateway obtains the originating IP address of the client based on the value of the numTrustedProxies parameter and sets the value of the X-Envoy-External-Address header. In this case, upstream services can access the originating IP address of the client based on the value of the X-Envoy-External-Address header.
Procedure
- Deploy a sample application.
- Create an ingress gateway.
- Create an Istio gateway and a virtual service.
- Obtain the IP address of the ingress gateway whose port number is 80. For more information, see Create an ingress gateway service.
- Add the IP address of the ingress gateway that you obtain in the preceding substep to WAF.4 For more information, see Tutorial.
- Add the numTrustedProxies parameter to the ingress gateway.
- Run the following command to access the httpbin application to obtain the originating IP address of the client:
curl http://{IP address of the ingress gateway}/get?show_env=true
Expected output:
{ "args": { "show_env": "true" }, "headers": { "Accept": "*/*", .... "X-Envoy-Attempt-Count": "1", "X-Envoy-External-Address": "106.11.**.**", .... }, .... }
The value of the X-Envoy-External-Address parameter is the originating IP address of the client.