Hitting AKS Kubernetes API with an AD app

Vlad Vantaroo 20 Reputation points
2024-02-23T19:54:38.6166667+00:00

Is there a way to use an AD app (app-id, secret, subscription) to authenticate with the Kubernetes API via HTTPS to get cluster's information without using azure cli? (like nodes, nodes configuration, roles, etc)

Azure Kubernetes Service (AKS)
Azure Kubernetes Service (AKS)
An Azure service that provides serverless Kubernetes, an integrated continuous integration and continuous delivery experience, and enterprise-grade security and governance.
2,146 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anveshreddy Nimmala 3,545 Reputation points Microsoft Vendor
    2024-02-26T06:30:10.1666667+00:00

    Hello Vlad Vantaroo Welcome to microsoft Q&A,Thankyou for posting your query yes, you can use an Azure AD app to authenticate with the Kubernetes API via HTTPS to get cluster's information without using Azure CLI. 1.Create an Azure AD App and Assign Required Permissions: i. Navigate to the Azure portal (https://portal.azure.com). ii. Go to Azure Active Directory > App registrations > New registration. iii. Fill in the necessary details (such as Name and Redirect URI). iv. Once the app is created, note down the Application (client) ID and Directory (tenant) ID. v. Under "Certificates & secrets," generate a new client secret and note down the value. vi. Assign the required permissions to the app. For Kubernetes API access, you may need to assign vii. Azure Kubernetes Service Cluster User role or custom role with appropriate permissions. 2.Use the following command to get the access token: curl -X POST -d 'grant_type=client_credentials&client_id=<app-id>&client_secret=<app-secret>&resource=https://management.azure.com/' https://login.microsoftonline.com/<tenant-id>/oauth2/token 3.Note down the access token received. 4.Authenticate with the Kubernetes API Server: 5.Use the access token obtained in step 2 to authenticate with the Kubernetes API server. 6.Use the following command to get the list of nodes in the cluster: curl -H "Authorization: Bearer <access-token>" https://<kubernetes-api-server>/api/v1/nodes Hope this helps you.please consider accepting the answer to help increase visibility of this question for other members of the Microsoft Q&A community. If not, please let us know what is still needed in the comments so the question can be answered. Thank you for helping to improve Microsoft Q&A!


  2. Vlad Vantaroo 20 Reputation points
    2024-02-29T18:22:05.6033333+00:00

    The issue was that the request to get the token was wrong. This is a good example that works:

    curl -X POST -d 'grant_type=client_credentials' \
    -d 'client_id=<app-id>' \
    -d 'client_secret=<app-secret>' \
    -d 'scope=<api-server-id>/.default' \
    https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/token
    

    The <api-server-id> can be grabbed from the kubernetes config


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.