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

Commit 7f447d8

Browse files
committed
Add add-attributes hook for integration tests
1 parent 4cbdbc8 commit 7f447d8

File tree

12 files changed

+183
-0
lines changed

12 files changed

+183
-0
lines changed

hooks/add-attributes/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

hooks/add-attributes/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

hooks/add-attributes/.helmignore

Lines changed: 30 additions & 0 deletions
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/add-attributes/Chart.lock

Lines changed: 3 additions & 0 deletions
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/add-attributes/Chart.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v2
2+
name: add-attributes-hook
3+
description: Lets you add new attributes to every finding
4+
5+
type: application
6+
7+
version: 0.1.0
8+
9+
appVersion: latest
10+
11+
dependencies: []

hooks/add-attributes/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM scbexperimental/hook-sdk-nodejs:latest
2+
WORKDIR /home/app/hook-wrapper/hook/
3+
COPY --chown=app:app ./hook.js ./hook.js

hooks/add-attributes/hook.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
async function handle({
2+
getFindings,
3+
updateFindings,
4+
attributeName = process.env["ATTRIBUTE_NAME"],
5+
attributeValue = process.env["ATTRIBUTE_VALUE"],
6+
}) {
7+
const findings = await getFindings();
8+
9+
const newFindings = findings.map((finding) => {
10+
finding.attributes[attributeName] = attributeValue;
11+
return finding;
12+
});
13+
14+
console.log(`Updated attributes on ${findings.length} findings`);
15+
16+
await updateFindings(newFindings);
17+
}
18+
module.exports.handle = handle;

hooks/add-attributes/hook.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { handle } = require("./hook");
2+
3+
test("should send a post request to the url when fired", async () => {
4+
const findings = [
5+
{
6+
name: "Open Port",
7+
attributes: {
8+
hostname: "foobar.com",
9+
},
10+
},
11+
];
12+
13+
const getFindings = async () => findings;
14+
15+
const updateFindings = jest.fn();
16+
17+
await handle({
18+
getFindings,
19+
updateFindings,
20+
attributeName: "cluster",
21+
attributeValue: "gke-internal",
22+
});
23+
24+
expect(updateFindings).toBeCalledWith([
25+
{
26+
name: "Open Port",
27+
attributes: {
28+
hostname: "foobar.com",
29+
cluster: "gke-internal",
30+
},
31+
},
32+
]);
33+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
AddAttribute Hook deployed.
2+
This will add a attribute "{{ .Values.attribute.name }}: {{ .Values.attribute.value }}" on every finding in this namespace.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{{/* vim: set filetype=mustache: */}}
2+
{{/*
3+
Expand the name of the chart.
4+
*/}}
5+
{{- define "add-attribute.name" -}}
6+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
7+
{{- end -}}
8+
9+
{{/*
10+
Create a default fully qualified app name.
11+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
12+
If release name contains chart name it will be used as a full name.
13+
*/}}
14+
{{- define "add-attribute.fullname" -}}
15+
{{- if .Values.fullnameOverride -}}
16+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
17+
{{- else -}}
18+
{{- $name := default .Chart.Name .Values.nameOverride -}}
19+
{{- if contains $name .Release.Name -}}
20+
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
21+
{{- else -}}
22+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
23+
{{- end -}}
24+
{{- end -}}
25+
{{- end -}}
26+
27+
{{/*
28+
Create chart name and version as used by the chart label.
29+
*/}}
30+
{{- define "add-attribute.chart" -}}
31+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
32+
{{- end -}}
33+
34+
{{/*
35+
Common labels
36+
*/}}
37+
{{- define "add-attribute.labels" -}}
38+
helm.sh/chart: {{ include "add-attribute.chart" . }}
39+
{{ include "add-attribute.selectorLabels" . }}
40+
{{- if .Chart.AppVersion }}
41+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
42+
{{- end }}
43+
app.kubernetes.io/managed-by: {{ .Release.Service }}
44+
{{- end -}}
45+
46+
{{/*
47+
Selector labels
48+
*/}}
49+
{{- define "add-attribute.selectorLabels" -}}
50+
app.kubernetes.io/name: {{ include "add-attribute.name" . }}
51+
app.kubernetes.io/instance: {{ .Release.Name }}
52+
{{- end -}}

0 commit comments

Comments
 (0)