github.com/vmware/govmomi@v0.37.1/govc/vm/guest/auth.go (about) 1 /* 2 Copyright (c) 2014-2016 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 guest 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "os" 24 "strings" 25 26 "github.com/vmware/govmomi/vim25/types" 27 ) 28 29 type AuthFlag struct { 30 auth types.NamePasswordAuthentication 31 proc bool 32 } 33 34 func newAuthFlag(ctx context.Context) (*AuthFlag, context.Context) { 35 return &AuthFlag{}, ctx 36 } 37 38 func (flag *AuthFlag) String() string { 39 return fmt.Sprintf("%s:%s", flag.auth.Username, strings.Repeat("x", len(flag.auth.Password))) 40 } 41 42 func (flag *AuthFlag) Set(s string) error { 43 c := strings.SplitN(s, ":", 2) 44 if len(c) > 0 { 45 flag.auth.Username = c[0] 46 if len(c) > 1 { 47 flag.auth.Password = c[1] 48 } 49 } 50 51 return nil 52 } 53 54 func (flag *AuthFlag) Register(ctx context.Context, f *flag.FlagSet) { 55 env := "GOVC_GUEST_LOGIN" 56 value := os.Getenv(env) 57 err := flag.Set(value) 58 if err != nil { 59 fmt.Printf("could not set guest login values: %v", err) 60 } 61 usage := fmt.Sprintf("Guest VM credentials (<user>:<password>) [%s]", env) 62 f.Var(flag, "l", usage) 63 if flag.proc { 64 f.BoolVar(&flag.auth.GuestAuthentication.InteractiveSession, "i", false, "Interactive session") 65 } 66 } 67 68 func (flag *AuthFlag) Process(ctx context.Context) error { 69 if flag.auth.Username == "" { 70 return fmt.Errorf("guest login username must not be empty") 71 } 72 73 return nil 74 } 75 76 func (flag *AuthFlag) Auth() types.BaseGuestAuthentication { 77 return &flag.auth 78 }