-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(api-rest): refactor ajax method to not relying on side effects #11498
Changes from 4 commits
709c4a5
91d263f
a14ac21
0302220
cb62a75
ea0094d
2d94b84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,39 +180,42 @@ export class RestClient { | |
return this._request(params, isAllResponse); | ||
} | ||
|
||
// Signing the request in case there credentials are available | ||
return this.Credentials.get().then( | ||
credentials => { | ||
return this._signed({ ...params }, credentials, isAllResponse, { | ||
region, | ||
service, | ||
}).catch(error => { | ||
if (DateUtils.isClockSkewError(error)) { | ||
const { headers } = error.response; | ||
const dateHeader = headers && (headers.date || headers.Date); | ||
const responseDate = new Date(dateHeader); | ||
const requestDate = DateUtils.getDateFromHeaderString( | ||
params.headers['x-amz-date'] | ||
); | ||
|
||
// Compare local clock to the server clock | ||
if (DateUtils.isClockSkewed(responseDate)) { | ||
DateUtils.setClockOffset( | ||
responseDate.getTime() - requestDate.getTime() | ||
); | ||
|
||
return this.ajax(urlOrApiInfo, method, init); | ||
} | ||
} | ||
|
||
throw error; | ||
}); | ||
}, | ||
err => { | ||
logger.debug('No credentials available, the request will be unsigned'); | ||
return this._request(params, isAllResponse); | ||
let credentials; | ||
try { | ||
credentials = await this.Credentials.get(); | ||
} catch (error) { | ||
logger.debug('No credentials available, the request will be unsigned'); | ||
return this._request(params, isAllResponse); | ||
} | ||
let signedParams; | ||
try { | ||
signedParams = this._sign({ ...params }, credentials, { | ||
region, | ||
service, | ||
}); | ||
const response = await axios(signedParams); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elorzafe It's from the original implementation. My understanding it this prevents repeated call to |
||
return isAllResponse ? response : response.data; | ||
} catch (error) { | ||
logger.debug(error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a little bit of context to this log message? |
||
if (DateUtils.isClockSkewError(error)) { | ||
const { headers } = error.response; | ||
const dateHeader = headers && (headers.date || headers.Date); | ||
const responseDate = new Date(dateHeader); | ||
const requestDate = DateUtils.getDateFromHeaderString( | ||
signedParams.headers['x-amz-date'] | ||
); | ||
|
||
// Compare local clock to the server clock | ||
if (DateUtils.isClockSkewed(responseDate)) { | ||
DateUtils.setClockOffset( | ||
responseDate.getTime() - requestDate.getTime() | ||
); | ||
|
||
return this.ajax(urlOrApiInfo, method, init); | ||
} | ||
} | ||
); | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -365,7 +368,7 @@ export class RestClient { | |
|
||
/** private methods **/ | ||
|
||
private _signed(params, credentials, isAllResponse, { service, region }) { | ||
private _sign(params, credentials, { service, region }) { | ||
const { signerServiceInfo: signerServiceInfoParams, ...otherParams } = | ||
params; | ||
|
||
|
@@ -400,12 +403,7 @@ export class RestClient { | |
|
||
delete signed_params.headers['host']; | ||
|
||
return axios(signed_params) | ||
.then(response => (isAllResponse ? response : response.data)) | ||
.catch(error => { | ||
logger.debug(error); | ||
throw error; | ||
}); | ||
return signed_params; | ||
} | ||
|
||
private _request(params, isAllResponse = false) { | ||
|
Check failure
Code scanning / CodeQL
Hard-coded credentials