Skip to content

Commit

Permalink
Sanitize datastore identifiers from ESXi providers
Browse files Browse the repository at this point in the history
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

Signed-off-by: Arik Hadas <[email protected]>
  • Loading branch information
ahadas committed Apr 3, 2024
1 parent 27ba10d commit c5b846a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
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
}

0 comments on commit c5b846a

Please sign in to comment.