import { AzureCliCredentials } from "../lib/msRestNodeAuth" ; import { ServiceClient , RequestPrepareOptions } from "@azure/ms-rest-js" ; const keyvaultAccountName = process . env . AZURE_KEY_VAULT_ACCOUNT_NAME || "foo" ; async function login ( ) { return AzureCliCredentials . create ( { resource : "https://vault.azure.net" } ) ; } async function listKeyVaultSecrets ( creds ) { function getKVUrl ( kvAccountName ) { return `https://${ kvAccountName }.vault.azure.net/secrets?api-version=7.0` ; } try { console . log ( ">>>>>>> KeyVault <<<<<<<<<<<" ) ; const client = new ServiceClient ( creds ) ; console . log ( ">>> Subscription associated with the access token: '%s'." , creds . tokenInfo . subscription ) ; const request = { url : getKVUrl ( keyvaultAccountName ) , method : "GET" } ; console . log ( ">>> Request url: '%s'." , request . url ) ; const res = await client . sendRequest ( request ) ; console . log ( "List of secrets from keyvault account '%s': \n%O" , keyvaultAccountName , res . parsedBody ) ; } catch ( err ) { console . log ( err ) ; } } async function listResourceGroups ( creds ) { function getUrl ( subscriptionId ) { return `https://management.azure.com/subscriptions/${ subscriptionId }/resourcegroups?api-version=2018-05-01` ; } try { console . log ( ">>>>>>> List Resource groups <<<<<<<<<<<" ) ; creds . resource = "https://management.azure.com" ; const client = new ServiceClient ( creds ) ; console . log ( ">>> Subscription associated with the access token: '%s'." , creds . tokenInfo . subscription ) ; const request = { url : getUrl ( creds . subscriptionInfo . id ) , method : "GET" } ; console . log ( ">>> Request url: '%s'." , request . url ) ; const res = await client . sendRequest ( request ) ; console . log ( "List of resource groups from subscriptionId '%s': \n%O" , creds . subscriptionInfo . id , res . parsedBody ) ; const subscriptions = await AzureCliCredentials . listAllSubscriptions ( ) ; creds . subscriptionInfo = subscriptions [ 1 ] ; console . log ( ">>> The new subscription id associated with the credential object is: '%s'." , creds . subscriptionInfo . id ) ; request . url = getUrl ( creds . subscriptionInfo . id ) ; console . log ( ">>> Request url: '%s'." , request . url ) ; const res2 = await client . sendRequest ( request ) ; console . log ( "List of resource groups from subscriptionId '%s': \n%O" , creds . subscriptionInfo . id , res2 . parsedBody ) ; console . log ( ">>> Subscription associated with the access token: '%s'." , creds . tokenInfo . subscription ) ; } catch ( err ) { console . log ( err ) ; } } async function main ( ) { const creds = await login ( ) ; await listKeyVaultSecrets ( creds ) ; await listResourceGroups ( creds ) ; } main ( ) ;