Skip to content

Instantly share code, notes, and snippets.

@ivanignatiev
Created February 17, 2015 18:10
Show Gist options
  • Save ivanignatiev/d8fca4ff8b3f729f7337 to your computer and use it in GitHub Desktop.
Save ivanignatiev/d8fca4ff8b3f729f7337 to your computer and use it in GitHub Desktop.
HTTP request to Azure Event Hub with Curl
# REST API documentation https://msdn.microsoft.com/en-us/library/azure/dn790664.aspx
curl -H 'Authorization: SharedAccessSignature sr={Service Bus Namespace}.servicebus.windows.net&sig={Url Encoded Shared Access Key}&se={Time Stamp with Shared Access Key expration}&skn={Shared Access Policy name}' -H 'Content-Type:application/atom+xml;type=entry;charset=utf-8' --data '{Event Data}' https://{Service Bus Namespace}.servicebus.windows.net/{Event Hub Name}/messages
@holms
Copy link

holms commented Jan 17, 2023

Why Microsoft didn't document this. This service is barely usable. They only provide examples in various languages..

@holms
Copy link

holms commented Jan 24, 2023

Just in case someone needs to generate signature for auth headers:

holms@holms:~/Dev/sdk-devops/terraform/prod/analytics$ cat sig.sh 
EVENTHUB_URI=$1
SHARED_ACCESS_KEY_NAME=$2
SHARED_ACCESS_KEY=$3
EXPIRY=${EXPIRY:=$((60 * 60 * 24))} # Default token expiry is 1 day

ENCODED_URI=$(echo -n $EVENTHUB_URI | jq -s -R -r @uri)
TTL=$(($(date +%s) + $EXPIRY))
UTF8_SIGNATURE=$(printf "%s\n%s" $ENCODED_URI $TTL | iconv -t utf8)

HASH=$(echo -n "$UTF8_SIGNATURE" | openssl sha256 -hmac $SHARED_ACCESS_KEY -binary | base64)
ENCODED_HASH=$(echo -n $HASH | jq -s -R -r @uri)

echo -n "SharedAccessSignature sr=$ENCODED_URI&sig=$ENCODED_HASH&se=$TTL&skn=$SHARED_ACCESS_KEY_NAME"

Launch this bash script with such params:

 ./sig.sh yourslug.servicebus.windows.net authRuleName sharedKeyHere

It will produce a line which you need to add after this: curl -H 'Authorization:

@ivanignatiev
Copy link
Author

@holms it is cool to see that SAS generation is achievable in bash, thanks ! :)

@guenhter
Copy link

This article also describes it pretty well I think:

https://learn.microsoft.com/en-us/rest/api/eventhub/generate-sas-token

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment