Skip to content

Commit

Permalink
Do not run collection if unregistered (#104)
Browse files Browse the repository at this point in the history
* Do not run collection if unregistered

Cleanup

Code cleanup

Fix

Fix reistration

* Update deps

* Add ps_spec
  • Loading branch information
kkuzmin authored Apr 8, 2020
1 parent 6b95b62 commit 9a7ca86
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 23 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alertlogic/paws-collector",
"version": "1.1.9",
"version": "1.2.0",
"license": "MIT",
"description": "Alert Logic AWS based API Poll Log Collector Library",
"repository": {
Expand All @@ -15,7 +15,7 @@
],
"scripts": {
"lint": "jshint --exclude \"./node_modules/*\" **/*.js",
"test": "JUNIT_REPORT_PATH=./test/report.xml nyc --reporter=cobertura mocha --colors --reporter mocha-jenkins-reporter",
"test": "JUNIT_REPORT_PATH=./test/report.xml nyc --reporter=cobertura mocha --colors",
"create-collector": "node create-collector.js",
"rel": "npm publish --access=public"
},
Expand All @@ -25,14 +25,13 @@
"clone": "*",
"jshint": "^2.9.5",
"mocha": "^6.2.2",
"mocha-jenkins-reporter": "^0.4.2",
"nyc": "^14.1.1",
"rewire": "^4.0.1",
"sinon": "^7.5.0",
"yargs": "^15.0.2"
},
"dependencies": {
"@alertlogic/al-aws-collector-js": "2.0.11",
"@alertlogic/al-aws-collector-js": "3.0.2",
"async": "3.1.0",
"debug": "4.1.1",
"moment": "2.24.0"
Expand Down
69 changes: 50 additions & 19 deletions paws_collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const packageJson = require('./package.json');

const CREDS_FILE_PATH = '/tmp/paws_creds.json';
var PAWS_DECRYPTED_CREDS = null;
const DOMAIN_REGEXP = /^[htps]*:\/\/|\/$/gi;

function getPawsParamStoreParam(){
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -73,6 +74,7 @@ class PawsCollector extends AlAwsCollector {

constructor(context, {aimsCreds, pawsCreds}, childVersion, healthChecks = [], statsChecks = []) {
const version = childVersion ? childVersion : packageJson.version;
const endpointDomain = process.env.paws_endpoint.replace(DOMAIN_REGEXP, '');
super(context, 'paws',
AlAwsCollector.IngestTypes.LOGMSGS,
version,
Expand All @@ -83,6 +85,9 @@ class PawsCollector extends AlAwsCollector {
this._pawsCollectorType = process.env.paws_type_name;
this.pollInterval = process.env.paws_poll_interval;
this.applicationId = process.env.al_application_id;
this._pawsEndpoint = process.env.paws_endpoint
this._pawsDomainEndpoint = endpointDomain;
this._pawsHttpsEndpoint = 'https://' + endpointDomain;
};

get application_id () {
Expand All @@ -103,7 +108,19 @@ class PawsCollector extends AlAwsCollector {

get pawsCollectorType() {
return this._pawsCollectorType;
}
};

get pawsEndpoint() {
return this._pawsEndpoint;
};

get pawsDomainEndpoint() {
return this._pawsDomainEndpoint;
};

get pawsHttpsEndpoint() {
return this._pawsHttpsEndpoint;
};

getProperties() {
const baseProps = super.getProperties();
Expand Down Expand Up @@ -154,32 +171,39 @@ class PawsCollector extends AlAwsCollector {
})
}

register(event) {
registerPawsCollector(event, callback) {
let collector = this;
let pawsRegisterProps = this.getProperties();
collector.pawsGetRegisterParameters(event, function(err, customRegister) {
if (err) {
console.err('PAWS000101 Error during registration', err);
return callback(err);
} else {
let registerProps = Object.assign(pawsRegisterProps, customRegister);
AlAwsCollector.prototype.register.call(collector, event, registerProps, callback);

}
});
}

register(event, customUnused, callback) {
let collector = this;

async.waterfall([
function(asyncCallback) {
collector.registerPawsCollector(event, asyncCallback);
},
function(regResp, asyncCallback) {
return collector.pawsInitCollectionState(event, asyncCallback);
},
function(state, nextInvocationTimeout, asyncCallback) {
return collector._storeCollectionState({}, state, nextInvocationTimeout, asyncCallback);
},
function(sqsResponse, asyncCallback) {
return collector.pawsGetRegisterParameters(event, asyncCallback);
}
], function(err, customRegister) {
if (err) {
console.error('PAWS000101 Error during registration', err);
return collector.done(err);
} else {
let registerProps = Object.assign(pawsRegisterProps, customRegister);
return AlAwsCollector.prototype.register.call(collector, event, registerProps);
}
});
],
callback);
};

deregister(event) {
deregister(event, customUnused, callback) {
let collector = this;
let pawsRegisterProps = {
pawsCollectorType : collector._pawsCollectorType
Expand All @@ -189,7 +213,7 @@ class PawsCollector extends AlAwsCollector {
console.warn('PAWS000102 Error during deregistration', err);
}
let registerProps = Object.assign(pawsRegisterProps, customRegister);
return AlAwsCollector.prototype.deregister.call(collector, event, registerProps);
return AlAwsCollector.prototype.deregister.call(collector, event, registerProps, callback);
});
};

Expand All @@ -206,12 +230,19 @@ class PawsCollector extends AlAwsCollector {
return super.handleEvent(event);
}
};

handlePollRequest(stateSqsMsg) {
let collector = this;
let pawsState = JSON.parse(stateSqsMsg.body);

async.waterfall([
function(asyncCallback) {
if (!collector.registered) {
return asyncCallback('PAWS000103 Collection attempt for unregistrered collector');
} else {
return asyncCallback();
}
},
function(asyncCallback) {
return collector.pawsGetLogs(pawsState.priv_collector_state, asyncCallback);
},
Expand Down Expand Up @@ -389,7 +420,7 @@ class PawsCollector extends AlAwsCollector {

/**
* @function collector callback to get specific (de)registration parameters
* @param event - collector register event coming in from CFT during stack Create/Delete operations.
* @param event - optional, collector register event coming in from CFT during stack Create/Delete operations.
* @param callback
* @returns callback - (error, objectWithRegistrationProperties)
*
Expand Down
48 changes: 48 additions & 0 deletions ps_spec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
version: 1.0
service_name: paws-collector

stages:
-
name: PR Test
when:
- pull_request
- pull_request:
trigger_phrase: test it
image: node:12
compute_size: small
commands:
- make test

-
name: Master Push - Publish
when:
- push: ['master']
image: node:12
compute_size: small
commands:
- |
set -ex
#git config --global --add url."[email protected]:".insteadOf "https://github.com/"
echo //registry.npmjs.org/:_authToken=$NPM_TOKEN > .npmrc
npm whoami

PKGNAME=$(node -e 'console.log(require("./package.json").name)')
PKGVERSION=$(node -e 'console.log(require("./package.json").version)')
V_VERSION=v$PKGVERSION
WORDCOUNT=$(npm view "${PKGNAME}@${PKGVERSION}" | wc -c)

git tag -a $V_VERSION -m "$PKGNAME release $V_VERSION" HEAD

npm install

if [ "$WORDCOUNT" -eq 0 ]; then
echo "PUBLISHING $PKGNAME $PKGVERSION"
npm run lint
git push origin $V_VERSION
npm publish --access public
else
echo "NOT PUBLISHING $PKGNAME $PKGVERSION"
fi
- echo done

0 comments on commit 9a7ca86

Please sign in to comment.