Skip to content

Commit

Permalink
Fixed nt-discovery.sh and parser
Browse files Browse the repository at this point in the history
  • Loading branch information
victormlg committed Jan 17, 2025
1 parent fdc173b commit de22413
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 38 deletions.
37 changes: 30 additions & 7 deletions cf_remote/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ def os_release(inp):
log.debug("Cannot parse os-release file (empty)")
return None
d = OrderedDict()
for line in inp.split(r"\n"):
for line in inp.splitlines():
line = line.strip()
if "=" not in line:
continue
key, sep, value = line.partition("=")
assert "=" not in key
if len(value) > 1 and value[0] == value[-1] and value[0] in ["'", '"']:
value = value[1:-1]
d[key] = value.strip(r"\"")
d[key] = value
return d


Expand Down Expand Up @@ -227,13 +227,36 @@ def is_different_checksum(checksum, content):
def parse_envfile(text):

data = OrderedDict()
lines = text.split("\n")
lines = text.splitlines()
for line in lines:
if "=" not in line:
return user_error("Invalid env file format: '=' missing")

key, _, val = line.partition("=")

if not key:
return user_error("Invalid env file format: Key missing")

if not re.fullmatch(r"([A-Z]+\_?)+", key):
return user_error("Invalid env file format: Invalid key")

if not (val.startswith('"') and val.endswith('"')):
return user_error(
"Invalid env file format: value must start and end with double quotes"
)

if "=" in line:
key, _, val = line.partition("=")
if check_unescaped_character(val, '"'):
return user_error("Invalid env file format: quotes not escaped")

if key and re.fullmatch(r"NTD\_([A-Z]+\_?)+", key):
data[key] = val.strip(r'\"')
data[key] = val.strip('"').encode("utf-8").decode("unicode_escape")

return data


def check_unescaped_character(string, char):
for i in range(1, len(string)):

if string[i] == char and string[i - 1] != str(chr(92)):
return False

return True
27 changes: 14 additions & 13 deletions nt-discovery.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#!/bin/bash

set -o pipefail

run_command() {
# $1: command to run
# $2: variable name to store in / output
# $3: custom error message (optional)
result="$(bash -c "$1" 2>&1)"
result="$(bash -c "$1" 2>&1 | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed -e 's/\"/\\"/g')"
status=$?
if [ "$status" -eq "0" ]; then
echo "NTD_$2=\"$result\"" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed -e 's/\"/\\"/g'
echo "NTD_$2=\"$result\""
else
echo "NTD_$2_CMD=\"$1\"" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed -e 's/\"/\\"/g'
echo "NTD_$2_CMD=\"$1\""
# custom output result
if [ "$#" -eq "3" ]; then
echo "NTD_$2_ERROR=\"$3\"" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed -e 's/\"/\\"/g'
echo "NTD_$2_ERROR=\"$3\""
else
echo "NTD_$2_ERROR=\"$result\"" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed -e 's/\"/\\"/g'
echo "NTD_$2_ERROR=\"$result\""
fi
fi
}
Expand All @@ -36,18 +38,17 @@ if ! [ $? -eq "0" ]; then
fi
fi

run_command "command -v $cfagent_path" "CFAGENT_PATH"
run_command "command -v $cfagent_path" "CFAGENT_PATH" "Cannot find cf-agent"
run_command "$cfagent_path --version" "CFAGENT_VERSION"
run_command "cat /var/cfengine/policy_server.dat" "POLICY_SERVER"

# packages

run_command "echo $UID" "UID"
run_command "command -v dpkg" "DPKG"
run_command "command -v rpm" "RPM"
run_command "command -v yum" "YUM"
run_command "command -v apt" "APT"
run_command "command -v pkg" "PKG"
run_command "command -v zypper" "ZYPPER"

run_command "command -v dpkg" "DPKG" "Cannot find dpkg"
run_command "command -v rpm" "RPM" "Cannot find rpm"
run_command "command -v yum" "YUM" "Cannot find yum"
run_command "command -v apt" "APT" "Cannot find apt"
run_command "command -v pkg" "PKG" "Cannot find pkg"
run_command "command -v zypper" "ZYPPER" "Cannot find zypper"

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pip has gotten strict with version numbers
# so change it to: "1.3.3+22.git.gdf81228"
# See: https://peps.python.org/pep-0440/#local-version-segments
v,i,s = cf_remote_version.split("-")
v, i, s = cf_remote_version.split("-")
cf_remote_version = v + "+" + i + ".git." + s

assert "-" not in cf_remote_version
Expand Down
32 changes: 15 additions & 17 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
from cf_remote.utils import parse_envfile
from cf_remote.utils import check_unescaped_character, parse_envfile


def test_parse_envfile():
data = parse_envfile("NTD_TEST=\"test\"")
assert "NTD_TEST" in data
data = parse_envfile('NTD_TEST="test"')
assert "NTD_TEST" in data
assert data["NTD_TEST"] == "test"

data = parse_envfile("NTD_X\"test\"")
assert not data
data = parse_envfile('NTD_TEST=""helloworld""')
assert data["NTD_TEST"] == "helloworld"

data = parse_envfile("=\"test\"")
assert not data
data = parse_envfile('NTD_TEST="\\nhello"')
assert data["NTD_TEST"] == "\nhello"

data = parse_envfile("NTD_test=\"test\"")
assert not data

data = parse_envfile("NTD_TEST=\"hello\"world\"")
assert "\"" in data["NTD_TEST"]
data = parse_envfile('NTD_TEST="test"\nNTD_NEWLINE="newline"')
assert len(data) == 2

data = parse_envfile("NTD_TEST=\"test\"\\n\"newline\"")
data = parse_envfile('NTD_TEST="NTD_TEST_TWO="test"\\nhello"')
assert len(data) == 1

data = parse_envfile("NTD_TEST=\"test\"\nnNTD_NEWLINE=\"newline\"")
assert len(data) == 1

data = parse_envfile("NTD_TEST=\"test\"\nNTD_NEWLINE=\"newline\"")
assert len(data) == 2
def test_check_unescaped_character():
assert check_unescaped_character(r"test", '"')
assert check_unescaped_character(r"\"test\"", '"')
assert not check_unescaped_character(r'hello"world', '"')
assert not check_unescaped_character(r'hello\""world', '"')

0 comments on commit de22413

Please sign in to comment.