-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1411 from blackflux/dev
[Gally]: master <- dev
- Loading branch information
Showing
41 changed files
with
1,209 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
test/util/cache-clearer-disabled.spec.js | ||
test/modules/response-healing.spec.js | ||
test/req-header-overwrite.spec.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/modules/request-recorder/heal-sqs-send-message-batch.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import crypto from 'crypto'; | ||
import { tryParseJson } from './util.js'; | ||
import migration from './heal-sqs/migration.js'; | ||
|
||
export default (requestBody, responseBody, scope, req) => { | ||
if (scope?.basePath !== 'https://sqs.us-west-2.amazonaws.com:443') { | ||
return responseBody; | ||
} | ||
|
||
const header = req?.options?.headers?.['x-amz-target']; | ||
|
||
if (typeof responseBody === 'string' && responseBody.startsWith('<?xml')) { | ||
return migration({ responseBody, header }); | ||
} | ||
|
||
const requestJson = tryParseJson(requestBody); | ||
const responseJson = tryParseJson(responseBody); | ||
|
||
if (header === 'AmazonSQS.SendMessageBatch') { | ||
return { | ||
Successful: requestJson.Entries.map(({ Id, MessageBody }, idx) => ({ | ||
Id, | ||
MessageId: responseJson?.Successful?.[idx]?.MessageId || crypto.randomUUID(), | ||
MD5OfMessageBody: crypto.createHash('md5').update(MessageBody).digest('hex') | ||
})) | ||
}; | ||
} | ||
|
||
return responseBody; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// This code logic is used to migrate legacy AWS SQS xml to json | ||
import xml2js from 'xml2js'; | ||
import objectScan from 'object-scan'; | ||
|
||
const tryParseXML = (body) => { | ||
let parsed = body; | ||
try { | ||
xml2js.parseString(body, (err, result) => { | ||
parsed = JSON.parse(JSON.stringify(result)); | ||
}); | ||
} catch (e) { | ||
return null; | ||
} | ||
return parsed; | ||
}; | ||
|
||
export default ({ responseBody, header }) => { | ||
const responseXml = tryParseXML(responseBody); | ||
if (responseXml !== null) { | ||
if (header === 'AmazonSQS.ListQueueTags') { | ||
const scanner = objectScan(['ListQueueTagsResponse.ListQueueTagsResult[0].Tag[*]'], { | ||
rtn: ({ value }) => [value.Key, value.Value[0]], | ||
afterFn: ({ result }) => Object.fromEntries(result) | ||
}); | ||
const Tags = scanner(responseXml); | ||
return { Tags }; | ||
} | ||
if (header === 'AmazonSQS.GetQueueAttributes') { | ||
const scanner = objectScan(['GetQueueAttributesResponse.GetQueueAttributesResult[0].Attribute[*]'], { | ||
rtn: ({ value }) => [value.Name[0], value.Value[0]], | ||
afterFn: ({ result }) => Object.fromEntries(result) | ||
}); | ||
const Attributes = scanner(responseXml); | ||
return { Attributes }; | ||
} | ||
if (header === 'AmazonSQS.GetQueueUrl') { | ||
if (responseXml?.ErrorResponse?.Error?.[0]?.Code?.[0] === 'AWS.SimpleQueueService.NonExistentQueue') { | ||
return { | ||
__type: 'com.amazonaws.sqs#QueueDoesNotExist', | ||
message: 'The specified queue does not exist.' | ||
}; | ||
} | ||
const QueueUrl = responseXml?.GetQueueUrlResponse?.GetQueueUrlResult?.[0]?.QueueUrl?.[0]; | ||
return { QueueUrl }; | ||
} | ||
if (header === 'AmazonSQS.CreateQueue') { | ||
if (responseXml?.ErrorResponse?.Error?.[0]?.Code?.[0] === 'QueueAlreadyExists') { | ||
return { | ||
__type: 'com.amazonaws.sqs#QueueNameExists', | ||
message: 'The specified queue name does exist.' | ||
}; | ||
} | ||
const QueueUrl = responseXml?.CreateQueueResponse?.CreateQueueResult?.[0]?.QueueUrl?.[0]; | ||
return { QueueUrl }; | ||
} | ||
if (header === 'AmazonSQS.ListQueues') { | ||
const scannerQueueUrls = objectScan( | ||
['ListQueuesResponse.ListQueuesResult[0].QueueUrl[*]'], | ||
{ rtn: 'value', reverse: false } | ||
); | ||
const scannerNextToken = objectScan( | ||
['ListQueuesResponse.ListQueuesResult[0].NextToken[0]'], | ||
{ rtn: 'value', reverse: false, abort: true } | ||
); | ||
return { | ||
QueueUrls: scannerQueueUrls(responseXml), | ||
NextToken: scannerNextToken(responseXml) | ||
}; | ||
} | ||
if (header === 'AmazonSQS.TagQueue') { | ||
return {}; | ||
} | ||
if (header === 'AmazonSQS.SetQueueAttributes') { | ||
return {}; | ||
} | ||
if (header === 'AmazonSQS.SendMessageBatch') { | ||
const scannerSuccessful = objectScan( | ||
['SendMessageBatchResponse.SendMessageBatchResult[0].SendMessageBatchResultEntry[*]'], | ||
{ | ||
rtn: ({ value }) => ({ | ||
Id: value.Id[0], | ||
MessageId: value.MessageId[0], | ||
MD5OfMessageBody: value.MD5OfMessageBody[0] | ||
}), | ||
reverse: false | ||
} | ||
); | ||
const scannerFailed = objectScan( | ||
['SendMessageBatchResponse.SendMessageBatchResult[0].BatchResultErrorEntry[*]'], | ||
{ | ||
rtn: ({ value }) => ({ | ||
Id: value.Id[0], | ||
SenderFault: value.SenderFault[0], | ||
Code: value.Code[0] | ||
}), | ||
reverse: false | ||
} | ||
); | ||
return { | ||
Successful: scannerSuccessful(responseXml), | ||
Failed: scannerFailed(responseXml) | ||
}; | ||
} | ||
} | ||
return responseBody; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
test/modules/request-recorder.spec.js__fixtures/sqs-cassette-bad.json
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import path from 'path'; | ||
import fs from 'smart-fs'; | ||
import { logger } from 'lambda-monitor-logger'; | ||
import { expect } from 'chai'; | ||
import awsSdkWrap from 'aws-sdk-wrap'; | ||
import { | ||
SQSClient, | ||
SendMessageBatchCommand, | ||
GetQueueUrlCommand, | ||
GetQueueAttributesCommand, | ||
ListQueueTagsCommand, | ||
CreateQueueCommand, | ||
ListQueuesCommand, | ||
TagQueueCommand, | ||
SetQueueAttributesCommand | ||
} from '@aws-sdk/client-sqs'; | ||
import objectScan from 'object-scan'; | ||
import { describe } from '../../src/index.js'; | ||
import { NockRecord } from '../server.js'; | ||
import reqHeaderOverwrite from '../req-header-overwrite.js'; | ||
|
||
const aws = awsSdkWrap({ | ||
logger, | ||
services: { | ||
SQS: SQSClient, | ||
'SQS:CMD': { | ||
SendMessageBatchCommand, | ||
GetQueueUrlCommand, | ||
GetQueueAttributesCommand, | ||
ListQueueTagsCommand, | ||
CreateQueueCommand, | ||
ListQueuesCommand, | ||
TagQueueCommand, | ||
SetQueueAttributesCommand | ||
} | ||
} | ||
}); | ||
|
||
const fixtureFolder = `${fs.filename(import.meta.url)}__fixtures`; | ||
const files = fs | ||
.walkDir(fixtureFolder) | ||
.filter((f) => !f.endsWith('.json__expected.json')); | ||
|
||
describe('Testing Response Healing', { | ||
useTmpDir: true, | ||
timestamp: 0, | ||
cryptoSeed: 'd28095c6-19f4-4dc2-a7cc-f7640c032967' | ||
}, () => { | ||
const cassetteFile = 'file1.json'; | ||
let tmpDir; | ||
let nockRecord; | ||
|
||
beforeEach(async ({ dir }) => { | ||
tmpDir = dir; | ||
nockRecord = NockRecord(tmpDir, cassetteFile); | ||
}); | ||
|
||
// eslint-disable-next-line mocha/no-setup-in-describe | ||
files.forEach((f) => { | ||
it(`Testing ${f}`, async ({ fixture }) => { | ||
const { fn, params, cassette } = fixture(f); | ||
fs.smartWrite(path.join(tmpDir, cassetteFile), cassette); | ||
const func = fn.split('.').reduce((p, v) => p[v], aws); | ||
objectScan(['**'], { | ||
filterFn: ({ value, parent, property }) => { | ||
if (value === '$queueUrl') { | ||
// eslint-disable-next-line no-param-reassign | ||
parent[property] = process.env.QUEUE_URL; | ||
} | ||
} | ||
})(params); | ||
const r = await nockRecord(() => func(...params), { heal: 'magic', reqHeaderOverwrite }); | ||
const outFile = path.join(fixtureFolder, `${f}__expected.json`); | ||
const overwritten = fs.smartWrite(outFile, r.expectedCassette); | ||
expect(overwritten).to.deep.equal(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
AWS_REGION: "us-west-2" | ||
AWS_ACCESS_KEY_ID: "XXXXXXXXXXXXXXXXXXXX" | ||
AWS_SECRET_ACCESS_KEY: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | ||
QUEUE_URL: "https://sqs.us-west-2.amazonaws.com/123456789101/service-name-data-local-SomeQueue" |
33 changes: 33 additions & 0 deletions
33
test/modules/response-healing.spec.js__fixtures/json_bad-md5.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"fn": "sqs.sendMessageBatch", | ||
"params": [{ | ||
"messages": [{ "k": 1 }, { "k": 2 }], | ||
"queueUrl": "$queueUrl" | ||
}], | ||
"cassette": [ | ||
{ | ||
"scope": "https://sqs.us-west-2.amazonaws.com:443", | ||
"method": "POST", | ||
"path": "/", | ||
"body": { | ||
"Entries": [ | ||
{ | ||
"Id": "4f494d6b422d1e5fbbe6b6d82584ce7f70c1530f", | ||
"MessageBody": "{\"k\":1}" | ||
} | ||
], | ||
"QueueUrl": "https://sqs.us-west-2.amazonaws.com/123456789101/service-name-data-local-SomeQueue" | ||
}, | ||
"status": 200, | ||
"response": { | ||
"Successful": [ | ||
{ | ||
"Id": "feabf4c420f7eec1fbd3ad1b74e972af9f879cad", | ||
"MessageId": "e40625e7-e66f-4c6c-8d9b-7bb8944b0108", | ||
"MD5OfMessageBody": "some-bad-hash45be96b2d5b9d22cdc4" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.