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