{"id":55158,"date":"2022-07-07T16:42:32","date_gmt":"2022-07-07T11:12:32","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55158"},"modified":"2022-07-18T22:44:13","modified_gmt":"2022-07-18T17:14:13","slug":"azure-account-authentication-using-python3","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/azure-account-authentication-using-python3\/","title":{"rendered":"Azure Account Authentication Using Python3"},"content":{"rendered":"<h4 style=\"text-align: left;\"><b>Need for Authentication:<\/b><\/h4>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">To communicate with Azure Resources we need to do authentication, suppose you have an application that monitors Azure resources. To fetch azure resources data from the azure account, we need a connection string or secrets to do Azure Authentication. Ather that, these all secrets are shared with the application developer who integrates secrets into the code itself. <\/span><span style=\"font-weight: 400;\">As we all know, Python is one of the most popular programming language because of its simplified syntax and it also provides great support for writing scripts to interact with cloud resources.\u00a0<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">Azure SDK is an Azure Software Development Kit for Python3, which allows Python developers to write software that makes use of Azure services.<\/span><\/p>\n<h4 style=\"text-align: left;\"><b>The Azure Authentication process requires:<\/b><\/h4>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\"><strong>Step 1.<\/strong> Create Azure account<br \/>\n<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\"><strong>Step 2.<\/strong> Get Subscription and Tenant Id.<br \/>\n<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\"><strong>Step 3.<\/strong> Registered Your Application<br \/>\n<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\"><strong>Step 4.<\/strong> Create New Client Secret in Registered Application and Save Value Id.<br \/>\n<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\"><strong>Step 5.<\/strong> Grant Reader permission to Registered App.<br \/>\n<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\"><strong>Step 6.<\/strong> Now use Client Secret value, subscription, tenant, and Client(Application) Id in Azure SDK script to fetch Azure Resource data. (Here we are fetching a list of Azure VM using these keys).<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">Workflow Diagram to authenticate your azure account to interact with Azure Resources using Azure Python SDK<\/span><\/p>\n<p style=\"text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55156 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/Flowchart1-2.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/Flowchart1-2.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/Flowchart1-2-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/Flowchart1-2-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/Flowchart1-2-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3 style=\"text-align: left;\"><b>Step1<\/b><b>:<\/b> <b>Create an Azure account\u00a0<\/b><\/h3>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">Link: <\/span><a href=\"https:\/\/www.acronis.com\/en-sg\/articles\/create-microsoft-azure-account\/\"><span style=\"font-weight: 400;\">https:\/\/www.acronis.com\/en-sg\/articles\/create-microsoft-azure-account\/<\/span><\/a><\/p>\n<h3 style=\"text-align: left;\"><b>Step2:<\/b> <b>Get a Subscription and Tenant Id<\/b><\/h3>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">To get your Azure Subscription and Tenant Id of your Azure account, use your local terminal or use the azure cloud shell terminal in the Azure Portal.<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">Open your Azure portal \u2192 <\/span><b>Go to Subscription<\/b><span style=\"font-weight: 400;\"> \u2192 Copy and <\/span><b>save your subscription id<\/b><span style=\"font-weight: 400;\"> \u2192 then go to <\/span><b>Azure Active Directory<\/b><span style=\"font-weight: 400;\"> \u2192 Copy and <\/span><b>save your tenant id<\/b><span style=\"font-weight: 400;\"> for later use.<\/span><\/p>\n<p style=\"text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55135\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/1-1.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/1-1.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/1-1-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/1-1-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/1-1-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/p>\n<p style=\"text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55136\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/2.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/2.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/2-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/2-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/2-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/p>\n<p style=\"text-align: left;\"><b>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0OR<\/b><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">Open your Azure Cloud Shell Terminal and use the below command to get your Account Subscription and Tenant Id.<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">&gt; <\/span><b>az account show<\/b><\/p><\/blockquote>\n<p style=\"text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft wp-image-55157 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/4-1.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/4-1.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/4-1-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/4-1-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/4-1-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3 style=\"text-align: left;\"><b>Step 3<\/b><b>:<\/b><b>\u00a0 <\/b><b>Registered Your Application<\/b><\/h3>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">you must have sufficient permission to register your application in your Azure AD. If you have a user role, you must make sure that non-administrators can register applications. <\/span><span style=\"font-weight: 400;\">You can ask for an Application Developer Role.<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">You can also check your access by going into <\/span><b>Subscription<\/b><span style=\"font-weight: 400;\">\u2192 <\/span><b>IAM<\/b><span style=\"font-weight: 400;\">\u2192 <\/span><b>view my access.<\/b><\/p>\n<p style=\"text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-55138 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/5.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/5.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/5-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/5-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/5-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/p>\n<p style=\"text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-55139 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/6.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/6.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/6-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/6-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/6-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/p>\n<p style=\"text-align: left;\"><b>\u00a0To register your app in Azure Portal:<\/b><\/p>\n<ol style=\"text-align: left;\">\n<li><span style=\"font-weight: 400;\"> Sign in to your Azure portal and go to Azure Active Directory<\/span><br \/>\nFrom Manage, Select App Registration<img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-55140 aligncenter\" style=\"font-size: 1rem;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/7.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/7.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/7-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/7-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/7-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/li>\n<li><span style=\"font-weight: 400;\">Select <\/span><b style=\"font-size: 1rem;\">App Registration<\/b><span style=\"font-weight: 400;\"> \u2192 <\/span><b style=\"font-size: 1rem;\">Add<\/b> <b style=\"font-size: 1rem;\"><b style=\"font-size: 1rem;\">New Registration<\/b><\/b><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-55266 size-full aligncenter\" style=\"font-size: 1rem;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/1converted-1.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/1converted-1.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/1converted-1-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/1converted-1-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/1converted-1-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/li>\n<li><span style=\"font-size: 1rem;\">Enter a name for the Application. ( Azure-Integration)<\/span><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55142\" style=\"font-size: 1rem;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/9.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/9.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/9-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/9-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/9-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/li>\n<li><span style=\"font-weight: 400;\">In the <\/span><b>Add a Redirect Url<\/b><span style=\"font-weight: 400;\"> \u2192 <\/span><b>Add a Platform<\/b><span style=\"font-weight: 400;\"> \u2192 <\/span><b>select Web<\/b><span style=\"font-weight: 400;\"> and <\/span><b>add your app URL (https:\/\/<\/b><a href=\"http:\/\/www.xyz.com\"><b>xyz.com<\/b><\/a><b>) as the sign-on URL<\/b><span style=\"font-weight: 400;\"> and <\/span><b>Create the Application \u2192 Register<\/b><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55143\" style=\"font-size: 1rem;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/10.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/10.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/10-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/10-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/10-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55144\" style=\"font-size: 1rem;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/11.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/11.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/11-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/11-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/11-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/li>\n<\/ol>\n<ol style=\"text-align: left;\" start=\"5\">\n<li><span style=\"font-weight: 400;\"> From Overview of Registered App, copy the Application\/Client Id and save it anywhere for later use<\/span><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55145\" style=\"font-size: 1rem;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/12.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/12.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/12-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/12-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/12-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/li>\n<\/ol>\n<h3 style=\"text-align: left;\"><b>Step4:<\/b> <b>Create New Client Secret in Registered Application and Save Value Id<\/b><\/h3>\n<p style=\"text-align: left;\"><b>To create a client secret associated with your app:<\/b><\/p>\n<ol style=\"text-align: left;\">\n<li><span style=\"font-weight: 400;\"> In Azure, under the application, you have just created, select Certificates &amp; Secrets<\/span><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55264 size-full\" style=\"font-size: 1rem;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/2-converted-2.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/2-converted-2.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/2-converted-2-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/2-converted-2-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/2-converted-2-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/li>\n<li><span style=\"font-weight: 400;\">Under these <\/span><b style=\"font-size: 1rem;\">Certificates &amp; Secrets<\/b><span style=\"font-weight: 400;\"> \u2192 <\/span><b style=\"font-size: 1rem;\">Add new Client Secret<\/b><span style=\"font-weight: 400;\"> -&gt; <\/span><b style=\"font-size: 1rem;\">Give Description<\/b><span style=\"font-weight: 400;\"> \u2192 <\/span><b style=\"font-size: 1rem;\">Select <\/b><b style=\"font-size: 1rem;\"><br \/>\nexpiration time \u2192 Copy Value somewhere else because it hides sometimes\u00a0<\/b><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55147\" style=\"font-size: 1rem;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/14.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/14.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/14-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/14-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/14-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/li>\n<\/ol>\n<h3 style=\"text-align: left;\"><b>Step5: <\/b><b>Grant Reader permission to Registered App<\/b><\/h3>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 Grant Reader permission to your Application:<\/span><\/p>\n<ol style=\"text-align: left;\">\n<li><span style=\"font-weight: 400;\"> Go to <\/span><b>Subscriptions <\/b><span style=\"font-weight: 400;\">\u2192 and open the <\/span><b>Subscription that you want Your app<\/b><span style=\"font-weight: 400;\"> ( Monitoring Webapp ) to monitor.<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55310 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/subscriptions-1.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/subscriptions-1.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/subscriptions-1-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/subscriptions-1-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/subscriptions-1-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><br \/>\n<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> Click on <\/span><b>Subscription <\/b><span style=\"font-weight: 400;\">\u2192 Select <\/span><b>Access control (IAM)<\/b><span style=\"font-weight: 400;\"> \u2192 <\/span><b>Add role Assignment<\/b><span style=\"font-weight: 400;\"> \u2192 <\/span><b>Add Member <\/b><span style=\"font-weight: 400;\">\u2192<\/span><b> Select your app and add and save<\/b><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-55151 \" style=\"font-size: 1rem;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/18.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/18.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/18-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/18-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/18-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55152\" style=\"font-size: 1rem;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/19.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/19.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/19-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/19-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/19-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/li>\n<\/ol>\n<h3><b>Step6:<\/b><strong> Fetch the number of VM&#8217;s data in our Azure Account using Azure SDK with Keys<\/strong><\/h3>\n<p style=\"text-align: left;\"><b>resources.py :<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">from azure.mgmt.compute import ComputeManagementClient\r\nfrom azure.common.credentials import ServicePrincipalCredentials\r\nimport os<\/span>\r\n<span style=\"font-weight: 400;\">Subscription_Id = os.environ[\u201csubscription_id\u201d]<\/span>\r\n<span style=\"font-weight: 400;\">Tenant_Id = os.environ[\u201ctenant_id\u201d]<\/span>\r\n<span style=\"font-weight: 400;\">Client_Id = os.environ[\u201cclient_id\u201d]<\/span>\r\n<span style=\"font-weight: 400;\">Secret = os.environ[\u201csecret\u201d]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">credential = ServicePrincipalCredentials(<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 client_id=Client_Id,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 secret=Secret,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 tenant=Tenant_Id<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 )<\/span>\r\n\r\n<span style=\"font-weight: 400;\">compute_client = ComputeManagementClient(credential, Subscription_Id)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">vm_list = compute_client.virtual_machines.list('azure-poc')<\/span>\r\n<span style=\"font-weight: 400;\"># vm_list = compute_client.virtual_machines.list('resource_groupname')<\/span>\r\n<span style=\"font-weight: 400;\">i= <\/span><span style=\"font-weight: 400;\">0<\/span>\r\n<span style=\"font-weight: 400;\">for vm <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> vm_list:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 array = vm.id.split(<\/span><span style=\"font-weight: 400;\">\"\/\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 resource_group = array[<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 vm_name = array[-<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 statuses = compute_client.virtual_machines.instance_view(resource_group, vm_name).statuses<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 status = len(statuses) &gt;= <\/span><span style=\"font-weight: 400;\">2<\/span> <span style=\"font-weight: 400;\">and<\/span><span style=\"font-weight: 400;\"> statuses[<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> status <\/span><span style=\"font-weight: 400;\">and<\/span><span style=\"font-weight: 400;\"> status.code == 'PowerState\/running':<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(vm_name)<\/span><\/pre>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\"><span style=\"text-decoration: underline;\">Output<\/span>:\u00a0<\/span><\/p>\n<p style=\"text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55154\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/22.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/22.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/22-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/22-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/22-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><b>Rotate Client Secrets Credentials:<\/b><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">It will be required to update the application&#8217;s authentication credentials using the Azure Portal, once the client secret expires (<\/span><b>the<\/b> <b>maximum expiration date available in Azure is 2 years<\/b><span style=\"font-weight: 400;\">). <\/span><span style=\"font-weight: 400;\">Go to <\/span><b>App Registration<\/b><span style=\"font-weight: 400;\"> \u2192 Select <\/span><b>Your App<\/b><span style=\"font-weight: 400;\"> \u2192 Go into <\/span><b>Client Secret<\/b><span style=\"font-weight: 400;\"> \u2014&gt;<\/span><b>Edit the Client Secret field with the new value <\/b><span style=\"font-weight: 400;\">and confirm with <\/span><b>Save<\/b><span style=\"font-weight: 400;\"> Changes.<\/span><\/p>\n<p style=\"text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55155\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/21.png\" alt=\"\" width=\"850\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/21.png 850w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/21-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/21-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/21-624x264.png 624w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3 style=\"text-align: left;\"><b>Use Case Of Managed Identity:<\/b><\/h3>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">Using a managed identity, you can authenticate to any service that supports Azure AD authentication without exposing credentials.<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">Azure Managed Identity\u00a0<\/span><\/p>\n<ul style=\"text-align: left;\">\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">User Assigned Managed Identities\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">System Assigned Managed Identities\u00a0<\/span><\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55312 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/Screenshot-2022-07-18-161006.png\" alt=\"\" width=\"1099\" height=\"360\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/07\/Screenshot-2022-07-18-161006.png 1099w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/Screenshot-2022-07-18-161006-300x98.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/Screenshot-2022-07-18-161006-1024x335.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/Screenshot-2022-07-18-161006-768x252.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/07\/Screenshot-2022-07-18-161006-624x204.png 624w\" sizes=\"(max-width: 1099px) 100vw, 1099px\" \/><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">But as we can see, the system assigned managed not to work for us where you have to monitor thousands of services because we need to create thousands of managed identities for each azure resource to communicate with them.<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">Same, In the case of User assigned managed identity, we need to grant permission every time whenever newly resource group is created in our Azure Infra. So it is also not good, as we need to add every time newly created resource group. <\/span><span style=\"font-weight: 400;\">It is clearly seen that there is no other way to do authentication Using Python SDK except App Registration.<\/span><\/p>\n<p style=\"text-align: left;\"><b>Can we Use Azure Key Vault to store secrets, Instead of passing Direct Client Id and Client Secret Id in code:<\/b><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">As we know, Azure Vault is the most secure way to handle such a situation in today\u2019s scenarios. But it is not free to use, you have to pay $0.03 for a 10k transaction in the standard pricing tier. So It is not beneficial in that scenario where you have to fetch the secret key from the vault to communicate with azure resources more frequently.<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">No, We can\u2019t fetch the ClientId and Client SecretId directly from Key Vault because Key Vault Url is not a Global URL, before accessing it we need to authenticate Key Vault and for authentication, we are using the app Registration Secret.<\/span><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>If you want to communicate with more than one service using the same credential in the entire subscription, then go with Azure App Registration Authentication. If your code runs in any Azure service, go with Azure Managed Identities Approach. If you want to store those credentials in Keyvault, then you need to authenticate the key vault first and then use their credentials in code but when your client&#8217;s secret expires you need to create a new Key vault again and provide the reader permission again to it.<\/p>\n<p>&nbsp;<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Need for Authentication: To communicate with Azure Resources we need to do authentication, suppose you have an application that monitors Azure resources. To fetch azure resources data from the azure account, we need a connection string or secrets to do Azure Authentication. Ather that, these all secrets are shared with the application developer who integrates [&hellip;]<\/p>\n","protected":false},"author":1435,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":136},"categories":[4308,2348],"tags":[4990],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55158"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1435"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55158"}],"version-history":[{"count":10,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55158\/revisions"}],"predecessor-version":[{"id":55315,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55158\/revisions\/55315"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}