go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/authorizedkeys.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package resources 5 6 import ( 7 "errors" 8 "strconv" 9 "strings" 10 11 "go.mondoo.com/cnquery/llx" 12 "go.mondoo.com/cnquery/providers-sdk/v1/plugin" 13 "go.mondoo.com/cnquery/providers/os/resources/authorizedkeys" 14 ) 15 16 func (x *mqlAuthorizedkeysEntry) id() (string, error) { 17 file := x.File.Data 18 if file == nil { 19 return "", errors.New("cannot determine authorized keys ID (missing file)") 20 } 21 22 path := file.Path.Data 23 if path == "" { 24 return "", errors.New("cannot determine authorized keys ID (missing file path)") 25 } 26 27 return path + ":" + strconv.FormatInt(x.Line.Data, 10), nil 28 } 29 30 func initAuthorizedkeys(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) { 31 // users may supply only the file or the path. Until we deprecate path in this 32 // resource, we have to make sure it gets filled; if we receive a file, 33 // set it from the file (for consistency) 34 if v, ok := args["file"]; ok { 35 file, ok := v.Value.(*mqlFile) 36 if !ok { 37 return nil, nil, errors.New("wrong type for 'file' in authorizedkeys initialization, it must be a file") 38 } 39 40 args["path"] = llx.StringData(file.Path.Data) 41 } 42 43 if path, ok := args["path"]; ok { 44 f, err := CreateResource(runtime, "file", map[string]*llx.RawData{ 45 "path": path, 46 }) 47 if err != nil { 48 return nil, nil, err 49 } 50 51 args["file"] = llx.ResourceData(f, "file") 52 } 53 54 return args, nil, nil 55 } 56 57 func (x *mqlAuthorizedkeys) id() (string, error) { 58 file := x.File.Data 59 if file == nil { 60 return "", errors.New("cannot determine authorized keys ID (missing file)") 61 } 62 63 path := file.Path.Data 64 if path == "" { 65 return "", errors.New("cannot determine authorized keys ID (missing file path)") 66 } 67 68 return "authorizedkeys:" + path, nil 69 } 70 71 func (a *mqlAuthorizedkeys) content(file *mqlFile) (string, error) { 72 if !file.GetExists().Data { 73 return "", file.Exists.Error 74 } 75 76 content := file.GetContent() 77 return content.Data, content.Error 78 } 79 80 func (x *mqlAuthorizedkeys) list(file *mqlFile, content string) ([]interface{}, error) { 81 res := []interface{}{} 82 83 if !file.GetExists().Data { 84 return res, file.Exists.Error 85 } 86 87 entries, err := authorizedkeys.Parse(strings.NewReader(content)) 88 if err != nil { 89 return nil, err 90 } 91 92 for i := range entries { 93 entry := entries[i] 94 95 ae, err := CreateResource(x.MqlRuntime, "authorizedkeys.entry", map[string]*llx.RawData{ 96 "line": llx.IntData(entry.Line), 97 "type": llx.StringData(entry.Key.Type()), 98 "key": llx.StringData(entry.Base64Key()), 99 "label": llx.StringData(entry.Label), 100 "options": llx.ArrayData(llx.TArr2Raw[string](entry.Options), "string"), 101 "file": llx.ResourceData(file, "file"), 102 }) 103 if err != nil { 104 return nil, err 105 } 106 107 res = append(res, ae.(*mqlAuthorizedkeysEntry)) 108 } 109 110 return res, nil 111 }