Skip to content

Commit

Permalink
Merge pull request #96 from honzajerabek/aws-credentials-fn
Browse files Browse the repository at this point in the history
Fix: Unify getting aws / serverless credentials
  • Loading branch information
k1LoW authored Feb 8, 2022
2 parents 29cf199 + 8b44f37 commit 29ee0fd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
25 changes: 25 additions & 0 deletions getAwsOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function getAwsOptions(provider) {
if (provider.cachedCredentials && typeof(provider.cachedCredentials.accessKeyId) != 'undefined'
&& typeof(provider.cachedCredentials.secretAccessKey) != 'undefined'
&& typeof(provider.cachedCredentials.sessionToken) != 'undefined') {

return {
// Temporarily disabled the below below because Serverless framework is not interpolating ${env:foo}
// in provider.credentials.region or provider.cachedCredentials.region
// region: provider.cachedCredentials.region,
region: provider.getRegion(),
credentials: {
accessKeyId: provider.cachedCredentials.accessKeyId,
secretAccessKey: provider.cachedCredentials.secretAccessKey,
sessionToken: provider.cachedCredentials.sessionToken,
}
}
} else {
return {
region: provider.getRegion() || provider.getCredentials().region,
credentials: provider.getCredentials().credentials
}
}
}

module.exports = getAwsOptions
34 changes: 8 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const minimatch = require('minimatch');
const path = require('path');
const fs = require('fs');
const resolveStackOutput = require('./resolveStackOutput')
const getAwsOptions = require('./getAwsOptions')
const messagePrefix = 'S3 Sync: ';
const mime = require('mime');
const child_process = require('child_process');
Expand Down Expand Up @@ -81,37 +82,18 @@ class ServerlessS3Sync {

client() {
const provider = this.serverless.getProvider('aws');
let awsCredentials, region;
if (provider.cachedCredentials && typeof(provider.cachedCredentials.accessKeyId) != 'undefined'
&& typeof(provider.cachedCredentials.secretAccessKey) != 'undefined'
&& typeof(provider.cachedCredentials.sessionToken) != 'undefined') {
// Temporarily disabled the below below because Serverless framework is not interpolating ${env:foo}
// in provider.credentials.region or provider.cachedCredentials.region
// region = provider.cachedCredentials.region
region = provider.getRegion();
awsCredentials = {
accessKeyId: provider.cachedCredentials.accessKeyId,
secretAccessKey: provider.cachedCredentials.secretAccessKey,
sessionToken: provider.cachedCredentials.sessionToken,
}
} else {
region = provider.getRegion() || provider.getCredentials().region;
awsCredentials = provider.getCredentials().credentials;
}
let s3Options = {
region: region,
credentials: awsCredentials
};
if(this.getEndpoint() && this.isOffline()) {
s3Options.endpoint = new provider.sdk.Endpoint(this.serverless.service.custom.s3Sync.endpoint);
s3Options.s3ForcePathStyle = true;
}
const s3Options = getAwsOptions(provider)

if(this.getEndpoint() && this.isOffline()) {
s3Options.endpoint = new provider.sdk.Endpoint(this.serverless.service.custom.s3Sync.endpoint);
s3Options.s3ForcePathStyle = true;
}
const s3Client = new provider.sdk.S3(s3Options);
if(this.getEndpoint() && this.isOffline()) {
//see: https://github.com/aws/aws-sdk-js/issues/1157
s3Client.shouldDisableBodySigning = () => true
}
return s3.createClient({ s3Client });
return s3.createClient({ s3Client });
}

sync() {
Expand Down
9 changes: 4 additions & 5 deletions resolveStackOutput.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const getAwsOptions = require('./getAwsOptions')

function resolveStackOutput(plugin, outputKey) {
const provider = plugin.serverless.getProvider('aws');
const awsCredentials = provider.getCredentials();
const cfn = new provider.sdk.CloudFormation({
region: provider.getRegion(),
credentials: awsCredentials.credentials
});
const options = getAwsOptions(provider)
const cfn = new provider.sdk.CloudFormation(options);
const stackName = provider.naming.getStackName();

return cfn
Expand Down

0 comments on commit 29ee0fd

Please sign in to comment.