Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NodeJS] Redesigned the structure of AzureEnvironment and updated environment info #998

Merged
merged 17 commits into from
May 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 143 additions & 33 deletions ClientRuntimes/NodeJS/ms-rest-azure/lib/azureEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,158 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

'use strict';

var _ = require('underscore');

var util = require('util');
/**
* @class
* Initializes a new instance of the AzureEnvironment class.
* @constructor
* @param {string} authenticationEndpoint - ActiveDirectory Endpoint for the Azure Environment.
* @param {string} tokenAudience - Token audience for an endpoint.
* @param {bool} [validateAuthority] - Determines whether the authentication endpoint should
* @param {string} parameters.name - The Environment name
* @param {string} parameters.portalUrl - the management portal URL
* @param {string} parameters.managementEndpointUrl - the management service endpoint
* @param {string} parameters.resourceManagerEndpointUrl - the resource management endpoint
* @param {string} parameters.activeDirectoryEndpointUrl - the Active Directory login endpoint
* @param {string} parameters.activeDirectoryResourceId - The resource ID to obtain AD tokens for
* @param {string} [parameters.publishingProfileUrl] - the publish settings file URL
* @param {string} [parameters.sqlManagementEndpointUrl] - the sql server management endpoint for mobile commands
* @param {string} [parameters.sqlServerHostnameSuffix] - the dns suffix for sql servers
* @param {string} [parameters.galleryEndpointUrl] - the template gallery endpoint
* @param {string} [parameters.activeDirectoryGraphResourceId] - the Active Directory resource ID
* @param {string} [parameters.activeDirectoryGraphApiVersion] - the Active Directory api version
* @param {string} [parameters.storageEndpointSuffix] - the endpoint suffix for storage accounts
* @param {string} [parameters.keyVaultDnsSuffix] - the keyvault service dns suffix
* @param {string} [parameters.azureDataLakeStoreFileSystemEndpointSuffix] - the data lake store filesystem service dns suffix
* @param {string} [parameters.azureDataLakeAnalyticsCatalogAndJobEndpointSuffix] - the data lake analytics job and catalog service dns suffix
* @param {bool} [parameters.validateAuthority] - Determines whether the authentication endpoint should
* be validated with Azure AD. Default value is true.
*/
function AzureEnvironment(authenticationEndpoint, tokenAudience, validateAuthority) {
this.authenticationEndpoint = authenticationEndpoint;
this.tokenAudience = tokenAudience;
this.validateAuthority = validateAuthority;
}
function AzureEnvironment(parameters) {
//Set defaults.
this.validateAuthority = true;

/**
* Provides the settings for authentication with Azure
*/
var Azure = new AzureEnvironment('https://login.microsoftonline.com/',
'https://management.core.windows.net/',
true);
if (parameters) {
//Validate required parameters
var requiredParams = [ 'name', 'portalUrl', 'managementEndpointUrl', 'resourceManagerEndpointUrl',
'activeDirectoryEndpointUrl', 'activeDirectoryResourceId'];
requiredParams.forEach(function (param) {
if (!parameters[param] || typeof parameters[param].valueOf() !== 'string') {
throw new Error(util.format('Please provide "%s" for the environment and it must be of type "string".', param));
}
});
//Assign provided parameters
for (var prop in parameters) {
this[prop] = parameters[prop];
}
}
}
var supportedEnvironments = {
Azure: {
name: 'Azure',
portalUrl: 'http://go.microsoft.com/fwlink/?LinkId=254433',
publishingProfileUrl: 'http://go.microsoft.com/fwlink/?LinkId=254432',
managementEndpointUrl: 'https://management.core.windows.net',
resourceManagerEndpointUrl: 'https://management.azure.com/',
sqlManagementEndpointUrl: 'https://management.core.windows.net:8443/',
sqlServerHostnameSuffix: '.database.windows.net',
galleryEndpointUrl: 'https://gallery.azure.com/',
activeDirectoryEndpointUrl: 'https://login.microsoftonline.com',
activeDirectoryResourceId: 'https://management.core.windows.net/',
activeDirectoryGraphResourceId: 'https://graph.windows.net/',
activeDirectoryGraphApiVersion: '2013-04-05',
storageEndpointSuffix: '.core.windows.net',
keyVaultDnsSuffix: '.vault.azure.net',
azureDataLakeStoreFileSystemEndpointSuffix: 'azuredatalakestore.net',
azureDataLakeAnalyticsCatalogAndJobEndpointSuffix: 'azuredatalakeanalytics.net'
},
AzureChina: {
name: 'AzureChina',
portalUrl: 'http://go.microsoft.com/fwlink/?LinkId=301902',
publishingProfileUrl: 'http://go.microsoft.com/fwlink/?LinkID=301774',
managementEndpointUrl: 'https://management.core.chinacloudapi.cn',
resourceManagerEndpointUrl: 'https://management.chinacloudapi.cn',
sqlManagementEndpointUrl: 'https://management.core.chinacloudapi.cn:8443/',
sqlServerHostnameSuffix: '.database.chinacloudapi.cn',
galleryEndpointUrl: 'https://gallery.chinacloudapi.cn/',
activeDirectoryEndpointUrl: 'https://login.chinacloudapi.cn',
activeDirectoryResourceId: 'https://management.core.chinacloudapi.cn/',
activeDirectoryGraphResourceId: 'https://graph.chinacloudapi.cn/',
activeDirectoryGraphApiVersion: '2013-04-05',
storageEndpointSuffix: '.core.chinacloudapi.cn',
keyVaultDnsSuffix: '.vault.azure.cn',
// TODO: add dns suffixes for the china cloud for datalake store and datalake analytics once they are defined.
azureDataLakeStoreFileSystemEndpointSuffix: 'N/A',
azureDataLakeAnalyticsCatalogAndJobEndpointSuffix: 'N/A'
},
AzureUSGovernment: {
name: 'AzureUSGovernment',
portalUrl: 'https://manage.windowsazure.us',
publishingProfileUrl: 'https://manage.windowsazure.us/publishsettings/index',
managementEndpointUrl: 'https://management.core.usgovcloudapi.net',
resourceManagerEndpointUrl: 'https://management.usgovcloudapi.net',
sqlManagementEndpointUrl: 'https://management.core.usgovcloudapi.net:8443/',
sqlServerHostnameSuffix: '.database.usgovcloudapi.net',
galleryEndpointUrl: 'https://gallery.usgovcloudapi.net/',
activeDirectoryEndpointUrl: 'https://login.microsoftonline.com',
activeDirectoryResourceId: 'https://management.core.usgovcloudapi.net/',
activeDirectoryGraphResourceId: 'https://graph.windows.net/',
activeDirectoryGraphApiVersion: '2013-04-05',
storageEndpointSuffix: '.core.usgovcloudapi.net',
keyVaultDnsSuffix: '.vault.usgovcloudapi.net',
// TODO: add dns suffixes for the US government for datalake store and datalake analytics once they are defined.
azureDataLakeStoreFileSystemEndpointSuffix: 'N/A',
azureDataLakeAnalyticsCatalogAndJobEndpointSuffix: 'N/A'
},
AzureGermanCloud: {
name: 'AzureGermanCloud',
portalUrl: 'http://portal.microsoftazure.de/',
publishingProfileUrl: 'https://manage.microsoftazure.de/publishsettings/index',
managementEndpointUrl: 'https://management.core.cloudapi.de',
resourceManagerEndpointUrl: 'https://management.microsoftazure.de',
sqlManagementEndpointUrl: 'https://management.core.cloudapi.de:8443/',
sqlServerHostnameSuffix: '.database.cloudapi.de',
galleryEndpointUrl: 'https://gallery.cloudapi.de/',
activeDirectoryEndpointUrl: 'https://login.microsoftonline.de',
activeDirectoryResourceId: 'https://management.core.cloudapi.de/',
activeDirectoryGraphResourceId: 'https://graph.cloudapi.de/',
activeDirectoryGraphApiVersion: '2013-04-05',
storageEndpointSuffix: '.core.cloudapi.de',
keyVaultDnsSuffix: '.vault.microsoftazure.de',
// TODO: add dns suffixes for the US government for datalake store and datalake analytics once they are defined.
azureDataLakeStoreFileSystemEndpointSuffix: 'N/A',
azureDataLakeAnalyticsCatalogAndJobEndpointSuffix: 'N/A'
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Does it make sense to include the dogfood environment here, or do we not want to expose those URIs in the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont want to expose the internal environments here. Have provided a mechanism to add new environment.


/**
* Provides the settings for authentication with Azure China
* Adds a new instance of the AzureEnvironment to the prototype.
* @param {string} parameters.name - The Environment name
* @param {string} parameters.portalUrl - the management portal URL
* @param {string} parameters.managementEndpointUrl - the management service endpoint
* @param {string} parameters.resourceManagerEndpointUrl - the resource management endpoint
* @param {string} parameters.activeDirectoryEndpointUrl - the Active Directory login endpoint
* @param {string} parameters.activeDirectoryResourceId - The resource ID to obtain AD tokens for
* @param {string} [parameters.publishingProfileUrl] - the publish settings file URL
* @param {string} [parameters.sqlManagementEndpointUrl] - the sql server management endpoint for mobile commands
* @param {string} [parameters.sqlServerHostnameSuffix] - the dns suffix for sql servers
* @param {string} [parameters.galleryEndpointUrl] - the template gallery endpoint
* @param {string} [parameters.activeDirectoryGraphResourceId] - the Active Directory resource ID
* @param {string} [parameters.activeDirectoryGraphApiVersion] - the Active Directory api version
* @param {string} [parameters.storageEndpointSuffix] - the endpoint suffix for storage accounts
* @param {string} [parameters.keyVaultDnsSuffix] - the keyvault service dns suffix
* @param {string} [parameters.azureDataLakeStoreFileSystemEndpointSuffix] - the data lake store filesystem service dns suffix
* @param {string} [parameters.azureDataLakeAnalyticsCatalogAndJobEndpointSuffix] - the data lake analytics job and catalog service dns suffix
* @param {bool} [parameters.validateAuthority] - Determines whether the authentication endpoint should
* be validated with Azure AD. Default value is true.
* @return {AzureEnvironment} - Reference to the newly added Environment
*/
var AzureChina = new AzureEnvironment('https://login.chinacloudapi.cn/',
'https://management.core.chinacloudapi.cn/',
true);
AzureEnvironment.prototype.add = function(parameters) {
var _environment = new AzureEnvironment(parameters);
AzureEnvironment.prototype[_environment.name] = _environment;
return _environment;
};

/**
* Provides the settings for authentication with Azure US Government
*/
var AzureUSGovernment = new AzureEnvironment('https://login.microsoftonline.com/',
'https://management.core.usgovcloudapi.net/',
true);
//Adding the supported environments
for(var key in supportedEnvironments) {
AzureEnvironment.prototype.add(supportedEnvironments[key]);
}

_.extend(module.exports, {
Azure: Azure,
AzureChina: AzureChina,
AzureEnvironment: AzureEnvironment,
AzureUSGovernment: AzureUSGovernment
});
module.exports = new AzureEnvironment();
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ function ApplicationTokenCredentials(clientId, domain, secret, options) {
*/
ApplicationTokenCredentials.prototype.signRequest = function (webResource, callback) {
var self = this;
var authorityUrl = self.environment.authenticationEndpoint + self.domain;
var authorityUrl = self.environment.activeDirectoryEndpointUrl + self.domain;
var context = new adal.AuthenticationContext(authorityUrl, self.environment.validateAuthority, self.tokenCache);

context.acquireTokenWithClientCredentials(self.environment.tokenAudience, self.clientId, self.secret, function (err, result) {
context.acquireTokenWithClientCredentials(self.environment.activeDirectoryResourceId, self.clientId, self.secret, function (err, result) {
if (err) {
return callback(new Error('Failed to acquire token for application. \n' + err));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ function UserTokenCredentials(clientId, domain, username, password, clientRedire
*/
UserTokenCredentials.prototype.signRequest = function (webResource, callback) {
var self = this;
var authorityUrl = self.environment.authenticationEndpoint + self.domain;
var authorityUrl = self.environment.activeDirectoryEndpointUrl + self.domain;
var context = new adal.AuthenticationContext(authorityUrl, self.environment.validateAuthority, self.tokenCache);

context.acquireTokenWithUsernamePassword(self.environment.tokenAudience, self.username, self.password, self.clientId, function (err, result) {
context.acquireTokenWithUsernamePassword(self.environment.activeDirectoryResourceId, self.username, self.password, self.clientId, function (err, result) {
if (err) {
return callback(new Error('Failed to acquire token. \n' + err));
}
Expand Down
Loading