github.com/vmware/govmomi@v0.43.0/govc/host/cert/info.go (about)

     1  /*
     2  Copyright (c) 2016-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 cert
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/vim25/mo"
    27  )
    28  
    29  type info struct {
    30  	*flags.HostSystemFlag
    31  	*flags.OutputFlag
    32  
    33  	show bool
    34  }
    35  
    36  func init() {
    37  	cli.Register("host.cert.info", &info{})
    38  }
    39  
    40  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    42  	cmd.HostSystemFlag.Register(ctx, f)
    43  
    44  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    45  	cmd.OutputFlag.Register(ctx, f)
    46  
    47  	f.BoolVar(&cmd.show, "show", false, "Show PEM encoded server certificate only")
    48  }
    49  
    50  func (cmd *info) Description() string {
    51  	return `Display SSL certificate info for HOST.`
    52  }
    53  
    54  func (cmd *info) Process(ctx context.Context) error {
    55  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    56  		return err
    57  	}
    58  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    59  		return err
    60  	}
    61  	return nil
    62  }
    63  
    64  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    65  	host, err := cmd.HostSystem()
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    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  
    80  	m, err := host.ConfigManager().CertificateManager(ctx)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	info, err := m.CertificateInfo(ctx)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	return cmd.WriteResult(info)
    91  }