From 12dca86f100c12258ec1f842c48aac5ecd75a9fe Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Tue, 6 Jul 2021 09:19:24 -0400 Subject: [PATCH] csi: account for nil volume/mount in API-to-structs conversion Fix a nil pointer in the API struct to `nomad/structs` conversion when a `volume` or `volume_mount` block is empty. --- .changelog/10855.txt | 3 +++ command/agent/job_endpoint.go | 17 ++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 .changelog/10855.txt diff --git a/.changelog/10855.txt b/.changelog/10855.txt new file mode 100644 index 000000000000..4760df222bf5 --- /dev/null +++ b/.changelog/10855.txt @@ -0,0 +1,3 @@ +```release-note:bug +csi: Fix a bug where the HTTP server would crash if a `volume` or `volume_mount` block was empty +``` diff --git a/command/agent/job_endpoint.go b/command/agent/job_endpoint.go index f6c1cefa4974..46147f5e56de 100644 --- a/command/agent/job_endpoint.go +++ b/command/agent/job_endpoint.go @@ -932,7 +932,7 @@ func ApiTgToStructsTG(job *structs.Job, taskGroup *api.TaskGroup, tg *structs.Ta if l := len(taskGroup.Volumes); l != 0 { tg.Volumes = make(map[string]*structs.VolumeRequest, l) for k, v := range taskGroup.Volumes { - if v.Type != structs.VolumeTypeHost && v.Type != structs.VolumeTypeCSI { + if v == nil || (v.Type != structs.VolumeTypeHost && v.Type != structs.VolumeTypeCSI) { // Ignore volumes we don't understand in this iteration currently. // - This is because we don't currently have a way to return errors here. continue @@ -1027,12 +1027,15 @@ func ApiTaskToStructsTask(job *structs.Job, group *structs.TaskGroup, if l := len(apiTask.VolumeMounts); l != 0 { structsTask.VolumeMounts = make([]*structs.VolumeMount, l) - for i, mount := range apiTask.VolumeMounts { - structsTask.VolumeMounts[i] = &structs.VolumeMount{ - Volume: *mount.Volume, - Destination: *mount.Destination, - ReadOnly: *mount.ReadOnly, - PropagationMode: *mount.PropagationMode, + for _, mount := range apiTask.VolumeMounts { + if mount != nil { + structsTask.VolumeMounts = append(structsTask.VolumeMounts, + &structs.VolumeMount{ + Volume: *mount.Volume, + Destination: *mount.Destination, + ReadOnly: *mount.ReadOnly, + PropagationMode: *mount.PropagationMode, + }) } } }