diff --git a/Tasks/AzureFileCopy/Strings/resources.resjson/en-US/resources.resjson b/Tasks/AzureFileCopy/Strings/resources.resjson/en-US/resources.resjson index b2df4063adc6..2a3c69c36916 100644 --- a/Tasks/AzureFileCopy/Strings/resources.resjson/en-US/resources.resjson +++ b/Tasks/AzureFileCopy/Strings/resources.resjson/en-US/resources.resjson @@ -88,5 +88,7 @@ "loc.messages.AFC_UnableToSetCustomScriptExtension": "Unable to set the custom script extension '{0}' for virtual machine '{1}': {2}", "loc.messages.AFC_CopyPrereqsFailed": "Failed to enable copy prerequisites. {0}", "loc.messages.AFC_BlobStorageNotFound": "Storage account: {0} not found. Please specify existing storage account", - "loc.messages.AFC_RootContainerAndDirectory": "'/S' option is not valid for $root containers." + "loc.messages.AFC_RootContainerAndDirectory": "'/S' option is not valid for $root containers.", + "loc.messages.AFC_RedirectResponseInvalidStatusCode": "The HTTP response code: '{0}' is not a valid redirect status code", + "loc.messages.AFC_RedirectResponseLocationHeaderIsNull": "Redirect response location header is null." } \ No newline at end of file diff --git a/Tasks/AzureFileCopy/Utility.ps1 b/Tasks/AzureFileCopy/Utility.ps1 index 8b98377a5f56..792d8d245684 100644 --- a/Tasks/AzureFileCopy/Utility.ps1 +++ b/Tasks/AzureFileCopy/Utility.ps1 @@ -1277,24 +1277,33 @@ function Is-WinRMCustomScriptExtensionExists function Get-TargetUriFromFwdLink { param( - [string]$fwdLink, - [string]$extensionName, - [string]$vmName + [string]$fwdLink ) Write-Verbose "Trying to get the target uri from the fwdLink: $fwdLink" $proxy = Get-VstsWebProxy Add-Type -AssemblyName System.Net.Http + $validHttpRedirectCodes = @( + [System.Net.HttpStatusCode]::Moved, + [System.Net.HttpStatusCode]::MovedPermanently, + [System.Net.HttpStatusCode]::Found, + [System.Net.HttpStatusCode]::Redirect, + [System.Net.HttpStatusCode]::RedirectKeepVerb, + [System.Net.HttpStatusCode]::TemporaryRedirect + ) $HttpClientHandler = New-Object System.Net.Http.HttpClientHandler $HttpClientHandler.Proxy = $proxy $HttpClientHandler.AllowAutoRedirect = $false $HttpClient = New-Object System.Net.Http.HttpClient -ArgumentList $HttpClientHandler $response = $HttpClient.GetAsync($fwdLink) $response.Wait() + if($validHttpRedirectCodes.IndexOf($response.Result.StatusCode) -eq -1) { + Write-Verbose "The http response code: $([int]$response.Result.StatusCode) is not a valid redirect response code." + throw (Get-VstsLocString -Key "AFC_RedirectResponseInvalidStatusCode" -ArgumentList $([int]$response.Result.StatusCode)) + } $targetUri = $response.Result.Headers.Location.AbsoluteUri if([string]::IsNullOrEmpty($targetUri)) { Write-Verbose "The target uri is null" - $errMessage = "targetUri = $targetUri" - throw (Get-VstsLocString -Key "AFC_SetCustomScriptExtensionFailed" -ArgumentList $extensionName, $vmName, $errMessage) + throw (Get-VstsLocString -Key "AFC_RedirectResponseLocationHeaderIsNull") } Write-Verbose "The target uri is: $targetUri" return $targetUri @@ -1361,8 +1370,8 @@ function Add-AzureVMCustomScriptExtension return } - $configWinRMScriptFile = Get-TargetUriFromFwdLink -fwdLink $configWinRMScriptFileFwdLink -extensionName $extensionName -vmName $vmName - $makeCertFile = Get-TargetUriFromFwdLink -fwdLink $makeCertFileFwdLink -extensionName $extensionName -vmName $vmName + $configWinRMScriptFile = Get-TargetUriFromFwdLink -fwdLink $configWinRMScriptFileFwdLink + $makeCertFile = Get-TargetUriFromFwdLink -fwdLink $makeCertFileFwdLink $result = Set-AzureMachineCustomScriptExtension -resourceGroupName $resourceGroupName -vmName $vmName -name $extensionName -fileUri $configWinRMScriptFile, $makeCertFile -run $scriptToRun -argument $dnsName -location $location $resultDetails = $result | ConvertTo-Json diff --git a/Tasks/AzureFileCopy/task.json b/Tasks/AzureFileCopy/task.json index 7011ee1737b3..1dcc5ebf3144 100644 --- a/Tasks/AzureFileCopy/task.json +++ b/Tasks/AzureFileCopy/task.json @@ -331,6 +331,8 @@ "AFC_UnableToSetCustomScriptExtension": "Unable to set the custom script extension '{0}' for virtual machine '{1}': {2}", "AFC_CopyPrereqsFailed": "Failed to enable copy prerequisites. {0}", "AFC_BlobStorageNotFound": "Storage account: {0} not found. Please specify existing storage account", - "AFC_RootContainerAndDirectory": "'/S' option is not valid for $root containers." + "AFC_RootContainerAndDirectory": "'/S' option is not valid for $root containers.", + "AFC_RedirectResponseInvalidStatusCode": "The HTTP response code: '{0}' is not a valid redirect status code", + "AFC_RedirectResponseLocationHeaderIsNull": "Redirect response location header is null." } } \ No newline at end of file diff --git a/Tasks/AzureFileCopy/task.loc.json b/Tasks/AzureFileCopy/task.loc.json index 3256591d5f28..496559f1e689 100644 --- a/Tasks/AzureFileCopy/task.loc.json +++ b/Tasks/AzureFileCopy/task.loc.json @@ -345,6 +345,8 @@ "AFC_UnableToSetCustomScriptExtension": "ms-resource:loc.messages.AFC_UnableToSetCustomScriptExtension", "AFC_CopyPrereqsFailed": "ms-resource:loc.messages.AFC_CopyPrereqsFailed", "AFC_BlobStorageNotFound": "ms-resource:loc.messages.AFC_BlobStorageNotFound", - "AFC_RootContainerAndDirectory": "ms-resource:loc.messages.AFC_RootContainerAndDirectory" + "AFC_RootContainerAndDirectory": "ms-resource:loc.messages.AFC_RootContainerAndDirectory", + "AFC_RedirectResponseInvalidStatusCode": "ms-resource:loc.messages.AFC_RedirectResponseInvalidStatusCode", + "AFC_RedirectResponseLocationHeaderIsNull": "ms-resource:loc.messages.AFC_RedirectResponseLocationHeaderIsNull" } } \ No newline at end of file diff --git a/Tasks/Common/azure-arm-rest/Strings/resources.resjson/en-US/resources.resjson b/Tasks/Common/azure-arm-rest/Strings/resources.resjson/en-US/resources.resjson index 0d00c67b7cb8..4e51ed5cdc62 100644 --- a/Tasks/Common/azure-arm-rest/Strings/resources.resjson/en-US/resources.resjson +++ b/Tasks/Common/azure-arm-rest/Strings/resources.resjson/en-US/resources.resjson @@ -127,5 +127,7 @@ "loc.messages.FailedToUploadFile": "Failed to upload file '%s/%s from Kudu. Error: %s", "loc.messages.FailedToGetFileContent": "Failed to get file content '%s/%s' from Kudu. Error: %s", "loc.messages.FailedToListPath": "Failed to list path '%s'. Error: %s", - "loc.messages.FailedToGetDeploymentLogs": "Failed to get deployment logs. Error: %s" + "loc.messages.FailedToGetDeploymentLogs": "Failed to get deployment logs. Error: %s", + "loc.messages.ARG_RedirectResponseInvalidStatusCode": "The HTTP response code: '%s' is not a valid redirect status code.", + "loc.messages.ARG_RedirectResponseLocationHeaderIsNull": "Location header is null for HTTP response with status code: %s" } \ No newline at end of file diff --git a/Tasks/Common/azure-arm-rest/module.json b/Tasks/Common/azure-arm-rest/module.json index 625172db6487..9122c04efb40 100644 --- a/Tasks/Common/azure-arm-rest/module.json +++ b/Tasks/Common/azure-arm-rest/module.json @@ -128,6 +128,8 @@ "FailedToUploadFile": "Failed to upload file '%s/%s from Kudu. Error: %s", "FailedToGetFileContent": "Failed to get file content '%s/%s' from Kudu. Error: %s", "FailedToListPath": "Failed to list path '%s'. Error: %s", - "FailedToGetDeploymentLogs": "Failed to get deployment logs. Error: %s" + "FailedToGetDeploymentLogs": "Failed to get deployment logs. Error: %s", + "ARG_RedirectResponseInvalidStatusCode": "The HTTP response code: '%s' is not a valid redirect status code.", + "ARG_RedirectResponseLocationHeaderIsNull": "Location header is null for HTTP response with status code: %s" } } \ No newline at end of file diff --git a/Tasks/Common/azure-arm-rest/webRequestUtility.ts b/Tasks/Common/azure-arm-rest/webRequestUtility.ts index 94bb278c883e..5b51c27f678a 100644 --- a/Tasks/Common/azure-arm-rest/webRequestUtility.ts +++ b/Tasks/Common/azure-arm-rest/webRequestUtility.ts @@ -9,11 +9,11 @@ class WebRequestUtility { httpRequest.uri = fwdLink; var httpResponse = await webClient.sendRequest(httpRequest); if(HttpRedirectCodes.indexOf(httpResponse.statusCode) == -1) { - throw new Error(`HttpResponse statuscode: ${httpResponse.statusCode} is not a valid HTTP redirect code.`); + throw new Error(tl.loc('ARG_RedirectResponseInvalidStatusCode', httpResponse.statusCode)); } var targetLink: string = httpResponse.headers["location"]; if(!targetLink) { - throw new Error(`Unable to find location header after HTTP ${httpResponse.statusCode} redirect.`); + throw new Error(tl.loc('ARG_RedirectResponseLocationHeaderIsNull', httpResponse.statusCode)); } tl.debug("the target link is : " + targetLink); return targetLink;