<?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 Key Vault &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/azure-key-vault/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Sat, 01 Oct 2022 14:34:39 +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 Key Vault &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Post Messages to Microsoft Teams Using Python</title>
		<link>https://www.sibeeshpassion.com/post-messages-to-microsoft-teams-using-python/</link>
					<comments>https://www.sibeeshpassion.com/post-messages-to-microsoft-teams-using-python/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sat, 01 Oct 2022 14:34:39 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Azure Key Vault]]></category>
		<category><![CDATA[Microsoft teams]]></category>
		<category><![CDATA[Microsoft Teams Webhook with Python]]></category>
		<category><![CDATA[Send message to Microsoft Teams]]></category>
		<category><![CDATA[Teams Python Client]]></category>
		<category><![CDATA[Teams Webhook]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=14592</guid>

					<description><![CDATA[Most of us uses Microsoft Teams on a daily basis, and we post a lot of messages, chats, contents to the platform. However there are times that you may need to post these contents automatically. Here in this post, we will see how we can post content to channel in Microsoft Teams using Python. Let's get started. ]]></description>
										<content:encoded><![CDATA[
<p>Most of us uses Microsoft Teams on a daily basis, and we post a lot of messages, chats, contents to the platform. However there are times that you may need to post these contents automatically. Here in this post, we will see how we can post content to channel in Microsoft Teams using Python. Let&#8217;s get started. </p>



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



<ol class="wp-block-list"><li>You will need Python installed on your machine</li><li>You will need to create a Team and a channel in the Team</li></ol>



<h1 class="wp-block-heading">Setting up a channel</h1>



<h2 class="wp-block-heading">Creating a channel</h2>



<p>You will need to <a rel="noreferrer noopener" href="https://support.microsoft.com/en-us/office/create-a-team-from-scratch-174adf5f-846b-4780-b765-de1a0a737e2b" target="_blank">set up Teams before</a> you create a channel, and if you have enough access, it is an easy thing to do. Once the Team is ready to use, you can either use one of the existing channel, or create a new one from the context menu. </p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="/wp-content/uploads/2022/10/image.png"><img fetchpriority="high" decoding="async" width="303" height="509" src="/wp-content/uploads/2022/10/image.png" alt="" class="wp-image-14594" srcset="/wp-content/uploads/2022/10/image.png 303w, /wp-content/uploads/2022/10/image-179x300.png 179w" sizes="(max-width: 303px) 100vw, 303px" /></a><figcaption>Teams Context Menu</figcaption></figure>
</div>


<h2 class="wp-block-heading">Setting up the connector</h2>



<p>When you have decided which channel to use, click on the three dot on the right-side of your channel name, and then click on the Connectors and then search for the connector &#8220;Incoming Webhook&#8221; and then click on the button &#8220;Add&#8221;. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>The above process might take a few minutes. If you are unable to perform this, you can try using <a href="https://teams.microsoft.com/" target="_blank" rel="noreferrer noopener">the web version of Microsoft Teams</a>.</p><cite>Use of web verstion of Microsoft Teams</cite></blockquote>



<p>The above step will add the connector to your Teams, so that you can start configuring your Webhook. Click on the Configure button.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="/wp-content/uploads/2022/10/image-1.png"><img decoding="async" width="1024" height="196" src="/wp-content/uploads/2022/10/image-1-1024x196.png" alt="" class="wp-image-14595" srcset="/wp-content/uploads/2022/10/image-1-1024x196.png 1024w, /wp-content/uploads/2022/10/image-1-300x57.png 300w, /wp-content/uploads/2022/10/image-1-768x147.png 768w, /wp-content/uploads/2022/10/image-1.png 1129w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>Configure Button</figcaption></figure>
</div>


<p>Provide a meaningful name for your Webhook and upload an image if required. Finally click on the Create button. A unique Url will be generated for your Webhook. Copy the Url and save it somewhere, we will later add this to <a rel="noreferrer noopener" href="https://sibeeshpassion.com/tag/azure-key-vault/" target="_blank">Azure Key Vault</a> and use from there. Click on the Done button. Your Webhook now should be shown as configured. </p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="/wp-content/uploads/2022/10/image-2.png"><img decoding="async" width="1024" height="214" src="/wp-content/uploads/2022/10/image-2-1024x214.png" alt="" class="wp-image-14596" srcset="/wp-content/uploads/2022/10/image-2-1024x214.png 1024w, /wp-content/uploads/2022/10/image-2-300x63.png 300w, /wp-content/uploads/2022/10/image-2-768x161.png 768w, /wp-content/uploads/2022/10/image-2.png 1147w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>Webhook Configured</figcaption></figure>
</div>


<h1 class="wp-block-heading">Post Message Using the Connector</h1>



<h2 class="wp-block-heading">Setting up Azure Key Vault</h2>



<p>Configuring the Azure Key Vault is as easy as you set up any other Azure Resource. You may get the steps required from <a rel="noreferrer noopener" href="https://sibeeshpassion.com/why-not-secure-your-keys-and-secrets-asp-net-core-with-azure-key-vault-integration/" target="_blank">this post</a> or <a rel="noreferrer noopener" href="https://learn.microsoft.com/en-us/azure/key-vault/general/overview" target="_blank">this</a>. Add a new secret and paste the Webhook Url value you copied earlier. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Make sure that you had set up the Access Policies on your Azure Key vault.</p><cite>Access Policy</cite></blockquote>



<p>Copy your Azure Key Vault name and the Secret name, we will use this in our Python function. </p>



<h2 class="wp-block-heading">Setting up Python Application</h2>



<p>Let&#8217;s set up our Python application. Create a directory msteams-webhook and create a file <strong>requirements.txt</strong> with the preceeding contents. </p>



<pre class="wp-block-code"><code>pymsteams&#91;async]
azure-identity
azure-keyvault-secrets</code></pre>



<p>Now run the commands below to create a virtual environment, activate the environment and then install the requirements in our environment. If you get a message like &#8220;WARNING: You are using pip version 19.2.3, however version 22.2.2 is available.&#8221;, you can upgrade pip by running the command <code>python -m pip install --upgrade pip</code></p>



<pre class="wp-block-code"><code>py -m venv .venv
.\.venv\Scripts\activate
py -m pip install -r requirements.txt</code></pre>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>You can learn more abou the Virtual Environment <a rel="noreferrer noopener" href="https://docs.python.org/3/library/venv.html" target="_blank">here</a>.</p><cite>Python Virtual Envrronment</cite></blockquote>



<h2 class="wp-block-heading">Get the Secret from Azure Key Vault</h2>



<p>Let&#8217;s create a new Python file <strong>keyvault_helper.py</strong> with the codes below. </p>



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



<h2 class="wp-block-heading">Send Message to Teams Channel</h2>



<p>To send a message to the Teams channel, we are going to use a package pymsteams. This has been already added to our requirements file. Let&#8217;s create a file <strong>notify_teams.py</strong> with the codes below. </p>



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



<p>As we have all the set up done, let&#8217;s create main.py file with the codes below to start sending the messages to the Teams channel. </p>



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



<p>Before you run the main file, make sure that you have logged in to the terminal using az login with the account that you had set the access policy in your azure key vault. We can also set this access policies by using a managed identity, or a service principal, so that we can use it for more production scenarios. But for this post, let&#8217;s stick with a user account. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>If you get the error,  <code>VisualStudioCodeCredential: The current credential is not configured to acquire tokens for tenant</code> or <code>SharedTokenCacheCredential: The current credential is not configured to acquire tokens for tenant</code> you can set the values  <code>exclude_shared_token_cache_credential=True</code> or <code>exclude_visual_studio_code_credential=True</code> when you get tokens using <code>DefaultAzureCredential</code> in your <code>get_key_vault_secret</code> function in <code>keyvault_helper.py</code> file. </p><cite>Keyvault Error</cite></blockquote>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="/wp-content/uploads/2022/10/image-3.png"><img decoding="async" width="1024" height="201" src="/wp-content/uploads/2022/10/image-3-1024x201.png" alt="" class="wp-image-14598" srcset="/wp-content/uploads/2022/10/image-3-1024x201.png 1024w, /wp-content/uploads/2022/10/image-3-300x59.png 300w, /wp-content/uploads/2022/10/image-3-768x150.png 768w, /wp-content/uploads/2022/10/image-3.png 1302w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>default.py file from Azure Identity</figcaption></figure>
</div>


<p>By this time, we can hope that you will be able to get the secret from the key vault and now we can execute <code>py .\main.py</code> file. Once the file is executed, you should get a response in your cli as preceding. </p>



<pre class="wp-block-code"><code>{"version":"1.1","content":{"headers":&#91;{"key":"Content-Type","value":&#91;"text/plain; charset=utf-8"]}]},"statusCode":200,"reasonPhrase":"OK","headers":&#91;],"trailingHeaders":&#91;],"requestMessage":null,"isSuccessStatusCode":true}</code></pre>



<p>You should also get a message on your Teams channel.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="/wp-content/uploads/2022/10/image-4.png"><img decoding="async" width="1024" height="175" src="/wp-content/uploads/2022/10/image-4-1024x175.png" alt="" class="wp-image-14599" srcset="/wp-content/uploads/2022/10/image-4-1024x175.png 1024w, /wp-content/uploads/2022/10/image-4-300x51.png 300w, /wp-content/uploads/2022/10/image-4-768x131.png 768w, /wp-content/uploads/2022/10/image-4-1536x263.png 1536w, /wp-content/uploads/2022/10/image-4-2048x350.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>Message in Teams Channel</figcaption></figure>
</div>


<h1 class="wp-block-heading">Source Code</h1>



<p>You can also see the codes in <a href="https://github.com/SibeeshVenu/msteams-webhook-py" target="_blank" rel="noreferrer noopener">this repository</a>. </p>



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



<p>I am yet another developer who is passionate about writing and video creation. I have written more than 500 blogs on my <a rel="noreferrer noopener" href="https://sibeeshpassion.com/" target="_blank">blog</a>. If you like this content, consider following me here,</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>



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



<p>Thanks a lot for reading. Did I miss anything that you may think 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/post-messages-to-microsoft-teams-using-python/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 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>
		<item>
		<title>Secure Serverless Azure Functions AppSetting Using Key Vault</title>
		<link>https://www.sibeeshpassion.com/secure-serverless-azure-functions-appsetting-using-key-vault/</link>
					<comments>https://www.sibeeshpassion.com/secure-serverless-azure-functions-appsetting-using-key-vault/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Fri, 05 Jul 2019 16:19:46 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Azure App Settings]]></category>
		<category><![CDATA[Azure Function]]></category>
		<category><![CDATA[Azure Key Vault]]></category>
		<category><![CDATA[Read Key Vault Settings]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Use Key Vault in Azure Function]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=13769</guid>

					<description><![CDATA[[toc] Introduction Security is something that we all are worried about, especially if your applications are in cloud. If someone is able to see the credentials or client&#8217;s data, then you lose the trust you have with your customer. I know, that no one likes it. Fortunately, every cloud providers have already provided enough options to make your application safe and secure. Here, in this post, we are going to discuss about such a feature in Microsoft Azure. I hope you have already used Azure Functions, if you have not, I strongly recommend you to see some related articles here. [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>[toc]</p>



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



<p>Security is something that we all are worried about, especially if your applications are in cloud. If someone is able to see the credentials or client&#8217;s data, then you lose the trust you have with your customer. I know, that no one likes it. Fortunately, every cloud providers have already provided enough options to make your application safe and secure. Here, in this post, we are going to discuss about such a feature in Microsoft Azure. I hope you have already used Azure Functions, if you have not, I strongly recommend you to see some related articles <a href="https://sibeeshpassion.com/tag/azure-function/">here</a>. Here in this article, we will see how we can get the connection strings from Azure Key Vault and use it in our Azure Function instead of using it from the usual App Settings. Sounds interesting? So, let us do it. </p>



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



<p>It would be great if are familiar with the preceding topics.</p>



<ul class="wp-block-list"><li>Microsoft Azure</li><li>Server less</li><li>C#, or any other programming languages</li></ul>



<p>You should have a valid Azure Subscription to follow along.</p>



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



<p>As I was explaining, we are going to change the way we take the connection strings and other values in our Azure Function.  Below is my Azure Function sample code.</p>



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



<p>As you can see that this function has custom binding with my service bus as it uses  ServiceBusTrigger. The connection string name is  ServiceBusConnectionString  which we have already set in the Azure Function configuration.</p>



<figure class="wp-block-image"><img decoding="async" width="910" height="732" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Azure-Function-Configuration.png" alt="" class="wp-image-13770" srcset="/wp-content/uploads/2019/07/Azure-Function-Configuration.png 910w, /wp-content/uploads/2019/07/Azure-Function-Configuration-300x241.png 300w, /wp-content/uploads/2019/07/Azure-Function-Configuration-768x618.png 768w, /wp-content/uploads/2019/07/Azure-Function-Configuration-425x342.png 425w" sizes="(max-width: 910px) 100vw, 910px" /><figcaption>Azure Function Configuration</figcaption></figure>



<p>You should be able to see all of your configurations and application settings once you click on the configuration tab.</p>



<figure class="wp-block-image"><img decoding="async" width="650" height="834" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Azure-Function-All-Application-Settings.png" alt="" class="wp-image-13773" srcset="/wp-content/uploads/2019/07/Azure-Function-All-Application-Settings.png 650w, /wp-content/uploads/2019/07/Azure-Function-All-Application-Settings-234x300.png 234w, /wp-content/uploads/2019/07/Azure-Function-All-Application-Settings-425x545.png 425w" sizes="(max-width: 650px) 100vw, 650px" /><figcaption>Azure Function All Application Settings</figcaption></figure>



<p>Here in this post, we are going to change the value of ServiceBusConnectionString to the AzureKey Vault reference. This connection string is given by the customer, isn&#8217;t it always better to make it as secured as possible?</p>



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



<p>To get start, we should create an Azure Key Vault, please go to your Azure Portal and search with the keyword Key Vaults.</p>



<p>Once you had filled all the required information in the form, you can click on the create button.</p>



<figure class="wp-block-image"><img decoding="async" width="586" height="988" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Create-Key-Vault.png" alt="" class="wp-image-13774" srcset="/wp-content/uploads/2019/07/Create-Key-Vault.png 586w, /wp-content/uploads/2019/07/Create-Key-Vault-178x300.png 178w, /wp-content/uploads/2019/07/Create-Key-Vault-326x550.png 326w" sizes="(max-width: 586px) 100vw, 586px" /><figcaption>Create Azure Key Vault</figcaption></figure>



<p>If you are getting an error as shown in the image below, you should go to the Resource Provider section of your Subscription and then search for Microsoft.KeyValut and then click on register. </p>



<figure class="wp-block-image"><img decoding="async" width="624" height="513" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Subscription-Resource-Provider-Error.png" alt="" class="wp-image-13772" srcset="/wp-content/uploads/2019/07/Subscription-Resource-Provider-Error.png 624w, /wp-content/uploads/2019/07/Subscription-Resource-Provider-Error-300x247.png 300w, /wp-content/uploads/2019/07/Subscription-Resource-Provider-Error-425x349.png 425w" sizes="(max-width: 624px) 100vw, 624px" /><figcaption>Subscription Resource Provider Error</figcaption></figure>



<p>The key vault should be deployed in few seconds or minutes. Now you can go to your Key Vault. You can add Keys, Secrets, Certificates etc to your Key Vaults. But in this post, we are going to add a new Secret for our connection string. Click on the Secrets blade and then click on Generate/Import button. Now let us give all the details as shown in the below image. </p>



<figure class="wp-block-image"><img decoding="async" width="789" height="833" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Create-Secret-in-Azure-Key-Vault.png" alt="" class="wp-image-13775" srcset="/wp-content/uploads/2019/07/Create-Secret-in-Azure-Key-Vault.png 789w, /wp-content/uploads/2019/07/Create-Secret-in-Azure-Key-Vault-284x300.png 284w, /wp-content/uploads/2019/07/Create-Secret-in-Azure-Key-Vault-768x811.png 768w, /wp-content/uploads/2019/07/Create-Secret-in-Azure-Key-Vault-425x449.png 425w" sizes="(max-width: 789px) 100vw, 789px" /><figcaption>Create Secret in Azure Key Vault</figcaption></figure>



<h2 class="wp-block-heading">Set Key Vault Access Policy</h2>



<p>It is great that we have a Key Vault and secret, now we can give permission to our Azure Function application to retrieve this secrets from the Key Vault. But before you do that, you need to add a managed identity to the Azure Function. </p>



<h3 class="wp-block-heading">Add a System Assigned Managed Identity to the Azure Function</h3>



<p>To create an identity, please go to the Platform Features of your Azure Function and click on Identity.</p>



<figure class="wp-block-image"><img decoding="async" width="651" height="408" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Identity-Option-in-Azure-Function.png" alt="" class="wp-image-13781" srcset="/wp-content/uploads/2019/07/Identity-Option-in-Azure-Function.png 651w, /wp-content/uploads/2019/07/Identity-Option-in-Azure-Function-300x188.png 300w, /wp-content/uploads/2019/07/Identity-Option-in-Azure-Function-425x266.png 425w" sizes="(max-width: 651px) 100vw, 651px" /><figcaption>Identity Option in Azure Function</figcaption></figure>



<p>Now let&#8217;s enable the System Assigned Identity.</p>



<figure class="wp-block-image"><img decoding="async" width="633" height="580" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Assign-System-Assigned-Identity.png" alt="" class="wp-image-13782" srcset="/wp-content/uploads/2019/07/Assign-System-Assigned-Identity.png 633w, /wp-content/uploads/2019/07/Assign-System-Assigned-Identity-300x275.png 300w, /wp-content/uploads/2019/07/Assign-System-Assigned-Identity-425x389.png 425w" sizes="(max-width: 633px) 100vw, 633px" /><figcaption>Assign System Assigned Identity</figcaption></figure>



<h3 class="wp-block-heading">Add Access Control</h3>



<p>To give the access policy, <a href="https://docs.microsoft.com/en-us/azure/key-vault/key-vault-secure-your-key-vault#data-plane-and-access-policies">first we should add a role assignment</a> with contributor role for our application. Now we should also be able to add new role assignment. To do so, go to the Access Control section of your Key Vault and click on Add a role assignment blade. </p>



<figure class="wp-block-image"><img decoding="async" width="482" height="1024" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Add-Role-Assignment-Option-482x1024.png" alt="" class="wp-image-13828" srcset="/wp-content/uploads/2019/07/Add-Role-Assignment-Option-482x1024.png 482w, /wp-content/uploads/2019/07/Add-Role-Assignment-Option-141x300.png 141w, /wp-content/uploads/2019/07/Add-Role-Assignment-Option-259x550.png 259w, /wp-content/uploads/2019/07/Add-Role-Assignment-Option.png 650w" sizes="(max-width: 482px) 100vw, 482px" /></figure>



<h3 class="wp-block-heading">Add Access Policy</h3>



<p>Now that we have created a managed identity and a role assignment,  we should be able to add the Access Polity in the Key Vault for our Azure Function. Go to your Key Vault and click on Access Policies and then click on Add new blade. </p>



<figure class="wp-block-image"><img decoding="async" width="600" height="866" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Add-Access-Policy-With-Managed-Service-Identity-Created-Option.png" alt="" class="wp-image-13830" srcset="/wp-content/uploads/2019/07/Add-Access-Policy-With-Managed-Service-Identity-Created-Option.png 600w, /wp-content/uploads/2019/07/Add-Access-Policy-With-Managed-Service-Identity-Created-Option-208x300.png 208w, /wp-content/uploads/2019/07/Add-Access-Policy-With-Managed-Service-Identity-Created-Option-381x550.png 381w" sizes="(max-width: 600px) 100vw, 600px" /></figure>



<p>Please makes sure to use the, Object ID of the System Assigned Manged Identity that we have already created, here in the Select Principal column. This will make sure that our Azure Function has access to get the value from the Azure Key Vault.</p>



<p>At the end, you should have access policies as below. </p>



<figure class="wp-block-image"><img decoding="async" width="651" height="379" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/All-Access-Policies.png" alt="" class="wp-image-13831" srcset="/wp-content/uploads/2019/07/All-Access-Policies.png 651w, /wp-content/uploads/2019/07/All-Access-Policies-300x175.png 300w, /wp-content/uploads/2019/07/All-Access-Policies-425x247.png 425w" sizes="(max-width: 651px) 100vw, 651px" /></figure>



<h2 class="wp-block-heading">Use the Key Vault Secret in Azure Function</h2>



<p>Now that we have created our secret, and set up the access policy, we can get the secret identifier, to get this, please click on the secret you had created and then version. You should see a window as below.</p>



<figure class="wp-block-image"><img decoding="async" width="634" height="975" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Secret-Identifier-in-Azure-Key-Vault.png" alt="" class="wp-image-13776" srcset="/wp-content/uploads/2019/07/Secret-Identifier-in-Azure-Key-Vault.png 634w, /wp-content/uploads/2019/07/Secret-Identifier-in-Azure-Key-Vault-195x300.png 195w, /wp-content/uploads/2019/07/Secret-Identifier-in-Azure-Key-Vault-358x550.png 358w" sizes="(max-width: 634px) 100vw, 634px" /><figcaption>Secret Identifier in Azure Key Vault</figcaption></figure>



<p>The good thing is you can even provide the activation date and the expiry date. Isn&#8217;t it handy? Copy the value and then go to the configuration page of your Azure Function and then click on the app settings that you want to change, in my case the app setting with the key ServiceBusConnectionString. Now, you should see an option to edit the value there. Now you can give the value there as below. </p>



<pre class="wp-block-code"><code>@Microsoft.KeyVault(SecretUri=Secret URI with version)</code></pre>



<figure class="wp-block-image"><img decoding="async" width="651" height="116" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Add-App-Setting-from-Key-Vault.png" alt="" class="wp-image-13780" srcset="/wp-content/uploads/2019/07/Add-App-Setting-from-Key-Vault.png 651w, /wp-content/uploads/2019/07/Add-App-Setting-from-Key-Vault-300x53.png 300w, /wp-content/uploads/2019/07/Add-App-Setting-from-Key-Vault-425x76.png 425w" sizes="(max-width: 651px) 100vw, 651px" /></figure>



<p>Remember to save the changes and you get the notification as &#8221; Successfully updated web app settings &#8220;. That&#8217;s it. Now you should be able to run your Azure Functions, the only difference is that we are getting the app settings value from the Key Vault. </p>



<p>By chance if you get an error as &#8220;<a href="https://stackoverflow.com/questions/56904926/the-function-runtime-is-unable-to-start-microsoft-azure-servicebus-value-for-t">The function runtime is unable to start. Microsoft.Azure.ServiceBus: Value for the connection string parameter name &#8216;@Microsoft.KeyVault</a>&#8220;, please make sure that the <a href="https://docs.microsoft.com/en-us/azure/app-service/app-service-key-vault-references#reference-syntax">right format</a> <strong>@Microsoft.KeyVault(SecretUri=Secret URI with version)</strong> in the Value column.</p>



<figure class="wp-block-image"><img decoding="async" width="650" height="458" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Azure-Function-Key-Vault-Error.png" alt="" class="wp-image-13778" srcset="/wp-content/uploads/2019/07/Azure-Function-Key-Vault-Error.png 650w, /wp-content/uploads/2019/07/Azure-Function-Key-Vault-Error-300x211.png 300w, /wp-content/uploads/2019/07/Azure-Function-Key-Vault-Error-425x299.png 425w" sizes="(max-width: 650px) 100vw, 650px" /><figcaption>Azure Function Key Vault Error</figcaption></figure>



<p>Or if you get any error as &#8220;<a href="https://stackoverflow.com/questions/40298883/value-cannot-be-null-parameter-name-uristring/56906284#56906284">Value can not be null. Parameter name: uriString</a>&#8220;, please make sure that you have done the Access Policy configuration correctly. </p>



<figure class="wp-block-image"><img decoding="async" width="688" height="316" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Secret-Error.png" alt="" class="wp-image-13789" srcset="/wp-content/uploads/2019/07/Secret-Error.png 688w, /wp-content/uploads/2019/07/Secret-Error-300x138.png 300w, /wp-content/uploads/2019/07/Secret-Error-425x195.png 425w" sizes="(max-width: 688px) 100vw, 688px" /><figcaption>Secret Error</figcaption></figure>



<h2 class="wp-block-heading">Run Azure Function</h2>



<p>Now you can go to your Azure Functions and try to run any one of them which uses the app settings that we have configured. If everything is fine, you should be able to run the Function and should get some responses. </p>



<figure class="wp-block-image"><img decoding="async" width="650" height="580" src="https://sibeeshpassion.com/wp-content/uploads/2019/07/Azure-Function-Run-Result.png" alt="" class="wp-image-13832" srcset="/wp-content/uploads/2019/07/Azure-Function-Run-Result.png 650w, /wp-content/uploads/2019/07/Azure-Function-Run-Result-300x268.png 300w, /wp-content/uploads/2019/07/Azure-Function-Run-Result-425x379.png 425w" sizes="(max-width: 650px) 100vw, 650px" /></figure>



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



<p>In this article, we have learned,</p>



<ul class="wp-block-list"><li>About Azure Function and it&#8217;s configuration</li><li>How to configure Azure Key Vault</li><li>How to set Managed Identity for an Azure Function application</li><li>How to create Role Assignments in Key Vault Access Control</li><li>How to create Access policies for the managed identity</li><li>How to use Azure Key vault secret in the Azure Function app settings </li></ul>



<p>Please let me know what else you had learned from this Article.</p>



<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 as useful? Kindly do not forget to share me your feedback.</p>



<p>Kindest Regards<br>Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/secure-serverless-azure-functions-appsetting-using-key-vault/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
