Skip to content

Commit

Permalink
Get rid of collector types in Azcollectc (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkuzmin authored May 4, 2020
1 parent 713742d commit c48cb80
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 71 deletions.
80 changes: 20 additions & 60 deletions azcollectc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
* @end
* -----------------------------------------------------------------------------
*/
'use strict';

const zlib = require('zlib');
const AlServiceC = require('./al_servicec').AlServiceC;

const COLLECTOR_TYPES = {
CWE: 'cwe',
CWL: 'cwl',
O365: 'o365',
EHUB: 'ehub',
PAWS: 'paws'
const CLOUD_TYPES = {
AWS: 'aws',
AZURE: 'azure'
};

const CLOUD_TYPES = {
AWS: 'aws',
Azure: 'azure'
};
const DEFAULT_RETRY_OPTS = {
factor: 2,
minTimeout: 300,
retries: 7,
maxTimeout: 10000
};

/**
* @class
Expand All @@ -31,55 +31,24 @@ const CLOUD_TYPES = {
* @constructor
* @param {string} apiEndpoint - Alert Logic API hostname
* @param {Object} aimsCreds - Alert Logic API credentials object, refer to AimsC.
* @param {string} [aimsCreds.access_key_id] - Aert Logic API access key id.
* @param {string} [aimsCreds.access_key_id] - Alert Logic API access key id.
* @param {string} [aimsCreds.secret_key] - Alert Logic API secret key.
* @param {string} collectorType - Al collector type: cwl, cwe, o365 etc
* @param {string} cloudType - type of cloud provider: aws, azure etc
* @param {string} collectorType - Alert Logic collector type: cwl, cwe, o365 etc
* @param {string} sendCheckinCompressed - gzip checkin body for transport. Default false.
* @param {Object} retryOptions - refer to retry#retrytimeoutsoptions of 'retry' npm package.
*
*/
class AzcollectC extends AlServiceC {
constructor(apiEndpoint, aimsCreds, collectorType, sendCheckinCompressed, retryOptions) {
'use strict';
super(apiEndpoint, 'azcollect', 'v1', aimsCreds, retryOptions);
constructor(apiEndpoint, aimsCreds, cloudType, collectorType, sendCheckinCompressed, retryOptions) {
const retryOpts = retryOptions ? retryOptions : DEFAULT_RETRY_OPTS;
super(apiEndpoint, 'azcollect', 'v1', aimsCreds, retryOpts);
this._sendCheckinCompressed = sendCheckinCompressed ? sendCheckinCompressed : false;
switch (collectorType){
case COLLECTOR_TYPES.CWE:
case COLLECTOR_TYPES.CWL:
case COLLECTOR_TYPES.PAWS:
this._collectorType = collectorType;
this._cloudType = CLOUD_TYPES.AWS;
break;
case COLLECTOR_TYPES.O365:
case COLLECTOR_TYPES.EHUB:
this._collectorType = collectorType;
this._cloudType = CLOUD_TYPES.AZURE;
break;
default:
// Keep for backward compatibility
this._collectorType = COLLECTOR_TYPES.CWE;
this._cloudType = CLOUD_TYPES.AWS;
break;
// TODO: Should have exception here when cwe-collector is migrated
// throw `Unknown collector type: ${collectorType}`;
}
}

doCheckin(checkinValues) {
// Deprecated:
return this._doCheckinAws(checkinValues);
}

doRegistration(registrationValues) {
// Deprecated:
return this._doRegistrationAws(registrationValues);
}

doDeregistration(registrationValues) {
// Deprecated:
return this._doDeregistrationAws(registrationValues);
this._collectorType = collectorType;
this._cloudType = cloudType;
}

_doCheckinAws(checkinValues) {
'use strict';
const type = this._collectorType;
var functionName = encodeURIComponent(checkinValues.functionName);
var checkinUrl = `/aws/${type}/checkin/${checkinValues.awsAccountId}/` +
Expand All @@ -88,23 +57,20 @@ class AzcollectC extends AlServiceC {
}

_doRegistrationAws(registrationValues) {
'use strict';
const type = this._collectorType;
var functionName = encodeURIComponent(registrationValues.functionName);
return this.post(`/aws/${type}/` +
`${registrationValues.awsAccountId}/${registrationValues.region}/${functionName}`, {body: registrationValues});
}

_doDeregistrationAws(registrationValues) {
'use strict';
const type = this._collectorType;
var functionName = encodeURIComponent(registrationValues.functionName);
return this.deleteRequest(`/aws/${type}/` +
`${registrationValues.awsAccountId}/${registrationValues.region}/${functionName}`, {body: registrationValues});
}

_doCheckinAzure(checkinInput) {
'use strict';
var checkinBody = Object.assign({}, checkinInput);
const type = this._collectorType;
var functionName = encodeURIComponent(checkinBody.web_app_name);
Expand All @@ -117,7 +83,6 @@ class AzcollectC extends AlServiceC {
}

_doRegistrationAzure(regInput) {
'use strict';
var regBody = Object.assign({}, regInput);
const collectorType = this._collectorType;
const functionName = encodeURIComponent(regInput.web_app_name);
Expand All @@ -129,7 +94,6 @@ class AzcollectC extends AlServiceC {
}

_doDeregistrationAzure(deregInput) {
'use strict';
var deregBody = Object.assign({}, deregInput);
const collectorType = this._collectorType;
var functionName = encodeURIComponent(deregBody.web_app_name);
Expand Down Expand Up @@ -161,7 +125,6 @@ class AzcollectC extends AlServiceC {
}

checkin(checkinValues) {
'use strict';
switch(this._cloudType) {
case CLOUD_TYPES.AWS:
return this._doCheckinAws(checkinValues);
Expand All @@ -173,7 +136,6 @@ class AzcollectC extends AlServiceC {
}

register(registrationValues) {
'use strict';
switch(this._cloudType) {
case CLOUD_TYPES.AWS:
return this._doRegistrationAws(registrationValues);
Expand All @@ -185,7 +147,6 @@ class AzcollectC extends AlServiceC {
}

deregister(registrationValues) {
'use strict';
switch(this._cloudType) {
case CLOUD_TYPES.AWS:
return this._doDeregistrationAws(registrationValues);
Expand All @@ -195,7 +156,6 @@ class AzcollectC extends AlServiceC {
break;
}
}

}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alertlogic/al-collector-js",
"version": "1.4.5",
"version": "2.0.0",
"license": "MIT",
"description": "Alert Logic Collector Common Library",
"repository": {
Expand Down
22 changes: 12 additions & 10 deletions test/azcollectc_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Unit Tests', function() {

it('AWS register no data type', function(done) {
var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'cwe');
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'aws', 'cwe');

const checkinValues = {
awsAccountId : '1234567890',
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('Unit Tests', function() {

it('AWS register with data type', function(done) {
var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'cwl');
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'aws', 'cwl');

const checkinValues = {
awsAccountId : '1234567890',
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('Unit Tests', function() {

it('AWS register with custom_fields', function(done) {
var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'cwl');
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'aws', 'cwl');

const checkinValues = {
awsAccountId : '1234567890',
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('Unit Tests', function() {

it('AWS deregister', function(done) {
var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'cwe');
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'aws', 'cwe');
const checkinValues = {
awsAccountId : '1234567890',
functionName : 'test-function',
Expand All @@ -166,7 +166,7 @@ describe('Unit Tests', function() {

it('AWS checkin (no compression)', function(done) {
var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'cwe', false);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'aws', 'cwe', false);
azc.checkin(m_alMock.AWS_CHECKIN).then( resp => {
sinon.assert.calledWith(
fakePost,
Expand All @@ -179,7 +179,7 @@ describe('Unit Tests', function() {

it('AWS checkin (with compression)', function(done) {
var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'cwe', true);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'aws', 'cwe', true);
azc.checkin(m_alMock.AWS_CHECKIN).then( resp => {
sinon.assert.calledWith(
fakePost,
Expand Down Expand Up @@ -207,6 +207,7 @@ describe('Unit Tests', function() {

fakePost = sinon.stub(AzcollectC.prototype, 'post').callsFake(
function fakeFn(path, options) {
console.log('!!!!FAKE POST', path);
return new Promise(function(resolve, reject) {
resolve('ok');
});
Expand All @@ -231,7 +232,8 @@ describe('Unit Tests', function() {

it('Azure register', function(done) {
var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'o365');
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'azure', 'o365');
//console.log('!!!!', azc);
var expectedRegisterBody = {
body: {
app_tenant_id: 'azure-tenant-id',
Expand All @@ -255,7 +257,7 @@ describe('Unit Tests', function() {

it('Azure deregister', function(done) {
var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'ehub');
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'azure', 'ehub');
const deregValues = {
web_app_name : 'azure-web-app-name',
app_tenant_id : 'azure-tenant-id',
Expand All @@ -279,7 +281,7 @@ describe('Unit Tests', function() {

it('Azure checkin (no compression)', function(done) {
var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'ehub', false);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'azure', 'ehub', false);
var expectedCheckinBody = {
body: {
version : '1.0.0',
Expand All @@ -301,7 +303,7 @@ describe('Unit Tests', function() {

it('Azure checkin compressed', function(done) {
var aimsc = new AimsC(m_alMock.AL_API, m_alMock.AIMS_CREDS);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'ehub', true);
var azc = new AzcollectC(m_alMock.INGEST_ENDPOINT, aimsc, 'azure', 'ehub', true);

var expectedCheckin = {
version : '1.0.0',
Expand Down

0 comments on commit c48cb80

Please sign in to comment.