Skip to content

Commit fa32386

Browse files
committed
helm: add native private registry credentials for scan jobs
Add trivy.registryCredentials to automatically create a dockerconfigjson Secret and inject it into scan jobs via the operator ConfigMap's scanJobCustomVolumes/scanJobCustomVolumesMount. Previously, users had to manually create the docker config secret and hand-craft the JSON for scanJobCustomVolumes, which was error-prone. This feature automates the common case while merging with any existing user-specified custom volumes. The credential secret is mounted at /root/.docker/config.json (standard Docker config location) so Trivy can pull images from private registries without additional configuration. New values: - trivy.registryCredentials.create: enable secret creation - trivy.registryCredentials.secretName: secret name - trivy.registryCredentials.registry: registry URL - trivy.registryCredentials.username: registry username - trivy.registryCredentials.password: registry password
1 parent 1caa4d4 commit fa32386

4 files changed

Lines changed: 84 additions & 7 deletions

File tree

deploy/helm/templates/configmaps/operator.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ data:
1212
{{- with .Values.trivyOperator.scanJobTolerations }}
1313
scanJob.tolerations: {{ . | toJson | quote }}
1414
{{- end }}
15-
{{- with .Values.trivyOperator.scanJobCustomVolumesMount }}
16-
scanJob.customVolumesMount: {{ . | toJson | quote }}
17-
{{- end }}
18-
{{- with .Values.trivyOperator.scanJobCustomVolumes }}
19-
scanJob.customVolumes: {{ . | toJson | quote }}
15+
{{- $customVolumesMount := .Values.trivyOperator.scanJobCustomVolumesMount | default list }}
16+
{{- $customVolumes := .Values.trivyOperator.scanJobCustomVolumes | default list }}
17+
{{- if .Values.trivy.registryCredentials.create }}
18+
{{- $regMount := dict "name" "registry-creds" "mountPath" "/root/.docker/config.json" "subPath" ".dockerconfigjson" "readOnly" true }}
19+
{{- $regVolume := dict "name" "registry-creds" "secret" (dict "secretName" .Values.trivy.registryCredentials.secretName) }}
20+
{{- $customVolumesMount = append $customVolumesMount $regMount }}
21+
{{- $customVolumes = append $customVolumes $regVolume }}
22+
{{- end }}
23+
{{- if $customVolumesMount }}
24+
scanJob.customVolumesMount: {{ $customVolumesMount | toJson | quote }}
25+
{{- end }}
26+
{{- if $customVolumes }}
27+
scanJob.customVolumes: {{ $customVolumes | toJson | quote }}
2028
{{- end }}
2129
{{- with .Values.nodeCollector.tolerations }}
2230
nodeCollector.tolerations: {{ . | toJson | quote }}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{- if .Values.trivy.registryCredentials.create }}
2+
{{- $username := .Values.trivy.registryCredentials.username }}
3+
{{- $password := .Values.trivy.registryCredentials.password }}
4+
{{- $registry := .Values.trivy.registryCredentials.registry }}
5+
{{- $auth := printf "%s:%s" $username $password | b64enc }}
6+
{{- $dockerConfig := dict "auths" (dict $registry (dict "username" $username "password" $password "auth" $auth)) }}
7+
---
8+
apiVersion: v1
9+
kind: Secret
10+
metadata:
11+
name: {{ .Values.trivy.registryCredentials.secretName }}
12+
namespace: {{ include "trivy-operator.namespace" . }}
13+
labels: {{- include "trivy-operator.labels" . | nindent 4 }}
14+
type: kubernetes.io/dockerconfigjson
15+
data:
16+
.dockerconfigjson: {{ $dockerConfig | toJson | b64enc }}
17+
{{- end }}

deploy/helm/values.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,24 @@ trivy:
368368
# -- pullPolicy is the imge pull policy used for trivy image , valid values are (Always, Never, IfNotPresent)
369369
pullPolicy: IfNotPresent
370370

371+
# -- registryCredentials configures private registry authentication for Trivy scan jobs.
372+
# When create is true, a kubernetes.io/dockerconfigjson Secret is automatically created
373+
# and injected into scan jobs via scanJobCustomVolumes/scanJobCustomVolumesMount, allowing
374+
# Trivy to pull images from private registries without manual volume configuration.
375+
# The secret is mounted at /root/.docker/config.json inside scan job containers.
376+
# This merges with any existing scanJobCustomVolumes/scanJobCustomVolumesMount values.
377+
registryCredentials:
378+
# -- create determines if the docker config secret should be created and injected into scan jobs
379+
create: false
380+
# -- secretName is the name of the Secret to create
381+
secretName: "trivy-registry-credentials"
382+
# -- registry is the registry URL (e.g., "registry.example.com", "ghcr.io")
383+
registry: ""
384+
# -- username for registry authentication
385+
username: ""
386+
# -- password for registry authentication
387+
password: ""
388+
371389
# -- mode is the Trivy client mode. Either Standalone or ClientServer. Depending
372390
# on the active mode other settings might be applicable or required.
373391
mode: Standalone

docs/tutorials/private-registries.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,42 @@ data:
299299
.dockerconfigjson: OUTPUT
300300
```
301301
302-
## Sixth Option: Grant access through managed registries
302+
## Sixth Option: Helm-managed registry credentials
303303
304-
The last way that you could give the Trivy operator access to your private container registry is through managed registries. In this case, the container registry and your Kubernetes cluster would have to be on the same cloud provider; then you can define access to your container namespace as part of the IAM account. Once defined, trivy will already have the permissions for the registry.
304+
If you want the Helm chart to manage registry credentials for scan jobs automatically, you can use the `trivy.registryCredentials` values. This creates a `kubernetes.io/dockerconfigjson` Secret and injects it into scan jobs via `scanJobCustomVolumes`/`scanJobCustomVolumesMount`, so Trivy can authenticate against your private registry without any manual Secret or volume configuration.
305+
306+
```yaml
307+
trivy:
308+
registryCredentials:
309+
create: true
310+
secretName: "trivy-registry-credentials"
311+
registry: "registry.example.com"
312+
username: "my-user"
313+
password: "my-password"
314+
```
315+
316+
Then install or upgrade the operator:
317+
318+
```sh
319+
helm upgrade --install trivy-operator aqua/trivy-operator \
320+
--namespace trivy-system \
321+
--create-namespace \
322+
--version {{ var.chart_version }} \
323+
--values ./values.yaml
324+
```
325+
326+
This will:
327+
328+
1. Create a `kubernetes.io/dockerconfigjson` Secret named `trivy-registry-credentials`
329+
2. Automatically append the Secret as a volume to scan jobs
330+
3. Mount it at `/root/.docker/config.json` inside scan job containers
331+
332+
The credentials are merged with any existing `scanJobCustomVolumes`/`scanJobCustomVolumesMount` values, so you can combine this with other custom volumes.
333+
334+
Note: for production use, consider providing the password through `--set` or an external secrets manager rather than storing it in a values file.
335+
336+
## Seventh Option: Grant access through managed registries
337+
338+
Another way to give the Trivy operator access to your private container registry is through managed registries. In this case, the container registry and your Kubernetes cluster would have to be on the same cloud provider; then you can define access to your container namespace as part of the IAM account. Once defined, trivy will already have the permissions for the registry.
305339

306340
For additional information, please refer to the [documentation on managed registries.](https://aquasecurity.github.io/trivy-operator/v0.29.0/docs/vulnerability-scanning/managed-registries/)

0 commit comments

Comments
 (0)