Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport/backport 377 to 1.x #400

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/aws/AwsSigv4Signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Connection = require('../Connection')
const Transport = require('../Transport')
const aws4 = require('aws4')
const AwsSigv4SignerError = require('./errors')
const crypto = require('crypto')

const getAwsSDKCredentialsProvider = async () => {
// First try V3
Expand Down Expand Up @@ -78,7 +79,12 @@ function AwsSigv4Signer (opts = {}) {
request.region = opts.region
request.headers = request.headers || {}
request.headers.host = request.hostname
return aws4.sign(request, credentialsState.credentials)
const signed = aws4.sign(request, credentialsState.credentials)
signed.headers['x-amz-content-sha256'] = crypto
.createHash('sha256')
.update(request.body || '', 'utf8')
.digest('hex')
return signed
}

class AwsSigv4SignerConnection extends Connection {
Expand Down
1 change: 1 addition & 0 deletions lib/aws/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { OpenSearchClientError } from '../errors';
interface AwsSigv4SignerOptions {
getCredentials: () => Promise<Credentials>;
region: string;
service?: 'es' | 'aoss';
}

interface AwsSigv4SignerResponse {
Expand Down
6 changes: 5 additions & 1 deletion test/unit/lib/aws/awssigv4signer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { Connection } = require('../../../../index')
const { Client, buildServer } = require('../../../utils')

test('Sign with SigV4', (t) => {
t.plan(3)
t.plan(4)

const mockCreds = {
accessKeyId: uuidv4(),
Expand Down Expand Up @@ -51,6 +51,10 @@ test('Sign with SigV4', (t) => {
const signedRequest = auth.buildSignedRequestObject(request)
t.hasProp(signedRequest.headers, 'X-Amz-Date')
t.hasProp(signedRequest.headers, 'Authorization')
t.same(
signedRequest.headers['x-amz-content-sha256'],
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
)
t.same(signedRequest.service, 'es')
})

Expand Down