Delegating K8S authentication with Microsoft Entra ID (OIDC/OAuth 2.0)
- Delegate authentication to the enterprise directory via OIDC and OAuth 2.0.
- Configuration in Microsoft Entra ID.
- Configuration of the API server and kubectl.
- Demonstration and security focus: conditional access, role review.
Introduction
In a previous article, I tried to describe the authentication logic for a Kubernetes cluster and provide some information about how permission management works.
I started with the default use of a private key and certificate. This is complemented by role-based access control RBAC (Role-based access control), which allows you to connect to a Kubernetes cluster and perform certain actions based on the permissions granted.
This key/certificate mechanism is not always the most practical to use, especially in enterprise environments.
In professional settings, it's common to have an enterprise directory that stores user accounts and groups to provide a centralized authentication service.
You often find Microsoft Active Directory, or its cloud extension Microsoft Entra ID (previously known as Azure Active Directory).
Increasingly, companies are trying to federate their authentication with this type of product. This allows for centralized access management, the imposition of specific authentication conditions, and easier tracking of associated activity.
In this article, I have chosen to present the configuration of Kubernetes in order to use Microsoft Entra ID as an authentication source.
I could have chosen other solutions, but Microsoft Entra ID is still widely used, and more importantly, the integration mechanism with Kubernetes can be easily adapted to other products.
The goal is to use the standard OIDC (OpenID Connect), which itself uses the OAuth 2.0 protocol to allow Kubernetes to communicate with Microsoft Entra ID.
OIDC/OAuth 2.0 are not specific to Microsoft Entra ID. It is also possible to use other Identity and Access Management (IAM) solutions such as Okta or Google Cloud Identity, based on the same protocols, with a Kubernetes configuration very similar to what we will see now.
Objectives and operating principles
As I usually do before getting into the details, I will summarize the objective of this article in an overall diagram.

Click the image to enlarge.
In the diagram above, I am assuming that the user account exists in an on-premises Microsoft Active Directory environment. This account is replicated in Microsoft Entra ID via a dedicated tool called Microsoft Entra Connect Sync. This is optional; it is perfectly possible to be in an environment where the account only exists in Microsoft Entra ID and is native to Azure. The important thing is that this account exists in Microsoft Entra ID.
The logic is as follows:
- The user (vburgun) executes a command using kubectl
- kubectl will first ask the user to authenticate
- kubectl will forward this authentication request to Microsoft Entra ID, which will then request the user's credentials.
- Microsoft Entra ID will process the request via a declared Enterprise Application (private-app-k8s) and will return an authentication token containing certain information about the user. (Only if the user is authorized at the level of the Enterprise Application).
- kubectl will retrieve this token and submit it to the Kubernetes API.
- The Kubernetes API will also communicate with the Enterprise Application hosted in Microsoft Entra ID. It will ask if the token received by kubectl is valid.
- The Enterprise Application will return confirmation of the token's validity (since it is the one that issued it).
- Now that the Kubernetes API has assurance that the token is valid, it will read the information contained within it to search for the user's login.
- The Kubernetes API will use the login found in the token to determine if it is associated with a role within the cluster and whether the action requested by kubectl is authorized by that role.
- If a matching role is found, and the user has the capabilities to perform the requested action, then the action is executed, and the command's output is returned to kubectl.
- The validity period of the token, as well as the amount of information contained within it, are configured in Microsoft Entra ID. Once the token expires, kubectl requests the user's credentials again to make a new token request and restart the process.
As mentioned previously, for this demonstration to work, a user account must exist in Microsoft Entra ID. This account can be created natively within the cloud directory solution or replicated from an internal directory. In my case, I run a Microsoft Entra Connect Sync service to synchronize my Active Directory with my Microsoft Entra ID tenant for my domain coolcorp.fr. I won't go into detail at this level; feel free to use the mechanism of your choice.

Click the image to enlarge.

