1

I've deployed a nodeJs app to a Linux Azure AppService. Now I would like to list the server settings of that same app-service. Under the Identity tab I enabled the managed Identity for this AppService. In my NodeJs App I've tried the following:

const { DefaultAzureCredential } = require("@azure/identity");

const credential = new DefaultAzureCredential();

credential.getToken().then(token => {
  ...
});

I'm not really sure what this is doing, but I don't think it connects, because the getToken never resolves. Any suggestions what I'm missing here?

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

1 Answers1

2

If you want to get server setting values inside of the app service, you can just try process.env.NODE_ENV as this doc indicated. Instead of calling Azure management API.

If you want to get server setting values outside of the app service, your code can't access server setting values directly, so you should call Azure management API. If you have some problem with DefaultAzureCredential, you can try ClientSecretCredential. Just try the code below:

const { ClientSecretCredential } = require("@azure/identity");

const fetch = require("node-fetch")

let  tenantId='';
let  clientID = '';
let clientSecret = '';

let subscriptionID = ''
let resourceGroup = ''
let appServiceName = ''

new ClientSecretCredential(tenantId,clientID,clientSecret).getToken(['https://management.azure.com/.default']).then(result=>{
    accessToken = result.token
    reqURL = `https://management.azure.com/subscriptions/${subscriptionID}/resourceGroups/${resourceGroup}/providers/Microsoft.Web/sites/${appServiceName}/config/appsettings/list?api-version=2019-08-01`
    fetch(reqURL, {
        method: 'post',
        headers: { 'Authorization': 'Bearer ' +  accessToken},
    })
    .then(res => res.json())
    .then(json => console.log(json));

})

Result :

enter image description here enter image description here

For how to create an Azure AD app and assign a role to it so that it could have permission to call Azure mgmt APIs, see this doc.

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16
  • My goal is to read and write settings. So, I agree, for reading you can use environment variables. But what about changing them, is all that AD app necessary? – Jeanluca Scaljeri Jun 02 '21 at 08:47
  • @JeanlucaScaljeri, yes, I think you should call Azure management API if you need to write/modify environment variables. – Stanley Gong Jun 02 '21 at 08:56
  • ok. but where can I find the **clientID** and **clientSecret** which you are using in your example? – Jeanluca Scaljeri Jun 02 '21 at 11:44
  • @JeanlucaScaljeri, you need to register an Azure AD app:https://learn.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal#register-an-application-with-azure-ad-and-create-a-service-principal so that you can get the clientID and create an secret :https://learn.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal#option-2-create-a-new-application-secret – Stanley Gong Jun 02 '21 at 12:50
  • @JeanlucaScaljeri after you finished this 2 steps, you need to assign subscription role to this app so that this app will have permission to query resources: https://learn.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal#assign-a-role-to-the-application – Stanley Gong Jun 02 '21 at 12:52
  • I have your code working, thanks! Is there also a sdk or lib for this that I can use, like ms-rest.js or something like this that you know of? – Jeanluca Scaljeri Jun 02 '21 at 18:47
  • 1
    For Azure management API, yes, see this : https://pypi.org/project/azure-mgmt-resource/ – Stanley Gong Jun 03 '21 at 00:58