Search code examples
node.jsazureazure-node-sdk

Error authenticating with Azure using node sdk:


I am following the tutorial Windows Azure SDK for Node.js - Compute Management to manage virtual machines.

I downloaded the pem file using azure account cert export to <Subscription GUID>.pem.

The script currently contains:

var subscriptionId ="<Subscription GUID>";
var pem = "<Subscription GUID>.pem";

var computeManagementClient = computeManagement.createComputeManagementClient(computeManagement.createCertificateCloudCredentials({
    subscriptionId: subscriptionId,
    pem: fs.readFileSync(pem)
}));

And when I run it from Node.js it produces the error:

C:\Apps\azure\node_modules\azure-mgmt-compute\node_modules\azure-common\lib\util\validate.js:416
  throw new Error('Required argument ' + name + ' for function ' + func + ' is
        ^
Error: Required argument credentials.pem for function CertificateCloudCredentials is not defined
    at throwMissingArgument (C:\Apps\azure\node_modules\azure-mgmt-compute\node_modules\azure-common\lib\util\validate.js:416:9)
    at ArgumentValidator._.extend.string (C:\Apps\azure\node_modules\azure-mgmt-compute\node_modules\azure-common\lib\util\validate.js:426:7)
    at C:\Apps\azure\node_modules\azure-mgmt-compute\node_modules\azure-common\lib\services\credentials\certificateCloudCredentials.js:35:9
    at Object.validateArgs (C:\Apps\azure\node_modules\azure-mgmt-compute\node_modules\azure-common\lib\util\validate.js:478:3)
    at new CertificateCloudCredentials (C:\Apps\azure\node_modules\azure-mgmt-compute\node_modules\azure-common\lib\services\credentials\certificateCloudCredentials.js:32:14)
    at Object.exports.createCertificateCloudCredentials (C:\Apps\azure\node_modules\azure-mgmt-compute\lib\compute.js:54:10)
    at Object.<anonymous> (C:\Apps\azure\setup.js:14:97)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)

Solution

  • The issue is with pem: fs.readFileSync(pem). The SDK is not converting the node buffer to a string. There is an issue on Github.

    Until this is fixed use toString on the buffer:

    pem: fs.readFileSync(pem).toString()