Skip to content

Commit 34c3788

Browse files
authored
Merge pull request #3476 from dougm/fix-byte-array
fix: xml marshal byte array fields as vCenter does
2 parents bd85d9c + 43510fb commit 34c3788

30 files changed

+793
-79
lines changed

gen/gen_from_vmodl.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2014 VMware, Inc. All Rights Reserved.
1+
# Copyright (c) 2014-2024 VMware, Inc. All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -86,6 +86,9 @@ def var_type
8686
when "dateTime"
8787
type ="time.Time"
8888
when "byte"
89+
if slice?
90+
return "types.ByteSlice"
91+
end
8992
when "double"
9093
type ="float64"
9194
when "float"

gen/vim_wsdl.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2014-2021 VMware, Inc. All Rights Reserved.
1+
# Copyright (c) 2014-2024 VMware, Inc. All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -321,6 +321,10 @@ def var_type
321321
self.need_omitempty = false
322322
end
323323
when "byte"
324+
if slice?
325+
prefix = ""
326+
t = "#{pkg}ByteSlice"
327+
end
324328
when "double"
325329
t = "float64"
326330
when "float"

govc/USAGE.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ but appear via `govc $cmd -h`:
200200
- [host.storage.info](#hoststorageinfo)
201201
- [host.storage.mark](#hoststoragemark)
202202
- [host.storage.partition](#hoststoragepartition)
203+
- [host.tpm.info](#hosttpminfo)
204+
- [host.tpm.report](#hosttpmreport)
203205
- [host.vnic.change](#hostvnicchange)
204206
- [host.vnic.hint](#hostvnichint)
205207
- [host.vnic.info](#hostvnicinfo)
@@ -2976,6 +2978,7 @@ Display SSL certificate info for HOST.
29762978
29772979
Options:
29782980
-host= Host system [GOVC_HOST]
2981+
-show=false Show PEM encoded server certificate only
29792982
```
29802983

29812984
## host.date.change
@@ -3293,6 +3296,37 @@ Options:
32933296
-host= Host system [GOVC_HOST]
32943297
```
32953298

3299+
## host.tpm.info
3300+
3301+
```
3302+
Usage: govc host.tpm.info [OPTIONS]
3303+
3304+
Trusted Platform Module summary.
3305+
3306+
Examples:
3307+
govc host.tpm.info
3308+
govc host.tpm.info -json
3309+
3310+
Options:
3311+
```
3312+
3313+
## host.tpm.report
3314+
3315+
```
3316+
Usage: govc host.tpm.report [OPTIONS]
3317+
3318+
Trusted Platform Module report.
3319+
3320+
Examples:
3321+
govc host.tpm.report
3322+
govc host.tpm.report -e
3323+
govc host.tpm.report -json
3324+
3325+
Options:
3326+
-e=false Print events
3327+
-host= Host system [GOVC_HOST]
3328+
```
3329+
32963330
## host.vnic.change
32973331

32983332
```
@@ -4611,8 +4645,8 @@ Examples:
46114645
govc object.collect -R create-filter-request.xml -O # convert filter to Go code
46124646
govc object.collect -s vm/my-vm summary.runtime.host | xargs govc ls -L # inventory path of VM's host
46134647
govc object.collect -dump -o "network/VM Network" # output Managed Object structure as Go code
4614-
govc object.collect -json $vm config | \ # use -json + jq to search array elements
4615-
jq -r '.[] | select(.val.hardware.device[].macAddress == "00:0c:29:0c:73:c0") | .val.name'
4648+
govc object.collect -json -s $vm config | \ # use -json + jq to search array elements
4649+
jq -r 'select(.hardware.device[].macAddress == "00:50:56:99:c4:27") | .name'
46164650
46174651
Options:
46184652
-O=false Output the CreateFilter request itself

govc/host/cert/info.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
2+
Copyright (c) 2016-2024 VMware, Inc. All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -19,14 +19,18 @@ package cert
1919
import (
2020
"context"
2121
"flag"
22+
"fmt"
2223

2324
"github.com/vmware/govmomi/govc/cli"
2425
"github.com/vmware/govmomi/govc/flags"
26+
"github.com/vmware/govmomi/vim25/mo"
2527
)
2628

2729
type info struct {
2830
*flags.HostSystemFlag
2931
*flags.OutputFlag
32+
33+
show bool
3034
}
3135

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

4044
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
4145
cmd.OutputFlag.Register(ctx, f)
46+
47+
f.BoolVar(&cmd.show, "show", false, "Show PEM encoded server certificate only")
4248
}
4349

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

70+
if cmd.show {
71+
var props mo.HostSystem
72+
err = host.Properties(ctx, host.Reference(), []string{"config.certificate"}, &props)
73+
if err != nil {
74+
return err
75+
}
76+
_, err = fmt.Fprint(cmd.Out, string(props.Config.Certificate))
77+
return err
78+
}
79+
6480
m, err := host.ConfigManager().CertificateManager(ctx)
6581
if err != nil {
6682
return err

govc/host/tpm/info.go

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package tpm
18+
19+
import (
20+
"context"
21+
"flag"
22+
"fmt"
23+
"io"
24+
"strconv"
25+
"strings"
26+
"text/tabwriter"
27+
"time"
28+
29+
"github.com/vmware/govmomi/govc/cli"
30+
"github.com/vmware/govmomi/govc/flags"
31+
"github.com/vmware/govmomi/view"
32+
"github.com/vmware/govmomi/vim25"
33+
"github.com/vmware/govmomi/vim25/mo"
34+
"github.com/vmware/govmomi/vim25/types"
35+
)
36+
37+
type info struct {
38+
*flags.DatacenterFlag
39+
}
40+
41+
func init() {
42+
cli.Register("host.tpm.info", &info{})
43+
}
44+
45+
func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
46+
cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
47+
cmd.DatacenterFlag.Register(ctx, f)
48+
}
49+
50+
func (cmd *info) Description() string {
51+
return `Trusted Platform Module summary.
52+
53+
Examples:
54+
govc host.tpm.info
55+
govc host.tpm.info -json`
56+
}
57+
58+
type TrustedPlatformModule struct {
59+
Name string `json:"name"`
60+
Supported bool `json:"supported"`
61+
Version string `json:"version,omitempty"`
62+
TxtEnabled bool `json:"txtEnabled,omitempty"`
63+
Attestation *types.HostTpmAttestationInfo `json:"attestation,omitempty"`
64+
StateEncryption *types.HostRuntimeInfoStateEncryptionInfo `json:"stateEncryption,omitempty"`
65+
}
66+
67+
func HostTrustedPlatformModule(ctx context.Context, c *vim25.Client, root types.ManagedObjectReference) ([]TrustedPlatformModule, error) {
68+
v, err := view.NewManager(c).CreateContainerView(ctx, root, []string{"HostSystem"}, true)
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
defer v.Destroy(ctx)
74+
75+
props := []string{
76+
"name",
77+
"summary.tpmAttestation",
78+
"summary.runtime.stateEncryption",
79+
"capability.tpmSupported",
80+
"capability.tpmVersion",
81+
"capability.txtEnabled",
82+
}
83+
84+
var hosts []mo.HostSystem
85+
err = v.Retrieve(ctx, []string{"HostSystem"}, props, &hosts)
86+
if err != nil {
87+
return nil, err
88+
}
89+
90+
tpm := make([]TrustedPlatformModule, len(hosts))
91+
92+
b := func(v *bool) bool {
93+
if v == nil {
94+
return false
95+
}
96+
return *v
97+
}
98+
99+
for i, host := range hosts {
100+
m := TrustedPlatformModule{
101+
Name: host.Name,
102+
Attestation: host.Summary.TpmAttestation,
103+
}
104+
if host.Capability != nil {
105+
m.Supported = b(host.Capability.TpmSupported)
106+
m.Version = host.Capability.TpmVersion
107+
m.TxtEnabled = b(host.Capability.TxtEnabled)
108+
}
109+
if host.Summary.Runtime != nil {
110+
m.StateEncryption = host.Summary.Runtime.StateEncryption
111+
}
112+
tpm[i] = m
113+
}
114+
115+
return tpm, nil
116+
}
117+
118+
func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
119+
dc, err := cmd.DatacenterIfSpecified()
120+
if err != nil {
121+
return err
122+
}
123+
c, err := cmd.Client()
124+
if err != nil {
125+
return err
126+
}
127+
128+
root := c.ServiceContent.RootFolder
129+
if dc != nil {
130+
root = dc.Reference()
131+
}
132+
133+
tpm, err := HostTrustedPlatformModule(ctx, c, root)
134+
if err != nil {
135+
return err
136+
}
137+
138+
return cmd.WriteResult(infoResult(tpm))
139+
}
140+
141+
type infoResult []TrustedPlatformModule
142+
143+
func (r infoResult) Write(w io.Writer) error {
144+
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
145+
146+
fields := []string{"Name", "Attestation", "Last Verified", "TPM version", "TXT", "Message"}
147+
fmt.Fprintln(tw, strings.Join(fields, "\t"))
148+
149+
for _, h := range r {
150+
if h.Supported {
151+
fields = []string{
152+
h.Name,
153+
string(h.Attestation.Status),
154+
h.Attestation.Time.Format(time.RFC3339),
155+
h.Version,
156+
strconv.FormatBool(h.TxtEnabled),
157+
}
158+
if m := h.Attestation.Message; m != nil {
159+
fields = append(fields, m.Message)
160+
}
161+
} else {
162+
fields = []string{h.Name, "N/A", "N/A", "N/A", "N/A"}
163+
}
164+
fmt.Fprintln(tw, strings.Join(fields, "\t"))
165+
}
166+
167+
return tw.Flush()
168+
}

0 commit comments

Comments
 (0)