Click the image to enlarge.
Configuration in Microsoft Entra ID
The following instructions will involve operations within Microsoft Entra ID. It is preferable to have some knowledge of the product to fully understand the subsequent steps, but I will try to be as explicit as possible so that those using other solutions can find a basis for comparison.
You need to log in to your Microsoft Entra ID tenant with an account that has the necessary permissions to create what Microsoft calls an Enterprise Application. This corresponds to a configuration specific to an external application that will allow it to communicate with Microsoft Entra ID.

Click the image to enlarge.

Click the image to enlarge.
In my example, it will authorize my external application, namely my Kubernetes cluster and my kubectl client, to interact with the cloud directory.
Microsoft has a repository of pre-configured applications to simplify the integration of well-known tools or partner companies. These are listed in a gallery, but we will not use it; instead, we will declare a "custom" application named arbitrarily private-app-k8s.

Click the image to enlarge.

Click the image to enlarge.
Once the application is created, you need to return to the main menu of Microsoft Entra ID and this time select the "App Registrations" option, where we will define the terms of use for the application that we have just declared.

Click the image to enlarge.
By clicking on "All Applications", you can find the applications in question, which you can select to continue the configuration.

Click the image to enlarge.
From here, we choose the "Authentication" option to add a "platform". A platform corresponds to the type of external application that will interact with Microsoft Entra ID via our Enterprise Application private-app-k8s.

Click the image to enlarge.

Click the image to enlarge.
We will choose "Web" because Kubernetes/kubectl will authenticate as if it were a web application.

Click the image to enlarge.
Next, you need to configure the redirection address to which you will be sent once authentication is successful. This corresponds to the external application that made the authentication request.
In our example, kubectl is making the token request, so we must be redirected to it once the authentication phase is complete on the Microsoft Entra ID side.
In this case, kubectl will wait for the response on port 18000, and since kubectl is local to the device used to run the command (e.g., your workstation), we use http://localhost:18000.

Click the image to enlarge.
It is also necessary to add another port, the 8000.

Click the image to enlarge.

Click the image to enlarge.
We can now go to the "API permissions" menu. Indeed, a successful authentication does not mean that you can retrieve information about the user themselves from Microsoft Entra ID.

Click the image to enlarge.
To do this, you need to add a permission on the Microsoft Graph API.

Click the image to enlarge.

Click the image to enlarge.
This is the most used API in Microsoft Entra ID because it allows you to retrieve a wide range of information about the authenticated user.
We request that we obtain delegation for the use of this API.

Click the image to enlarge.
We want to be able to read the user's profile via the two API entry points: User.Read and User.Read.All.

Click the image to enlarge.
It is thanks to this that our authentication token will be able to contain the user's login, their groups, their email address, etc.…
Once the delegation request has been made, it must be authorized by clicking the "Grant Admin consent for Default Directory" button.

Click the image to enlarge.

Click the image to enlarge.
This is an additional security measure. In companies, it is not uncommon for the creators of Enterprise Application to not be fully administrators of the tenant Microsoft Entra ID. In this case, they create their configuration and submit their access request to the API Microsoft Entra ID to the tenant administrators, who, depending on the criticality of the requested information or the associated interactions, may refuse or grant the delegation.
We continue with the "Certificates & Secrets" menu.

Click the image to enlarge.

Click the image to enlarge.
Kubectl will need to communicate with Microsoft Entra ID before the user even authenticates. It is the kubectl application itself that will be making the request, so it needs to prove its legitimacy. This is what the secret (note: this has nothing to do with the Secret object in the K8S API) is for. Of course, its value is confidential and should not be shared.
We assign a validity period to the secret; best practice is to not exceed 6 months.

Click the image to enlarge.

Click the image to enlarge.
Next, we go to the "Token configuration" section. By default, the token issued contains limited information about the user. However, we requested via the Graph API to be able to access the user's complete profile. Therefore, we need to tell Microsoft Entra ID that we want the token to contain additional information about the user, which we can then use at the Kubernetes level.

Click the image to enlarge.
For example, we want to have a list of the groups to which the user belongs, so that at the K8S level, we can avoid creating roles per user, but rather per group of users.

