Access KeyVault from AzureFunciton with public network disabled

previousversiondocs 61 Reputation points
2023-04-19T09:41:41.1266667+00:00

I have an azure function that needs to access key-vault.

The code looks like this:

credentials = DefaultAzureCredential()
secret = secret_client.get_secret(secret_name)

Locally, It works with the following settings:
KeyVault -> Settings -> Networking -> Allow public access (either my local IP or all networks, both works)

But I don't want to allow all public networks to be able to access the vault.

When disabling "Allow public access" and deploying the Function to Azure, I get this error:
(Forbidden) Public network access is disabled and request is not from a trusted service nor via an approved private link.

I expected such error when trying to run it locally, because my local IP is obviously not allowed. But i expected it to work when deployed, because i thought, the AzureFunction was a "trusted service", but it is not listed here.

Also, I have added role-based access and I have added the Azure-Function. As said, this works when I allow public access, which I don't want. Is there any way to restrict public access to the vault but make it available for the AzureFunction?

Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,448 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JamesTran-MSFT 36,906 Reputation points Microsoft Employee Moderator
    2023-04-19T20:47:14.9133333+00:00

    @previousversiondocs

    Thank you for your post!

    Error Message:

    (Forbidden) Public network access is disabled and request is not from a trusted service nor via an approved private link.
    

    I understand that you're trying to access your Key Vault from your Azure Functions App and are running into the above error message due to your Vault's public network being disabled. When it comes to troubleshooting your issue, I'll share some options below to hopefully point you in the right direction.

    Public Access Disabled:

    Managed identities Azure Functions:

    Allow public access from specific virtual networks and IP addresses:

    Key Vault Access Policies:

    I hope this helps!

    If you have any other questions, please let me know. Thank you for your time and patience throughout this issue.


    Additional Link:


    If the information helped address your question, please Accept the answer. This will help us and also improve searchability for others in the community who might be researching similar information.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.