From c5c7d2c63b96b40bd56c8b72caa8fc2fc98ef019 Mon Sep 17 00:00:00 2001 From: Slobodan Pavkov Date: Fri, 8 Sep 2023 10:20:24 +0200 Subject: [PATCH 1/3] 3398502 - Fix ACS PSTN Sip Configuration SDK tests --- .../test-resources/test-resources-post.ps1 | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 sdk/communication/test-resources/test-resources-post.ps1 diff --git a/sdk/communication/test-resources/test-resources-post.ps1 b/sdk/communication/test-resources/test-resources-post.ps1 new file mode 100644 index 000000000000..5a39cc8007ed --- /dev/null +++ b/sdk/communication/test-resources/test-resources-post.ps1 @@ -0,0 +1,78 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +# This script is used to set up SIP Configuration domains for Azure Communication Services SIP Routing SDK GA tests + +# It is invoked by the https://github.com/Azure/azure-sdk-for-python/blob/main/eng/New-TestResources.ps1 +# script after the ARM template, defined in https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/storage/test-resources.json, +# is finished being deployed. The ARM template is responsible for creating the Storage accounts needed for live tests. + +param ( + [hashtable] $DeploymentOutputs, + [string] $TenantId, + [string] $TestApplicationId, + [string] $TestApplicationSecret +) + +# By default stop for any error. +if (!$PSBoundParameters.ContainsKey('ErrorAction')) { + $ErrorActionPreference = 'Stop' +} + +function Log($Message) { + Write-Host ('{0} - {1}' -f [DateTime]::Now.ToLongTimeString(), $Message) +} + +Log 'Starting sdk\communication\test-resources\test-resources-post.ps1' + +$communicationServiceEndpoint = $DeploymentOutputs["COMMUNICATION_SERVICE_ENDPOINT"] + +if ($communicationServiceEndpoint -notmatch '\/$') { + Log "adding trailing slash to $communicationServiceEndpoint" + $communicationServiceEndpoint = $communicationServiceEndpoint + "/" +} + +$communicationServiceApiKey = $DeploymentOutputs["COMMUNICATION_SERVICE_ACCESS_KEY"] +$testDomain = $DeploymentOutputs["AZURE_TEST_DOMAIN"] + +$payload = @" +{"domains": { "$testDomain": {"enabled": true}},"trunks": null,"routes": null} +"@ + +$utcNow = [DateTimeOffset]::UtcNow.ToString('r', [cultureinfo]::InvariantCulture) +$contentBytes = [Text.Encoding]::UTF8.GetBytes($payload) +$sha256 = [System.Security.Cryptography.HashAlgorithm]::Create('sha256') +$contentHash = $sha256.ComputeHash($contentBytes) +$contentHashBase64String = [Convert]::ToBase64String($contentHash) +$endpointParsedUri = [System.Uri]$communicationServiceEndpoint +$hostAndPort = $endpointParsedUri.Host +$apiVersion = "2023-04-01-preview" +$urlPathAndQuery = $communicationServiceEndpoint + "sip?api-version=$apiVersion" +$stringToSign = "PATCH`n/sip?api-version=$apiVersion`n$utcNow;$hostAndPort;$contentHashBase64String" +$hmacsha = New-Object System.Security.Cryptography.HMACSHA256 +$hmacsha.key = [System.Convert]::FromBase64String($communicationServiceApiKey) +$signatureBytes = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($stringToSign)) +$requestSignatureBase64String = [Convert]::ToBase64String($signatureBytes) +$authorizationValue = "HMAC-SHA256 SignedHeaders=date;host;x-ms-content-sha256&Signature=$requestSignatureBase64String" + +$headers = @{ + "Authorization" = $authorizationValue + "x-ms-content-sha256" = $contentHashBase64String + "Date" = $utcNow + "X-Forwarded-Host" = $hostAndPort +} + +try { + Log "Inserting Domains in SipConfig for Communication Livetest Dynamic Resource..." + $response = Invoke-RestMethod -ContentType "application/merge-patch+json" -Uri $urlPathAndQuery -Method PATCH -Headers $headers -UseBasicParsing -Body $payload -Verbose | ConvertTo-Json + Log $response + Log "Inserted Domains in SipConfig for Communication Livetest Dynamic Resource" +} +catch { + Write-Host "Exception while invoking the SIP Config Patch:" + Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ + Write-Host "StatusDescription:" $_.Exception.Response + Write-Host "Error Message:" $_.ErrorDetails.Message +} + +Log 'Finishing sdk\communication\test-resources\test-resources-post.ps1' From cbc4902e4f11a75e38f5ea4f4368059b3bdf17dd Mon Sep 17 00:00:00 2001 From: Slobodan Pavkov Date: Fri, 8 Sep 2023 11:52:39 +0200 Subject: [PATCH 2/3] Renaming --- sdk/communication/test-resources/test-resources-post.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/communication/test-resources/test-resources-post.ps1 b/sdk/communication/test-resources/test-resources-post.ps1 index 5a39cc8007ed..df4ac441853e 100644 --- a/sdk/communication/test-resources/test-resources-post.ps1 +++ b/sdk/communication/test-resources/test-resources-post.ps1 @@ -49,9 +49,9 @@ $hostAndPort = $endpointParsedUri.Host $apiVersion = "2023-04-01-preview" $urlPathAndQuery = $communicationServiceEndpoint + "sip?api-version=$apiVersion" $stringToSign = "PATCH`n/sip?api-version=$apiVersion`n$utcNow;$hostAndPort;$contentHashBase64String" -$hmacsha = New-Object System.Security.Cryptography.HMACSHA256 -$hmacsha.key = [System.Convert]::FromBase64String($communicationServiceApiKey) -$signatureBytes = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($stringToSign)) +$hasher = New-Object System.Security.Cryptography.HMACSHA256 +$hasher.key = [System.Convert]::FromBase64String($communicationServiceApiKey) +$signatureBytes = $hasher.ComputeHash([Text.Encoding]::ASCII.GetBytes($stringToSign)) $requestSignatureBase64String = [Convert]::ToBase64String($signatureBytes) $authorizationValue = "HMAC-SHA256 SignedHeaders=date;host;x-ms-content-sha256&Signature=$requestSignatureBase64String" From 58f1bbeb955914addd445c1e57fd1974065fe7b2 Mon Sep 17 00:00:00 2001 From: Slobodan Pavkov Date: Fri, 8 Sep 2023 13:03:00 +0200 Subject: [PATCH 3/3] Spellcheck --- sdk/communication/test-resources/test-resources-post.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sdk/communication/test-resources/test-resources-post.ps1 b/sdk/communication/test-resources/test-resources-post.ps1 index df4ac441853e..041569fdc7c2 100644 --- a/sdk/communication/test-resources/test-resources-post.ps1 +++ b/sdk/communication/test-resources/test-resources-post.ps1 @@ -49,7 +49,9 @@ $hostAndPort = $endpointParsedUri.Host $apiVersion = "2023-04-01-preview" $urlPathAndQuery = $communicationServiceEndpoint + "sip?api-version=$apiVersion" $stringToSign = "PATCH`n/sip?api-version=$apiVersion`n$utcNow;$hostAndPort;$contentHashBase64String" +/* cspell:disable */ $hasher = New-Object System.Security.Cryptography.HMACSHA256 +/* cspell:enable */ $hasher.key = [System.Convert]::FromBase64String($communicationServiceApiKey) $signatureBytes = $hasher.ComputeHash([Text.Encoding]::ASCII.GetBytes($stringToSign)) $requestSignatureBase64String = [Convert]::ToBase64String($signatureBytes)