{"id":50893,"date":"2024-03-14T10:05:00","date_gmt":"2024-03-14T17:05:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=50893"},"modified":"2024-03-14T10:05:00","modified_gmt":"2024-03-14T17:05:00","slug":"generate-dotnet-secrets-automatically-from-azure-deployment","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/generate-dotnet-secrets-automatically-from-azure-deployment\/","title":{"rendered":"Generate Local .NET Secrets from Azure Deployments"},"content":{"rendered":"<p>Often sample projects starts with a few &#8220;magic strings&#8221;, those variables contains URLs and key information related to a deployment or external resources that we will have to change to use the sample. As an example in .NET it could look like this: <\/p>\n<pre><code class=\"language-csharp\">string openAIEndpoint = \"https:\/\/\";\nstring openAIDeploymentName = \"my-ai-model\";\nstring openAiKey = \"123456abcd789EFGH\";\n\/\/ ...<\/code><\/pre>\n<p>This post shows how you to automatically generate .NET secrets from the Azure deployment and how your .NET app can read them. Users who would like to try your sample won&#8217;t have to edit anything anymore! They will only have to deploy using <code>azd up<\/code>, and then <code>dotnet run<\/code> to execute the app. Sound interesting? Here are to implement it. <\/p>\n<blockquote>\n<p>The code for the entire project can be found on GitHub, in the <a href=\"https:\/\/github.com\/FBoucher\/hikerai\">fboucher\/hikerai<\/a>.<\/p>\n<\/blockquote>\n<h2>The Preparation<\/h2>\n<p>Bicep is a language for creating infrastructure definitions for Azure resources that you want to deploy. Since these resources will have information like endpoints or model names, we want a way to export those, and in Bicep we use the <code>output<\/code> syntax:<\/p>\n<pre><code class=\"language-bash\">\/\/ some bicep deployment...\n\noutput AZURE_OPENAI_ENDPOINT string = ai.outputs.AZURE_OPENAI_ENDPOINT\noutput AZURE_OPENAI_GPT_NAME string = ai.outputs.AZURE_OPENAI_GPT_NAME\noutput AZURE_OPENAI_KEY string = ai.outputs.AZURE_OPENAI_KEY<\/code><\/pre>\n<p>With the Azure Developer CLI (azd) the command <code>azd env get-values<\/code> returns all those values in list of key-paired values. A PowerShell or Bash script could read those and create new .NET secrets using the command <code>dotnet user-secrets set<\/code>. Here the script <code>postprovision.ps1<\/code>.<\/p>\n<pre><code class=\"language-powershell\">function Set-DotnetUserSecrets {\n    param ($path, $lines)\n    Push-Location\n    cd $path\n    dotnet user-secrets init\n    dotnet user-secrets clear\n    foreach ($line in $lines) {\n        $name, $value = $line -split '='\n        $value = $value -replace '\"', ''\n        $name = $name -replace '__', ':'\n        if ($value -ne '') {\n            dotnet user-secrets set $name $value | Out-Null\n        }\n    }\n    Pop-Location\n}\n\n$lines = (azd env get-values) -split \"`n\"\nSet-DotnetUserSecrets -path \".\" -lines $lines<\/code><\/pre>\n<p>This script start by creating a function <code>Set-DotnetUserSecrets<\/code> that takes two parameters. The first parameter <code>$path<\/code> will be used so the script can change directory to that location. This is essential to make sure the secrets are created where needed. The second parameter <code>$lines<\/code> contains all the substring where key-paired values are saved (ex: VARIABLE_NAME=&#8221;variable_value&#8221;). The script loops through all lines to isolate the names and the values and create a secret for each of them <code>dotnet user-secrets set $name $value<\/code>.<\/p>\n<p>On the two last lines, the script retrieves the environment variable generated by the outputs using <code>azd env get-values<\/code> and split the result in substring. It will finally call the function <code>Set-DotnetUserSecrets<\/code> declared previously passing the the path and lines. A <code>postprovision.sh<\/code> version of the script is also available in the repository.<\/p>\n<p>To execute the script after the deployment we need to add a post provision hook into the <code>azure.yaml<\/code> file. The <code>azure.yaml<\/code> is the schema that defines and describes the apps and types of Azure resources that are included in a project. Here how it looks.<\/p>\n<pre><code class=\"language-yaml\">hooks:\n  postprovision:\n    windows:\n      shell: pwsh\n      run: .\/infra\/post-script\/postprovision.ps1\n      interactive: true\n      continueOnError: true<\/code><\/pre>\n<h2>The deployment<\/h2>\n<p>Execute your deployment using Azure CLI Developer CLI command <code>azd up<\/code>. You can use the code of HikerAI available at <a href=\"https:\/\/github.com\/FBoucher\/hikerai\">fboucher\/hikerai<\/a>. Once you clone or download the repository, make sure you are at the root directory, to deploy the solution.<\/p>\n<p>To test that the secrets have been created used the command <code>dotnet user-secrets list<\/code>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2024\/03\/secrets-list.png\" alt=\"result of the secrets-list command\" \/><\/p>\n<h2>Use the Secrets<\/h2>\n<p>Using the NuGet package <code>Microsoft.Extensions.Configuration<\/code>, the application will be able to read the user secrets. You can now replace those magic strings.<\/p>\n<pre><code class=\"language-csharp\">\/\/ == Retrieve the local secrets saved during the Azure deployment ==========\nusing Microsoft.Extensions.Configuration;\n\nvar config = new ConfigurationBuilder().AddUserSecrets&lt;Program&gt;().Build();\nstring openAIEndpoint = config[\"AZURE_OPENAI_ENDPOINT\"];\nstring openAIDeploymentName = config[\"AZURE_OPENAI_GPT_NAME\"];\nstring openAiKey = config[\"AZURE_OPENAI_KEY\"];<\/code><\/pre>\n<h2>Video<\/h2>\n<p><iframe width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/NpE7edalTlQ?si=f6GmyhteooCAKVhz\" allowfullscreen><\/iframe><\/p>\n<h2>Summary<\/h2>\n<p>This post shared a few tips to replace &#8220;magic strings&#8221; by user secrets that can be automatically generates from the Azure deployment outputs. Try it in your solution or with our sample available ay <a href=\"https:\/\/github.com\/FBoucher\/hikerai\">fboucher\/hikerai<\/a> in the HikerAI folder.<\/p>\n<h3>References<\/h3>\n<ul>\n<li>All code from sample HikerAI <a href=\"https:\/\/github.com\/FBoucher\/hikerai\">fboucher\/hikerai<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/azure\/developer\/azure-developer-cli\/overview\">Azure Developer CLI<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/aspnet\/core\/security\/app-secrets?view=aspnetcore-8.0&amp;tabs=windows#secret-manager\">App secrets in development<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This post shows how to automatically generate .NET secrets that a .NET app can use from the Azure deployment.<\/p>\n","protected":false},"author":149092,"featured_media":50894,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685,7509,327],"tags":[4,7788,37],"class_list":["post-50893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","category-aspnetcore","category-azure","tag-net","tag-azd","tag-azure"],"acf":[],"blog_post_summary":"<p>This post shows how to automatically generate .NET secrets that a .NET app can use from the Azure deployment.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/50893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/149092"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=50893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/50893\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/50894"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=50893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=50893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=50893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}