Click the image to enlarge.

Click the image to enlarge.
We also want to add the User Principal Name (UPN), which is a unique ID per user and can be used as a login reference.

Click the image to enlarge.

Click the image to enlarge.

Click the image to enlarge.
Now, we need to go back to the "Enterprise Application" menu and click on our application.

Click the image to enlarge.
We go to "Users and Groups", and then we add our user. Indeed, you may have all your users and groups in Microsoft Entra ID, but that doesn't mean they are authorized at the level of your enterprise application. It is up to you to specify which people or groups can use this enterprise application; in other words, those who will be authorized to delegate their authentication via kubectl to Microsoft Entra ID.

Click the image to enlarge.
For my example, I have a free version of Microsoft Entra ID in which I cannot grant rights to a group. Therefore, I will choose my user directly.

Click the image to enlarge.

Click the image to enlarge.

Click the image to enlarge.
We are now at the end of the configuration on the Microsoft Entra ID side. Before moving on to Kubernetes, we need to retrieve some information:
- The application ID: you can find it in your enterprise application.
- The tenant ID: you can find it in the default console of Microsoft Entra ID
- The secret: this has already been retrieved previously

Click the image to enlarge.

Click the image to enlarge.
These three references will be useful for the rest of this guide.
API Server configuration in K8S
We'll start by configuring Kubernetes, and specifically the API Server, since it is responsible for receiving all external requests to interact with the cluster.
For this example, I am using my vanilla K8S cluster deployed as part of my cookbook with kubeadm.
All the basic components of a Kubernetes cluster are associated with YAML files: manifests.
These manifests are stored in a specific folder, usually /etc/kubernetes/manifests/ on each node of the cluster.
This folder is monitored by the Kubelet agent installed on the servers of the cluster. Any changes to one of its files will be detected by the agent and will cause the associated component to restart. This is how a cluster's "bootstrap" process works.
In our case, we need to modify the kube-apiserver.yaml manifest located on each control plane in /etc/kubernetes/manifests/.
The first thing to do is to create a backup of the original file. Be careful not to store anything in /etc/kubernetes/manifests/. The Kubelet agent monitors everything that is in it, even with an extension other than YAML... Believe me, this can be problematic. Therefore, copy your backup to a separate directory.

Click the image to enlarge.
Then, edit the kube-apiserver.yaml file:
We will add the following lines in the spec section, under the command instructions:
#Azure conf
- --oidc-client-id=id_entreprise_application
- --oidc-issuer-url=https://login.microsoftonline.com/id_tenant_entra/v2.0
- --oidc-username-claim=preferred_username
- --oidc-groups-claim=groups
- --oidc-username-prefix=-
#fin Azure conf

Click the image to enlarge.
- oidc-client-id: corresponds to the ID of the enterprise application (private-app-k8s).
- oidc-issuer-url: corresponds to the URL used to query the external directory. In our case, it is Microsoft Entra ID, and to associate it with the correct tenant, you must include the associated ID in the URL: https://login.microsoftonline.com/id_tenant/v2.0. In this example, we are using the URL that allows us to obtain a JWT (JSON Web Token) in version 2.0. This is only supported by K8S from version 1.30. For those who require a token in v1.0, you must use the URL https://sts.windows.net/tenant_id/
- oidc-username-claim: This corresponds to the field you want to use in the token to identify the user's login. In version 2.0, it is the preferred_username field; in version 1, it is the upn field. Note that you can use other values, such as the email address. It all depends on the configuration you have chosen for your token. In the example, we added the groups and the UPN; it's up to you to decide which elements of the user profile you want to use as the login. Of course, you will need to create your K8S role associations based on the value of this field.
- oidc-groups-claim: This corresponds to the field in the token that contains the user's groups. In the example, it is group (because we explicitly requested it during the token configuration step).
- oidc-username-prefix: This allows you to correctly extract the field that you have specified as being used for the login. In the example, it is necessary to use – so that Kubernetes only uses the value of the field itself and not the entire string including the URL called.
When you save the file, this will cause the API component (and its dependencies) of the cluster to restart for the control plane that you have just modified.

