Table of contents
1.
Introduction
2.
AWS Signature
2.1.
Step 1: Creating a Canonical String
2.2.
Step 2: Use the canonical request and metadata to create a string
2.3.
Step 3: Create a signing key and signature
2.4.
Step 4: Attach the signature to the request
3.
Setting up AWS and SQS
3.1.
IAM
3.2.
Create the SQS
4.
Sending authenticated requests from Postman to AWS
5.
Frequently Asked Questions
5.1.
What’s a hashing algorithm?
5.2.
Why is AWS used?
5.3.
What’s the benefit of using Postman instead of testing manually?
6.
Conclusion
Last Updated: Mar 27, 2024

AWS Signature in Postman

Author Satvik Gupta
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

AWS stands for Amazon Web Services, which is a suite of cloud computing services that Amazon offers for businesses, enterprises, and even personal projects developed by people. It offers database management, user authentication, website hosting, analytic tools, artificial intelligence tools, etc. For every request made to any AWS service, AWS verifies the identity of the application sending the request using AWS Signature

Postman is an API development tool. It is used to develop, test, design and use APIs (Application Programming Interface). It’s especially useful in testing APIs during development, since it can automate sending requests, and even the order in which they are sent. 

Authentication using AWS signature in postman

In this blog, we will discuss how AWS Signature works, how we can use it in Postman, and see a demo implementation as well.

AWS Signature

AWS Signature4

 

Like any HTTP request, verifying the identity of the application sending the request is essential. Multiple authentication methods exist, such as  Bearer Tokens, OAuth, etc. For AWS requests, Amazon has defined a customised, unique way to authenticate the requests so that they know they are coming from an authorised user.

All AWS services created after 2014 use AWS Signature 4 Verification method. Since this is the latest, most secure and state-of-the-art method designed by Amazon, we will discuss this in detail. 

There are four steps to signing a request in AWS Signature 4. You must have your AWS AccessKey ID and Secret Access Key for these steps. You can take them from AWS IAM (Identity and Access Management) console. (If no access account exists, you must create one). The steps are:

  1. Create a canonical request using the specification described by AWS
  2. Use the canonical request and metadata, such as date, region of the AWS service, etc., to create a string.
  3. Use the AWS signing key and additional metadata, similar to the one used above, to create a signing key. Use the signing key and the string created in step 2 to create a signature.
  4. Attach the signature to the HTTP request and send the request!


Confused? Too many new terms at once? Don’t worry, we got you! 

Let’s now look at all these steps in more detail.

Step 1: Creating a Canonical String

From the official AWS documentation, the canonical string is represented by:

CanonicalRequest = HTTPRequestMethod + '\n' + 
				   CanonicalURI + '\n' + 
				   CanonicalQueryString + '\n' + 
				   CanonicalHeaders + '\n' + 
				   SignedHeaders + '\n' + 
				   HexEncode(Hash(RequestPayload))


Let us see these, one by one, without going into extreme detail.

  • HTTP Request Method - refers to the type of HTTP request it is, for example- POST,GET,PUT, etc.
     
  • CanonicalURI - This refers to the request URL, but only the data after the host.
    For example, if your request URL is iam.amazonaws.com\user-8hdae1ac\service-accounts\
    Then canonical URI should include only \user-8hdae1ac\service-accounts\
     
  • Canonical Query String - This consists of the query after the URL, for example:
    ?action=showData&user-type=admin&sort=asc
    If this is empty, pass an empty string.
     
  • Canonical Headers - This includes all the headers in the form of key-value pairs separated by a new line. 
    For example:
    content-type:application/x-www-form-urlencoded; charset=utf-8\n
     
  • Signed Headers- These are the list of headers that you want AWS to consider. Sometimes browsers and other services, such as proxy servers, might attach their own headers that you haven’t attached. AWS will ignore the headers not mentioned under signed headers.
     
  • HexEncode(Hash(Payload)) - This sounds confusing, but it actually isn’t! Payload is simply the body of your HTTP request. Hashing is passing that body, in string form, to a hash function such as SHA-256 and getting the result. We convert the hash output to hexadecimal format using HexEncode.

 

This process creates our canonical string. Now, we hash this canonical string using the same hashing algorithm we used for the payload. The output of the hash function is our hashed canonical request. We will use this later.

Step 2: Use the canonical request and metadata to create a string

Again, we will go to the official AWS documentation for reference, which says:

