Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ms-rest-azure

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ms-rest-azure - npm Package Compare versions

Comparing version 2.4.1 to 2.4.2

lib/credentials/cognitiveServicesCredentials.js

8

Changelog.md

@@ -0,3 +1,9 @@

### 2.4.2 (11/06/2017)
- Relaxed check for the value of provisioningState property by making it case insensitive.
- Updated the activeDirectoryEndpointUrl for the `AzureUSGovernment` Azure Environment from `login-us.microsoftonline.com` to `login.microsoftonline.us`
- Added support for `CognitiveServicesCredentials`.
- Added support for `MSIAppServiceTokenCredentials` and `loginWithAppServiceMSI()` #2292.
### 2.4.1 (10/11/2017)
- Restricting dependency on "moment" from "^2.18.1" to "~2.18.1" due to bugs in 2.19.0
- Restricted dependency on "moment" from "^2.18.1" to "~2.18.1" due to bugs in 2.19.0

@@ -4,0 +10,0 @@ ### 2.4.0 (10/03/2017)

@@ -324,3 +324,3 @@ import * as msRest from 'ms-rest';

galleryEndpointUrl: 'https://gallery.usgovcloudapi.net/',
activeDirectoryEndpointUrl: 'https://login-us.microsoftonline.com/',
activeDirectoryEndpointUrl: 'https://login.microsoftonline.us/',
activeDirectoryResourceId: 'https://management.core.usgovcloudapi.net/',

@@ -503,10 +503,29 @@ activeDirectoryGraphResourceId: 'https://graph.windows.net/',

