-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Users/ajya/nationalcloudclientsidechanges #3551
Changes from 3 commits
75ff28b
c0ea485
c948cb9
23cbd25
4162125
2ae2c5e
a484fcf
047f93c
ff1f403
107a0ee
a5cea63
28c263d
41b7a4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,16 +8,17 @@ var httpObj = new httpClient.HttpCallbackClient(tl.getVariable("AZURE_HTTP_USER_ | |
var authUrl = 'https://login.windows.net/'; | ||
var azureApiVersion = '2016-09-01'; | ||
|
||
function getAccessToken(SPN, endpointUrl: string): Q.Promise<string> { | ||
function getAccessToken(endpoint, endpointUrl: string): Q.Promise<string> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having the second parameter as enpointurl is confusing. There is already endpoint taken as input which has endpoint.url. I presume this is the resource for which you are requesting token. So should we just call it resource? |
||
|
||
var deferred = Q.defer<string>(); | ||
var authorityUrl = authUrl + SPN.tenantID + "/oauth2/token/"; | ||
var envAuthUrl = endpoint.envAuthUrl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if it is necessary to hard-code envAuthUrl if it is null. |
||
var authorityUrl = envAuthUrl + endpoint.tenantID + "/oauth2/token/"; | ||
|
||
var post_data = querystring.stringify({ | ||
resource: endpointUrl, | ||
client_id: SPN.servicePrincipalClientID, | ||
client_id: endpoint.servicePrincipalClientID, | ||
grant_type: "client_credentials", | ||
client_secret: SPN.servicePrincipalKey | ||
client_secret: endpoint.servicePrincipalKey | ||
}); | ||
|
||
var requestHeader = { | ||
|
@@ -40,11 +41,11 @@ function getAccessToken(SPN, endpointUrl: string): Q.Promise<string> { | |
return deferred.promise; | ||
} | ||
|
||
export async function getNetworkInterfacesInRG(SPN, endpointUrl: string, resourceGroupName: string) { | ||
export async function getNetworkInterfacesInRG(endpoint, endpointUrl: string, resourceGroupName: string) { | ||
|
||
var deferred = Q.defer<any>(); | ||
var restUrl = "https://management.azure.com/subscriptions/" + SPN.subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/networkInterfaces?api-version=" + azureApiVersion; | ||
var accessToken = await getAccessToken(SPN, endpointUrl); | ||
var restUrl = endpoint.url + "subscriptions/" + endpoint.subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/networkInterfaces?api-version=" + azureApiVersion; | ||
var accessToken = await getAccessToken(endpoint, endpointUrl); | ||
|
||
var requestHeader = { | ||
Authorization: 'Bearer ' + accessToken | ||
|
@@ -66,11 +67,11 @@ export async function getNetworkInterfacesInRG(SPN, endpointUrl: string, resourc | |
return deferred.promise; | ||
} | ||
|
||
export async function getLoadBalancer(SPN, endpointUrl: string, name: string, resourceGroupName: string) { | ||
export async function getLoadBalancer(endpoint, endpointUrl: string, name: string, resourceGroupName: string) { | ||
|
||
var deferred = Q.defer<any>(); | ||
var restUrl = "https://management.azure.com/subscriptions/" + SPN.subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/loadBalancers/" + name + "?api-version=" + azureApiVersion; | ||
var accessToken = await getAccessToken(SPN, endpointUrl); | ||
var restUrl = endpoint.url + "subscriptions/" + endpoint.subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/loadBalancers/" + name + "?api-version=" + azureApiVersion; | ||
var accessToken = await getAccessToken(endpoint, endpointUrl); | ||
|
||
var requestHeader = { | ||
authorization: 'Bearer ' + accessToken | ||
|
@@ -92,10 +93,10 @@ export async function getLoadBalancer(SPN, endpointUrl: string, name: string, re | |
return deferred.promise; | ||
} | ||
|
||
export async function getNetworkInterface(SPN, endpointUrl, name: string, resourceGroupName: string) { | ||
export async function getNetworkInterface(endpoint, endpointUrl, name: string, resourceGroupName: string) { | ||
var deferred = Q.defer<any>(); | ||
var restUrl = "https://management.azure.com/subscriptions/" + SPN.subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/networkInterfaces/" + name + "?api-version=" + azureApiVersion; | ||
var accessToken = await getAccessToken(SPN, endpointUrl); | ||
var restUrl = endpoint.url + "subscriptions/" + endpoint.subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/networkInterfaces/" + name + "?api-version=" + azureApiVersion; | ||
var accessToken = await getAccessToken(endpoint, endpointUrl); | ||
|
||
var requestHeader = { | ||
authorization: 'Bearer ' + accessToken | ||
|
@@ -136,11 +137,11 @@ async function checkProvisioningState(url: string, accessToken: string) { | |
return deferred.promise; | ||
} | ||
|
||
export async function setNetworkInterface(SPN, endpointUrl: string, nic, resourceGroupName: string){ | ||
export async function setNetworkInterface(endpoint, endpointUrl: string, nic, resourceGroupName: string){ | ||
|
||
var deferred = Q.defer(); | ||
var restUrl = "https://management.azure.com/subscriptions/" + SPN.subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/networkInterfaces/" + nic.name + "?api-version=" + azureApiVersion; | ||
var accessToken = await getAccessToken(SPN, endpointUrl); | ||
var restUrl = endpoint.url + "subscriptions/" + endpoint.subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/networkInterfaces/" + nic.name + "?api-version=" + azureApiVersion; | ||
var accessToken = await getAccessToken(endpoint, endpointUrl); | ||
var requestHeader = { | ||
"Content-Type": "application/json; charset=utf-8", | ||
"Authorization": 'Bearer ' + accessToken | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,11 @@ export class ApplicationTokenCredentials { | |
private clientId: string; | ||
private domain: string; | ||
private secret: string; | ||
public armUrl: string; | ||
public authUrl: string; | ||
private token_deferred: Q.Promise<string>; | ||
|
||
constructor(clientId: string, domain: string, secret: string) { | ||
constructor(clientId: string, domain: string, secret: string, armUrl: string, authUrl: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. authUrl is confusing since auth is mostly used for authentication. I would prefer full name authorityUrl |
||
if (!Boolean(clientId) || typeof clientId.valueOf() !== 'string') { | ||
throw new Error(tl.loc("ClientIdCannotBeEmpty")); | ||
} | ||
|
@@ -30,9 +32,19 @@ export class ApplicationTokenCredentials { | |
throw new Error(tl.loc("SecretCannotBeEmpty")); | ||
} | ||
|
||
if (!Boolean(armUrl) || typeof armUrl.valueOf() !== 'string') { | ||
throw new Error(tl.loc("armUrlCannotBeEmpty")); | ||
} | ||
|
||
if (!Boolean(authUrl) || typeof authUrl.valueOf() !== 'string') { | ||
throw new Error(tl.loc("authUrlCannotBeEmpty")); | ||
} | ||
|
||
this.clientId = clientId; | ||
this.domain = domain; | ||
this.secret = secret; | ||
this.armUrl = armUrl; | ||
this.authUrl = authUrl; | ||
} | ||
|
||
public getToken(force?: boolean): Q.Promise<string> { | ||
|
@@ -45,9 +57,9 @@ export class ApplicationTokenCredentials { | |
|
||
private getAuthorizationToken(): Q.Promise<string> { | ||
var deferred = Q.defer<string>(); | ||
var authorityUrl = authUrl + this.domain + "/oauth2/token/"; | ||
var authorityUrl = this.authUrl + this.domain + "/oauth2/token/"; | ||
var requestData = querystring.stringify({ | ||
resource: 'https://management.azure.com/', | ||
resource: this.armUrl, | ||
client_id: this.clientId, | ||
grant_type: "client_credentials", | ||
client_secret: this.secret | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,11 @@ function Initialize-AzureSubscription { | |
|
||
#Set UserAgent for Azure Calls | ||
Set-UserAgent | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you ensure Azure PowerShell task is covered as part of your testing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I have tested Azure Powershell Task |
||
$environmentName = "AzureCloud" | ||
if( $Endpoint.Data.Environment ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this does not validate if it contains an empty string with space. Please check if you want to trim it before it gets used |
||
$environmentName = $Endpoint.Data.Environment | ||
} | ||
|
||
if ($Endpoint.Auth.Scheme -eq 'Certificate') { | ||
# Certificate is only supported for the Azure module. | ||
|
@@ -57,11 +62,6 @@ function Initialize-AzureSubscription { | |
$additional['CurrentStorageAccountName'] = $StorageAccount | ||
} | ||
|
||
$environmentName = "AzureCloud" | ||
if( $Endpoint.Data.Environment ) { | ||
$environmentName = $Endpoint.Data.Environment | ||
} | ||
|
||
# Set the subscription. | ||
Write-Host "##[command]Set-AzureSubscription -SubscriptionName $($Endpoint.Data.SubscriptionName) -SubscriptionId $($Endpoint.Data.SubscriptionId) -Certificate ******** -Environment $environmentName $(Format-Splat $additional)" | ||
Set-AzureSubscription -SubscriptionName $Endpoint.Data.SubscriptionName -SubscriptionId $Endpoint.Data.SubscriptionId -Certificate $certificate -Environment $environmentName @additional | ||
|
@@ -127,7 +127,7 @@ function Initialize-AzureSubscription { | |
# Else, this is AzureRM. | ||
try { | ||
Write-Host "##[command]Add-AzureRMAccount -ServicePrincipal -Tenant $($Endpoint.Auth.Parameters.TenantId) -Credential $psCredential" | ||
$null = Add-AzureRMAccount -ServicePrincipal -Tenant $Endpoint.Auth.Parameters.TenantId -Credential $psCredential | ||
$null = Add-AzureRMAccount -ServicePrincipal -Tenant $Endpoint.Auth.Parameters.TenantId -Credential $psCredential -EnvironmentName $environmentName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better to log $environmentName if it is within security rules |
||
} catch { | ||
# Provide an additional, custom, credentials-related error message. | ||
Write-VstsTaskError -Message $_.Exception.Message | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# Private module-scope variables. | ||
$script:jsonContentType = "application/json;charset=utf-8" | ||
$script:formContentType = "application/x-www-form-urlencoded;charset=utf-8" | ||
$script:azureRmUri = "https://management.azure.com" | ||
$script:authUri = "https://login.microsoftonline.com/" | ||
$script:defaultAuthUri = "https://login.microsoftonline.com/" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't find this URL in the get-Environment output. What url does this map to? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. login.windows.net is legacy auth URL, login.microsoftonline.com is latest auth url. In environment property its showing only latest one. Since other URL is being used at many places in application so added it as another property. |
||
$script:defaultEnvironmentAuthUri = "https://login.windows.net/" | ||
|
||
# Connection Types | ||
$certificateConnection = 'Certificate' | ||
|
@@ -103,8 +103,13 @@ function Get-UsernamePasswordAccessToken { | |
# Well known Client-Id | ||
$password = $endpoint.Auth.Parameters.Password | ||
$username = $endpoint.Auth.Parameters.UserName | ||
$authUrl = $script:defaultAuthUri | ||
if($endpoint.Data.activeDirectoryAuthority) | ||
{ | ||
$authUrl = $endpoint.Data.activeDirectoryAuthority | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is activeDirecotryAuthority and environment Auth Url used interchangeably? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the GetAccessToken in nlbUtility you were using the environment Auth url. Why? |
||
} | ||
|
||
$authUri = "$script:authUri/common/oauth2/token" | ||
$authUri = "$authUrl/common/oauth2/token" | ||
$body = @{ | ||
resource=$script:azureUri | ||
client_id=$azurePsClientId | ||
|
@@ -134,12 +139,17 @@ function Get-SpnAccessToken { | |
$principalId = $endpoint.Auth.Parameters.ServicePrincipalId | ||
$tenantId = $endpoint.Auth.Parameters.TenantId | ||
$principalKey = $endpoint.Auth.Parameters.ServicePrincipalKey | ||
$envAuthUrl = $script:defaultEnvironmentAuthUri | ||
if($endpoint.Data.environmentAuthorityUrl) | ||
{ | ||
$envAuthUrl = $endpoint.Data.environmentAuthorityUrl | ||
} | ||
|
||
$azureUri = Get-AzureUri $endpoint | ||
|
||
# Prepare contents for POST | ||
$method = "POST" | ||
$authUri = "https://login.windows.net/$tenantId/oauth2/token" | ||
$authUri = "$envAuthUrl" + "$tenantId/oauth2/token" | ||
$body = @{ | ||
resource=$azureUri+"/" | ||
client_id=$principalId | ||
|
@@ -240,7 +250,7 @@ function Get-AzRMStorageKeys | |
$resourceGroupId = $resourceGroupDetails.id | ||
|
||
$method = "POST" | ||
$uri = "$script:azureRmUri$resourceGroupId/providers/Microsoft.Storage/storageAccounts/$storageAccountName/listKeys" + '?api-version=2015-06-15' | ||
$uri = "$($endpoint.Url)$resourceGroupId/providers/Microsoft.Storage/storageAccounts/$storageAccountName/listKeys" + '?api-version=2015-06-15' | ||
|
||
$headers = @{"Authorization" = ("{0} {1}" -f $accessToken.token_type, $accessToken.access_token)} | ||
|
||
|
@@ -281,7 +291,7 @@ function Get-AzRmVmCustomScriptExtension | |
$resourceGroupId = $resourceGroupDetails.id | ||
|
||
$method="GET" | ||
$uri = "$script:azureRmUri$resourceGroupId/providers/Microsoft.Compute/virtualMachines/$vmName/extensions/$Name" + '?api-version=2016-03-30' | ||
$uri = "$($endpoint.Url)$resourceGroupId/providers/Microsoft.Compute/virtualMachines/$vmName/extensions/$Name" + '?api-version=2016-03-30' | ||
|
||
$headers = @{"accept-language" = "en-US"} | ||
$headers.Add("Authorization", ("{0} {1}" -f $accessToken.token_type, $accessToken.access_token)) | ||
|
@@ -323,7 +333,7 @@ function Remove-AzRmVmCustomScriptExtension | |
$resourceGroupId = $resourceGroupDetails.id | ||
|
||
$method="DELETE" | ||
$uri = "$script:azureRmUri$resourceGroupId/providers/Microsoft.Compute/virtualMachines/$vmName/extensions/$Name" + '?api-version=2016-03-30' | ||
$uri = "$($endpoint.Url)$resourceGroupId/providers/Microsoft.Compute/virtualMachines/$vmName/extensions/$Name" + '?api-version=2016-03-30' | ||
|
||
$headers = @{"accept-language" = "en-US"} | ||
$headers.Add("Authorization", ("{0} {1}" -f $accessToken.token_type, $accessToken.access_token)) | ||
|
@@ -403,7 +413,7 @@ function Get-AzRmStorageAccount | |
$resourceGroupId = $resourceGroupDetails.id | ||
|
||
$method="GET" | ||
$uri = "$script:azureRmUri$resourceGroupId/providers/Microsoft.Storage/storageAccounts/$storageAccountName" + '?api-version=2016-01-01' | ||
$uri = "$($endpoint.Url)$resourceGroupId/providers/Microsoft.Storage/storageAccounts/$storageAccountName" + '?api-version=2016-01-01' | ||
|
||
$headers = @{"Authorization" = ("{0} {1}" -f $accessToken.token_type, $accessToken.access_token)} | ||
|
||
|
@@ -457,7 +467,7 @@ function Get-AzRmResourceGroup | |
$subscriptionId = $endpoint.Data.SubscriptionId.ToLower() | ||
|
||
$method="GET" | ||
$uri = "$script:azureRmUri/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName" + '?api-version=2016-02-01' | ||
$uri = "$($endpoint.Url)/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName" + '?api-version=2016-02-01' | ||
|
||
$headers = @{"Authorization" = ("{0} {1}" -f $accessToken.token_type, $accessToken.access_token)} | ||
|
||
|
@@ -497,7 +507,7 @@ function Get-AzureSqlDatabaseServerResourceId | |
|
||
Write-Verbose "[Azure Rest Call] Get Resource Groups" | ||
$method = "GET" | ||
$uri = "$script:azureRmUri/subscriptions/$subscriptionId/resources?api-version=$apiVersion" | ||
$uri = "$($endpoint.Url)/subscriptions/$subscriptionId/resources?api-version=$apiVersion" | ||
$headers = @{Authorization=("{0} {1}" -f $accessToken.token_type, $accessToken.access_token)} | ||
|
||
do { | ||
|
@@ -568,7 +578,7 @@ function Add-AzureRmSqlServerFirewall | |
# get azure sql server resource Id | ||
$azureResourceId = Get-AzureSqlDatabaseServerResourceId -endpoint $endpoint -serverName $serverName -accessToken $accessToken | ||
|
||
$uri = "$script:azureRmUri/$azureResourceId/firewallRules/$firewallRuleName\?api-version=$apiVersion" | ||
$uri = "$($endpoint.Url)/$azureResourceId/firewallRules/$firewallRuleName\?api-version=$apiVersion" | ||
$body = "{ | ||
'properties' : { | ||
'startIpAddress':'$startIPAddress', | ||
|
@@ -621,7 +631,7 @@ function Remove-AzureRmSqlServerFirewall | |
# Fetch Azure SQL server resource Id | ||
$azureResourceId = Get-AzureSqlDatabaseServerResourceId -endpoint $endpoint -serverName $serverName -accessToken $accessToken | ||
|
||
$uri = "$script:azureRmUri/$azureResourceId/firewallRules/$firewallRuleName\?api-version=$apiVersion" | ||
$uri = "$($endpoint.Url)/$azureResourceId/firewallRules/$firewallRuleName\?api-version=$apiVersion" | ||
$headers = @{Authorization=("{0} {1}" -f $accessToken.token_type, $accessToken.access_token)} | ||
|
||
Invoke-RestMethod -Uri $uri -Method Delete -Headers $headers | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did this API version work fine in other Cloud environment types. Do you see any issue with Azure deployments causing issues?