From 9585c2593dba710b8e8c84a8dd9af755cf963776 Mon Sep 17 00:00:00 2001 From: Mobmaker <45888585+Mobmaker55@users.noreply.github.com> Date: Wed, 16 Aug 2023 10:39:36 -0400 Subject: [PATCH] Fix only Regex or only Not bug --- checks.go | 29 ++++++++++++++--------------- checks_linux.go | 14 +++++++------- docs/regex.md | 3 ++- utility_linux.go | 2 +- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/checks.go b/checks.go index 7347754..4ee5716 100644 --- a/checks.go +++ b/checks.go @@ -36,7 +36,7 @@ type cond struct { Key string Value string After string - Regex bool + regex bool } // requireArgs is a convenience function that prints a warning if any required @@ -106,22 +106,21 @@ func runCheck(cond cond) bool { regex := "Regex" condFunc := "" negation := false - cond.Regex = false + cond.regex = false // Ensure that condition type is a valid length if len(cond.Type) <= len(regex) { fail(`Condition type "` + cond.Type + `" is not long enough to be valid. Do you have a "type = 'CheckTypeHere'" for all check conditions?`) return false } - + condFunc = cond.Type if cond.Type[len(cond.Type)-len(not):len(cond.Type)] == not { negation = true condFunc = cond.Type[:len(cond.Type)-len(not)] - } else if cond.Type[len(cond.Type)-len(regex):len(cond.Type)] == regex { - cond.Regex = true + } + if cond.Type[len(cond.Type)-len(regex):len(cond.Type)] == regex { + cond.regex = true condFunc = cond.Type[:len(cond.Type)-len(regex)] - } else { - condFunc = cond.Type } // Catch panic if check type doesn't exist @@ -151,11 +150,11 @@ func runCheck(cond cond) bool { func (c cond) CommandContains() (bool, error) { c.requireArgs("Cmd", "Value") out, err := shellCommandOutput(c.Cmd) - if c.Regex { + if err != nil { + return false, err + } + if c.regex { outTrim := strings.TrimSpace(out) - if err != nil { - return false, err - } return regexp.Match(c.Value, []byte(outTrim)) } return strings.Contains(strings.TrimSpace(out), c.Value), err @@ -222,14 +221,14 @@ func (c cond) FileContains() (bool, error) { } found := false for _, line := range strings.Split(fileContent, "\n") { - if c.Regex { + if c.regex { found, err = regexp.Match(c.Value, []byte(line)) + if err != nil { + return false, err + } } else { found = strings.Contains(line, c.Value) } - if err != nil { - return false, err - } if found { break } diff --git a/checks_linux.go b/checks_linux.go index 0e049d3..9d3f402 100644 --- a/checks_linux.go +++ b/checks_linux.go @@ -13,7 +13,7 @@ func (c cond) AutoCheckUpdatesEnabled() (bool, error) { result, err := cond{ Path: "/etc/apt/apt.conf.d/", Value: `(?i)^\s*APT::Periodic::Update-Package-Lists\s+"1"\s*;\s*$`, - Regex: true, + regex: true, }.DirContains() // If /etc/apt/ does not exist, try dnf (RHEL) if err != nil { @@ -27,7 +27,7 @@ func (c cond) AutoCheckUpdatesEnabled() (bool, error) { applyUpdates, err := cond{ Path: "/etc/dnf/automatic.conf", Value: `(?i)^\s*apply_updates\s*=\s*(1|on|yes|true)`, - Regex: true, + regex: true, }.FileContains() if err != nil { return false, err @@ -82,7 +82,7 @@ func (c cond) FirewallUp() (bool, error) { result, err := cond{ Path: "/etc/ufw/ufw.conf", Value: `^\s*ENABLED=yes\s*$`, - Regex: true, + regex: true, }.FileContains() if err != nil { // If ufw.conf does not exist, check firewalld status (RHEL) @@ -98,13 +98,13 @@ func (c cond) GuestDisabledLDM() (bool, error) { result, err := cond{ Path: "/usr/share/lightdm/lightdm.conf.d/", Value: guestStr, - Regex: true, + regex: true, }.DirContains() if !result { return cond{ Path: "/etc/lightdm/", Value: guestStr, - Regex: true, + regex: true, }.DirContains() } return result, err @@ -249,7 +249,7 @@ func (c cond) UserExists() (bool, error) { return cond{ Path: "/etc/passwd", Value: "^" + c.User + ":", - Regex: true, + regex: true, }.FileContains() } @@ -258,6 +258,6 @@ func (c cond) UserInGroup() (bool, error) { return cond{ Path: "/etc/group", Value: c.Group + `[0-9a-zA-Z,:\s+]+` + c.User, - Regex: true, + regex: true, }.FileContains() } diff --git a/docs/regex.md b/docs/regex.md index da87fde..842f6ac 100644 --- a/docs/regex.md +++ b/docs/regex.md @@ -5,7 +5,8 @@ to score something simple, but we think it significantly increases the overall q to each line of the input file, so currently, no multi-line regexes are currently possible. The checks that are specifically supported are `CommandContainsRegex`, `DirContainsRegex`, and `FileContainsRegex`. -Please note that you **must** append `Regex` to the end for the check to use regular expressions. +Please note that you **must** add `Regex` for the check to use regular expressions. You can also still append `Not` to +the end to invert the condition, such as `CommandContainsRegexNot` > We're using the Golang Regular Expression package ([documentation here](https://godocs.io/regexp)). It uses RE2 > syntax, which is also generally the same as Perl, Python, and other languages. diff --git a/utility_linux.go b/utility_linux.go index 2fb7311..a3ed868 100644 --- a/utility_linux.go +++ b/utility_linux.go @@ -46,7 +46,7 @@ func checkTrace() { result, err := cond{ Path: "/proc/self/status", Value: `^TracerPid:\s+0$`, - Regex: true, + regex: true, }.FileContains() // If there was an error reading the file, the user may be restricting access to /proc for the phocus binary