/**
* Creates a new CognitiveServicesCredentials object.
*/
export class CognitiveServicesCredentials extends msRest.ApiKeyCredentials {
/**
* Creates a new CognitiveServicesCredentials object.
*
* @constructor
* @param {string} subscriptionKey The CognitiveServices subscription key
*/
constructor(subscriptionKey: string);
}
/**
* @class MSITokenCredentials
*/
export class MSITokenCredentials {
/**
* Authenticates using the identity service running on an Azure virtual machine.
* This method makes a request to the authentication service hosted on the VM
* and gets back an access token.
* @property {string} resource - The resource uri or token audience for which the token is needed.
* Default is: "https://management.azure.com/"
*/
resource? = "https://management.azure.com/";
/**
* Authenticates using the identity service.
*

@@ -523,2 +542,3 @@ * @param {MSIOptions} [options] - Optional parameters.

*/
getToken(callback: { (error: Error, result: { token_type: string, access_token: string }): void }): void;

@@ -529,2 +549,54 @@ signRequest(webResource: msRest.WebResource, callback: { (err: Error): void }): void;

/**
* @class MSIVmTokenCredentials
*/
export class MSIVmTokenCredentials extends MSITokenCredentials {
/**
* @property {number} [port] port on which the MSI service is running on the host VM. Default port is 50342
*/
port?: 50342
/**
* Authenticates using the identity service running on an Azure virtual machine.
* This method makes a request to the authentication service hosted on the VM
* and gets back an access token.
*
* @param {MSIVmOptions} [options] - Optional parameters.
*/
constructor(options?: MSIOptions);
}
/**
* @class MSIAppServiceTokenCredentials
*/
export class MSIAppServiceTokenCredentials extends MSITokenCredentials {
/**
* @property {string} msiEndpoint - The local URL from which your app can request tokens.
* Either provide this parameter or set the environment varaible `MSI_ENDPOINT`.
* For example: `MSI_ENDPOINT="http://127.0.0.1:41741/MSI/token/"`
*/
msiEndpoint: string;
/**
* @property {string} msiSecret - The secret used in communication between your code and the local MSI agent.
* Either provide this parameter or set the environment varaible `MSI_SECRET`.
* For example: `MSI_SECRET="69418689F1E342DD946CB82994CDA3CB"`
*/
msiSecret: string;
/**
* @property {string} [msiApiVersion] The api-version of the local MSI agent. Default value is "2017-09-01".
*/
msiApiVersion?: "2017-09-01";
/**
* Authenticates using the identity service running on an Azure virtual machine.
* This method makes a request to the authentication service hosted on the VM
* and gets back an access token.
*
* @param {MSIAppServiceOptions} [options] - Optional parameters.
*/
constructor(options?: MSIAppServiceOptions);
}
/**
* Defines the base class for a Resource in Azure. It is an empty class.

@@ -739,6 +811,2 @@ */

/**
* @prop {number} [port] - port on which the MSI service is running on the host VM. Default port is 50342
*/
port?: number;
/**
* @prop {string} [resource] - The resource uri or token audience for which the token is needed.

@@ -753,2 +821,34 @@ * For e.g. it can be:

/**
* @interface MSIAppServiceOptions Defines the optional parameters for authentication with MSI for AppService.
*/
export interface MSIAppServiceOptions extends MSIOptions {
/**
* @property {string} [msiEndpoint] - The local URL from which your app can request tokens.
* Either provide this parameter or set the environment varaible `MSI_ENDPOINT`.
* For example: `export MSI_ENDPOINT="http://127.0.0.1:41741/MSI/token/"`
*/
msiEndpoint: string;
/**
* @property {string} [msiSecret] - The secret used in communication between your code and the local MSI agent.
* Either provide this parameter or set the environment varaible `MSI_SECRET`.
* For example: `export MSI_SECRET="69418689F1E342DD946CB82994CDA3CB"`
*/
msiSecret: string;
/**
* @property {string} [msiApiVersion] - The api-version of the local MSI agent. Default value is "2017-09-01".
*/
msiApiVersion?: string;
}
/**
* @interface MSIVmOptions Defines the optional parameters for authentication with MSI for Virtual Machine.
*/
export interface MSIVmOptions extends MSIOptions {
/**
* @prop {number} [port] - port on which the MSI service is running on the host VM. Default port is 50342
*/
port?: number;
}
/**
* Before using this method please install az cli from https://github.com/Azure/azure-cli/releases.

@@ -789,4 +889,72 @@ * If you have an Azure virtual machine provisioned with az cli and has MSI enabled,

*/
export function loginWithMSI(callback: { (err: Error, credentials: MSITokenCredentials): void }): void;
export function loginWithMSI(options: MSIOptions, callback: { (err: Error, credentials: MSITokenCredentials): void }): void;
export function loginWithMSI(options?: MSIOptions): Promise<MSITokenCredentials>;
export function loginWithMSI(callback: { (err: Error, credentials: MSIVmTokenCredentials): void }): void;
export function loginWithMSI(options: MSIVmOptions, callback: { (err: Error, credentials: MSIVmTokenCredentials): void }): void;
export function loginWithMSI(options?: MSIVmOptions): Promise<MSIVmTokenCredentials>;
/**
* Before using this method please install az cli from https://github.com/Azure/azure-cli/releases.
* If you have an Azure virtual machine provisioned with az cli and has MSI enabled,
* you can then use this method to get auth tokens from the VM.
*
* To create a new VM, enable MSI, please execute this command:
* az vm create -g <resource_group_name> -n <vm_name> --assign-identity --image <os_image_name>
* Note: the above command enables a service endpoint on the host, with a default port 50342
*
* To enable MSI on a already provisioned VM, execute the following command:
* az vm --assign-identity -g <resource_group_name> -n <vm_name> --port <custom_port_number>
*
* To know more about this command, please execute:
* az vm --assign-identity -h
*
* Authenticates using the identity service running on an Azure virtual machine.
* This method makes a request to the authentication service hosted on the VM
* and gets back an access token.
*
* @param {object} [options] - Optional parameters
* @param {string} [options.port] - port on which the MSI service is running on the host VM. Default port is 50342
* @param {string} [options.resource] - The resource uri or token audience for which the token is needed.
* For e.g. it can be:
* - resourcemanagement endpoint "https://management.azure.com"(default)
* - management endpoint "https://management.core.windows.net/"
* @param {function} [optionalCallback] The optional callback.
*
* @returns {function | Promise} If a callback was passed as the last parameter then it returns the callback else returns a Promise.
*
* {function} optionalCallback(err, credentials)
* {Error} [err] - The Error object if an error occurred, null otherwise.
* {object} [tokenResponse] - The tokenResponse (token_type and access_token are the two important properties)
* {Promise} A promise is returned.
* @resolve {object} - tokenResponse.
* @reject {Error} - error object.
*/
export function loginWithVmMSI(callback: { (err: Error, credentials: MSIVmTokenCredentials): void }): void;
export function loginWithVmMSI(options: MSIVmOptions, callback: { (err: Error, credentials: MSIVmTokenCredentials): void }): void;
export function loginWithVmMSI(options?: MSIVmOptions): Promise<MSIVmTokenCredentials>;
/**
* Authenticate using the App Service MSI.
* @param {object} [options] - Optional parameters
* @param {string} [options.msiEndpoint] - The local URL from which your app can request tokens.
* Either provide this parameter or set the environment varaible `MSI_ENDPOINT`.
* For example: `MSI_ENDPOINT="http://127.0.0.1:41741/MSI/token/"`
* @param {string} [options.msiSecret] - The secret used in communication between your code and the local MSI agent.
* Either provide this parameter or set the environment varaible `MSI_SECRET`.
* For example: `MSI_SECRET="69418689F1E342DD946CB82994CDA3CB"`
* @param {string} [options.resource] - The resource uri or token audience for which the token is needed.
* For example, it can be:
* - resourcemanagement endpoint "https://management.azure.com"(default)
* - management endpoint "https://management.core.windows.net/"
* @param {string} [options.msiApiVersion] - The api-version of the local MSI agent. Default value is "2017-09-01".
* @param {function} [optionalCallback] - The optional callback.
* @returns {function | Promise} If a callback was passed as the last parameter then it returns the callback else returns a Promise.
*
* {function} optionalCallback(err, credentials)
* {Error} [err] - The Error object if an error occurred, null otherwise.
* {object} [tokenResponse] - The tokenResponse (token_type and access_token are the two important properties)
* {Promise} A promise is returned.
* @resolve {object} - tokenResponse.
* @reject {Error} - error object.
*/
export function loginWithAppServiceMSI(callback: { (err: Error, credentials: MSIAppServiceTokenCredentials): void }): void;
export function loginWithAppServiceMSI(options: MSIAppServiceOptions, callback: { (err: Error, credentials: MSIAppServiceTokenCredentials): void }): void;
export function loginWithAppServiceMSI(options?: MSIAppServiceOptions): Promise<MSIAppServiceTokenCredentials>;

2

lib/azureEnvironment.js

@@ -95,3 +95,3 @@ // Copyright (c) Microsoft Corporation. All rights reserved.

galleryEndpointUrl: 'https://gallery.usgovcloudapi.net/',
activeDirectoryEndpointUrl: 'https://login-us.microsoftonline.com/',
activeDirectoryEndpointUrl: 'https://login.microsoftonline.us/',
activeDirectoryResourceId: 'https://management.core.usgovcloudapi.net/',

@@ -98,0 +98,0 @@ activeDirectoryGraphResourceId: 'https://graph.windows.net/',

@@ -122,3 +122,3 @@ // Copyright (c) Microsoft Corporation. All rights reserved.

let finished = [LroStates.Succeeded, LroStates.Failed, LroStates.Canceled].some(function (e) {
return e === pollingState.status;
return (pollingState.status) ? e.toLowerCase() === pollingState.status.toLowerCase() : e === pollingState.status;
});

@@ -148,3 +148,3 @@ return !finished;

function (err) {
if (pollingState.status === LroStates.Succeeded) {
if (pollingState.status.toLowerCase() === LroStates.Succeeded.toLowerCase()) {
if ((pollingState.azureAsyncOperationHeaderLink || !pollingState.resource) &&

@@ -151,0 +151,0 @@ (initialRequestMethod === 'PUT' || initialRequestMethod === 'PATCH')) {

@@ -12,17 +12,20 @@ // Copyright (c) Microsoft Corporation. All rights reserved.

const msrest = require('ms-rest');
const request = require('request');
const Constants = msrest.Constants;
/**
* Base class for MSI based credentials
*/
class MSITokenCredentials {
/**
* Creates an instance of MSITokenCredentials.
* @param {object} [options] - Optional parameters
* @param {string} [options.resource] - The resource uri or token audience for which the token is needed.
* For e.g. it can be:
* - resourcemanagement endpoint "https://management.azure.com"(default)
* - management endpoint "https://management.core.windows.net/"
*/
constructor(options) {
if (!options) {
options = {};
}
if (!options) options = {};
if (!options.port) {
options.port = 50342; // default port where token service runs.
} else if (typeof options.port.valueOf() !== 'number') {
throw new Error('port must be a number.');
}
if (!options.resource) {

@@ -33,4 +36,2 @@ options.resource = 'https://management.azure.com/';

}
this.port = options.port;
this.resource = options.resource;

@@ -40,3 +41,3 @@ }

/**
* Prepares and sends a POST request to a service endpoint hosted on the Azure VM, which responds with the access token.
* Prepares and sends a request to the MSI service endpoint which responds with the access token.
* @param {function} callback The callback in the form (err, result)

@@ -48,26 +49,6 @@ * @return {function} callback

getToken(callback) {
const postUrl = `http://localhost:${this.port}/oauth2/token`;
const reqOptions = this.prepareRequestOptions();
request.post(postUrl, reqOptions, (err, response, body) => {
if (err) {
return callback(err);
}
try {
let tokenResponse = JSON.parse(body);
if (!tokenResponse.token_type) {
throw new Error(`Invalid token response, did not find token_type. Response body is: ${body}`);
} else if (!tokenResponse.access_token) {
throw new Error(`Invalid token response, did not find access_token. Response body is: ${body}`);
}
return callback(null, tokenResponse);
} catch (error) {
return callback(error);
}
});
return callback();
}
prepareRequestOptions() {
const resource = encodeURIComponent(this.resource);
const forwardSlash = encodeURIComponent('/');
let reqOptions = {

@@ -78,6 +59,2 @@ headers: {},

reqOptions.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
reqOptions.headers['Metadata'] = 'true';
reqOptions.body = `resource=${resource}`;
return reqOptions;

@@ -87,8 +64,8 @@ }

/**
* Signs a request with the Authentication header.
*
* @param {webResource} The WebResource to be signed.
* @param {function(error)} callback The callback function.
* @return {undefined}
*/
* Signs a request with the Authentication header.
*
* @param {webResource} The WebResource to be signed.
* @param {function(error)} callback The callback function.
* @return {undefined}
*/
signRequest(webResource, callback) {

@@ -95,0 +72,0 @@ this.getToken(function (err, result) {

@@ -15,3 +15,4 @@ // Copyright (c) Microsoft Corporation. All rights reserved.

const UserTokenCredentials = require('./credentials/userTokenCredentials');
const MSITokenCredentials = require('./credentials/msiTokenCredentials');
const MSIVmTokenCredentials = require('./credentials/msiVmTokenCredentials');
const MSIAppServiceTokenCredentials = require('./credentials/msiAppServiceTokenCredentials');
const SubscriptionClient = require('./subscriptionManagement/subscriptionClient');

@@ -725,3 +726,3 @@

}
const creds = new MSITokenCredentials(options);
const creds = new MSIVmTokenCredentials(options);
creds.getToken(function (err) {

@@ -787,2 +788,63 @@ if (err) return callback(err);

exports = module.exports;
/**
* Private method
*/
function _withAppServiceMSI(options, callback) {
if (!callback) {
throw new Error('callback cannot be null or undefined.');
}
let creds;
try {
creds = new MSIAppServiceTokenCredentials(options);
} catch (err) {
return callback(err);
}
creds.getToken(function (err) {
if (err) return callback(err);
return callback(null, creds);
});
}
/**
* Authenticate using the App Service MSI.
* @param {object} [options] - Optional parameters
* @param {string} [options.msiEndpoint] - The local URL from which your app can request tokens.
* Either provide this parameter or set the environment varaible `MSI_ENDPOINT`.
* For example: `MSI_ENDPOINT="http://127.0.0.1:41741/MSI/token/"`
* @param {string} [options.msiSecret] - The secret used in communication between your code and the local MSI agent.
* Either provide this parameter or set the environment varaible `MSI_SECRET`.
* For example: `MSI_SECRET="69418689F1E342DD946CB82994CDA3CB"`
* @param {string} [options.resource] - The resource uri or token audience for which the token is needed.
* For example, it can be:
* - resourcemanagement endpoint "https://management.azure.com"(default)
* - management endpoint "https://management.core.windows.net/"
* @param {string} [options.msiApiVersion] - The api-version of the local MSI agent. Default value is "2017-09-01".
* @param {function} [optionalCallback] - The optional callback.
* @returns {function | Promise} If a callback was passed as the last parameter then it returns the callback else returns a Promise.
*
* {function} optionalCallback(err, credentials)
* {Error} [err] - The Error object if an error occurred, null otherwise.
* {object} [tokenResponse] - The tokenResponse (token_type and access_token are the two important properties)
* {Promise} A promise is returned.
* @resolve {object} - tokenResponse.
* @reject {Error} - error object.
*/
exports.withAppServiceMSI = function withAppServiceMSI(options, optionalCallback) {
if (!optionalCallback && typeof options === 'function') {
optionalCallback = options;
options = {};
}
if (!optionalCallback) {
return new Promise((resolve, reject) => {
_withAppServiceMSI(options, (err, credentials) => {
if (err) { reject(err); }
else { resolve(credentials); }
return;
});
});
} else {
return _withAppServiceMSI(options, optionalCallback);
}
};
exports = module.exports;

@@ -11,2 +11,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved.

exports.DeviceTokenCredentials = require('./credentials/deviceTokenCredentials');
exports.CognitiveServicesCredentials = require('./credentials/cognitiveServicesCredentials');
exports.MSITokenCredentials = require('./credentials/msiTokenCredentials');
exports.MSIVmTokenCredentials = require('./credentials/msiVmTokenCredentials');
exports.MSIAppServiceTokenCredentials = require('./credentials/msiAppServiceTokenCredentials');
exports.AzureEnvironment = require('./azureEnvironment');

@@ -27,1 +31,3 @@ exports.BaseResource = require('./baseResource');

exports.loginWithMSI = require('./login').withMSI;
exports.loginWithVmMSI = exports.loginWithMSI;
exports.loginWithAppServiceMSI = require('./login').withAppServiceMSI;

@@ -8,3 +8,3 @@ {

},
"version": "2.4.1",
"version": "2.4.2",
"description": "Client Runtime for Node.js Azure client libraries generated using AutoRest",

@@ -31,4 +31,4 @@ "tags": [

"uuid": "^3.1.0",
"adal-node": "^0.1.17",
"ms-rest": "^2.2.3",
"adal-node": "^0.1.25",
"ms-rest": "^2.2.5",
"moment": "~2.18.1",

@@ -35,0 +35,0 @@ "@types/uuid": "^3.4.2",

@@ -41,3 +41,3 @@ // Copyright (c) Microsoft Corporation. All rights reserved.

tempEnv.name.should.equal('AzureUSGovernment');
tempEnv.activeDirectoryEndpointUrl.should.equal('https://login-us.microsoftonline.com/');
tempEnv.activeDirectoryEndpointUrl.should.equal('https://login.microsoftonline.us/');
tempEnv.activeDirectoryResourceId.should.equal('https://management.core.usgovcloudapi.net/');

@@ -44,0 +44,0 @@ tempEnv.managementEndpointUrl.should.equal('https://management.core.usgovcloudapi.net');

@@ -10,2 +10,3 @@ // Copyright (c) Microsoft Corporation. All rights reserved.

var sinon = require('sinon');
var CognitiveServicesCredentials = require('../lib/credentials/cognitiveServicesCredentials');

@@ -25,17 +26,17 @@ var testPrefix = 'cred-tests';

var dummyCreds = {
tokenAudience: undefined,
environment: {},
authorizationScheme: 'Bearer',
tokenCache: {},
clientId: credsObj.clientSecret,
domain: credsObj.tenantId,
secret: credsObj.clientSecret,
context: {}
}
var clientId = credsObj.clientId;
var secret = credsObj.clientSecret;
var domain = credsObj.tenantId;
sinon.stub(login, 'withServicePrincipalSecret').callsFake((clientId, secret, domain, {}, callback) => {
return callback(null, dummyCreds, []);
});
tokenAudience: undefined,
environment: {},
authorizationScheme: 'Bearer',
tokenCache: {},
clientId: credsObj.clientSecret,
domain: credsObj.tenantId,
secret: credsObj.clientSecret,
context: {}
}
var clientId = credsObj.clientId;
var secret = credsObj.clientSecret;
var domain = credsObj.tenantId;
sinon.stub(login, 'withServicePrincipalSecret').callsFake((clientId, secret, domain, { }, callback) => {
return callback(null, dummyCreds, []);
});
} catch (err) {

@@ -46,7 +47,7 @@ done(err);

});
after((done) => {
done();
});
beforeEach((done) => {

@@ -57,3 +58,3 @@ delete process.env['AZURE_SUBSCRIPTION_ID'];

});
afterEach((done) => {

@@ -114,2 +115,19 @@ done();

});
});
describe('CognitiveServices credentials', function () {
it('should set subscriptionKey properly in request', function (done) {
var creds = new CognitiveServicesCredentials('123-456-7890');
var request = {
headers: {}
};
creds.signRequest(request, function () {
request.headers.should.have.property('Ocp-Apim-Subscription-Key');
request.headers.should.have.property('X-BingApis-SDK-Client');
request.headers['Ocp-Apim-Subscription-Key'].should.match(new RegExp('^123\-456\-7890$'));
request.headers['X-BingApis-SDK-Client'].should.match(new RegExp('^node\-SDK$'));
done();
});
});
});

@@ -5,2 +5,3 @@ azureServiceClientTests.js

credTests.js
msiTokenCredentialTests.js
msiVmTokenCredentialTests.js
msiAppServiceTokenCredentialTests.js
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc