我正在尝试验证由我的消费者生成的pact.json。但是,为了验证我需要包含AWS4凭据,以便能够从我的提供商处获得响应。我试图使用customProviderHeaders执行此操作。我正在使用库AWS4(https://github.com/mhart/aws4)来生成令牌。以下是我的代码:
const aws4 = require('aws4');
const path = require('path');
import { before, beforeEach, describe, it } from 'mocha';
const {
Verifier
} = require('../../../node_modules/@pact-foundation/pact');
function getToken() {
const opts: any = {
method: 'GET',
region: 'us-east-2',
service: 'execute-api',
path: '/qa/api/',
host: '123456789.execute-api.us-east-2.amazonaws.com',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
aws4.sign(opts, {accessKeyId: '$AWSACCESSKEY', secretAccessKey: '$AWSSECRETKEY'});
return opts.headers;
}
describe('Pact Verification', () => {
it('should validate the watchlist expectations', () => {
let headers = getToken();
let authToken = headers.Authorization;
let date = headers[`X-Amz-Date`];
let opts = {
provider: 'DealerBlock',
providerBaseUrl: 'https://3ua1cprd53.execute-api.us-east-2.amazonaws.com',
pactUrls: [path.resolve(process.cwd(), 'src/test/pact/path_to_my_json')],
customProviderHeaders: [`Authorization: ${authToken}`, `X-Amz-Date: ${date}`]
};
return new Verifier().verifyProvider(opts)
.then(output => {
console.log('STARTED');
console.log(opts.pactUrls);
console.log('Pact Verification Complete');
console.log(output);
});
});
});
函数getToken()生成一个新令牌,然后我获取令牌和日期,并使用客户提供程序头将它们插入到我的请求中。
我看到以下内容:
INFO: Replacing header 'Authorization: ' with 'Authorization: AWS4-HMAC-SHA256 Credential=AKIAJ5FTCODVMSUTEST/2018908/us-east-2/execute-api/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=ceea9aac0303769da58357cb37cb849cb0bbfc13ff0a25cea977385368531349'
INFO: Replacing header 'X-Amz-Date: ' with 'X-Amz-Date: 20180528T184202Z'
但是我收到以下错误:
Actual: {"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."}
我是否以正确的方式使用customProviderHeaders?或者有没有人对我应该采取哪些不同的做法有任何建议?我可以通过Postman使用相同的凭据发送请求,所以不确定这里是什么。
谢谢!
答案 0 :(得分:0)
我的眼睛看起来不错。
可能是因为您没有在以下语句中插入变量(也似乎没有在任何地方定义):
aws4.sign(opts, {accessKeyId: '$AWSACCESSKEY', secretAccessKey: '$AWSSECRETKEY'});
答案 1 :(得分:0)
当我通过customProviderHeaders传递标题:'Content-Type':'application / x-www-form-urlencoded'时能够正常工作。
即使这个标题在我的消费者生成的json合约中列出,但协议提供者似乎也没有看到它。