Skip to content

Commit

Permalink
Merge pull request #17 from timothyclarke/docker-helm
Browse files Browse the repository at this point in the history
Added Helm chart for Kubernetes support
  • Loading branch information
jacobbednarz authored May 24, 2020
2 parents dee32ee + ab8adc1 commit 6ee32da
Show file tree
Hide file tree
Showing 12 changed files with 371 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,12 @@ violation data because there are already a bunch of great solutions out
there. Once you have your violations being collected, be sure to slurp
them into your favourite log aggregation tool.

### Deployments

Currently supported deployment mechanisms:

- [kubernetes/helm][3]

[1]: https://github.com/jacobbednarz/go-csp-collector/blob/master/sample.filterlist.txt
[2]: https://github.com/jacobbednarz/go-csp-collector/releases
[3]: https://github.com/jacobbednarz/go-csp-collector/blob/master/deployment/kubernetes-helm/README.md
21 changes: 21 additions & 0 deletions deployments/kubernetes-helm/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*~
# Various IDEs
.project
.idea/
*.tmproj
5 changes: 5 additions & 0 deletions deployments/kubernetes-helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
appVersion: "1.0"
description: A Content Security Policy violation collector
name: csp-collector
version: 0.1.0
55 changes: 55 additions & 0 deletions deployments/kubernetes-helm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Kubernetes / Helm Deployment Template for the CSP violation collector

## TL;DR;

```console
$ helm upgrade csp-collector kubernetes-helm --values custom-values.yaml --install
```

## Introduction

This chart deploys a Content Security Policy violation collector from
https://github.com/jacobbednarz/go-csp-collector/

Using the [kubernetes-helm/values.yaml][1] file create a custom-values.yaml override
with just the changed values then run the command above.
eg.
replicaCount: 2
custom:
filterlist: "custom.filter.list"

