Downstream and Upstream Mutual TLS With an API Gateway
Sven Walther
Like many developers and operations professionals, you may have had complicated experiences with security and certificates (encryption of the connection and authentication). Maybe so much so that you try to avoid working on them whenever possible.
If you're looking for a simpler way, Kong may be the answer. As with everything with Kong, the idea is to make things as lightweight as possible, including the complexity of setting up and maintaining certificate-based security.
One of our global customers in the banking industry recently had a mature certificate infrastructure that needed adoption from internal and governmental regulations. Based on their previous experience with legacy gateways, they were afraid of the complexity of integrating the gateway into their certificate management system and making sure updates are propagated automatically to the gateway cluster. In their previous solution, this was a manual task. With the below Kong mTLS use cases, this was quick and easy.
When thinking about certificates in an API gateway context, you might immediately think about these two use cases:
The gateway should present a certificate to the client.
The gateway should authenticate a user/grant access based on the client's mutual TLS (mTLS).
But there are two more use cases you may want to consider:
Kong presents the certificate to the backend (upstream).
Kong sends the trust to the upstream (which certificate/s we allow the backend to present).
#1: Certificate Presented by Kong to the Client
Let's start with the certificate Kong serves to the client (the one you would see when opening the proxy in the browser).
The Default Certificate
When someone accesses a route exposed using Kong Gateway, the presented certificate by Kong should match the entered hostname (for example, proxy.example.com) so the client can trust that Kong established the connection.
The default certificate presented by Kong after installation is a self-signed certificate. If you want to change this default, copy your desired certificate and key on the machine and then set ssl_cert and ssl_cert_key in your /etc/kong/kong.conf.
But what if Kong is listening on multiple different hostnames/FQDN? In this case, we need to present different certificates depending on the route. Examples might be different brandings (like api.myshoesbrand.com and api.myshirtsbrand.com) or also very common internal vs. external phasing routes (like api.interal.lan and api.mybrand.com).
When looking at the route object, we notice the hosts parameter that we can use to limit the hosts being listened on and the snis parameter (SNI stands for "server name indication“).
Example on how to create a route for a specific hostname:
But how do we define an SNI and which certificates it shall use? Well, you might have guessed it already. There is an API for that: SNI API endpoint.
Now that we attached an SNI to a route, how do we add the certificate to the SNI? There's an API to upload a certificate.
The actual order is the exact opposite of how we just went through it:
Upload the certificate (I do expect you have created it already and have put the files into the current folder) http -f admin.kong.internal.lan/certificates cert=@myCert.crt key=@myCert.key tags=myCert
Create an SNI and link to the uploaded certificate (the above command created the id) http -f admin.kong.internal.lan/snis name=api.interal.lan certificate.id=the-certificate-id-you-got-from-above-upload
Create a route linking to the SNI (the above command created the id) http -f admin.kong.internal.lan/services/myservice/routes name=myInternalRoute hosts=api.internal.lan snis=the-sni-id-you-got-from-above-upload
Let's Encrypt
If you want to automate the whole process using Let's Encrypt, look at the ACME plugin that integrates Kong with Let's Encrypt to create and auto-update certificates. We won't cover the details of this, as the Let's Encrypt setup is out of scope for this blog post.
Note: While all the other documentation here is true for both Kong and Kong Enterprise, mTLS is an enterprise-only plugin.
Authenticating consumers based on certificates is a prevalent mechanism, especially for (but not limited to) financial users of Kong. Each client gets its own certificate to present on every API call to prove its identity.
Authentication
The mTLS plugin has one parameter called ca_certificates. As the name already tells us, we need to specify one or multiple CAs, which we'll use as the trusted source. Only incoming certificates that use those CAs will be trusted.
Now that we authenticated the incoming certificate, how do we make sure not every certificate issued by the CA is allowed to make the call?
For this, the mTLS plugin provides two parameters:
By default, the parameter skip_consumer_lookup is set to false, so there must be a matching consumer in Kong. If no consumer is found, the call gets denied. We can do all the typical consumer-based steps if we find a consumer, even adding the consumer to one or multiple groups using the ACL plugin.
Depending on the contents of your certificate, it already might contain group membership information (see parameter authenticated_group_by). If so, we can directly use those extracted groups with the ACL plugin even without consumer matching in place.
#3: Certificate Presented by Kong to the Upstream
The trust between the gateway and the backend system can be secured using certificates. This time, Kong presents a certificate to the upstream to prove its identity. This use case is about the backend validating that an accepted incoming call came from Kong Gateway and not another intermediary.
The Default Certificate
Kong presents a self-signed certificate (the default) after installation to the upstream. If you want to change this default:
Copy your desired certificate and key to the machine.
Upload the certificate (I do expect you have created it already and have put the files into the current folder) http -f admin.kong.internal.lan/certificates cert=@myCert.crt key=@myCert.key tags=myCert
Attach the certificate to all calls for a specific service (the id got returned from the previous call) http -f admin.kong.internal.lan/services/my_sensitive_backend url=https://sensitive.internal.lan client_certificate.id=the_id_from_previous_upload
#4: Trusting the Upstream's Certificate
We can limit the trusted certificates Kong expects from the upstreams by changing the lua_ssl_trusted_certificate. This is used when Kong Gateway is sure that the connected backend is connected and there's no service in the middle. We're achieving two-way mutual trust in combination with the above #3.
Certificates can play a vital role in the trusted connections to your clients and your backend systems. Using them, you can achieve both the encryption of the traffic on the transport level and create (mutual) trust between the connected systems.
Certificate configurations can often be very complex. Handling all of this within Kong keeps it simple. It can be integrated into existing certificate management systems to automate all the configurations.
Try out these examples yourself by getting a Kong installation. If you have any questions, do not hesitate to contact us.
"The whole is more than the sum of its parts." Aristotle is credited with this quote, and it's true in the world of data. Legacy systems typically approached their role in a limited manner. Each system was intended to be used by a certain user set
Ahmed Koshok
Configuring Kong Dedicated Cloud Gateways with Managed Redis in a Multi-Cloud Environment
Architecture Overview
A multicloud DCGW architecture typically contains three main layers.
1\. Konnect Control Plane
The SaaS control plane manages configuration, plugins, and policies. All gateways connect securely to this layer.
2\. Dedicated C
Hugo Guerrero
Building Secure AI Agents with Kong's MCP Proxy and Volcano SDK
The example below shows how an AI agent can be built using Volcano SDK with minimal code, while still interacting with backend services in a controlled way. The agent is created by first configuring an LLM, then defining an MCP (Model Context Prot
Eugene Tan
Kong Event Gateway: Unifying APIs and Events in a Single API Platform
Kong customers include some of the most forward-thinking, tech-savvy organizations in the world. And while we’re proud to help them innovate through traditional APIs, the reality is that their ambitions don’t stop there. Increasingly, our customers a
Umair Waheed
Kong Gateway 3.9: Extended AI Support and Enhanced Security
Today we're excited to announce Kong Gateway 3.9! Since unveiling Kong Gateway 3.8 at API Summit 2024 just a few months ago, we’ve been busy making important updates and improvements to Kong Gateway. This release introduces new functionality arou
Alex Drag
Your Secrets and Tokens are Secure with Kong Gateway Enterprise 3.5
Kong Gateway Enterprise 3.5 is packed with security features to support the use cases demanded by our enterprise customers through major improvements in Secrets Management integrations and our Open-ID Connect (OIDC) plugin. Additionally, we’ve a
Tom Brightbill
Kong Releases Product Updates to Address Novel HTTP/2 'Rapid Reset' DDoS Vulnerability
At Kong, the security and reliability of our products have always been paramount. In light of the recent discovery of the Novel HTTP/2 ‘Rapid Reset’ DDoS attack ( CVE-2023-44487 ), we have taken steps to proactively address potential issues. Today w
Tom Brightbill
Ready to see Kong in action?
Get a personalized walkthrough of Kong's platform tailored to your architecture, use cases, and scale requirements.