github.com/vmware/govmomi@v0.51.0/cli/flags/host_connect.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 flags 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 "net/url" 12 13 "github.com/vmware/govmomi/fault" 14 "github.com/vmware/govmomi/object" 15 "github.com/vmware/govmomi/vim25" 16 "github.com/vmware/govmomi/vim25/types" 17 ) 18 19 type HostConnectFlag struct { 20 common 21 22 types.HostConnectSpec 23 24 noverify bool 25 } 26 27 var hostConnectFlagKey = flagKey("hostConnect") 28 29 func NewHostConnectFlag(ctx context.Context) (*HostConnectFlag, context.Context) { 30 if v := ctx.Value(hostConnectFlagKey); v != nil { 31 return v.(*HostConnectFlag), ctx 32 } 33 34 v := &HostConnectFlag{} 35 ctx = context.WithValue(ctx, hostConnectFlagKey, v) 36 return v, ctx 37 } 38 39 func (flag *HostConnectFlag) Register(ctx context.Context, f *flag.FlagSet) { 40 flag.RegisterOnce(func() { 41 f.StringVar(&flag.HostName, "hostname", "", "Hostname or IP address of the host") 42 f.StringVar(&flag.UserName, "username", "", "Username of administration account on the host") 43 f.StringVar(&flag.Password, "password", "", "Password of administration account on the host") 44 f.StringVar(&flag.SslThumbprint, "thumbprint", "", "SHA-1 thumbprint of the host's SSL certificate") 45 f.BoolVar(&flag.Force, "force", false, "Force when host is managed by another VC") 46 47 f.BoolVar(&flag.noverify, "noverify", false, "Accept host thumbprint without verification") 48 }) 49 } 50 51 func (flag *HostConnectFlag) Process(ctx context.Context) error { 52 return nil 53 } 54 55 // Spec attempts to fill in SslThumbprint if empty. 56 // First checks GOVC_TLS_KNOWN_HOSTS, if not found and noverify=true then 57 // use object.HostCertificateInfo to get the thumbprint. 58 func (flag *HostConnectFlag) Spec(c *vim25.Client) types.HostConnectSpec { 59 spec := flag.HostConnectSpec 60 61 if spec.SslThumbprint == "" { 62 spec.SslThumbprint = c.Thumbprint(spec.HostName) 63 64 if spec.SslThumbprint == "" && flag.noverify { 65 var info object.HostCertificateInfo 66 t := c.DefaultTransport() 67 _ = info.FromURL(&url.URL{Host: spec.HostName}, t.TLSClientConfig) 68 spec.SslThumbprint = info.ThumbprintSHA1 69 } 70 } 71 72 return spec 73 } 74 75 // Fault checks if error is SSLVerifyFault, including the thumbprint if so 76 func (flag *HostConnectFlag) Fault(err error) error { 77 var verify *types.SSLVerifyFault 78 if _, ok := fault.As(err, &verify); ok { 79 return fmt.Errorf("%s thumbprint=%s", err, verify.Thumbprint) 80 } 81 82 return err 83 }