github.com/vmware/govmomi@v0.43.0/govc/host/tpm/info.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  	"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  }