<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>azure service security &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/azure-service-security/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Tue, 02 Feb 2021 09:38:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>azure service security &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Why Not Secure Your Keys and Secrets? Asp Net Core with Azure Key Vault Integration</title>
		<link>https://www.sibeeshpassion.com/why-not-secure-your-keys-and-secrets-asp-net-core-with-azure-key-vault-integration/</link>
					<comments>https://www.sibeeshpassion.com/why-not-secure-your-keys-and-secrets-asp-net-core-with-azure-key-vault-integration/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 18 Nov 2020 12:31:33 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[azure app service and azure key vault]]></category>
		<category><![CDATA[Azure Key Vault]]></category>
		<category><![CDATA[azure managed identity]]></category>
		<category><![CDATA[azure service security]]></category>
		<category><![CDATA[azure system assigned managed identity]]></category>
		<category><![CDATA[azure user assigned managed identity]]></category>
		<category><![CDATA[secret storage tool]]></category>
		<category><![CDATA[secure app settings]]></category>
		<category><![CDATA[secure secrets in asp net core]]></category>
		<category><![CDATA[secure secrets in azure]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=14244</guid>

					<description><![CDATA[Why not secure your keys and secrets in your application, if you can do it in a few minutes? If this question make you think at least for a minute, then don't think more, just do it. ]]></description>
										<content:encoded><![CDATA[
<p>Why not secure your keys and secrets in your application, if you can do it in a few minutes? If this question makes you think at least for a minute, then don&#8217;t think more, just do it. Here in this post, we will secure our connection string and use it in our application. Sounds good? Then, let&#8217;s start.</p>



<h2 class="wp-block-heading">Prerequisites</h2>



<p>I will try to make this post as simple as it can, however, a basic idea of the preceding things definitely makes things easier.</p>



<ul class="wp-block-list"><li>Azure Key Vault</li><li>Azure App Service</li><li>Asp Net Core</li><li>C#</li></ul>



<p>Please remember that you need a valid Azure subscription. If you are looking to secure your Azure Function app settings, read my post <a href="https://sibeeshpassion.com/secure-serverless-azure-functions-appsetting-using-key-vault/" target="_blank" rel="noreferrer noopener">here</a>.</p>



<h2 class="wp-block-heading">Build an Asp Net Core Web App</h2>



<p>At thing stage, I am assuming that you already have an application, if you don&#8217;t have just create a sample application using some visual studio templates. Once the application is loaded, we can install the package &#8220;Microsoft.Extensions.Configuration.AzureKeyVault&#8221; from NuGet.  As I said earlier, we will be using Azure services for this demo, we can leverage the option called &#8221; Managed Identities&#8221; in Azure. Please be noted that if you are using any other cloud providers, the steps will be different. </p>



<h3 class="wp-block-heading">Now, what is Managed Identities in Azure?</h3>



<p>The one challenge we developers will always have is about the security and where we can save them and use them without compromising on the credentials. Azure Managed Identity will provide an identity for the resource in Azure AD and use the same to get the Azure Active Directory token. This token will be used to communicate between the services, in our cases, our Azure app service will talk to the Key Vault. The Managed Identity service is free of cost, another reason why not to use it.</p>



<p>There are two types of Managed Identities.</p>



<ul class="wp-block-list"><li>System-assigned</li><li>User-assigned</li></ul>



<p>The  System-assigned managed identity is tied to the Azure resource where you add the identity, this means that when you deleted the resource the identity will also be deleted automatically. Only some Azure resources support this identity type.</p>



<p>In the User-assigned identity, we can create a managed identity as a standalone Azure resource, the advantage of doing so, is that we can assign this identity to any resources we need and it is not tied to any resources. Thus, deleting a resource will not delete the identity. The preceding image explains when you can use a managed identity. </p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="651" height="259" src="/wp-content/uploads/2020/11/azure-managed-identity.jpg" alt="" class="wp-image-14246" srcset="/wp-content/uploads/2020/11/azure-managed-identity.jpg 651w, /wp-content/uploads/2020/11/azure-managed-identity-300x119.jpg 300w" sizes="(max-width: 651px) 100vw, 651px" /><figcaption>azure managed identity</figcaption></figure></div>



<p>In our case, we need to get the secrets from our Key Vault and use the same in our Azure App service, thus I am going to use the System-assigned managed identity. I am sure, you know why.</p>



<h3 class="wp-block-heading">Update the Appsetting</h3>



<p>In the &#8220;appsettings.json,&#8221; we will be providing the Key vault name and that is it. No more connection string, no more client id, client secret, etc. Here is the sample setting file.</p>



<script src="https://gist.github.com/SibeeshVenu/b824c6bd12a8f71efe54d0605ccb1287.js"></script>



<p>Now, let&#8217;s configure our app to use the values from the Azure Key Vault. Go to the &#8220;Program. cs&#8221; file and update the method CreateHostBuilder as follows. </p>



<script src="https://gist.github.com/SibeeshVenu/2e931b11413138a0c142ed61a131e836.js"></script>



<p> As you can see that in the above code, we create an instance of &#8220;AzureServiceTokenProvider&#8221; without a connection string, and the provider will get an access token from the managed identity. Now let&#8217;s go to our Startup class and add the preceding code in the ConfigureServices method. </p>



<pre class="wp-block-code"><code>options.UseSqlServer(Configuration&#91;"DefaultConnection"],
            action =&gt; action.MigrationsAssembly(typeof(TenantContext).Assembly.FullName)));</code></pre>



<p>That&#8217;s all. Now if you run this application you will get an error saying that &#8220;Value can not be null&#8221;. Remember that we have not created the Azure resources. </p>



<h2 class="wp-block-heading">Configure Azure App Service and Azure Key Vault</h2>



<p>Once you create your Azure Key Vault and Azure App Service, go to your Azure Key Vault and click on the secrets and add the secrets you have, in our case, a connection string. I will give the secret name &#8220;DefaultConnection&#8221; and the value as my database connection string.</p>



<p>An application deployed Azure App Service is automatically registered with Azure AD when the service is created. Let&#8217;s go to the identity panel of the Azure App service that you had created and enable the System-assigned managed identity.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="506" height="296" src="/wp-content/uploads/2020/11/enable-system-assigned-managed-identity.jpg" alt="" class="wp-image-14247" srcset="/wp-content/uploads/2020/11/enable-system-assigned-managed-identity.jpg 506w, /wp-content/uploads/2020/11/enable-system-assigned-managed-identity-300x175.jpg 300w" sizes="(max-width: 506px) 100vw, 506px" /><figcaption>enable system assigned managed identity</figcaption></figure></div>



<p>Please be noted that once you click on the save button, the app service will be registered with Azure Active Directory and it can be granted permission to access resources protected by Azure AD. Now, get the object id from the screen and make a note of the same, as we will be using this in a while.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="438" height="347" src="/wp-content/uploads/2020/11/get-the-object-id-from-system-assigned-managed-identity.jpg" alt="" class="wp-image-14248" srcset="/wp-content/uploads/2020/11/get-the-object-id-from-system-assigned-managed-identity.jpg 438w, /wp-content/uploads/2020/11/get-the-object-id-from-system-assigned-managed-identity-300x238.jpg 300w" sizes="(max-width: 438px) 100vw, 438px" /><figcaption>get the object id from system assigned managed identity</figcaption></figure></div>



<p>Now, go to the Azure Key Vault you have created and click on the &#8220;Access policies&#8221; from the left side pane, and click on the &#8220;+Add Access Policy&#8221;.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="628" height="357" src="/wp-content/uploads/2020/11/add-keyvault-access-policy.jpg" alt="" class="wp-image-14249" srcset="/wp-content/uploads/2020/11/add-keyvault-access-policy.jpg 628w, /wp-content/uploads/2020/11/add-keyvault-access-policy-300x171.jpg 300w" sizes="(max-width: 628px) 100vw, 628px" /><figcaption>add key vault access policy</figcaption></figure></div>



<p>From the next screen, select the items as in the preceding image. Please be noted that you can choose the permissions that you want to set. In the &#8220;Select Principal&#8221; screen, search the item with the object id of our app service. Select the item and click save. This is how your screen may look like. </p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="582" height="404" src="/wp-content/uploads/2020/11/add-a-policy-with-app-service-object-id.jpg" alt="" class="wp-image-14250" srcset="/wp-content/uploads/2020/11/add-a-policy-with-app-service-object-id.jpg 582w, /wp-content/uploads/2020/11/add-a-policy-with-app-service-object-id-300x208.jpg 300w" sizes="(max-width: 582px) 100vw, 582px" /><figcaption>add a policy with app service object id</figcaption></figure></div>



<p> Click on the Add button. The policy will be added. <strong>Please do not forget to remember to click on the Save button from the next screen</strong>. </p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="536" height="468" src="/wp-content/uploads/2020/11/save-policy-key-vault.jpg" alt="" class="wp-image-14251" srcset="/wp-content/uploads/2020/11/save-policy-key-vault.jpg 536w, /wp-content/uploads/2020/11/save-policy-key-vault-300x262.jpg 300w" sizes="(max-width: 536px) 100vw, 536px" /><figcaption>save policy key vault</figcaption></figure></div>



<p>Do not forget to r<strong>estart the Azure App Service</strong>, this is important.  Now go ahead and publish your Asp Net Core application to your Azure App service. You can also use the Visual Studio Publish option or use the Azure DevOps pipeline. If you choose the second option, read my detailed article about it <a rel="noreferrer noopener" href="https://sibeeshpassion.com/azure-multi-stage-pipeline-for-asp-net-core-and-azure-deploy-to-azure-app-service/" target="_blank">here</a>. </p>



<p>That&#8217;s it. Well done. We now have a running application in the Azure app service, that fetches the secrets from the Azure Key Vault and uses them. But, will it work with the development environment? No, that requires a few more setups. </p>



<h2 class="wp-block-heading">Secret Storage for Development Environment</h2>



<p>Here, we are going to use a tool called Secret Manager. This tool will help us not to save any sensitive data in the application. Please be noted that the tool doesn&#8217;t encrypt the values, thus use it only for the development environment. This tool operates on project-specific configuration settings stored in your user profile. Go to your project home directory and run the preceding command to enable the Secret Storage. </p>



<pre class="wp-block-code"><code>dotnet user-secrets init</code></pre>



<p>This will produce the output below. </p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="1024" height="228" src="/wp-content/uploads/2020/11/init-secret-storage-1024x228.jpg" alt="" class="wp-image-14252" srcset="/wp-content/uploads/2020/11/init-secret-storage-1024x228.jpg 1024w, /wp-content/uploads/2020/11/init-secret-storage-300x67.jpg 300w, /wp-content/uploads/2020/11/init-secret-storage-768x171.jpg 768w, /wp-content/uploads/2020/11/init-secret-storage.jpg 1193w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>init secret storage</figcaption></figure></div>



<p>Now, if you check the content of your &#8220;.csproj&#8221; file, you will see that a new property is added to the property group with GUID as value. </p>



<pre class="wp-block-code"><code>&lt;UserSecretsId&gt;cbc82397-befe-4fce-885d-d355bf89ef45&lt;/UserSecretsId&gt;</code></pre>



<p>Right-click on your project and click on the Manage User Secrets, this will show a &#8220;secret.json&#8221; file and this is where we are going to add all of our secrets, shh don&#8217;t say this to anyone. This is the location where this file is located &#8220;C:\Users\SibeeshVenu\AppData\Roaming\Microsoft\UserSecrets&#8221;. We can edit our secret.json file with the connection string, this is how your file may look like. </p>



<pre class="wp-block-code"><code>{
  "DefaultConnection": "yourconnectionstring"
}</code></pre>



<p>Save the file, and run your application, it should work as it is. The secrets configuration source is automatically added to the development environment. Just look at the providers in the Configuration object now.  </p>



<figure class="wp-block-image size-large"><img decoding="async" width="816" height="175" src="/wp-content/uploads/2020/11/configuration-provider-for-secrets.jpg" alt="" class="wp-image-14253" srcset="/wp-content/uploads/2020/11/configuration-provider-for-secrets.jpg 816w, /wp-content/uploads/2020/11/configuration-provider-for-secrets-300x64.jpg 300w, /wp-content/uploads/2020/11/configuration-provider-for-secrets-768x165.jpg 768w" sizes="(max-width: 816px) 100vw, 816px" /><figcaption>configuration provider for secrets</figcaption></figure>



<p>You can also add any properties via command line.</p>



<pre class="wp-block-code"><code>dotnet user-secrets set "AzureAd:ClientSecret" "secretvalue"</code></pre>



<p>Here AzureAd is my object literal with a property ClientSecret in it. Once you run the command, you will get a response in the command line as preceding.</p>



<figure class="wp-block-image size-large"><a href="/wp-content/uploads/2021/02/Adding-secret-via-command-line.jpg"><img decoding="async" width="1024" height="45" src="/wp-content/uploads/2021/02/Adding-secret-via-command-line-1024x45.jpg" alt="" class="wp-image-14292" srcset="/wp-content/uploads/2021/02/Adding-secret-via-command-line-1024x45.jpg 1024w, /wp-content/uploads/2021/02/Adding-secret-via-command-line-300x13.jpg 300w, /wp-content/uploads/2021/02/Adding-secret-via-command-line-768x34.jpg 768w, /wp-content/uploads/2021/02/Adding-secret-via-command-line.jpg 1324w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>Adding secrets via command line</figcaption></figure>



<p>You can do many other things with this tool, I strongly recommend you to read <a rel="noreferrer noopener" href="https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?WT.mc_id=AZ-MVP-5001828" target="_blank">this post</a> to know more.</p>



<h2 class="wp-block-heading">Conclusion</h2>



<p>Congratulations and thanks a lot for being with me this far. We now have a complete secured application where we didn&#8217;t compromise on the credentials and secrets. Happy Coding!.</p>



<h2 class="wp-block-heading">About the Author</h2>



<p>I am yet another developer who is passionate about writing and video creation. I have written close to 500 blogs on my&nbsp;<a href="https://sibeeshpassion.com/" target="_blank" rel="noreferrer noopener">blog</a>. And I upload videos on my YouTube channels&nbsp;<a href="https://www.youtube.com/njanorumalayali" target="_blank" rel="noreferrer noopener">Njan Oru Malayali</a>&nbsp;and&nbsp;<a href="https://www.youtube.com/SibeeshPassion" target="_blank" rel="noreferrer noopener">Sibeesh Passion</a>. Please feel free to follow me.</p>



<ul class="wp-block-list"><li><a href="https://github.com/SibeeshVenu">GitHub</a></li><li><a href="https://medium.com/@sibeeshvenu">medium</a></li><li><a href="https://twitter.com/sibeeshvenu">Twitter</a></li></ul>



<h2 class="wp-block-heading">Your turn. What do you think?</h2>



<p>Thanks a lot for reading. Did I miss anything that you may think which is needed in this article? Could you find this post useful? Kindly do not forget to share your feedback.</p>



<p>Kindest Regards</p>



<p>Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/why-not-secure-your-keys-and-secrets-asp-net-core-with-azure-key-vault-integration/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