ingress:
enabled: true
annotations:
kubernetes.io/ingress.class: nginx
certmanager.k8s.io/cluster-issuer: 'my-key-name'
certmanager.k8s.io/acme-challenge-type: 'dns01'
certmanager.k8s.io/acme-dns01-provider: 'route53'
nginx.ingress.kubernetes.io/force-ssl-redirect: 'true'
hosts:
- csp-reports.example.com
tls:
- secretName: csp-reports.example.com-tls
hosts:
- csp-reports.example.com
```
## Config params
| Parameter | Description | Default |
| --------------------------- | :------------------------------- | :----------------------------- |
| `ingress` | A standard ingress block | |
| `ingress.enabled` | Enables or Disables the ingress block | `false` |
| `ingress.annotations` | Ingress annotations | `{}` |
| `ingress.hosts` | List of FQDN's the be browsed to | Not Set |
| `ingress.tls.secretName` | Name of the secret to use | Not Set |
| `ingress.tls.hosts` | List of FQDN's the above secret is associated with| Not Set |
| `service.type` | Service type | `ClusterIP` |
| `service.port` | Service port | `80` |
| `service.annotations` | Service annotations | `{}` |
| `custom` | CLI Param Options (see Below) | |
| `custom.debug` | Logs in debug mode | `false` |
| `custom.filterlist` | Name of file within the configMaps dir for custom filters| `false` Uses list compiled into the app |
| `custom.jsonOutput` | Log entries as json objects, use `false` for plain text | `true` |
[1]: https://github.com/jacobbednarz/go-csp-collector/blob/master/deployment/kubernetes-helm/values.yaml
25 changes: 25 additions & 0 deletions deployments/kubernetes-helm/configMaps/filterlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# hash indicates a comment
resource://
chromenull://
chrome-extension://
safari-extension://
mxjscall://
webviewprogressproxy://
res://
mx://
safari-resource://
chromeinvoke://
chromeinvokeimmediate://
mbinit://
opera://
ms-appx://
ms-appx-web://
localhost
127.0.0.1
none://
about:blank
android-webview
ms-browser-extension
wvjbscheme://__wvjb_queue_message__
nativebaiduhd://adblock
bdvideo://error
19 changes: 19 additions & 0 deletions deployments/kubernetes-helm/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{- if .Values.ingress.enabled }}
1. The CSP violation collector is available by setting your report-uri to:
{{- range .Values.ingress.hosts }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ . }}{{ $.Values.ingress.path }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "csp-collector.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get svc -w {{ template "csp-collector.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "csp-collector.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "csp-collector.name" . }},release={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl port-forward $POD_NAME 8080:{{ .Values.service.port }}
{{- end }}
50 changes: 50 additions & 0 deletions deployments/kubernetes-helm/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "csp-collector.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "csp-collector.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "csp-collector.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Common labels
*/}}
{{- define "csp-collector.labels" -}}
helm.sh/chart: {{ include "csp-collector.chart" . }}
{{ include "csp-collector.selectorLabels" . }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}

{{/*
Selector labels
*/}}
{{- define "csp-collector.selectorLabels" -}}
app.kubernetes.io/name: {{ include "csp-collector.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}

9 changes: 9 additions & 0 deletions deployments/kubernetes-helm/templates/configMap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "csp-collector.fullname" . }}
labels:
{{- include "csp-collector.labels" . | nindent 4 }}
data:
{{ (.Files.Glob "configMaps/*").AsConfig | indent 2 }}
69 changes: 69 additions & 0 deletions deployments/kubernetes-helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ template "csp-collector.fullname" . }}
labels:
{{- include "csp-collector.labels" . | nindent 4 }}
checksum/config: {{ include (print $.Template.BasePath "/configMap.yaml") . | sha256sum | trunc 63 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "csp-collector.selectorLabels" . | nindent 6 }}
template:
metadata:
annotations:
checksum/config: {{ include (print $.Template.BasePath "/configMap.yaml") . | sha256sum | trunc 63 }}
labels:
{{- include "csp-collector.selectorLabels" . | nindent 8 }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
volumeMounts:
- name: config-volume
mountPath: /configs
args:
- "/csp_collector"
{{- if .Values.custom.jsonOutput }}
- "--output-format"
- "json"
{{- end }}
{{- if .Values.custom.filterlist }}
- "--filter-file"
- "/configs/{{- .Values.custom.filterlist -}}"
{{- end }}
{{- if .Values.custom.debug }}
- "--debug"
{{- end }}
ports:
- name: http
containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /_healthcheck
port: http
readinessProbe:
httpGet:
path: /_healthcheck
port: http
resources:
{{ toYaml .Values.resources | indent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{ toYaml . | indent 8 }}
{{- end }}
volumes:
- name: config-volume
configMap:
name: {{ template "csp-collector.fullname" . }}
{{- with .Values.affinity }}
affinity:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{ toYaml . | indent 8 }}
{{- end }}
36 changes: 36 additions & 0 deletions deployments/kubernetes-helm/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "csp-collector.fullname" . -}}
{{- $servicePort := .Values.service.port -}}
{{- $ingressPath := .Values.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
{{- include "csp-collector.labels" . | nindent 4 }}
{{- with .Values.ingress.annotations }}
annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if .Values.ingress.tls }}
tls:
{{- range .Values.ingress.tls }}
- hosts:
{{- range .hosts }}
- {{ . }}
{{- end }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules:
{{- range .Values.ingress.hosts }}
- host: {{ . }}
http:
paths:
- path: {{ $ingressPath }}
backend:
serviceName: {{ $fullName }}
servicePort: http
{{- end }}
{{- end }}
20 changes: 20 additions & 0 deletions deployments/kubernetes-helm/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: Service
metadata:
name: {{ template "csp-collector.fullname" . }}
labels:
{{- include "csp-collector.labels" . | nindent 4 }}
{{- if .Values.service.annotations }}
annotations:
{{ toYaml .Values.service.annotations | indent 4 }}
{{- end }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
app: {{ template "csp-collector.name" . }}
release: {{ .Release.Name }}
55 changes: 55 additions & 0 deletions deployments/kubernetes-helm/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Default values for csp-collector.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
repository: jacobbednarz/go-csp-collector
tag: latest
pullPolicy: Always

service:
type: ClusterIP
port: 8080
annotations: {}

custom:
# filterlist is either false, to use the built in filter list,
# or the path within the configMaps directly without any prefixes. Eg
# filterlist: 'filterlist.txt'
filterlist: false
# Log Json Output
jsonOutput: true
debug: false

ingress:
enabled: false
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
path: /
hosts:
- chart-example.local
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local

resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi

nodeSelector: {}

tolerations: []

affinity: {}

0 comments on commit 6ee32da

Please sign in to comment.