{"id":7287,"date":"2016-12-06T15:21:58","date_gmt":"2016-12-06T14:21:58","guid":{"rendered":"https:\/\/blog.redbaronofazure.com\/?p=7287"},"modified":"2016-12-06T15:28:51","modified_gmt":"2016-12-06T14:28:51","slug":"authentication-in-azure-sdk-for-java","status":"publish","type":"post","link":"https:\/\/blog.redbaronofazure.com\/?p=7287","title":{"rendered":"Authentication in Azure SDK for Java"},"content":{"rendered":"<p>The 1.0.0-beta version of the Azure\u00a0SDK for java introduces a new model for authenticating that is called &#8220;file-based authentication&#8221;. Although being described as an experimental feature, it is a way to use a service principal account when managing ARM-based resources in Azure.<\/p>\n<p><strong>Creating a Service Principal<\/strong><\/p>\n<p>The documentation for setting this up is tucked away in a file named <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-java\/blob\/master\/AUTH.md\">AUTH.md<\/a> in github, but you have to know a little Azure AD in order to understand what is happening here. In step 5. you create the service principal via the Azure CLI command below<\/p>\n<pre class=\"lang:sh decode:true\">azure config mode arm\r\nazure ad sp create --name spFawltytowers2 --password &lt;pwd&gt;<\/pre>\n<p>This Service Principal shows up as an application in the Azure Portal which is a little confusing since it is listed amongst real application entries. The guid of the the Application ID is something you need to copy, because you will use this as the client id in your Java program configuration.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-01.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7288\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-01.png\" alt=\"azurejdk-auth-01\" width=\"1240\" height=\"578\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-01.png 1240w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-01-300x140.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-01-768x358.png 768w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-01-1024x477.png 1024w\" sizes=\"(max-width: 1240px) 100vw, 1240px\" \/><\/a><\/p>\n<p>Even though you did provide a password, you need to create a key in the portal. The value can be anything you like but make sure you don&#8217;t loose it, because the portal will not display its value once you have saved it.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-02.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7289\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-02.png\" alt=\"azurejdk-auth-02\" width=\"1310\" height=\"458\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-02.png 1310w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-02-300x105.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-02-768x269.png 768w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-02-1024x358.png 1024w\" sizes=\"(max-width: 1310px) 100vw, 1310px\" \/><\/a><\/p>\n<p><strong>Creating the configuration file<\/strong><\/p>\n<p>The configuration file holds the client + key to identify the service principal but it should also contain information about the\u00a0Azure subscription that we like to use and the Azure AD that can authenticate us. The Azure CLI command &#8220;azure account show&#8221; can display your current subscription and AAD tenant. The ID field is the subscription ID and you also need to copy the Tenant ID, which is the guid for the Azure AD that we will authenticate with.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-03.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7290\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-03.png\" alt=\"azurejdk-auth-03\" width=\"1020\" height=\"540\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-03.png 1020w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-03-300x159.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-03-768x407.png 768w\" sizes=\"(max-width: 1020px) 100vw, 1020px\" \/><\/a><\/p>\n<p>These four values &#8211; client, key, subscription id and tenant id &#8211; is something you put in a text file you store with your Java application. It has the following format:<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-04.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7291\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-04.png\" alt=\"azurejdk-auth-04\" width=\"614\" height=\"302\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-04.png 614w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-04-300x148.png 300w\" sizes=\"(max-width: 614px) 100vw, 614px\" \/><\/a><\/p>\n<p>The URLs is something you likely just can steal with pride from somewhere, but if you really like to get the values for real, you can get them with the Azure CLI command &#8220;azure account env show AzureCloud&#8221;, where the value &#8220;AzureCloud&#8221; comes\u00a0of the output in the previous CLI command.<\/p>\n<p><strong>Granting access to the Service Principal<\/strong><\/p>\n<p>If you ran your Java program now, it would be able to authenticate against Azure AD using the Service Principal but it would not be able to do anything in the Azure Subscription because we haven&#8217;t granted it any access rights yet. Depending on how you are going to use this service principal, you may add it to a resource group, etc, but if you are building something that should be able to manage all resources in the subscription, you need to add it as a Contributor to the entire subscription.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-05.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7292\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-05.png\" alt=\"azurejdk-auth-05\" width=\"1688\" height=\"720\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-05.png 1688w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-05-300x128.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-05-768x328.png 768w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AzureJDK-Auth-05-1024x437.png 1024w\" sizes=\"(max-width: 1688px) 100vw, 1688px\" \/><\/a><\/p>\n<p>Go to Subscriptions (Yellow key) &gt; Access &gt;Control (IAM) and add the service principal as a Contributor.<\/p>\n<p><strong>Testing that it works<\/strong><\/p>\n<p>To test that it works you can create a tiny Java application that uses the Azure SDK for Java. The 1.0.0-beta version of the SDK builds with Maven, so create a Maven project and enter the dependancy in the pom.xml file<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\r\n         xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n         xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n    &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\r\n    &lt;groupId&gt;com.fawltytowers2&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;aztest1&lt;\/artifactId&gt;\r\n    &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;\r\n\r\n    &lt;dependencies&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;com.microsoft.azure&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;azure&lt;\/artifactId&gt;\r\n            &lt;version&gt;1.0.0-beta3&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n    &lt;\/dependencies&gt;\r\n\r\n&lt;\/project&gt;<\/pre>\n<p>Then pass the configuration file to the Azure.authenticate method and you will be able to access resources in your subscription. The below little sample program just lists all resource groups (which in this test was just a single one)<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AZUREJDK-AUTH-06.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7293\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AZUREJDK-AUTH-06.png\" alt=\"azurejdk-auth-06\" width=\"1356\" height=\"1396\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AZUREJDK-AUTH-06.png 1356w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AZUREJDK-AUTH-06-291x300.png 291w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AZUREJDK-AUTH-06-768x791.png 768w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/12\/AZUREJDK-AUTH-06-995x1024.png 995w\" sizes=\"(max-width: 1356px) 100vw, 1356px\" \/><\/a><\/p>\n<p><strong>References<\/strong><\/p>\n<p>Azure SDK for Java<br \/>\n<a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-java\">https:\/\/github.com\/Azure\/azure-sdk-for-java<\/a><\/p>\n<p>Documentation for creating the Service Principal<br \/>\n<a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-java\/blob\/master\/AUTH.md\">https:\/\/github.com\/Azure\/azure-sdk-for-java\/blob\/master\/AUTH.md<\/a><\/p>\n<p>Sample Java program for testing<br \/>\n<a href=\"https:\/\/github.com\/Azure-Samples\/resources-java-manage-resource-group\">https:\/\/github.com\/Azure-Samples\/resources-java-manage-resource-group<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The 1.0.0-beta version of the Azure\u00a0SDK for java introduces a new model for authenticating that is called &#8220;file-based authentication&#8221;. Although being described as an experimental feature, it is a way to use a service principal account when managing ARM-based resources in Azure. Creating a Service Principal The documentation for setting this up is tucked away [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[392,151],"tags":[393,409],"_links":{"self":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/7287"}],"collection":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7287"}],"version-history":[{"count":3,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/7287\/revisions"}],"predecessor-version":[{"id":7296,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/7287\/revisions\/7296"}],"wp:attachment":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}