go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/ntp.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  )
    13  
    14  func initNtpConf(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
    15  	if x, ok := args["path"]; ok {
    16  		path, ok := x.Value.(string)
    17  		if !ok {
    18  			return nil, nil, errors.New("Wrong type for 'path' in ntp.conf initialization, it must be a string")
    19  		}
    20  
    21  		f, err := CreateResource(runtime, "file", map[string]*llx.RawData{
    22  			"path": llx.StringData(path),
    23  		})
    24  		if err != nil {
    25  			return nil, nil, err
    26  		}
    27  		args["file"] = llx.ResourceData(f, "file")
    28  		delete(args, "path")
    29  	}
    30  
    31  	return args, nil, nil
    32  }
    33  
    34  const defaultNtpConf = "/etc/ntp.conf"
    35  
    36  func (s *mqlNtpConf) id() (string, error) {
    37  	file := s.GetFile()
    38  	if file.Error != nil {
    39  		return "", file.Error
    40  	}
    41  	if file.Data == nil {
    42  		return "", errors.New("cannot get file for ntp.conf")
    43  	}
    44  	return file.Data.Path.Data, nil
    45  }
    46  
    47  func (s *mqlNtpConf) file() (*mqlFile, error) {
    48  	f, err := CreateResource(s.MqlRuntime, "file", map[string]*llx.RawData{
    49  		"path": llx.StringData(defaultNtpConf),
    50  	})
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return f.(*mqlFile), nil
    55  }
    56  
    57  func (s *mqlNtpConf) content(file *mqlFile) (string, error) {
    58  	content := file.GetContent()
    59  	return content.Data, content.Error
    60  }
    61  
    62  func (s *mqlNtpConf) settings(content string) ([]interface{}, error) {
    63  	lines := strings.Split(content, "\n")
    64  
    65  	settings := []interface{}{}
    66  	var line string
    67  	for i := range lines {
    68  		line = lines[i]
    69  		if idx := strings.Index(line, "#"); idx >= 0 {
    70  			line = line[0:idx]
    71  		}
    72  		line = strings.Trim(line, " \t\r")
    73  
    74  		if line != "" {
    75  			settings = append(settings, line)
    76  		}
    77  	}
    78  
    79  	return settings, nil
    80  }
    81  
    82  func (s *mqlNtpConf) servers(settings []interface{}) ([]interface{}, error) {
    83  	res := []interface{}{}
    84  	var line string
    85  	for i := range settings {
    86  		line = settings[i].(string)
    87  		if strings.HasPrefix(line, "server ") {
    88  			res = append(res, line[7:])
    89  		}
    90  	}
    91  
    92  	return res, nil
    93  }
    94  
    95  func (s *mqlNtpConf) restrict(settings []interface{}) ([]interface{}, error) {
    96  	res := []interface{}{}
    97  	var line string
    98  	for i := range settings {
    99  		line = settings[i].(string)
   100  		if strings.HasPrefix(line, "restrict ") {
   101  			res = append(res, line[9:])
   102  		}
   103  	}
   104  
   105  	return res, nil
   106  }
   107  
   108  func (s *mqlNtpConf) fudge(settings []interface{}) ([]interface{}, error) {
   109  	res := []interface{}{}
   110  	var line string
   111  	for i := range settings {
   112  		line = settings[i].(string)
   113  		if strings.HasPrefix(line, "fudge ") {
   114  			res = append(res, line[6:])
   115  		}
   116  	}
   117  
   118  	return res, nil
   119  }