Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First draft of passwd discovery agent. #29

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions buildtools/vimrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Set this as a .vimrc and call your edits with "vim -u <this_vimrc> <file>"
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
set number
set colorcolumn=80
119 changes: 119 additions & 0 deletions discovery_agents/passwd
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/sh -eu
# vim: smartindent tabstop=4 shiftwidth=4 expandtab number colorcolumn=80
#
# This file is part of the Assimilation Project.
#
# Author: Leam Hall <[email protected]>
# Copyright (C) 2013,2014 - Assimilation Systems Limited
#
# Free support is available from the Assimilation Project community - http://assimproj.org
# Paid support is available from Assimilation Systems Limited - http://assimilationsystems.com
#
# The Assimilation software is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The Assimilation software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the Assimilation Project software. If not, see http://www.gnu.org/licenses/
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin

set -e
: ${PASSWD_FILE:=/etc/passwd}

spacer=' '
two_spacers=' '
comma=',
'

discover() {
prefix=" "
outcount=0
cat <<-END
{
"discovertype": "passwd",
"description": "${PASSWD_FILE} information",
"host": "$(uname -n)",
"source": "$0",
"data": {
END
if [ ! -f "$PASSWD_FILE" -o ! -r "$PASSWD_FILE" ]
then
printf '%s"NODATA": "ERROR: Cannot read %s"\n' "$prefix" "$PASSWD_FILE"
else

while IFS=: read user placeholder uid gid comment home shell
do
if [ ${outcount} == 1 ]
then
printf "%s\n" "${comma}"
fi
outcount=1

if [ -z ${user} ]
then
_USER='NULL'
else
_USER=${user}
fi

placeholder='x'

expr ${uid} + 1 > /dev/null 2>&1
if [ $? == 0 ]
then
_UID=$uid
else
_UID=NULL
fi

expr ${gid} + 1 > /dev/null 2>&1
if [ $? == 0 ]
then
_GID=$gid
else
_GID=NULL
fi

if [ -z "${comment}" ]
then
_COMMENT=""
else
_COMMENT=${comment}
fi

if [ -z "${home}" ]
then
_HOME=""
else
_HOME=${home}
fi

if [ -z "${shell}" ]
then
_SHELL=""
else
_SHELL=${shell}
fi

printf "%s" "\"${_USER}\" : {
"
printf "%s %s" '"placeholder": ' "\"${placeholder}\"$comma"
printf "%s %s" '"uid": ' "${_UID}$comma"
printf "%s %s" '"gid": ' "${_GID}$comma"
printf "%s %s" '"comment": ' "\"${_COMMENT}\"$comma"
printf "%s %s" '"home": ' "\"${_HOME}\"$comma"
printf "%s %s}" '"shell": ' "\"${_SHELL}\""
done < ${PASSWD_FILE}

fi
printf "\n}\n}\n"
}

discover
5 changes: 5 additions & 0 deletions testcode/discovery_input/passwd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
memcached:x:493:488:Memcached daemon:/var/run/memcached:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
neo4j:x:492:484::/usr/share/neo4j:/bin/false
assimilation:x:491:483:Assimilation Collective Management Authority:/home/assimilation:/sbin/nologin
52 changes: 52 additions & 0 deletions testcode/discovery_output/passwd
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"discovertype": "passwd",
"description": "passwd information",
"host": "FAKETESTHOST",
"source": "../../discovery_agents/passwd",
"data": {
"memcached": {
"placeholder": "x",
"uid": 493,
"gid": 488,
"comment": "Memcached daemon",
"home": "/var/run/memcached",
"shell": "/sbin/nologin"
},

"mailnull": {
"placeholder": "x",
"uid": 47,
"gid": 47,
"comment": "",
"home": "/var/spool/mqueue",
"shell": "/sbin/nologin"
},

"smmsp": {
"placeholder": "x",
"uid": 51,
"gid": 51,
"comment": "",
"home": "/var/spool/mqueue",
"shell": "/sbin/nologin"
},

"neo4j": {
"placeholder": "x",
"uid": 492,
"gid": 484,
"comment": "",
"home": "/usr/share/neo4j",
"shell": "/bin/false"
},

"assimilation": {
"placeholder": "x",
"uid": 491,
"gid": 483,
"comment": "Assimilation Collective Management Authority",
"home": "/home/assimilation",
"shell": "/sbin/nologin"
}
}
}
160 changes: 160 additions & 0 deletions testcode/leam_test_discovery.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/bin/sh
# vim: smartindent tabstop=4 shiftwidth=4 expandtab number
#
# Program to regression test discovery agents.
#
# This file is part of the Assimilation Project.
#
# Author: Alan Robertson <[email protected]>
# Copyright (C) 2015 - Assimilation Systems Limited
#
# Free support is available from the Assimilation Project community - http://assimproj.org
# Paid support is available from Assimilation Systems Limited - http://assimilationsystems.com
#
# The Assimilation software is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The Assimilation software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the Assimilation Project software. If not, see http://www.gnu.org/licenses/
#
#
ourdir=$(dirname $0)
TESTDIR=../../discovery_agents
INPUTDIR=$PWD/$ourdir/discovery_input
OUTPUTDIR=$PWD/$ourdir/discovery_output

if [ ! -d /tmp/assim_proj_test ]
then
mkdir /tmp/assim_proj_test
fi

#TMPOUT=/tmp/assim_proj_test/$$.testout
#trap 'rm -f $TMPOUT' 0
export CDPATH=''


we_have_cmd() {
cmd=$1
for dir in $(echo "$PATH" | tr ':' ' ')
do
if
[ -f $dir/$cmd -a -x $dir/$cmd ]
then
return 0
fi
done
return 1
}

run_regression_test() {
test=$1
varname=$2
TMPOUT=/tmp/assim_proj_test/$$.${test}.testout
TESTNAME=$TESTDIR/$test
TESTFILE=$INPUTDIR/$test
OUTFILE=$OUTPUTDIR/$test
cd $INPUTDIR
if
eval export $varname='$test'
$TESTNAME | sed -e 's%^\( *"host": *\)"[^"]*"\(.*\)$%\1"FAKETESTHOST"\2%' > $TMPOUT 2>&1
then
: OK it thinks it succeeded
if
jsonlint $TMPOUT >/dev/null
then
: OK
else
jsonlint -v $TMPOUT
echo "Discovery $test produced invalid JSON - output follows"
cat $OUTFILE
return 1
fi
if
[ ! -f $OUTFILE ]
then
echo "No previous output for agent $test"
cp $TMPOUT $OUTFILE
return 0
fi
jsonlint -f $TMPOUT > $TMPOUT.pretty
jsonlint -f $OUTFILE > $OUTFILE.pretty
if
cmp $TMPOUT.pretty $OUTFILE.pretty
then
: "They're the same - Great!"
# rm -f $TMPOUT.pretty $OUTFILE.pretty
else
echo "ERROR: Discovery output $test was incorrect (has changed)."
echo "Diff -u follows"
diff -u $OUTFILE.pretty $TMPOUT.pretty
# rm -f $TMPOUT.pretty $OUTFILE.pretty
return 1
fi
else
rc=$?
echo "ERROR: Discovery test $test failed - output follows."
cat $TMPOUT
echo "END OF TEST $test FAILURE OUTPUT"
return 1
fi
if
eval export $varname='/tmp/foo/bar/no-such-file'
$TESTNAME > $TMPOUT 2>&1
then
if
jsonlint $TMPOUT >/dev/null
then
if
we_have_cmd 'jq'
then
ERROUT=$(jq --ascii-output --raw-output .data.NODATA < $TMPOUT)
case $ERROUT in
*ERROR*) : OK;;
*) echo "Discovery failure test produced incorrect result [$ERROUT]"
return 1;;
esac
fi
else
jsonlint -v $TMPOUT
echo "Discovery failure $test produced invalid JSON - output follows"
cat $TMPOUT
return 1
fi
else
echo "ERROR: Discovery failure $test exited with return code $?"
return 1
fi
echo "Discovery test $test succeeded."
return 0
}

testlines='auditd_conf AUDITD_CONFIG
findmnt FINDMNT_TEST_DATA
login_defs LOGIN_DEFS_CONFIG
mdadm MDADM_CONFIG
nsswitch NSSWITCH_CONFIG
pam PAM_DIRECTORY
partitions PROC_PARTITIONS
sshd SSHD_CONFIG
sudoers SUDOERS_FILE'
#passwd PASSWD_FILE'

echo "$testlines" |
(
failcount=0
while
read testname envname
do
run_regression_test "$testname" "$envname"
failcount=$(expr $failcount + $?)
done
exit $failcount
)
exit $?
1 change: 1 addition & 0 deletions testcode/test_discovery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ mdadm MDADM_CONFIG
nsswitch NSSWITCH_CONFIG
pam PAM_DIRECTORY
partitions PROC_PARTITIONS
passwd PASSWD_FILE
sshd SSHD_CONFIG
sudoers SUDOERS_FILE'

Expand Down
Loading