{"id":616,"date":"2018-10-15T20:21:49","date_gmt":"2018-10-15T18:21:49","guid":{"rendered":"http:\/\/cwiok.pl\/?p=616"},"modified":"2018-10-15T20:29:50","modified_gmt":"2018-10-15T18:29:50","slug":"setting-up-azure-key-vault","status":"publish","type":"post","link":"https:\/\/cwiok.pl\/index.php\/en\/2018\/10\/15\/setting-up-azure-key-vault\/","title":{"rendered":"Setting up Azure Key Vault"},"content":{"rendered":"<p align=\"justify\">Key Vault is an Azure module, which lets you safely store your secrets\/keys. This way your developers do not have to store credentials or connection strings in the code, but can simpy ask Key Vault to provide them. The application will be handed the password and will be able to log in without the developer even knowing it. Let us create a key vault!<\/p>\n<p><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-618\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf.png\" alt=\"\" width=\"1200\" height=\"628\" srcset=\"https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf.png 1200w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf-300x157.png 300w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf-768x402.png 768w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf-1024x536.png 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h1>Creating Key Vault<\/h1>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb.png\" alt=\"image\" width=\"871\" height=\"200\" border=\"0\" \/><\/a><\/div>\n<p align=\"justify\">To create a Key Vault (just like you probably expect), simply search for it in Azure. When creating your Key Vault you may specify access policies for your account. For now, you can leave it with default values. You may experiment it with more, once you set up the service.<\/p>\n<h1>Registering you app with Azure Active Directory<\/h1>\n<p align=\"justify\">To enable access to the secrets by Function App, you have to register your app in Azure Active Directory. Only then will you app be able to authenticate with Key Vault.<\/p>\n<p align=\"justify\">Open your Function App and navigate to Platform features:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-1.png\" alt=\"image\" width=\"932\" height=\"745\" border=\"0\" \/><\/a><\/div>\n<p align=\"justify\">Click on Managed service indentity and register with AAD:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-2.png\" alt=\"image\" width=\"587\" height=\"276\" border=\"0\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Alternatively, you can use Powershell:<\/p>\n<pre class=\"toolbar:2 wrap:true lang:ps decode:true\">Set-AzureRmWebApp -AssignIdentity $true -Name $appname \u2013ResourceGroupName $resourcegroupname<\/pre>\n<p>&nbsp;<\/p>\n<p align=\"justify\">Now that the app is registered with AAD, you can add it in access policies in your Key Vault. To do that, go to access policies:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-3.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-3.png\" alt=\"image\" width=\"266\" height=\"304\" border=\"0\" \/><\/a><\/div>\n<p align=\"justify\">Click add new, select principal (you app name), specify permissions and click OK:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-4.png\" alt=\"image\" width=\"323\" height=\"226\" border=\"0\" \/><\/a><\/div>\n<p align=\"justify\">Now, your app can access your secrets! Even if you have no permission to see the secrets, you still can use them in an app.<\/p>\n<h1><\/h1>\n<h1>Creating a secret<\/h1>\n<div align=\"center\">\n<p><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-5.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-5.png\" alt=\"image\" width=\"276\" height=\"303\" border=\"0\" \/><\/a><\/p>\n<\/div>\n<p align=\"justify\">Go to Secrets in the main Settings pane. In there you can enter your passwords, which will be accessable only by specified people\/apps. Click on Generate\/Import and add a password:<\/p>\n<p>&nbsp;<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-6.png\" alt=\"image\" width=\"239\" height=\"69\" border=\"0\" \/><\/a><\/div>\n<div align=\"center\"><\/div>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-7.png\" alt=\"image\" width=\"592\" height=\"392\" border=\"0\" \/><\/a><\/div>\n<div align=\"center\"><\/div>\n<p>&nbsp;<\/p>\n<p align=\"justify\">After creating a secret, copy its identifier. This URL can be specified as an environmental variable or pasted into the code like in the example below.<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-8.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-8.png\" alt=\"image\" width=\"1215\" height=\"665\" border=\"0\" \/><\/a><\/div>\n<h1><\/h1>\n<h1>Accessing secrets through Function App<\/h1>\n<p align=\"justify\">Now, you are ready to create a function and access the secret with code.<\/p>\n<p align=\"justify\">Go to Visual Studio and create a new function with HTTP Trigger.<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-9.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-9.png\" alt=\"image\" width=\"910\" height=\"185\" border=\"0\" \/><\/a><\/div>\n<div align=\"center\"><\/div>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-10.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-10.png\" alt=\"image\" width=\"723\" height=\"370\" border=\"0\" \/><\/a><\/div>\n<p>You have to include two namespaces:<\/p>\n<pre class=\"toolbar:2 wrap:true lang:c# decode:true\">using Microsoft.Azure.Services.AppAuthentication;\r\nusing Microsoft.Azure.KeyVault;<\/pre>\n<p>&nbsp;<\/p>\n<p align=\"justify\">Next add three lines into the code:<\/p>\n<pre class=\"toolbar:2 wrap:true lang:c# decode:true\">var azureServiceTokenProvider = new AzureServiceTokenProvider();\r\nvar kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));\r\nstring secret = kv.GetSecretAsync(\u201c&lt;Secret URL you have copied&gt;\u201d).Result.Value;<\/pre>\n<p>&nbsp;<\/p>\n<p>Also change what the function returns to:<\/p>\n<pre class=\"toolbar:2 wrap:true lang:default decode:true\">return (ActionResult)new OkObjectResult($\"Your secret is: \" + secret);<\/pre>\n<p>&nbsp;<\/p>\n<p>Whole code:<\/p>\n<pre class=\"toolbar:2 wrap:true lang:c# decode:true\">using System;\r\nusing System.IO;\r\nusing System.Threading.Tasks;\r\nusing Microsoft.AspNetCore.Mvc;\r\nusing Microsoft.Azure.WebJobs;\r\nusing Microsoft.Azure.WebJobs.Extensions.Http;\r\nusing Microsoft.AspNetCore.Http;\r\nusing Microsoft.Azure.WebJobs.Host;\r\nusing Microsoft.Extensions.Logging;\r\nusing Newtonsoft.Json;\r\nusing Microsoft.Azure.Services.AppAuthentication;\r\nusing Microsoft.Azure.KeyVault;\r\n\r\n\r\nnamespace MySecretApp\r\n{\r\n    public static class Function1\r\n    {\r\n        [FunctionName(\"Function1\")]\r\n        public static async Task&lt;IActionResult&gt; Run([HttpTrigger(AuthorizationLevel.Function, \"get\", \"post\", Route = null)]HttpRequest req, ILogger log)\r\n        {\r\n            log.LogInformation(\"C# HTTP trigger function processed a request.\");\r\n\r\n            var azureServiceTokenProvider = new AzureServiceTokenProvider();\r\n            var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));\r\n            string secret = kv.GetSecretAsync(\u201c&lt;Secret URL you have copied&gt;\u201d).Result.Value;\r\n\r\n            return (ActionResult)new OkObjectResult($\"Your secret is: \" + secret);\r\n                \r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p align=\"justify\">After you publish the function, you can run the function from within Azure portal to get your secret:<\/p>\n<div align=\"center\">\n<p><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-11.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-11.png\" alt=\"image\" width=\"475\" height=\"281\" border=\"0\" \/><\/a><\/p>\n<\/div>\n<p>Hope this helps.<\/p>\n<p>Micha\u0142<\/p>\n","protected":false},"excerpt":{"rendered":"<p align=\"justify\">Key Vault is an Azure module, which lets you safely store your secrets\/keys. This way your developers do not have to store credentials or connection strings in the code, but can simpy ask Key Vault to provide them. The application will be handed the password and will be able to log in without the developer even knowing it. Let us create a key vault!<\/p>\n<p><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-618\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf.png\" alt=\"\" width=\"1200\" height=\"628\" srcset=\"https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf.png 1200w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf-300x157.png 300w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf-768x402.png 768w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/artyku\u0142_11_sejf-1024x536.png 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<div class=\"tech_read_more\"><a href=\"https:\/\/cwiok.pl\/index.php\/en\/2018\/10\/15\/setting-up-azure-key-vault\/\">Read More<\/a><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-616","post","type-post","status-publish","format-standard","hentry","category-azure"],"_links":{"self":[{"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/posts\/616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":0,"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"wp:attachment":[{"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}