github.com/vmware/govmomi@v0.43.0/govc/host/tpm/report.go (about)

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