github.com/vmware/govmomi@v0.51.0/cli/host/tpm/info.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package tpm
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"io"
    12  	"strconv"
    13  	"strings"
    14  	"text/tabwriter"
    15  	"time"
    16  
    17  	"github.com/vmware/govmomi/cli"
    18  	"github.com/vmware/govmomi/cli/flags"
    19  	"github.com/vmware/govmomi/view"
    20  	"github.com/vmware/govmomi/vim25"
    21  	"github.com/vmware/govmomi/vim25/mo"
    22  	"github.com/vmware/govmomi/vim25/types"
    23  )
    24  
    25  type info struct {
    26  	*flags.DatacenterFlag
    27  }
    28  
    29  func init() {
    30  	cli.Register("host.tpm.info", &info{})
    31  }
    32  
    33  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    34  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    35  	cmd.DatacenterFlag.Register(ctx, f)
    36  }
    37  
    38  func (cmd *info) Description() string {
    39  	return `Trusted Platform Module summary.
    40  
    41  Examples:
    42    govc host.tpm.info
    43    govc host.tpm.info -json`
    44  }
    45  
    46  type TrustedPlatformModule struct {
    47  	Name            string                                    `json:"name"`
    48  	Supported       bool                                      `json:"supported"`
    49  	Version         string                                    `json:"version,omitempty"`
    50  	TxtEnabled      bool                                      `json:"txtEnabled,omitempty"`
    51  	Attestation     *types.HostTpmAttestationInfo             `json:"attestation,omitempty"`
    52  	StateEncryption *types.HostRuntimeInfoStateEncryptionInfo `json:"stateEncryption,omitempty"`
    53  }
    54  
    55  func HostTrustedPlatformModule(ctx context.Context, c *vim25.Client, root types.ManagedObjectReference) ([]TrustedPlatformModule, error) {
    56  	v, err := view.NewManager(c).CreateContainerView(ctx, root, []string{"HostSystem"}, true)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	defer v.Destroy(ctx)
    62  
    63  	props := []string{
    64  		"name",
    65  		"summary.tpmAttestation",
    66  		"summary.runtime.stateEncryption",
    67  		"capability.tpmSupported",
    68  		"capability.tpmVersion",
    69  		"capability.txtEnabled",
    70  	}
    71  
    72  	var hosts []mo.HostSystem
    73  	err = v.Retrieve(ctx, []string{"HostSystem"}, props, &hosts)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	tpm := make([]TrustedPlatformModule, len(hosts))
    79  
    80  	b := func(v *bool) bool {
    81  		if v == nil {
    82  			return false
    83  		}
    84  		return *v
    85  	}
    86  
    87  	for i, host := range hosts {
    88  		m := TrustedPlatformModule{
    89  			Name:        host.Name,
    90  			Attestation: host.Summary.TpmAttestation,
    91  		}
    92  		if host.Capability != nil {
    93  			m.Supported = b(host.Capability.TpmSupported)
    94  			m.Version = host.Capability.TpmVersion
    95  			m.TxtEnabled = b(host.Capability.TxtEnabled)
    96  		}
    97  		if host.Summary.Runtime != nil {
    98  			m.StateEncryption = host.Summary.Runtime.StateEncryption
    99  		}
   100  		tpm[i] = m
   101  	}
   102  
   103  	return tpm, nil
   104  }
   105  
   106  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
   107  	dc, err := cmd.DatacenterIfSpecified()
   108  	if err != nil {
   109  		return err
   110  	}
   111  	c, err := cmd.Client()
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	root := c.ServiceContent.RootFolder
   117  	if dc != nil {
   118  		root = dc.Reference()
   119  	}
   120  
   121  	tpm, err := HostTrustedPlatformModule(ctx, c, root)
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	return cmd.WriteResult(infoResult(tpm))
   127  }
   128  
   129  type infoResult []TrustedPlatformModule
   130  
   131  func (r infoResult) Write(w io.Writer) error {
   132  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
   133  
   134  	fields := []string{"Name", "Attestation", "Last Verified", "TPM version", "TXT", "Message"}
   135  	fmt.Fprintln(tw, strings.Join(fields, "\t"))
   136  
   137  	for _, h := range r {
   138  		if h.Supported {
   139  			fields = []string{
   140  				h.Name,
   141  				string(h.Attestation.Status),
   142  				h.Attestation.Time.Format(time.RFC3339),
   143  				h.Version,
   144  				strconv.FormatBool(h.TxtEnabled),
   145  			}
   146  			if m := h.Attestation.Message; m != nil {
   147  				fields = append(fields, m.Message)
   148  			}
   149  		} else {
   150  			fields = []string{h.Name, "N/A", "N/A", "N/A", "N/A"}
   151  		}
   152  		fmt.Fprintln(tw, strings.Join(fields, "\t"))
   153  	}
   154  
   155  	return tw.Flush()
   156  }