Skip to content

Commit

Permalink
Merge pull request vmware#3476 from dougm/fix-byte-array
Browse files Browse the repository at this point in the history
fix: xml marshal byte array fields as vCenter does
  • Loading branch information
dougm authored Jun 24, 2024
2 parents bd85d9c + 43510fb commit 34c3788
Show file tree
Hide file tree
Showing 30 changed files with 793 additions and 79 deletions.
5 changes: 4 additions & 1 deletion gen/gen_from_vmodl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2014 VMware, Inc. All Rights Reserved.
# Copyright (c) 2014-2024 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -86,6 +86,9 @@ def var_type
when "dateTime"
type ="time.Time"
when "byte"
if slice?
return "types.ByteSlice"
end
when "double"
type ="float64"
when "float"
Expand Down
6 changes: 5 additions & 1 deletion gen/vim_wsdl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2014-2021 VMware, Inc. All Rights Reserved.
# Copyright (c) 2014-2024 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -321,6 +321,10 @@ def var_type
self.need_omitempty = false
end
when "byte"
if slice?
prefix = ""
t = "#{pkg}ByteSlice"
end
when "double"
t = "float64"
when "float"
Expand Down
38 changes: 36 additions & 2 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ but appear via `govc $cmd -h`:
- [host.storage.info](#hoststorageinfo)
- [host.storage.mark](#hoststoragemark)
- [host.storage.partition](#hoststoragepartition)
- [host.tpm.info](#hosttpminfo)
- [host.tpm.report](#hosttpmreport)
- [host.vnic.change](#hostvnicchange)
- [host.vnic.hint](#hostvnichint)
- [host.vnic.info](#hostvnicinfo)
Expand Down Expand Up @@ -2976,6 +2978,7 @@ Display SSL certificate info for HOST.
Options:
-host= Host system [GOVC_HOST]
-show=false Show PEM encoded server certificate only
```

## host.date.change
Expand Down Expand Up @@ -3293,6 +3296,37 @@ Options:
-host= Host system [GOVC_HOST]
```

## host.tpm.info

```
Usage: govc host.tpm.info [OPTIONS]
Trusted Platform Module summary.
Examples:
govc host.tpm.info
govc host.tpm.info -json
Options:
```

## host.tpm.report

```
Usage: govc host.tpm.report [OPTIONS]
Trusted Platform Module report.
Examples:
govc host.tpm.report
govc host.tpm.report -e
govc host.tpm.report -json
Options:
-e=false Print events
-host= Host system [GOVC_HOST]
```

## host.vnic.change

```
Expand Down Expand Up @@ -4611,8 +4645,8 @@ Examples:
govc object.collect -R create-filter-request.xml -O # convert filter to Go code
govc object.collect -s vm/my-vm summary.runtime.host | xargs govc ls -L # inventory path of VM's host
govc object.collect -dump -o "network/VM Network" # output Managed Object structure as Go code
govc object.collect -json $vm config | \ # use -json + jq to search array elements
jq -r '.[] | select(.val.hardware.device[].macAddress == "00:0c:29:0c:73:c0") | .val.name'
govc object.collect -json -s $vm config | \ # use -json + jq to search array elements
jq -r 'select(.hardware.device[].macAddress == "00:50:56:99:c4:27") | .name'
Options:
-O=false Output the CreateFilter request itself
Expand Down
20 changes: 18 additions & 2 deletions govc/host/cert/info.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
Copyright (c) 2016-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -19,14 +19,18 @@ package cert
import (
"context"
"flag"
"fmt"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/vim25/mo"
)

type info struct {
*flags.HostSystemFlag
*flags.OutputFlag

show bool
}

func init() {
Expand All @@ -39,6 +43,8 @@ func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {

cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
cmd.OutputFlag.Register(ctx, f)

f.BoolVar(&cmd.show, "show", false, "Show PEM encoded server certificate only")
}

func (cmd *info) Description() string {
Expand All @@ -61,6 +67,16 @@ func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
return err
}

if cmd.show {
var props mo.HostSystem
err = host.Properties(ctx, host.Reference(), []string{"config.certificate"}, &props)
if err != nil {
return err
}
_, err = fmt.Fprint(cmd.Out, string(props.Config.Certificate))
return err
}

m, err := host.ConfigManager().CertificateManager(ctx)
if err != nil {
return err
Expand Down
168 changes: 168 additions & 0 deletions govc/host/tpm/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tpm

import (
"context"
"flag"
"fmt"
"io"
"strconv"
"strings"
"text/tabwriter"
"time"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)

type info struct {
*flags.DatacenterFlag
}

func init() {
cli.Register("host.tpm.info", &info{})
}

func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
cmd.DatacenterFlag.Register(ctx, f)
}

func (cmd *info) Description() string {
return `Trusted Platform Module summary.
Examples:
govc host.tpm.info
govc host.tpm.info -json`
}

type TrustedPlatformModule struct {
Name string `json:"name"`
Supported bool `json:"supported"`
Version string `json:"version,omitempty"`
TxtEnabled bool `json:"txtEnabled,omitempty"`
Attestation *types.HostTpmAttestationInfo `json:"attestation,omitempty"`
StateEncryption *types.HostRuntimeInfoStateEncryptionInfo `json:"stateEncryption,omitempty"`
}

func HostTrustedPlatformModule(ctx context.Context, c *vim25.Client, root types.ManagedObjectReference) ([]TrustedPlatformModule, error) {
v, err := view.NewManager(c).CreateContainerView(ctx, root, []string{"HostSystem"}, true)
if err != nil {
return nil, err
}

defer v.Destroy(ctx)

props := []string{
"name",
"summary.tpmAttestation",
"summary.runtime.stateEncryption",
"capability.tpmSupported",
"capability.tpmVersion",
"capability.txtEnabled",
}

var hosts []mo.HostSystem
err = v.Retrieve(ctx, []string{"HostSystem"}, props, &hosts)
if err != nil {
return nil, err
}

tpm := make([]TrustedPlatformModule, len(hosts))

b := func(v *bool) bool {
if v == nil {
return false
}
return *v
}

for i, host := range hosts {
m := TrustedPlatformModule{
Name: host.Name,
Attestation: host.Summary.TpmAttestation,
}
if host.Capability != nil {
m.Supported = b(host.Capability.TpmSupported)
m.Version = host.Capability.TpmVersion
m.TxtEnabled = b(host.Capability.TxtEnabled)
}
if host.Summary.Runtime != nil {
m.StateEncryption = host.Summary.Runtime.StateEncryption
}
tpm[i] = m
}

return tpm, nil
}

func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
dc, err := cmd.DatacenterIfSpecified()
if err != nil {
return err
}
c, err := cmd.Client()
if err != nil {
return err
}

root := c.ServiceContent.RootFolder
if dc != nil {
root = dc.Reference()
}

tpm, err := HostTrustedPlatformModule(ctx, c, root)
if err != nil {
return err
}

return cmd.WriteResult(infoResult(tpm))
}

type infoResult []TrustedPlatformModule

func (r infoResult) Write(w io.Writer) error {
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)

fields := []string{"Name", "Attestation", "Last Verified", "TPM version", "TXT", "Message"}
fmt.Fprintln(tw, strings.Join(fields, "\t"))

for _, h := range r {
if h.Supported {
fields = []string{
h.Name,
string(h.Attestation.Status),
h.Attestation.Time.Format(time.RFC3339),
h.Version,
strconv.FormatBool(h.TxtEnabled),
}
if m := h.Attestation.Message; m != nil {
fields = append(fields, m.Message)
}
} else {
fields = []string{h.Name, "N/A", "N/A", "N/A", "N/A"}
}
fmt.Fprintln(tw, strings.Join(fields, "\t"))
}

return tw.Flush()
}
Loading

0 comments on commit 34c3788

Please sign in to comment.