github.com/vmware/govmomi@v0.51.0/cli/host/tpm/report.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  	"reflect"
    13  	"strings"
    14  	"text/tabwriter"
    15  
    16  	"github.com/vmware/govmomi/cli"
    17  	"github.com/vmware/govmomi/cli/flags"
    18  	"github.com/vmware/govmomi/vim25/methods"
    19  	"github.com/vmware/govmomi/vim25/types"
    20  )
    21  
    22  type report struct {
    23  	*flags.HostSystemFlag
    24  
    25  	e bool
    26  }
    27  
    28  func init() {
    29  	cli.Register("host.tpm.report", &report{})
    30  }
    31  
    32  func (cmd *report) Register(ctx context.Context, f *flag.FlagSet) {
    33  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    34  	cmd.HostSystemFlag.Register(ctx, f)
    35  
    36  	f.BoolVar(&cmd.e, "e", false, "Print events")
    37  }
    38  
    39  func (cmd *report) Description() string {
    40  	return `Trusted Platform Module report.
    41  
    42  Examples:
    43    govc host.tpm.report
    44    govc host.tpm.report -e
    45    govc host.tpm.report -json`
    46  }
    47  
    48  func (cmd *report) Run(ctx context.Context, f *flag.FlagSet) error {
    49  	c, err := cmd.Client()
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	host, err := cmd.HostSystem()
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	query := types.QueryTpmAttestationReport{This: host.Reference()}
    60  	report, err := methods.QueryTpmAttestationReport(ctx, c, &query)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	return cmd.WriteResult(&reportResult{report.Returnval, cmd})
    66  }
    67  
    68  type reportResult struct {
    69  	Report *types.HostTpmAttestationReport
    70  	cmd    *report
    71  }
    72  
    73  func (r *reportResult) Write(w io.Writer) error {
    74  	if r.Report == nil {
    75  		return nil
    76  	}
    77  
    78  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    79  
    80  	if r.cmd.e {
    81  		for _, e := range r.Report.TpmEvents {
    82  			pcr := e.PcrIndex
    83  			d := e.EventDetails.GetHostTpmEventDetails()
    84  			meth := d.DataHashMethod
    85  			hash := d.DataHash
    86  			var name string
    87  
    88  			switch x := e.EventDetails.(type) {
    89  			case *types.HostTpmBootSecurityOptionEventDetails:
    90  				name = x.BootSecurityOption
    91  			case *types.HostTpmSoftwareComponentEventDetails:
    92  				name = x.ComponentName
    93  			case *types.HostTpmCommandEventDetails:
    94  				name = x.CommandLine
    95  			case *types.HostTpmSignerEventDetails:
    96  				name = x.BootSecurityOption
    97  			case *types.HostTpmVersionEventDetails:
    98  				name = fmt.Sprintf("%x", x.Version)
    99  			case *types.HostTpmOptionEventDetails:
   100  				name = x.OptionsFileName
   101  			case *types.HostTpmBootCompleteEventDetails:
   102  			}
   103  
   104  			kind := reflect.ValueOf(e.EventDetails).Elem().Type().Name()
   105  			kind = strings.TrimPrefix(strings.TrimSuffix(kind, "EventDetails"), "HostTpm")
   106  
   107  			fmt.Fprintf(tw, "%d\t%s\t%s\t%x\t%s\n", pcr, kind, meth, hash, name)
   108  		}
   109  	} else {
   110  		for _, e := range r.Report.TpmPcrValues {
   111  			fmt.Fprintf(tw, "PCR %d\t%s\t%x\t%s\n", e.PcrNumber, e.DigestMethod, e.DigestValue, e.ObjectName)
   112  		}
   113  	}
   114  
   115  	return tw.Flush()
   116  }