StringToSign = Algorithm + \n + 
			   RequestDateTime + \n + 
			   CredentialScope + \n + 
			   HashedCanonicalRequest

 

  • Algorithm - This is the hashing algorithm we are using. We need to match the hashing algorithm we use with the names specified in AWS. For example, for SHA-256, the Algorithm name in AWS is AWS4-HMAC-SHA256.
     
  • RequestDateTime- This is simply the date and time of the request in ISO8601 basic format.
     
    NOTE: This Date value must be the same as we have used in previous steps or will use in future steps.
     
  • CredentialScope: This includes the date (in YYYYMMDD format), the region of the service you are targeting, the name of the service, and ending with the fixed string aws4_request\n”, where \n is a newline character. It looks like this:
    20180708/us-east1/iam/aws4_request\n
     
  • HashedCanonicalRequest- This is the string that was generated at the end of Step 1 (after hashing)


This process generates our string, which we will now sign in our next step. We will call this string stringToSign.

Step 3: Create a signing key and signature

We do not sign our string directly with our secret key for extra security. To add additional layers of security, we generate a new signing key from our secret key and other data. Let us see what AWS documentation tells us to do.

kSecret = your secret access key 
kDate = HMAC("AWS4" + kSecret, Date) 
kRegion = HMAC(kDate, Region) 
kService = HMAC(kRegion, Service) 
kSigning = HMAC(kService, "aws4_request")


What’s this HMAC thing mentioned here?

HMAC is a function that takes two values - a secret key and the data to sign. Using complex cryptographic operations and a hash function, HMAC generates a hash output similar to the one SHA-256 generates. 

