Skip to content

Commit

Permalink
Relative include paths are now considered to be in the parent ini-fil… (
Browse files Browse the repository at this point in the history
#33)

* Relative include paths are now considered to be in the parent ini-file's folder

* fix a formatting error

* rename test config snclient_incl.ini

* update test case for config, include from rel. subdir
  • Loading branch information
lausser authored Jul 26, 2023
1 parent d3a3263 commit 279f207
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/snclient/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ func (config *Config) ParseINI(file io.Reader, iniPath string) error {

func (config *Config) parseInclude(inclPath, srcPath string) error {
log.Tracef("reading config include: %s", inclPath)
if !filepath.IsAbs(inclPath) {
inclPath = filepath.Join(filepath.Dir(srcPath), inclPath)
}
matchingPaths, err := filepath.Glob(inclPath)
if err != nil {
return fmt.Errorf("malformed include path: %s", err.Error())
Expand Down
22 changes: 22 additions & 0 deletions pkg/snclient/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,25 @@ func TestConfigPackaging(t *testing.T) {
assert.NoErrorf(t, err, "parse ini without error")
assert.Equalf(t, origConfig, strings.TrimSpace(cfg.ToString()), "default config should not change when opened and saved unchanged")
}

func TestConfigRelativeIncludes(t *testing.T) {
testDir, _ := os.Getwd()
pkgDir := filepath.Join(testDir, "t", "configs")
pkgCfgFile := filepath.Join(pkgDir, "snclient_incl.ini")

file, err := os.Open(pkgCfgFile)
assert.NoErrorf(t, err, "open ini without error")

cfg := NewConfig(true)
err = cfg.ParseINI(file, pkgCfgFile)
file.Close()
assert.NoErrorf(t, err, "config parsed")

section := cfg.Section("/settings/WEB/server")
webPort, _ := section.GetString("port")
assert.Equalf(t, "11122", webPort, "got web port")
useSSL, _ := section.GetString("use ssl")
assert.Equalf(t, "false", useSSL, "got use ssl")
webPassword, _ := section.GetString("password")
assert.Equalf(t, "INCL02PW", webPassword, "got password")
}
276 changes: 276 additions & 0 deletions pkg/snclient/t/configs/snclient_incl.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
[/paths]
; exe-path - Path to snclient executable (auto-detected).
exe-path =

; shared-path- Path to shared things.
shared-path = /etc/snclient

; scripts - Path to scripts.
scripts = ${shared-path}/scripts

; certificate-path - Path for certificates.
certificate-path = ${shared-path}


[/modules]
; WEBServer - Enable HTTP REST API requests via check_nsc_web.
WEBServer = enabled

; NRPEServer - Enable NRPE server.
NRPEServer = disabled

; PrometheusServer - Enable /metrics HTTP server for the agent itself.
PrometheusServer = enabled

; CheckExternalScripts - Enable scripts from /settings/external scripts/...
CheckExternalScripts = disabled

; CheckSystem - Collect windows cpu metrics which can be queried by the check_cpu plugin.
CheckSystem = enabled

; CheckSystemUnix - Collect non-windows cpu metrics which can be queried by the check_cpu plugin.
CheckSystemUnix = enabled

; CheckDisk - Controls wether check_drivesize is allowed or not.
CheckDisk = enabled

; CheckWMI - Controls wether check_wmi is allowed or not.
CheckWMI = disabled


[/settings/default]
; allowed hosts - List of ips/networks/hostname allowed to connect.
allowed hosts = 127.0.0.1, ::1

; cache allowed hosts - Cache resolved dns names.
cache allowed hosts = true

; timeout - Timeout when reading packets on incoming sockets.
timeout = 30

; bind to - Allows you to bind server to a specific local address.
; This has to be a dotted ip address not a host name. Leaving this blank will bind to all available IP addresses.
bind to =

; tls min version - Set minimum allowed tls version, leave empty to allow all versions
; or specify one of: tls1.0, tls1.1, tls1.2, tls1.3
tls min version = "tls1.1"


; log - Configure log properties.
[/settings/log]

; file name - The file to write log data to.
; Set this to none or /dev/null to disable log to file or use "stdout" or "stderr" to log there.
file name = /var/log/snclient/snclient.log

; level - Log level to use. Available levels are error,warning,info,debug,trace.
level = info


; log file - Configure log file properties.
[/settings/log/file]

; max size - When file size reaches this it will be truncated to 50% if set to 0 truncation will be disabled.
max size = 0


[/settings/updates]
; automatic updates - Update snclient automatically.
automatic updates = disabled

; automatic restart - Automatically restart snclient after update is finished.
automatic restart = disabled

; channel - comma separated list of channel to search for updates.
channel = stable

; pre release - Control if pre releases from the stable channel should be considered as well.
pre release = false

; insecure - - Skip all ssl verifications.
insecure = false

; update hours - Set time range(s) in which updates are allowed.
update hours = 0-24

; update days - set day range(s) in which updates are allowed.
update days = mon-sun


[/settings/updates/channel]
; stable - This is the stable release channel.
stable = https://api.github.com/repos/ConSol-monitoring/snclient/releases

; dev - This url is for the development channel.
dev = https://api.github.com/repos/ConSol-monitoring/snclient/actions/artifacts

; custom - Any other url works as well.
; custom - https://company-server.local/snclient/snclient-${goos}-${goarch}

; local - Even local files can be checked.
; local - file://z:\updates\snclient-${goos}-${goarch}


[/settings/updates/channel/dev]
; github token - the dev channel requires a github token to download the update
;github token = <GITHUB-TOKEN>


[/settings/NRPE/server]
; insecure - - Skip all ssl verifications
insecure = false

; allow nasty characters - This option determines whether or not the we will allow clients to specify nasty (as in |`&><'"\[]{}) characters in arguments.
allow nasty characters = false

; port - Port to use for NRPE.
port = 5666

; allow arguments - This option determines whether or not the we will allow clients to specify arguments to commands that are executed.
allow arguments = false

; use ssl - This option controls if SSL will be enabled.
use ssl = true

; certificate - SSL certificate to use for the ssl server.
certificate = ${certificate-path}/server.crt

; certificate key - ssl private key to use for the ssl server.
certificate key = ${certificate-path}/server.key

; client certificates - require client certificate (list of pem files).
;client certificates = ${certificate-path}/client1.pem, ${certificate-path}/client2.pem


; Windows system - Section for windows system checks and system settings
[/settings/system/windows]

; default buffer length - Contols the counter bucket size ex.: for cpu counter.
default buffer length = 1h


; Unix system - Section for non windows system checks
[/settings/system/unix]

; default buffer length - Contols the counter bucket size ex.: for cpu counter.
default buffer length = 1h


; External script settings - General settings for the external scripts module (CheckExternalScripts).
[/settings/external scripts]

; timeout - The maximum time in seconds that a command can execute. (if more then this execution will be aborted).
; NOTICE this only affects external commands not internal ones.
timeout = 60

; Allow certain potentially dangerous characters in arguments - This option determines whether or not
; we will allow clients to specify nasty (as in |`&><'"\[]{}) characters in arguments.
allow nasty characters = false

; Allow arguments when executing external scripts - This option determines whether or not the we will allow
; clients to specify arguments to commands that are executed.
allow arguments = false

; Script root folder - Root path where all scripts are contained (You can not upload/download scripts outside this folder).
script root = ${scripts}

; Load all scripts in a given folder - Load all (${script path}/*.*) scripts in a given directory and use them as commands.
script path =

; ignore perfdata - Do not parse performance data from the output
ignore perfdata = no


; External scripts - A list of scripts available to run from the CheckExternalScripts module.
; Syntax is: `command=script arguments`
[/settings/external scripts/scripts]


; script: default - The configuration section for the default script.
[/settings/external scripts/scripts/default]

; ignore perfdata - Do not parse performance data from the output
ignore perfdata = no

; command - Command to execute
command =


; Wrapped scripts - A list of wrapped scripts (ie. script using a template mechanism).
; The template used will be defined by the extension of the script.
; Thus a foo.ps1 will use the ps1 wrapping from the wrappings section.
; Syntax is: `command=script`
[/settings/external scripts/wrapped scripts]


; Command aliases - A list of aliases for already defined commands (with arguments).
; An alias is an internal command that has been predefined to provide a single command without arguments.
; Be careful so you don't create loops (ie check_loop=check_a, check_a=check_loop)
[/settings/external scripts/alias]


; alias: default - The configuration section for the default alias
[/settings/external scripts/alias/default]

; ignore perfdata - Do not parse performance data from the output
ignore perfdata = no

; command - Command to execute
command =


; Script wrappings - A list of templates for defining script commands.
; Enter any command line here and they will be expanded by scripts placed under
; the wrapped scripts section.
; %SCRIPT% will be replaced by the actual script and
; %ARGS% will be replaced by any given arguments.
[/settings/external scripts/wrappings]

; Batch file - Command used for executing wrapped batch files
bat = ${script root}\\%SCRIPT% %ARGS%

; Visual basic script - Command line used for wrapped vbs scripts
vbs = cscript.exe //T:30 //NoLogo ${script root}\\lib\\wrapper.vbs %SCRIPT% %ARGS%

; Powershell Wrapping - Command line used for executing wrapped ps1 (powershell) scripts
ps1 = cmd /c echo If (-Not (Test-Path "${script root}\%SCRIPT%") ) { Write-Host "UNKNOWN: Script `"%SCRIPT%`" not found."; exit(3) }; ${script root}\%SCRIPT% $ARGS$; exit($lastexitcode) | powershell.exe /noprofile -command -


; Web server - Section for http REST service
[/settings/WEB/server]
; password - must be changed from default value, set to empty string to disable passwords.
; password can be stored encrypted when using the format: <HASH>:<hashed password>, ex.: SHA256:...
; supported hash algorithms are SHA256, you can use "snclient hash" to generate password hashes.
password = CHANGEME

; use ssl - This option controls if SSL will be enabled.
use ssl = true

; port - Port to use for WEB server.
port = 8443

; certificate - ssl certificate to use for the ssl server
certificate = ${certificate-path}/server.crt

; certificate key - ssl private key to use for the ssl server
certificate key = ${certificate-path}/server.key

; allow nasty characters - This option determines whether or not the we will allow clients to specify nasty (as in |`&><'"\[]{}) characters in arguments.
allow nasty characters = false

; allow arguments - This option determines whether or not the we will allow clients to specify arguments to commands that are executed.
allow arguments = true


[/settings/Prometheus/server]
; port - Port to use for WEB server.
port = 9999

; use ssl - This option controls if SSL will be enabled.
use ssl = false


; INCLUDED FILES - Files to be included in the configuration
[/includes]
local = snclient_local*.ini
7 changes: 7 additions & 0 deletions pkg/snclient/t/configs/snclient_local_01.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[/settings/WEB/server]
port = 11122
use ssl = true
password = INCL01PW

[/includes]
local = subdir/snclient_local*.ini
3 changes: 3 additions & 0 deletions pkg/snclient/t/configs/subdir/snclient_local_02.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[/settings/WEB/server]
use ssl = false
password = INCL02PW

0 comments on commit 279f207

Please sign in to comment.