Click the image to enlarge.
Therefore, you need to proceed control plane by control plane, making sure that the components restart (kubectl get pod -n kube-system) each time to avoid any service interruptions.
All of these parameters are not specific to Microsoft Entra ID. These are the options related to the support for OIDC provided by the Kubernetes API server.
If you need to use a different external directory solution, you will need to make the same changes. However, the values of these options may change; it all depends on the configuration of your token, which is delivered in JWT format, and the URLs that need to be called.
Be careful: when updating your cluster as described here, there is a high probability that your OIDC configuration will be lost. You will need to restore it each time you update the API component of the cluster. To do this, feel free to use a tool like Ansible to automatically restore the configuration.
kubectl configuration
You can now move on to kubectl. I recommend reading my previous article on the subject, which gives you the basics of how kubectl works, and specifically, how to use the associated config file.
By default, kubectl does not support OIDC; you need to add a plugin called kubelogin.
You can obtain this from the following URL, and it is in the form of a simple binary. You should choose the format that corresponds to your operating system.

Click the image to enlarge.
Once you have downloaded it, it is important to rename the binary to kubectl-oidc_login and place it in a location specified in your system path. This way, it will be implicitly called by kubectl.

Click the image to enlarge.
Next, you need to modify its configuration file, which is read by kubectl. (More information here).

Click the image to enlarge.
You need to create a new user in the users section. In my example, I call it auth-entra.
- name: auth-entra
user:
exec:
apiVersion: client.authentication.k8s.io/v1beta1
args:
- oidc-login
- get-token
- --oidc-issuer-url=https://login.microsoftonline.com/id_tenant_entra/v2.0
- --oidc-client-id=id_entreprise_application
- --oidc-client-secret=secret_application
- --oidc-extra-scope=profile
command: kubectl
env: null
interactiveMode: IfAvailable
provideClusterInfo: false

Click the image to enlarge.
The parameters you find there are actually those that will be processed by the kubelogin addon that we just downloaded.
- oidc-issuer-url: This must be identical to the URL used for the API server. If you have specified the URL for using a V2 token, you must use exactly the same one; this applies to V1 as well. In both cases, the URL contains the tenant ID of Microsoft Entra ID
- oidc-client-id: Similar to the API Server, this is the ID of the enterprise application.
- oidc-client-secret: We haven't used this yet, but you need to specify the secret that was created for our Enterprise Application.
- oidc-extra-scope=profile: This is required for a V2.0 token to request all information about the user profile in the token.
The other elements are necessary for the proper functioning of the plugin.
Then, we create a new context, associating the auth-entra user with the cluster.
- context:
cluster: kub
user: auth-entra
name: auth-entra@kub
Demonstration
Let's try switching to this new context using the following command:
kubectl config use-context auth-entra@kub

Click the image to enlarge.
When I type a command that interacts with the cluster, such as kubectl get node.
My browser will open, and I will see an authentication prompt where I can enter my login and password.

Click the image to enlarge.
If the information is correct, the authentication is accepted, and I can return to my CLI.

Click the image to enlarge.
However, I receive a negative response. This is normal because, as it stands, I have not assigned any roles to my user.

Click the image to enlarge.
It's important to understand this. When I use OIDC with Kubernetes, I delegate the authentication process to a third-party solution, in this case, Microsoft Entra ID, but the RBAC logic remains, and I retain control of role management at the cluster level.
Therefore, I need to use a ClusterRoleBinding object to associate my user [email protected] with a role on the cluster. For this example, I will use the default role cluster-admin (this is not recommended, but it simplifies the demonstration).
Here is the content of the yaml file clusterrolebinding-vburgun-entra-to-clusteradmin.yml:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: clusterrolebinding-vburgun-entra-to-clusteradmin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: User
name: "[email protected]"
apiGroup: rbac.authorization.k8s.io
I need to switch back to my local authentication by default: kubectl config use-context kubernetes-admin@kub.
I am applying my ClusterRoleBinding object: kubectl apply -f clusterrolebinding-vburgun-entra-to-clusteradmin.yml.

