0

I am calling aws with async call and I'm getting this. <![CDATA["Signature expired: 20171114T190104Z is now earlier than 20180323T213705Z (20180323T214205Z - 5 min.)"]]>.

sI know that this is most likely caused by my machine having the clock off, but how do I fix it? The time is set to be determine automatically like so. enter image description here

I'm using Fedora.

This answer suggested that the AWS keys might be timing out(?) (I don't fully understand this one). If that's the case how do I fix this? I tried manually changing my clock both -5min and +5min, as suggested by the error, but it didn't do anything.

Edit: The same error appears when using a live heroku server.

Edit2: I'm using Node Express and making the call like this:

function(callback) {
  // third call
  var results = '';
  var options = {
    host: urlObject.host,
    protocol: urlObject.protocol,
    path: urlObject.path,
    headers: {
      Authorization: `AWS4-HMAC-SHA256 Credential=key/20171114/us-west-1/awis/aws4_request, SignedHeaders=host;x-amz-date, Signature=key`,
      'Content-Type': 'application/xml',
      'X-Amz-Date': '20171114T190104Z',
      Accept: 'application/xml',
    },
  };
  https.get(options, resource => {
    resource.setEncoding('utf8');
    resource.on('data', function(data) {
      // results += JSON.parse(data);
      results += data;
    });
    resource.on('end', function() {
      callback(null, results);
    });
    resource.on('error', function(err) {
      callback(err);
    });
  });
},
Alex Ironside
  • 4,658
  • 11
  • 59
  • 119

1 Answers1

4

You'll need to provide the current date using the X-Amz-Date header, not the date from the example code. This blog post has an example of how to do that in Node.js to follow the format that the AWS API expects:

// get the various date formats needed to form our request
var amzDate = getAmzDate(new Date().toISOString());

// this function converts the generic JS ISO8601 date format to the specific format the AWS API wants
function getAmzDate(dateStr) {
  var chars = [":","-"];
  for (var i=0;i<chars.length;i++) {
    while (dateStr.indexOf(chars[i]) != -1) {
      dateStr = dateStr.replace(chars[i],"");
    }
  }
  dateStr = dateStr.split(".")[0] + "Z";
  return dateStr;
}

Then, of course, you'll want to assign that amzDate variable to the X-Amz-Date header in your request.

Long term, I think you'll find it is more useful to use the official Node.js AWS SDK instead of trying to custom-make these queries to the format that AWS expects, so you may want to explore that!

Chris Foster
  • 2,639
  • 2
  • 23
  • 30
  • 1
    Ok thanks. The blog post helped. Now I need to get my client to tell me the url for the bucket and it should work. Thanks! – Alex Ironside Mar 28 '18 at 17:52