Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Catch ASG error of failing to upload WAF policy #343

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 5 additions & 3 deletions app/waf/src/controllers/wafpolicy.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@ export class WafpolicyController extends BaseController {
wafpolicy.id,
);
} catch (error) {
throw new HttpErrors.unprocessableEntity(
'check wafpolicy from asg service failed',
);
if (error.code === 404) {
throw new HttpErrors.NotFound(`ASG error: ${error.message}`);
} else {
throw new HttpErrors.UnprocessableEntity(`ASG error: ${error.message}`);
}
Comment on lines +230 to +234
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ASG error may be not accurate. How do we know if fails because of ASG not because of some reason else.
Furthermore, I think this change's benefit is adding ${error.message} in the error response. if (error.code === 404) is useless.

}

if (!resp || !resp[0]) {
Expand Down
56 changes: 56 additions & 0 deletions app/waf/test/acceptance/wafpolicy.controller.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,62 @@ describe('WafpolicyController', () => {
},
);

it(
'get ' +
prefix +
'/wafpolicies/${id}/adcs/${adcId}: ASG fails when check wafpolicy',
async () => {
const wafpolicy = await givenWafpolicyData(wafapp, {
tenantId: 'a random id',
public: true,
});

const adc = await givenAdcData(wafapp, {
management: {
trustedDeviceId: uuid(),
},
});

checkWafpolicyStub.throws(new Error('OMG'));

const resp = await client
.get(prefix + `/wafpolicies/${wafpolicy.id}/adcs/${adc.id}`)
.set('X-Auth-Token', ExpectedData.userToken)
.set('tenant-id', ExpectedData.tenantId)
.expect(422);

expect(resp.body.error.message).equal('ASG error: OMG');
},
);

it(
'get ' + prefix + '/wafpolicies/${id}/adcs/${adcId}: ASG returns not found',
async () => {
const wafpolicy = await givenWafpolicyData(wafapp, {
tenantId: 'a random id',
public: true,
});

const adc = await givenAdcData(wafapp, {
management: {
trustedDeviceId: uuid(),
},
});

let err = new Error('Not found');
Object.assign(err, {code: 404});
checkWafpolicyStub.throws(err);

const resp = await client
.get(prefix + `/wafpolicies/${wafpolicy.id}/adcs/${adc.id}`)
.set('X-Auth-Token', ExpectedData.userToken)
.set('tenant-id', ExpectedData.tenantId)
.expect(404);

expect(resp.body.error.message).equal('ASG error: Not found');
},
);

it('get ' + prefix + '/wafpolicies: of all', async () => {
const wafpolicy = await givenWafpolicyData(wafapp);

Expand Down
46 changes: 25 additions & 21 deletions test/onboard-collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1992,33 +1992,37 @@
"script": {
"id": "8a7c44c1-f82e-4877-b56b-406c1f5efb7d",
"exec": [
"pm.test('response code is 200', function () {",
" pm.response.to.have.status(200);",
"});",
"",
"let resp = pm.response.json();",
"",
"console.log(resp.wafpolicyondevice.state);",
"",
"let retry = Number(pm.environment.get('UPLOAD_RETRY'));",
"if (retry === 0) {",
" pm.test('Upload WAF policy timeout', function () {",
" pm.expect(false).to.be.true;",
" });",
"}",
"pm.environment.set('UPLOAD_RETRY', --retry);",
"",
"pm.test('WAF policy state is not ERROR', function () {",
" pm.expect(resp.wafpolicyondevice.state).to.not.have.string('ERROR');",
"pm.test('response code is 200 or 404', function () {",
" console.log(pm.response.code);",
" pm.expect(pm.response.code).to.be.oneOf([200, 404]);",
"});",
"",
"if (resp.wafpolicyondevice.state !== 'AVAILABLE') {",
"if (pm.response.code === 404) {",
" // ASG may return 404 sometimes during uploading",
" postman.setNextRequest('wait-wafpolicy-upload');",
" setTimeout(function(){}, 10000)",
"}",
"} else {",
" let resp = pm.response.json();",
"",
" console.log(resp.wafpolicyondevice.state);",
"",
""
" let retry = Number(pm.environment.get('UPLOAD_RETRY'));",
" if (retry === 0) {",
" pm.test('Upload WAF policy timeout', function () {",
" pm.expect(false).to.be.true;",
" });",
" }",
" pm.environment.set('UPLOAD_RETRY', --retry);",
"",
" pm.test('WAF policy state is not ERROR', function () {",
" pm.expect(resp.wafpolicyondevice.state).to.not.have.string('ERROR');",
" });",
"",
" if (resp.wafpolicyondevice.state !== 'AVAILABLE') {",
" postman.setNextRequest('wait-wafpolicy-upload');",
" setTimeout(function(){}, 10000)",
" }",
"}"
],
"type": "text/javascript"
}
Expand Down