0
votes

I am trying to create an UDF in cosmos db using REST call from powershell. I am getting unauthorized for the generated auth header. I am using the below methods to build auth header and make REST call for UDF creation.

I am using resource type as udf while building auth header.

function GetKey([System.String]$Verb = '',[System.String]$ResourceId = '',
            [System.String]$ResourceType = '',[System.String]$Date = '',[System.String]$masterKey = '') {
        $keyBytes = [System.Convert]::FromBase64String($masterKey) 
        $text = @($Verb.ToLowerInvariant() + "`n" + $ResourceType.ToLowerInvariant() + "`n" + $ResourceId + "`n" + $Date.ToLowerInvariant() + "`n" + "`n")
        $body =[Text.Encoding]::UTF8.GetBytes($text)
        $hmacsha = new-object -TypeName System.Security.Cryptography.HMACSHA256 -ArgumentList (,$keyBytes) 
        $hash = $hmacsha.ComputeHash($body)
        $signature = [System.Convert]::ToBase64String($hash)

        [System.Web.HttpUtility]::UrlEncode($('type=master&ver=1.0&sig=' + $signature))
    }

 function BuildHeaders([string]$action = "get",[string]$resType, [string]$resourceId){
        $authz = GetKey -Verb $action -ResourceType $resType -ResourceId $resourceId -Date $apiDate -masterKey $connectionKey
        $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
        $headers.Add("Authorization", $authz)
        $headers.Add("x-ms-version", '2015-12-16')
        $headers.Add("x-ms-date", $apiDate) 
        $headers
    }


 function CreateUDF([string]$dbname,[string]$collectionName){
        $uri = $rootUri + "/" + $dbname + "/colls/" + $collectionName + "/udfs"
        $headers = BuildHeaders -action Post -resType udfs -resourceId $dbname
        $response = Invoke-RestMethod -Uri $uri -Method POST -Headers $headers -Body $udfCreationInput
        $response
        Write-Host ("Create Udf Response is " + $Response)
   }

Not sure on what is wrong here while building auth header. Please help me on getting this working.

1

1 Answers

0
votes

I used the following script to generate the authorization token for creating UDF via Rest API:

$Verb="POST"
$ResourceType="udfs"
$ResourceId="dbs/brucedb/colls/brucecoll"
$Date=[System.DateTime]::UtcNow.ToString("r")
$text = @($Verb.ToLowerInvariant() + "`n" + $ResourceType.ToLowerInvariant() + "`n" + $ResourceId + "`n" + $Date.ToLowerInvariant() + "`n" + "`n")
$masterKey="<your-master-key>"

$keyBytes = [System.Convert]::FromBase64String($masterKey) 
$body =[Text.Encoding]::UTF8.GetBytes($text)
$hmacsha = new-object -TypeName System.Security.Cryptography.HMACSHA256 -ArgumentList (,$keyBytes) 
$hash = $hmacsha.ComputeHash($body)
$signature = [System.Convert]::ToBase64String($hash)

Add-Type -AssemblyName System.Web
$token=[System.Web.HttpUtility]::UrlEncode($('type=master&ver=1.0&sig=' + $signature))
Write-Host ("the token is:" + $token)

TEST:

enter image description here

Moreover, you could follow Create User Defined Function, Access control in the Azure Cosmos DB SQL API and this github C# Rest sample.