Press ESC to close

Azure KeyVault Integration in D365FO

Azure KeyVault is a cloud service provided by azure which allows us to store and manage the sensitive data like passwords, API keys, and encryption keys etc used by our applications.  

Challenge: Our D365FO application rely on various third-party APIs, each requiring unique API keys for access. These keys are sensitive credentials that need to be handled with utmost care to prevent unauthorized access and potential security breaches.  

Current Approach: We currently store these API keys directly within our applications, This poses a significant security risk and creates challenges in managing and rotating these keys.  

Desired Outcome: We need a secure and centralized solution to manage and access these API keys, ensuring keys are stored in secure environment, minimizing the risk of unauthorized access.  

Now let’s see how we can create KeyVault, store secrets and retrieve it in our D365FO application.  

1) Create and Setup a new Azure KeyVault  

Go to the Azure Subscription you have on Azure Portal. Search for KeyVaults page.  

image-171.png  

. Resource Group: Select an existing resource group or create a new one by clicking “Create new” and entering a desired name.  

. Name: Give your KeyVault a name that identifies its purpose.  

. Location: Choose a region where your KeyVault will be deployed.  

After filling the necessary details. click the “Review + create” button.  

image-172.png  

You can see the deployment is completed successfully.  

image-174.png  


Copy the KeyVault URI we just created, we will use it inside our D365FO application.  

image-172.png  

Navigate To Secrets on the left side of the menu for storing your secrets.  

image-172.png  

When you open a Secrets window, you will see there is an error message that says “You are unauthorized to view these contents” indicates that your user account lacks the necessary permissions to access KeyVault data.  

image-173.png  

To solve this, Navigate to Access configuration on the left side of the menu.  

image-186.png  

Select the Permission model to “Vault access policy” which applies access control exclusively to KeyVault itself, Click “Apply” to save changes.  

image-187.png  

Navigate to Access Policies on the left side of the menu. Click on “Create” button to create a new access policy.  

image-170.png  

For now, I am assigning all permissions for Key permissions, Secret permissions and Certificate permissions. Click the “Next” button.  

image-183.png  

On the Principal window, select the user that you want to grant access to your KeyVault. Click the “Next” button.  

image-174.png  

On the Review + create tab, you will see the permissions previously assigned, Click on the     
“Create” button. Navigate to Secrets page, now we can see the error message is no longer here and we can “Generate/Import” the secrets.  

image-179.png  

2) App Registration  

Let’s register an app which allows us to access our Azure KeyVault from D365FO.  

On the Azure Portal, search for App Registration.  

image-178.png  

Click on the “New registration” button.  

image-176.png  

.Name: Give your App a name.  

.Supported account types: We will select the “Accounts in this directory” option which allows access to this app for your tenant.   

Click on “Register” button. We will copy the Client ID and Tenant ID that we will be using in the D365FO application.  

image-187.png  

Navigate to Certificates & secrets on the left side of the menu.  

image-182.png  

We will create a client secret by clicking on the “New client secret” button.  

image-180.png  

Copy the Value which is the “Secret ID”.  

image-177.png  

3) Assign Permission  

Let’s assign permission from our KeyVault that we created to access the app we registered in the previous step.  

Navigate to the KeyVault you created. Go to Access policies on the left side of the menu.  

image-181.png  

Create a new access policy by clicking the “Create” button. Assign the permissions for our KeyVault.  

image-182.png  

On the Principal tab, select the application you registered in the previous step.  

image-175.png  

As you can see, we have assigned the access policy for our application.  

image-177.png  

4) Configure in D365FO  

Open the D365FO environment you have.   

Navigate to System administration > Setup. Open the Key Vault parameters form.  

image-171.png  

Fill the necessary details we copied earlier.  

Name: Give it some name.  

Key Vault URL: KeyVault URI.  

Key Vault Client: App Registration- ClientID  

Key Vault secret key: App Registration-Secret Value  

image-184.png  

Now we can define our secrets. Format for the Secret field should be written in the below format.  

vault://[keyvaultname]/secretname/[secretversion]  

If you don’t specify a secret version, it will use the latest one.  

Set the Secret Type as Manual.  

image-185.png  

In this manner, we can define as many secrets we created in the Azure KeyVault.  

5) Retrieve Secrets Using X++  

Let’s see how can we retrieve secrets from Azure KeyVault using X++.private static str  

getKeyVaultSecretValue(str _keyvaultSecretName)
{
    KeyVaultCertificateTable certificateTable = KeyVaultCertificateTable::findByName(_keyvaultSecretName);
    str keyvaultValue = KeyVaultCertificateHelper::getManualSecretValue(certificateTable.RecId);
    return keyvaultValue;

}

In this manner, we can retrieve any secret defined in the Azure KeyVault.  

That’s it folks! Happy TechJourney.