Skip to content

Commit

Permalink
Backport/backport 377 to 1.x (#400)
Browse files Browse the repository at this point in the history
* Backport #366 into 1.x

Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com>

* Add missing types for AwsSigv4SignerOptions.service (#377)

* Add missing types for AwsSigv4SignerOptions.service

Signed-off-by: magoz <apps@magoz.is>

* update changelog with Add missing types for AwsSigv4SignerOptions.service

Signed-off-by: magoz <apps@magoz.is>

* fix: service is optional

Signed-off-by: magoz <apps@magoz.is>

---------

Signed-off-by: magoz <apps@magoz.is>
(cherry picked from commit c44cc1c)

---------

Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com>
Co-authored-by: Magoz <apps@magoz.is>
  • Loading branch information
harshavamsi and magoz committed Feb 22, 2023
1 parent 8156473 commit 73d8028
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
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

0 comments on commit 73d8028

Please sign in to comment.