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

@@ -15,7 +15,7 @@ import {
IWebhookFunctions,
} from 'n8n-core';
import { IDataObject, JsonObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
import { IDataObject, IHttpRequestOptions, JsonObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
export async function awsApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
@@ -30,43 +30,24 @@ export async function awsApiRequest(
// tslint:disable-next-line:no-any
): Promise<any> {
const credentials = await this.getCredentials('aws');
const endpoint = new URL(
(((credentials.s3Endpoint as string) || '').replace('{region}', credentials.region as string) ||
`https://${service}.${credentials.region}.amazonaws.com`) + path,
);
// Sign AWS API request with the user credentials
const signOpts = {
headers: headers || {},
host: endpoint.host,
const requestOptions = {
qs: {
service,
path,
query,
},
method,
path: `${endpoint.pathname}?${queryToString(query).replace(/\+/g, '%2B')}`,
body,
} as Request;
const securityHeaders = {
accessKeyId: `${credentials.accessKeyId}`.trim(),
secretAccessKey: `${credentials.secretAccessKey}`.trim(),
sessionToken: credentials.temporaryCredentials
? `${credentials.sessionToken}`.trim()
: undefined,
};
sign(signOpts, securityHeaders);
const options: OptionsWithUri = {
headers: signOpts.headers,
method,
qs: query,
uri: endpoint.href,
body: signOpts.body,
};
body: JSON.stringify(body),
url: '',
headers,
//region: credentials?.region as string,
} as IHttpRequestOptions;
if (Object.keys(option).length !== 0) {
Object.assign(options, option);
Object.assign(requestOptions, option);
}
try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}