Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/27176.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
csi: Fixed a bug where reading a volume from the API or event stream could erase its secrets
```
21 changes: 21 additions & 0 deletions nomad/structs/csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,27 @@ func (v *CSIVolume) Copy() *CSIVolume {
return out
}

// Sanitize returns a deep copy of the volume, with sensitive fields redacted
func (v *CSIVolume) Sanitize() *CSIVolume {
if v == nil {
return nil
}

clean := v.Copy()

// would be better not to have at all but left in and redacted for backwards
// compatibility with the existing API
clean.Secrets = nil

// MountFlags can contain secrets, so we always redact it but want to show
// the user that we have the value
if v.MountOptions != nil {
clean.MountOptions = clean.MountOptions.Sanitize()
}

return clean
}

// Claim updates the allocations and changes the volume state
func (v *CSIVolume) Claim(claim *CSIVolumeClaim, alloc *Allocation) error {
// COMPAT: volumes registered prior to 1.1.0 will be missing caps for the
Expand Down
24 changes: 24 additions & 0 deletions nomad/structs/csi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,30 @@ func TestTaskCSIPluginConfig_Equal(t *testing.T) {
}})
}

func TestCSIVolumeSanitize(t *testing.T) {
ci.Parallel(t)

orig := &CSIVolume{
ID: "foo",
MountOptions: &CSIMountOptions{
FSType: "ext4",
MountFlags: []string{"ro", "noatime"},
},
Secrets: CSISecrets{
"foo": "bar",
"baz": "qux",
},
Parameters: map[string]string{"example": "unchanged"},
}

sanitized := orig.Sanitize()
must.Eq(t, []string{"[REDACTED]"}, sanitized.MountOptions.MountFlags)
must.Nil(t, sanitized.Secrets)

orig.Parameters["example"] = "different"
must.Eq(t, "unchanged", sanitized.Parameters["example"])
}

func TestCSISecretsSanitize(t *testing.T) {
ci.Parallel(t)

Expand Down
12 changes: 1 addition & 11 deletions nomad/structs/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func nodeExt(v interface{}) interface{} {
}

func csiVolumeExt(v interface{}) interface{} {
vol := v.(*CSIVolume)
vol := v.(*CSIVolume).Sanitize()
type EmbeddedCSIVolume CSIVolume

allocCount := len(vol.ReadAllocs) + len(vol.WriteAllocs)
Expand Down Expand Up @@ -100,15 +100,5 @@ func csiVolumeExt(v interface{}) interface{} {
}
}

// MountFlags can contain secrets, so we always redact it but want
// to show the user that we have the value
if vol.MountOptions != nil && len(vol.MountOptions.MountFlags) > 0 {
apiVol.MountOptions.MountFlags = []string{"[REDACTED]"}
}

// would be better not to have at all but left in and redacted for
// backwards compatibility with the existing API
apiVol.Secrets = nil

return apiVol
}