Azure / azure-sdk-for-node

Azure SDK for Node.js - Documentation
https://github.com/Azure/azure-sdk-for-node#documentation
MIT License
1.19k stars 566 forks source link

How to get az-cli context or az-PowerShell context? #5199

Closed cveld closed 3 years ago

cveld commented 4 years ago

How can we get the current az-cli context or az-PowerShell context from within a nodejs Azure workload?

https://azure.github.io/azure-sdk-for-node/Documentation/Authentication.md currently gives a 404.

https://github.com/Azure/azure-sdk-for-node/blob/master/Documentation/Authentication.md does not describe a way to reuse the existing contexts.

Related: Can we run nodejs workloads within Azure Pipelines using an Azure Service Connection?

child_process.exec('az account get-access-token', (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
    var json = JSON.parse(stdout);
    console.log(`accessToken: ${json.accessToken}`)
    main(json.accessToken);
});

I am able to fetch the access token. But how do I initialize the library with this?

update

I read the docs on https://github.com/Azure/ms-rest-nodeauth and there actually is a built-in feature to work with the az-cli!

import { AzureCliCredentials } from "@azure/ms-rest-nodeauth";
const creds = await AzureCliCredentials.create();
const client = new ServiceClient(creds);

Remaining question: How to get the currently selected subscriptionId?

ramya-rao-a commented 3 years ago

Thanks for the question @cveld and apologies for not responding sooner.

Have you figured out how to get the currently selected subscriptionId or do you still need help with that?

lirenhe commented 3 years ago

No update for 1 month. Close this item and @cveld, please let us know if you have more information for this ask.