go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/authorizedkeys/authorizedkeys.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package authorizedkeys 5 6 import ( 7 "bufio" 8 "encoding/base64" 9 "io" 10 "strings" 11 12 "golang.org/x/crypto/ssh" 13 ) 14 15 // most ssh keys include base64 padding, so lets use it too (not default in Go) 16 var RawStdEncoding = base64.StdEncoding.WithPadding(base64.StdPadding) 17 18 type Entry struct { 19 Line int64 20 Key ssh.PublicKey 21 Label string 22 Options []string 23 } 24 25 func (e Entry) Base64Key() string { 26 return RawStdEncoding.EncodeToString(e.Key.Marshal()) 27 } 28 29 func Parse(r io.Reader) ([]Entry, error) { 30 res := []Entry{} 31 scanner := bufio.NewScanner(r) 32 33 lineNo := int64(1) 34 for scanner.Scan() { 35 line := scanner.Text() 36 37 in := strings.TrimSpace(line) 38 if len(in) == 0 || in[0] == '#' { 39 continue 40 } 41 42 key, comment, options, _, err := ssh.ParseAuthorizedKey([]byte(line)) 43 if err != nil { 44 return nil, err 45 } 46 47 res = append(res, Entry{ 48 Line: lineNo, 49 Key: key, 50 Label: comment, 51 Options: options, 52 }) 53 lineNo++ 54 } 55 return res, nil 56 }