N8N-4134 Add AWS cred testing and http custom calls with credentials (#3924)

*  Add Aws testing and http custom api
This commit is contained in:
agobrech
2022-08-23 19:02:32 +02:00
committed by GitHub
parent a85d565ffc
commit 5285fc1de6
10 changed files with 196 additions and 231 deletions

View File

@@ -1,7 +1,3 @@
import { URL } from 'url';
import { sign } from 'aws4';
import {
IExecuteFunctions,
IHookFunctions,
@@ -9,25 +5,15 @@ import {
IWebhookFunctions,
} from 'n8n-core';
import { ICredentialDataDecryptedObject, IDataObject, INodeExecutionData } from 'n8n-workflow';
import {
ICredentialDataDecryptedObject,
IDataObject,
IHttpRequestOptions,
INodeExecutionData,
} from 'n8n-workflow';
import { IRequestBody } from './types';
function getEndpointForService(
service: string,
credentials: ICredentialDataDecryptedObject,
): string {
let endpoint;
if (service === 'lambda' && credentials.lambdaEndpoint) {
endpoint = credentials.lambdaEndpoint;
} else if (service === 'sns' && credentials.snsEndpoint) {
endpoint = credentials.snsEndpoint;
} else {
endpoint = `https://${service}.${credentials.region}.amazonaws.com`;
}
return (endpoint as string).replace('{region}', credentials.region as string);
}
export async function awsApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
service: string,
@@ -38,32 +24,22 @@ export async function awsApiRequest(
// tslint:disable-next-line:no-any
): Promise<any> {
const credentials = await this.getCredentials('aws');
// Concatenate path and instantiate URL object so it parses correctly query strings
const endpoint = new URL(getEndpointForService(service, credentials) + path);
const securityHeaders = {
accessKeyId: `${credentials.accessKeyId}`.trim(),
secretAccessKey: `${credentials.secretAccessKey}`.trim(),
sessionToken: credentials.temporaryCredentials
? `${credentials.sessionToken}`.trim()
: undefined,
};
const options = sign(
{
// @ts-ignore
uri: endpoint,
const requestOptions = {
qs: {
service,
region: credentials.region as string,
method,
path: '/',
headers: { ...headers },
body: JSON.stringify(body),
path,
},
securityHeaders,
);
method,
body: JSON.stringify(body),
url: '',
headers,
region: credentials?.region as string,
} as IHttpRequestOptions;
try {
return JSON.parse(await this.helpers.request!(options));
return JSON.parse(
await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions),
);
} catch (error) {
const errorMessage =
(error.response && error.response.body && error.response.body.message) ||