n8n/packages/nodes-base/nodes/Aws/S3/GenericFunctions.ts

128 lines
4.5 KiB
TypeScript
Raw Normal View History

2020-04-15 15:42:37 -07:00
import {
sign,
2020-04-20 11:44:22 -07:00
} from 'aws4';
import {
get,
} from 'lodash';
2020-04-15 15:42:37 -07:00
import {
OptionsWithUri,
2020-04-20 11:44:22 -07:00
} from 'request';
2020-04-15 15:42:37 -07:00
import {
parseString,
2020-04-20 11:44:22 -07:00
} from 'xml2js';
2020-04-15 15:42:37 -07:00
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, service: string, method: string, path: string, body?: string | Buffer, query: IDataObject = {}, headers?: object, option: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('aws');
2020-04-15 15:42:37 -07:00
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
2020-04-20 17:33:38 -07:00
const endpoint = `${service}.${region || credentials.region}.amazonaws.com`;
2020-04-15 15:42:37 -07:00
// Sign AWS API request with the user credentials
const signOpts = {headers: headers || {}, host: endpoint, method, path: `${path}?${queryToString(query).replace(/\+/g, '%2B')}`, body};
2020-04-15 15:42:37 -07:00
2020-09-01 13:29:56 -07:00
sign(signOpts, { accessKeyId: `${credentials.accessKeyId}`.trim(), secretAccessKey: `${credentials.secretAccessKey}`.trim()});
2020-04-15 15:42:37 -07:00
const options: OptionsWithUri = {
headers: signOpts.headers,
method,
qs: query,
uri: `https://${endpoint}${signOpts.path}`,
2020-04-15 15:42:37 -07:00
body: signOpts.body,
};
2020-04-20 17:33:38 -07:00
2020-04-15 15:42:37 -07:00
if (Object.keys(option).length !== 0) {
Object.assign(options, option);
}
try {
return await this.helpers.request!(options);
} catch (error) {
const errorMessage = error.response.body.message || error.response.body.Message || error.message;
2020-04-15 15:42:37 -07:00
if (error.statusCode === 403) {
if (errorMessage === 'The security token included in the request is invalid.') {
throw new Error('The AWS credentials are not valid!');
} else if (errorMessage.startsWith('The request signature we calculated does not match the signature you provided')) {
throw new Error('The AWS credentials are not valid!');
}
}
throw new Error(`AWS error response [${error.statusCode}]: ${errorMessage}`);
}
}
export async function awsApiRequestREST(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, service: string, method: string, path: string, body?: string, query: IDataObject = {}, headers?: object, options: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
const response = await awsApiRequest.call(this, service, method, path, body, query, headers, options, region);
2020-04-15 15:42:37 -07:00
try {
return JSON.parse(response);
} catch (e) {
return response;
}
}
export async function awsApiRequestSOAP(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, service: string, method: string, path: string, body?: string | Buffer, query: IDataObject = {}, headers?: object, option: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
const response = await awsApiRequest.call(this, service, method, path, body, query, headers, option, region);
2020-04-15 15:42:37 -07:00
try {
return await new Promise((resolve, reject) => {
parseString(response, { explicitArray: false }, (err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
} catch (e) {
return e;
}
}
export async function awsApiRequestSOAPAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, propertyName: string, service: string, method: string, path: string, body?: string, query: IDataObject = {}, headers: IDataObject = {}, option: IDataObject = {}, region?: string): Promise<any> { // tslint:disable-line:no-any
2020-04-15 15:42:37 -07:00
const returnData: IDataObject[] = [];
let responseData;
do {
responseData = await awsApiRequestSOAP.call(this, service, method, path, body, query, headers, option, region);
2020-04-15 15:42:37 -07:00
//https://forums.aws.amazon.com/thread.jspa?threadID=55746
if (get(responseData, `${propertyName.split('.')[0]}.NextContinuationToken`)) {
query['continuation-token'] = get(responseData, `${propertyName.split('.')[0]}.NextContinuationToken`);
}
if (get(responseData, propertyName)) {
if (Array.isArray(get(responseData, propertyName))) {
returnData.push.apply(returnData, get(responseData, propertyName));
} else {
returnData.push(get(responseData, propertyName));
}
}
if (query.limit && query.limit <= returnData.length) {
return returnData;
}
} while (
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== undefined &&
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== 'false'
);
return returnData;
}
function queryToString(params: IDataObject) {
return Object.keys(params).map(key => key + '=' + params[key]).join('&');
}