Node.js app on Azure App Service fails to fetch secrets from Azure Key Vault: “No MSI credential available”

I’m encountering an issue with my Node.js application deployed on Azure App Service. The app is unable to fetch secrets from Azure Key Vault due to a credential error.

Environment:

Azure App Service (Linux)
Node.js 20 LTS
Azure Key Vault for storing secrets

Issue:
When the application attempts to retrieve secrets from Key Vault, it fails with the following error:

Error in loadSecrets function:
Error name: CredentialUnavailableError
Error message: ManagedIdentityCredential: Authentication failed. Message ManagedIdentityCredential – No MSI credential available

Here’s the relevant part of my server.js file:

const { DefaultAzureCredential } = require("@azure/identity");
const { SecretClient } = require("@azure/keyvault-secrets");

const keyVaultName = process.env.KEY_VAULT_NAME;
const keyVaultUrl = `https://${keyVaultName}.vault.azure.net`;

async function loadSecrets() {
    try {
        require('dotenv').config();

        console.log('Starting loadSecrets function');
        console.log('NODE_ENV:', process.env.NODE_ENV);
        console.log('DATABASE_URL:', process.env.DATABASE_URL);
        console.log('KEY_VAULT_NAME:', process.env.KEY_VAULT_NAME);

        if (process.env.NODE_ENV === 'production') {
            console.log('Production environment detected. Attempting to load secrets from Key Vault.');

            console.log('Creating DefaultAzureCredential...');
            const credential = new DefaultAzureCredential();
            console.log('DefaultAzureCredential created successfully');

            const keyVaultUrl = `https://${process.env.KEY_VAULT_NAME}.vault.azure.net`;
            console.log('Key Vault URL:', keyVaultUrl);

            console.log('Creating SecretClient...');
            const secretClient = new SecretClient(keyVaultUrl, credential);
            console.log('SecretClient created successfully');

            console.log('Fetching secrets...');
            process.env.CLAUDE_API_KEY = (await secretClient.getSecret('CLAUDE-API-KEY')).value;
            console.log('CLAUDE-API-KEY fetched successfully');

            // ... [other secret fetching operations] ...

            console.log('All secrets loaded successfully from Key Vault');
        } else {
            console.log('Non-production environment. Skipping Key Vault secret loading.');
        }
    } catch (error) {
        console.error('Error in loadSecrets function:');
        console.error('Error name:', error.name);
        console.error('Error message:', error.message);
        // ... [additional error logging] ...
        throw error;
    }
}

// Load secrets before starting the server
loadSecrets().then(() => {
    // Server setup and route definitions...
}).catch(error => {
    console.error('Failed to load secrets:', error);
    process.exit(1);
});

What I’ve Tried:

Verified that the system-assigned managed identity is enabled for the App Service.
Checked that the App Service’s managed identity has the necessary permissions (Get, List) for secrets in the Key Vault access policies.
Confirmed that the KEY_VAULT_NAME environment variable is correctly set in the App Service configuration.
Tried using DefaultAzureCredential instead of ManagedIdentityCredential.
Verified that the secrets exist in Key Vault with the correct names.

Azure Configuration:

System-assigned managed identity is enabled for the App Service.
The App Service has the “Key Vault Secrets User” role assigned in the Key Vault’s Access Control (IAM).
Key Vault is configured to use Azure role-based access control (RBAC).

Questions:

What could be causing the “No MSI credential available” error despite having the managed identity enabled?
Are there any additional configurations or permissions I might be missing?
How can I troubleshoot the managed identity authentication process?
Are there any known issues with Node.js 20 LTS and the Azure Identity SDK that could be causing this?
Is there a way to verify if the managed identity is correctly associated with the App Service at runtime?

Any help or insights would be greatly appreciated. Thank you!

New contributor

user26559929 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật