Skip to content

Commit

Permalink
add support for scan.plugins.trivy.fsGroup variable
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusfm committed Oct 29, 2024
1 parent 7db1699 commit 8250f45
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions charts/zora/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ The following table lists the configurable parameters of the Zora chart and thei
| scan.plugins.trivy.envFrom | list | `[]` | List of sources to populate environment variables in trivy container. |
| scan.plugins.trivy.timeout | string | `"10m"` | Trivy timeout |
| scan.plugins.trivy.insecure | bool | `false` | Allow insecure server connections for Trivy |
| scan.plugins.trivy.fsGroup | int | `nil` | Trivy fsGroup. Should be greater than 0. |
| scan.plugins.trivy.persistence.enabled | bool | `true` | Specifies whether Trivy vulnerabilities database should be persisted between the scans, using PersistentVolumeClaim |
| scan.plugins.trivy.persistence.accessMode | string | `"ReadWriteOnce"` | [Persistence access mode](https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modes) |
| scan.plugins.trivy.persistence.storageClass | string | `""` | [Persistence storage class](https://kubernetes.io/docs/concepts/storage/storage-classes/). Set to empty for default storage class |
Expand Down
1 change: 1 addition & 0 deletions charts/zora/templates/operator/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ spec:
- --cronjob-clusterrolebinding-name=zora-plugins-rolebinding
- --cronjob-serviceaccount-name=zora-plugins
- --trivy-db-pvc={{- if .Values.scan.plugins.trivy.persistence.enabled }}trivy-db-volume{{- end }}
- --trivy-fs-group={{- if .Values.scan.plugins.trivy.fsGroup }}{{ .Values.scan.plugins.trivy.fsGroup }}{{- else }}0{{- end}}
{{- if .Values.scan.plugins.annotations}}
- --cronjob-serviceaccount-annotations={{ $first := true }}{{- range $key, $value := .Values.scan.plugins.annotations }}{{if not $first}},{{else}}{{$first = false}}{{end}}{{ $key }}={{$value}}{{- end }}
{{- end }}
Expand Down
4 changes: 4 additions & 0 deletions charts/zora/templates/plugins/trivy-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ spec:
- name: trivy-db
persistentVolumeClaim:
claimName: trivy-db-volume
{{- if .Values.scan.plugins.trivy.fsGroup }}
securityContext:
fsGroup: {{ .Values.scan.plugins.trivy.fsGroup }}
{{- end }}
containers:
- name: trivy-download-db
image: "{{ .Values.scan.plugins.trivy.image.repository }}:{{ .Values.scan.plugins.trivy.image.tag }}"
Expand Down
3 changes: 3 additions & 0 deletions charts/zora/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ scan:
# -- Allow insecure server connections for Trivy
insecure: false

# -- (int) Trivy fsGroup. Should be greater than 0.
fsGroup: null

persistence:
# -- Specifies whether Trivy vulnerabilities database should be persisted between the scans, using PersistentVolumeClaim
enabled: true
Expand Down
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func main() {
var kubexnsImage string
var kubexnsPullPolicy string
var trivyPVC string
var trivyFSGroup int64
var updateCRDs bool
var injectConversion bool
var caPath string
Expand Down Expand Up @@ -113,6 +114,7 @@ func main() {
flag.StringVar(&kubexnsImage, "kubexns-image", "ghcr.io/undistro/kubexns:latest", "kubexns image")
flag.StringVar(&kubexnsPullPolicy, "kubexns-pull-policy", "Always", "kubexns image pull policy")
flag.StringVar(&trivyPVC, "trivy-db-pvc", "", "PersistentVolumeClaim name for Trivy DB")
flag.Int64Var(&trivyFSGroup, "trivy-fs-group", 0, "fsGroup for Trivy")
flag.BoolVar(&updateCRDs, "update-crds", false,
"If set to true, operator will update Zora CRDs if needed")
flag.BoolVar(&injectConversion, "inject-conversion", false,
Expand Down Expand Up @@ -226,6 +228,7 @@ func main() {
KubexnsImage: kubexnsImage,
KubexnsPullPolicy: kubexnsPullPolicy,
TrivyPVC: trivyPVC,
TrivyFSGroup: trivyFSGroup,
ChecksConfigMap: fmt.Sprintf("%s/%s", checksConfigMapNamespace, checksConfigMapName),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterScan")
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/zora/clusterscan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type ClusterScanReconciler struct {
KubexnsPullPolicy string
ChecksConfigMap string
TrivyPVC string
TrivyFSGroup int64
Annotations map[string]string
OnUpdate saas.ClusterScanHook
OnDelete saas.ClusterScanHook
Expand Down Expand Up @@ -221,6 +222,7 @@ func (r *ClusterScanReconciler) reconcile(ctx context.Context, clusterscan *v1al
KubexnsPullPolicy: r.KubexnsPullPolicy,
ChecksConfigMap: r.ChecksConfigMap,
TrivyPVC: r.TrivyPVC,
TrivyFSGroup: r.TrivyFSGroup,
ClusterUID: cluster.UID,
}

Expand Down
20 changes: 13 additions & 7 deletions pkg/plugins/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type CronJobMutator struct {
KubexnsPullPolicy string
ChecksConfigMap string
TrivyPVC string
TrivyFSGroup int64
ClusterUID types.UID
}

Expand Down Expand Up @@ -145,13 +146,18 @@ func (r *CronJobMutator) Mutate() error {
})
}

if r.Plugin.Name == "trivy" && r.TrivyPVC != "" {
r.Existing.Spec.JobTemplate.Spec.Template.Spec.Volumes = append(r.Existing.Spec.JobTemplate.Spec.Template.Spec.Volumes, corev1.Volume{
Name: trivyDBVolumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ClaimName: r.TrivyPVC},
},
})
if r.Plugin.Name == "trivy" {
if r.TrivyPVC != "" {
r.Existing.Spec.JobTemplate.Spec.Template.Spec.Volumes = append(r.Existing.Spec.JobTemplate.Spec.Template.Spec.Volumes, corev1.Volume{
Name: trivyDBVolumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ClaimName: r.TrivyPVC},
},
})
}
if r.TrivyFSGroup != 0 {
r.Existing.Spec.JobTemplate.Spec.Template.Spec.SecurityContext = &corev1.PodSecurityContext{FSGroup: &r.TrivyFSGroup}
}
}

if pointer.BoolDeref(r.Plugin.Spec.MountCustomChecksVolume, false) {
Expand Down

0 comments on commit 8250f45

Please sign in to comment.