This blog post will show you how to use one central OAuth2 Proxy (see the official page) as authentication proxy for multiple services inside your Kubernetes Cluster.
The default example on how to secure a service with Nginx and OAuth2 Proxy shows you how to secure one service. To achieve this, it uses two Ingress objects for the service to be secured. If you plan to secure multiple services with the same OAuth provider, you end up with a lot of Ingress objects. Another problem of this setup is that it is not supported by most Helm charts. Most Helm charts only allow you to create one Ingress object. You would have to set up the service via its Helm chart and then add somehow the additional Ingress object needed by OAuth2 Proxy.
One central authentication service for multiple services
This post will show you how you can achieve the same with one central OAuth2 Proxy Ingress. I used the official Helm chart for OAuth2 Proxy (see https://github.com/oauth2-proxy/manifests) to install the proxy. The Helm chart allows you to define an Ingress:
- secretName: tls-cert
As the OAuth2 Proxy documentation explains how to set up the different authentication providers, I will focus on the Ingress setup here.
The above values result in this Ingress object:
- host: oauth.example.com
This Ingress will handle all authentication request as we will see in the next Ingress definition. For the service you want to secure, add the below annotations to the Ingress:
- host: alertmanager.example.com
The `auth-sigin` redirects any needed login to the OAuth2 Proxy Ingress.
The `auth-url` annotation can access the OAuth2 Proxy internally via its service to verify a submitted token.
The OAuth2 Proxy will handle the authentication and later redirect you to the protected service again.
An additional advantage of this setup is, that you only need to specify one valid redirect URL in your OIDC client. OAuth2 Proxy will handle the service specific redirects.
This post showed you how to secure multiple services with just one central OAuth2 Proxy. I hope this helps you to reduce the complexity of your cluster and also limit the number of resources consumed.