Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit e218e4b

Browse files
committed
Add generic-webhook ScanCompletionHook
1 parent fa2682a commit e218e4b

16 files changed

+4916
-8
lines changed

hook-sdk/nodejs/hook-wrapper.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const axios = require("axios");
2-
const { persist } = require("./persistence/persist");
2+
const { handle } = require("./hook/hook");
33
const k8s = require("@kubernetes/client-node");
44

55
function downloadFile(url) {
@@ -79,7 +79,7 @@ function uploadFindings() {
7979
async function main() {
8080
const scanName = process.env["SCAN_NAME"];
8181
const namespace = process.env["NAMESPACE"];
82-
console.log(`Starting PersistenceProvider for Scan "${scanName}"`);
82+
console.log(`Starting hook for Scan "${scanName}"`);
8383

8484
const kc = new k8s.KubeConfig();
8585
kc.loadFromCluster();
@@ -103,22 +103,20 @@ async function main() {
103103
}
104104

105105
try {
106-
await persist({
107-
getRawResult,
106+
await handle({
107+
getRawResults,
108108
getFindings,
109109
uploadRawResults,
110110
uploadFindings,
111111
scan,
112112
});
113113
} catch (error) {
114-
console.error(
115-
"Error was thrown while running PersistenceProviders persist function"
116-
);
114+
console.error("Error was thrown while running hooks handle function");
117115
console.error(error);
118116
process.exit(1);
119117
}
120118

121-
console.log(`Completed PersistenceProvider`);
119+
console.log(`Hook completed`);
122120
}
123121

124122
main();

hooks/generic-webhook/.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

hooks/generic-webhook/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

hooks/generic-webhook/.helmignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*~
18+
# Various IDEs
19+
.project
20+
.idea/
21+
*.tmproj
22+
.vscode/
23+
# Node.js files
24+
node_modules/*
25+
package.json
26+
package-lock.json
27+
src/*
28+
config/*
29+
Dockerfile
30+
.dockerignore

hooks/generic-webhook/Chart.lock

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies: []
2+
digest: sha256:643d5437104296e21d906ecb15b2c96ad278f20cfc4af53b12bb6069bd853726
3+
generated: "2020-05-26T16:56:03.119255+02:00"

hooks/generic-webhook/Chart.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v2
2+
name: generic-webhook
3+
description: Lets you send http webhooks after scans are completed
4+
5+
type: application
6+
7+
version: 0.1.0
8+
9+
appVersion: latest
10+
11+
dependencies: []

hooks/generic-webhook/Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM node:12-alpine as build
2+
RUN mkdir -p /home/app
3+
WORKDIR /home/app
4+
COPY package.json package-lock.json ./
5+
RUN npm ci --production
6+
7+
FROM scbexperimental/hook-sdk-nodejs:latest
8+
WORKDIR /home/app/hook-wrapper/hook/
9+
COPY --from=build --chown=app:app /home/app/node_modules/ ./node_modules/
10+
COPY --chown=app:app ./hook.js ./hook.js
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.post = jest.fn();

hooks/generic-webhook/hook.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const axios = require("axios");
2+
3+
async function handle({
4+
getFindings,
5+
scan,
6+
webhookUrl = process.env["WEBHOOK_URL"],
7+
}) {
8+
const findings = await getFindings();
9+
10+
console.log(`Sending ${findings.length} findings to ${webhookUrl}`);
11+
12+
await axios.post(webhookUrl, { scan, findings });
13+
}
14+
module.exports.handle = handle;
15+
module.exports.axios = axios;

hooks/generic-webhook/hook.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { handle, axios } = require("./hook");
2+
3+
beforeEach(() => {
4+
axios.post.mockClear();
5+
});
6+
7+
test("should send a post request to the url when fired", async () => {
8+
const findings = [];
9+
10+
const getFindings = async () => findings;
11+
12+
const scan = {
13+
metadata: {
14+
uid: "09988cdf-1fc7-4f85-95ee-1b1d65dbc7cc",
15+
name: "demo-scan",
16+
labels: {
17+
company: "iteratec",
18+
},
19+
},
20+
spec: {
21+
scanType: "Nmap",
22+
parameters: ["-Pn", "localhost"],
23+
},
24+
};
25+
26+
const webhookUrl = "http://example.com/foo/bar";
27+
28+
await handle({ getFindings, scan, webhookUrl });
29+
30+
expect(axios.post).toBeCalledWith(webhookUrl, {
31+
scan,
32+
findings: [],
33+
});
34+
});

0 commit comments

Comments
 (0)