Securing Envoy

Envoy provides a number of features to secure traffic in and out of your network, and between proxies and services within your network.

Transport Layer Security (TLS) can be used to secure all types of HTTP traffic, including WebSockets.

Envoy also has support for transmitting and receiving generic TCP traffic with TLS.

Envoy also offers a number of other HTTP-based protocols for authentication and authorization such as JWT, RBAC and OAuth.

Warning

The following guide takes you through individual aspects of securing traffic.

To secure traffic over a network that is untrusted, you are strongly advised to make use of encryption and mutual authentication wherever you control both sides of the connection or where relevant protocols are available.

Here we provide a guide to using mTLS which provides both encryption and mutual authentication.

When using TLS, you are strongly encouraged to validate all certificates wherever possible.

It is your responsibility to ensure the integrity of your certificate chain, and outside the scope of this guide.

Upstream and downstream TLS contexts

Machines connecting to Envoy to proxy traffic are “downstream” in relation to Envoy.

Specifying a TLS context that clients can connect to is achieved by setting the DownstreamTLSContext in the transport_socket of a listener.

You will also need to provide valid certificates.

envoy-demo-tls.yaml

  1. static_resources:
  2. listeners:
  3. - name: listener_0
  4. address:
  5. socket_address:
  6. address: 0.0.0.0
  7. port_value: 10000
  8. filter_chains:
  9. - filters:
  10. - name: envoy.filters.network.http_connection_manager
  11. typed_config:
  12. "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
  13. stat_prefix: ingress_http
  14. http_filters:
  15. - name: envoy.filters.http.router
  16. route_config:
  17. name: local_route
  18. virtual_hosts:
  19. - name: local_service
  20. domains: ["*"]
  21. routes:
  22. - match:
  23. prefix: "/"
  24. route:
  25. host_rewrite_literal: www.envoyproxy.io
  26. cluster: service_envoyproxy_io
  27. transport_socket:
  28. name: envoy.transport_sockets.tls
  29. typed_config:
  30. "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
  31. common_tls_context:
  32. tls_certificates:
  33. - certificate_chain:
  34. filename: certs/servercert.pem
  35. private_key:
  36. filename: certs/serverkey.pem

Connecting to an “upstream” TLS service is conversely done by adding an UpstreamTLSContext to the transport_socket of a cluster.

envoy-demo-tls.yaml

  1. clusters:
  2. - name: service_envoyproxy_io
  3. type: LOGICAL_DNS
  4. # Comment out the following line to test on v6 networks
  5. dns_lookup_family: V4_ONLY
  6. load_assignment:
  7. cluster_name: service_envoyproxy_io
  8. endpoints:
  9. - lb_endpoints:
  10. - endpoint:
  11. address:
  12. socket_address:
  13. address: www.envoyproxy.io
  14. port_value: 443
  15. transport_socket:
  16. name: envoy.transport_sockets.tls
  17. typed_config:
  18. "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext

Validate an endpoint’s certificates when connecting

When Envoy connects to an upstream TLS service, it does not, by default, validate the certificates that it is presented with.

You can use the validation_context to specify how Envoy should validate these certificates.

Firstly, you can ensure that the certificates are from a mutually trusted certificate authority:

envoy-demo-tls-validation.yaml

  1. port_value: 443
  2. transport_socket:
  3. name: envoy.transport_sockets.tls
  4. typed_config:
  5. "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
  6. common_tls_context:
  7. validation_context:
  8. trusted_ca:
  9. filename: certs/cacert.pem
  10. match_typed_subject_alt_names:
  11. - san_type: DNS

You can also ensure that the “Subject Alternative Names” for the cerficate match.

This is commonly used by web certificates (X.509) to identify the domain or domains that a certificate is valid for.

envoy-demo-tls-validation.yaml

  1. port_value: 443
  2. transport_socket:
  3. name: envoy.transport_sockets.tls
  4. typed_config:
  5. "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
  6. common_tls_context:
  7. validation_context:
  8. trusted_ca:
  9. filename: certs/cacert.pem
  10. match_typed_subject_alt_names:
  11. - san_type: DNS

