I have use case of getting access tokens for various API’s such as Graph and Azure Resource Manager without registering a client for web API. The use case is mostly running API operations against Azure Resource Manager, with minimal interference to the environment.
When I don’t have AZCLI installed?
I browse just an URL to get access token. The web app worker will spawn a child process for OAuth2 AZCLI client, and respond with Refresh-id and access token in the console output.
NodeJS app
The NodeJS app is express app spawning a child process, and piping output to browser.
Express Route (Snippet of the code)
var router = require('express').Router()
var {Readable} = require('stream')
var readStream = Readable()
readStream._read = function () {}
const { spawn } = require('child_process')
const fs = require('fs')
router.get('/', async function(req, res, next) {
//console.log(req)
const ls = spawn('node', ['./src/azCli2.js'],{argv0:req.query.aud})
fs.createReadStream('./public/client.html').pipe(res,{end:false})
console.log('ss')
readStream.pipe(res)
res.setHeader('content-type','text/html')
ls.stdout.on('data', (data) => {
var out = data.toString()
console.log(out)
readStream.push(`<br> ${out}`)
});
ls.on('close', (code) => {
console.log('close')
readStream.unpipe(res)
readStream.destroy()
console.log(`child process exited with code ${code}`);
return res.end()
});
})
module.exports = router
References
https://docs.microsoft.com/en-us/cli/azure/
- Style used in Web app console is from this cool example
- NodeJS Child Process https://nodejs.org/api/child_process.html
0 comments on “Demo: Accessing AZ CLI remotely via NodeJS express app”