Routing egress traffic to wildcard destinations
A generic approach to set up egress gateways that can route traffic to a restricted set of target remote hosts dynamically, including wildcard domains.
If you are using Istio to handle application-originated traffic to destinations outside of the mesh, you’re probably familiar with the concept of egress gateways. Egress gateways can be used to monitor and forward traffic from mesh-internal applications to locations outside of the mesh. This is a useful feature if your system is operating in a restricted environment and you want to control what can be reached on the public internet from your mesh.
The use-case of configuring an egress gateway to handle arbitrary wildcard domains had been included in the official Istio docs up until version 1.13, but was subsequently removed because the documented solution was not officially supported or recommended and was subject to breakage in future versions of Istio. Nevertheless, the old solution was still usable with Istio versions before 1.20. Istio 1.20, however, dropped some Envoy functionality that was required for the approach to work.
This post attempts to describe how we resolved the issue and filled the gap with a similar approach using Istio version-independent components and Envoy features, but without the need for a separate Nginx SNI proxy. Our approach allows users of the old solution to seamlessly migrate configurations before their systems face the breaking changes in Istio 1.20.
Problem to solve
The currently documented egress gateway use-cases rely on the fact that the target of the traffic
(the hostname) is statically configured in a VirtualService
, telling Envoy in the egress gateway pod where to TCP proxy
the matching outbound connections. You can use multiple, and even wildcard, DNS names to match the routing criteria, but you
are not able to route the traffic to the exact location specified in the application request. For example you can match traffic for targets
*.wikipedia.org
, but you then need to forward the traffic to a single final target, e.g., en.wikipedia.org
. If there is another
service, e.g., anyservice.wikipedia.org
, that is not hosted by the same server(s) as en.wikipedia.org
, the traffic to that host will fail. This is because, even though the target hostname in the
TLS handshake of the HTTP payload contains anyservice.wikipedia.org
, the en.wikipedia.org
servers will not be able to serve the request.
The solution to this problem at a high level is to inspect the original server name (SNI extension) in the application TLS handshake (which is sent in plain-text, so no TLS termination or other man-in-the-middle operation is needed) in every new gateway connection and use it as the target to dynamically TCP proxy the traffic leaving the gateway.
When restricting egress traffic via egress gateways, we need to lock down the egress gateways so that they can only be used
by clients within the mesh. This is achieved by enforcing ISTIO_MUTUAL
(mTLS peer authentication) between the application
sidecar and the gateway. That means that there will be two layers of TLS on the application L7 payload. One that is the application
originated end-to-end TLS session terminated by the final remote target, and another one that is the Istio mTLS session.
Another thing to keep in mind is that in order to mitigate any potential application pod corruption, the application sidecar and the gateway should both perform hostname list checks. This way, any compromised application pod will still only be able to access the allowed targets and nothing more.
Low-level Envoy programming to the rescue
Recent Envoy releases include a dynamic TCP forward proxy solution that uses the SNI header on a per-
connection basis to determine the target of an application request. While an Istio VirtualService
cannot configure a target like this, we are able to use
EnvoyFilter
s to alter the Istio generated routing instructions so that the SNI header is used to determine the target.
To make it all work, we start by configuring a custom egress gateway to listen for the outbound traffic. Using
a DestinationRule
and a VirtualService
we instruct the application sidecars to route the traffic (for a selected
list of hostnames) to that gateway, using Istio mTLS. On the gateway pod side we build the SNI forwarder with the
EnvoyFilter
s, mentioned above, introducing internal Envoy listeners and clusters to make it all work. Finally, we patch the
internal destination of the gateway-implemented TCP proxy to the internal SNI forwarder.
The end-to-end request flow is shown in the following diagram:
This diagram shows an egress HTTPS request to en.wikipedia.org
using SNI as a routing key.
Application container
Application originates HTTP/TLS connection towards the final destination. Puts destination’s hostname into the SNI header. This TLS session is not decrypted inside the mesh. Only SNI header is inspected (as it is in cleartext).
Sidecar proxy
Sidecar intercepts traffic to matching hostnames in the SNI header from the application originated TLS sessions. Based on the VirtualService, the traffic is routed to the egress gateway while wrapping original traffic into Istio mTLS as well. Outer TLS session has the gateway Service address in the SNI header.
Mesh listener
A dedicated listener is created in the Gateway that mutually authenticates the Istio mTLS traffic. After the outer Istio mTLS termination, it unconditionally sends the inner TLS traffic with a TCP proxy to the other (internal) listener in the same Gateway.
SNI forwarder
Another listener with SNI forwarder performs a new TLS header inspection for the original TLS session. If the inner SNI hostname matches the allowed domain names (including wildcards), it TCP proxies the traffic to the destination, read from the header per connection. This listener is internal to Envoy (allowing it to restart traffic processing to see the inner SNI value), so that no pods (inside or outside the mesh) can connect to it directly. This listener is 100% manually configured through EnvoyFilter.
Deploy the sample
In order to deploy the sample configuration, start by creating the istio-egress
namespace and then use the following YAML to deploy an egress gateway, along with some RBAC
and its Service
. We use the gateway injection method to create the gateway in this example. Depending on your install method, you may want to
deploy it differently (for example, using an IstioOperator
CR or using Helm).
Verify the gateway pod is up and running in the istio-egress
namespace and then apply the following YAML to configure the gateway routing:
Check the istiod
and gateway logs for any errors or warnings. If all went well, your mesh sidecars are now routing
*.wikipedia.org
requests to your gateway pod while the gateway pod is then forwarding them to the exact remote host specified in the application
request.
Try it out
Following other Istio egress examples, we will use the sleep pod as a test source for sending requests. Assuming automatic sidecar injection is enabled in your default namespace, deploy the test app using the following command:
Get your sleep and gateway pods:
Run the following command to confirm that you are able to connect to the wikipedia.org
site:
We could reach both English and German wikipedia.org
subdomains, great!
Normally, in a production environment, we would block external requests that are not configured to redirect through the egress gateway, but since we didn’t do that in our test environment, let’s access another external site for comparison:
Since we have access logging turned on globally (with the Telemetry
CR in the manifest), we can now inspect the logs to see how the above requests were handled by the proxies.
First, check the gateway logs:
There are four log entries, representing two of our three curl requests. Each pair shows how a single request flows through the envoy traffic processing pipeline.
They are printed in reverse order, but we can see the 2nd and the 4th line show that the requests arrived at the gateway service and were passed through the internal sni_cluster
target.
The 1st and 3rd line show that the final target is determined from the inner SNI header, i.e., the target host set by the application.
The request is forwarded to dynamic_forward_proxy_cluster
which finally sends on the request from Envoy to the remote target.
Great, but where is the third request to IBM Cloud? Let’s check the sidecar logs:
As you can see, Wikipedia requests were sent through the gateway while the request to IBM Cloud went straight out from the application pod to the internet, as indicated by the PassthroughCluster
log.
Conclusion
We implemented controlled routing for egress HTTPS/TLS traffic using egress gateways, supporting arbitrary and wildcard domain names. In a production environment, the example shown in this post
would be extended to support HA requirements (e.g., adding zone aware gateway Deployment
s, etc.) and to restrict the direct external
network access of your application so that the application can only access the public network through the gateway, which is limited to a predefined set of remote hostnames.
The solution scales easily. You can include multiple domain names in the configuration, and they will be allow-listed as soon as you roll it out!
No need to configure per domain VirtualService
s or other routing details. Be careful, however, as the domain names are listed in multiple places in the config. If you use
tooling for CI/CD (e.g., Kustomize), it’s best to extract the domain name list into a single place from which you can render into the required configuration resources.
That’s all! I hope this was helpful. If you’re an existing user of the previous Nginx-based solution, you can now migrate to this approach before upgrading to Istio 1.20, which will otherwise disrupt your current setup.
Happy SNI routing!