Note

If the “Subject Alternative Names” for a certificate are for a wildcard domain, eg *.example.com, this is what you should use when matching with match_typed_subject_alt_names.

Note

See here to view all of the possible configurations for certificate validation.

Use mutual TLS (mTLS) to enforce client certificate authentication

With mutual TLS (mTLS), Envoy also provides a way to authenticate connecting clients.

At a minimum you will need to set require_client_certificate and specify a mutually trusted certificate authority:

envoy-demo-tls-client-auth.yaml

  1. cluster: service_envoyproxy_io
  2. transport_socket:
  3. name: envoy.transport_sockets.tls
  4. typed_config:
  5. "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
  6. require_client_certificate: true
  7. common_tls_context:
  8. validation_context:
  9. trusted_ca:
  10. filename: certs/cacert.pem
  11. match_typed_subject_alt_names:
  12. - san_type: DNS
  13. matcher:
  14. exact: proxy-postgres-frontend.example.com
  15. tls_certificates:

You can further restrict the authentication of connecting clients by specifying the allowed “Subject Alternative Names” in match_typed_subject_alt_names, similar to validating upstream certificates described above.

envoy-demo-tls-client-auth.yaml

  1. cluster: service_envoyproxy_io
  2. transport_socket:
  3. name: envoy.transport_sockets.tls
  4. typed_config:
  5. "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
  6. require_client_certificate: true
  7. common_tls_context:
  8. validation_context:
  9. trusted_ca:
  10. filename: certs/cacert.pem
  11. match_typed_subject_alt_names:
  12. - san_type: DNS
  13. matcher:
  14. exact: proxy-postgres-frontend.example.com
  15. tls_certificates:

Note

See here to view all of the possible configurations for certificate validation.

Use mutual TLS (mTLS) to connect with client certificates

When connecting to an upstream with client certificates you can set them as follows:

envoy-demo-tls-client-auth.yaml

  1. clusters:
  2. - name: service_envoyproxy_io
  3. type: LOGICAL_DNS
  4. # Comment out the following line to test on v6 networks
  5. dns_lookup_family: V4_ONLY
  6. load_assignment:
  7. cluster_name: service_envoyproxy_io
  8. endpoints:
  9. - lb_endpoints:
  10. - endpoint:
  11. address:
  12. socket_address:
  13. address: www.envoyproxy.io
  14. port_value: 443
  15. transport_socket:
  16. name: envoy.transport_sockets.tls
  17. typed_config:
  18. "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
  19. common_tls_context:
  20. tls_certificates:
  21. - certificate_chain:
  22. filename: certs/clientcert.pem
  23. private_key:
  24. filename: certs/clientkey.pem

Provide multiple TLS domains at the same IP address with SNI

SNI is an extension to the TLS protocol which allows multiple domains served from the same IP address to be secured with TLS.

To secure specific domains on a listening connection with SNI, you should set the filter_chain_match of the listener:

envoy-demo-tls-sni.yaml

  1. cluster: service_envoyproxy_io
  2. filter_chain_match:
  3. server_names:
  4. - my-service-name.example.com
  5. transport_socket:
  6. name: envoy.transport_sockets.tls
  7. typed_config:
  8. "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
  9. common_tls_context:

See here for more info about creating multiple endpoints with SNI

Connect to an endpoint with SNI

When connecting to a TLS endpoint that uses SNI you should set sni in the configuration of the UpstreamTLSContext.

This will usually be the DNS name of the service you are connecting to.

envoy-demo-tls-sni.yaml

  1. port_value: 443
  2. transport_socket:
  3. name: envoy.transport_sockets.tls
  4. typed_config:
  5. "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
  6. sni: www.envoyproxy.io

When connecting to an Envoy endpoint that is protected by SNI, this must match one of the server_names set in the endpoint’s filter_chain_match, as described above.