Skip to content

Commit

Permalink
Merge pull request #131 from castle/failover_fix
Browse files Browse the repository at this point in the history
failover response enhancement
  • Loading branch information
bartes authored Mar 22, 2022
2 parents 52a7cea + ac401d8 commit e89c6a9
Show file tree
Hide file tree
Showing 8 changed files with 1,155 additions and 1,047 deletions.
17 changes: 15 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## 1.1.1

**Enhancements**

- [#131](https://github.com/castle/castle-node/pull/131)
* failover response enhancement, removed X-Castle header from allowlist

## 1.1.0

**Enhancements**

- [#127](https://github.com/castle/castle-node/pull/127)
* Introduce a new exception `InvalidRequestTokenError` for facilitating handling of all request token related errors

## 1.0.3

**BugFix:**
Expand Down Expand Up @@ -36,7 +50,7 @@
- dropped EVENTS const with list of events
* [#97](https://github.com/castle/castle-node/pull/114)
**Enhancements:**
- restructuring the codebase
- restructuring the codebase
* [#89](https://github.com/castle/castle-node/pull/89)
* [#90](https://github.com/castle/castle-node/pull/90)
* [#91](https://github.com/castle/castle-node/pull/91)
Expand Down Expand Up @@ -83,4 +97,3 @@
**Enhancements:**

- Re-publish to fix broken build

30 changes: 16 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "http://json.schemastore.org/package",
"name": "@castleio/sdk",
"description": "Castle SDK for Node",
"version": "1.0.3",
"version": "1.1.1",
"main": "dist/index.js",
"repository": "[email protected]:castle/castle-node.git",
"author": {
Expand All @@ -24,37 +24,39 @@
"prepush": "yarn build && yarn lint-fix && yarn prettier-verify",
"prepublishOnly": "yarn build"
},
"files": ["dist/*"],
"files": [
"dist/*"
],
"husky": {
"hooks": {
"pre-push": "yarn prepush"
}
},
"devDependencies": {
"@types/fetch-mock": "7.3.3",
"@types/jest": "26.0.23",
"@types/fetch-mock": "7.3.5",
"@types/jest": "27.4.1",
"@types/lodash.get": "4.4.6",
"@types/lodash.isempty": "4.4.6",
"@types/lodash.merge": "4.6.6",
"@types/lodash.pickby": "4.6.6",
"@types/lodash.reduce": "4.6.6",
"@types/mockdate": "3.0.0",
"@types/node": "15.6.1",
"@types/node-fetch": "2.5.10",
"@types/pino": "6.3.8",
"@types/node": "17.0.22",
"@types/node-fetch": "2.6.1",
"@types/pino": "7.0.5",
"fetch-mock": "9.11.0",
"husky": "6.0.0",
"jest": "27.0.3",
"husky": "7.0.4",
"jest": "27.5.1",
"mockdate": "3.0.5",
"prettier": "2.3.0",
"prettier": "2.6.0",
"rimraf": "3.0.2",
"source-map-support": "^0.5.19",
"ts-jest": "27.0.2",
"ts-node": "10.0.0",
"source-map-support": "^0.5.21",
"ts-jest": "27.1.3",
"ts-node": "10.7.0",
"tslint": "6.1.3",
"tslint-config-prettier": "1.18.0",
"tslint-config-standard": "9.0.0",
"typescript": "4.3.2"
"typescript": "4.6.2"
},
"dependencies": {
"abort-controller": "^3.0.0",
Expand Down
3 changes: 3 additions & 0 deletions src/api/services/api-risk.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const handleFailover = (
}

return {
policy: {
action: configuration.failoverStrategy,
},
action: configuration.failoverStrategy,
failover: true,
failover_reason: reason,
Expand Down
4 changes: 3 additions & 1 deletion src/command/services/command-generate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const CommandGenerateService = {
signal: controller.signal,
method,
headers: CoreGenerateDefaultHeadersService.call(configuration),
...(method !== 'GET' && { body: CoreGenerateRequestBody.call(options, configuration) }),
...(method !== 'GET' && {
body: CoreGenerateRequestBody.call(options, configuration),
}),
},
};
},
Expand Down
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const DEFAULT_ALLOWLIST = [
'te',
'upgrade-insecure-requests',
'user-agent',
'x-castle-client-id',
'x-requested-with',
];
export const TRUSTED_PROXIES = [
Expand Down
6 changes: 4 additions & 2 deletions src/core/services/core-process-response.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const RESPONSE_ERRORS = {
};

const RESPONSE_SUB_ERRORS = {
'invalid_request_token': InvalidRequestTokenError
invalid_request_token: InvalidRequestTokenError,
};

// The body on the request is a stream and can only be
Expand Down Expand Up @@ -66,7 +66,9 @@ export const CoreProcessResponseService = {
// Throw a special exception for subtype errors if defined. Eg. for
// invalid request token, which is a subtype of InvalidParametersError.
// Otherwise, throw exception as defined per status code
const err = RESPONSE_SUB_ERRORS[body.type] || RESPONSE_ERRORS[response.status.toString()];
const err =
RESPONSE_SUB_ERRORS[body.type] ||
RESPONSE_ERRORS[response.status.toString()];

throw new err(`Castle: Responded with ${response.status} code`);
},
Expand Down
21 changes: 12 additions & 9 deletions test/core/services/core-process-response.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,24 @@ describe('CoreProcessResponseService', () => {
});

describe('with invalid request token', () => {
const response = new Response(JSON.stringify({
type: "invalid_request_token",
message: "Invalid Request Token"
}), {
headers: { 'Content-Type': 'application/json' },
status: 422
})
const response = new Response(
JSON.stringify({
type: 'invalid_request_token',
message: 'Invalid Request Token',
}),
{
headers: { 'Content-Type': 'application/json' },
status: 422,
}
);

it('throws InvalidRequestTokenError', async () => {
await expect(
CoreProcessResponseService.call('risk', {}, response, {
info: () => {},
})
).rejects.toThrow(InvalidRequestTokenError)
})
).rejects.toThrow(InvalidRequestTokenError);
});
});
});
});
Expand Down
Loading

0 comments on commit e89c6a9

Please sign in to comment.