opensearch-ruby: inconsistent signature signing failures on PUT updates when using sigv4 signing

What is the bug?

We are using the OpenSearch::Aws::Sigv4Client according to instructions, but we have signature errors when trying to update documents. We are using IAM users.

We have two indices in Opensearch and 80% PUT calls are success for one index (user data), but the other index (product info) fails at 100% when we try to update documents. We can successfully import all the data correctly to begin with.

We are switching over from Elasticsearch, and have a lot built out using the previous Elasticsearch gems that these were continued from.

How can one reproduce the bug?

This is how we are setting up the Opensearch client:

    config = { url: Rails.application.config.opensearch_url, port: URI.parse(Rails.application.config.opensearch_url).port }
    aws_iam_credentials = Aws::AssumeRoleWebIdentityCredentials.new(
      region: Rails.application.config.aws_region,
      role_arn: Rails.application.config.aws_iam_role_arn,
      web_identity_token_file: Rails.application.config.aws_web_identity_token_file
    )

    signer = Aws::Sigv4::Signer.new(
      service: 'es',
      region: Rails.application.config.aws_region,
      credentials_provider: aws_iam_credentials
    )
    OpenSearch::Aws::Sigv4Client.new({ **config, adapter: :typhoeus, log: true }, signer)

We have also tried using access/secret like this:


    signer = Aws::Sigv4::Signer.new(service: 'es',
                                    region: Rails.application.config.aws_region,
                                    access_key_id: ENV['OPENSEARCH_ACCESS_KEY'],
                                    secret_access_key: ENV['OPENSEARCH_SECRET_KEY']
                                    )
    OpenSearch::Aws::Sigv4Client.new({ **config, adapter: :typhoeus, log: true }, signer)

What is the expected behavior?

A clear and concise description of what you expected to happen. We expect 100% of updates to be successful

What is your host/environment?

Kubernetes EKS. 1.21

We are using the current HEAD for this repo in our Gemfile (both OS-ruby and sigv4) to include the recent fixes.

Do you have any screenshots?

If applicable, add screenshots to help explain your problem. Log example:

[403] {"message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

The Canonical String for this request should have been
'PUT
/users_staging_index/_doc/31794036

host:vpc-trr-opensearch-staging-joqhkufrgzviea7u5sgnj7tota.us-east-1.es.amazonaws.com
x-amz-content-sha256:fb489c2eba88386ac1d8670360ad211dac0268eaa8ef6bf9360c02f071c37bf1
x-amz-date:20230209T074519Z

host;x-amz-content-sha256;x-amz-date
259087aee273e588d1f839bb37f507a9e76da88e04251eef98f271f854651477'

The String-to-Sign should have been
'AWS4-HMAC-SHA256
20230209T074519Z
20230209/us-east-1/es/aws4_request
fde9b011da5d8acd05b802df02a64bff816cf51df244bde6226d58dc000a1fb9'
"}

Do you have any additional context?

Add any other context about the problem. It seems similar to this issue https://github.com/amzn/selling-partner-api-models/issues/774

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 35 (5 by maintainers)

Most upvoted comments

@nhtruong yes, multi-json is just a shim in front of various serializers, and I suggest we remove it - Rails did it in 2013, https://github.com/rails/rails/pull/10576, or we could do what we did in Grape, ie. users can require multi_json explicitly in which case it will be used - https://github.com/ruby-grape/grape/pull/1623

The monkeypatch seems to have fixed the signature issues that we had. I’ll make a PR.

If you have any observability integrations that instrument the various HTTP clients or AWS SDKs, you may want to check on those. In particular, we’ve found that OpenTelemetry’s Faraday middleware instrumentation can interfere with the signatures

IF my theory is correct then yes, re-instantiate the client or the signer (The signer is an accessible attribute of the client: client.signer = new_signer) once the cred’s expired will solve it. How to implement it is up to you. See if the Ruby AWS SDK has the capability to refresh the cred on its own or at least tell you when it’s about to expire. 😃

Thank you for that extra info! @nhtruong can you think of next steps for debugging this?