Skip to content

Commit

Permalink
✨ Add S3 SES Textract credentials injection
Browse files Browse the repository at this point in the history
  • Loading branch information
agobrech committed Aug 10, 2022
1 parent 0ac510e commit 1318711
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 73 deletions.
10 changes: 10 additions & 0 deletions packages/nodes-base/credentials/Aws.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,17 @@ export class Aws implements ICredentialType {
} else if (service === 's3' && credentials.s3Endpoint) {
endpoint = credentials.s3Endpoint;
}
else if (service === 'ses' && credentials.sesEndpoint) {
endpoint = credentials.sesEndpoint;
}
else if (service === 'rekognition' && credentials.rekognitionEndpoint) {
endpoint = credentials.rekognitionEndpoint;
}
else if (service === 'sqs' && credentials.sqsEndpoint) {
endpoint = credentials.sqsEndpoint;
}
else {
console.log('ERROR SQS ', service);
endpoint = `https://${service}.${credentials.region}.amazonaws.com`;
}
endpoint = new URL((endpoint as string).replace('{region}', credentials.region as string));
Expand Down
33 changes: 9 additions & 24 deletions packages/nodes-base/nodes/Aws/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
IWebhookFunctions,
} from 'n8n-core';

import { ICredentialDataDecryptedObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
import { ICredentialDataDecryptedObject, IHttpRequestOptions, NodeApiError, NodeOperationError } from 'n8n-workflow';

function getEndpointForService(
service: string,
Expand Down Expand Up @@ -40,35 +40,20 @@ export async function awsApiRequest(
): 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);

// Sign AWS API request with the user credentials
const signOpts = { headers: headers || {}, host: endpoint.host, method, path, 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,
uri: endpoint.href,
body: signOpts.body,
const requestOptions = {
qs: {
service,
method,
path,
},
};
method,
body: JSON.stringify(body),
url: '',
headers,
region: credentials?.region as string,
} as IHttpRequestOptions;

try {
return await this.helpers.requestWithAuthentication.call(this,'aws',options);
return await this.helpers.requestWithAuthentication.call(this,'aws', requestOptions);
} catch (error) {
throw new NodeApiError(this.getNode(), error, { parseXml: true });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Aws/S3/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function awsApiRequest(
body: JSON.stringify(body),
url: '',
headers,
region: credentials?.region as string,
//region: credentials?.region as string,
} as IHttpRequestOptions;

if (Object.keys(option).length !== 0) {
Expand Down
39 changes: 12 additions & 27 deletions packages/nodes-base/nodes/Aws/SES/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
IWebhookFunctions,
} from 'n8n-core';

import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
import { IDataObject, IHttpRequestOptions, NodeApiError, NodeOperationError } from 'n8n-workflow';

import { get } from 'lodash';

Expand All @@ -28,35 +28,20 @@ export async function awsApiRequest(
): Promise<any> {
const credentials = await this.getCredentials('aws');

const endpoint = new URL(
(((credentials.sesEndpoint 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, 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,
},
method,
uri: endpoint.href,
body: signOpts.body as string,
};
body: JSON.stringify(body),
url: '',
headers,
region: credentials?.region as string,
} as IHttpRequestOptions;

try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this,'aws', requestOptions);
} catch (error) {
throw new NodeApiError(this.getNode(), error, { parseXml: true });
}
Expand Down
33 changes: 12 additions & 21 deletions packages/nodes-base/nodes/Aws/Textract/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IHttpRequestOptions,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
Expand Down Expand Up @@ -46,30 +47,20 @@ export async function awsApiRequest(
): 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);

// Sign AWS API request with the user credentials
const signOpts = { headers: headers || {}, host: endpoint.host, method, path, 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,
},
method,
uri: endpoint.href,
body: signOpts.body,
};
body: JSON.stringify(body),
url: '',
headers,
region: credentials?.region as string,
} as IHttpRequestOptions;

try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
} catch (error) {
if (error?.response?.data || error?.response?.body) {
const errorMessage = error?.response?.data || error?.response?.body;
Expand Down

0 comments on commit 1318711

Please sign in to comment.