go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/_motor/providers/ssh/hostkey.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package ssh 5 6 import ( 7 "os" 8 "path/filepath" 9 10 "github.com/mitchellh/go-homedir" 11 "github.com/rs/zerolog/log" 12 "golang.org/x/crypto/ssh" 13 "golang.org/x/crypto/ssh/knownhosts" 14 ) 15 16 func KnownHostsCallback() (ssh.HostKeyCallback, error) { 17 home, err := homedir.Dir() 18 if err != nil { 19 log.Debug().Err(err).Msg("Failed to determine user home directory") 20 return nil, err 21 } 22 23 // load default host keys 24 files := []string{ 25 filepath.Join(home, ".ssh", "known_hosts"), 26 // see https://cloud.google.com/compute/docs/instances/connecting-to-instance 27 // NOTE: content in that file is structured by compute.instanceid key 28 // TODO: we need to keep the instance information during the resolve step 29 filepath.Join(home, ".ssh", "google_compute_known_hosts"), 30 } 31 32 // filter all files that do not exits 33 existentKnownHosts := []string{} 34 for i := range files { 35 _, err := os.Stat(files[i]) 36 if err == nil { 37 log.Debug().Str("file", files[i]).Msg("load ssh known_hosts file") 38 existentKnownHosts = append(existentKnownHosts, files[i]) 39 } 40 } 41 42 return knownhosts.New(existentKnownHosts...) 43 }