|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright (C) 2015, Benjamin Drung <benjamin.drung@profitbricks.com> |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining |
| 6 | +# a copy of this software and associated documentation files (the |
| 7 | +# "Software"), to deal in the Software without restriction, including |
| 8 | +# without limitation the rights to use, copy, modify, merge, publish, |
| 9 | +# distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | +# permit persons to whom the Software is furnished to do so, subject to |
| 11 | +# the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included |
| 14 | +# in all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 | +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 | +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 | +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + |
| 24 | +COMMAND="${0%/*}/test/restricted-ssh-commands" |
| 25 | + |
| 26 | +runCommand() { |
| 27 | + local ssh_param="$1" |
| 28 | + local param="$2" |
| 29 | + local exp_stdout="$3" |
| 30 | + local exp_stderr="$4" |
| 31 | + local exp_retval=$5 |
| 32 | + local stdoutF="${SHUNIT_TMPDIR}/stdout" |
| 33 | + local stderrF="${SHUNIT_TMPDIR}/stderr" |
| 34 | + eval TEST_ROOT=${SHUNIT_TMPDIR} SSH_ORIGINAL_COMMAND="${ssh_param}" "${COMMAND} $param" > ${stdoutF} 2> ${stderrF} |
| 35 | + retval=$? |
| 36 | + assertEquals "standard output of ${COMMAND} $param\n" "$exp_stdout" "$(cat ${stdoutF})" |
| 37 | + assertEquals "error output of ${COMMAND} $param\n" "$exp_stderr" "$(cat ${stderrF})" |
| 38 | + assertEquals "return value of ${COMMAND} $param\n" $exp_retval $retval |
| 39 | +} |
| 40 | + |
| 41 | +add_rule() { |
| 42 | + local config_name="$1" |
| 43 | + local config_content="$2" |
| 44 | + mkdir -p "${SHUNIT_TMPDIR}/etc/restricted-ssh-commands" |
| 45 | + printf "%s\n" "$config_content" >> "${SHUNIT_TMPDIR}/etc/restricted-ssh-commands/${config_name}" |
| 46 | +} |
| 47 | + |
| 48 | +success() { |
| 49 | + runCommand "$1" "$2" "$3" "" ${4-0} |
| 50 | +} |
| 51 | + |
| 52 | +failure() { |
| 53 | + runCommand "$1" "$2" "" "$3" ${4-124} |
| 54 | +} |
| 55 | + |
| 56 | +tearDown() { |
| 57 | + rm -rf ${SHUNIT_TMPDIR}/etc |
| 58 | +} |
| 59 | + |
| 60 | +test_missing_config() { |
| 61 | + failure "true" "foo" "restricted-ssh-commands: No configuration in ${SHUNIT_TMPDIR}/etc/restricted-ssh-commands/foo. All commands are denied." 125 |
| 62 | +} |
| 63 | + |
| 64 | +test_empty_config() { |
| 65 | + add_rule "foo" "" |
| 66 | + failure "true" "foo" "restricted-ssh-commands: Empty configuration in ${SHUNIT_TMPDIR}/etc/restricted-ssh-commands/foo. All commands are denied." 125 |
| 67 | +} |
| 68 | + |
| 69 | +test_single_rule_config() { |
| 70 | + add_rule "$(id -un)" "^echo" |
| 71 | + failure "true" "" "restricted-ssh-commands: Rejecting command \"true\". It does not match the one allow rule in ${SHUNIT_TMPDIR}/etc/restricted-ssh-commands/$(id -un)." |
| 72 | +} |
| 73 | + |
| 74 | +test_two_rules_config() { |
| 75 | + add_rule "bar" "^echo" |
| 76 | + add_rule "bar" "^false" |
| 77 | + failure "true" "bar" "restricted-ssh-commands: Rejecting command \"true\". It does not match any of the 2 allow rules in ${SHUNIT_TMPDIR}/etc/restricted-ssh-commands/bar." |
| 78 | +} |
| 79 | + |
| 80 | +test_matching_rule() { |
| 81 | + add_rule "foo" '^true$' |
| 82 | + success "true" "foo" "" |
| 83 | +} |
| 84 | + |
| 85 | +test_user_config() { |
| 86 | + add_rule "$(id -un)" '^true$' |
| 87 | + success "true" "" "" |
| 88 | +} |
| 89 | + |
| 90 | +test_custom_config() { |
| 91 | + add_rule "foobar" '^true$' |
| 92 | + success "true" "foobar" "" |
| 93 | +} |
| 94 | + |
| 95 | +test_failing_command() { |
| 96 | + add_rule "bla" '^true$' |
| 97 | + add_rule "bla" '^false$' |
| 98 | + failure "false" "bla" "" 1 |
| 99 | +} |
| 100 | + |
| 101 | +. shunit2 |
0 commit comments