Search code examples
azurepowershellrestazure-batch

Azure Batch REST API, authorization issue


I've tried to authenticate to Azure Batch using REST API, to do so I wrote following PowerShell code

$Key = 'key'
$region = "region"
$sharedKey = [System.Convert]::FromBase64String($Key)
$date = [System.DateTime]::UtcNow.ToString("R")
$stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`nocp-date:$date`n /$batchAccount/jobs`napi-version:2019-08-01.10.0`ntimeout:20"
[byte[]]$dataBytes = ([System.Text.Encoding]::UTF8).GetBytes($stringToSign)
$hmacsha256 = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha256.Key = [Convert]::FromBase64String($key)
$sig = [Convert]::ToBase64String($hmacsha256.ComputeHash($dataBytes))
$authhdr = "SharedKey $BatchAccount`:$sig"
$headers = @{
    "ocp-date" = $date;
    "Authorization" = "$authhdr";
}
Invoke-restmethod -Headers $headers -Uri 'https://$BatchAccount.$region.batch.azure.com/jobs?api-version=2019-08-01.10.0'

please note that I know that I can

  • use OAuth2 as alternative authentication mechanism
  • use Az.Batch powershell modules

I just wanted to do this using REST and SharedKey scheme as described here

https://learn.microsoft.com/en-us/rest/api/batchservice/authenticate-requests-to-the-azure-batch-service

for this API

https://learn.microsoft.com/en-us/rest/api/batchservice/job/list

But for some reason it doesn't work

I get this error but everything seems to be folowing the docs

"message":{
    "lang":"en-US",
    "value":"Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:eb5134f2-2821-4244-ac5b-066bf19bec10\nTime:2019-11-24T21:08:20.3223384Z"
},
"values":[
    {
        "key":"AuthenticationErrorDetail",
        "value":"The MAC signature found in the HTTP request 'signature-goes-here' is not the same as any computed signature. Server used following string to sign: 'GET\n\n\n\n\napplication/json; odata=minimalmetadata; charset=utf-8\n\n\n\n\n\n\nocp-date:Sun, 24 Nov 2019 21:08:20 GMT\n/name-goes-here/jobs\napi-version:2019-08-01.10.0'."
    }
]

Solution

  • There is something wrong with $stringToSign . Try this :

    $Key = "your key"
    $region = "your region"
    $BatchAccount = "your account name"
    $BatchAccountURL = "Https://$BatchAccount.$region.batch.azure.com"
    
    $sharedKey = [System.Convert]::FromBase64String($Key)
    $date = [System.DateTime]::UtcNow.ToString("R")
    $stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`nocp-date:$date`n/$BatchAccount/jobs`napi-version:2019-08-01.10.0"
    [byte[]]$dataBytes = ([System.Text.Encoding]::UTF8).GetBytes($stringToSign)
    $hmacsha256 = New-Object System.Security.Cryptography.HMACSHA256
    $hmacsha256.Key = [Convert]::FromBase64String($key)
    $sig = [Convert]::ToBase64String($hmacsha256.ComputeHash($dataBytes))
    $authhdr = "SharedKey $BatchAccount`:$sig"
    $headers = @{
        "ocp-date" = $date;
        "Authorization" = "$authhdr";
    }
    
    
    
    Invoke-restmethod -Headers $headers -Uri "$BatchAccountURL/jobs?api-version=2019-08-01.10.0"
    

    Result :

    enter image description here