OpenSearch Serverless Javascript - Content-Length 403 Error

0

I am trying the OpenSearch Serverless and the requests are working on search requests because it does not have a request body.

The client.index(indexParams) function is not working and just returns "content-length is not a supported SignedHeader in the AWS Opensearch Serverless"

I am using exactly what is specified here https://docs.aws.amazon.com/opensearch-service/latest/developerguide/serverless-clients.html#serverless-javascript

I also tried "aws4-axios" approach and still same error.

2 Answers
0

I just tested this myself locally and it worked. @eginstockinger can you share the code you're using?

elizabsn$ node sample.js
Creating index:
{ acknowledged: true, shards_acknowledged: true, index: 'my-index' }
Adding document:
{
  _index: 'my-index',
  _id: '8wC2F4UBUrNWN66Pz6UQ',
  _version: 1,
  result: 'created',
  _shards: { total: 0, successful: 0, failed: 0 },
  _seq_no: 0,
  _primary_term: 0
}

Here's the script I used (copied from docs and modified host and region:

var AWS = require('aws-sdk');
var aws4  = require('aws4');
var { Client, Connection } = require("@opensearch-project/opensearch");

var client = new Client({
  node: 'https://xd3tlw1kvxtzaykzgxfh.us-east-1.aoss.amazonaws.com',
  Connection: class extends Connection {
    buildRequestObject (params) {
      var request = super.buildRequestObject(params)
      request.service = 'aoss';
      request.region = 'us-east-1'; // e.g. us-east-1

      var contentLength = '0';

      if (request.headers['content-length']) {
        contentLength = request.headers['content-length'];
        request.headers['content-length'] = '0';
      }
      request.headers['x-amz-content-sha256'] = 'UNSIGNED-PAYLOAD';
      request = aws4.sign(request, AWS.config.credentials);
      request.headers['content-length'] = contentLength;

      return request
    }
  }
});

async function index_document() {
  // Create an index with non-default settings.
  var index_name = "my-index";
  var settings = "{ \"settings\": { \"number_of_shards\": 1, \"number_of_replicas\": 0 }, \"mappings\": { \"properties\": { \"title\": {\"type\": \"text\"}, \"director\": {\"type\": \"text\"}, \"year\": {\"type\": \"text\"} } } }";

  var response = await client.indices.create({
    index: index_name,
    body: settings
  });

  console.log("Creating index:");
  console.log(response.body);

  // Add a document to the index
  var document = "{ \"title\": \"Avatar\", \"director\": \"James Cameron\", \"year\": \"2003\" }\n";

  var response = await client.index({
    index: index_name,
    body: document
  });

  console.log("Adding document:");
  console.log(response.body);
}

index_document().catch(console.log);
profile pictureAWS
answered a year ago
  • I'm having this same problem and I literally copy pasted your answer and still getting error.

    content-length is not a supported SignedHeader in the AWS Opensearch Serverless.
    

    Why I try your code exactly this is what comes out in the request header

    {
        'content-length': '58',
        Authorization: 'AWS4-HMAC-SHA256 Credential=AKIA6CU6TCNUT3ERYPFB/20221223/us-east-2/aoss/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-content-sha256;x-amz-date, Signature=...
      }
    

    I notice that the SignedHeaders in Authorization still includes content-length

0

content-length is not supported with OpenSearch Serverless as per the limitation mentioned here: https://docs.aws.amazon.com/opensearch-service/latest/developerguide/serverless-clients.html#:~:text=You%20can%27t%20include%20Content%2DLength%20as%20a%20signed%20header%2C%20otherwise%20you%27ll%20get%20an%20invalid%20signature%20error

I have escalated to internal team and will fix the documents, meanwhile can you try without content-length header with javascript.

AWS
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions