go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/logindefs.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package resources
     5  
     6  import (
     7  	"errors"
     8  	"strings"
     9  
    10  	"go.mondoo.com/cnquery/llx"
    11  	"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
    12  	"go.mondoo.com/cnquery/providers/os/resources/logindefs"
    13  )
    14  
    15  func initLogindefs(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
    16  	if x, ok := args["path"]; ok {
    17  		path, ok := x.Value.(string)
    18  		if !ok {
    19  			return nil, nil, errors.New("Wrong type for 'path' in logindefs initialization, it must be a string")
    20  		}
    21  
    22  		f, err := CreateResource(runtime, "file", map[string]*llx.RawData{
    23  			"path": llx.StringData(path),
    24  		})
    25  		if err != nil {
    26  			return nil, nil, err
    27  		}
    28  		args["file"] = llx.ResourceData(f, "file")
    29  		delete(args, "path")
    30  	}
    31  
    32  	return args, nil, nil
    33  }
    34  
    35  const defaultLoginDefsConfig = "/etc/login.defs"
    36  
    37  func (s *mqlLogindefs) id() (string, error) {
    38  	file := s.GetFile()
    39  	if file.Data == nil {
    40  		return "", errors.New("no file for logindefs")
    41  	}
    42  	return file.Data.Path.Data, nil
    43  }
    44  
    45  func (s *mqlLogindefs) file() (*mqlFile, error) {
    46  	f, err := CreateResource(s.MqlRuntime, "file", map[string]*llx.RawData{
    47  		"path": llx.StringData(defaultLoginDefsConfig),
    48  	})
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	return f.(*mqlFile), nil
    53  }
    54  
    55  // borrowed from ssh resource
    56  func (s *mqlLogindefs) content(file *mqlFile) (string, error) {
    57  	c := file.GetContent()
    58  	return c.Data, c.Error
    59  }
    60  
    61  func (s *mqlLogindefs) params(content string) (map[string]interface{}, error) {
    62  	res := make(map[string]interface{})
    63  
    64  	params := logindefs.Parse(strings.NewReader(content))
    65  
    66  	for k, v := range params {
    67  		res[k] = v
    68  	}
    69  
    70  	return res, nil
    71  }