Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize datastore identifiers from ESXi providers #850

Merged
merged 1 commit into from
Apr 3, 2024
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
1 change: 1 addition & 0 deletions pkg/controller/provider/container/vsphere/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
"collector.go",
"doc.go",
"model.go",
"utils.go",
"watch.go",
],
importpath = "github.com/konveyor/forklift-controller/pkg/controller/provider/container/vsphere",
Expand Down
12 changes: 10 additions & 2 deletions pkg/controller/provider/container/vsphere/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,10 +838,17 @@ func (r *Collector) selectAdapter(u types.ObjectUpdate) (Adapter, bool) {
},
}
case Datastore:
// when we get datastores from the ESXi SDK, their identifier may be in the
// form of '10.11.12.13:/vol/virtv2v/function' which leads to invalid
// endpoints in the inventory so we sanitize such identifiers
datastoreId, changed := sanitize(u.Obj.Value)
if changed {
r.log.Info("sanitized datastore ID", "reported", u.Obj.Value, "sanitized", datastoreId)
}
adapter = &DatastoreAdapter{
model: model.Datastore{
Base: model.Base{
ID: u.Obj.Value,
ID: datastoreId,
},
},
}
Expand Down Expand Up @@ -932,9 +939,10 @@ func (r Collector) applyLeave(tx *libmodel.Tx, u types.ObjectUpdate) error {
},
}
case Datastore:
datastoreId, _ := sanitize(u.Obj.Value)
deleted = &model.Datastore{
Base: model.Base{
ID: u.Obj.Value,
ID: datastoreId,
},
}
case VirtualMachine:
Expand Down
9 changes: 6 additions & 3 deletions pkg/controller/provider/container/vsphere/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,39 +688,42 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
switch disk.Backing.(type) {
case *types.VirtualDiskFlatVer1BackingInfo:
backing := disk.Backing.(*types.VirtualDiskFlatVer1BackingInfo)
datastoreId, _ := sanitize(backing.Datastore.Value)
md := model.Disk{
Key: disk.Key,
File: backing.FileName,
Capacity: disk.CapacityInBytes,
Datastore: model.Ref{
Kind: model.DsKind,
ID: backing.Datastore.Value,
ID: datastoreId,
},
}
disks = append(disks, md)
case *types.VirtualDiskFlatVer2BackingInfo:
backing := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo)
datastoreId, _ := sanitize(backing.Datastore.Value)
md := model.Disk{
Key: disk.Key,
File: backing.FileName,
Capacity: disk.CapacityInBytes,
Shared: backing.Sharing != "sharingNone",
Datastore: model.Ref{
Kind: model.DsKind,
ID: backing.Datastore.Value,
ID: datastoreId,
},
}
disks = append(disks, md)
case *types.VirtualDiskRawDiskMappingVer1BackingInfo:
backing := disk.Backing.(*types.VirtualDiskRawDiskMappingVer1BackingInfo)
datastoreId, _ := sanitize(backing.Datastore.Value)
md := model.Disk{
Key: disk.Key,
File: backing.FileName,
Capacity: disk.CapacityInBytes,
Shared: backing.Sharing != "sharingNone",
Datastore: model.Ref{
Kind: model.DsKind,
ID: backing.Datastore.Value,
ID: datastoreId,
},
RDM: true,
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/controller/provider/container/vsphere/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package vsphere

import (
"crypto/md5"
"encoding/hex"
"net/url"
)

func sanitize(datastoreId string) (sanitizedId string, changed bool) {
sanitizedId = url.PathEscape(datastoreId)
if sanitizedId != datastoreId {
sum := md5.Sum([]byte(datastoreId))
sanitizedId = hex.EncodeToString(sum[:])
changed = true
}
return
}
Loading