Skip to content

Instantly share code, notes, and snippets.

@sajeetharan
Created May 23, 2022
Embed
What would you like to do?
FUNCTION GenerateMasterKeyAuthorizationSignature
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][String] $verb, # HTTP verb, such as GET, POST, or PUT
[Parameter(Mandatory=$true)][String] $resourceLink, # Case SeNsItIvE!!! (this is the parent path for the $resourceType)
[Parameter(Mandatory=$true)][String] $resourceType, # type of resource that the request is for, Eg. "dbs", "colls", "docs"
[Parameter(Mandatory=$true)][String] $dateTime, # Must exactly match x-ms-date in header (and be lower case)
[Parameter(Mandatory=$true)][String] $key, #
[Parameter(Mandatory=$true)][String] $keyType, # "master" or "resource"
[Parameter(Mandatory=$false)][String] $tokenVersion = "1.0" #
)
$hmacSha256 = New-Object System.Security.Cryptography.HMACSHA256
$hmacSha256.Key = [System.Convert]::FromBase64String($key)
If ($resourceLink -eq $resourceType)
{ $resourceLink = "" }
$payLoad = "$($verb.ToLowerInvariant())`n$($resourceType.ToLowerInvariant())`n$resourceLink`n$($dateTime.ToLowerInvariant())`n`n" # WAS: ($dateTime.ToLowerInvariant())
# write-host -ForegroundColor Cyan "---- Payload ----`n$payload-----------------" # DEBUG
$hashPayLoad = $hmacSha256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($payLoad))
$signature = [System.Convert]::ToBase64String($hashPayLoad)
$authSet = [System.Web.HttpUtility]::UrlEncode("type=$keyType&ver=$tokenVersion&sig=$signature");
return $authSet;
}
$environment = 'dev'
$documentPath = '\Users\sajee\Downloads\config.json'
$cosmosDbAccount = ' '
$databaseId = ' '
$containerId = 'test'
$resourceGroupName = ''
$partitionKey = 'configId'
$key = "jQMbCDBiowNSGScSMBiQO8HunaTAJ2vIB6Fw1OsN2lXUSd4gTQfXgld2d6P0CRrvO5337iD1LE6EiB6pferzeg=="
#$encode = #[System.Text.Encoding]::UTF8.GetBytes($string)
#$encode = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($string))
$method = 'POST';
$resourceType = 'docs';
$requestDateString = [DateTime]::UtcNow.ToString('r')
$resourceLink = "dbs/$databaseId/colls/$containerId";
#Write-Output $encode
$dateTime = [DateTime]::UtcNow.ToString("r")
$auth = GenerateMasterKeyAuthorizationSignature -verb $method -resourceLink $resourceLink -resourceType $resourceType -key $key -keyType "master" -tokenVersion "1.0" -dateTime $dateTime
#return;
$uri = "https://$cosmosDbAccount.documents.azure.com/$resourceLink/docs";
$files = Get-ChildItem $documentPath
foreach($f in $files){
Write-Output "File: "$f.FullName
$document = Get-Content $f.FullName #-Raw | ConvertFrom-Json
$header = @{'x-ms-date' = $requestDateString;'x-ms-version'= '2018-12-31';'Accept'='application/json';'Content-Type'='application/json';'authorization'= $auth;'x-ms-documentdb-is-upsert' = 'true';'x-ms-documentdb-partitionkey' = '["' + $partitionKey + '"]' }
Write-Output $header
$result = Invoke-WebRequest -Method $method -Uri $uri -Headers $header -Body $document
Write-Output "Response: $result"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment