diff --git a/README.md b/README.md index 9949c018..ccd3f9f3 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,9 @@ Samples are provided in the [etc](etc) folder. ### Examples +See [Scanning packaged charts for secrets](examples/chart-package-secrets) +for an opt-in check you can use with `ct lint` and before publishing. + The following example show various way of configuring the same thing: #### CLI diff --git a/examples/chart-package-secrets/README.md b/examples/chart-package-secrets/README.md new file mode 100644 index 00000000..104d7240 --- /dev/null +++ b/examples/chart-package-secrets/README.md @@ -0,0 +1,39 @@ +# Scanning packaged charts for secrets + +Helm can include files such as local values overrides in a packaged chart. +Use `.helmignore` to keep those files out of the package, and scan the resulting +archive for recognizable credentials before publishing it. The example uses +Helm and [Gitleaks](https://github.com/gitleaks/gitleaks#scanning-archives) v8 +with the `dir` command and archive scanning support. Both tools must be on +`PATH`. The script also uses `tar` to verify that the package is readable. + +Copy [check-chart-package.sh](check-chart-package.sh) into your chart repository +and add this to your `ct` configuration: + +```yaml +additional-commands: + - >- + sh ./check-chart-package.sh {{ printf "%q" .Path }} +``` + +Run `ct lint` from the repository root. For each chart under test, the script +packages the chart in a temporary directory, scans the archive, and returns a +nonzero status if packaging or scanning fails. The script scans an exact +copy with a `.tar.gz` extension so Gitleaks recognizes the archive. The +scanner uses its built-in rules with redacted output. + +Also scan the exact archive your release job will publish. Release packaging +can differ from lint packaging, for example when the job sets a chart version. + +```sh +helm package charts/example --destination dist +sh ./check-chart-package.sh --archive dist/example-0.1.0.tgz +# Publish this same archive only after the check succeeds. +``` + +Use the script and scanner from a trusted revision in the publishing job. Keep +publishing credentials out of jobs that run code from untrusted pull requests. +Pattern matching can miss credentials. Gitleaks may skip nested archives, +including packaged `.tgz` dependencies, so scan dependency charts separately. +Store credentials outside the chart and have workloads use references to +existing secrets or a workload identity. diff --git a/examples/chart-package-secrets/check-chart-package.sh b/examples/chart-package-secrets/check-chart-package.sh new file mode 100644 index 00000000..15e1bd63 --- /dev/null +++ b/examples/chart-package-secrets/check-chart-package.sh @@ -0,0 +1,49 @@ +#!/bin/sh +set -eu +umask 077 + +usage() { + printf 'Usage: %s CHART_DIR | --archive CHART.tgz\n' "$0" >&2 + exit 2 +} + +case "$#:${1-}" in + 1:|1:--archive) usage ;; + 1:*) ;; + 2:--archive) [ -n "$2" ] || usage ;; + *) usage ;; +esac + +workdir=$(mktemp -d) +trap 'rm -rf -- "$workdir"' EXIT +trap 'exit 1' HUP INT TERM + +if [ "$1" = --archive ]; then + archive=$2 +else + helm package --destination "$workdir" -- "$1" + set -- "$workdir"/*.tgz + if [ "$#" -ne 1 ]; then + printf 'Expected one packaged chart\n' >&2 + exit 1 + fi + archive=$1 +fi + +case "$archive" in + /*) ;; + *) archive=$PWD/$archive ;; +esac +case "$archive" in + *.tgz) test -f "$archive" ;; + *) printf 'Expected a packaged Helm chart (.tgz)\n' >&2; exit 2 ;; +esac + +# Gitleaks may skip .tgz files. Give the scanner the same bytes as .tar.gz. +cp "$archive" "$workdir/chart.tar.gz" +tar -tzf "$workdir/chart.tar.gz" > /dev/null + +# Use a trusted config and ignore file from the private working directory. +printf '[extend]\nuseDefault = true\n' > "$workdir/gitleaks.toml" +gitleaks dir --config "$workdir/gitleaks.toml" --gitleaks-ignore-path "$workdir" \ + --ignore-gitleaks-allow --max-archive-depth 2 --redact=100 "$workdir/chart.tar.gz"