Oauth2 Proxy is a reverse proxy and static file server that provides authentication using Providers (Google, GitHub, and others) to validate accounts by email, domain or group.
In this post, we will learn how to set up one central OAuth2 Proxy as authentication proxy for multiple services inside your Kubernetes Cluster. We will use Gitlab OpenID Connect application as our identity provider.
This is how oauth2-proxy works:
- Any request passing through the proxy (and not matched by
--skip-auth-regex
) is checked for the proxy’s session cookie (--cookie-name
) (or, if allowed, a JWT token - see--skip-jwt-bearer-tokens
). - If authentication is required but missing then the user is asked to log in and redirected to the authentication provider (unless it is an Ajax request, i.e. one with
Accept: application/json
, in which case401 Unauthorized
is returned) - After returning from the authentication provider, the oauth tokens are stored in the configured session store (cookie, redis, …) and a cookie is set
- The request is forwarded to the upstream server with added user info and authentication headers (depending on the configuration)
Kubernetes Ingress external authentication is a mechanism that enables authentication for incoming requests to services deployed within a Kubernetes cluster through an Ingress controller. It allows you to enforce authentication before granting access to your applications, providing an additional layer of security and control.
Benefits of using Kubernetes Ingress external authentication:
- Centralized Authentication: With external authentication, you can centralize the authentication logic for multiple services deployed within your Kubernetes cluster. This eliminates the need for individual authentication mechanisms for each service.
- Flexible Authentication Providers: Kubernetes Ingress supports various authentication providers, including OAuth2 providers like Google, GitHub, or OpenID Connect providers. This flexibility allows you to leverage existing authentication systems or choose the one that best suits your needs.
- Simplified User Experience: External authentication allows users to authenticate using their existing credentials from well-known providers. This simplifies the user experience, as users don’t need to create new accounts or remember additional usernames and passwords.
- Centralized Management and Configuration: Ingress controllers provide a centralized point for managing and configuring external authentication rules for all the services within your cluster. This simplifies the management and ensures consistency across your applications.
- Integration with Existing Identity Providers: If you already have an identity provider or authentication system in place, Kubernetes Ingress external authentication allows you to integrate seamlessly with it, leveraging your existing user management infrastructure.
Configure a GitLab OpenID Application as identity provider
Before doing anything else, you need to create a GitLab OpenID Connect application.
GitLab includes support for allowing other applications to authenticate users via OpenID connect
You can create a new application by opening the Applications dashboard in the Admin Area or the Profile preferences section.. From there, click on the New Application button, You will be prompted to provide an application name
, a redirect URI
, and to set the applications permissions
. As you do, keep in mind:
name
is an application label for your referenceredirect_uri
is the endpoint to which GitLab should send users after they have authenticated, and should be of the formhttps://<my-cloud-application-url>/oauth2/callback
Scopes
define what level of access the application will have to the GitLab user profile. For most applications, you will want to check openid, profile, and email.
Once done, please take note of the Application ID
and Secret
they are important for future steps:
Generate a Cookie Secret
Next, we need to generate a Cookie Secret for NGINX. The cookie secret is used by NGINX as a seed string to generate secure cookies, which are used by NGINX to identify users who are interacting with the gateway.
The code in the listing below can be used to generate a Base64 encoded string that will serve as the --cookie-secret
option for the proxy.
|
|
Installing oauth2-proxy
We will use the official helm chart found here https://github.com/oauth2-proxy/manifests
to install the proxy. We will need to do some customization to the helm chart, so let’s create a values file with the following content:
Open values file with your prefered text editor, I am using vim
.
|
|
Then add the following content:
|
|
In the above yaml, we are defining an ingress that will dictate how we access the oauth2-proxy. In my case I am defining a domain oauth.citizix.com
and I have nginx ingress set up and a cert manager with the issuer letsencrypt-prod-issuer
for tls.
As the OAuth2 Proxy documentation explains how to set up the different authentication providers, I will focus on the Ingress setup here.
To install, use this helm command:
First add the oauth2-proxy repository:
|
|
To know which version of oauth2-proxy are present, you can search:
|
|
In my case, the latest version is 6.16.1
, let us install this version:
|
|
Once the install is successful, you should confirm using this command:
|
|
Also confirm that the pod is running
|
|
If you are a fan of IaC and you can use helmfile to define the installation. Create a file called helmfile.yaml
with the following content:
|
|
Then you can apply using this command:
|
|
The above values result in this Ingress object:
|
|
This Ingress will handle all authentication request as we will see in the next Ingress definition.
For services that you want to secure, add the below annotations to the Ingress:
|
|
Try to enter alertmanager.citizix.com in browser. You should see Gitlab auth page.
Please note the annotations:
|
|
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.
Conclusion
In this guide, we learnt how to secure multiple services with just one central OAuth2 Proxy. I hope this helps you to reduce the complexity of how you handle authentication within your system.
Oauth2 Proxy is useful when you want:
- One or more of your applications to be accessible only by authenticated users, for instance, users using a specific domain, emails whitelisting, and more
- To rely on a third-party provider to handle the authentication process (Google, GitHub, etc.)
- To keep a clear separation between the authentication service and the rest of your applications