Skip to content

Commit 62da8e2

Browse files
authored
Merge pull request #386 from fuhrmeistery/feature/email-notifier
Add Email Notifier To Notification Hook
2 parents f8ec95e + cd2b739 commit 62da8e2

11 files changed

Lines changed: 268 additions & 7 deletions

File tree

‎hooks/notification-hook/NotifierFactory.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { Notifier } from "./Notifier"
22
import { NotifierType } from "./NotifierType";
33
import { SlackNotifier } from "./Notifiers/SlackNotifier"
4+
import { EMailNotifier } from "./Notifiers/EMailNotifier"
45
import { NotificationChannel } from "./model/NotificationChannel";
56
import { Scan } from "./model/Scan";
67
import { Finding } from "./model/Finding";
78

89
export class NotifierFactory {
9-
static create(channel: NotificationChannel, scan: Scan, findings: Finding[], args: Object): Notifier {
10+
static create(channel: NotificationChannel, scan: Scan, findings: Finding[], args: any): Notifier {
1011
switch (channel.type) {
1112
case NotifierType.SLACK:
1213
return new SlackNotifier(channel, scan, findings, args);
14+
case NotifierType.EMAIL:
15+
return new EMailNotifier(channel, scan, findings, args);
1316
default:
1417
throw new Error("This Type is not Implemented :(")
1518
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export enum NotifierType {
22
SLACK = "slack",
3-
MS_TEAMS = "ms-teams"
3+
MS_TEAMS = "ms-teams",
4+
EMAIL = "email"
45
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { NotifierType } from "../NotifierType";
2+
import { EMailNotifier } from "./EMailNotifier";
3+
import { NotificationChannel } from "../model/NotificationChannel";
4+
import { Scan } from "../model/Scan";
5+
6+
const sendMail = jest.fn();
7+
const close = jest.fn();
8+
9+
jest.mock("nodemailer", () => {
10+
return {
11+
createTransport: () => {
12+
return {
13+
sendMail,
14+
close,
15+
}
16+
}
17+
};
18+
});
19+
20+
test("Should Send Mail", async () => {
21+
const from = "secureCodeBox";
22+
const smtp = "smtp://user:pass@smtp.ethereal.email/"
23+
process.env[EMailNotifier.SMTP_CONFIG] = smtp;
24+
const channel: NotificationChannel = {
25+
name: "Channel Name",
26+
type: NotifierType.EMAIL,
27+
template: "email",
28+
rules: [],
29+
endPoint: "mail@some.email"
30+
};
31+
const scan: Scan = {
32+
metadata: {
33+
uid: "09988cdf-1fc7-4f85-95ee-1b1d65dbc7cc",
34+
name: "demo-scan-1601086432",
35+
namespace: "my-scans",
36+
creationTimestamp: new Date("2021-01-01T14:29:25Z"),
37+
labels: {
38+
company: "iteratec",
39+
"attack-surface": "external",
40+
},
41+
},
42+
spec: {
43+
scanType: "nmap",
44+
parameters: ["-Pn", "localhost"],
45+
},
46+
status: {
47+
findingDownloadLink:
48+
"https://my-secureCodeBox-instance.com/scan-b9as-sdweref--sadf-asdfsdf-dasdgf-asdffdsfa7/findings.json",
49+
findings: {
50+
categories: {
51+
"A Client Error response code was returned by the server": 1,
52+
"Information Disclosure - Sensitive Information in URL": 1,
53+
"Strict-Transport-Security Header Not Set": 1,
54+
},
55+
count: 3,
56+
severities: {
57+
high: 10,
58+
medium: 5,
59+
low: 2,
60+
informational: 1,
61+
},
62+
},
63+
finishedAt: new Date("2020-05-25T02:38:13Z"),
64+
rawResultDownloadLink:
65+
"https://my-secureCodeBox-instance.com/scan-blkfsdg-sdgfsfgd-sfg-sdfg-dfsg-gfs98-e8af2172caa7/zap-results.json?Expires=1601691232",
66+
rawResultFile: "zap-results.json",
67+
rawResultType: "zap-json",
68+
state: "Done",
69+
},
70+
};
71+
72+
const args = new Array();
73+
args[EMailNotifier.EMAIL_FROM] = from;
74+
75+
const notifier = new EMailNotifier(channel, scan, [], args);
76+
77+
await notifier.sendMessage();
78+
79+
expect(sendMail).toBeCalled();
80+
expect(close).toBeCalled();
81+
82+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { NotifierType } from "../NotifierType";
2+
import { AbstractNotifier } from "./AbstractNotifier";
3+
import { createTransport } from "nodemailer"
4+
5+
export class EMailNotifier extends AbstractNotifier {
6+
public static readonly SMTP_CONFIG = 'SMTP_CONFIG';
7+
public static readonly EMAIL_FROM = 'EMAIL_FROM';
8+
protected type: NotifierType.EMAIL;
9+
10+
public async sendMessage(): Promise<void> {
11+
const message = this.prepareMessage();
12+
const smtpConfig = this.getSMTPConfig();
13+
await this.sendMail(message, smtpConfig);
14+
}
15+
16+
private getSMTPConfig(): any {
17+
return process.env[EMailNotifier.SMTP_CONFIG];
18+
}
19+
20+
protected async sendMail(message: any, smtpConfig: any) {
21+
const transporter = createTransport(smtpConfig);
22+
try {
23+
const info = await transporter.sendMail(message);
24+
console.log(info);
25+
} catch (e) {
26+
console.log(`There was an error sending the email: ${e}`)
27+
} finally {
28+
transporter.close();
29+
}
30+
}
31+
32+
private prepareMessage(): any {
33+
const message = JSON.parse(this.renderMessage());
34+
message.to = this.channel.endPoint;
35+
message.from = this.args[EMailNotifier.EMAIL_FROM];
36+
return message;
37+
}
38+
}

‎hooks/notification-hook/README.md‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ helm upgrade --install nwh ./hooks/notification-hook/ --values /path/to/your/val
1818
The `values.yaml` you need depends on the notification type you want to use.
1919
Please take a look at the documentation for each type (e.g. for slack see [Configuration of a Slack Notification](#configuration-o-a-slack-notification))
2020
21+
## Available Notifier
22+
23+
* [Slack](#configuration-of-a-slack-notification)
24+
* [Email](#configuration-of-an-email-notification)
25+
2126
## Configuration of a Notification
2227
2328
The general configuration of a notification looks something like this
@@ -98,6 +103,36 @@ To configure a slack notification set the `type` to `slack` and the `endPoint` t
98103
You can use one of the following default templates:
99104
* slack-messageCard
100105
106+
### Configuration Of An Email Notification
107+
108+
To configure an email notification set the `type` to `email` and the `endPoint` to point to your env containing your target email address.
109+
You can use one of the following default templates:
110+
* email
111+
112+
Additional to this configuration you will have to provide a special smtp configuration URL.
113+
This config reflects the transporter configuration of nodemailer (See [nodemailer | SMTP Transport](https://nodemailer.com/smtp/)).
114+
This configuration needs to be specified under `env` in the values yaml.
115+
The identifier for this config has to be `SMTP_CONFIG`.
116+
A basic configuration could look like this:
117+
118+
```
119+
...
120+
env:
121+
- name: SMTP_CONFIG
122+
value: "smtp://user@domain.tld:pass@smtp.domain.tld/"
123+
```
124+
125+
To provide a custom `from` field for your email you can specify `EMAIL_FROM` under env.
126+
For example:
127+
128+
```
129+
env:
130+
- name: SMTP_CONFIG
131+
value: "smtp://user@domain.tld:pass@smtp.domain.tld/"
132+
- name: EMAIL_FROM
133+
value: secureCodeBox
134+
```
135+
101136
## Custom Message Templates
102137
103138
CAUTION: Nunjucks templates allow code to be injected! Use templates from trusted sources only!
@@ -122,8 +157,9 @@ To fill your template with data we provide the following objects.
122157
| env[0].name | string | `"SOME_ENV_KEY"` | |
123158
| env[0].valueFrom.secretKeyRef.key | string | `"some-key"` | |
124159
| env[0].valueFrom.secretKeyRef.name | string | `"some-secret"` | |
125-
| env[1].name | string | `"key"` | |
126-
| env[1].value | string | `"value"` | |
160+
| env[1].name | string | `"SMTP_CONFIG"` | |
161+
| env[1].valueFrom.secretKeyRef.key | string | `"smtp-config-key"` | |
162+
| env[1].valueFrom.secretKeyRef.name | string | `"some-secret"` | |
127163
| hookJob.ttlSecondsAfterFinished | string | `nil` | seconds after which the kubernetes job for the hook will be deleted. Requires the Kubernetes TTLAfterFinished controller: https://kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/ |
128164
| image.pullPolicy | string | `"Always"` | |
129165
| image.repository | string | `"docker.io/securecodebox/notification-hook"` | Hook image repository |

‎hooks/notification-hook/README.md.gotmpl‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ helm upgrade --install nwh ./hooks/notification-hook/ --values /path/to/your/val
1818
The `values.yaml` you need depends on the notification type you want to use.
1919
Please take a look at the documentation for each type (e.g. for slack see [Configuration of a Slack Notification](#configuration-o-a-slack-notification))
2020

21+
## Available Notifier
22+
23+
* [Slack](#configuration-of-a-slack-notification)
24+
* [Email](#configuration-of-an-email-notification)
25+
2126
## Configuration of a Notification
2227

2328
The general configuration of a notification looks something like this
@@ -98,6 +103,37 @@ To configure a slack notification set the `type` to `slack` and the `endPoint` t
98103
You can use one of the following default templates:
99104
* slack-messageCard
100105

106+
### Configuration Of An Email Notification
107+
108+
To configure an email notification set the `type` to `email` and the `endPoint` to point to your env containing your target email address.
109+
You can use one of the following default templates:
110+
* email
111+
112+
Additional to this configuration you will have to provide a special smtp configuration URL.
113+
This config reflects the transporter configuration of nodemailer (See [nodemailer | SMTP Transport](https://nodemailer.com/smtp/)).
114+
This configuration needs to be specified under `env` in the values yaml.
115+
The identifier for this config has to be `SMTP_CONFIG`.
116+
A basic configuration could look like this:
117+
118+
```
119+
...
120+
env:
121+
- name: SMTP_CONFIG
122+
value: "smtp://user@domain.tld:pass@smtp.domain.tld/"
123+
```
124+
125+
To provide a custom `from` field for your email you can specify `EMAIL_FROM` under env.
126+
For example:
127+
128+
```
129+
env:
130+
- name: SMTP_CONFIG
131+
value: "smtp://user@domain.tld:pass@smtp.domain.tld/"
132+
- name: EMAIL_FROM
133+
value: secureCodeBox
134+
```
135+
136+
101137
## Custom Message Templates
102138

103139
CAUTION: Nunjucks templates allow code to be injected! Use templates from trusted sources only!

‎hooks/notification-hook/hook.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import * as jsyaml from 'js-yaml';
2323

2424
const BASE_PATH = "/home/app/config"
2525
const CHANNEL_FILE = `${BASE_PATH}/notification-channel.yaml`;
26-
const ARGS_FILE = `${BASE_PATH}/args`
2726

2827
export async function handle({ getFindings, scan }) {
2928
let findings: Finding[] = await getFindings();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
subject: New {{ scan.spec.scanType }} security scan resulsts are available!
2+
text: |
3+
*Scan {{ scan.metadata.name }}*
4+
Created at {{ scan.metadata.creationTimestamp }}
5+
6+
*Findings Severity Overview*:
7+
{% for severity, counter in scan.status.findings.severities -%}
8+
{{ severity }}: {{ counter }}
9+
{% endfor %}
10+
*Findings Category Overview*:
11+
{% for category, counter in scan.status.findings.categories -%}
12+
{{ category }}: {{ counter }}
13+
{% endfor %}
14+
html: |
15+
<strong>Scan {{ scan.metadata.name }}</strong><br>
16+
Created at {{ scan.metadata.creationTimestamp }}
17+
<br>
18+
<br>
19+
<strong>Findings Severity Overview:</strong><br>
20+
{% for severity, counter in scan.status.findings.severities -%}
21+
{{ severity }}: {{ counter }}<br>
22+
{% endfor %}
23+
<br>
24+
<strong>Findings Category Overview:</strong><br>
25+
{% for category, counter in scan.status.findings.categories -%}
26+
{{ category }}: {{ counter }}<br>
27+
{% endfor %}

‎hooks/notification-hook/package-lock.json‎

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎hooks/notification-hook/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@types/lodash": "^4.14.168",
2828
"@types/mustache": "^4.1.1",
2929
"@types/node": "^14.14.33",
30+
"@types/nodemailer": "^6.4.1",
3031
"jest": "^26.6.3",
3132
"ts-jest": "^26.5.3"
3233
},
@@ -37,6 +38,7 @@
3738
"axios": "^0.21.1",
3839
"js-yaml": "^3.14.1",
3940
"lodash": "^4.17.21",
41+
"nodemailer": "^6.5.0",
4042
"nunjucks": "^3.2.3",
4143
"typescript": "^4.2.3"
4244
}

0 commit comments

Comments
 (0)