{"id":58,"date":"2023-03-20T20:28:40","date_gmt":"2023-03-21T04:28:40","guid":{"rendered":"https:\/\/www.dumpsterfirecomputing.com\/?p=58"},"modified":"2023-03-20T20:29:56","modified_gmt":"2023-03-21T04:29:56","slug":"using-azure-key-vault-secrets-in-bicep","status":"publish","type":"post","link":"https:\/\/www.dumpsterfirecomputing.com\/?p=58","title":{"rendered":"Using Azure Key Vault Secrets in Bicep"},"content":{"rendered":"\n<p>In last week&#8217;s learning, I mentioned that I&#8217;d figured out how to utilize Azure Key Vault secrets in a bicep deployment.  This isn&#8217;t exactly earth shattering, but I thought it was fun.  The practical applications are numerous.  In last week&#8217;s case, I was building an IPSec VPN tunnel and wanted to have the pre-shared key available in key vault as I built the VPN tunnel and the rest of the infrastructure.<\/p>\n\n\n\n<p>Getting there takes a couple steps&#8230;<\/p>\n\n\n\n<p>To start, let me just say that <a rel=\"noreferrer noopener\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/azure-resource-manager\/bicep\/key-vault-parameter?tabs=azure-cli\" target=\"_blank\">Microsoft does have good documentation<\/a> around this, but I found it took some hands-on to truly understand what I was doing, and how the darn thing was supposed to work.<\/p>\n\n\n\n<p>For this example, I&#8217;m going to provide the bare minimum code necessary to show the process.  I&#8217;m going to extract a secret from Key Vault and use that secret name to build a virtual network.  Ludicrous, right?  What we <em>do <\/em>with the secret doesn&#8217;t really matter though &#8211; it&#8217;s how we get that secret and leverage it in code that matters.<\/p>\n\n\n\n<p>To start, I have a basic key vault named <code>kv-vault01<\/code> with a secret named &#8216;<code>networkname'<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1013\" height=\"250\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-6.png\" alt=\"\" class=\"wp-image-59\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-6.png 1013w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-6-300x74.png 300w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-6-768x190.png 768w\" sizes=\"auto, (max-width: 1013px) 100vw, 1013px\" \/><figcaption class=\"wp-element-caption\">Example Key Vault<\/figcaption><\/figure>\n\n\n\n<p>Be sure you&#8217;ve allowed ARM deployments to the vault &#8211; you&#8217;ll find that setting below.  Failing to do this will result in your code unable to pull secrets.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"784\" height=\"200\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-7.png\" alt=\"\" class=\"wp-image-60\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-7.png 784w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-7-300x77.png 300w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-7-768x196.png 768w\" sizes=\"auto, (max-width: 784px) 100vw, 784px\" \/><figcaption class=\"wp-element-caption\">Allow Azure Resource Manager for template deployment<\/figcaption><\/figure>\n\n\n\n<p>That&#8217;s all the pre-requisite work.  Now we&#8217;ll build two bicep files, and I&#8217;m going to describe them in reverse order from how they&#8217;re called.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"562\" height=\"350\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-8.png\" alt=\"\" class=\"wp-image-61\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-8.png 562w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-8-300x187.png 300w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><figcaption class=\"wp-element-caption\">vnet.bicep<\/figcaption><\/figure>\n\n\n\n<p>The role of this file is to build a virtual network using a minimum set of parameters.  Line 1 and 2 combine to create the <em>secure parameter<\/em> called <code>name<\/code>, the value of which will ultimately come from the key vault secret.  This seems to be the only way to actually <em>use<\/em> a secret from key vault.  It must be declared as a secure parameter.<\/p>\n\n\n\n<p>So how do we get to this file?  Think of that as a separate function or module, and we&#8217;ll call it from a main bicep file that will be used in the deployment.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"261\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-9.png\" alt=\"\" class=\"wp-image-62\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-9.png 592w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-9-300x132.png 300w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><figcaption class=\"wp-element-caption\">main.bicep<\/figcaption><\/figure>\n\n\n\n<p>In the first block (lines 1 through 3), we&#8217;re simply making a reference to an existing key vault named <code>kv-vault01<\/code>.<\/p>\n\n\n\n<p>On line 5, you can see that instead of using the<code> 'resource' <\/code>identifier, we&#8217;re using <code>'module' <\/code>and calling the name of a bicep file.  Lines 7 through 10 you can see the parameters that the <code>'vnet.bicep'<\/code> module wants, and line 8 is how we get the key vault secret.<\/p>\n\n\n\n<p>It&#8217;s important to state that the <code>getSecret()<\/code> function can really only be used in this context.  You can get the secret only when passing that secret as a parameter to a module.  So anytime you may want to use a secret, be sure to split out your bicep code so that you&#8217;re getting the secret and passing it off to a module.<\/p>\n\n\n\n<p>If you&#8217;re not familiar with how to run the above, it&#8217;s actually quite straightforward:<\/p>\n\n\n\n<p><code>New-AzResourceGroupDeployment -Name \"deployment-name\" -ResourceGroupName \"rg-tester\" -TemplateFile .\\main.bicep<\/code><\/p>\n\n\n\n<p>The deployment name (&#8220;deployment-name&#8221; in my example) can be anything and the resource group name (&#8220;rg-tester&#8221; in my example) needs to be a pre-existing resource group.  If you&#8217;re following along, and have the pre-requisites taken care of, you should get a successful deployment:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"177\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-10.png\" alt=\"\" class=\"wp-image-63\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-10.png 350w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-10-300x152.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"896\" height=\"181\" src=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-11.png\" alt=\"\" class=\"wp-image-64\" srcset=\"https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-11.png 896w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-11-300x61.png 300w, https:\/\/www.dumpsterfirecomputing.com\/wp-content\/uploads\/2023\/03\/image-11-768x155.png 768w\" sizes=\"auto, (max-width: 896px) 100vw, 896px\" \/><\/figure>\n\n\n\n<p>In this example the value of the key vault secret was indeed <code>secretnetwork<\/code>, so that worked precisely as expected.  It&#8217;s not fancy, it&#8217;s not flashy, but it works.  <a href=\"https:\/\/ironscripter.us\/factions\/\" target=\"_blank\" rel=\"noreferrer noopener\">#BattleFaction<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In last week&#8217;s learning, I mentioned that I&#8217;d figured out how to utilize Azure Key Vault secrets in a bicep deployment. This isn&#8217;t exactly earth shattering, but I thought it [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[10,13,11,9,12],"class_list":["post-58","post","type-post","status-publish","format-standard","hentry","category-azure","category-bicep","tag-azure","tag-battlefaction","tag-bicep","tag-key-vault","tag-secrets"],"_links":{"self":[{"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/posts\/58","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=58"}],"version-history":[{"count":1,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=\/wp\/v2\/posts\/58\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dumpsterfirecomputing.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}