{"content":"Environment variables in Dataverse are a powerful vehicule to develop portable customizations between different environment. Recently, a new Secret Data Type was introduced that enables the use of secrets stored in Azure Key Vaults 🔐 .\nThe integration between Dataverse and Azure Key Vault was long-awaited and I am really happy to see it materialize. It brings new kind of use cases and an additional security layer to protect sensitive information needed in platform customizations.\nIn this post, I will share my findings setting up the key vaults. And, while most of the documentation and videos out there are showing how to consume secret environment variables in PowerAutomate Flows, I will focus on their usage inside Dataverse Plugin code.\nAzure Key Vault setup The first step is to setup an Azure Key Vault to hold the secrets and give Dataverse environments the right to read the secrets stored in the vault.\nThe official documentation gives really good instructions on how to configure your key vaults so I will not repeat everything here. The main steps are :\nEnable Microsoft.PowerPlatform as a resource provider in your Azure subscription Create a Key Vault Give proper security role to the Dataverse application There are 2 permission model available in a Key Vault\nVault access policy Azure role-based access control (RBAC) The official documentation assumes that the permission model of the Key Vault is \u0026rsquo; Vault access policy\u0026rsquo; follow the instructions if that is your case.\nIf you use Role-based access control ( RBAC), you need to grant the Key Vault Secrets User role to the Dataverse application. Here\u0026rsquo;s how to do it in the Azure portal :\nHead to the Access control (IAM) blade and add a Role Assignment, and select Key Vault Secrets User.\nYou will be prompted to select the members. Type Dataverse in the search box and the Dataverse application service principal will be proposed. Select the Dataverse application and save.\nIn the Access control (IAM), you can now assess that the Dataverse application as proper access to ther Key Vault.\n🔔 Be aware that once you granted the read secrets permissions to the Dataverse application on a given Key Vault, all the Dataverse environments in your tenant are entitled to read secrets from this vault.\nSo as a best practice, its a good idea to have dedicated Key Vaults for Dataverse secrets usage only and don\u0026rsquo;t mix up secrets from other systems.\nIt\u0026rsquo;s also recommended to have seperate key vaults for all your different environments (ex. DEV, QA, PROD)\nCreate a Secret in the Key Vault For our example, we will create a secret with the following properties.\nName : TopSecret Value : \u0026lsquo;🔐 For Your Eyes 👀 Only 🔐\u0026rsquo; Given that your user has admin rights on the key vault secrets, head to the Secrets blade of the Key Vault and select Generate/Import.\nNotice that you can even set an an activation and/or expiration date. That\u0026rsquo;s a feature that is not possible using a normal Environment variable. This could be quite useful in certain scenarios.\nCreate a Secret Environment Variable We\u0026rsquo;re almost there. Now we need to create an Environment Variable of type Secret in Dataverse that will reference the secret that we just created in the key vault.\nSwitch to the PowerApps Maker portal and open a solution. Here, I created a solution called \u0026lsquo;KeyVault Test\u0026rsquo; and added an Environment Variable from the \u0026rsquo; New\u0026rsquo; option in the top menu.\nNote : The user who creates the environment variable must have read permission on the specific key vault. This provides an additional layer of security\nBe sure to choose the \u0026rsquo; Secret\u0026rsquo; data type and \u0026lsquo;Azure Key Vault\u0026rsquo; as the Secret Store. Then click on New Azure Key Vault secret reference where you\u0026rsquo;ll be asked to enter the info needed to resolve your key.\nAzure subscription Id Resource Group Name Azure Key Vault Name Secret Name : we will use \u0026lsquo;TopSecret\u0026rsquo; wich is the name of the secret defined earlier Once saved, the Environment Variable will only hold the reference to the secret in the key vault without storing its value inside your Dataverse environment. Think of it as a pointer.\nThe reference to the key vault secret will be stored in the EnvironmentVariableValue table using this form :\n/subscriptions/{subscriptionid}/resourceGroups/{resourcegroupname}/providers/Microsoft.KeyVault/vaults/{keyvaultname}/secrets/{secretname} Retrieve the Secret Value In order to retrieve the Environment variable secret value, the platform exposes an unbound Custom Api called RetrieveEnvironmentVariableSecretValue that can be called at runtime inside customizations.\nOne of the easiest way to test this API is to fire-up the XrmToolBox and open the Custom API Tester tool by Jonas Rapp.\nSelect the RetrieveEnvironmentVariableSecretValue custom API and set the name of the variable to fetch as the EnvironmentVariableName input. Execute the API and the secret value will be received in the EnvironmentVariableSecretValue output, as seen in the image below.\nSince the Custom API is adressable, it\u0026rsquo;s also possible to make a direct call to the Dataverse web API to retrieve the secret value.\nPOST =\u0026gt; https://{{baseurl}}/api/data/v9.2/RetrieveEnvironmentVariableSecretValue BODY : { \u0026#34;EnvironmentVariableName\u0026#34; : \u0026#34;{VariableName}\u0026#34; } Using Secrets in Plugin code Awesome, now let see how we can leverage the usage of a Secret Environment Variable in a Dataverse Plugin.\nIt\u0026rsquo;s only a matter of making a call to the RetrieveEnvironmentVariableSecretValue API inside the code of the plugin.\nHere\u0026rsquo;s an example using Late Bound coding style. You need to create an OrganizationRequest object and set the EnvironmentVariableName parameter. Upon execution of the request, the secret value will be found in the EnvironmentVariableSecretValue Results collection of the response.\nI personally prefer the Early Bound coding style for my plugin development. Early bound classes can be generated not only for Tables (Entities) but also for Custom Actions/API.\nMy weapon of choice for early bound classes generation is the spkl Task Runner by Scott Durow. Just add RetrieveEnvironmentVariableSecretValue in the \u0026quot; actions\u0026quot; section of the spkl.json configuration file.\nThis will produce specialized RetrieveEnvironmentVariableSecretValueRequest and RetrieveEnvironmentVariableSecretValueResponse that can be used instead of the generic OrganizationRequest used in the late bound example. Difference here is that there are no magic strings only concrete objects with properly typed properties.\nNow, when deployed and registered on Create of a contact record, eighter of the plugins showed above will produce the following. I\u0026rsquo;m just throwing an error with the secret value.\nI used the technique showed above to enhance my own Dataverse-CustomApis collection community project. GitHub - drivardxrm/Dataverse-CustomApis: Collection of Dataverse Custom Apis Collection of Dataverse Custom Apis. Contribute to drivardxrm/Dataverse-CustomApis development by creating an account on GitHub. github.com In this project I expose a Custom API called GetEnvironmentVariable. This API takes the name of an environmenmt variable as an Input and returns a bunch of information on the variable. Most importantly, it casts the value accordingly depending on the type (String, Boolean, Number and now \u0026hellip; 🎉 Secret).\nAs you can see below, when the GetEnvironmentVariable API is called with a variable Key of type Secret. The secret value is resolved in the ValueSecret output property.\nInstall the latest release of the solution if you want to try it.\nTakeaway I\u0026rsquo;m thrilled by the addition of the Secret data type for Dataverse Environment Variables and I see a lot of potential use cases in my current projects.\nThere are a lot of benefits to store secrets in Key vaults instead of Dataverse tables. Think about logging, monitoring, key rotation just to name a few.\nHere are some other great resources and use cases on the subject :\nEnvironment Variable Secrets - YouTube Access Azure Key Vault Secrets using Environment Variables. #CitizenCan E15 | 365.Training - YouTube Photo by George Becker from Pexels\n","date":"2022-01-25T05:21:28Z","image":"/azure-key-vault-secrets-in-dataverse/pexels-george-becker-333837.jpg","permalink":"/azure-key-vault-secrets-in-dataverse/","title":"Azure Key Vault Secrets in Dataverse"}