Skip to content

Commit

Permalink
Merge pull request #87 from imranalisyed506/im-sdk-changes1
Browse files Browse the repository at this point in the history
ENG-52113: Update to aws-sdk v3 and fix the test cases
  • Loading branch information
imranalisyed506 authored Nov 6, 2023
2 parents 42c8f1e + 7f982fc commit a7acb09
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 43 deletions.
12 changes: 8 additions & 4 deletions checkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
* @end
* -----------------------------------------------------------------------------
*/

const AWS = require('aws-sdk');



const { CloudWatchEvents } = require("@aws-sdk/client-cloudwatch-events");
const { Lambda } = require("@aws-sdk/client-lambda");

const async = require('async');
const AlLogger = require('@alertlogic/al-aws-collector-js').Logger;

function checkCloudWatchEventsRule(event, finalCallback) {
var cwe = new AWS.CloudWatchEvents();
var cwe = new CloudWatchEvents();
async.waterfall([
function(callback) {
cwe.describeRule({Name: event.CloudWatchEventsRule}, function(err, data) {
Expand Down Expand Up @@ -47,7 +51,7 @@ function checkCloudWatchEventsRule(event, finalCallback) {
}

function checkEventSourceMapping(checkinEvent, context, callback) {
var lambda = new AWS.Lambda();
var lambda = new Lambda();
lambda.listEventSourceMappings({FunctionName: context.functionName},
function(err, data) {
if (err) {
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

const debug = require('debug') ('index');
const AWS = require('aws-sdk');
const { KMS } = require("@aws-sdk/client-kms");
const async = require('async');

const { Util: m_alAws } = require('@alertlogic/al-aws-collector-js');
Expand All @@ -26,7 +26,7 @@ function getDecryptedCredentials(callback) {
if (AIMS_CREDS) {
return callback(null);
} else {
const kms = new AWS.KMS();
const kms = new KMS();
kms.decrypt(
{CiphertextBlob: Buffer.from(process.env.aims_secret_key, 'base64')},
(err, data) => {
Expand All @@ -35,7 +35,7 @@ function getDecryptedCredentials(callback) {
} else {
AIMS_CREDS = {
access_key_id: process.env.aims_access_key_id,
secret_key: data.Plaintext.toString('ascii')
secret_key: new TextDecoder("utf-8").decode(data.Plaintext)
};
return callback(null);
}
Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "al-cwe-collector",
"version": "1.3.22",
"version": "1.3.23",
"license": "MIT",
"description": "Alert Logic CloudWatch Events Collector",
"repository": {
Expand All @@ -21,21 +21,26 @@
}
],
"devDependencies": {
"aws-sdk-mock": "^5.8.0",
"clone": "^2.1.2",
"dotenv": "^16.3.1",
"jshint": "^2.13.6",
"mocha": "^10.2.0",
"mocha-jenkins-reporter": "^0.4.8",
"nyc": "^15.1.0",
"rewire": "^7.0.0",
"sinon": "^15.2.0"
"sinon": "^15.2.0",
"@aws-sdk/client-cloudformation": "^3.427.0",
"@aws-sdk/client-cloudwatch": "^3.427.0",
"@aws-sdk/client-cloudwatch-events": "^3.427.0",
"@aws-sdk/client-kms": "^3.427.0",
"@aws-sdk/client-lambda": "^3.427.0",
"@aws-sdk/client-ssm": "^3.427.0",
"@aws-sdk/client-s3": "^3.427.0"
},
"dependencies": {
"@alertlogic/al-aws-collector-js": "4.1.22",
"@alertlogic/al-aws-collector-js": "4.1.24",
"@alertlogic/al-collector-js": "3.0.10",
"async": "^3.2.4",
"aws-sdk": "^2.1454.0",
"cfn-response": "^1.0.1",
"debug": "^4.3.4",
"moment": "^2.29.4"
Expand Down
20 changes: 11 additions & 9 deletions test/al-cwe-collector_test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
var CweCollector = require('../al-cwe-collector').cweCollector;
var AWS = require('aws-sdk-mock');
var m_alCollector = require('@alertlogic/al-collector-js');
const m_al_aws = require('@alertlogic/al-aws-collector-js').Util;
const m_response = require('cfn-response');
const cweMock = require('./cwe_mock');
const sinon = require('sinon');
const assert = require('assert');

var cweStub = require('./cwe_stub');
const { KMS } = require("@aws-sdk/client-kms"),
{ SSM } = require("@aws-sdk/client-ssm");
let alserviceStub = {};
let ingestCStub = {};
let setEnvStub = {};
Expand Down Expand Up @@ -105,16 +106,16 @@ describe('CWE collector Tests', function() {
beforeEach(function () {
decryptStub = sinon.stub().callsFake(function (params, callback) {
const data = {
Plaintext: 'decrypted-sercret-key'
Plaintext: Buffer.from('decrypted-sercret-key')
};
return callback(null, data);
});

AWS.mock('KMS', 'decrypt', decryptStub);
cweStub.mock(KMS, 'decrypt', decryptStub);

AWS.mock('KMS', 'encrypt', function (params, callback) {
cweStub.mock(KMS, 'encrypt', function (params, callback) {
const data = {
CiphertextBlob: Buffer.from('creds-from-file')
CiphertextBlob: Buffer.from('creds-from-file').toString('base64')
};
return callback(null, data);
});
Expand All @@ -124,7 +125,7 @@ describe('CWE collector Tests', function() {
return callback(null, { Parameter: { Value: data.toString('base64') } });
});

AWS.mock('SSM', 'getParameter', ssmStub);
cweStub.mock(SSM, 'getParameter', ssmStub);

responseStub = sinon.stub(m_response, 'send').callsFake(
function fakeFn(event, mockContext, responseStatus, responseData, physicalResourceId) {
Expand All @@ -139,8 +140,9 @@ describe('CWE collector Tests', function() {
restoreAlServiceStub();
setEnvStub.restore();
responseStub.restore();
AWS.restore('KMS');
AWS.restore('SSM');
cweStub.restore(SSM,'getParameter');
cweStub.restore(KMS, 'decrypt');
cweStub.restore(KMS, 'encrypt');
});

it('Check process method get called form handleEvent method if we have records', function (done) {
Expand Down
33 changes: 18 additions & 15 deletions test/cwe_checkin_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ process.env.AWS_REGION = 'us-east-1';
const assert = require('assert');
const rewire = require('rewire');
const sinon = require('sinon');
const AWS = require('aws-sdk-mock');
const cweMock = require('./cwe_mock');
const cweMockErrors = require('./cwe_mock_errors');
const clone = require('clone');
var {AlServiceC} = require('@alertlogic/al-collector-js');
var cweStub = require('./cwe_stub');
const { CloudWatchEvents } = require("@aws-sdk/client-cloudwatch-events"),
{ CloudFormation } = require("@aws-sdk/client-cloudformation"),
{ Lambda } = require("@aws-sdk/client-lambda");
var azcollectStub;

function setAzcollectStub() {
Expand Down Expand Up @@ -320,53 +323,53 @@ describe('CWE Checkin Tests', function() {
});

function mock() {
AWS.mock('CloudFormation', 'describeStacks', function (data, callback) {
cweStub.mock(CloudFormation, 'describeStacks', function (data, callback) {
assert.equal(data.StackName, cweMock.STACK_NAME);
return callback(null, cweMock.CF_DESCRIBE_STACKS_RESPONSE);
});
AWS.mock('CloudWatchEvents', 'describeRule', function (data, callback) {
cweStub.mock(CloudWatchEvents, 'describeRule', function (data, callback) {
assert.equal(data.Name, cweMock.CWE_RULE_NAME);
return callback(null, cweMock.CWE_DESCRIBE_RULE);
});
AWS.mock('CloudWatchEvents', 'listTargetsByRule', function (data, callback) {
cweStub.mock(CloudWatchEvents, 'listTargetsByRule', function (data, callback) {
assert.equal(data.Rule, cweMock.CWE_RULE_NAME);
return callback(null, cweMock.CWE_LIST_TARGETS_BY_RULE);
});
AWS.mock('Lambda', 'listEventSourceMappings', function (data, callback) {
cweStub.mock(Lambda, 'listEventSourceMappings', function (data, callback) {
assert.equal(data.FunctionName, cweMock.CHECKIN_TEST_FUNCTION_NAME);
return callback(null, cweMock.LAMBDA_LIST_EVENTSOURCE_MAPPINGS_OK);
});
}


function unmock() {
AWS.restore('CloudFormation', 'describeStacks');
AWS.restore('CloudWatchEvents', 'describeRule');
AWS.restore('CloudWatchEvents', 'listTargetsByRule');
AWS.restore('Lambda', 'listEventSourceMappings');
cweStub.restore(CloudFormation, 'describeStacks');
cweStub.restore(CloudWatchEvents, 'describeRule');
cweStub.restore(CloudWatchEvents, 'listTargetsByRule');
cweStub.restore(Lambda, 'listEventSourceMappings');
}


function mockCWEDescribeRule(fun) {
AWS.restore('CloudWatchEvents', 'describeRule');
AWS.mock('CloudWatchEvents', 'describeRule', function (data, callback) {
cweStub.restore(CloudWatchEvents, 'describeRule');
cweStub.mock(CloudWatchEvents, 'describeRule', function (data, callback) {
assert.equal(data.Name, cweMock.CWE_RULE_NAME);
return fun(data, callback);
});
}


function mockCWEListTargetsByRule(fun) {
AWS.restore('CloudWatchEvents', 'listTargetsByRule');
AWS.mock('CloudWatchEvents', 'listTargetsByRule', function (data, callback) {
cweStub.restore(CloudWatchEvents, 'listTargetsByRule');
cweStub.mock(CloudWatchEvents, 'listTargetsByRule', function (data, callback) {
assert.equal(data.Rule, cweMock.CWE_RULE_NAME);
return fun(data, callback);
});
}

function mockLambdaListEventSourceMappings(fun) {
AWS.restore('Lambda', 'listEventSourceMappings');
AWS.mock('Lambda', 'listEventSourceMappings', function (data, callback) {
cweStub.restore(Lambda, 'listEventSourceMappings');
cweStub.mock(Lambda, 'listEventSourceMappings', function (data, callback) {
assert.equal(data.FunctionName, cweMock.CHECKIN_TEST_FUNCTION_NAME);
return fun(data, callback);
});
Expand Down
15 changes: 15 additions & 0 deletions test/cwe_stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const sinon = require('sinon');
const sandbox = sinon.createSandbox();

function mock(service, methodName, callback) {
return sandbox.stub(service.prototype, methodName).callsFake(callback);
}

function restore(serviceName, methodName) {
serviceName.prototype[methodName].restore();
}

module.exports = {
mock: mock,
restore: restore
};
15 changes: 8 additions & 7 deletions test/cwe_test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

const assert = require('assert');
const rewire = require('rewire');
var AWS = require('aws-sdk-mock');
const cweMock = require('./cwe_mock');
var cweRewire = rewire('../index');

var cweStub = require('./cwe_stub');
const { KMS } = require("@aws-sdk/client-kms");

describe('CWE Unit Tests', function() {

describe('getStatisticsFunctions()', () => {
Expand Down Expand Up @@ -123,15 +124,15 @@ describe('CWE Unit Tests', function() {
});

afterEach(function() {
AWS.restore('KMS', 'decrypt');
cweStub.restore(KMS, 'decrypt');
});

it('if AIMS_CREDS are declared already it returns ok', function(done) {
cweRewire.__set__('AIMS_CREDS', {
access_key_id : ACCESS_KEY_ID,
secret_key: DECRYPTED_SECRET_KEY
});
AWS.mock('KMS', 'decrypt', function (data, callback) {
cweStub.mock(KMS, 'decrypt', function (data, callback) {
throw Error('don\'t call me');
});
rewireGetDecryptedCredentials(function(err) { if (err === null) done(); });
Expand All @@ -142,9 +143,9 @@ describe('CWE Unit Tests', function() {
process.env.aims_access_key_id = ACCESS_KEY_ID;
process.env.aims_secret_key = ENCRYPTED_SECRET_KEY_BASE64;

AWS.mock('KMS', 'decrypt', function (data, callback) {
cweStub.mock(KMS, 'decrypt', function (data, callback) {
assert.equal(data.CiphertextBlob, ENCRYPTED_SECRET_KEY);
return callback(null, {Plaintext : DECRYPTED_SECRET_KEY});
return callback(null, { Plaintext: Buffer.from(DECRYPTED_SECRET_KEY) });
});
rewireGetDecryptedCredentials(function(err) {
assert.equal(err, null);
Expand All @@ -160,7 +161,7 @@ describe('CWE Unit Tests', function() {
cweRewire.__set__('AIMS_CREDS', undefined);
process.env.aims_access_key_id = ACCESS_KEY_ID;
process.env.aims_secret_key = Buffer.from('wrong_key').toString('base64');
AWS.mock('KMS', 'decrypt', function (data, callback) {
cweStub.mock(KMS, 'decrypt', function (data, callback) {
assert.equal(data.CiphertextBlob, 'wrong_key');
return callback('error', 'stack');
});
Expand Down

0 comments on commit a7acb09

Please sign in to comment.