Skip to content

Commit

Permalink
✨ Add credentials injection to S3 AWS
Browse files Browse the repository at this point in the history
  • Loading branch information
agobrech committed Aug 10, 2022
1 parent 9cd5010 commit 0ac510e
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions packages/nodes-base/nodes/Aws/S3/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
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,
const requestOptions = {
qs: {
service,
path,
query,
},
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);
}
Expand Down

0 comments on commit 0ac510e

Please sign in to comment.