From 638ddd95d6b7f07c88baa44a8c6aa1022bf405e0 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sun, 7 Oct 2018 19:41:44 +0200 Subject: [PATCH 1/4] Fix problems found by sellcheck in type explorers. --- cdist/conf/type/__apt_ppa/explorer/state | 3 ++- .../conf/type/__go_get/explorer/go-executable | 2 ++ .../type/__jail_freebsd10/explorer/status | 2 +- .../conf/type/__jail_freebsd9/explorer/status | 2 +- .../type/__postgres_database/explorer/state | 2 +- .../conf/type/__postgres_role/explorer/state | 2 +- .../type/__ssh_authorized_key/explorer/entry | 2 +- .../conf/type/__start_on_boot/explorer/state | 21 ++++++++++++++++--- .../type/__zypper_service/explorer/service_id | 2 +- 9 files changed, 28 insertions(+), 10 deletions(-) diff --git a/cdist/conf/type/__apt_ppa/explorer/state b/cdist/conf/type/__apt_ppa/explorer/state index 2bb4f65a..d47e7d20 100755 --- a/cdist/conf/type/__apt_ppa/explorer/state +++ b/cdist/conf/type/__apt_ppa/explorer/state @@ -23,10 +23,11 @@ name="$__object_id" +# shellcheck disable=SC1091 . /etc/lsb-release repo_name="${name#ppa:}" -repo_file_name="$(echo "$repo_name" | sed -e "s|[/:]|-|" -e "s|\.|_|")-${DISTRIB_CODENAME}.list" +repo_file_name="$(echo "$repo_name" | sed -e 's|[/:]|-|' -e 's|\.|_|')-${DISTRIB_CODENAME}.list" [ -s "/etc/apt/sources.list.d/${repo_file_name}" ] \ && echo present || echo absent diff --git a/cdist/conf/type/__go_get/explorer/go-executable b/cdist/conf/type/__go_get/explorer/go-executable index bdce7559..87182282 100755 --- a/cdist/conf/type/__go_get/explorer/go-executable +++ b/cdist/conf/type/__go_get/explorer/go-executable @@ -1,4 +1,6 @@ #!/bin/sh +# shellcheck disable=SC1091 [ -f /etc/environment ] && . /etc/environment +# shellcheck disable=SC1091 [ -f /etc/profile ] && . /etc/profile go version 2>/dev/null || true diff --git a/cdist/conf/type/__jail_freebsd10/explorer/status b/cdist/conf/type/__jail_freebsd10/explorer/status index 1ceba212..c8039f21 100755 --- a/cdist/conf/type/__jail_freebsd10/explorer/status +++ b/cdist/conf/type/__jail_freebsd10/explorer/status @@ -39,7 +39,7 @@ fi # backslash-escaped $jaildir sjaildir="$(echo ${jaildir} | sed 's#/#\\/#g')" -jls_output="$(jls | grep "[ ]${sjaildir}\/${name}\$")" || true +jls_output="$(jls | grep "[ ]${sjaildir}\\/${name}\$")" || true if [ -n "${jls_output}" ]; then echo "STARTED" diff --git a/cdist/conf/type/__jail_freebsd9/explorer/status b/cdist/conf/type/__jail_freebsd9/explorer/status index 1ceba212..c8039f21 100755 --- a/cdist/conf/type/__jail_freebsd9/explorer/status +++ b/cdist/conf/type/__jail_freebsd9/explorer/status @@ -39,7 +39,7 @@ fi # backslash-escaped $jaildir sjaildir="$(echo ${jaildir} | sed 's#/#\\/#g')" -jls_output="$(jls | grep "[ ]${sjaildir}\/${name}\$")" || true +jls_output="$(jls | grep "[ ]${sjaildir}\\/${name}\$")" || true if [ -n "${jls_output}" ]; then echo "STARTED" diff --git a/cdist/conf/type/__postgres_database/explorer/state b/cdist/conf/type/__postgres_database/explorer/state index dc9659e2..54eb768d 100755 --- a/cdist/conf/type/__postgres_database/explorer/state +++ b/cdist/conf/type/__postgres_database/explorer/state @@ -20,7 +20,7 @@ name="$__object_id" -if su - postgres -c "echo '\q' | psql '$name'" 2>/dev/null; then +if su - postgres -c "echo '\\q' | psql '$name'" 2>/dev/null; then echo "present" else echo "absent" diff --git a/cdist/conf/type/__postgres_role/explorer/state b/cdist/conf/type/__postgres_role/explorer/state index 8c102df9..40f64cef 100755 --- a/cdist/conf/type/__postgres_role/explorer/state +++ b/cdist/conf/type/__postgres_role/explorer/state @@ -20,7 +20,7 @@ name="$__object_id" -if su - postgres -c "psql -c '\du' | grep -q '^ *$name *|'"; then +if su - postgres -c "psql -c '\\du' | grep -q '^ *$name *|'"; then echo "present" else echo "absent" diff --git a/cdist/conf/type/__ssh_authorized_key/explorer/entry b/cdist/conf/type/__ssh_authorized_key/explorer/entry index b528b26e..78efbb48 100755 --- a/cdist/conf/type/__ssh_authorized_key/explorer/entry +++ b/cdist/conf/type/__ssh_authorized_key/explorer/entry @@ -27,5 +27,5 @@ then file="$(cat "$__object/parameter/file")" # get any entries that match the type and key - grep ".*$type_and_key\([ \n]\|$\)" "$file" || true + grep ".*$type_and_key\\([ \\n]\\|$\\)" "$file" || true fi diff --git a/cdist/conf/type/__start_on_boot/explorer/state b/cdist/conf/type/__start_on_boot/explorer/state index d8f9b7ba..cef9013e 100644 --- a/cdist/conf/type/__start_on_boot/explorer/state +++ b/cdist/conf/type/__start_on_boot/explorer/state @@ -38,12 +38,27 @@ if [ "$init" = 'systemd' ]; then else case "$os" in debian|openwrt|devuan) - state="present" - [ -f "/etc/rc$runlevel.d/S"??"$name" ] || state="absent" + state="absent" + for file in "/etc/rc$runlevel.d/S"??"$name" + do + if [ -f "$file" ] + then + state="present" + break + fi + done ;; ubuntu) state="absent" - [ -f "/etc/rc$runlevel.d/S"??"$name" ] && state="present" + for file in "/etc/rc$runlevel.d/S"??"$name" + do + if [ -f "$file" ] + then + state="present" + break + fi + done + [ -f "/etc/init/${name}.conf" ] && state="present" ;; diff --git a/cdist/conf/type/__zypper_service/explorer/service_id b/cdist/conf/type/__zypper_service/explorer/service_id index 91858d84..7161f804 100644 --- a/cdist/conf/type/__zypper_service/explorer/service_id +++ b/cdist/conf/type/__zypper_service/explorer/service_id @@ -27,4 +27,4 @@ else fi # simpler command which works only on SLES11 SP3 or newer: # echo $(zypper ls -u -E | grep -E "\<$uri\>" | cut -d'|' -f 1 ) -zypper ls -u | grep -E '^([^|]+\|){3,3} Yes' | grep -E "\<$uri\>" | cut -d'|' -f 1 +zypper ls -u | grep -E '^([^|]+\|){3,3} Yes' | grep -E "\\<$uri\\>" | cut -d'|' -f 1 From ec186673bf4021d67221d011921ce7165e9473f0 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sun, 7 Oct 2018 19:46:34 +0200 Subject: [PATCH 2/4] Add TODO markers for SC2012. --- cdist/conf/type/__link/explorer/state | 3 +++ cdist/conf/type/__link/explorer/type | 1 + 2 files changed, 4 insertions(+) diff --git a/cdist/conf/type/__link/explorer/state b/cdist/conf/type/__link/explorer/state index 587835a2..edccf08d 100755 --- a/cdist/conf/type/__link/explorer/state +++ b/cdist/conf/type/__link/explorer/state @@ -33,6 +33,7 @@ destination_dir="${destination%/*}" case "$type" in symbolic) cd "$destination_dir" || exit 1 + # TODO SC2012: use readlink or something? source_is=$(ls -l "$destination" | sed 's/.*-> //g') if [ -h "$destination" ]; then # ignore trailing slashes for comparison @@ -52,7 +53,9 @@ case "$type" in echo sourcemissing exit 0 fi + # TODO SC2012: use stat? destination_inode=$(ls -i "$destination" | awk '{print $1}') + # TODO SC2012: use stat? source_inode=$(ls -i "$source" | awk '{print $1}') if [ "$destination_inode" -eq "$source_inode" ]; then echo present diff --git a/cdist/conf/type/__link/explorer/type b/cdist/conf/type/__link/explorer/type index 48a79bf0..aa1aff2c 100755 --- a/cdist/conf/type/__link/explorer/type +++ b/cdist/conf/type/__link/explorer/type @@ -31,6 +31,7 @@ elif [ -f "$destination" ]; then type="$(cat "$__object/parameter/type")" case "$type" in hard) + # TODO SC2012: use stat? link_count=$(ls -l "$destination" | awk '{ print $2 }') if [ "$link_count" -gt 1 ]; then echo hardlink From 0458f66c96b1061b81f759cfa47f8fe20f2d66ae Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Mon, 8 Oct 2018 15:35:29 +0200 Subject: [PATCH 3/4] Resolve SC2012. --- cdist/conf/type/__link/explorer/state | 11 +++++---- cdist/conf/type/__link/explorer/type | 34 ++++++++++++++------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/cdist/conf/type/__link/explorer/state b/cdist/conf/type/__link/explorer/state index edccf08d..a601d53b 100755 --- a/cdist/conf/type/__link/explorer/state +++ b/cdist/conf/type/__link/explorer/state @@ -33,8 +33,7 @@ destination_dir="${destination%/*}" case "$type" in symbolic) cd "$destination_dir" || exit 1 - # TODO SC2012: use readlink or something? - source_is=$(ls -l "$destination" | sed 's/.*-> //g') + source_is=$(readlink "$destination") if [ -h "$destination" ]; then # ignore trailing slashes for comparison if [ "${source_is%/}" = "${source%/}" ]; then @@ -53,9 +52,13 @@ case "$type" in echo sourcemissing exit 0 fi - # TODO SC2012: use stat? + # Currently not worth the effor to change it, stat is not defined by POSIX + # and different OSes has different implementations for it. + # shellcheck disable=SC2012 destination_inode=$(ls -i "$destination" | awk '{print $1}') - # TODO SC2012: use stat? + # Currently not worth the effor to change it, stat is not defined by POSIX + # and different OSes has different implementations for it. + # shellcheck disable=SC2012 source_inode=$(ls -i "$source" | awk '{print $1}') if [ "$destination_inode" -eq "$source_inode" ]; then echo present diff --git a/cdist/conf/type/__link/explorer/type b/cdist/conf/type/__link/explorer/type index aa1aff2c..796f1ac6 100755 --- a/cdist/conf/type/__link/explorer/type +++ b/cdist/conf/type/__link/explorer/type @@ -24,24 +24,26 @@ destination="/$__object_id" if [ ! -e "$destination" ]; then - echo none + echo none elif [ -h "$destination" ]; then - echo symlink + echo symlink elif [ -f "$destination" ]; then - type="$(cat "$__object/parameter/type")" - case "$type" in - hard) - # TODO SC2012: use stat? - link_count=$(ls -l "$destination" | awk '{ print $2 }') - if [ "$link_count" -gt 1 ]; then - echo hardlink - exit 0 - fi - ;; - esac - echo file + type="$(cat "$__object/parameter/type")" + case "$type" in + hard) + # Currently not worth the effor to change it, stat is not defined by POSIX + # and different OSes has different implementations for it. + # shellcheck disable=SC2012 + ink_count=$(ls -l "$destination" | awk '{ print $2 }') + if [ "$link_count" -gt 1 ]; then + echo hardlink + exit 0 + fi + ;; + esac + echo file elif [ -d "$destination" ]; then - echo directory + echo directory else - echo unknown + echo unknown fi From 69790ec138a9ef63c179b5d2ca3777c758ed7347 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Mon, 8 Oct 2018 15:43:42 +0200 Subject: [PATCH 4/4] Fix typing error. --- cdist/conf/type/__link/explorer/type | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdist/conf/type/__link/explorer/type b/cdist/conf/type/__link/explorer/type index 796f1ac6..b322bf42 100755 --- a/cdist/conf/type/__link/explorer/type +++ b/cdist/conf/type/__link/explorer/type @@ -34,7 +34,7 @@ elif [ -f "$destination" ]; then # Currently not worth the effor to change it, stat is not defined by POSIX # and different OSes has different implementations for it. # shellcheck disable=SC2012 - ink_count=$(ls -l "$destination" | awk '{ print $2 }') + link_count=$(ls -l "$destination" | awk '{ print $2 }') if [ "$link_count" -gt 1 ]; then echo hardlink exit 0