Click the image to enlarge.
I am switching back to my auth-entra@kub context: kubectl config use-context auth-entra@kub.

Click the image to enlarge.
I re-executed my command kubectl get node, and this time I received a positive response.

Click the image to enlarge.
Please note that I did not have to re-enter my credentials. The reason is that I previously retrieved a token. Even though I didn't have the necessary permissions on the cluster, the authentication worked, and I still have the associated token.
This token is located in the cache directory associated with the kubelogin addon. Generally, the folder is in your Kubernetes configuration profile: User_profile\.
kube\cache\oidc-login
.

Click the image to enlarge.
For those who are curious, you can open this token and use a site like https://jwt.io/ to decode it.

Click the image to enlarge.
You will find all the information related to the user there.
In our example, we see the IDs of the groups Microsoft Entra ID, because we did not explicitly request them during the configuration of our Enterprise Application.
Let's take the group ID 7acf3d08-ac27-423e-9f01-20aebd9ca880 as an example.
If we consult Microsoft Entra ID, we see that this ID corresponds to the group GRP-APP-K8S-ADM, in which my user is listed.

Click the image to enlarge.

Click the image to enlarge.
I am going to recreate my ClusterRoleBinding object, but instead of using the user's login, I will use the group ID.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: clusterrolebinding-vburgun-entra-to-clusteradmin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: Group
name: "7acf3d08-ac27-423e-9f01-20aebd9ca880"
apiGroup: rbac.authorization.k8s.io
I reapply the object with the command kubectl apply -f clusterrolebinding-vburgun-entra-to-clusteradmin.yml.

Click the image to enlarge.
If I return to my kubectl context auth-entra and run my command again, kubectl get node I still get a correct response.

Click the image to enlarge.
Simply, at the Kubernetes level, my access is no longer based on my login, but on my membership in a group.
Security focus
One final point regarding security. As we've seen, the token that is retrieved is stored in the kubelogin cache. If a malicious person were to retrieve it, they could exploit the token to impersonate you and access the cluster. Normally, this risk is low because the token has a limited lifespan that you can configure. However, it also implies that you have made your cache directory accessible.
Other mechanisms can be implemented, such as conditional access rules in Microsoft Entra ID (available only in the paid version), which can also enforce specific authentication sources (hardware and/or geographical).
Finally, the configuration file associated with kubectl contains the secret of the Enterprise Application. Even though this information is confidential, it is not sufficient for an attacker to impersonate you. This secret allows you to request authentication from Microsoft Entra ID, but you still need correct credentials to obtain a token.
Finally, even in the event of unavailability of Microsoft Entra ID, you always have the option of connecting to the cluster via the more traditional authentication mechanism.
Conclusion
After seeing the basics of authentication and role management under Kubernetes, I thought it would be interesting to demonstrate K8S's ability to leverage standard mechanisms such as OIDC and OAuth 2.0 to delegate authentication to specialized solutions. This is even possible with a vanilla cluster set up with Kubeadm.
Once again, it's important to understand how authentication works and the underlying logic under Kubernetes.
In a context where cybersecurity is constantly emphasized every two posts on X (Twitter), I believe it's essential to focus on what we are doing and why we are doing it...and not just follow the latest ChatGPT prompt.
For those who would like to implement this type of configuration, please pay attention to the following points:
- Reconfigure the API component after a cluster update
- Monitor the expiration of the Microsoft Entra ID secret. Plan for monitoring and maintenance periods to change it.
- Don't hesitate to use complementary options such as conditional access rules if you are a user of Microsoft Entra ID
- Prefer roles based on groups of users rather than individual users.
- Regularly review your roles and permissions, regardless of whether you use a solution like Microsoft Entra ID.
- Unlike my example, limit the use of the default cluster role as much as possible, especially cluster-admin; always prefer to use custom roles.