Skip to content

Commit 37929c5

Browse files
committed
Add jenkins CI groovy
Change-Id: I16d94289b282df8c4fc2f1e195874653e6d9c45f
1 parent 96b52f8 commit 37929c5

File tree

9 files changed

+433
-114
lines changed

9 files changed

+433
-114
lines changed

.eslintrc.json renamed to .eslintrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"env": {
44
"node": true,
55
"commonjs": true,
6-
"es6": true
6+
"es6": true,
7+
"mocha": true
78
},
89
"parserOptions": {
910
"ecmaVersion": 8
@@ -90,7 +91,7 @@
9091
"operator-linebreak": ["error", "after"],
9192
"prefer-template": "error",
9293
"quote-props": ["error", "as-needed"],
93-
"quotes": ["error", "single"],
94+
"quotes": ["error", "single", {"avoidEscape": true}],
9495
"require-await": "error",
9596
"semi": ["error", "always", {
9697
"omitLastInOneLineBlock": true

generator.js

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const bucketExists = () => {
4141
let params = {
4242
Bucket: process.env.S3_BUCKET
4343
};
44-
S3.headBucket(params, function(err, data) { // eslint-disable-line no-unused-vars
44+
// eslint-disable-next-line no-unused-vars
45+
S3.headBucket(params, function(err, data) {
4546
if (err) {
4647
console.log('called bucketExists and return error: ', err.stack);
4748
reject(err);
@@ -79,21 +80,24 @@ const scanDBTable = () => {
7980
*/
8081
const getBlockListFile = () => {
8182
return new Promise((resolve, reject) => {
82-
S3.getObject({
83-
Bucket: process.env.S3_BUCKET,
84-
Key: process.env.S3_BLOCKLIST_KEY
85-
}, function(err, data) {
86-
if (err && err.statusCode.toString() !== '404') {
87-
console.log('called saveBlockListToBucket and return error: ', err.stack);
88-
reject('Get ip block list error.');
89-
} else {
90-
if (err && err.statusCode.toString() === '404') {
91-
resolve('');
83+
S3.getObject(
84+
{
85+
Bucket: process.env.S3_BUCKET,
86+
Key: process.env.S3_BLOCKLIST_KEY
87+
},
88+
function(err, data) {
89+
if (err && err.statusCode.toString() !== '404') {
90+
console.log('called saveBlockListToBucket and return error: ', err.stack);
91+
reject('Get ip block list error.');
9292
} else {
93-
resolve(data.Body.toString('ascii'));
93+
if (err && err.statusCode.toString() === '404') {
94+
resolve('');
95+
} else {
96+
resolve(data.Body.toString('ascii'));
97+
}
9498
}
9599
}
96-
});
100+
);
97101
});
98102
};
99103

@@ -113,28 +117,31 @@ const saveBlockListFile = (items, blockList) => {
113117
found.add(finding.ip);
114118
});
115119

116-
S3.putObject({
117-
Body: blockList,
118-
Bucket: process.env.S3_BUCKET,
119-
Key: process.env.S3_BLOCKLIST_KEY,
120-
ACL: 'public-read',
121-
ContentType: 'text/plain'
122-
}, function(err, data) { // eslint-disable-line no-unused-vars
123-
if (err) {
124-
console.log('called saveBlockListToBucket and return error: ',
125-
err.stack);
126-
reject('Put ip block list error');
127-
} else {
128-
console.log('called saveBlockListToBucket: no error.');
129-
let msg = `${found.size} IP addresses found,
120+
S3.putObject(
121+
{
122+
Body: blockList,
123+
Bucket: process.env.S3_BUCKET,
124+
Key: process.env.S3_BLOCKLIST_KEY,
125+
ACL: 'public-read',
126+
ContentType: 'text/plain'
127+
},
128+
// eslint-disable-next-line no-unused-vars
129+
function(err, data) {
130+
if (err) {
131+
console.log('called saveBlockListToBucket and return error: ', err.stack);
132+
reject('Put ip block list error');
133+
} else {
134+
console.log('called saveBlockListToBucket: no error.');
135+
let msg = `${found.size} IP addresses found,
130136
and ${added} new IP addresses have been added to ip block list.`;
131-
setResp(msg, {
132-
found: found.size,
133-
added: added
134-
});
135-
resolve();
137+
setResp(msg, {
138+
found: found.size,
139+
added: added
140+
});
141+
resolve();
142+
}
136143
}
137-
});
144+
);
138145
});
139146
};
140147

@@ -189,8 +196,11 @@ exports.handler = async (event, context, callback) => {
189196
// update and save the ip block list file
190197
await saveBlockListFile(ipRecords, blockList);
191198
} catch (err) {
192-
setResp('There\'s a problem in generating ip block list. Pleasesee detailed' +
193-
' information in CloudWatch logs.', null);
199+
setResp(
200+
"There's a problem in generating ip block list. Please see detailed" +
201+
' information in CloudWatch logs.',
202+
null
203+
);
194204
} finally {
195205
callback(null, respArr);
196206
}

jenkins/ci.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
node('devops-aws') {
2+
stage('Clean up') {
3+
sh "rm -rf *"
4+
}
5+
6+
stage('Checkout') {
7+
def changeBranch = "change-${GERRIT_CHANGE_NUMBER}-${GERRIT_PATCHSET_NUMBER}"
8+
def scmVars = checkout scm
9+
git url: scmVars.GIT_URL
10+
sh "git fetch origin ${GERRIT_REFSPEC}:${changeBranch}"
11+
sh "git checkout ${changeBranch}"
12+
}
13+
14+
stage('NPM Install') {
15+
echo 'NPM Install..'
16+
sh 'npm install'
17+
}
18+
19+
stage('Format check') {
20+
echo 'Format checking..'
21+
sh './node_modules/.bin/ftnt-devops-ci check -f "**/*.{js,json}"'
22+
}
23+
24+
stage('Lint') {
25+
echo 'Linting..'
26+
sh './node_modules/.bin/ftnt-devops-ci check -l "**/*.js"'
27+
}
28+
29+
stage('Test') {
30+
echo 'Testing..'
31+
sh 'npm test'
32+
}
33+
34+
stage('Build') {
35+
echo 'Building..'
36+
sh 'npm run build'
37+
}
38+
}

local.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ var fs = require('fs');
3131

3232
var event = null,
3333
context = {},
34-
callback = function(context, response) { // eslint-disable-line no-shadow
34+
// eslint-disable-next-line no-shadow
35+
callback = function(context, response) {
3536
console.log('handle callback is called with:', response, context);
3637
};
3738

@@ -41,9 +42,13 @@ if (process.argv[ARGV_PROCESS_ENV_SCRIPT] !== undefined) {
4142
}
4243

4344
// if provided an event json file, use is. otherwise, use an empty event.
44-
if (process.argv[ARGV_PROCESS_EVENT_JSON] !== undefined &&
45-
fs.existsSync(process.argv[ARGV_PROCESS_EVENT_JSON])) {
45+
if (
46+
process.argv[ARGV_PROCESS_EVENT_JSON] !== undefined &&
47+
fs.existsSync(process.argv[ARGV_PROCESS_EVENT_JSON])
48+
) {
4649
const data = fs.readFileSync(process.argv[ARGV_PROCESS_EVENT_JSON]);
50+
// TODO: fix this:
51+
// eslint-disable-next-line no-useless-catch
4752
try {
4853
event = JSON.parse(data);
4954
} catch (e) {

monitor.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ Required IAM permissions:
1010
DynamoDB: UpdateItem
1111
1212
*/
13-
const
14-
objectUtils = require('./utils/ObjectUtils.js'),
13+
const objectUtils = require('./utils/ObjectUtils.js'),
1514
respArr = [];
1615

1716
let docClient = null;
@@ -57,8 +56,9 @@ const updateDBTable = (findingId, ip, lastSeen) => {
5756
console.log('called updateDBTable and returned with error:', err.stack);
5857
reject('Unable to Update ip into DynamoDB Table.');
5958
} else {
60-
console.log('called updateDBTable: ' +
61-
`finding entry (${findingId}) updated into DB.`);
59+
console.log(
60+
'called updateDBTable: ' + `finding entry (${findingId}) updated into DB.`
61+
);
6262
resolve(data);
6363
}
6464
});
@@ -99,42 +99,41 @@ exports.handler = async (event, context, callback) => {
9999

100100
const minSeverity = process.env.minSeverity || 3,
101101
detail = objectUtils.fetch(event, 'detail') || {},
102-
ip = objectUtils.fetch(detail,
103-
'service/action/networkConnectionAction/remoteIpDetails/ipAddressV4'),
104-
direction = objectUtils.fetch(detail,
105-
'service/action/networkConnectionAction/connectionDirection'),
106-
threatListName = objectUtils.fetch(detail,
107-
'service/additionalInfo/threatListName'),
102+
ip = objectUtils.fetch(
103+
detail,
104+
'service/action/networkConnectionAction/remoteIpDetails/ipAddressV4'
105+
),
106+
direction = objectUtils.fetch(
107+
detail,
108+
'service/action/networkConnectionAction/connectionDirection'
109+
),
110+
threatListName = objectUtils.fetch(detail, 'service/additionalInfo/threatListName'),
108111
findingId = objectUtils.fetch(event, 'id'),
109112
lastSeen = objectUtils.fetch(detail, 'service/eventLastSeen');
110113

111114
if (!ip) {
112-
113115
setResp('IP not found', null);
114116
callback(null, respArr);
115-
116117
} else if (direction === 'OUTBOUND') {
117-
118118
setResp('Ignore OUTBOUND connection', null);
119119
callback(null, respArr);
120-
121120
} else if (direction === 'UNKNOWN' && !threatListName) {
122-
123121
setResp('Ignore UNKNOWN connection due to undefined threat list name', null);
124122
callback(null, respArr);
125-
126123
} else if (detail.severity >= minSeverity) {
127124
try {
128125
await updateDBTable(findingId, ip, lastSeen);
129126
setResp(`finding entry (${findingId}) updated into DB.`, null);
130127
} catch (err) {
131-
setResp('There\'s a problem in updating ip to the DB. Please' +
132-
' see detailed information in CloudWatch logs.', null);
128+
setResp(
129+
"There's a problem in updating ip to the DB. Please" +
130+
' see detailed information in CloudWatch logs.',
131+
null
132+
);
133133
} finally {
134134
callback(null, respArr);
135135
}
136136
} else {
137-
138137
setResp(`Ignore due to severity less than ${minSeverity}`, null);
139138
callback(null, respArr);
140139
}

0 commit comments

Comments
 (0)