Xynanxdb
Xynanxdb

Reputation: 1

How to use REST API to delete message on Azure Storage Queue in bash?

I would like to know how to delete a message on Azure Storage Queue. Currently I keep getting the following error.

<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:47de86b5-3003-004d-0fc0-dd56e6000000
Time:2024-07-24T11:56:26.1466778Z</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request '<signature>' is not the same as any computed signature. Server used following string to sign: 'DELETE











x-ms-date:Wed, 24 Jul 2024 11:56:25 GMT
x-ms-version:2016-05-31
/gildedhonordev/ghmatchedtickets/messages/<messageid>'.</AuthenticationErrorDetail></Error>

Using this page as guidance, I have created a function that successfully get the queued message. However, the same method keeps failing at the Authorization when I want to delete a message.

Below are my codes:

get_queued_message() {
    DATE_ISO=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")
    VERSION="2016-05-31"
    HEADER_RESOURCE="x-ms-date:$DATE_ISO\nx-ms-version:$VERSION"
    URL_RESOURCE="/$STORAGE_ACC/$QUEUE_NAME/messages"
    STRING_TO_SIGN="GET\n\n\n\n\n\n\n\n\n\n\n\n$HEADER_RESOURCE\n$URL_RESOURCE"

    DECODED_KEY="$(echo -n "$STORAGE_KEY" | base64 -d -w0 | xxd -p -c256)"
    SIGN=$(printf "$STRING_TO_SIGN" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$DECODED_KEY" -binary | base64 -w0)
    
    RET=$(curl -X GET \
        -H "x-ms-date:$DATE_ISO" \
        -H "x-ms-version:$VERSION" \
        -H "Authorization: SharedKey $STORAGE_ACC:$SIGN" \
        -H "Content-Length:0" \
        "https://$STORAGE_ACC.queue.core.windows.net/$QUEUE_NAME/messages")

    MESSAGE=$(echo "$RET" | grep -oP '<MessageText>\K[^<]+')
    MESSAGE_ID=$(echo "$RET" | grep -oP '<MessageId>\K[^<]+')
    POP_RECEIPT=$(echo "$RET" | grep -oP '<PopReceipt>\K[^<]+')
}

delete_queued_message() {
    DATE_ISO=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")
    VERSION="2016-05-31"
    HEADER_RESOURCE="x-ms-date:$DATE_ISO\nx-ms-version:$VERSION"
    URL_RESOURCE="/$STORAGE_ACC/$QUEUE_NAME/messages/$MESSAGE_ID?popreceipt=$POP_RECEIPT"
    STRING_TO_SIGN="DELETE\n\n\n\n\n\n\n\n\n\n\n\n$HEADER_RESOURCE\n$URL_RESOURCE"

    DECODED_KEY="$(echo -n "$STORAGE_KEY" | base64 -d -w0 | xxd -p -c256)"
    SIGN=$(printf "$STRING_TO_SIGN" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$DECODED_KEY" -binary | base64 -w0)

    curl -X DELETE \
        -H "x-ms-date:$DATE_ISO" \
        -H "x-ms-version:$VERSION" \
        -H "Authorization: SharedKey $STORAGE_ACC:$SIGN" \
        "https://$STORAGE_ACC.queue.core.windows.net/$QUEUE_NAME/messages/$MESSAGE_ID?popreceipt=$POP_RECEIPT"
}

Any help would be appreciated

Upvotes: 0

Views: 96

Answers (1)

Venkatesan
Venkatesan

Reputation: 10490

How to use REST API to delete message on Azure Storage Queue in bash?

You can use the below script to Delete Azure Storage Queue message using Bash.

Script:

#!/bin/bash

# Function to convert base64 to hex
base64_to_hex() {
    echo -n "$1" | base64 --decode | od -An -tx1 | tr -d ' \n'
}

# Function to delete the queued message
delete_queued_message() {
    STORAGE_ACC=$1
    STORAGE_KEY=$2
    QUEUE_NAME=$3
    MESSAGE_ID=$4
    POP_RECEIPT=$5

    DATE_ISO=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")
    VERSION="2020-04-08"
    HEADER_RESOURCE="x-ms-date:$DATE_ISO\nx-ms-version:$VERSION"
    CANONICALIZED_RESOURCE="/$STORAGE_ACC/$QUEUE_NAME/messages/$MESSAGE_ID\npopreceipt:$POP_RECEIPT"
    STRING_TO_SIGN="DELETE\n\n\n\n\n\n\n\n\n\n\n\n$HEADER_RESOURCE\n$CANONICALIZED_RESOURCE"

    DECODED_KEY=$(base64_to_hex "$STORAGE_KEY")
    SIGN=$(printf "$STRING_TO_SIGN" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$DECODED_KEY" -binary | base64)

    curl -X DELETE \
        -H "x-ms-date:$DATE_ISO" \
        -H "x-ms-version:$VERSION" \
        -H "Authorization: SharedKey $STORAGE_ACC:$SIGN" \
        "https://$STORAGE_ACC.queue.core.windows.net/$QUEUE_NAME/messages/$MESSAGE_ID?popreceipt=$POP_RECEIPT"
}


STORAGE_ACC="venkat326123"
STORAGE_KEY="T3czZpu1gZzzzzzzztD9nyWw=="
QUEUE_NAME="queue1"
MESSAGE_ID="aeacazzzzzf6914a"
POP_RECEIPT="AgAzzzAAAA09p8nc3d2gE="


delete_queued_message $STORAGE_ACC $STORAGE_KEY $QUEUE_NAME $MESSAGE_ID $POP_RECEIPT

The above script executed and deleted azure storage queue message in my environment.

Portal: enter image description here

Reference: Delete Message (REST API) - Azure Storage | Microsoft Learn

Upvotes: 0

Related Questions