Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.idea
.DS_Store
coverage
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.git
.git
tester/
test/
11 changes: 7 additions & 4 deletions factory.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const config = require('leo-config');
const AWS = require('aws-sdk');
const { fromIni } = require("@aws-sdk/credential-providers");
const merge = require('lodash.merge');
const requireFn = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;


const leoaws = {
cloudformation: require('./lib/cloudformation'),
Expand All @@ -12,10 +14,10 @@ const leoaws = {
sqs: require('./lib/sqs'),
};

function factory (service, options) {
function factory(service, options) {
const configuration = config.leoaws;
if (configuration && configuration.profile && !configuration.credentials) {
configuration.credentials = new AWS.SharedIniFileCredentials({
configuration.credentials = fromIni({
profile: configuration.profile,
role: configuration.role,
});
Expand All @@ -30,8 +32,9 @@ function factory (service, options) {
return leoaws[lowerService](configuration);
} else {
// return a configured AWS service
let serviceLib = requireFn("@aws-sdk/client-" + service.replace(/[A-Z]/g, (a) => "-" + a.toLowerCase()).replace(/^-/, ""));
return {
_service: new AWS[service](configuration),
_service: new serviceLib[service](configuration),
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const dynamodb = require("./lib/dynamodb");
const kms = require("./lib/kms");
const secrets = require("./lib/secretsmanager");
const sqs = require("./lib/sqs");
const AWS = require('aws-sdk');
const { fromIni } = require("@aws-sdk/credential-providers");

/**
*
Expand All @@ -15,7 +15,7 @@ const AWS = require('aws-sdk');
*/
function build(configuration) {
if (configuration.profile && !configuration.credentials) {
configuration.credentials = new AWS.SharedIniFileCredentials({
configuration.credentials = fromIni({
profile: configuration.profile,
role: configuration.role
});
Expand Down
34 changes: 17 additions & 17 deletions lib/cloudformation.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";
const aws = require('aws-sdk');
const { CloudFormation } = require('@aws-sdk/client-cloudformation');
const readline = require('readline');

module.exports = function(configuration) {
let service = new aws.CloudFormation(configuration);
let service = new CloudFormation(configuration);
return {
_service: service,
getStackResources: function(stack) {
return service.listStackResources({
StackName: stack
}).promise().then((data) => {
}).then((data) => {
if (data.NextToken) {
console.log("We need to deal with next token");
}
Expand All @@ -28,7 +28,7 @@ module.exports = function(configuration) {
get: function(stack, opts) {
return service.getTemplate(Object.assign({
StackName: stack,
}, opts)).promise().then(data => JSON.parse(data.TemplateBody));
}, opts)).then(data => JSON.parse(data.TemplateBody));
},
/**
* Create a run a changeset
Expand Down Expand Up @@ -58,7 +58,7 @@ module.exports = function(configuration) {
"CAPABILITY_IAM",
]
}, updateOpts);
return service.createChangeSet(params).promise().then(data => {
return service.createChangeSet(params).then(data => {
changeSetId = data.Id;
service.api.waiters["changeSetCreateComplete"].delay = 5;
return this.waitFor("changeSetCreateComplete", {
Expand All @@ -71,10 +71,10 @@ module.exports = function(configuration) {
function rightPad(val, count) {
return (val + " ".repeat(count)).slice(0, count) + " ";
}
console.log(`${rightPad("Action",30)}${rightPad("Logical ID",30)}${rightPad("Physical ID",30)}${rightPad("Resource Type",30)}${rightPad("Replacement",30)}`);
console.log(`${rightPad("Action", 30)}${rightPad("Logical ID", 30)}${rightPad("Physical ID", 30)}${rightPad("Resource Type", 30)}${rightPad("Replacement", 30)}`);
data.Changes.map(change => {
change = change.ResourceChange;
console.log(`${rightPad(change.Action,30)}${rightPad(change.LogicalResourceId,30)}${rightPad(change.PhysicalResourceId,30)}${rightPad(change.ResourceType,30)}${rightPad(change.Replacement,30)}`);
console.log(`${rightPad(change.Action, 30)}${rightPad(change.LogicalResourceId, 30)}${rightPad(change.PhysicalResourceId, 30)}${rightPad(change.ResourceType, 30)}${rightPad(change.Replacement, 30)}`);
});
const rl = readline.createInterface({
input: process.stdin,
Expand Down Expand Up @@ -108,7 +108,7 @@ module.exports = function(configuration) {
return service.describeChangeSet({
ChangeSetName: changeSetId,
StackName: stack
}).promise().then(cs => {
}).then(cs => {
throw cs;
}).catch((err2) => {
if (err2.StatusReason) {
Expand All @@ -135,15 +135,15 @@ module.exports = function(configuration) {
console.log("Executing Change Set");
resolve(service.executeChangeSet({
ChangeSetName: changeSetId
}).promise().then(data => {
}).then(data => {
service.api.waiters["stackUpdateComplete"].delay = 10;
return this.waitFor("stackUpdateComplete", {
StackName: stack
});
}).catch(err => {
return service.describeStackEvents({
StackName: stack
}).promise().then(data => {
}).then(data => {
let messages = [];
let addLinkMessage = false;
for (let i = 0; i < data.StackEvents.length; i++) {
Expand All @@ -170,7 +170,7 @@ module.exports = function(configuration) {
getStackErrorStatus: function(stack, start) {
return service.describeStackEvents({
StackName: stack
}).promise().then(data => {
}).then(data => {
let messages = [];
let addLinkMessage = false;
for (let i = 0; i < data.StackEvents.length; i++) {
Expand Down Expand Up @@ -209,7 +209,7 @@ module.exports = function(configuration) {
Capabilities: [
"CAPABILITY_IAM",
]
}, updateOpts)).promise().then(data => {
}, updateOpts)).then(data => {
service.api.waiters["stackUpdateComplete"].delay = 10;
return this.waitFor("stackUpdateComplete", {
StackName: stack
Expand All @@ -220,7 +220,7 @@ module.exports = function(configuration) {
} else {
return service.describeStackEvents({
StackName: stack
}).promise().then(data => {
}).then(data => {
let messages = [];
let addLinkMessage = false;
for (let i = 0; i < data.StackEvents.length; i++) {
Expand Down Expand Up @@ -252,10 +252,10 @@ module.exports = function(configuration) {
describeStackResources: function(stack) {
return service.describeStackResources({
StackName: stack
}).promise().then(data => data.StackResources);
}).then(data => data.StackResources);
},
waitFor: function(action, params) {
return service.waitFor(action, params).promise();
return service.waitFor(action, params);
},
createStack: async function createStack(name, template, paramaters = [], waitFor = true, describeStack = true) {
let templateBody;
Expand All @@ -276,7 +276,7 @@ module.exports = function(configuration) {
OnFailure: "DELETE",
[templateBody]: template,
Parameters: paramaters
}).promise().then(stackInfo => {
}).then(stackInfo => {
createInfo = stackInfo;
return stackInfo;
});
Expand Down Expand Up @@ -306,4 +306,4 @@ module.exports = function(configuration) {
}));
}
};
};
};
40 changes: 21 additions & 19 deletions lib/dynamodb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';
const AWS = require('aws-sdk');
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const { DynamoDBDocument } = require("@aws-sdk/lib-dynamodb");
const { NodeHttpHandler } = require('@aws-sdk/node-http-handler');
const https = require('https');
const merge = require('lodash.merge');
const chunk = require('../utils/chunker.js');
Expand All @@ -11,23 +13,23 @@ const logger = require('leo-logger');
/**
*
* @param configuration
* @returns {{_service: AWS.DynamoDB.DocumentClient, get: get, put: put, update: update, scan: scan, query: query, smartQuery: query, batchGetTable: batchGetTable, batchGetHashkey: batchGetHashkey, writeToTableInChunks: writeToTableInChunks}}
* @returns {{_service: DynamoDBDocument, get: get, put: put, update: update, scan: scan, query: query, smartQuery: query, batchGetTable: batchGetTable, batchGetHashkey: batchGetHashkey, writeToTableInChunks: writeToTableInChunks}}
*/
module.exports = function(configuration) {
configuration = merge({
convertEmptyValues: true,
httpOptions: {
agent: new https.Agent({
requestHandler: new NodeHttpHandler({
connectionTimeout: 2000,
requestTimeout: 5000,
httpsAgent: new https.Agent({
ciphers: 'ALL',
// keepAlive: true
}),
connectTimeout: 2000,
timeout: 5000,
},
maxRetries: 2,
})
}),
maxAttempts: 3,
}, configuration.dynamodb || configuration || {});

let docClient = new AWS.DynamoDB.DocumentClient(configuration);
let docClient = DynamoDBDocument.from(new DynamoDBClient(configuration));
return {
_service: docClient,
batchGetHashkey: function(table, hashkey, ids, opts = {}) {
Expand Down Expand Up @@ -124,7 +126,7 @@ module.exports = function(configuration) {
Key: key,
ReturnConsumedCapacity: 'TOTAL',
TableName: table,
}).promise().then(data => {
}).then(data => {
if (!data.Item) {
return opts.default || null;
} else {
Expand Down Expand Up @@ -156,19 +158,19 @@ module.exports = function(configuration) {
Key,
ReturnConsumedCapacity: 'TOTAL',
TableName,
}).promise().then(data => true);
}).then(data => true);
},
query: (params) => {
return docClient.query(params).promise().then(data => data.Items);
return docClient.query(params).then(data => data.Items);
},
scan: function(table) {
if (typeof table === 'string') {
return docClient.scan({
ReturnConsumedCapacity: 'TOTAL',
TableName: table,
}).promise().then(data => data.Items);
}).then(data => data.Items);
} else {
return docClient.scan(table).promise().then(data => data.Items);
return docClient.scan(table).then(data => data.Items);
}
},
smartQuery: function query(params, configuration = {}, stats = {}) {
Expand Down Expand Up @@ -256,9 +258,9 @@ module.exports = function(configuration) {
if (opts.merge) {
assign = (self, key, obj) => {
if (key in self.data) {
if (typeof opts.merge === 'function'){
if (typeof opts.merge === 'function') {
self.data[key] = opts.merge(self.data[key], obj);
}else{
} else {
self.data[key] = merge(self.data[key], obj);
}
return true;
Expand Down Expand Up @@ -399,7 +401,7 @@ module.exports = function(configuration) {
}
existing.map(e => {
let newObj = lookup[key(e)];
if (typeof opts.merge === 'function'){
if (typeof opts.merge === 'function') {
newObj.PutRequest.Item = opts.merge(e, newObj.PutRequest.Item);
} else {
newObj.PutRequest.Item = merge(e, newObj.PutRequest.Item);
Expand Down Expand Up @@ -495,7 +497,7 @@ module.exports = function(configuration) {
command.ReturnValues = opts.ReturnValues;
}

return docClient.update(command).promise().then(data => command.ReturnValues && command.ReturnValues !== 'NONE' ? data : true);
return docClient.update(command).then(data => command.ReturnValues && command.ReturnValues !== 'NONE' ? data : true);
},
updateMulti: function(items, opts = {}) {
opts = merge({
Expand Down
10 changes: 5 additions & 5 deletions lib/kms.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
"use strict";
var aws = require('aws-sdk');
var { KMS } = require('@aws-sdk/client-kms');
module.exports = function(configuration) {
let kms = new aws.KMS(configuration);
let kms = new KMS(configuration);
return {
_service: kms,
decrypt: function(encryptedString) {
return kms.decrypt({
CiphertextBlob: new Buffer(encryptedString, 'base64')
}).promise().then(data => data.Plaintext.toString('ascii'));
CiphertextBlob: Buffer.from(encryptedString, 'base64')
}).then(data => Buffer.from(data.Plaintext).toString('ascii'));
},
encrypt: function(key, value) {
return kms.encrypt({
KeyId: key,
Plaintext: value
}).promise().then(data => data.CiphertextBlob.toString("base64"));
}).then(data => Buffer.from(data.CiphertextBlob).toString("base64"));
}
}
};
6 changes: 3 additions & 3 deletions lib/secretsmanager.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
const aws = require('aws-sdk');
const { SecretsManager } = require('@aws-sdk/client-secrets-manager');

let cache = {};
module.exports = function(configuration) {
let secret = new aws.SecretsManager(configuration);
let secret = new SecretsManager(configuration);
return {
_service: secret,
getSecret: function(secretName, opts) {
Expand All @@ -20,7 +20,7 @@ module.exports = function(configuration) {
return new Promise((resolve, reject) => {
secret.getSecretValue({
SecretId: secretName
}).promise().then(data => {
}).then(data => {
try {
let r = JSON.parse(data.SecretString);
cache[secretName] = {
Expand Down
12 changes: 6 additions & 6 deletions lib/sqs.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
"use strict";
const aws = require('aws-sdk');
const { SQS } = require('@aws-sdk/client-sqs');

module.exports = function(configuration) {
let sqs = new aws.SQS(configuration);
let sqs = new SQS(configuration);

return {
_service: sqs,
deleteMessage: function(params) {
return sqs.deleteMessage(params).promise().then(data => data.ResponseMetadata.RequestId);
return sqs.deleteMessage(params).then(data => data.ResponseMetadata.RequestId);
},
sendMessage: function(params) {
return sqs.sendMessage(params).promise().then(data => data.MessageId);
return sqs.sendMessage(params).then(data => data.MessageId);
},
receiveMessage: function(params) {
return sqs.receiveMessage(params).promise().then(data => data.Messages);
return sqs.receiveMessage(params).then(data => data.Messages);
},
sendMessageBatch: function(params) {
return sqs.sendMessageBatch(params).promise().then(data => data);
return sqs.sendMessageBatch(params).then(data => data);
}
};
};
Loading