When we send a message to a receiver, we send the data and the HMAC output. The receiver uses their copy of the secret key (same as the sender's) to generate an HMAC of the data at their end. If their HMAC matches ours, they will know that the data sent has not been tampered with, and is valid and secure. Otherwise, they will reject the message.

Here, we make repeated use of HMAC. The output of one step is used as the input to the next step as the key, and the data is kept as Date, Region, Service, and the fixed string “aws4_request”.
The final output becomes our signing key

Now, to calculate the signature, we call HMAC again, with the signing key as the key and the stringToSign generated in Step 2 as data. We hex-encode the HMAC function's output, which becomes our final signature.

signature = HexEncode(HMAC(kSigning, string_to_sign))

Step 4: Attach the signature to the request

Finally, just one step is left!
Now, we simply attach our signature to the Authorization header in our request in the following way:

Authorization: algorithm Credential=access key ID/credential scope, SignedHeaders=SignedHeaders, Signature=signature


The algorithm, credential scope and signed headers are the same as in Steps 1 and 2. The Access Key ID is your AWS Access Key ID (this is not the same as your secret key, be careful with it!). The signature is the signature that we calculated at the end of Step 3. 

And that’s it! 🥳 

But that’s a lot of work to understand and implement, isn’t it? Well, Amazon agrees. 

That’s why their SDKs (Software Development Kits) for JavaScript, .NET, Java, Ruby, and many more - don’t require you to implement all this yourself. You simply give your secret key and access ID, and whenever you invoke a request to an AWS service, the SDK will do the rest for you. 

So, if you’re making your backend server in any of these frameworks, you don’t need to bother about any of this. 

But what if we want to add our AWS API to our Postman testing? Will we have to implement all this? Fortunately, the answer is no. Postman implements this for you. Let’s try it out!

Setting up AWS and SQS

Creating and securing the queue

Before trying it out in Postman, we need to set up AWS and any service we can try. We have chosen SQS, which stands for Simple Queue Service, as that is the simplest one. It will be enough for our demonstration purposes. SQS stores, receives and sends messages in a queue-like manner. Using Postman and SQS, we will test out AWS Signature 4 authentication.

We assume that the user already has an AWS account. If not, then it needs to be created. 

IAM

The first thing we will do in AWS is set up IAM, which stands for Identity and Access Management.

We will create a new IAM user, who will only be granted access to SQS services. AWS allows us to have varying amounts and scope of permissions for any user we create. Here we will allow only SQS, as that is enough for our purposes.

Here’s what you need to do:

  • Log in to AWS and search for IAM. Click on Users, as shown:
     
Searching for IAM in AWS Console

 

  • Click on add user
Adding user in IAM

 

  • Enter the username, and make sure Programmatic access is enabled (this is what gives us the Access Key ID and Secret Access Key that we need)
Choose username and enable programmatic access

 

  • On the next page, choose “Attach existing policies directly” and search for “SQS”. Click the checkbox next to “AmazonSQSFullAccess”. 
Allow AmazonSQSFullAccess

 

  • Continue onto the following pages, and leave all settings as default until the user is created.
Save access key ID and secret access key

 

Save the Access Key ID and Secure Access Key in a secure location
Note that anyone who has your AccessKeyID and Secure Access Key can do anything that this user is allowed to do.

Go to the user’s details by clicking on the user name (queue_ninja in this example). 

Copy user's ARN from user details

Copy the ARN number mentioned here, and save it. We will be needing this later.

Create the SQS

Now let’s go to the next step, creating the SQS. Search for SQS in AWS, and click on CREATE QUEUE. You will see the following screen:

Create SQS

You can choose either of Standard or FIFO queue for this example, and you can choose your desired name for the Queue as well.

Scroll down to permissions, and select Only the specified AWS Accounts, IAM Users and Roles for both sending and receiving.

In the text box, paste the ARN number of the user we copied earlier.

Paste the ARN we copied into textbox of SQS permissions

Leave all other options as default, and click on Create Queue.

You will be taken to this page. Copy the URL and save it.

Copy the URL from queue details

Click on Send and Receive Messages, and send a test message.

Send a test message to the queue

Now, it’s time to go into Postman and try to receive the message.

Sending authenticated requests from Postman to AWS

Try receiving the message in Postman

Let’s now go into Postman, and try to receive the message we just sent. 

So, go into Postman, and create a new GET Request. Paste the URL of the SQS you just created. 

In Params, set a Param with Key Action, and Value ReceiveMessage.

Setting parameters in Postman

And then, hit send, and send the request. What response do we get?

Error response from AWS server to Postman

We got an error message! 

But that’s okay, this just means AWS security is working correctly, since we haven’t attached our credentials yet!

So, to test if Postman can authenticate using Amazon Signature 4, go into the Authorization tab in Postman, and select AWS Signature.

Choose AWS Signature in Postman Authorization

On the right-hand side, enter your access key and secret key. Scroll down and set Service Name to sqs.

Enter Access Key and Secret Key

Now, hit send again! What response do we get this time?

Correct response received from AWS

We get the correct response, with our message body! Voila! 🥳

Now, let’s check something. Go into the headers tab, and expand the value for Authorization.

Authorization header matches the format we saw before

We see that it is in the same format that we discussed earlier!

Frequently Asked Questions

What’s a hashing algorithm?

A hashing algorithm is a function or an algorithm, that takes a single piece of data as input. It then produces a single-value string or binary output of a fixed size. For example, SHA-256 always produces a hash of 256 bits. The algorithm should always result in the same output for the same data. Hashes are irreversible, which means, given the hash, you cannot know what the original data was. Even slight changes in the original data result in a totally new hash value.

Why is AWS used?

AWS offers robust, battle-tested and highly scalable services to its users. It allows anyone setting up their website/tech platform to operate at the scale of Amazon and simultaneously serve millions of users without any lag. Users can use database services, AI tools, analytic tools, web hosting, content delivery, and many, many more. These are the same services Amazon uses if needed, so they are guaranteed to be reliable and effective.

What’s the benefit of using Postman instead of testing manually?

Postman allows automated testing for everything. In complex software, pieces of code and APIs are often interrelated with each other. This means that changing one API might affect many others. If you manually test, you will have to keep track of which API is related to which other. Postman can automate this for you, with a single click, you can run all APIs together and test them. It also offers a host of other services, such as documentation, development and design of APIs.

Conclusion

In this blog, we have explored what AWS and Postman are. We have discussed in detail what steps are taken in calculating the authentication for AWS using AWS Signature 4. We have seen canonical URLs, hashing using SHA-256, HMAC, and attaching signatures to requests. We also demoed using AWS Signature 4 in Postman.

We hope you leave this article with a broader knowledge of AWS, Postman and Authentication. We recommend that you explore our different articles on AWS and Postman as well, such as:

Security and Identity in AWS

SQS in AWS

Define, Develop and Test your API in Postman

You can practice questions on various problems on Coding Ninjas Studio, attempt mock tests, go through interview experiences, interview bundle, and go along guided paths for preparations, and a whole lot more!

Keep coding, keep reading Ninjas. 

Live masterclass