Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion hooks/notification-hook/NotifierFactory.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Notifier } from "./Notifier"
import { NotifierType } from "./NotifierType";
import { SlackNotifier } from "./Notifiers/SlackNotifier"
import { EMailNotifier } from "./Notifiers/EMailNotifier"
import { NotificationChannel } from "./model/NotificationChannel";
import { Scan } from "./model/Scan";
import { Finding } from "./model/Finding";

export class NotifierFactory {
static create(channel: NotificationChannel, scan: Scan, findings: Finding[], args: Object): Notifier {
static create(channel: NotificationChannel, scan: Scan, findings: Finding[], args: any): Notifier {
switch (channel.type) {
case NotifierType.SLACK:
return new SlackNotifier(channel, scan, findings, args);
case NotifierType.EMAIL:
return new EMailNotifier(channel, scan, findings, args);
default:
throw new Error("This Type is not Implemented :(")
}
Expand Down
3 changes: 2 additions & 1 deletion hooks/notification-hook/NotifierType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum NotifierType {
SLACK = "slack",
MS_TEAMS = "ms-teams"
MS_TEAMS = "ms-teams",
EMAIL = "email"
}
82 changes: 82 additions & 0 deletions hooks/notification-hook/Notifiers/EMailNotifier.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { NotifierType } from "../NotifierType";
import { EMailNotifier } from "./EMailNotifier";
import { NotificationChannel } from "../model/NotificationChannel";
import { Scan } from "../model/Scan";

const sendMail = jest.fn();
const close = jest.fn();

jest.mock("nodemailer", () => {
return {
createTransport: () => {
return {
sendMail,
close,
}
}
};
});

test("Should Send Mail", async () => {
const from = "secureCodeBox";
const smtp = "smtp://user:pass@smtp.ethereal.email/"
process.env[EMailNotifier.SMTP_CONFIG] = smtp;
const channel: NotificationChannel = {
name: "Channel Name",
type: NotifierType.EMAIL,
template: "email",
rules: [],
endPoint: "mail@some.email"
};
const scan: Scan = {
metadata: {
uid: "09988cdf-1fc7-4f85-95ee-1b1d65dbc7cc",
name: "demo-scan-1601086432",
namespace: "my-scans",
creationTimestamp: new Date("2021-01-01T14:29:25Z"),
labels: {
company: "iteratec",
"attack-surface": "external",
},
},
spec: {
scanType: "nmap",
parameters: ["-Pn", "localhost"],
},
status: {
findingDownloadLink:
"https://my-secureCodeBox-instance.com/scan-b9as-sdweref--sadf-asdfsdf-dasdgf-asdffdsfa7/findings.json",
findings: {
categories: {
"A Client Error response code was returned by the server": 1,
"Information Disclosure - Sensitive Information in URL": 1,
"Strict-Transport-Security Header Not Set": 1,
},
count: 3,
severities: {
high: 10,
medium: 5,
low: 2,
informational: 1,
},
},
finishedAt: new Date("2020-05-25T02:38:13Z"),
rawResultDownloadLink:
"https://my-secureCodeBox-instance.com/scan-blkfsdg-sdgfsfgd-sfg-sdfg-dfsg-gfs98-e8af2172caa7/zap-results.json?Expires=1601691232",
rawResultFile: "zap-results.json",
rawResultType: "zap-json",
state: "Done",
},
};

const args = new Array();
args[EMailNotifier.EMAIL_FROM] = from;

const notifier = new EMailNotifier(channel, scan, [], args);

await notifier.sendMessage();

expect(sendMail).toBeCalled();
expect(close).toBeCalled();

})
38 changes: 38 additions & 0 deletions hooks/notification-hook/Notifiers/EMailNotifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { NotifierType } from "../NotifierType";
import { AbstractNotifier } from "./AbstractNotifier";
import { createTransport } from "nodemailer"

export class EMailNotifier extends AbstractNotifier {
public static readonly SMTP_CONFIG = 'SMTP_CONFIG';
public static readonly EMAIL_FROM = 'EMAIL_FROM';
protected type: NotifierType.EMAIL;

public async sendMessage(): Promise<void> {
const message = this.prepareMessage();
const smtpConfig = this.getSMTPConfig();
await this.sendMail(message, smtpConfig);
}

private getSMTPConfig(): any {
return process.env[EMailNotifier.SMTP_CONFIG];
}

protected async sendMail(message: any, smtpConfig: any) {
const transporter = createTransport(smtpConfig);
try {
const info = await transporter.sendMail(message);
console.log(info);
} catch (e) {
console.log(`There was an error sending the email: ${e}`)
} finally {
transporter.close();
}
}

private prepareMessage(): any {
const message = JSON.parse(this.renderMessage());
message.to = this.channel.endPoint;
message.from = this.args[EMailNotifier.EMAIL_FROM];
return message;
}
}
40 changes: 38 additions & 2 deletions hooks/notification-hook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ helm upgrade --install nwh ./hooks/notification-hook/ --values /path/to/your/val
The `values.yaml` you need depends on the notification type you want to use.
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))

## Available Notifier

* [Slack](#configuration-of-a-slack-notification)
* [Email](#configuration-of-an-email-notification)

## Configuration of a Notification

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

### Configuration Of An Email Notification

To configure an email notification set the `type` to `email` and the `endPoint` to point to your env containing your target email address.
You can use one of the following default templates:
* email

Additional to this configuration you will have to provide a special smtp configuration URL.
This config reflects the transporter configuration of nodemailer (See [nodemailer | SMTP Transport](https://nodemailer.com/smtp/)).
This configuration needs to be specified under `env` in the values yaml.
The identifier for this config has to be `SMTP_CONFIG`.
A basic configuration could look like this:

```
...
env:
- name: SMTP_CONFIG
value: "smtp://user@domain.tld:pass@smtp.domain.tld/"
```

To provide a custom `from` field for your email you can specify `EMAIL_FROM` under env.
For example:

```
env:
- name: SMTP_CONFIG
value: "smtp://user@domain.tld:pass@smtp.domain.tld/"
- name: EMAIL_FROM
value: secureCodeBox
```

## Custom Message Templates

CAUTION: Nunjucks templates allow code to be injected! Use templates from trusted sources only!
Expand All @@ -122,8 +157,9 @@ To fill your template with data we provide the following objects.
| env[0].name | string | `"SOME_ENV_KEY"` | |
| env[0].valueFrom.secretKeyRef.key | string | `"some-key"` | |
| env[0].valueFrom.secretKeyRef.name | string | `"some-secret"` | |
| env[1].name | string | `"key"` | |
| env[1].value | string | `"value"` | |
| env[1].name | string | `"SMTP_CONFIG"` | |
| env[1].valueFrom.secretKeyRef.key | string | `"smtp-config-key"` | |
| env[1].valueFrom.secretKeyRef.name | string | `"some-secret"` | |
| 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/ |
| image.pullPolicy | string | `"Always"` | |
| image.repository | string | `"docker.io/securecodebox/notification-hook"` | Hook image repository |
Expand Down
36 changes: 36 additions & 0 deletions hooks/notification-hook/README.md.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ helm upgrade --install nwh ./hooks/notification-hook/ --values /path/to/your/val
The `values.yaml` you need depends on the notification type you want to use.
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))

## Available Notifier

* [Slack](#configuration-of-a-slack-notification)
* [Email](#configuration-of-an-email-notification)

## Configuration of a Notification

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

### Configuration Of An Email Notification

To configure an email notification set the `type` to `email` and the `endPoint` to point to your env containing your target email address.
You can use one of the following default templates:
* email

Additional to this configuration you will have to provide a special smtp configuration URL.
This config reflects the transporter configuration of nodemailer (See [nodemailer | SMTP Transport](https://nodemailer.com/smtp/)).
This configuration needs to be specified under `env` in the values yaml.
The identifier for this config has to be `SMTP_CONFIG`.
A basic configuration could look like this:

```
...
env:
- name: SMTP_CONFIG
value: "smtp://user@domain.tld:pass@smtp.domain.tld/"
```

To provide a custom `from` field for your email you can specify `EMAIL_FROM` under env.
For example:

```
env:
- name: SMTP_CONFIG
value: "smtp://user@domain.tld:pass@smtp.domain.tld/"
- name: EMAIL_FROM
value: secureCodeBox
```


## Custom Message Templates

CAUTION: Nunjucks templates allow code to be injected! Use templates from trusted sources only!
Expand Down
1 change: 0 additions & 1 deletion hooks/notification-hook/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import * as jsyaml from 'js-yaml';

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

export async function handle({ getFindings, scan }) {
let findings: Finding[] = await getFindings();
Expand Down
27 changes: 27 additions & 0 deletions hooks/notification-hook/notification-templates/email.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
subject: New {{ scan.spec.scanType }} security scan resulsts are available!
text: |
*Scan {{ scan.metadata.name }}*
Created at {{ scan.metadata.creationTimestamp }}

*Findings Severity Overview*:
{% for severity, counter in scan.status.findings.severities -%}
{{ severity }}: {{ counter }}
{% endfor %}
*Findings Category Overview*:
{% for category, counter in scan.status.findings.categories -%}
{{ category }}: {{ counter }}
{% endfor %}
html: |
<strong>Scan {{ scan.metadata.name }}</strong><br>
Created at {{ scan.metadata.creationTimestamp }}
<br>
<br>
<strong>Findings Severity Overview:</strong><br>
{% for severity, counter in scan.status.findings.severities -%}
{{ severity }}: {{ counter }}<br>
{% endfor %}
<br>
<strong>Findings Category Overview:</strong><br>
{% for category, counter in scan.status.findings.categories -%}
{{ category }}: {{ counter }}<br>
{% endfor %}
33 changes: 33 additions & 0 deletions hooks/notification-hook/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions hooks/notification-hook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@types/lodash": "^4.14.168",
"@types/mustache": "^4.1.1",
"@types/node": "^14.14.33",
"@types/nodemailer": "^6.4.1",
"jest": "^26.6.3",
"ts-jest": "^26.5.3"
},
Expand All @@ -37,6 +38,7 @@
"axios": "^0.21.1",
"js-yaml": "^3.14.1",
"lodash": "^4.17.21",
"nodemailer": "^6.5.0",
"nunjucks": "^3.2.3",
"typescript": "^4.2.3"
}
Expand Down
8 changes: 6 additions & 2 deletions hooks/notification-hook/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ env:
secretKeyRef:
name: some-secret
key: some-key
- name: key
value: value
- name: SMTP_CONFIG
valueFrom:
secretKeyRef:
name: some-secret
key: smtp-config-key